1
2
3
4
5
6 using System;
7 using System.Collections;
8
9 namespace System.Net.Http
10 {
11 public static class HttpParser
12 {
13 public static HttpHeader ParseHeader(const string& header)
14 {
15 try
16 {
17 HttpLexer lexer(ToUtf32(header), "", 0);
18 return HttpHeaderParser.Parse(lexer);
19 }
20 catch (const Exception& ex)
21 {
22 throw Exception("error: could not parse header '" + header + "': " + ex.Message());
23 }
24 return HttpHeader();
25 }
26 public static HttpStatus ParseStatus(const string& statusLine)
27 {
28 try
29 {
30 HttpLexer lexer(ToUtf32(statusLine), "", 0);
31 return HttpStatusLineParser.Parse(lexer);
32 }
33 catch (const Exception& ex)
34 {
35 throw Exception("error: could not parse status '" + statusLine + "': " + ex.Message());
36 }
37 return HttpStatus();
38 }
39 public static void ParseChunkHeader(const string& chunkHeader, ulong* chunkSize, ChunkExtensionAdder* adder)
40 {
41 try
42 {
43 HttpLexer lexer(ToUtf32(chunkHeader), "", 0);
44 HttpChunkHeaderParser.Parse(lexer, chunkSize, adder);
45 }
46 catch (const Exception& ex)
47 {
48 throw Exception("error: could not parse chunk header '" + chunkHeader + "': " + ex.Message());
49 }
50 }
51 public static void ParseFieldValue(const string& fieldValue, List<HttpFieldValue>* values)
52 {
53 try
54 {
55 HttpLexer lexer(ToUtf32(fieldValue), "", 0);
56 HttpFieldValueParser.Parse(lexer, values);
57 }
58 catch (const Exception& ex)
59 {
60 throw Exception("error: coulnd not parse field value '" + fieldValue + "': " + ex.Message());
61 }
62 }
63 public static MimeType ParseMediaType(const string& fieldValue)
64 {
65 try
66 {
67 HttpLexer lexer(ToUtf32(fieldValue), "", 0);
68 MimeType mimeType;
69 HttpMediaTypeParser.Parse(lexer, &mimeType);
70 return mimeType;
71 }
72 catch (const Exception& ex)
73 {
74 throw Exception("could not parse media type '" + fieldValue + "': " + ex.Message());
75 }
76 return MimeType();
77 }
78 public static DateTime ParseDate(const string& fieldValue)
79 {
80 try
81 {
82 TrivialLexer lexer(ToUtf32(fieldValue), "", 0);
83 return HttpDateParser.Parse(lexer);
84 }
85 catch (const Exception& ex)
86 {
87 throw Exception("could not parse date '" + fieldValue + "':" + ex.Message());
88 }
89 return DateTime();
90 }
91 }
92 }