]>
Commit | Line | Data |
---|---|---|
f9a14916 JW |
1 | /** @file\r |
2 | This library is used to share code between UEFI network stack modules.\r | |
3 | It provides the helper routines to parse the HTTP message byte stream.\r | |
4 | \r | |
5 | Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r | |
6 | This program and the accompanying materials\r | |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at<BR>\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | #include <Uefi.h>\r | |
17 | #include <Library/NetLib.h>\r | |
18 | #include <Library/HttpLib.h>\r | |
19 | #include <Library/BaseLib.h>\r | |
20 | #include <Library/DebugLib.h>\r | |
21 | #include <Library/MemoryAllocationLib.h>\r | |
22 | #include <Library/UefiBootServicesTableLib.h>\r | |
23 | \r | |
24 | #define BIT(x) (1 << x)\r | |
25 | \r | |
26 | #define NET_IS_HEX_CHAR(Ch) \\r | |
27 | ((('0' <= (Ch)) && ((Ch) <= '9')) || \\r | |
28 | (('A' <= (Ch)) && ((Ch) <= 'F')) || \\r | |
29 | (('a' <= (Ch)) && ((Ch) <= 'f')))\r | |
30 | \r | |
31 | //\r | |
32 | // Field index of the HTTP URL parse result.\r | |
33 | //\r | |
34 | #define HTTP_URI_FIELD_SCHEME 0\r | |
35 | #define HTTP_URI_FIELD_AUTHORITY 1\r | |
36 | #define HTTP_URI_FIELD_PATH 2\r | |
37 | #define HTTP_URI_FIELD_QUERY 3\r | |
38 | #define HTTP_URI_FIELD_FRAGMENT 4\r | |
39 | #define HTTP_URI_FIELD_USERINFO 5\r | |
40 | #define HTTP_URI_FIELD_HOST 6\r | |
41 | #define HTTP_URI_FIELD_PORT 7\r | |
42 | #define HTTP_URI_FIELD_MAX 8\r | |
43 | \r | |
44 | //\r | |
45 | // Structure to store the parse result of a HTTP URL.\r | |
46 | //\r | |
47 | typedef struct {\r | |
48 | UINT32 Offset;\r | |
49 | UINT32 Length;\r | |
50 | } HTTP_URL_FILED_DATA;\r | |
51 | \r | |
52 | typedef struct {\r | |
53 | UINT16 FieldBitMap;\r | |
54 | HTTP_URL_FILED_DATA FieldData[HTTP_URI_FIELD_MAX];\r | |
55 | } HTTP_URL_PARSER;\r | |
56 | \r | |
57 | typedef enum {\r | |
58 | UrlParserUrlStart,\r | |
59 | UrlParserScheme,\r | |
60 | UrlParserSchemeColon, // ":"\r | |
61 | UrlParserSchemeColonSlash, // ":/"\r | |
62 | UrlParserSchemeColonSlashSlash, // "://"\r | |
63 | UrlParserAuthority,\r | |
64 | UrlParserAtInAuthority,\r | |
65 | UrlParserPath,\r | |
66 | UrlParserQueryStart, // "?"\r | |
67 | UrlParserQuery,\r | |
68 | UrlParserFragmentStart, // "#"\r | |
69 | UrlParserFragment,\r | |
70 | UrlParserUserInfo,\r | |
71 | UrlParserHostStart, // "@"\r | |
72 | UrlParserHost,\r | |
e91cc29d | 73 | UrlParserHostIpv6, // "["(Ipv6 address) "]"\r |
f9a14916 JW |
74 | UrlParserPortStart, // ":"\r |
75 | UrlParserPort,\r | |
76 | UrlParserStateMax\r | |
77 | } HTTP_URL_PARSE_STATE;\r | |
78 | \r | |
79 | /**\r | |
80 | Decode a percent-encoded URI component to the ASCII character.\r | |
81 | \r | |
82 | Decode the input component in Buffer according to RFC 3986. The caller is responsible to make \r | |
83 | sure ResultBuffer points to a buffer with size equal or greater than ((AsciiStrSize (Buffer))\r | |
84 | in bytes. \r | |
85 | \r | |
86 | @param[in] Buffer The pointer to a percent-encoded URI component.\r | |
87 | @param[in] BufferLength Length of Buffer in bytes.\r | |
88 | @param[out] ResultBuffer Point to the buffer to store the decode result.\r | |
89 | @param[out] ResultLength Length of decoded string in ResultBuffer in bytes.\r | |
90 | \r | |
91 | @retval EFI_SUCCESS Successfully decoded the URI.\r | |
92 | @retval EFI_INVALID_PARAMETER Buffer is not a valid percent-encoded string.\r | |
93 | \r | |
94 | **/\r | |
95 | EFI_STATUS\r | |
96 | EFIAPI\r | |
97 | UriPercentDecode (\r | |
98 | IN CHAR8 *Buffer,\r | |
99 | IN UINT32 BufferLength,\r | |
100 | OUT CHAR8 *ResultBuffer,\r | |
101 | OUT UINT32 *ResultLength\r | |
102 | )\r | |
103 | {\r | |
104 | UINTN Index;\r | |
105 | UINTN Offset;\r | |
106 | CHAR8 HexStr[3];\r | |
107 | \r | |
108 | if (Buffer == NULL || BufferLength == 0 || ResultBuffer == NULL) {\r | |
109 | return EFI_INVALID_PARAMETER;\r | |
110 | }\r | |
111 | \r | |
112 | Index = 0;\r | |
113 | Offset = 0;\r | |
114 | HexStr[2] = '\0';\r | |
115 | while (Index < BufferLength) {\r | |
116 | if (Buffer[Index] == '%') {\r | |
117 | if (!NET_IS_HEX_CHAR (Buffer[Index+1]) || !NET_IS_HEX_CHAR (Buffer[Index+2])) {\r | |
118 | return EFI_INVALID_PARAMETER;\r | |
119 | }\r | |
120 | HexStr[0] = Buffer[Index+1];\r | |
121 | HexStr[1] = Buffer[Index+2];\r | |
122 | ResultBuffer[Offset] = (CHAR8) AsciiStrHexToUintn (HexStr);\r | |
123 | Index += 3;\r | |
124 | } else {\r | |
125 | ResultBuffer[Offset] = Buffer[Index];\r | |
126 | Index++;\r | |
127 | }\r | |
128 | Offset++;\r | |
129 | }\r | |
130 | \r | |
131 | *ResultLength = (UINT32) Offset;\r | |
132 | \r | |
133 | return EFI_SUCCESS;\r | |
134 | }\r | |
135 | \r | |
136 | /**\r | |
137 | This function return the updated state accroding to the input state and next character of\r | |
138 | the authority.\r | |
139 | \r | |
140 | @param[in] Char Next character.\r | |
141 | @param[in] State Current value of the parser state machine.\r | |
e91cc29d ZL |
142 | @param[in] IsRightBracket TRUE if there is an sign ']' in the authority component and \r |
143 | indicates the next part is ':' before Port. \r | |
f9a14916 JW |
144 | \r |
145 | @return Updated state value.\r | |
146 | **/\r | |
147 | HTTP_URL_PARSE_STATE\r | |
148 | NetHttpParseAuthorityChar (\r | |
149 | IN CHAR8 Char,\r | |
e91cc29d ZL |
150 | IN HTTP_URL_PARSE_STATE State,\r |
151 | IN BOOLEAN *IsRightBracket\r | |
f9a14916 JW |
152 | )\r |
153 | {\r | |
154 | \r | |
155 | //\r | |
156 | // RFC 3986:\r | |
157 | // The authority component is preceded by a double slash ("//") and is\r | |
158 | // terminated by the next slash ("/"), question mark ("?"), or number\r | |
159 | // sign ("#") character, or by the end of the URI.\r | |
160 | //\r | |
161 | if (Char == ' ' || Char == '\r' || Char == '\n') {\r | |
162 | return UrlParserStateMax;\r | |
163 | }\r | |
164 | \r | |
165 | //\r | |
166 | // authority = [ userinfo "@" ] host [ ":" port ]\r | |
167 | //\r | |
168 | switch (State) {\r | |
169 | case UrlParserUserInfo:\r | |
170 | if (Char == '@') {\r | |
171 | return UrlParserHostStart;\r | |
172 | }\r | |
173 | break;\r | |
174 | \r | |
175 | case UrlParserHost:\r | |
e91cc29d ZL |
176 | case UrlParserHostStart: \r |
177 | if (Char == '[') {\r | |
178 | return UrlParserHostIpv6;\r | |
179 | }\r | |
180 | \r | |
f9a14916 JW |
181 | if (Char == ':') {\r |
182 | return UrlParserPortStart;\r | |
183 | }\r | |
e91cc29d | 184 | \r |
f9a14916 | 185 | return UrlParserHost;\r |
e91cc29d ZL |
186 | \r |
187 | case UrlParserHostIpv6: \r | |
188 | if (Char == ']') {\r | |
189 | *IsRightBracket = TRUE;\r | |
190 | }\r | |
191 | \r | |
b8b65a03 | 192 | if (Char == ':' && *IsRightBracket) {\r |
e91cc29d ZL |
193 | return UrlParserPortStart;\r |
194 | }\r | |
195 | return UrlParserHostIpv6;\r | |
196 | \r | |
f9a14916 JW |
197 | case UrlParserPort:\r |
198 | case UrlParserPortStart:\r | |
199 | return UrlParserPort;\r | |
200 | \r | |
201 | default:\r | |
202 | break;\r | |
203 | }\r | |
204 | \r | |
205 | return State;\r | |
206 | }\r | |
207 | \r | |
208 | /**\r | |
209 | This function parse the authority component of the input URL and update the parser.\r | |
210 | \r | |
211 | @param[in] Url The pointer to a HTTP URL string.\r | |
212 | @param[in] FoundAt TRUE if there is an at sign ('@') in the authority component.\r | |
213 | @param[in, out] UrlParser Pointer to the buffer of the parse result.\r | |
214 | \r | |
215 | @retval EFI_SUCCESS Successfully parse the authority.\r | |
216 | @retval Other Error happened.\r | |
217 | \r | |
218 | **/\r | |
219 | EFI_STATUS\r | |
220 | NetHttpParseAuthority (\r | |
221 | IN CHAR8 *Url,\r | |
222 | IN BOOLEAN FoundAt,\r | |
223 | IN OUT HTTP_URL_PARSER *UrlParser\r | |
224 | )\r | |
225 | {\r | |
226 | CHAR8 *Char;\r | |
227 | CHAR8 *Authority;\r | |
228 | UINT32 Length;\r | |
229 | HTTP_URL_PARSE_STATE State;\r | |
230 | UINT32 Field;\r | |
231 | UINT32 OldField;\r | |
e91cc29d | 232 | BOOLEAN IsrightBracket;\r |
f9a14916 JW |
233 | \r |
234 | ASSERT ((UrlParser->FieldBitMap & BIT (HTTP_URI_FIELD_AUTHORITY)) != 0);\r | |
235 | \r | |
236 | //\r | |
237 | // authority = [ userinfo "@" ] host [ ":" port ]\r | |
238 | //\r | |
239 | if (FoundAt) {\r | |
240 | State = UrlParserUserInfo;\r | |
241 | } else {\r | |
242 | State = UrlParserHost;\r | |
243 | }\r | |
244 | \r | |
e91cc29d | 245 | IsrightBracket = FALSE;\r |
f9a14916 JW |
246 | Field = HTTP_URI_FIELD_MAX;\r |
247 | OldField = Field;\r | |
248 | Authority = Url + UrlParser->FieldData[HTTP_URI_FIELD_AUTHORITY].Offset;\r | |
249 | Length = UrlParser->FieldData[HTTP_URI_FIELD_AUTHORITY].Length;\r | |
250 | for (Char = Authority; Char < Authority + Length; Char++) {\r | |
e91cc29d | 251 | State = NetHttpParseAuthorityChar (*Char, State, &IsrightBracket);\r |
f9a14916 JW |
252 | switch (State) {\r |
253 | case UrlParserStateMax:\r | |
254 | return EFI_INVALID_PARAMETER;\r | |
255 | \r | |
256 | case UrlParserHostStart:\r | |
257 | case UrlParserPortStart:\r | |
258 | continue;\r | |
259 | \r | |
260 | case UrlParserUserInfo:\r | |
261 | Field = HTTP_URI_FIELD_USERINFO;\r | |
262 | break;\r | |
263 | \r | |
264 | case UrlParserHost:\r | |
265 | Field = HTTP_URI_FIELD_HOST;\r | |
266 | break;\r | |
e91cc29d ZL |
267 | \r |
268 | case UrlParserHostIpv6:\r | |
269 | Field = HTTP_URI_FIELD_HOST;\r | |
270 | break;\r | |
f9a14916 JW |
271 | \r |
272 | case UrlParserPort:\r | |
273 | Field = HTTP_URI_FIELD_PORT;\r | |
274 | break;\r | |
275 | \r | |
276 | default:\r | |
277 | ASSERT (FALSE);\r | |
278 | }\r | |
279 | \r | |
280 | //\r | |
281 | // Field not changed, count the length.\r | |
282 | //\r | |
283 | ASSERT (Field < HTTP_URI_FIELD_MAX);\r | |
284 | if (Field == OldField) {\r | |
285 | UrlParser->FieldData[Field].Length++;\r | |
286 | continue;\r | |
287 | }\r | |
288 | \r | |
289 | //\r | |
290 | // New field start\r | |
291 | //\r | |
292 | UrlParser->FieldBitMap |= BIT (Field);\r | |
293 | UrlParser->FieldData[Field].Offset = (UINT32) (Char - Url);\r | |
294 | UrlParser->FieldData[Field].Length = 1;\r | |
295 | OldField = Field;\r | |
296 | }\r | |
297 | \r | |
298 | return EFI_SUCCESS;\r | |
299 | }\r | |
300 | \r | |
301 | /**\r | |
302 | This function return the updated state accroding to the input state and next character of a URL.\r | |
303 | \r | |
304 | @param[in] Char Next character.\r | |
305 | @param[in] State Current value of the parser state machine.\r | |
306 | \r | |
307 | @return Updated state value.\r | |
308 | \r | |
309 | **/\r | |
310 | HTTP_URL_PARSE_STATE\r | |
311 | NetHttpParseUrlChar (\r | |
312 | IN CHAR8 Char,\r | |
313 | IN HTTP_URL_PARSE_STATE State\r | |
314 | )\r | |
315 | {\r | |
316 | if (Char == ' ' || Char == '\r' || Char == '\n') {\r | |
317 | return UrlParserStateMax;\r | |
318 | }\r | |
319 | \r | |
320 | //\r | |
321 | // http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]\r | |
322 | // \r | |
323 | // Request-URI = "*" | absolute-URI | path-absolute | authority\r | |
324 | // \r | |
325 | // absolute-URI = scheme ":" hier-part [ "?" query ]\r | |
326 | // path-absolute = "/" [ segment-nz *( "/" segment ) ]\r | |
327 | // authority = [ userinfo "@" ] host [ ":" port ]\r | |
328 | //\r | |
329 | switch (State) {\r | |
330 | case UrlParserUrlStart:\r | |
331 | if (Char == '*' || Char == '/') {\r | |
332 | return UrlParserPath;\r | |
333 | }\r | |
334 | return UrlParserScheme;\r | |
335 | \r | |
336 | case UrlParserScheme:\r | |
337 | if (Char == ':') {\r | |
338 | return UrlParserSchemeColon;\r | |
339 | }\r | |
340 | break;\r | |
341 | \r | |
342 | case UrlParserSchemeColon:\r | |
343 | if (Char == '/') {\r | |
344 | return UrlParserSchemeColonSlash;\r | |
345 | }\r | |
346 | break;\r | |
347 | \r | |
348 | case UrlParserSchemeColonSlash:\r | |
349 | if (Char == '/') {\r | |
350 | return UrlParserSchemeColonSlashSlash;\r | |
351 | }\r | |
352 | break;\r | |
353 | \r | |
354 | case UrlParserAtInAuthority:\r | |
355 | if (Char == '@') {\r | |
356 | return UrlParserStateMax;\r | |
357 | }\r | |
358 | \r | |
359 | case UrlParserAuthority:\r | |
360 | case UrlParserSchemeColonSlashSlash:\r | |
361 | if (Char == '@') {\r | |
362 | return UrlParserAtInAuthority;\r | |
363 | }\r | |
364 | if (Char == '/') {\r | |
365 | return UrlParserPath;\r | |
366 | }\r | |
367 | if (Char == '?') {\r | |
368 | return UrlParserQueryStart;\r | |
369 | }\r | |
370 | if (Char == '#') {\r | |
371 | return UrlParserFragmentStart;\r | |
372 | }\r | |
373 | return UrlParserAuthority;\r | |
374 | \r | |
375 | case UrlParserPath:\r | |
376 | if (Char == '?') {\r | |
377 | return UrlParserQueryStart;\r | |
378 | }\r | |
379 | if (Char == '#') {\r | |
380 | return UrlParserFragmentStart;\r | |
381 | }\r | |
382 | break;\r | |
383 | \r | |
384 | case UrlParserQuery:\r | |
385 | case UrlParserQueryStart:\r | |
386 | if (Char == '#') {\r | |
387 | return UrlParserFragmentStart;\r | |
388 | }\r | |
389 | return UrlParserQuery;\r | |
390 | \r | |
391 | case UrlParserFragmentStart:\r | |
392 | return UrlParserFragment;\r | |
393 | \r | |
394 | default:\r | |
395 | break;\r | |
396 | }\r | |
397 | \r | |
398 | return State;\r | |
399 | }\r | |
400 | /**\r | |
401 | Create a URL parser for the input URL string.\r | |
402 | \r | |
403 | This function will parse and dereference the input HTTP URL into it components. The original\r | |
404 | content of the URL won't be modified and the result will be returned in UrlParser, which can\r | |
405 | be used in other functions like NetHttpUrlGetHostName().\r | |
406 | \r | |
407 | @param[in] Url The pointer to a HTTP URL string.\r | |
408 | @param[in] Length Length of Url in bytes.\r | |
409 | @param[in] IsConnectMethod Whether the Url is used in HTTP CONNECT method or not.\r | |
410 | @param[out] UrlParser Pointer to the returned buffer to store the parse result.\r | |
411 | \r | |
412 | @retval EFI_SUCCESS Successfully dereferenced the HTTP URL.\r | |
413 | @retval EFI_INVALID_PARAMETER UrlParser is NULL or Url is not a valid HTTP URL.\r | |
414 | @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r | |
415 | \r | |
416 | **/\r | |
417 | EFI_STATUS\r | |
418 | EFIAPI\r | |
419 | HttpParseUrl (\r | |
420 | IN CHAR8 *Url,\r | |
421 | IN UINT32 Length,\r | |
422 | IN BOOLEAN IsConnectMethod,\r | |
423 | OUT VOID **UrlParser\r | |
424 | )\r | |
425 | {\r | |
426 | HTTP_URL_PARSE_STATE State;\r | |
427 | CHAR8 *Char;\r | |
428 | UINT32 Field;\r | |
429 | UINT32 OldField;\r | |
430 | BOOLEAN FoundAt;\r | |
431 | EFI_STATUS Status;\r | |
432 | HTTP_URL_PARSER *Parser;\r | |
433 | \r | |
434 | if (Url == NULL || Length == 0 || UrlParser == NULL) {\r | |
435 | return EFI_INVALID_PARAMETER;\r | |
436 | }\r | |
437 | \r | |
438 | Parser = AllocateZeroPool (sizeof (HTTP_URL_PARSER));\r | |
439 | if (Parser == NULL) {\r | |
440 | return EFI_OUT_OF_RESOURCES;\r | |
441 | }\r | |
442 | \r | |
443 | if (IsConnectMethod) {\r | |
444 | //\r | |
445 | // According to RFC 2616, the authority form is only used by the CONNECT method.\r | |
446 | //\r | |
447 | State = UrlParserAuthority;\r | |
448 | } else {\r | |
449 | State = UrlParserUrlStart;\r | |
450 | }\r | |
451 | \r | |
452 | Field = HTTP_URI_FIELD_MAX;\r | |
453 | OldField = Field;\r | |
454 | FoundAt = FALSE;\r | |
455 | for (Char = Url; Char < Url + Length; Char++) {\r | |
456 | //\r | |
457 | // Update state machine accoring to next char.\r | |
458 | //\r | |
459 | State = NetHttpParseUrlChar (*Char, State);\r | |
460 | \r | |
461 | switch (State) {\r | |
462 | case UrlParserStateMax:\r | |
463 | return EFI_INVALID_PARAMETER;\r | |
464 | \r | |
465 | case UrlParserSchemeColon:\r | |
466 | case UrlParserSchemeColonSlash:\r | |
467 | case UrlParserSchemeColonSlashSlash:\r | |
468 | case UrlParserQueryStart:\r | |
469 | case UrlParserFragmentStart:\r | |
470 | //\r | |
471 | // Skip all the delimiting char: "://" "?" "@"\r | |
472 | //\r | |
473 | continue;\r | |
474 | \r | |
475 | case UrlParserScheme:\r | |
476 | Field = HTTP_URI_FIELD_SCHEME;\r | |
477 | break;\r | |
478 | \r | |
479 | case UrlParserAtInAuthority:\r | |
480 | FoundAt = TRUE;\r | |
481 | case UrlParserAuthority:\r | |
482 | Field = HTTP_URI_FIELD_AUTHORITY;\r | |
483 | break;\r | |
484 | \r | |
485 | case UrlParserPath:\r | |
486 | Field = HTTP_URI_FIELD_PATH;\r | |
487 | break;\r | |
488 | \r | |
489 | case UrlParserQuery:\r | |
490 | Field = HTTP_URI_FIELD_QUERY;\r | |
491 | break;\r | |
492 | \r | |
493 | case UrlParserFragment:\r | |
494 | Field = HTTP_URI_FIELD_FRAGMENT;\r | |
495 | break;\r | |
496 | \r | |
497 | default:\r | |
498 | ASSERT (FALSE);\r | |
499 | }\r | |
500 | \r | |
501 | //\r | |
502 | // Field not changed, count the length.\r | |
503 | //\r | |
504 | ASSERT (Field < HTTP_URI_FIELD_MAX);\r | |
505 | if (Field == OldField) {\r | |
506 | Parser->FieldData[Field].Length++;\r | |
507 | continue;\r | |
508 | }\r | |
509 | \r | |
510 | //\r | |
511 | // New field start\r | |
512 | //\r | |
513 | Parser->FieldBitMap |= BIT (Field);\r | |
514 | Parser->FieldData[Field].Offset = (UINT32) (Char - Url);\r | |
515 | Parser->FieldData[Field].Length = 1;\r | |
516 | OldField = Field;\r | |
517 | }\r | |
518 | \r | |
519 | //\r | |
520 | // If has authority component, continue to parse the username, host and port.\r | |
521 | //\r | |
522 | if ((Parser->FieldBitMap & BIT (HTTP_URI_FIELD_AUTHORITY)) != 0) {\r | |
523 | Status = NetHttpParseAuthority (Url, FoundAt, Parser);\r | |
524 | if (EFI_ERROR (Status)) {\r | |
525 | return Status;\r | |
526 | }\r | |
527 | }\r | |
528 | \r | |
529 | *UrlParser = Parser;\r | |
530 | return EFI_SUCCESS; \r | |
531 | }\r | |
532 | \r | |
533 | /**\r | |
534 | Get the Hostname from a HTTP URL.\r | |
535 | \r | |
536 | This function will return the HostName according to the Url and previous parse result ,and\r | |
537 | it is the caller's responsibility to free the buffer returned in *HostName.\r | |
538 | \r | |
539 | @param[in] Url The pointer to a HTTP URL string.\r | |
540 | @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().\r | |
541 | @param[out] HostName Pointer to a buffer to store the HostName.\r | |
542 | \r | |
543 | @retval EFI_SUCCESS Successfully get the required component.\r | |
544 | @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.\r | |
545 | @retval EFI_NOT_FOUND No hostName component in the URL.\r | |
546 | @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r | |
547 | \r | |
548 | **/\r | |
549 | EFI_STATUS\r | |
550 | EFIAPI\r | |
551 | HttpUrlGetHostName (\r | |
552 | IN CHAR8 *Url,\r | |
553 | IN VOID *UrlParser,\r | |
554 | OUT CHAR8 **HostName\r | |
555 | )\r | |
556 | {\r | |
557 | CHAR8 *Name;\r | |
558 | EFI_STATUS Status;\r | |
559 | UINT32 ResultLength;\r | |
560 | HTTP_URL_PARSER *Parser;\r | |
561 | \r | |
562 | if (Url == NULL || UrlParser == NULL || HostName == NULL) {\r | |
563 | return EFI_INVALID_PARAMETER;\r | |
564 | }\r | |
565 | \r | |
566 | Parser = (HTTP_URL_PARSER*) UrlParser;\r | |
567 | \r | |
568 | if ((Parser->FieldBitMap & BIT (HTTP_URI_FIELD_HOST)) == 0) {\r | |
569 | return EFI_NOT_FOUND;\r | |
570 | }\r | |
571 | \r | |
572 | Name = AllocatePool (Parser->FieldData[HTTP_URI_FIELD_HOST].Length + 1);\r | |
573 | if (Name == NULL) {\r | |
574 | return EFI_OUT_OF_RESOURCES;\r | |
575 | }\r | |
576 | \r | |
577 | Status = UriPercentDecode (\r | |
578 | Url + Parser->FieldData[HTTP_URI_FIELD_HOST].Offset,\r | |
579 | Parser->FieldData[HTTP_URI_FIELD_HOST].Length,\r | |
580 | Name,\r | |
581 | &ResultLength\r | |
582 | );\r | |
583 | if (EFI_ERROR (Status)) {\r | |
584 | return Status;\r | |
585 | }\r | |
586 | \r | |
587 | Name[ResultLength] = '\0';\r | |
588 | *HostName = Name;\r | |
589 | return EFI_SUCCESS;\r | |
590 | }\r | |
591 | \r | |
592 | \r | |
593 | /**\r | |
594 | Get the IPv4 address from a HTTP URL.\r | |
595 | \r | |
596 | This function will return the IPv4 address according to the Url and previous parse result.\r | |
597 | \r | |
598 | @param[in] Url The pointer to a HTTP URL string.\r | |
599 | @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().\r | |
600 | @param[out] Ip4Address Pointer to a buffer to store the IP address.\r | |
601 | \r | |
602 | @retval EFI_SUCCESS Successfully get the required component.\r | |
603 | @retval EFI_INVALID_PARAMETER Uri is NULL or Ip4Address is NULL or UrlParser is invalid.\r | |
604 | @retval EFI_NOT_FOUND No IPv4 address component in the URL.\r | |
605 | @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r | |
606 | \r | |
607 | **/\r | |
608 | EFI_STATUS\r | |
609 | EFIAPI\r | |
610 | HttpUrlGetIp4 (\r | |
611 | IN CHAR8 *Url,\r | |
612 | IN VOID *UrlParser,\r | |
613 | OUT EFI_IPv4_ADDRESS *Ip4Address\r | |
614 | )\r | |
615 | {\r | |
616 | CHAR8 *Ip4String;\r | |
617 | EFI_STATUS Status;\r | |
618 | UINT32 ResultLength;\r | |
619 | HTTP_URL_PARSER *Parser;\r | |
620 | \r | |
621 | if (Url == NULL || UrlParser == NULL || Ip4Address == NULL) {\r | |
622 | return EFI_INVALID_PARAMETER;\r | |
623 | }\r | |
624 | \r | |
625 | Parser = (HTTP_URL_PARSER*) UrlParser;\r | |
626 | \r | |
627 | if ((Parser->FieldBitMap & BIT (HTTP_URI_FIELD_HOST)) == 0) {\r | |
628 | return EFI_INVALID_PARAMETER;\r | |
629 | }\r | |
630 | \r | |
631 | Ip4String = AllocatePool (Parser->FieldData[HTTP_URI_FIELD_HOST].Length + 1);\r | |
632 | if (Ip4String == NULL) {\r | |
633 | return EFI_OUT_OF_RESOURCES;\r | |
634 | }\r | |
635 | \r | |
636 | Status = UriPercentDecode (\r | |
637 | Url + Parser->FieldData[HTTP_URI_FIELD_HOST].Offset,\r | |
638 | Parser->FieldData[HTTP_URI_FIELD_HOST].Length,\r | |
639 | Ip4String,\r | |
640 | &ResultLength\r | |
641 | );\r | |
642 | if (EFI_ERROR (Status)) {\r | |
643 | return Status;\r | |
644 | }\r | |
645 | \r | |
646 | Ip4String[ResultLength] = '\0';\r | |
647 | Status = NetLibAsciiStrToIp4 (Ip4String, Ip4Address);\r | |
648 | FreePool (Ip4String);\r | |
649 | \r | |
650 | return Status;\r | |
651 | }\r | |
652 | \r | |
653 | /**\r | |
654 | Get the IPv6 address from a HTTP URL.\r | |
655 | \r | |
656 | This function will return the IPv6 address according to the Url and previous parse result.\r | |
657 | \r | |
658 | @param[in] Url The pointer to a HTTP URL string.\r | |
659 | @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().\r | |
660 | @param[out] Ip6Address Pointer to a buffer to store the IP address.\r | |
661 | \r | |
662 | @retval EFI_SUCCESS Successfully get the required component.\r | |
663 | @retval EFI_INVALID_PARAMETER Uri is NULL or Ip6Address is NULL or UrlParser is invalid.\r | |
664 | @retval EFI_NOT_FOUND No IPv6 address component in the URL.\r | |
665 | @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r | |
666 | \r | |
667 | **/\r | |
668 | EFI_STATUS\r | |
669 | EFIAPI\r | |
670 | HttpUrlGetIp6 (\r | |
671 | IN CHAR8 *Url,\r | |
672 | IN VOID *UrlParser,\r | |
673 | OUT EFI_IPv6_ADDRESS *Ip6Address\r | |
674 | )\r | |
675 | {\r | |
676 | CHAR8 *Ip6String;\r | |
677 | CHAR8 *Ptr;\r | |
678 | UINT32 Length;\r | |
679 | EFI_STATUS Status;\r | |
680 | UINT32 ResultLength;\r | |
681 | HTTP_URL_PARSER *Parser;\r | |
682 | \r | |
683 | if (Url == NULL || UrlParser == NULL || Ip6Address == NULL) {\r | |
684 | return EFI_INVALID_PARAMETER;\r | |
685 | }\r | |
686 | \r | |
687 | Parser = (HTTP_URL_PARSER*) UrlParser;\r | |
688 | \r | |
689 | if ((Parser->FieldBitMap & BIT (HTTP_URI_FIELD_HOST)) == 0) {\r | |
690 | return EFI_INVALID_PARAMETER;\r | |
691 | }\r | |
692 | \r | |
693 | //\r | |
694 | // IP-literal = "[" ( IPv6address / IPvFuture ) "]"\r | |
695 | //\r | |
696 | Length = Parser->FieldData[HTTP_URI_FIELD_HOST].Length;\r | |
697 | if (Length < 2) {\r | |
698 | return EFI_INVALID_PARAMETER;\r | |
699 | }\r | |
700 | \r | |
701 | Ptr = Url + Parser->FieldData[HTTP_URI_FIELD_HOST].Offset;\r | |
702 | if ((Ptr[0] != '[') || (Ptr[Length - 1] != ']')) {\r | |
703 | return EFI_INVALID_PARAMETER;\r | |
704 | }\r | |
705 | \r | |
706 | Ip6String = AllocatePool (Length);\r | |
707 | if (Ip6String == NULL) {\r | |
708 | return EFI_OUT_OF_RESOURCES;\r | |
709 | }\r | |
710 | \r | |
711 | Status = UriPercentDecode (\r | |
712 | Ptr + 1,\r | |
713 | Length - 2,\r | |
714 | Ip6String,\r | |
715 | &ResultLength\r | |
716 | );\r | |
717 | if (EFI_ERROR (Status)) {\r | |
718 | return Status;\r | |
719 | }\r | |
720 | \r | |
721 | Ip6String[ResultLength] = '\0';\r | |
722 | Status = NetLibAsciiStrToIp6 (Ip6String, Ip6Address);\r | |
723 | FreePool (Ip6String);\r | |
724 | \r | |
725 | return Status;\r | |
726 | }\r | |
727 | \r | |
728 | /**\r | |
729 | Get the port number from a HTTP URL.\r | |
730 | \r | |
731 | This function will return the port number according to the Url and previous parse result.\r | |
732 | \r | |
733 | @param[in] Url The pointer to a HTTP URL string.\r | |
734 | @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().\r | |
735 | @param[out] Port Pointer to a buffer to store the port number.\r | |
736 | \r | |
737 | @retval EFI_SUCCESS Successfully get the required component.\r | |
738 | @retval EFI_INVALID_PARAMETER Uri is NULL or Port is NULL or UrlParser is invalid.\r | |
739 | @retval EFI_NOT_FOUND No port number in the URL.\r | |
740 | @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r | |
741 | \r | |
742 | **/\r | |
743 | EFI_STATUS\r | |
744 | EFIAPI\r | |
745 | HttpUrlGetPort (\r | |
746 | IN CHAR8 *Url,\r | |
747 | IN VOID *UrlParser,\r | |
748 | OUT UINT16 *Port\r | |
749 | )\r | |
750 | {\r | |
751 | CHAR8 *PortString;\r | |
752 | EFI_STATUS Status;\r | |
753 | UINT32 ResultLength;\r | |
754 | HTTP_URL_PARSER *Parser;\r | |
755 | \r | |
756 | if (Url == NULL || UrlParser == NULL || Port == NULL) {\r | |
757 | return EFI_INVALID_PARAMETER;\r | |
758 | }\r | |
759 | \r | |
760 | Parser = (HTTP_URL_PARSER*) UrlParser;\r | |
761 | \r | |
762 | if ((Parser->FieldBitMap & BIT (HTTP_URI_FIELD_PORT)) == 0) {\r | |
763 | return EFI_INVALID_PARAMETER;\r | |
764 | }\r | |
765 | \r | |
766 | PortString = AllocatePool (Parser->FieldData[HTTP_URI_FIELD_PORT].Length + 1);\r | |
767 | if (PortString == NULL) {\r | |
768 | return EFI_OUT_OF_RESOURCES;\r | |
769 | }\r | |
770 | \r | |
771 | Status = UriPercentDecode (\r | |
772 | Url + Parser->FieldData[HTTP_URI_FIELD_PORT].Offset,\r | |
773 | Parser->FieldData[HTTP_URI_FIELD_PORT].Length,\r | |
774 | PortString,\r | |
775 | &ResultLength\r | |
776 | );\r | |
777 | if (EFI_ERROR (Status)) {\r | |
778 | return Status;\r | |
779 | }\r | |
780 | \r | |
781 | PortString[ResultLength] = '\0';\r | |
782 | *Port = (UINT16) AsciiStrDecimalToUintn (Url + Parser->FieldData[HTTP_URI_FIELD_PORT].Offset);\r | |
783 | \r | |
784 | return EFI_SUCCESS;\r | |
785 | }\r | |
786 | \r | |
787 | /**\r | |
788 | Release the resource of the URL parser.\r | |
789 | \r | |
790 | @param[in] UrlParser Pointer to the parser.\r | |
791 | \r | |
792 | **/\r | |
793 | VOID\r | |
794 | EFIAPI\r | |
795 | HttpUrlFreeParser (\r | |
796 | IN VOID *UrlParser\r | |
797 | )\r | |
798 | {\r | |
799 | FreePool (UrlParser);\r | |
800 | }\r | |
801 | \r | |
802 | /**\r | |
803 | Find a specified header field according to the field name.\r | |
804 | \r | |
805 | @param[in] HeaderCount Number of HTTP header structures in Headers list. \r | |
806 | @param[in] Headers Array containing list of HTTP headers.\r | |
807 | @param[in] FieldName Null terminated string which describes a field name. \r | |
808 | \r | |
809 | @return Pointer to the found header or NULL.\r | |
810 | \r | |
811 | **/\r | |
812 | EFI_HTTP_HEADER *\r | |
813 | HttpIoFindHeader (\r | |
814 | IN UINTN HeaderCount,\r | |
815 | IN EFI_HTTP_HEADER *Headers,\r | |
816 | IN CHAR8 *FieldName\r | |
817 | )\r | |
818 | {\r | |
819 | UINTN Index;\r | |
820 | \r | |
821 | if (HeaderCount == 0 || Headers == NULL || FieldName == NULL) {\r | |
822 | return NULL;\r | |
823 | }\r | |
824 | \r | |
825 | for (Index = 0; Index < HeaderCount; Index++){\r | |
826 | //\r | |
827 | // Field names are case-insensitive (RFC 2616).\r | |
828 | //\r | |
829 | if (AsciiStriCmp (Headers[Index].FieldName, FieldName) == 0) {\r | |
830 | return &Headers[Index];\r | |
831 | }\r | |
832 | }\r | |
833 | return NULL;\r | |
834 | }\r | |
835 | \r | |
836 | typedef enum {\r | |
837 | BodyParserBodyStart,\r | |
838 | BodyParserBodyIdentity,\r | |
839 | BodyParserChunkSizeStart,\r | |
840 | BodyParserChunkSize,\r | |
841 | BodyParserChunkSizeEndCR,\r | |
842 | BodyParserChunkExtStart,\r | |
843 | BodyParserChunkDataStart,\r | |
844 | BodyParserChunkDataEnd,\r | |
845 | BodyParserChunkDataEndCR,\r | |
846 | BodyParserTrailer,\r | |
847 | BodyParserLastCRLF,\r | |
848 | BodyParserLastCRLFEnd,\r | |
849 | BodyParserComplete,\r | |
850 | BodyParserStateMax\r | |
851 | } HTTP_BODY_PARSE_STATE;\r | |
852 | \r | |
853 | typedef struct {\r | |
854 | BOOLEAN IgnoreBody; // "MUST NOT" include a message-body\r | |
855 | BOOLEAN IsChunked; // "chunked" transfer-coding.\r | |
856 | BOOLEAN ContentLengthIsValid;\r | |
857 | UINTN ContentLength; // Entity length (not the message-body length), invalid until ContentLengthIsValid is TRUE\r | |
858 | \r | |
859 | HTTP_BODY_PARSER_CALLBACK Callback;\r | |
860 | VOID *Context;\r | |
861 | UINTN ParsedBodyLength;\r | |
862 | HTTP_BODY_PARSE_STATE State;\r | |
863 | UINTN CurrentChunkSize;\r | |
864 | UINTN CurrentChunkParsedSize;\r | |
865 | } HTTP_BODY_PARSER;\r | |
866 | \r | |
867 | /**\r | |
868 | \r | |
869 | Convert an Ascii char to its uppercase.\r | |
870 | \r | |
871 | @param[in] Char Ascii character.\r | |
872 | \r | |
873 | @return Uppercase value of the input Char.\r | |
874 | \r | |
875 | **/\r | |
876 | CHAR8\r | |
877 | HttpIoCharToUpper (\r | |
878 | IN CHAR8 Char\r | |
879 | )\r | |
880 | {\r | |
881 | if (Char >= 'a' && Char <= 'z') {\r | |
882 | return Char - ('a' - 'A');\r | |
883 | }\r | |
884 | \r | |
885 | return Char;\r | |
886 | }\r | |
887 | \r | |
888 | /**\r | |
889 | Convert an hexadecimal char to a value of type UINTN.\r | |
890 | \r | |
891 | @param[in] Char Ascii character.\r | |
892 | \r | |
893 | @return Value translated from Char.\r | |
894 | \r | |
895 | **/\r | |
896 | UINTN\r | |
897 | HttpIoHexCharToUintn (\r | |
898 | IN CHAR8 Char\r | |
899 | )\r | |
900 | {\r | |
901 | if (Char >= '0' && Char <= '9') {\r | |
902 | return Char - '0';\r | |
903 | }\r | |
904 | \r | |
905 | return (10 + HttpIoCharToUpper (Char) - 'A');\r | |
906 | }\r | |
907 | \r | |
908 | /**\r | |
909 | Get the value of the content length if there is a "Content-Length" header.\r | |
910 | \r | |
911 | @param[in] HeaderCount Number of HTTP header structures in Headers.\r | |
912 | @param[in] Headers Array containing list of HTTP headers.\r | |
913 | @param[out] ContentLength Pointer to save the value of the content length.\r | |
914 | \r | |
915 | @retval EFI_SUCCESS Successfully get the content length.\r | |
916 | @retval EFI_NOT_FOUND No "Content-Length" header in the Headers.\r | |
917 | \r | |
918 | **/\r | |
919 | EFI_STATUS\r | |
920 | HttpIoParseContentLengthHeader (\r | |
921 | IN UINTN HeaderCount,\r | |
922 | IN EFI_HTTP_HEADER *Headers,\r | |
923 | OUT UINTN *ContentLength\r | |
924 | )\r | |
925 | {\r | |
926 | EFI_HTTP_HEADER *Header;\r | |
927 | \r | |
928 | Header = HttpIoFindHeader (HeaderCount, Headers, "Content-Length");\r | |
929 | if (Header == NULL) {\r | |
930 | return EFI_NOT_FOUND;\r | |
931 | }\r | |
932 | \r | |
933 | *ContentLength = AsciiStrDecimalToUintn (Header->FieldValue);\r | |
934 | return EFI_SUCCESS;\r | |
935 | }\r | |
936 | \r | |
937 | /**\r | |
938 | \r | |
939 | Check whether the HTTP message is using the "chunked" transfer-coding.\r | |
940 | \r | |
941 | @param[in] HeaderCount Number of HTTP header structures in Headers.\r | |
942 | @param[in] Headers Array containing list of HTTP headers.\r | |
943 | \r | |
944 | @return The message is "chunked" transfer-coding (TRUE) or not (FALSE).\r | |
945 | \r | |
946 | **/\r | |
947 | BOOLEAN\r | |
948 | HttpIoIsChunked (\r | |
949 | IN UINTN HeaderCount,\r | |
950 | IN EFI_HTTP_HEADER *Headers\r | |
951 | )\r | |
952 | {\r | |
953 | EFI_HTTP_HEADER *Header;\r | |
954 | \r | |
955 | \r | |
956 | Header = HttpIoFindHeader (HeaderCount, Headers, "Transfer-Encoding");\r | |
957 | if (Header == NULL) {\r | |
958 | return FALSE;\r | |
959 | }\r | |
960 | \r | |
961 | if (AsciiStriCmp (Header->FieldValue, "identity") != 0) {\r | |
962 | return TRUE;\r | |
963 | }\r | |
964 | \r | |
965 | return FALSE;\r | |
966 | }\r | |
967 | \r | |
968 | /**\r | |
969 | Check whether the HTTP message should have a message-body.\r | |
970 | \r | |
971 | @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.\r | |
972 | @param[in] StatusCode Response status code returned by the remote host.\r | |
973 | \r | |
974 | @return The message should have a message-body (FALSE) or not (TRUE).\r | |
975 | \r | |
976 | **/\r | |
977 | BOOLEAN\r | |
978 | HttpIoNoMessageBody (\r | |
979 | IN EFI_HTTP_METHOD Method,\r | |
980 | IN EFI_HTTP_STATUS_CODE StatusCode\r | |
981 | )\r | |
982 | {\r | |
983 | //\r | |
984 | // RFC 2616:\r | |
985 | // All responses to the HEAD request method\r | |
986 | // MUST NOT include a message-body, even though the presence of entity-\r | |
987 | // header fields might lead one to believe they do. All 1xx\r | |
988 | // (informational), 204 (no content), and 304 (not modified) responses\r | |
989 | // MUST NOT include a message-body. All other responses do include a\r | |
990 | // message-body, although it MAY be of zero length.\r | |
991 | //\r | |
992 | if (Method == HttpMethodHead) {\r | |
993 | return TRUE;\r | |
994 | }\r | |
995 | \r | |
996 | if ((StatusCode == HTTP_STATUS_100_CONTINUE) ||\r | |
997 | (StatusCode == HTTP_STATUS_101_SWITCHING_PROTOCOLS) ||\r | |
998 | (StatusCode == HTTP_STATUS_204_NO_CONTENT) ||\r | |
999 | (StatusCode == HTTP_STATUS_304_NOT_MODIFIED))\r | |
1000 | {\r | |
1001 | return TRUE;\r | |
1002 | }\r | |
1003 | \r | |
1004 | return FALSE;\r | |
1005 | }\r | |
1006 | \r | |
1007 | /**\r | |
1008 | Initialize a HTTP message-body parser.\r | |
1009 | \r | |
1010 | This function will create and initialize a HTTP message parser according to caller provided HTTP message\r | |
1011 | header information. It is the caller's responsibility to free the buffer returned in *UrlParser by HttpFreeMsgParser().\r | |
1012 | \r | |
1013 | @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.\r | |
1014 | @param[in] StatusCode Response status code returned by the remote host.\r | |
1015 | @param[in] HeaderCount Number of HTTP header structures in Headers.\r | |
1016 | @param[in] Headers Array containing list of HTTP headers.\r | |
1017 | @param[in] Callback Callback function that is invoked when parsing the HTTP message-body,\r | |
1018 | set to NULL to ignore all events.\r | |
1019 | @param[in] Context Pointer to the context that will be passed to Callback.\r | |
1020 | @param[out] MsgParser Pointer to the returned buffer to store the message parser.\r | |
1021 | \r | |
1022 | @retval EFI_SUCCESS Successfully initialized the parser.\r | |
1023 | @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r | |
1024 | @retval EFI_INVALID_PARAMETER MsgParser is NULL or HeaderCount is not NULL but Headers is NULL.\r | |
1025 | @retval Others Failed to initialize the parser.\r | |
1026 | \r | |
1027 | **/\r | |
1028 | EFI_STATUS\r | |
1029 | EFIAPI\r | |
1030 | HttpInitMsgParser (\r | |
1031 | IN EFI_HTTP_METHOD Method,\r | |
1032 | IN EFI_HTTP_STATUS_CODE StatusCode,\r | |
1033 | IN UINTN HeaderCount,\r | |
1034 | IN EFI_HTTP_HEADER *Headers,\r | |
1035 | IN HTTP_BODY_PARSER_CALLBACK Callback,\r | |
1036 | IN VOID *Context,\r | |
1037 | OUT VOID **MsgParser\r | |
1038 | )\r | |
1039 | {\r | |
1040 | EFI_STATUS Status;\r | |
1041 | HTTP_BODY_PARSER *Parser;\r | |
1042 | \r | |
1043 | if (HeaderCount != 0 && Headers == NULL) {\r | |
1044 | return EFI_INVALID_PARAMETER;\r | |
1045 | }\r | |
1046 | \r | |
1047 | if (MsgParser == NULL) {\r | |
1048 | return EFI_INVALID_PARAMETER;\r | |
1049 | }\r | |
1050 | \r | |
1051 | Parser = AllocateZeroPool (sizeof (HTTP_BODY_PARSER));\r | |
1052 | if (Parser == NULL) {\r | |
1053 | return EFI_OUT_OF_RESOURCES;\r | |
1054 | }\r | |
1055 | \r | |
1056 | Parser->State = BodyParserBodyStart;\r | |
1057 | \r | |
1058 | //\r | |
1059 | // Determine the message length accroding to RFC 2616.\r | |
1060 | // 1. Check whether the message "MUST NOT" have a message-body.\r | |
1061 | //\r | |
1062 | Parser->IgnoreBody = HttpIoNoMessageBody (Method, StatusCode);\r | |
1063 | //\r | |
1064 | // 2. Check whether the message using "chunked" transfer-coding.\r | |
1065 | //\r | |
1066 | Parser->IsChunked = HttpIoIsChunked (HeaderCount, Headers);\r | |
1067 | //\r | |
1068 | // 3. Check whether the message has a Content-Length header field.\r | |
1069 | //\r | |
1070 | Status = HttpIoParseContentLengthHeader (HeaderCount, Headers, &Parser->ContentLength);\r | |
1071 | if (!EFI_ERROR (Status)) {\r | |
1072 | Parser->ContentLengthIsValid = TRUE;\r | |
1073 | }\r | |
1074 | //\r | |
1075 | // 4. Range header is not supported now, so we won't meet media type "multipart/byteranges".\r | |
1076 | // 5. By server closing the connection\r | |
1077 | //\r | |
1078 | \r | |
1079 | //\r | |
1080 | // Set state to skip body parser if the message shouldn't have a message body.\r | |
1081 | //\r | |
1082 | if (Parser->IgnoreBody) {\r | |
1083 | Parser->State = BodyParserComplete;\r | |
1084 | } else {\r | |
1085 | Parser->Callback = Callback;\r | |
1086 | Parser->Context = Context;\r | |
1087 | }\r | |
1088 | \r | |
1089 | *MsgParser = Parser;\r | |
1090 | return EFI_SUCCESS;\r | |
1091 | }\r | |
1092 | \r | |
1093 | /**\r | |
1094 | Parse message body.\r | |
1095 | \r | |
1096 | Parse BodyLength of message-body. This function can be called repeatedly to parse the message-body partially.\r | |
1097 | \r | |
1098 | @param[in, out] MsgParser Pointer to the message parser.\r | |
1099 | @param[in] BodyLength Length in bytes of the Body.\r | |
1100 | @param[in] Body Pointer to the buffer of the message-body to be parsed.\r | |
1101 | \r | |
1102 | @retval EFI_SUCCESS Successfully parse the message-body.\r | |
1103 | @retval EFI_INVALID_PARAMETER MsgParser is NULL or Body is NULL or BodyLength is 0.\r | |
1104 | @retval Others Operation aborted.\r | |
1105 | \r | |
1106 | **/\r | |
1107 | EFI_STATUS\r | |
1108 | EFIAPI\r | |
1109 | HttpParseMessageBody (\r | |
1110 | IN OUT VOID *MsgParser,\r | |
1111 | IN UINTN BodyLength,\r | |
1112 | IN CHAR8 *Body\r | |
1113 | )\r | |
1114 | {\r | |
1115 | CHAR8 *Char;\r | |
1116 | UINTN RemainderLengthInThis;\r | |
1117 | UINTN LengthForCallback;\r | |
1118 | EFI_STATUS Status;\r | |
1119 | HTTP_BODY_PARSER *Parser;\r | |
1120 | \r | |
1121 | if (BodyLength == 0 || Body == NULL) {\r | |
1122 | return EFI_INVALID_PARAMETER;\r | |
1123 | }\r | |
1124 | \r | |
1125 | if (MsgParser == NULL) {\r | |
1126 | return EFI_INVALID_PARAMETER;\r | |
1127 | }\r | |
1128 | \r | |
1129 | Parser = (HTTP_BODY_PARSER*) MsgParser;\r | |
1130 | \r | |
1131 | if (Parser->IgnoreBody) {\r | |
1132 | Parser->State = BodyParserComplete;\r | |
1133 | if (Parser->Callback != NULL) {\r | |
1134 | Status = Parser->Callback (\r | |
1135 | BodyParseEventOnComplete,\r | |
1136 | Body,\r | |
1137 | 0,\r | |
1138 | Parser->Context\r | |
1139 | );\r | |
1140 | if (EFI_ERROR (Status)) {\r | |
1141 | return Status;\r | |
1142 | }\r | |
1143 | }\r | |
1144 | return EFI_SUCCESS;\r | |
1145 | }\r | |
1146 | \r | |
1147 | if (Parser->State == BodyParserBodyStart) {\r | |
1148 | Parser->ParsedBodyLength = 0;\r | |
1149 | if (Parser->IsChunked) {\r | |
1150 | Parser->State = BodyParserChunkSizeStart;\r | |
1151 | } else {\r | |
1152 | Parser->State = BodyParserBodyIdentity;\r | |
1153 | }\r | |
1154 | }\r | |
1155 | \r | |
1156 | //\r | |
1157 | // The message body might be truncated in anywhere, so we need to parse is byte-by-byte.\r | |
1158 | //\r | |
1159 | for (Char = Body; Char < Body + BodyLength; ) {\r | |
1160 | \r | |
1161 | switch (Parser->State) {\r | |
1162 | case BodyParserStateMax:\r | |
1163 | return EFI_ABORTED;\r | |
47bda627 | 1164 | \r |
f9a14916 JW |
1165 | case BodyParserBodyIdentity:\r |
1166 | //\r | |
1167 | // Identity transfer-coding, just notify user to save the body data.\r | |
1168 | //\r | |
1169 | if (Parser->Callback != NULL) {\r | |
1170 | Status = Parser->Callback (\r | |
1171 | BodyParseEventOnData,\r | |
1172 | Char,\r | |
1173 | MIN (BodyLength, Parser->ContentLength - Parser->ParsedBodyLength),\r | |
1174 | Parser->Context\r | |
1175 | );\r | |
1176 | if (EFI_ERROR (Status)) {\r | |
1177 | return Status;\r | |
1178 | }\r | |
1179 | }\r | |
1180 | Char += MIN (BodyLength, Parser->ContentLength - Parser->ParsedBodyLength);\r | |
1181 | Parser->ParsedBodyLength += MIN (BodyLength, Parser->ContentLength - Parser->ParsedBodyLength);\r | |
1182 | if (Parser->ParsedBodyLength == Parser->ContentLength) {\r | |
1183 | Parser->State = BodyParserComplete;\r | |
47bda627 ZL |
1184 | if (Parser->Callback != NULL) {\r |
1185 | Status = Parser->Callback (\r | |
1186 | BodyParseEventOnComplete,\r | |
1187 | Char,\r | |
1188 | 0,\r | |
1189 | Parser->Context\r | |
1190 | );\r | |
1191 | if (EFI_ERROR (Status)) {\r | |
1192 | return Status;\r | |
1193 | }\r | |
1194 | }\r | |
f9a14916 JW |
1195 | }\r |
1196 | break;\r | |
1197 | \r | |
1198 | case BodyParserChunkSizeStart:\r | |
1199 | //\r | |
1200 | // First byte of chunk-size, the chunk-size might be truncated.\r | |
1201 | //\r | |
1202 | Parser->CurrentChunkSize = 0;\r | |
1203 | Parser->State = BodyParserChunkSize;\r | |
1204 | case BodyParserChunkSize:\r | |
1205 | if (!NET_IS_HEX_CHAR (*Char)) {\r | |
1206 | if (*Char == ';') {\r | |
1207 | Parser->State = BodyParserChunkExtStart;\r | |
1208 | Char++;\r | |
1209 | } else if (*Char == '\r') {\r | |
1210 | Parser->State = BodyParserChunkSizeEndCR;\r | |
1211 | Char++;\r | |
1212 | } else {\r | |
1213 | Parser->State = BodyParserStateMax;\r | |
1214 | }\r | |
1215 | break;\r | |
1216 | }\r | |
1217 | \r | |
1218 | if (Parser->CurrentChunkSize > (((~((UINTN) 0)) - 16) / 16)) {\r | |
1219 | return EFI_INVALID_PARAMETER;\r | |
1220 | }\r | |
1221 | Parser->CurrentChunkSize = Parser->CurrentChunkSize * 16 + HttpIoHexCharToUintn (*Char);\r | |
1222 | Char++;\r | |
1223 | break;\r | |
1224 | \r | |
1225 | case BodyParserChunkExtStart:\r | |
1226 | //\r | |
1227 | // Ignore all the chunk extensions.\r | |
1228 | //\r | |
1229 | if (*Char == '\r') {\r | |
1230 | Parser->State = BodyParserChunkSizeEndCR;\r | |
1231 | }\r | |
1232 | Char++;\r | |
1233 | break;\r | |
1234 | \r | |
1235 | case BodyParserChunkSizeEndCR:\r | |
1236 | if (*Char != '\n') {\r | |
1237 | Parser->State = BodyParserStateMax;\r | |
1238 | break;\r | |
1239 | }\r | |
1240 | Char++;\r | |
1241 | if (Parser->CurrentChunkSize == 0) {\r | |
1242 | //\r | |
1243 | // The last chunk has been parsed and now assumed the state \r | |
1244 | // of HttpBodyParse is ParserLastCRLF. So it need to decide\r | |
1245 | // whether the rest message is trailer or last CRLF in the next round.\r | |
1246 | //\r | |
1247 | Parser->ContentLengthIsValid = TRUE;\r | |
1248 | Parser->State = BodyParserLastCRLF;\r | |
1249 | break;\r | |
1250 | }\r | |
1251 | Parser->State = BodyParserChunkDataStart;\r | |
1252 | Parser->CurrentChunkParsedSize = 0;\r | |
1253 | break;\r | |
1254 | \r | |
1255 | case BodyParserLastCRLF:\r | |
1256 | //\r | |
1257 | // Judge the byte is belong to the Last CRLF or trailer, and then \r | |
1258 | // configure the state of HttpBodyParse to corresponding state.\r | |
1259 | //\r | |
1260 | if (*Char == '\r') {\r | |
1261 | Char++;\r | |
1262 | Parser->State = BodyParserLastCRLFEnd;\r | |
1263 | break;\r | |
1264 | } else {\r | |
1265 | Parser->State = BodyParserTrailer;\r | |
1266 | break;\r | |
1267 | }\r | |
1268 | \r | |
1269 | case BodyParserLastCRLFEnd:\r | |
1270 | if (*Char == '\n') {\r | |
1271 | Parser->State = BodyParserComplete;\r | |
47bda627 ZL |
1272 | Char++;\r |
1273 | if (Parser->Callback != NULL) {\r | |
1274 | Status = Parser->Callback (\r | |
1275 | BodyParseEventOnComplete,\r | |
1276 | Char,\r | |
1277 | 0,\r | |
1278 | Parser->Context\r | |
1279 | );\r | |
1280 | if (EFI_ERROR (Status)) {\r | |
1281 | return Status;\r | |
1282 | }\r | |
1283 | }\r | |
f9a14916 JW |
1284 | break;\r |
1285 | } else {\r | |
1286 | Parser->State = BodyParserStateMax;\r | |
1287 | break;\r | |
1288 | }\r | |
1289 | \r | |
1290 | case BodyParserTrailer:\r | |
1291 | if (*Char == '\r') {\r | |
1292 | Parser->State = BodyParserChunkSizeEndCR;\r | |
1293 | }\r | |
1294 | Char++;\r | |
1295 | break; \r | |
1296 | \r | |
1297 | case BodyParserChunkDataStart:\r | |
1298 | //\r | |
1299 | // First byte of chunk-data, the chunk data also might be truncated.\r | |
1300 | //\r | |
1301 | RemainderLengthInThis = BodyLength - (Char - Body);\r | |
1302 | LengthForCallback = MIN (Parser->CurrentChunkSize - Parser->CurrentChunkParsedSize, RemainderLengthInThis);\r | |
1303 | if (Parser->Callback != NULL) {\r | |
1304 | Status = Parser->Callback (\r | |
1305 | BodyParseEventOnData,\r | |
1306 | Char,\r | |
1307 | LengthForCallback,\r | |
1308 | Parser->Context\r | |
1309 | );\r | |
1310 | if (EFI_ERROR (Status)) {\r | |
1311 | return Status;\r | |
1312 | }\r | |
1313 | }\r | |
1314 | Char += LengthForCallback;\r | |
1315 | Parser->ContentLength += LengthForCallback;\r | |
1316 | Parser->CurrentChunkParsedSize += LengthForCallback;\r | |
1317 | if (Parser->CurrentChunkParsedSize == Parser->CurrentChunkSize) {\r | |
1318 | Parser->State = BodyParserChunkDataEnd;\r | |
1319 | } \r | |
1320 | break;\r | |
1321 | \r | |
1322 | case BodyParserChunkDataEnd:\r | |
1323 | if (*Char == '\r') {\r | |
1324 | Parser->State = BodyParserChunkDataEndCR;\r | |
1325 | } else {\r | |
1326 | Parser->State = BodyParserStateMax;\r | |
1327 | }\r | |
1328 | Char++;\r | |
1329 | break;\r | |
1330 | \r | |
1331 | case BodyParserChunkDataEndCR:\r | |
1332 | if (*Char != '\n') {\r | |
1333 | Parser->State = BodyParserStateMax;\r | |
1334 | break;\r | |
1335 | }\r | |
1336 | Char++;\r | |
1337 | Parser->State = BodyParserChunkSizeStart;\r | |
1338 | break; \r | |
1339 | \r | |
1340 | default:\r | |
1341 | break;\r | |
1342 | }\r | |
1343 | \r | |
1344 | }\r | |
1345 | \r | |
1346 | if (Parser->State == BodyParserStateMax) {\r | |
1347 | return EFI_ABORTED;\r | |
1348 | }\r | |
1349 | \r | |
1350 | return EFI_SUCCESS;\r | |
1351 | }\r | |
1352 | \r | |
1353 | /**\r | |
1354 | Check whether the message-body is complete or not.\r | |
1355 | \r | |
1356 | @param[in] MsgParser Pointer to the message parser.\r | |
1357 | \r | |
1358 | @retval TRUE Message-body is complete.\r | |
1359 | @retval FALSE Message-body is not complete.\r | |
1360 | \r | |
1361 | **/\r | |
1362 | BOOLEAN\r | |
1363 | EFIAPI\r | |
1364 | HttpIsMessageComplete (\r | |
1365 | IN VOID *MsgParser\r | |
1366 | )\r | |
1367 | {\r | |
1368 | HTTP_BODY_PARSER *Parser;\r | |
1369 | \r | |
1370 | Parser = (HTTP_BODY_PARSER*) MsgParser;\r | |
1371 | \r | |
1372 | if (Parser->State == BodyParserComplete) {\r | |
1373 | return TRUE;\r | |
1374 | }\r | |
1375 | return FALSE;\r | |
1376 | }\r | |
1377 | \r | |
1378 | /**\r | |
1379 | Get the content length of the entity.\r | |
1380 | \r | |
1381 | Note that in trunk transfer, the entity length is not valid until the whole message body is received.\r | |
1382 | \r | |
1383 | @param[in] MsgParser Pointer to the message parser.\r | |
1384 | @param[out] ContentLength Pointer to store the length of the entity.\r | |
1385 | \r | |
1386 | @retval EFI_SUCCESS Successfully to get the entity length.\r | |
1387 | @retval EFI_NOT_READY Entity length is not valid yet.\r | |
1388 | @retval EFI_INVALID_PARAMETER MsgParser is NULL or ContentLength is NULL.\r | |
1389 | \r | |
1390 | **/\r | |
1391 | EFI_STATUS\r | |
1392 | EFIAPI\r | |
1393 | HttpGetEntityLength (\r | |
1394 | IN VOID *MsgParser,\r | |
1395 | OUT UINTN *ContentLength\r | |
1396 | )\r | |
1397 | {\r | |
1398 | HTTP_BODY_PARSER *Parser;\r | |
1399 | \r | |
1400 | if (MsgParser == NULL || ContentLength == NULL) {\r | |
1401 | return EFI_INVALID_PARAMETER;\r | |
1402 | }\r | |
1403 | \r | |
1404 | Parser = (HTTP_BODY_PARSER*) MsgParser;\r | |
1405 | \r | |
1406 | if (!Parser->ContentLengthIsValid) {\r | |
1407 | return EFI_NOT_READY;\r | |
1408 | }\r | |
1409 | \r | |
1410 | *ContentLength = Parser->ContentLength;\r | |
1411 | return EFI_SUCCESS;\r | |
1412 | }\r | |
1413 | \r | |
1414 | /**\r | |
1415 | Release the resource of the message parser.\r | |
1416 | \r | |
1417 | @param[in] MsgParser Pointer to the message parser.\r | |
1418 | \r | |
1419 | **/\r | |
1420 | VOID\r | |
1421 | EFIAPI\r | |
1422 | HttpFreeMsgParser (\r | |
1423 | IN VOID *MsgParser\r | |
1424 | )\r | |
1425 | {\r | |
1426 | FreePool (MsgParser);\r | |
1427 | }\r |