MdeModulePkg: Add new API HttpUrlGetPath() to HttpLib.h
[mirror_edk2.git] / MdeModulePkg / Library / DxeHttpLib / DxeHttpLib.c
CommitLineData
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
6796629d 5Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
f9a14916
JW
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at<BR>\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT 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
47typedef struct {\r
48 UINT32 Offset;\r
49 UINT32 Length;\r
50} HTTP_URL_FILED_DATA;\r
51\r
52typedef struct {\r
53 UINT16 FieldBitMap;\r
54 HTTP_URL_FILED_DATA FieldData[HTTP_URI_FIELD_MAX];\r
55} HTTP_URL_PARSER;\r
56\r
57typedef 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
95EFI_STATUS\r
96EFIAPI\r
97UriPercentDecode (\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
8a1c4a8c 137 This function return the updated state according to the input state and next character of\r
f9a14916
JW
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
147HTTP_URL_PARSE_STATE\r
148NetHttpParseAuthorityChar (\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
219EFI_STATUS\r
220NetHttpParseAuthority (\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
8a1c4a8c 302 This function return the updated state according to the input state and next character of a URL.\r
f9a14916
JW
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
310HTTP_URL_PARSE_STATE\r
311NetHttpParseUrlChar (\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
417EFI_STATUS\r
418EFIAPI\r
419HttpParseUrl (\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
549EFI_STATUS\r
550EFIAPI\r
551HttpUrlGetHostName (\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
608EFI_STATUS\r
609EFIAPI\r
610HttpUrlGetIp4 (\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
668EFI_STATUS\r
669EFIAPI\r
670HttpUrlGetIp6 (\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
743EFI_STATUS\r
744EFIAPI\r
745HttpUrlGetPort (\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
6796629d
FS
787/**\r
788 Get the Path from a HTTP URL.\r
789\r
790 This function will return the Path according to the Url and previous parse result,and\r
791 it is the caller's responsibility to free the buffer returned in *Path.\r
792\r
793 @param[in] Url The pointer to a HTTP URL string.\r
794 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().\r
795 @param[out] Path Pointer to a buffer to store the Path.\r
796\r
797 @retval EFI_SUCCESS Successfully get the required component.\r
798 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.\r
799 @retval EFI_NOT_FOUND No hostName component in the URL.\r
800 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r
801 \r
802**/\r
803EFI_STATUS\r
804EFIAPI\r
805HttpUrlGetPath (\r
806 IN CHAR8 *Url,\r
807 IN VOID *UrlParser,\r
808 OUT CHAR8 **Path\r
809 )\r
810{\r
811 CHAR8 *PathStr;\r
812 EFI_STATUS Status;\r
813 UINT32 ResultLength;\r
814 HTTP_URL_PARSER *Parser;\r
815\r
816 if (Url == NULL || UrlParser == NULL || Path == NULL) {\r
817 return EFI_INVALID_PARAMETER;\r
818 }\r
819\r
820 Parser = (HTTP_URL_PARSER*) UrlParser;\r
821\r
822 if ((Parser->FieldBitMap & BIT (HTTP_URI_FIELD_PATH)) == 0) {\r
823 return EFI_NOT_FOUND;\r
824 }\r
825\r
826 PathStr = AllocatePool (Parser->FieldData[HTTP_URI_FIELD_PATH].Length + 1);\r
827 if (PathStr == NULL) {\r
828 return EFI_OUT_OF_RESOURCES;\r
829 }\r
830 \r
831 Status = UriPercentDecode (\r
832 Url + Parser->FieldData[HTTP_URI_FIELD_PATH].Offset,\r
833 Parser->FieldData[HTTP_URI_FIELD_PATH].Length,\r
834 PathStr,\r
835 &ResultLength\r
836 );\r
837 if (EFI_ERROR (Status)) {\r
838 return Status;\r
839 }\r
840\r
841 PathStr[ResultLength] = '\0';\r
842 *Path = PathStr;\r
843 return EFI_SUCCESS;\r
844}\r
845\r
f9a14916
JW
846/**\r
847 Release the resource of the URL parser.\r
848\r
849 @param[in] UrlParser Pointer to the parser.\r
850 \r
851**/\r
852VOID\r
853EFIAPI\r
854HttpUrlFreeParser (\r
855 IN VOID *UrlParser\r
856 )\r
857{\r
858 FreePool (UrlParser);\r
859}\r
860\r
861/**\r
862 Find a specified header field according to the field name.\r
863\r
864 @param[in] HeaderCount Number of HTTP header structures in Headers list. \r
865 @param[in] Headers Array containing list of HTTP headers.\r
866 @param[in] FieldName Null terminated string which describes a field name. \r
867\r
868 @return Pointer to the found header or NULL.\r
869\r
870**/\r
871EFI_HTTP_HEADER *\r
872HttpIoFindHeader (\r
873 IN UINTN HeaderCount,\r
874 IN EFI_HTTP_HEADER *Headers,\r
875 IN CHAR8 *FieldName\r
876 )\r
877{\r
878 UINTN Index;\r
879 \r
880 if (HeaderCount == 0 || Headers == NULL || FieldName == NULL) {\r
881 return NULL;\r
882 }\r
883\r
884 for (Index = 0; Index < HeaderCount; Index++){\r
885 //\r
886 // Field names are case-insensitive (RFC 2616).\r
887 //\r
888 if (AsciiStriCmp (Headers[Index].FieldName, FieldName) == 0) {\r
889 return &Headers[Index];\r
890 }\r
891 }\r
892 return NULL;\r
893}\r
894\r
895typedef enum {\r
896 BodyParserBodyStart,\r
897 BodyParserBodyIdentity,\r
898 BodyParserChunkSizeStart,\r
899 BodyParserChunkSize,\r
900 BodyParserChunkSizeEndCR,\r
901 BodyParserChunkExtStart,\r
902 BodyParserChunkDataStart,\r
903 BodyParserChunkDataEnd,\r
904 BodyParserChunkDataEndCR,\r
905 BodyParserTrailer,\r
906 BodyParserLastCRLF,\r
907 BodyParserLastCRLFEnd,\r
908 BodyParserComplete,\r
909 BodyParserStateMax\r
910} HTTP_BODY_PARSE_STATE;\r
911\r
912typedef struct {\r
913 BOOLEAN IgnoreBody; // "MUST NOT" include a message-body\r
914 BOOLEAN IsChunked; // "chunked" transfer-coding.\r
915 BOOLEAN ContentLengthIsValid;\r
916 UINTN ContentLength; // Entity length (not the message-body length), invalid until ContentLengthIsValid is TRUE\r
917 \r
918 HTTP_BODY_PARSER_CALLBACK Callback;\r
919 VOID *Context;\r
920 UINTN ParsedBodyLength;\r
921 HTTP_BODY_PARSE_STATE State;\r
922 UINTN CurrentChunkSize;\r
923 UINTN CurrentChunkParsedSize;\r
924} HTTP_BODY_PARSER;\r
925\r
926/**\r
927\r
928 Convert an Ascii char to its uppercase.\r
929\r
930 @param[in] Char Ascii character.\r
931\r
932 @return Uppercase value of the input Char.\r
933\r
934**/\r
935CHAR8\r
936HttpIoCharToUpper (\r
937 IN CHAR8 Char\r
938 )\r
939{\r
940 if (Char >= 'a' && Char <= 'z') {\r
941 return Char - ('a' - 'A');\r
942 }\r
943\r
944 return Char;\r
945}\r
946\r
947/**\r
948 Convert an hexadecimal char to a value of type UINTN.\r
949\r
950 @param[in] Char Ascii character.\r
951\r
952 @return Value translated from Char.\r
953\r
954**/\r
955UINTN\r
956HttpIoHexCharToUintn (\r
957 IN CHAR8 Char\r
958 )\r
959{\r
960 if (Char >= '0' && Char <= '9') {\r
961 return Char - '0';\r
962 }\r
963\r
964 return (10 + HttpIoCharToUpper (Char) - 'A');\r
965}\r
966\r
967/**\r
968 Get the value of the content length if there is a "Content-Length" header.\r
969\r
970 @param[in] HeaderCount Number of HTTP header structures in Headers.\r
971 @param[in] Headers Array containing list of HTTP headers.\r
972 @param[out] ContentLength Pointer to save the value of the content length.\r
973\r
974 @retval EFI_SUCCESS Successfully get the content length.\r
975 @retval EFI_NOT_FOUND No "Content-Length" header in the Headers.\r
976\r
977**/\r
978EFI_STATUS\r
979HttpIoParseContentLengthHeader (\r
980 IN UINTN HeaderCount,\r
981 IN EFI_HTTP_HEADER *Headers,\r
982 OUT UINTN *ContentLength\r
983 )\r
984{\r
985 EFI_HTTP_HEADER *Header;\r
986 \r
987 Header = HttpIoFindHeader (HeaderCount, Headers, "Content-Length");\r
988 if (Header == NULL) {\r
989 return EFI_NOT_FOUND;\r
990 }\r
991\r
992 *ContentLength = AsciiStrDecimalToUintn (Header->FieldValue);\r
993 return EFI_SUCCESS;\r
994}\r
995\r
996/**\r
997\r
998 Check whether the HTTP message is using the "chunked" transfer-coding.\r
999\r
1000 @param[in] HeaderCount Number of HTTP header structures in Headers.\r
1001 @param[in] Headers Array containing list of HTTP headers.\r
1002\r
1003 @return The message is "chunked" transfer-coding (TRUE) or not (FALSE).\r
1004 \r
1005**/\r
1006BOOLEAN\r
1007HttpIoIsChunked (\r
1008 IN UINTN HeaderCount,\r
1009 IN EFI_HTTP_HEADER *Headers\r
1010 )\r
1011{\r
1012 EFI_HTTP_HEADER *Header;\r
1013\r
1014\r
1015 Header = HttpIoFindHeader (HeaderCount, Headers, "Transfer-Encoding");\r
1016 if (Header == NULL) {\r
1017 return FALSE;\r
1018 }\r
1019\r
1020 if (AsciiStriCmp (Header->FieldValue, "identity") != 0) {\r
1021 return TRUE;\r
1022 }\r
1023\r
1024 return FALSE;\r
1025}\r
1026\r
1027/**\r
1028 Check whether the HTTP message should have a message-body.\r
1029\r
1030 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.\r
1031 @param[in] StatusCode Response status code returned by the remote host.\r
1032\r
1033 @return The message should have a message-body (FALSE) or not (TRUE).\r
1034\r
1035**/\r
1036BOOLEAN\r
1037HttpIoNoMessageBody (\r
1038 IN EFI_HTTP_METHOD Method,\r
1039 IN EFI_HTTP_STATUS_CODE StatusCode\r
1040 )\r
1041{\r
1042 //\r
1043 // RFC 2616:\r
1044 // All responses to the HEAD request method\r
1045 // MUST NOT include a message-body, even though the presence of entity-\r
1046 // header fields might lead one to believe they do. All 1xx\r
1047 // (informational), 204 (no content), and 304 (not modified) responses\r
1048 // MUST NOT include a message-body. All other responses do include a\r
1049 // message-body, although it MAY be of zero length.\r
1050 //\r
1051 if (Method == HttpMethodHead) {\r
1052 return TRUE;\r
1053 }\r
1054\r
1055 if ((StatusCode == HTTP_STATUS_100_CONTINUE) ||\r
1056 (StatusCode == HTTP_STATUS_101_SWITCHING_PROTOCOLS) ||\r
1057 (StatusCode == HTTP_STATUS_204_NO_CONTENT) ||\r
1058 (StatusCode == HTTP_STATUS_304_NOT_MODIFIED))\r
1059 {\r
1060 return TRUE;\r
1061 }\r
1062\r
1063 return FALSE;\r
1064}\r
1065\r
1066/**\r
1067 Initialize a HTTP message-body parser.\r
1068\r
1069 This function will create and initialize a HTTP message parser according to caller provided HTTP message\r
1070 header information. It is the caller's responsibility to free the buffer returned in *UrlParser by HttpFreeMsgParser().\r
1071\r
1072 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.\r
1073 @param[in] StatusCode Response status code returned by the remote host.\r
1074 @param[in] HeaderCount Number of HTTP header structures in Headers.\r
1075 @param[in] Headers Array containing list of HTTP headers.\r
1076 @param[in] Callback Callback function that is invoked when parsing the HTTP message-body,\r
1077 set to NULL to ignore all events.\r
1078 @param[in] Context Pointer to the context that will be passed to Callback.\r
1079 @param[out] MsgParser Pointer to the returned buffer to store the message parser.\r
1080\r
1081 @retval EFI_SUCCESS Successfully initialized the parser.\r
1082 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r
1083 @retval EFI_INVALID_PARAMETER MsgParser is NULL or HeaderCount is not NULL but Headers is NULL.\r
1084 @retval Others Failed to initialize the parser.\r
1085\r
1086**/\r
1087EFI_STATUS\r
1088EFIAPI\r
1089HttpInitMsgParser (\r
1090 IN EFI_HTTP_METHOD Method,\r
1091 IN EFI_HTTP_STATUS_CODE StatusCode,\r
1092 IN UINTN HeaderCount,\r
1093 IN EFI_HTTP_HEADER *Headers,\r
1094 IN HTTP_BODY_PARSER_CALLBACK Callback,\r
1095 IN VOID *Context,\r
1096 OUT VOID **MsgParser\r
1097 )\r
1098{\r
1099 EFI_STATUS Status;\r
1100 HTTP_BODY_PARSER *Parser;\r
1101 \r
1102 if (HeaderCount != 0 && Headers == NULL) {\r
1103 return EFI_INVALID_PARAMETER;\r
1104 }\r
1105\r
1106 if (MsgParser == NULL) {\r
1107 return EFI_INVALID_PARAMETER;\r
1108 }\r
1109\r
1110 Parser = AllocateZeroPool (sizeof (HTTP_BODY_PARSER));\r
1111 if (Parser == NULL) {\r
1112 return EFI_OUT_OF_RESOURCES;\r
1113 }\r
1114\r
1115 Parser->State = BodyParserBodyStart;\r
1116 \r
1117 //\r
8a1c4a8c 1118 // Determine the message length according to RFC 2616.\r
f9a14916
JW
1119 // 1. Check whether the message "MUST NOT" have a message-body.\r
1120 //\r
1121 Parser->IgnoreBody = HttpIoNoMessageBody (Method, StatusCode);\r
1122 //\r
1123 // 2. Check whether the message using "chunked" transfer-coding.\r
1124 //\r
1125 Parser->IsChunked = HttpIoIsChunked (HeaderCount, Headers);\r
1126 //\r
1127 // 3. Check whether the message has a Content-Length header field.\r
1128 //\r
1129 Status = HttpIoParseContentLengthHeader (HeaderCount, Headers, &Parser->ContentLength);\r
1130 if (!EFI_ERROR (Status)) {\r
1131 Parser->ContentLengthIsValid = TRUE;\r
1132 }\r
1133 //\r
1134 // 4. Range header is not supported now, so we won't meet media type "multipart/byteranges".\r
1135 // 5. By server closing the connection\r
1136 //\r
1137 \r
1138 //\r
1139 // Set state to skip body parser if the message shouldn't have a message body.\r
1140 //\r
1141 if (Parser->IgnoreBody) {\r
1142 Parser->State = BodyParserComplete;\r
1143 } else {\r
1144 Parser->Callback = Callback;\r
1145 Parser->Context = Context;\r
1146 }\r
1147\r
1148 *MsgParser = Parser;\r
1149 return EFI_SUCCESS;\r
1150}\r
1151\r
1152/**\r
1153 Parse message body.\r
1154\r
1155 Parse BodyLength of message-body. This function can be called repeatedly to parse the message-body partially.\r
1156\r
1157 @param[in, out] MsgParser Pointer to the message parser.\r
1158 @param[in] BodyLength Length in bytes of the Body.\r
1159 @param[in] Body Pointer to the buffer of the message-body to be parsed.\r
1160\r
1161 @retval EFI_SUCCESS Successfully parse the message-body.\r
1162 @retval EFI_INVALID_PARAMETER MsgParser is NULL or Body is NULL or BodyLength is 0.\r
1163 @retval Others Operation aborted.\r
1164\r
1165**/\r
1166EFI_STATUS\r
1167EFIAPI\r
1168HttpParseMessageBody (\r
1169 IN OUT VOID *MsgParser,\r
1170 IN UINTN BodyLength,\r
1171 IN CHAR8 *Body\r
1172 )\r
1173{\r
1174 CHAR8 *Char;\r
1175 UINTN RemainderLengthInThis;\r
1176 UINTN LengthForCallback;\r
1177 EFI_STATUS Status;\r
1178 HTTP_BODY_PARSER *Parser;\r
1179 \r
1180 if (BodyLength == 0 || Body == NULL) {\r
1181 return EFI_INVALID_PARAMETER;\r
1182 }\r
1183\r
1184 if (MsgParser == NULL) {\r
1185 return EFI_INVALID_PARAMETER;\r
1186 }\r
1187\r
1188 Parser = (HTTP_BODY_PARSER*) MsgParser;\r
1189\r
1190 if (Parser->IgnoreBody) {\r
1191 Parser->State = BodyParserComplete;\r
1192 if (Parser->Callback != NULL) {\r
1193 Status = Parser->Callback (\r
1194 BodyParseEventOnComplete,\r
1195 Body,\r
1196 0,\r
1197 Parser->Context\r
1198 );\r
1199 if (EFI_ERROR (Status)) {\r
1200 return Status;\r
1201 }\r
1202 }\r
1203 return EFI_SUCCESS;\r
1204 }\r
1205\r
1206 if (Parser->State == BodyParserBodyStart) {\r
1207 Parser->ParsedBodyLength = 0;\r
1208 if (Parser->IsChunked) {\r
1209 Parser->State = BodyParserChunkSizeStart;\r
1210 } else {\r
1211 Parser->State = BodyParserBodyIdentity;\r
1212 }\r
1213 }\r
1214\r
1215 //\r
1216 // The message body might be truncated in anywhere, so we need to parse is byte-by-byte.\r
1217 //\r
1218 for (Char = Body; Char < Body + BodyLength; ) {\r
1219\r
1220 switch (Parser->State) {\r
1221 case BodyParserStateMax:\r
1222 return EFI_ABORTED;\r
47bda627 1223 \r
f9a14916
JW
1224 case BodyParserBodyIdentity:\r
1225 //\r
1226 // Identity transfer-coding, just notify user to save the body data.\r
1227 //\r
1228 if (Parser->Callback != NULL) {\r
1229 Status = Parser->Callback (\r
1230 BodyParseEventOnData,\r
1231 Char,\r
1232 MIN (BodyLength, Parser->ContentLength - Parser->ParsedBodyLength),\r
1233 Parser->Context\r
1234 );\r
1235 if (EFI_ERROR (Status)) {\r
1236 return Status;\r
1237 }\r
1238 }\r
1239 Char += MIN (BodyLength, Parser->ContentLength - Parser->ParsedBodyLength);\r
1240 Parser->ParsedBodyLength += MIN (BodyLength, Parser->ContentLength - Parser->ParsedBodyLength);\r
1241 if (Parser->ParsedBodyLength == Parser->ContentLength) {\r
1242 Parser->State = BodyParserComplete;\r
47bda627
ZL
1243 if (Parser->Callback != NULL) {\r
1244 Status = Parser->Callback (\r
1245 BodyParseEventOnComplete,\r
1246 Char,\r
1247 0,\r
1248 Parser->Context\r
1249 );\r
1250 if (EFI_ERROR (Status)) {\r
1251 return Status;\r
1252 }\r
1253 }\r
f9a14916
JW
1254 }\r
1255 break;\r
1256\r
1257 case BodyParserChunkSizeStart:\r
1258 //\r
1259 // First byte of chunk-size, the chunk-size might be truncated.\r
1260 //\r
1261 Parser->CurrentChunkSize = 0;\r
1262 Parser->State = BodyParserChunkSize;\r
1263 case BodyParserChunkSize:\r
1264 if (!NET_IS_HEX_CHAR (*Char)) {\r
1265 if (*Char == ';') {\r
1266 Parser->State = BodyParserChunkExtStart;\r
1267 Char++;\r
1268 } else if (*Char == '\r') {\r
1269 Parser->State = BodyParserChunkSizeEndCR;\r
1270 Char++;\r
1271 } else {\r
1272 Parser->State = BodyParserStateMax;\r
1273 }\r
1274 break;\r
1275 }\r
1276\r
1277 if (Parser->CurrentChunkSize > (((~((UINTN) 0)) - 16) / 16)) {\r
1278 return EFI_INVALID_PARAMETER;\r
1279 }\r
1280 Parser->CurrentChunkSize = Parser->CurrentChunkSize * 16 + HttpIoHexCharToUintn (*Char);\r
1281 Char++;\r
1282 break;\r
1283\r
1284 case BodyParserChunkExtStart:\r
1285 //\r
1286 // Ignore all the chunk extensions.\r
1287 //\r
1288 if (*Char == '\r') {\r
1289 Parser->State = BodyParserChunkSizeEndCR;\r
1290 }\r
1291 Char++;\r
1292 break;\r
1293 \r
1294 case BodyParserChunkSizeEndCR:\r
1295 if (*Char != '\n') {\r
1296 Parser->State = BodyParserStateMax;\r
1297 break;\r
1298 }\r
1299 Char++;\r
1300 if (Parser->CurrentChunkSize == 0) {\r
1301 //\r
1302 // The last chunk has been parsed and now assumed the state \r
1303 // of HttpBodyParse is ParserLastCRLF. So it need to decide\r
1304 // whether the rest message is trailer or last CRLF in the next round.\r
1305 //\r
1306 Parser->ContentLengthIsValid = TRUE;\r
1307 Parser->State = BodyParserLastCRLF;\r
1308 break;\r
1309 }\r
1310 Parser->State = BodyParserChunkDataStart;\r
1311 Parser->CurrentChunkParsedSize = 0;\r
1312 break;\r
1313 \r
1314 case BodyParserLastCRLF:\r
1315 //\r
1316 // Judge the byte is belong to the Last CRLF or trailer, and then \r
1317 // configure the state of HttpBodyParse to corresponding state.\r
1318 //\r
1319 if (*Char == '\r') {\r
1320 Char++;\r
1321 Parser->State = BodyParserLastCRLFEnd;\r
1322 break;\r
1323 } else {\r
1324 Parser->State = BodyParserTrailer;\r
1325 break;\r
1326 }\r
1327 \r
1328 case BodyParserLastCRLFEnd:\r
1329 if (*Char == '\n') {\r
1330 Parser->State = BodyParserComplete;\r
47bda627
ZL
1331 Char++;\r
1332 if (Parser->Callback != NULL) {\r
1333 Status = Parser->Callback (\r
1334 BodyParseEventOnComplete,\r
1335 Char,\r
1336 0,\r
1337 Parser->Context\r
1338 );\r
1339 if (EFI_ERROR (Status)) {\r
1340 return Status;\r
1341 }\r
1342 }\r
f9a14916
JW
1343 break;\r
1344 } else {\r
1345 Parser->State = BodyParserStateMax;\r
1346 break;\r
1347 }\r
1348 \r
1349 case BodyParserTrailer:\r
1350 if (*Char == '\r') {\r
1351 Parser->State = BodyParserChunkSizeEndCR;\r
1352 }\r
1353 Char++;\r
1354 break; \r
1355\r
1356 case BodyParserChunkDataStart:\r
1357 //\r
1358 // First byte of chunk-data, the chunk data also might be truncated.\r
1359 //\r
1360 RemainderLengthInThis = BodyLength - (Char - Body);\r
1361 LengthForCallback = MIN (Parser->CurrentChunkSize - Parser->CurrentChunkParsedSize, RemainderLengthInThis);\r
1362 if (Parser->Callback != NULL) {\r
1363 Status = Parser->Callback (\r
1364 BodyParseEventOnData,\r
1365 Char,\r
1366 LengthForCallback,\r
1367 Parser->Context\r
1368 );\r
1369 if (EFI_ERROR (Status)) {\r
1370 return Status;\r
1371 }\r
1372 }\r
1373 Char += LengthForCallback;\r
1374 Parser->ContentLength += LengthForCallback;\r
1375 Parser->CurrentChunkParsedSize += LengthForCallback;\r
1376 if (Parser->CurrentChunkParsedSize == Parser->CurrentChunkSize) {\r
1377 Parser->State = BodyParserChunkDataEnd;\r
1378 } \r
1379 break;\r
1380\r
1381 case BodyParserChunkDataEnd:\r
1382 if (*Char == '\r') {\r
1383 Parser->State = BodyParserChunkDataEndCR;\r
1384 } else {\r
1385 Parser->State = BodyParserStateMax;\r
1386 }\r
1387 Char++;\r
1388 break;\r
1389\r
1390 case BodyParserChunkDataEndCR:\r
1391 if (*Char != '\n') {\r
1392 Parser->State = BodyParserStateMax;\r
1393 break;\r
1394 }\r
1395 Char++;\r
1396 Parser->State = BodyParserChunkSizeStart;\r
1397 break; \r
1398\r
1399 default:\r
1400 break;\r
1401 }\r
1402\r
1403 }\r
1404\r
1405 if (Parser->State == BodyParserStateMax) {\r
1406 return EFI_ABORTED;\r
1407 }\r
1408\r
1409 return EFI_SUCCESS;\r
1410}\r
1411\r
1412/**\r
1413 Check whether the message-body is complete or not.\r
1414\r
1415 @param[in] MsgParser Pointer to the message parser.\r
1416\r
1417 @retval TRUE Message-body is complete.\r
1418 @retval FALSE Message-body is not complete.\r
1419\r
1420**/\r
1421BOOLEAN\r
1422EFIAPI\r
1423HttpIsMessageComplete (\r
1424 IN VOID *MsgParser\r
1425 )\r
1426{\r
1427 HTTP_BODY_PARSER *Parser;\r
1428\r
1429 Parser = (HTTP_BODY_PARSER*) MsgParser;\r
1430\r
1431 if (Parser->State == BodyParserComplete) {\r
1432 return TRUE;\r
1433 }\r
1434 return FALSE;\r
1435}\r
1436\r
1437/**\r
1438 Get the content length of the entity.\r
1439\r
1440 Note that in trunk transfer, the entity length is not valid until the whole message body is received.\r
1441\r
1442 @param[in] MsgParser Pointer to the message parser.\r
1443 @param[out] ContentLength Pointer to store the length of the entity.\r
1444\r
1445 @retval EFI_SUCCESS Successfully to get the entity length.\r
1446 @retval EFI_NOT_READY Entity length is not valid yet.\r
1447 @retval EFI_INVALID_PARAMETER MsgParser is NULL or ContentLength is NULL.\r
1448 \r
1449**/\r
1450EFI_STATUS\r
1451EFIAPI\r
1452HttpGetEntityLength (\r
1453 IN VOID *MsgParser,\r
1454 OUT UINTN *ContentLength\r
1455 )\r
1456{\r
1457 HTTP_BODY_PARSER *Parser;\r
1458\r
1459 if (MsgParser == NULL || ContentLength == NULL) {\r
1460 return EFI_INVALID_PARAMETER;\r
1461 }\r
1462\r
1463 Parser = (HTTP_BODY_PARSER*) MsgParser;\r
1464\r
1465 if (!Parser->ContentLengthIsValid) {\r
1466 return EFI_NOT_READY;\r
1467 }\r
1468\r
1469 *ContentLength = Parser->ContentLength;\r
1470 return EFI_SUCCESS;\r
1471}\r
1472\r
1473/**\r
1474 Release the resource of the message parser.\r
1475\r
1476 @param[in] MsgParser Pointer to the message parser.\r
1477 \r
1478**/\r
1479VOID\r
1480EFIAPI\r
1481HttpFreeMsgParser (\r
1482 IN VOID *MsgParser\r
1483 )\r
1484{\r
1485 FreePool (MsgParser);\r
1486}\r