2 This library is used to share code between UEFI network stack modules.
3 It provides the helper routines to parse the HTTP message byte stream.
5 Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
6 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "DxeHttpLib.h"
16 Decode a percent-encoded URI component to the ASCII character.
18 Decode the input component in Buffer according to RFC 3986. The caller is responsible to make
19 sure ResultBuffer points to a buffer with size equal or greater than ((AsciiStrSize (Buffer))
22 @param[in] Buffer The pointer to a percent-encoded URI component.
23 @param[in] BufferLength Length of Buffer in bytes.
24 @param[out] ResultBuffer Point to the buffer to store the decode result.
25 @param[out] ResultLength Length of decoded string in ResultBuffer in bytes.
27 @retval EFI_SUCCESS Successfully decoded the URI.
28 @retval EFI_INVALID_PARAMETER Buffer is not a valid percent-encoded string.
35 IN UINT32 BufferLength
,
36 OUT CHAR8
*ResultBuffer
,
37 OUT UINT32
*ResultLength
44 if (Buffer
== NULL
|| BufferLength
== 0 || ResultBuffer
== NULL
) {
45 return EFI_INVALID_PARAMETER
;
51 while (Index
< BufferLength
) {
52 if (Buffer
[Index
] == '%') {
53 if (Index
+ 1 >= BufferLength
|| Index
+ 2 >= BufferLength
||
54 !NET_IS_HEX_CHAR (Buffer
[Index
+1]) || !NET_IS_HEX_CHAR (Buffer
[Index
+2])) {
55 return EFI_INVALID_PARAMETER
;
57 HexStr
[0] = Buffer
[Index
+1];
58 HexStr
[1] = Buffer
[Index
+2];
59 ResultBuffer
[Offset
] = (CHAR8
) AsciiStrHexToUintn (HexStr
);
62 ResultBuffer
[Offset
] = Buffer
[Index
];
68 *ResultLength
= (UINT32
) Offset
;
74 This function return the updated state according to the input state and next character of
77 @param[in] Char Next character.
78 @param[in] State Current value of the parser state machine.
79 @param[in] IsRightBracket TRUE if there is an sign ']' in the authority component and
80 indicates the next part is ':' before Port.
82 @return Updated state value.
85 NetHttpParseAuthorityChar (
87 IN HTTP_URL_PARSE_STATE State
,
88 IN BOOLEAN
*IsRightBracket
94 // The authority component is preceded by a double slash ("//") and is
95 // terminated by the next slash ("/"), question mark ("?"), or number
96 // sign ("#") character, or by the end of the URI.
98 if (Char
== ' ' || Char
== '\r' || Char
== '\n') {
99 return UrlParserStateMax
;
103 // authority = [ userinfo "@" ] host [ ":" port ]
106 case UrlParserUserInfo
:
108 return UrlParserHostStart
;
113 case UrlParserHostStart
:
115 return UrlParserHostIpv6
;
119 return UrlParserPortStart
;
122 return UrlParserHost
;
124 case UrlParserHostIpv6
:
126 *IsRightBracket
= TRUE
;
129 if (Char
== ':' && *IsRightBracket
) {
130 return UrlParserPortStart
;
132 return UrlParserHostIpv6
;
135 case UrlParserPortStart
:
136 return UrlParserPort
;
146 This function parse the authority component of the input URL and update the parser.
148 @param[in] Url The pointer to a HTTP URL string.
149 @param[in] FoundAt TRUE if there is an at sign ('@') in the authority component.
150 @param[in, out] UrlParser Pointer to the buffer of the parse result.
152 @retval EFI_SUCCESS Successfully parse the authority.
153 @retval EFI_INVALID_PARAMETER The Url is invalid to parse the authority component.
157 NetHttpParseAuthority (
160 IN OUT HTTP_URL_PARSER
*UrlParser
166 HTTP_URL_PARSE_STATE State
;
169 BOOLEAN IsrightBracket
;
171 ASSERT ((UrlParser
->FieldBitMap
& BIT (HTTP_URI_FIELD_AUTHORITY
)) != 0);
174 // authority = [ userinfo "@" ] host [ ":" port ]
177 State
= UrlParserUserInfo
;
179 State
= UrlParserHost
;
182 IsrightBracket
= FALSE
;
183 Field
= HTTP_URI_FIELD_MAX
;
185 Authority
= Url
+ UrlParser
->FieldData
[HTTP_URI_FIELD_AUTHORITY
].Offset
;
186 Length
= UrlParser
->FieldData
[HTTP_URI_FIELD_AUTHORITY
].Length
;
187 for (Char
= Authority
; Char
< Authority
+ Length
; Char
++) {
188 State
= NetHttpParseAuthorityChar (*Char
, State
, &IsrightBracket
);
190 case UrlParserStateMax
:
191 return EFI_INVALID_PARAMETER
;
193 case UrlParserHostStart
:
194 case UrlParserPortStart
:
197 case UrlParserUserInfo
:
198 Field
= HTTP_URI_FIELD_USERINFO
;
202 Field
= HTTP_URI_FIELD_HOST
;
205 case UrlParserHostIpv6
:
206 Field
= HTTP_URI_FIELD_HOST
;
210 Field
= HTTP_URI_FIELD_PORT
;
218 // Field not changed, count the length.
220 ASSERT (Field
< HTTP_URI_FIELD_MAX
);
221 if (Field
== OldField
) {
222 UrlParser
->FieldData
[Field
].Length
++;
229 UrlParser
->FieldBitMap
|= BIT (Field
);
230 UrlParser
->FieldData
[Field
].Offset
= (UINT32
) (Char
- Url
);
231 UrlParser
->FieldData
[Field
].Length
= 1;
239 This function return the updated state according to the input state and next character of a URL.
241 @param[in] Char Next character.
242 @param[in] State Current value of the parser state machine.
244 @return Updated state value.
248 NetHttpParseUrlChar (
250 IN HTTP_URL_PARSE_STATE State
253 if (Char
== ' ' || Char
== '\r' || Char
== '\n') {
254 return UrlParserStateMax
;
258 // http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]
260 // Request-URI = "*" | absolute-URI | path-absolute | authority
262 // absolute-URI = scheme ":" hier-part [ "?" query ]
263 // path-absolute = "/" [ segment-nz *( "/" segment ) ]
264 // authority = [ userinfo "@" ] host [ ":" port ]
267 case UrlParserUrlStart
:
268 if (Char
== '*' || Char
== '/') {
269 return UrlParserPath
;
271 return UrlParserScheme
;
273 case UrlParserScheme
:
275 return UrlParserSchemeColon
;
279 case UrlParserSchemeColon
:
281 return UrlParserSchemeColonSlash
;
285 case UrlParserSchemeColonSlash
:
287 return UrlParserSchemeColonSlashSlash
;
291 case UrlParserAtInAuthority
:
293 return UrlParserStateMax
;
296 case UrlParserAuthority
:
297 case UrlParserSchemeColonSlashSlash
:
299 return UrlParserAtInAuthority
;
302 return UrlParserPath
;
305 return UrlParserQueryStart
;
308 return UrlParserFragmentStart
;
310 return UrlParserAuthority
;
314 return UrlParserQueryStart
;
317 return UrlParserFragmentStart
;
322 case UrlParserQueryStart
:
324 return UrlParserFragmentStart
;
326 return UrlParserQuery
;
328 case UrlParserFragmentStart
:
329 return UrlParserFragment
;
338 Create a URL parser for the input URL string.
340 This function will parse and dereference the input HTTP URL into it components. The original
341 content of the URL won't be modified and the result will be returned in UrlParser, which can
342 be used in other functions like NetHttpUrlGetHostName().
344 @param[in] Url The pointer to a HTTP URL string.
345 @param[in] Length Length of Url in bytes.
346 @param[in] IsConnectMethod Whether the Url is used in HTTP CONNECT method or not.
347 @param[out] UrlParser Pointer to the returned buffer to store the parse result.
349 @retval EFI_SUCCESS Successfully dereferenced the HTTP URL.
350 @retval EFI_INVALID_PARAMETER UrlParser is NULL or Url is not a valid HTTP URL.
351 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
359 IN BOOLEAN IsConnectMethod
,
363 HTTP_URL_PARSE_STATE State
;
369 HTTP_URL_PARSER
*Parser
;
373 if (Url
== NULL
|| Length
== 0 || UrlParser
== NULL
) {
374 return EFI_INVALID_PARAMETER
;
377 Parser
= AllocateZeroPool (sizeof (HTTP_URL_PARSER
));
378 if (Parser
== NULL
) {
379 return EFI_OUT_OF_RESOURCES
;
382 if (IsConnectMethod
) {
384 // According to RFC 2616, the authority form is only used by the CONNECT method.
386 State
= UrlParserAuthority
;
388 State
= UrlParserUrlStart
;
391 Field
= HTTP_URI_FIELD_MAX
;
394 for (Char
= Url
; Char
< Url
+ Length
; Char
++) {
396 // Update state machine according to next char.
398 State
= NetHttpParseUrlChar (*Char
, State
);
401 case UrlParserStateMax
:
403 return EFI_INVALID_PARAMETER
;
405 case UrlParserSchemeColon
:
406 case UrlParserSchemeColonSlash
:
407 case UrlParserSchemeColonSlashSlash
:
408 case UrlParserQueryStart
:
409 case UrlParserFragmentStart
:
411 // Skip all the delimiting char: "://" "?" "@"
415 case UrlParserScheme
:
416 Field
= HTTP_URI_FIELD_SCHEME
;
419 case UrlParserAtInAuthority
:
421 case UrlParserAuthority
:
422 Field
= HTTP_URI_FIELD_AUTHORITY
;
426 Field
= HTTP_URI_FIELD_PATH
;
430 Field
= HTTP_URI_FIELD_QUERY
;
433 case UrlParserFragment
:
434 Field
= HTTP_URI_FIELD_FRAGMENT
;
442 // Field not changed, count the length.
444 ASSERT (Field
< HTTP_URI_FIELD_MAX
);
445 if (Field
== OldField
) {
446 Parser
->FieldData
[Field
].Length
++;
453 Parser
->FieldBitMap
|= BIT (Field
);
454 Parser
->FieldData
[Field
].Offset
= (UINT32
) (Char
- Url
);
455 Parser
->FieldData
[Field
].Length
= 1;
460 // If has authority component, continue to parse the username, host and port.
462 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_AUTHORITY
)) != 0) {
463 Status
= NetHttpParseAuthority (Url
, FoundAt
, Parser
);
464 if (EFI_ERROR (Status
)) {
475 Get the Hostname from a HTTP URL.
477 This function will return the HostName according to the Url and previous parse result ,and
478 it is the caller's responsibility to free the buffer returned in *HostName.
480 @param[in] Url The pointer to a HTTP URL string.
481 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
482 @param[out] HostName Pointer to a buffer to store the HostName.
484 @retval EFI_SUCCESS Successfully get the required component.
485 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.
486 @retval EFI_NOT_FOUND No hostName component in the URL.
487 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
501 HTTP_URL_PARSER
*Parser
;
503 if (Url
== NULL
|| UrlParser
== NULL
|| HostName
== NULL
) {
504 return EFI_INVALID_PARAMETER
;
507 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
509 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_HOST
)) == 0) {
510 return EFI_NOT_FOUND
;
513 Name
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
+ 1);
515 return EFI_OUT_OF_RESOURCES
;
518 Status
= UriPercentDecode (
519 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Offset
,
520 Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
,
524 if (EFI_ERROR (Status
)) {
529 Name
[ResultLength
] = '\0';
536 Get the IPv4 address from a HTTP URL.
538 This function will return the IPv4 address according to the Url and previous parse result.
540 @param[in] Url The pointer to a HTTP URL string.
541 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
542 @param[out] Ip4Address Pointer to a buffer to store the IP address.
544 @retval EFI_SUCCESS Successfully get the required component.
545 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip4Address is NULL or UrlParser is invalid.
546 @retval EFI_NOT_FOUND No IPv4 address component in the URL.
547 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
555 OUT EFI_IPv4_ADDRESS
*Ip4Address
561 HTTP_URL_PARSER
*Parser
;
563 if (Url
== NULL
|| UrlParser
== NULL
|| Ip4Address
== NULL
) {
564 return EFI_INVALID_PARAMETER
;
567 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
569 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_HOST
)) == 0) {
570 return EFI_NOT_FOUND
;
573 Ip4String
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
+ 1);
574 if (Ip4String
== NULL
) {
575 return EFI_OUT_OF_RESOURCES
;
578 Status
= UriPercentDecode (
579 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Offset
,
580 Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
,
584 if (EFI_ERROR (Status
)) {
585 FreePool (Ip4String
);
589 Ip4String
[ResultLength
] = '\0';
590 Status
= NetLibAsciiStrToIp4 (Ip4String
, Ip4Address
);
591 FreePool (Ip4String
);
597 Get the IPv6 address from a HTTP URL.
599 This function will return the IPv6 address according to the Url and previous parse result.
601 @param[in] Url The pointer to a HTTP URL string.
602 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
603 @param[out] Ip6Address Pointer to a buffer to store the IP address.
605 @retval EFI_SUCCESS Successfully get the required component.
606 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip6Address is NULL or UrlParser is invalid.
607 @retval EFI_NOT_FOUND No IPv6 address component in the URL.
608 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
616 OUT EFI_IPv6_ADDRESS
*Ip6Address
624 HTTP_URL_PARSER
*Parser
;
626 if (Url
== NULL
|| UrlParser
== NULL
|| Ip6Address
== NULL
) {
627 return EFI_INVALID_PARAMETER
;
630 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
632 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_HOST
)) == 0) {
633 return EFI_NOT_FOUND
;
637 // IP-literal = "[" ( IPv6address / IPvFuture ) "]"
639 Length
= Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
;
641 return EFI_INVALID_PARAMETER
;
644 Ptr
= Url
+ Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Offset
;
645 if ((Ptr
[0] != '[') || (Ptr
[Length
- 1] != ']')) {
646 return EFI_INVALID_PARAMETER
;
649 Ip6String
= AllocatePool (Length
);
650 if (Ip6String
== NULL
) {
651 return EFI_OUT_OF_RESOURCES
;
654 Status
= UriPercentDecode (
660 if (EFI_ERROR (Status
)) {
661 FreePool (Ip6String
);
665 Ip6String
[ResultLength
] = '\0';
666 Status
= NetLibAsciiStrToIp6 (Ip6String
, Ip6Address
);
667 FreePool (Ip6String
);
673 Get the port number from a HTTP URL.
675 This function will return the port number according to the Url and previous parse result.
677 @param[in] Url The pointer to a HTTP URL string.
678 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
679 @param[out] Port Pointer to a buffer to store the port number.
681 @retval EFI_SUCCESS Successfully get the required component.
682 @retval EFI_INVALID_PARAMETER Uri is NULL or Port is NULL or UrlParser is invalid.
683 @retval EFI_NOT_FOUND No port number in the URL.
684 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
700 HTTP_URL_PARSER
*Parser
;
702 if (Url
== NULL
|| UrlParser
== NULL
|| Port
== NULL
) {
703 return EFI_INVALID_PARAMETER
;
709 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
711 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_PORT
)) == 0) {
712 return EFI_NOT_FOUND
;
715 PortString
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Length
+ 1);
716 if (PortString
== NULL
) {
717 return EFI_OUT_OF_RESOURCES
;
720 Status
= UriPercentDecode (
721 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Offset
,
722 Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Length
,
726 if (EFI_ERROR (Status
)) {
730 PortString
[ResultLength
] = '\0';
732 while (Index
< ResultLength
) {
733 if (!NET_IS_DIGIT (PortString
[Index
])) {
734 Status
= EFI_INVALID_PARAMETER
;
740 Status
= AsciiStrDecimalToUintnS (Url
+ Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Offset
, (CHAR8
**) NULL
, &Data
);
742 if (Data
> HTTP_URI_PORT_MAX_NUM
) {
743 Status
= EFI_INVALID_PARAMETER
;
747 *Port
= (UINT16
) Data
;
750 FreePool (PortString
);
755 Get the Path from a HTTP URL.
757 This function will return the Path according to the Url and previous parse result,and
758 it is the caller's responsibility to free the buffer returned in *Path.
760 @param[in] Url The pointer to a HTTP URL string.
761 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
762 @param[out] Path Pointer to a buffer to store the Path.
764 @retval EFI_SUCCESS Successfully get the required component.
765 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.
766 @retval EFI_NOT_FOUND No hostName component in the URL.
767 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
781 HTTP_URL_PARSER
*Parser
;
783 if (Url
== NULL
|| UrlParser
== NULL
|| Path
== NULL
) {
784 return EFI_INVALID_PARAMETER
;
787 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
789 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_PATH
)) == 0) {
790 return EFI_NOT_FOUND
;
793 PathStr
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_PATH
].Length
+ 1);
794 if (PathStr
== NULL
) {
795 return EFI_OUT_OF_RESOURCES
;
798 Status
= UriPercentDecode (
799 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_PATH
].Offset
,
800 Parser
->FieldData
[HTTP_URI_FIELD_PATH
].Length
,
804 if (EFI_ERROR (Status
)) {
809 PathStr
[ResultLength
] = '\0';
815 Release the resource of the URL parser.
817 @param[in] UrlParser Pointer to the parser.
826 FreePool (UrlParser
);
830 Find a specified header field according to the field name.
832 @param[in] HeaderCount Number of HTTP header structures in Headers list.
833 @param[in] Headers Array containing list of HTTP headers.
834 @param[in] FieldName Null terminated string which describes a field name.
836 @return Pointer to the found header or NULL.
842 IN UINTN HeaderCount
,
843 IN EFI_HTTP_HEADER
*Headers
,
849 if (HeaderCount
== 0 || Headers
== NULL
|| FieldName
== NULL
) {
853 for (Index
= 0; Index
< HeaderCount
; Index
++){
855 // Field names are case-insensitive (RFC 2616).
857 if (AsciiStriCmp (Headers
[Index
].FieldName
, FieldName
) == 0) {
858 return &Headers
[Index
];
866 BodyParserBodyIdentity
,
867 BodyParserChunkSizeStart
,
869 BodyParserChunkSizeEndCR
,
870 BodyParserChunkExtStart
,
871 BodyParserChunkDataStart
,
872 BodyParserChunkDataEnd
,
873 BodyParserChunkDataEndCR
,
876 BodyParserLastCRLFEnd
,
879 } HTTP_BODY_PARSE_STATE
;
882 BOOLEAN IgnoreBody
; // "MUST NOT" include a message-body
883 BOOLEAN IsChunked
; // "chunked" transfer-coding.
884 BOOLEAN ContentLengthIsValid
;
885 UINTN ContentLength
; // Entity length (not the message-body length), invalid until ContentLengthIsValid is TRUE
887 HTTP_BODY_PARSER_CALLBACK Callback
;
889 UINTN ParsedBodyLength
;
890 HTTP_BODY_PARSE_STATE State
;
891 UINTN CurrentChunkSize
;
892 UINTN CurrentChunkParsedSize
;
896 Convert an hexadecimal char to a value of type UINTN.
898 @param[in] Char Ascii character.
900 @return Value translated from Char.
904 HttpIoHexCharToUintn (
908 if (Char
>= '0' && Char
<= '9') {
912 return (10 + AsciiCharToUpper (Char
) - 'A');
916 Get the value of the content length if there is a "Content-Length" header.
918 @param[in] HeaderCount Number of HTTP header structures in Headers.
919 @param[in] Headers Array containing list of HTTP headers.
920 @param[out] ContentLength Pointer to save the value of the content length.
922 @retval EFI_SUCCESS Successfully get the content length.
923 @retval EFI_NOT_FOUND No "Content-Length" header in the Headers.
927 HttpIoParseContentLengthHeader (
928 IN UINTN HeaderCount
,
929 IN EFI_HTTP_HEADER
*Headers
,
930 OUT UINTN
*ContentLength
933 EFI_HTTP_HEADER
*Header
;
935 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_CONTENT_LENGTH
);
936 if (Header
== NULL
) {
937 return EFI_NOT_FOUND
;
940 return AsciiStrDecimalToUintnS (Header
->FieldValue
, (CHAR8
**) NULL
, ContentLength
);
945 Check whether the HTTP message is using the "chunked" transfer-coding.
947 @param[in] HeaderCount Number of HTTP header structures in Headers.
948 @param[in] Headers Array containing list of HTTP headers.
950 @return The message is "chunked" transfer-coding (TRUE) or not (FALSE).
955 IN UINTN HeaderCount
,
956 IN EFI_HTTP_HEADER
*Headers
959 EFI_HTTP_HEADER
*Header
;
962 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_TRANSFER_ENCODING
);
963 if (Header
== NULL
) {
967 if (AsciiStriCmp (Header
->FieldValue
, "identity") != 0) {
975 Check whether the HTTP message should have a message-body.
977 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.
978 @param[in] StatusCode Response status code returned by the remote host.
980 @return The message should have a message-body (FALSE) or not (TRUE).
984 HttpIoNoMessageBody (
985 IN EFI_HTTP_METHOD Method
,
986 IN EFI_HTTP_STATUS_CODE StatusCode
991 // All responses to the HEAD request method
992 // MUST NOT include a message-body, even though the presence of entity-
993 // header fields might lead one to believe they do. All 1xx
994 // (informational), 204 (no content), and 304 (not modified) responses
995 // MUST NOT include a message-body. All other responses do include a
996 // message-body, although it MAY be of zero length.
998 if (Method
== HttpMethodHead
) {
1002 if ((StatusCode
== HTTP_STATUS_100_CONTINUE
) ||
1003 (StatusCode
== HTTP_STATUS_101_SWITCHING_PROTOCOLS
) ||
1004 (StatusCode
== HTTP_STATUS_204_NO_CONTENT
) ||
1005 (StatusCode
== HTTP_STATUS_304_NOT_MODIFIED
))
1014 Initialize a HTTP message-body parser.
1016 This function will create and initialize a HTTP message parser according to caller provided HTTP message
1017 header information. It is the caller's responsibility to free the buffer returned in *UrlParser by HttpFreeMsgParser().
1019 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.
1020 @param[in] StatusCode Response status code returned by the remote host.
1021 @param[in] HeaderCount Number of HTTP header structures in Headers.
1022 @param[in] Headers Array containing list of HTTP headers.
1023 @param[in] Callback Callback function that is invoked when parsing the HTTP message-body,
1024 set to NULL to ignore all events.
1025 @param[in] Context Pointer to the context that will be passed to Callback.
1026 @param[out] MsgParser Pointer to the returned buffer to store the message parser.
1028 @retval EFI_SUCCESS Successfully initialized the parser.
1029 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
1030 @retval EFI_INVALID_PARAMETER MsgParser is NULL or HeaderCount is not NULL but Headers is NULL.
1031 @retval Others Failed to initialize the parser.
1037 IN EFI_HTTP_METHOD Method
,
1038 IN EFI_HTTP_STATUS_CODE StatusCode
,
1039 IN UINTN HeaderCount
,
1040 IN EFI_HTTP_HEADER
*Headers
,
1041 IN HTTP_BODY_PARSER_CALLBACK Callback
,
1043 OUT VOID
**MsgParser
1047 HTTP_BODY_PARSER
*Parser
;
1049 if (HeaderCount
!= 0 && Headers
== NULL
) {
1050 return EFI_INVALID_PARAMETER
;
1053 if (MsgParser
== NULL
) {
1054 return EFI_INVALID_PARAMETER
;
1057 Parser
= AllocateZeroPool (sizeof (HTTP_BODY_PARSER
));
1058 if (Parser
== NULL
) {
1059 return EFI_OUT_OF_RESOURCES
;
1062 Parser
->State
= BodyParserBodyStart
;
1065 // Determine the message length according to RFC 2616.
1066 // 1. Check whether the message "MUST NOT" have a message-body.
1068 Parser
->IgnoreBody
= HttpIoNoMessageBody (Method
, StatusCode
);
1070 // 2. Check whether the message using "chunked" transfer-coding.
1072 Parser
->IsChunked
= HttpIoIsChunked (HeaderCount
, Headers
);
1074 // 3. Check whether the message has a Content-Length header field.
1076 Status
= HttpIoParseContentLengthHeader (HeaderCount
, Headers
, &Parser
->ContentLength
);
1077 if (!EFI_ERROR (Status
)) {
1078 Parser
->ContentLengthIsValid
= TRUE
;
1081 // 4. Range header is not supported now, so we won't meet media type "multipart/byteranges".
1082 // 5. By server closing the connection
1086 // Set state to skip body parser if the message shouldn't have a message body.
1088 if (Parser
->IgnoreBody
) {
1089 Parser
->State
= BodyParserComplete
;
1091 Parser
->Callback
= Callback
;
1092 Parser
->Context
= Context
;
1095 *MsgParser
= Parser
;
1102 Parse BodyLength of message-body. This function can be called repeatedly to parse the message-body partially.
1104 @param[in, out] MsgParser Pointer to the message parser.
1105 @param[in] BodyLength Length in bytes of the Body.
1106 @param[in] Body Pointer to the buffer of the message-body to be parsed.
1108 @retval EFI_SUCCESS Successfully parse the message-body.
1109 @retval EFI_INVALID_PARAMETER MsgParser is NULL or Body is NULL or BodyLength is 0.
1110 @retval EFI_ABORTED Operation aborted.
1111 @retval Other Error happened while parsing message body.
1116 HttpParseMessageBody (
1117 IN OUT VOID
*MsgParser
,
1118 IN UINTN BodyLength
,
1123 UINTN RemainderLengthInThis
;
1124 UINTN LengthForCallback
;
1126 HTTP_BODY_PARSER
*Parser
;
1128 if (BodyLength
== 0 || Body
== NULL
) {
1129 return EFI_INVALID_PARAMETER
;
1132 if (MsgParser
== NULL
) {
1133 return EFI_INVALID_PARAMETER
;
1136 Parser
= (HTTP_BODY_PARSER
*) MsgParser
;
1138 if (Parser
->IgnoreBody
) {
1139 Parser
->State
= BodyParserComplete
;
1140 if (Parser
->Callback
!= NULL
) {
1141 Status
= Parser
->Callback (
1142 BodyParseEventOnComplete
,
1147 if (EFI_ERROR (Status
)) {
1154 if (Parser
->State
== BodyParserBodyStart
) {
1155 Parser
->ParsedBodyLength
= 0;
1156 if (Parser
->IsChunked
) {
1157 Parser
->State
= BodyParserChunkSizeStart
;
1159 Parser
->State
= BodyParserBodyIdentity
;
1164 // The message body might be truncated in anywhere, so we need to parse is byte-by-byte.
1166 for (Char
= Body
; Char
< Body
+ BodyLength
; ) {
1168 switch (Parser
->State
) {
1169 case BodyParserStateMax
:
1172 case BodyParserBodyIdentity
:
1174 // Identity transfer-coding, just notify user to save the body data.
1176 if (Parser
->Callback
!= NULL
) {
1177 Status
= Parser
->Callback (
1178 BodyParseEventOnData
,
1180 MIN (BodyLength
, Parser
->ContentLength
- Parser
->ParsedBodyLength
),
1183 if (EFI_ERROR (Status
)) {
1187 Char
+= MIN (BodyLength
, Parser
->ContentLength
- Parser
->ParsedBodyLength
);
1188 Parser
->ParsedBodyLength
+= MIN (BodyLength
, Parser
->ContentLength
- Parser
->ParsedBodyLength
);
1189 if (Parser
->ParsedBodyLength
== Parser
->ContentLength
) {
1190 Parser
->State
= BodyParserComplete
;
1191 if (Parser
->Callback
!= NULL
) {
1192 Status
= Parser
->Callback (
1193 BodyParseEventOnComplete
,
1198 if (EFI_ERROR (Status
)) {
1205 case BodyParserChunkSizeStart
:
1207 // First byte of chunk-size, the chunk-size might be truncated.
1209 Parser
->CurrentChunkSize
= 0;
1210 Parser
->State
= BodyParserChunkSize
;
1211 case BodyParserChunkSize
:
1212 if (!NET_IS_HEX_CHAR (*Char
)) {
1214 Parser
->State
= BodyParserChunkExtStart
;
1216 } else if (*Char
== '\r') {
1217 Parser
->State
= BodyParserChunkSizeEndCR
;
1220 Parser
->State
= BodyParserStateMax
;
1225 if (Parser
->CurrentChunkSize
> (((~((UINTN
) 0)) - 16) / 16)) {
1226 return EFI_INVALID_PARAMETER
;
1228 Parser
->CurrentChunkSize
= Parser
->CurrentChunkSize
* 16 + HttpIoHexCharToUintn (*Char
);
1232 case BodyParserChunkExtStart
:
1234 // Ignore all the chunk extensions.
1236 if (*Char
== '\r') {
1237 Parser
->State
= BodyParserChunkSizeEndCR
;
1242 case BodyParserChunkSizeEndCR
:
1243 if (*Char
!= '\n') {
1244 Parser
->State
= BodyParserStateMax
;
1248 if (Parser
->CurrentChunkSize
== 0) {
1250 // The last chunk has been parsed and now assumed the state
1251 // of HttpBodyParse is ParserLastCRLF. So it need to decide
1252 // whether the rest message is trailer or last CRLF in the next round.
1254 Parser
->ContentLengthIsValid
= TRUE
;
1255 Parser
->State
= BodyParserLastCRLF
;
1258 Parser
->State
= BodyParserChunkDataStart
;
1259 Parser
->CurrentChunkParsedSize
= 0;
1262 case BodyParserLastCRLF
:
1264 // Judge the byte is belong to the Last CRLF or trailer, and then
1265 // configure the state of HttpBodyParse to corresponding state.
1267 if (*Char
== '\r') {
1269 Parser
->State
= BodyParserLastCRLFEnd
;
1272 Parser
->State
= BodyParserTrailer
;
1276 case BodyParserLastCRLFEnd
:
1277 if (*Char
== '\n') {
1278 Parser
->State
= BodyParserComplete
;
1280 if (Parser
->Callback
!= NULL
) {
1281 Status
= Parser
->Callback (
1282 BodyParseEventOnComplete
,
1287 if (EFI_ERROR (Status
)) {
1293 Parser
->State
= BodyParserStateMax
;
1297 case BodyParserTrailer
:
1298 if (*Char
== '\r') {
1299 Parser
->State
= BodyParserChunkSizeEndCR
;
1304 case BodyParserChunkDataStart
:
1306 // First byte of chunk-data, the chunk data also might be truncated.
1308 RemainderLengthInThis
= BodyLength
- (Char
- Body
);
1309 LengthForCallback
= MIN (Parser
->CurrentChunkSize
- Parser
->CurrentChunkParsedSize
, RemainderLengthInThis
);
1310 if (Parser
->Callback
!= NULL
) {
1311 Status
= Parser
->Callback (
1312 BodyParseEventOnData
,
1317 if (EFI_ERROR (Status
)) {
1321 Char
+= LengthForCallback
;
1322 Parser
->ContentLength
+= LengthForCallback
;
1323 Parser
->CurrentChunkParsedSize
+= LengthForCallback
;
1324 if (Parser
->CurrentChunkParsedSize
== Parser
->CurrentChunkSize
) {
1325 Parser
->State
= BodyParserChunkDataEnd
;
1329 case BodyParserChunkDataEnd
:
1330 if (*Char
== '\r') {
1331 Parser
->State
= BodyParserChunkDataEndCR
;
1333 Parser
->State
= BodyParserStateMax
;
1338 case BodyParserChunkDataEndCR
:
1339 if (*Char
!= '\n') {
1340 Parser
->State
= BodyParserStateMax
;
1344 Parser
->State
= BodyParserChunkSizeStart
;
1353 if (Parser
->State
== BodyParserStateMax
) {
1361 Check whether the message-body is complete or not.
1363 @param[in] MsgParser Pointer to the message parser.
1365 @retval TRUE Message-body is complete.
1366 @retval FALSE Message-body is not complete.
1371 HttpIsMessageComplete (
1375 HTTP_BODY_PARSER
*Parser
;
1377 if (MsgParser
== NULL
) {
1381 Parser
= (HTTP_BODY_PARSER
*) MsgParser
;
1383 if (Parser
->State
== BodyParserComplete
) {
1390 Get the content length of the entity.
1392 Note that in trunk transfer, the entity length is not valid until the whole message body is received.
1394 @param[in] MsgParser Pointer to the message parser.
1395 @param[out] ContentLength Pointer to store the length of the entity.
1397 @retval EFI_SUCCESS Successfully to get the entity length.
1398 @retval EFI_NOT_READY Entity length is not valid yet.
1399 @retval EFI_INVALID_PARAMETER MsgParser is NULL or ContentLength is NULL.
1404 HttpGetEntityLength (
1406 OUT UINTN
*ContentLength
1409 HTTP_BODY_PARSER
*Parser
;
1411 if (MsgParser
== NULL
|| ContentLength
== NULL
) {
1412 return EFI_INVALID_PARAMETER
;
1415 Parser
= (HTTP_BODY_PARSER
*) MsgParser
;
1417 if (!Parser
->ContentLengthIsValid
) {
1418 return EFI_NOT_READY
;
1421 *ContentLength
= Parser
->ContentLength
;
1426 Release the resource of the message parser.
1428 @param[in] MsgParser Pointer to the message parser.
1437 FreePool (MsgParser
);
1442 Get the next string, which is distinguished by specified separator.
1444 @param[in] String Pointer to the string.
1445 @param[in] Separator Specified separator used to distinguish where is the beginning
1448 @return Pointer to the next string.
1449 @return NULL if not find or String is NULL.
1453 AsciiStrGetNextToken (
1454 IN CONST CHAR8
*String
,
1465 if (*Token
== Separator
) {
1466 return (CHAR8
*)(Token
+ 1);
1473 Set FieldName and FieldValue into specified HttpHeader.
1475 @param[in,out] HttpHeader Specified HttpHeader.
1476 @param[in] FieldName FieldName of this HttpHeader, a NULL terminated ASCII string.
1477 @param[in] FieldValue FieldValue of this HttpHeader, a NULL terminated ASCII string.
1480 @retval EFI_SUCCESS The FieldName and FieldValue are set into HttpHeader successfully.
1481 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1482 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
1487 HttpSetFieldNameAndValue (
1488 IN OUT EFI_HTTP_HEADER
*HttpHeader
,
1489 IN CONST CHAR8
*FieldName
,
1490 IN CONST CHAR8
*FieldValue
1493 UINTN FieldNameSize
;
1494 UINTN FieldValueSize
;
1496 if (HttpHeader
== NULL
|| FieldName
== NULL
|| FieldValue
== NULL
) {
1497 return EFI_INVALID_PARAMETER
;
1500 if (HttpHeader
->FieldName
!= NULL
) {
1501 FreePool (HttpHeader
->FieldName
);
1503 if (HttpHeader
->FieldValue
!= NULL
) {
1504 FreePool (HttpHeader
->FieldValue
);
1507 FieldNameSize
= AsciiStrSize (FieldName
);
1508 HttpHeader
->FieldName
= AllocateZeroPool (FieldNameSize
);
1509 if (HttpHeader
->FieldName
== NULL
) {
1510 return EFI_OUT_OF_RESOURCES
;
1512 CopyMem (HttpHeader
->FieldName
, FieldName
, FieldNameSize
);
1513 HttpHeader
->FieldName
[FieldNameSize
- 1] = 0;
1515 FieldValueSize
= AsciiStrSize (FieldValue
);
1516 HttpHeader
->FieldValue
= AllocateZeroPool (FieldValueSize
);
1517 if (HttpHeader
->FieldValue
== NULL
) {
1518 FreePool (HttpHeader
->FieldName
);
1519 return EFI_OUT_OF_RESOURCES
;
1521 CopyMem (HttpHeader
->FieldValue
, FieldValue
, FieldValueSize
);
1522 HttpHeader
->FieldValue
[FieldValueSize
- 1] = 0;
1528 Get one key/value header pair from the raw string.
1530 @param[in] String Pointer to the raw string.
1531 @param[out] FieldName Points directly to field name within 'HttpHeader'.
1532 @param[out] FieldValue Points directly to field value within 'HttpHeader'.
1534 @return Pointer to the next raw string.
1535 @return NULL if no key/value header pair from this raw string.
1540 HttpGetFieldNameAndValue (
1542 OUT CHAR8
**FieldName
,
1543 OUT CHAR8
**FieldValue
1546 CHAR8
*FieldNameStr
;
1547 CHAR8
*FieldValueStr
;
1551 if (String
== NULL
|| FieldName
== NULL
|| FieldValue
== NULL
) {
1557 FieldNameStr
= NULL
;
1558 FieldValueStr
= NULL
;
1564 // Check whether the raw HTTP header string is valid or not.
1566 EndofHeader
= AsciiStrStr (String
, "\r\n\r\n");
1567 if (EndofHeader
== NULL
) {
1572 // Each header field consists of a name followed by a colon (":") and the field value.
1573 // The field value MAY be preceded by any amount of LWS, though a single SP is preferred.
1575 // message-header = field-name ":" [ field-value ]
1576 // field-name = token
1577 // field-value = *( field-content | LWS )
1579 // Note: "*(element)" allows any number element, including zero; "1*(element)" requires at least one element.
1580 // [element] means element is optional.
1581 // LWS = [CRLF] 1*(SP|HT), it can be ' ' or '\t' or '\r\n ' or '\r\n\t'.
1586 FieldNameStr
= String
;
1587 FieldValueStr
= AsciiStrGetNextToken (FieldNameStr
, ':');
1588 if (FieldValueStr
== NULL
) {
1593 // Replace ':' with 0, then FieldName has been retrived from String.
1595 *(FieldValueStr
- 1) = 0;
1598 // Handle FieldValueStr, skip all the preceded LWS.
1601 if (*FieldValueStr
== ' ' || *FieldValueStr
== '\t') {
1603 // Boundary condition check.
1605 if ((UINTN
) EndofHeader
- (UINTN
) FieldValueStr
< 1) {
1607 // Wrong String format!
1613 } else if (*FieldValueStr
== '\r') {
1615 // Boundary condition check.
1617 if ((UINTN
) EndofHeader
- (UINTN
) FieldValueStr
< 3) {
1619 // No more preceded LWS, so break here.
1624 if (*(FieldValueStr
+ 1) == '\n' ) {
1625 if (*(FieldValueStr
+ 2) == ' ' || *(FieldValueStr
+ 2) == '\t') {
1626 FieldValueStr
= FieldValueStr
+ 3;
1629 // No more preceded LWS, so break here.
1635 // Wrong String format!
1641 // No more preceded LWS, so break here.
1647 StrPtr
= FieldValueStr
;
1650 // Handle the LWS within the field value.
1652 StrPtr
= AsciiStrGetNextToken (StrPtr
, '\r');
1653 if (StrPtr
== NULL
|| *StrPtr
!= '\n') {
1655 // Wrong String format!
1661 } while (*StrPtr
== ' ' || *StrPtr
== '\t');
1664 // Replace '\r' with 0
1669 // Get FieldName and FieldValue.
1671 *FieldName
= FieldNameStr
;
1672 *FieldValue
= FieldValueStr
;
1678 Free existing HeaderFields.
1680 @param[in] HeaderFields Pointer to array of key/value header pairs waitting for free.
1681 @param[in] FieldCount The number of header pairs in HeaderFields.
1686 HttpFreeHeaderFields (
1687 IN EFI_HTTP_HEADER
*HeaderFields
,
1693 if (HeaderFields
!= NULL
) {
1694 for (Index
= 0; Index
< FieldCount
; Index
++) {
1695 if (HeaderFields
[Index
].FieldName
!= NULL
) {
1696 FreePool (HeaderFields
[Index
].FieldName
);
1698 if (HeaderFields
[Index
].FieldValue
!= NULL
) {
1699 FreePool (HeaderFields
[Index
].FieldValue
);
1703 FreePool (HeaderFields
);
1708 Generate HTTP request message.
1710 This function will allocate memory for the whole HTTP message and generate a
1711 well formatted HTTP Request message in it, include the Request-Line, header
1712 fields and also the message body. It is the caller's responsibility to free
1713 the buffer returned in *RequestMsg.
1715 @param[in] Message Pointer to the EFI_HTTP_MESSAGE structure which
1716 contains the required information to generate
1717 the HTTP request message.
1718 @param[in] Url The URL of a remote host.
1719 @param[out] RequestMsg Pointer to the created HTTP request message.
1720 NULL if any error occured.
1721 @param[out] RequestMsgSize Size of the RequestMsg (in bytes).
1723 @retval EFI_SUCCESS If HTTP request string was created successfully.
1724 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
1725 @retval EFI_INVALID_PARAMETER The input arguments are invalid.
1730 HttpGenRequestMessage (
1731 IN CONST EFI_HTTP_MESSAGE
*Message
,
1732 IN CONST CHAR8
*Url
,
1733 OUT CHAR8
**RequestMsg
,
1734 OUT UINTN
*RequestMsgSize
1744 EFI_HTTP_HEADER
**AppendList
;
1746 EFI_HTTP_UTILITIES_PROTOCOL
*HttpUtilitiesProtocol
;
1748 Status
= EFI_SUCCESS
;
1754 HttpUtilitiesProtocol
= NULL
;
1757 // 1. If we have a Request, we cannot have a NULL Url
1758 // 2. If we have a Request, HeaderCount can not be non-zero
1759 // 3. If we do not have a Request, HeaderCount should be zero
1760 // 4. If we do not have Request and Headers, we need at least a message-body
1762 if ((Message
== NULL
|| RequestMsg
== NULL
|| RequestMsgSize
== NULL
) ||
1763 (Message
->Data
.Request
!= NULL
&& Url
== NULL
) ||
1764 (Message
->Data
.Request
!= NULL
&& Message
->HeaderCount
== 0) ||
1765 (Message
->Data
.Request
== NULL
&& Message
->HeaderCount
!= 0) ||
1766 (Message
->Data
.Request
== NULL
&& Message
->HeaderCount
== 0 && Message
->BodyLength
== 0)) {
1767 return EFI_INVALID_PARAMETER
;
1770 if (Message
->HeaderCount
!= 0) {
1772 // Locate the HTTP_UTILITIES protocol.
1774 Status
= gBS
->LocateProtocol (
1775 &gEfiHttpUtilitiesProtocolGuid
,
1777 (VOID
**) &HttpUtilitiesProtocol
1780 if (EFI_ERROR (Status
)) {
1781 DEBUG ((DEBUG_ERROR
,"Failed to locate Http Utilities protocol. Status = %r.\n", Status
));
1786 // Build AppendList to send into HttpUtilitiesBuild
1788 AppendList
= AllocateZeroPool (sizeof (EFI_HTTP_HEADER
*) * (Message
->HeaderCount
));
1789 if (AppendList
== NULL
) {
1790 return EFI_OUT_OF_RESOURCES
;
1793 for(Index
= 0; Index
< Message
->HeaderCount
; Index
++){
1794 AppendList
[Index
] = &Message
->Headers
[Index
];
1798 // Build raw HTTP Headers
1800 Status
= HttpUtilitiesProtocol
->Build (
1801 HttpUtilitiesProtocol
,
1806 Message
->HeaderCount
,
1812 FreePool (AppendList
);
1814 if (EFI_ERROR (Status
) || HttpHdr
== NULL
){
1820 // If we have headers to be sent, account for it.
1822 if (Message
->HeaderCount
!= 0) {
1823 MsgSize
= HttpHdrSize
;
1827 // If we have a request line, account for the fields.
1829 if (Message
->Data
.Request
!= NULL
) {
1830 MsgSize
+= HTTP_METHOD_MAXIMUM_LEN
+ AsciiStrLen (HTTP_VERSION_CRLF_STR
) + AsciiStrLen (Url
);
1835 // If we have a message body to be sent, account for it.
1837 MsgSize
+= Message
->BodyLength
;
1840 // memory for the string that needs to be sent to TCP
1843 *RequestMsg
= AllocateZeroPool (MsgSize
);
1844 if (*RequestMsg
== NULL
) {
1845 Status
= EFI_OUT_OF_RESOURCES
;
1849 RequestPtr
= *RequestMsg
;
1851 // Construct header request
1853 if (Message
->Data
.Request
!= NULL
) {
1854 switch (Message
->Data
.Request
->Method
) {
1856 StrLength
= sizeof (HTTP_METHOD_GET
) - 1;
1857 CopyMem (RequestPtr
, HTTP_METHOD_GET
, StrLength
);
1858 RequestPtr
+= StrLength
;
1861 StrLength
= sizeof (HTTP_METHOD_PUT
) - 1;
1862 CopyMem (RequestPtr
, HTTP_METHOD_PUT
, StrLength
);
1863 RequestPtr
+= StrLength
;
1865 case HttpMethodPatch
:
1866 StrLength
= sizeof (HTTP_METHOD_PATCH
) - 1;
1867 CopyMem (RequestPtr
, HTTP_METHOD_PATCH
, StrLength
);
1868 RequestPtr
+= StrLength
;
1870 case HttpMethodPost
:
1871 StrLength
= sizeof (HTTP_METHOD_POST
) - 1;
1872 CopyMem (RequestPtr
, HTTP_METHOD_POST
, StrLength
);
1873 RequestPtr
+= StrLength
;
1875 case HttpMethodHead
:
1876 StrLength
= sizeof (HTTP_METHOD_HEAD
) - 1;
1877 CopyMem (RequestPtr
, HTTP_METHOD_HEAD
, StrLength
);
1878 RequestPtr
+= StrLength
;
1880 case HttpMethodDelete
:
1881 StrLength
= sizeof (HTTP_METHOD_DELETE
) - 1;
1882 CopyMem (RequestPtr
, HTTP_METHOD_DELETE
, StrLength
);
1883 RequestPtr
+= StrLength
;
1887 Status
= EFI_INVALID_PARAMETER
;
1891 StrLength
= AsciiStrLen(EMPTY_SPACE
);
1892 CopyMem (RequestPtr
, EMPTY_SPACE
, StrLength
);
1893 RequestPtr
+= StrLength
;
1895 StrLength
= AsciiStrLen (Url
);
1896 CopyMem (RequestPtr
, Url
, StrLength
);
1897 RequestPtr
+= StrLength
;
1899 StrLength
= sizeof (HTTP_VERSION_CRLF_STR
) - 1;
1900 CopyMem (RequestPtr
, HTTP_VERSION_CRLF_STR
, StrLength
);
1901 RequestPtr
+= StrLength
;
1903 if (HttpHdr
!= NULL
) {
1907 CopyMem (RequestPtr
, HttpHdr
, HttpHdrSize
);
1908 RequestPtr
+= HttpHdrSize
;
1915 if (Message
->Body
!= NULL
) {
1916 CopyMem (RequestPtr
, Message
->Body
, Message
->BodyLength
);
1917 RequestPtr
+= Message
->BodyLength
;
1923 (*RequestMsgSize
) = (UINTN
)(RequestPtr
) - (UINTN
)(*RequestMsg
);
1929 if (*RequestMsg
!= NULL
) {
1930 FreePool (*RequestMsg
);
1936 if (HttpHdr
!= NULL
) {
1944 Translate the status code in HTTP message to EFI_HTTP_STATUS_CODE defined
1945 in UEFI 2.5 specification.
1947 @param[in] StatusCode The status code value in HTTP message.
1949 @return Value defined in EFI_HTTP_STATUS_CODE .
1952 EFI_HTTP_STATUS_CODE
1954 HttpMappingToStatusCode (
1958 switch (StatusCode
) {
1960 return HTTP_STATUS_100_CONTINUE
;
1962 return HTTP_STATUS_101_SWITCHING_PROTOCOLS
;
1964 return HTTP_STATUS_200_OK
;
1966 return HTTP_STATUS_201_CREATED
;
1968 return HTTP_STATUS_202_ACCEPTED
;
1970 return HTTP_STATUS_203_NON_AUTHORITATIVE_INFORMATION
;
1972 return HTTP_STATUS_204_NO_CONTENT
;
1974 return HTTP_STATUS_205_RESET_CONTENT
;
1976 return HTTP_STATUS_206_PARTIAL_CONTENT
;
1978 return HTTP_STATUS_300_MULTIPLE_CHOICES
;
1980 return HTTP_STATUS_301_MOVED_PERMANENTLY
;
1982 return HTTP_STATUS_302_FOUND
;
1984 return HTTP_STATUS_303_SEE_OTHER
;
1986 return HTTP_STATUS_304_NOT_MODIFIED
;
1988 return HTTP_STATUS_305_USE_PROXY
;
1990 return HTTP_STATUS_307_TEMPORARY_REDIRECT
;
1992 return HTTP_STATUS_308_PERMANENT_REDIRECT
;
1994 return HTTP_STATUS_400_BAD_REQUEST
;
1996 return HTTP_STATUS_401_UNAUTHORIZED
;
1998 return HTTP_STATUS_402_PAYMENT_REQUIRED
;
2000 return HTTP_STATUS_403_FORBIDDEN
;
2002 return HTTP_STATUS_404_NOT_FOUND
;
2004 return HTTP_STATUS_405_METHOD_NOT_ALLOWED
;
2006 return HTTP_STATUS_406_NOT_ACCEPTABLE
;
2008 return HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED
;
2010 return HTTP_STATUS_408_REQUEST_TIME_OUT
;
2012 return HTTP_STATUS_409_CONFLICT
;
2014 return HTTP_STATUS_410_GONE
;
2016 return HTTP_STATUS_411_LENGTH_REQUIRED
;
2018 return HTTP_STATUS_412_PRECONDITION_FAILED
;
2020 return HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE
;
2022 return HTTP_STATUS_414_REQUEST_URI_TOO_LARGE
;
2024 return HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE
;
2026 return HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED
;
2028 return HTTP_STATUS_417_EXPECTATION_FAILED
;
2030 return HTTP_STATUS_500_INTERNAL_SERVER_ERROR
;
2032 return HTTP_STATUS_501_NOT_IMPLEMENTED
;
2034 return HTTP_STATUS_502_BAD_GATEWAY
;
2036 return HTTP_STATUS_503_SERVICE_UNAVAILABLE
;
2038 return HTTP_STATUS_504_GATEWAY_TIME_OUT
;
2040 return HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED
;
2043 return HTTP_STATUS_UNSUPPORTED_STATUS
;
2048 Check whether header field called FieldName is in DeleteList.
2050 @param[in] DeleteList Pointer to array of key/value header pairs.
2051 @param[in] DeleteCount The number of header pairs.
2052 @param[in] FieldName Pointer to header field's name.
2054 @return TRUE if FieldName is not in DeleteList, that means this header field is valid.
2055 @return FALSE if FieldName is in DeleteList, that means this header field is invalid.
2060 HttpIsValidHttpHeader (
2061 IN CHAR8
*DeleteList
[],
2062 IN UINTN DeleteCount
,
2068 if (FieldName
== NULL
) {
2072 for (Index
= 0; Index
< DeleteCount
; Index
++) {
2073 if (DeleteList
[Index
] == NULL
) {
2077 if (AsciiStrCmp (FieldName
, DeleteList
[Index
]) == 0) {