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 - 2017, Intel Corporation. All rights reserved.<BR>
6 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at<BR>
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "DxeHttpLib.h"
22 Decode a percent-encoded URI component to the ASCII character.
24 Decode the input component in Buffer according to RFC 3986. The caller is responsible to make
25 sure ResultBuffer points to a buffer with size equal or greater than ((AsciiStrSize (Buffer))
28 @param[in] Buffer The pointer to a percent-encoded URI component.
29 @param[in] BufferLength Length of Buffer in bytes.
30 @param[out] ResultBuffer Point to the buffer to store the decode result.
31 @param[out] ResultLength Length of decoded string in ResultBuffer in bytes.
33 @retval EFI_SUCCESS Successfully decoded the URI.
34 @retval EFI_INVALID_PARAMETER Buffer is not a valid percent-encoded string.
41 IN UINT32 BufferLength
,
42 OUT CHAR8
*ResultBuffer
,
43 OUT UINT32
*ResultLength
50 if (Buffer
== NULL
|| BufferLength
== 0 || ResultBuffer
== NULL
) {
51 return EFI_INVALID_PARAMETER
;
57 while (Index
< BufferLength
) {
58 if (Buffer
[Index
] == '%') {
59 if (Index
+ 1 >= BufferLength
|| Index
+ 2 >= BufferLength
|| !NET_IS_HEX_CHAR (Buffer
[Index
+1]) || !NET_IS_HEX_CHAR (Buffer
[Index
+2])) {
60 return EFI_INVALID_PARAMETER
;
62 HexStr
[0] = Buffer
[Index
+1];
63 HexStr
[1] = Buffer
[Index
+2];
64 ResultBuffer
[Offset
] = (CHAR8
) AsciiStrHexToUintn (HexStr
);
67 ResultBuffer
[Offset
] = Buffer
[Index
];
73 *ResultLength
= (UINT32
) Offset
;
79 This function return the updated state according to the input state and next character of
82 @param[in] Char Next character.
83 @param[in] State Current value of the parser state machine.
84 @param[in] IsRightBracket TRUE if there is an sign ']' in the authority component and
85 indicates the next part is ':' before Port.
87 @return Updated state value.
90 NetHttpParseAuthorityChar (
92 IN HTTP_URL_PARSE_STATE State
,
93 IN BOOLEAN
*IsRightBracket
99 // The authority component is preceded by a double slash ("//") and is
100 // terminated by the next slash ("/"), question mark ("?"), or number
101 // sign ("#") character, or by the end of the URI.
103 if (Char
== ' ' || Char
== '\r' || Char
== '\n') {
104 return UrlParserStateMax
;
108 // authority = [ userinfo "@" ] host [ ":" port ]
111 case UrlParserUserInfo
:
113 return UrlParserHostStart
;
118 case UrlParserHostStart
:
120 return UrlParserHostIpv6
;
124 return UrlParserPortStart
;
127 return UrlParserHost
;
129 case UrlParserHostIpv6
:
131 *IsRightBracket
= TRUE
;
134 if (Char
== ':' && *IsRightBracket
) {
135 return UrlParserPortStart
;
137 return UrlParserHostIpv6
;
140 case UrlParserPortStart
:
141 return UrlParserPort
;
151 This function parse the authority component of the input URL and update the parser.
153 @param[in] Url The pointer to a HTTP URL string.
154 @param[in] FoundAt TRUE if there is an at sign ('@') in the authority component.
155 @param[in, out] UrlParser Pointer to the buffer of the parse result.
157 @retval EFI_SUCCESS Successfully parse the authority.
158 @retval Other Error happened.
162 NetHttpParseAuthority (
165 IN OUT HTTP_URL_PARSER
*UrlParser
171 HTTP_URL_PARSE_STATE State
;
174 BOOLEAN IsrightBracket
;
176 ASSERT ((UrlParser
->FieldBitMap
& BIT (HTTP_URI_FIELD_AUTHORITY
)) != 0);
179 // authority = [ userinfo "@" ] host [ ":" port ]
182 State
= UrlParserUserInfo
;
184 State
= UrlParserHost
;
187 IsrightBracket
= FALSE
;
188 Field
= HTTP_URI_FIELD_MAX
;
190 Authority
= Url
+ UrlParser
->FieldData
[HTTP_URI_FIELD_AUTHORITY
].Offset
;
191 Length
= UrlParser
->FieldData
[HTTP_URI_FIELD_AUTHORITY
].Length
;
192 for (Char
= Authority
; Char
< Authority
+ Length
; Char
++) {
193 State
= NetHttpParseAuthorityChar (*Char
, State
, &IsrightBracket
);
195 case UrlParserStateMax
:
196 return EFI_INVALID_PARAMETER
;
198 case UrlParserHostStart
:
199 case UrlParserPortStart
:
202 case UrlParserUserInfo
:
203 Field
= HTTP_URI_FIELD_USERINFO
;
207 Field
= HTTP_URI_FIELD_HOST
;
210 case UrlParserHostIpv6
:
211 Field
= HTTP_URI_FIELD_HOST
;
215 Field
= HTTP_URI_FIELD_PORT
;
223 // Field not changed, count the length.
225 ASSERT (Field
< HTTP_URI_FIELD_MAX
);
226 if (Field
== OldField
) {
227 UrlParser
->FieldData
[Field
].Length
++;
234 UrlParser
->FieldBitMap
|= BIT (Field
);
235 UrlParser
->FieldData
[Field
].Offset
= (UINT32
) (Char
- Url
);
236 UrlParser
->FieldData
[Field
].Length
= 1;
244 This function return the updated state according to the input state and next character of a URL.
246 @param[in] Char Next character.
247 @param[in] State Current value of the parser state machine.
249 @return Updated state value.
253 NetHttpParseUrlChar (
255 IN HTTP_URL_PARSE_STATE State
258 if (Char
== ' ' || Char
== '\r' || Char
== '\n') {
259 return UrlParserStateMax
;
263 // http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]
265 // Request-URI = "*" | absolute-URI | path-absolute | authority
267 // absolute-URI = scheme ":" hier-part [ "?" query ]
268 // path-absolute = "/" [ segment-nz *( "/" segment ) ]
269 // authority = [ userinfo "@" ] host [ ":" port ]
272 case UrlParserUrlStart
:
273 if (Char
== '*' || Char
== '/') {
274 return UrlParserPath
;
276 return UrlParserScheme
;
278 case UrlParserScheme
:
280 return UrlParserSchemeColon
;
284 case UrlParserSchemeColon
:
286 return UrlParserSchemeColonSlash
;
290 case UrlParserSchemeColonSlash
:
292 return UrlParserSchemeColonSlashSlash
;
296 case UrlParserAtInAuthority
:
298 return UrlParserStateMax
;
301 case UrlParserAuthority
:
302 case UrlParserSchemeColonSlashSlash
:
304 return UrlParserAtInAuthority
;
307 return UrlParserPath
;
310 return UrlParserQueryStart
;
313 return UrlParserFragmentStart
;
315 return UrlParserAuthority
;
319 return UrlParserQueryStart
;
322 return UrlParserFragmentStart
;
327 case UrlParserQueryStart
:
329 return UrlParserFragmentStart
;
331 return UrlParserQuery
;
333 case UrlParserFragmentStart
:
334 return UrlParserFragment
;
343 Create a URL parser for the input URL string.
345 This function will parse and dereference the input HTTP URL into it components. The original
346 content of the URL won't be modified and the result will be returned in UrlParser, which can
347 be used in other functions like NetHttpUrlGetHostName().
349 @param[in] Url The pointer to a HTTP URL string.
350 @param[in] Length Length of Url in bytes.
351 @param[in] IsConnectMethod Whether the Url is used in HTTP CONNECT method or not.
352 @param[out] UrlParser Pointer to the returned buffer to store the parse result.
354 @retval EFI_SUCCESS Successfully dereferenced the HTTP URL.
355 @retval EFI_INVALID_PARAMETER UrlParser is NULL or Url is not a valid HTTP URL.
356 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
364 IN BOOLEAN IsConnectMethod
,
368 HTTP_URL_PARSE_STATE State
;
374 HTTP_URL_PARSER
*Parser
;
378 if (Url
== NULL
|| Length
== 0 || UrlParser
== NULL
) {
379 return EFI_INVALID_PARAMETER
;
382 Parser
= AllocateZeroPool (sizeof (HTTP_URL_PARSER
));
383 if (Parser
== NULL
) {
384 return EFI_OUT_OF_RESOURCES
;
387 if (IsConnectMethod
) {
389 // According to RFC 2616, the authority form is only used by the CONNECT method.
391 State
= UrlParserAuthority
;
393 State
= UrlParserUrlStart
;
396 Field
= HTTP_URI_FIELD_MAX
;
399 for (Char
= Url
; Char
< Url
+ Length
; Char
++) {
401 // Update state machine according to next char.
403 State
= NetHttpParseUrlChar (*Char
, State
);
406 case UrlParserStateMax
:
408 return EFI_INVALID_PARAMETER
;
410 case UrlParserSchemeColon
:
411 case UrlParserSchemeColonSlash
:
412 case UrlParserSchemeColonSlashSlash
:
413 case UrlParserQueryStart
:
414 case UrlParserFragmentStart
:
416 // Skip all the delimiting char: "://" "?" "@"
420 case UrlParserScheme
:
421 Field
= HTTP_URI_FIELD_SCHEME
;
424 case UrlParserAtInAuthority
:
426 case UrlParserAuthority
:
427 Field
= HTTP_URI_FIELD_AUTHORITY
;
431 Field
= HTTP_URI_FIELD_PATH
;
435 Field
= HTTP_URI_FIELD_QUERY
;
438 case UrlParserFragment
:
439 Field
= HTTP_URI_FIELD_FRAGMENT
;
447 // Field not changed, count the length.
449 ASSERT (Field
< HTTP_URI_FIELD_MAX
);
450 if (Field
== OldField
) {
451 Parser
->FieldData
[Field
].Length
++;
458 Parser
->FieldBitMap
|= BIT (Field
);
459 Parser
->FieldData
[Field
].Offset
= (UINT32
) (Char
- Url
);
460 Parser
->FieldData
[Field
].Length
= 1;
465 // If has authority component, continue to parse the username, host and port.
467 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_AUTHORITY
)) != 0) {
468 Status
= NetHttpParseAuthority (Url
, FoundAt
, Parser
);
469 if (EFI_ERROR (Status
)) {
480 Get the Hostname from a HTTP URL.
482 This function will return the HostName according to the Url and previous parse result ,and
483 it is the caller's responsibility to free the buffer returned in *HostName.
485 @param[in] Url The pointer to a HTTP URL string.
486 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
487 @param[out] HostName Pointer to a buffer to store the HostName.
489 @retval EFI_SUCCESS Successfully get the required component.
490 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.
491 @retval EFI_NOT_FOUND No hostName component in the URL.
492 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
506 HTTP_URL_PARSER
*Parser
;
508 if (Url
== NULL
|| UrlParser
== NULL
|| HostName
== NULL
) {
509 return EFI_INVALID_PARAMETER
;
512 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
514 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_HOST
)) == 0) {
515 return EFI_NOT_FOUND
;
518 Name
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
+ 1);
520 return EFI_OUT_OF_RESOURCES
;
523 Status
= UriPercentDecode (
524 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Offset
,
525 Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
,
529 if (EFI_ERROR (Status
)) {
534 Name
[ResultLength
] = '\0';
541 Get the IPv4 address from a HTTP URL.
543 This function will return the IPv4 address according to the Url and previous parse result.
545 @param[in] Url The pointer to a HTTP URL string.
546 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
547 @param[out] Ip4Address Pointer to a buffer to store the IP address.
549 @retval EFI_SUCCESS Successfully get the required component.
550 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip4Address is NULL or UrlParser is invalid.
551 @retval EFI_NOT_FOUND No IPv4 address component in the URL.
552 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
560 OUT EFI_IPv4_ADDRESS
*Ip4Address
566 HTTP_URL_PARSER
*Parser
;
568 if (Url
== NULL
|| UrlParser
== NULL
|| Ip4Address
== NULL
) {
569 return EFI_INVALID_PARAMETER
;
572 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
574 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_HOST
)) == 0) {
575 return EFI_INVALID_PARAMETER
;
578 Ip4String
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
+ 1);
579 if (Ip4String
== NULL
) {
580 return EFI_OUT_OF_RESOURCES
;
583 Status
= UriPercentDecode (
584 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Offset
,
585 Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
,
589 if (EFI_ERROR (Status
)) {
590 FreePool (Ip4String
);
594 Ip4String
[ResultLength
] = '\0';
595 Status
= NetLibAsciiStrToIp4 (Ip4String
, Ip4Address
);
596 FreePool (Ip4String
);
602 Get the IPv6 address from a HTTP URL.
604 This function will return the IPv6 address according to the Url and previous parse result.
606 @param[in] Url The pointer to a HTTP URL string.
607 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
608 @param[out] Ip6Address Pointer to a buffer to store the IP address.
610 @retval EFI_SUCCESS Successfully get the required component.
611 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip6Address is NULL or UrlParser is invalid.
612 @retval EFI_NOT_FOUND No IPv6 address component in the URL.
613 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
621 OUT EFI_IPv6_ADDRESS
*Ip6Address
629 HTTP_URL_PARSER
*Parser
;
631 if (Url
== NULL
|| UrlParser
== NULL
|| Ip6Address
== NULL
) {
632 return EFI_INVALID_PARAMETER
;
635 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
637 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_HOST
)) == 0) {
638 return EFI_INVALID_PARAMETER
;
642 // IP-literal = "[" ( IPv6address / IPvFuture ) "]"
644 Length
= Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
;
646 return EFI_INVALID_PARAMETER
;
649 Ptr
= Url
+ Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Offset
;
650 if ((Ptr
[0] != '[') || (Ptr
[Length
- 1] != ']')) {
651 return EFI_INVALID_PARAMETER
;
654 Ip6String
= AllocatePool (Length
);
655 if (Ip6String
== NULL
) {
656 return EFI_OUT_OF_RESOURCES
;
659 Status
= UriPercentDecode (
665 if (EFI_ERROR (Status
)) {
666 FreePool (Ip6String
);
670 Ip6String
[ResultLength
] = '\0';
671 Status
= NetLibAsciiStrToIp6 (Ip6String
, Ip6Address
);
672 FreePool (Ip6String
);
678 Get the port number from a HTTP URL.
680 This function will return the port number according to the Url and previous parse result.
682 @param[in] Url The pointer to a HTTP URL string.
683 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
684 @param[out] Port Pointer to a buffer to store the port number.
686 @retval EFI_SUCCESS Successfully get the required component.
687 @retval EFI_INVALID_PARAMETER Uri is NULL or Port is NULL or UrlParser is invalid.
688 @retval EFI_NOT_FOUND No port number in the URL.
689 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
705 HTTP_URL_PARSER
*Parser
;
707 if (Url
== NULL
|| UrlParser
== NULL
|| Port
== NULL
) {
708 return EFI_INVALID_PARAMETER
;
714 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
716 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_PORT
)) == 0) {
717 return EFI_INVALID_PARAMETER
;
720 PortString
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Length
+ 1);
721 if (PortString
== NULL
) {
722 return EFI_OUT_OF_RESOURCES
;
725 Status
= UriPercentDecode (
726 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Offset
,
727 Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Length
,
731 if (EFI_ERROR (Status
)) {
735 PortString
[ResultLength
] = '\0';
737 while (Index
< ResultLength
) {
738 if (!NET_IS_DIGIT (PortString
[Index
])) {
739 Status
= EFI_INVALID_PARAMETER
;
745 Status
= AsciiStrDecimalToUintnS (Url
+ Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Offset
, (CHAR8
**) NULL
, &Data
);
747 if (Data
> HTTP_URI_PORT_MAX_NUM
) {
748 Status
= EFI_INVALID_PARAMETER
;
752 *Port
= (UINT16
) Data
;
755 FreePool (PortString
);
760 Get the Path from a HTTP URL.
762 This function will return the Path according to the Url and previous parse result,and
763 it is the caller's responsibility to free the buffer returned in *Path.
765 @param[in] Url The pointer to a HTTP URL string.
766 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
767 @param[out] Path Pointer to a buffer to store the Path.
769 @retval EFI_SUCCESS Successfully get the required component.
770 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.
771 @retval EFI_NOT_FOUND No hostName component in the URL.
772 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
786 HTTP_URL_PARSER
*Parser
;
788 if (Url
== NULL
|| UrlParser
== NULL
|| Path
== NULL
) {
789 return EFI_INVALID_PARAMETER
;
792 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
794 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_PATH
)) == 0) {
795 return EFI_NOT_FOUND
;
798 PathStr
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_PATH
].Length
+ 1);
799 if (PathStr
== NULL
) {
800 return EFI_OUT_OF_RESOURCES
;
803 Status
= UriPercentDecode (
804 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_PATH
].Offset
,
805 Parser
->FieldData
[HTTP_URI_FIELD_PATH
].Length
,
809 if (EFI_ERROR (Status
)) {
814 PathStr
[ResultLength
] = '\0';
820 Release the resource of the URL parser.
822 @param[in] UrlParser Pointer to the parser.
831 FreePool (UrlParser
);
835 Find a specified header field according to the field name.
837 @param[in] HeaderCount Number of HTTP header structures in Headers list.
838 @param[in] Headers Array containing list of HTTP headers.
839 @param[in] FieldName Null terminated string which describes a field name.
841 @return Pointer to the found header or NULL.
847 IN UINTN HeaderCount
,
848 IN EFI_HTTP_HEADER
*Headers
,
854 if (HeaderCount
== 0 || Headers
== NULL
|| FieldName
== NULL
) {
858 for (Index
= 0; Index
< HeaderCount
; Index
++){
860 // Field names are case-insensitive (RFC 2616).
862 if (AsciiStriCmp (Headers
[Index
].FieldName
, FieldName
) == 0) {
863 return &Headers
[Index
];
871 BodyParserBodyIdentity
,
872 BodyParserChunkSizeStart
,
874 BodyParserChunkSizeEndCR
,
875 BodyParserChunkExtStart
,
876 BodyParserChunkDataStart
,
877 BodyParserChunkDataEnd
,
878 BodyParserChunkDataEndCR
,
881 BodyParserLastCRLFEnd
,
884 } HTTP_BODY_PARSE_STATE
;
887 BOOLEAN IgnoreBody
; // "MUST NOT" include a message-body
888 BOOLEAN IsChunked
; // "chunked" transfer-coding.
889 BOOLEAN ContentLengthIsValid
;
890 UINTN ContentLength
; // Entity length (not the message-body length), invalid until ContentLengthIsValid is TRUE
892 HTTP_BODY_PARSER_CALLBACK Callback
;
894 UINTN ParsedBodyLength
;
895 HTTP_BODY_PARSE_STATE State
;
896 UINTN CurrentChunkSize
;
897 UINTN CurrentChunkParsedSize
;
902 Convert an Ascii char to its uppercase.
904 @param[in] Char Ascii character.
906 @return Uppercase value of the input Char.
914 if (Char
>= 'a' && Char
<= 'z') {
915 return Char
- ('a' - 'A');
922 Convert an hexadecimal char to a value of type UINTN.
924 @param[in] Char Ascii character.
926 @return Value translated from Char.
930 HttpIoHexCharToUintn (
934 if (Char
>= '0' && Char
<= '9') {
938 return (10 + HttpIoCharToUpper (Char
) - 'A');
942 Get the value of the content length if there is a "Content-Length" header.
944 @param[in] HeaderCount Number of HTTP header structures in Headers.
945 @param[in] Headers Array containing list of HTTP headers.
946 @param[out] ContentLength Pointer to save the value of the content length.
948 @retval EFI_SUCCESS Successfully get the content length.
949 @retval EFI_NOT_FOUND No "Content-Length" header in the Headers.
953 HttpIoParseContentLengthHeader (
954 IN UINTN HeaderCount
,
955 IN EFI_HTTP_HEADER
*Headers
,
956 OUT UINTN
*ContentLength
959 EFI_HTTP_HEADER
*Header
;
961 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_CONTENT_LENGTH
);
962 if (Header
== NULL
) {
963 return EFI_NOT_FOUND
;
966 return AsciiStrDecimalToUintnS (Header
->FieldValue
, (CHAR8
**) NULL
, ContentLength
);
971 Check whether the HTTP message is using the "chunked" transfer-coding.
973 @param[in] HeaderCount Number of HTTP header structures in Headers.
974 @param[in] Headers Array containing list of HTTP headers.
976 @return The message is "chunked" transfer-coding (TRUE) or not (FALSE).
981 IN UINTN HeaderCount
,
982 IN EFI_HTTP_HEADER
*Headers
985 EFI_HTTP_HEADER
*Header
;
988 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_TRANSFER_ENCODING
);
989 if (Header
== NULL
) {
993 if (AsciiStriCmp (Header
->FieldValue
, "identity") != 0) {
1001 Check whether the HTTP message should have a message-body.
1003 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.
1004 @param[in] StatusCode Response status code returned by the remote host.
1006 @return The message should have a message-body (FALSE) or not (TRUE).
1010 HttpIoNoMessageBody (
1011 IN EFI_HTTP_METHOD Method
,
1012 IN EFI_HTTP_STATUS_CODE StatusCode
1017 // All responses to the HEAD request method
1018 // MUST NOT include a message-body, even though the presence of entity-
1019 // header fields might lead one to believe they do. All 1xx
1020 // (informational), 204 (no content), and 304 (not modified) responses
1021 // MUST NOT include a message-body. All other responses do include a
1022 // message-body, although it MAY be of zero length.
1024 if (Method
== HttpMethodHead
) {
1028 if ((StatusCode
== HTTP_STATUS_100_CONTINUE
) ||
1029 (StatusCode
== HTTP_STATUS_101_SWITCHING_PROTOCOLS
) ||
1030 (StatusCode
== HTTP_STATUS_204_NO_CONTENT
) ||
1031 (StatusCode
== HTTP_STATUS_304_NOT_MODIFIED
))
1040 Initialize a HTTP message-body parser.
1042 This function will create and initialize a HTTP message parser according to caller provided HTTP message
1043 header information. It is the caller's responsibility to free the buffer returned in *UrlParser by HttpFreeMsgParser().
1045 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.
1046 @param[in] StatusCode Response status code returned by the remote host.
1047 @param[in] HeaderCount Number of HTTP header structures in Headers.
1048 @param[in] Headers Array containing list of HTTP headers.
1049 @param[in] Callback Callback function that is invoked when parsing the HTTP message-body,
1050 set to NULL to ignore all events.
1051 @param[in] Context Pointer to the context that will be passed to Callback.
1052 @param[out] MsgParser Pointer to the returned buffer to store the message parser.
1054 @retval EFI_SUCCESS Successfully initialized the parser.
1055 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
1056 @retval EFI_INVALID_PARAMETER MsgParser is NULL or HeaderCount is not NULL but Headers is NULL.
1057 @retval Others Failed to initialize the parser.
1063 IN EFI_HTTP_METHOD Method
,
1064 IN EFI_HTTP_STATUS_CODE StatusCode
,
1065 IN UINTN HeaderCount
,
1066 IN EFI_HTTP_HEADER
*Headers
,
1067 IN HTTP_BODY_PARSER_CALLBACK Callback
,
1069 OUT VOID
**MsgParser
1073 HTTP_BODY_PARSER
*Parser
;
1075 if (HeaderCount
!= 0 && Headers
== NULL
) {
1076 return EFI_INVALID_PARAMETER
;
1079 if (MsgParser
== NULL
) {
1080 return EFI_INVALID_PARAMETER
;
1083 Parser
= AllocateZeroPool (sizeof (HTTP_BODY_PARSER
));
1084 if (Parser
== NULL
) {
1085 return EFI_OUT_OF_RESOURCES
;
1088 Parser
->State
= BodyParserBodyStart
;
1091 // Determine the message length according to RFC 2616.
1092 // 1. Check whether the message "MUST NOT" have a message-body.
1094 Parser
->IgnoreBody
= HttpIoNoMessageBody (Method
, StatusCode
);
1096 // 2. Check whether the message using "chunked" transfer-coding.
1098 Parser
->IsChunked
= HttpIoIsChunked (HeaderCount
, Headers
);
1100 // 3. Check whether the message has a Content-Length header field.
1102 Status
= HttpIoParseContentLengthHeader (HeaderCount
, Headers
, &Parser
->ContentLength
);
1103 if (!EFI_ERROR (Status
)) {
1104 Parser
->ContentLengthIsValid
= TRUE
;
1107 // 4. Range header is not supported now, so we won't meet media type "multipart/byteranges".
1108 // 5. By server closing the connection
1112 // Set state to skip body parser if the message shouldn't have a message body.
1114 if (Parser
->IgnoreBody
) {
1115 Parser
->State
= BodyParserComplete
;
1117 Parser
->Callback
= Callback
;
1118 Parser
->Context
= Context
;
1121 *MsgParser
= Parser
;
1128 Parse BodyLength of message-body. This function can be called repeatedly to parse the message-body partially.
1130 @param[in, out] MsgParser Pointer to the message parser.
1131 @param[in] BodyLength Length in bytes of the Body.
1132 @param[in] Body Pointer to the buffer of the message-body to be parsed.
1134 @retval EFI_SUCCESS Successfully parse the message-body.
1135 @retval EFI_INVALID_PARAMETER MsgParser is NULL or Body is NULL or BodyLength is 0.
1136 @retval Others Operation aborted.
1141 HttpParseMessageBody (
1142 IN OUT VOID
*MsgParser
,
1143 IN UINTN BodyLength
,
1148 UINTN RemainderLengthInThis
;
1149 UINTN LengthForCallback
;
1151 HTTP_BODY_PARSER
*Parser
;
1153 if (BodyLength
== 0 || Body
== NULL
) {
1154 return EFI_INVALID_PARAMETER
;
1157 if (MsgParser
== NULL
) {
1158 return EFI_INVALID_PARAMETER
;
1161 Parser
= (HTTP_BODY_PARSER
*) MsgParser
;
1163 if (Parser
->IgnoreBody
) {
1164 Parser
->State
= BodyParserComplete
;
1165 if (Parser
->Callback
!= NULL
) {
1166 Status
= Parser
->Callback (
1167 BodyParseEventOnComplete
,
1172 if (EFI_ERROR (Status
)) {
1179 if (Parser
->State
== BodyParserBodyStart
) {
1180 Parser
->ParsedBodyLength
= 0;
1181 if (Parser
->IsChunked
) {
1182 Parser
->State
= BodyParserChunkSizeStart
;
1184 Parser
->State
= BodyParserBodyIdentity
;
1189 // The message body might be truncated in anywhere, so we need to parse is byte-by-byte.
1191 for (Char
= Body
; Char
< Body
+ BodyLength
; ) {
1193 switch (Parser
->State
) {
1194 case BodyParserStateMax
:
1197 case BodyParserBodyIdentity
:
1199 // Identity transfer-coding, just notify user to save the body data.
1201 if (Parser
->Callback
!= NULL
) {
1202 Status
= Parser
->Callback (
1203 BodyParseEventOnData
,
1205 MIN (BodyLength
, Parser
->ContentLength
- Parser
->ParsedBodyLength
),
1208 if (EFI_ERROR (Status
)) {
1212 Char
+= MIN (BodyLength
, Parser
->ContentLength
- Parser
->ParsedBodyLength
);
1213 Parser
->ParsedBodyLength
+= MIN (BodyLength
, Parser
->ContentLength
- Parser
->ParsedBodyLength
);
1214 if (Parser
->ParsedBodyLength
== Parser
->ContentLength
) {
1215 Parser
->State
= BodyParserComplete
;
1216 if (Parser
->Callback
!= NULL
) {
1217 Status
= Parser
->Callback (
1218 BodyParseEventOnComplete
,
1223 if (EFI_ERROR (Status
)) {
1230 case BodyParserChunkSizeStart
:
1232 // First byte of chunk-size, the chunk-size might be truncated.
1234 Parser
->CurrentChunkSize
= 0;
1235 Parser
->State
= BodyParserChunkSize
;
1236 case BodyParserChunkSize
:
1237 if (!NET_IS_HEX_CHAR (*Char
)) {
1239 Parser
->State
= BodyParserChunkExtStart
;
1241 } else if (*Char
== '\r') {
1242 Parser
->State
= BodyParserChunkSizeEndCR
;
1245 Parser
->State
= BodyParserStateMax
;
1250 if (Parser
->CurrentChunkSize
> (((~((UINTN
) 0)) - 16) / 16)) {
1251 return EFI_INVALID_PARAMETER
;
1253 Parser
->CurrentChunkSize
= Parser
->CurrentChunkSize
* 16 + HttpIoHexCharToUintn (*Char
);
1257 case BodyParserChunkExtStart
:
1259 // Ignore all the chunk extensions.
1261 if (*Char
== '\r') {
1262 Parser
->State
= BodyParserChunkSizeEndCR
;
1267 case BodyParserChunkSizeEndCR
:
1268 if (*Char
!= '\n') {
1269 Parser
->State
= BodyParserStateMax
;
1273 if (Parser
->CurrentChunkSize
== 0) {
1275 // The last chunk has been parsed and now assumed the state
1276 // of HttpBodyParse is ParserLastCRLF. So it need to decide
1277 // whether the rest message is trailer or last CRLF in the next round.
1279 Parser
->ContentLengthIsValid
= TRUE
;
1280 Parser
->State
= BodyParserLastCRLF
;
1283 Parser
->State
= BodyParserChunkDataStart
;
1284 Parser
->CurrentChunkParsedSize
= 0;
1287 case BodyParserLastCRLF
:
1289 // Judge the byte is belong to the Last CRLF or trailer, and then
1290 // configure the state of HttpBodyParse to corresponding state.
1292 if (*Char
== '\r') {
1294 Parser
->State
= BodyParserLastCRLFEnd
;
1297 Parser
->State
= BodyParserTrailer
;
1301 case BodyParserLastCRLFEnd
:
1302 if (*Char
== '\n') {
1303 Parser
->State
= BodyParserComplete
;
1305 if (Parser
->Callback
!= NULL
) {
1306 Status
= Parser
->Callback (
1307 BodyParseEventOnComplete
,
1312 if (EFI_ERROR (Status
)) {
1318 Parser
->State
= BodyParserStateMax
;
1322 case BodyParserTrailer
:
1323 if (*Char
== '\r') {
1324 Parser
->State
= BodyParserChunkSizeEndCR
;
1329 case BodyParserChunkDataStart
:
1331 // First byte of chunk-data, the chunk data also might be truncated.
1333 RemainderLengthInThis
= BodyLength
- (Char
- Body
);
1334 LengthForCallback
= MIN (Parser
->CurrentChunkSize
- Parser
->CurrentChunkParsedSize
, RemainderLengthInThis
);
1335 if (Parser
->Callback
!= NULL
) {
1336 Status
= Parser
->Callback (
1337 BodyParseEventOnData
,
1342 if (EFI_ERROR (Status
)) {
1346 Char
+= LengthForCallback
;
1347 Parser
->ContentLength
+= LengthForCallback
;
1348 Parser
->CurrentChunkParsedSize
+= LengthForCallback
;
1349 if (Parser
->CurrentChunkParsedSize
== Parser
->CurrentChunkSize
) {
1350 Parser
->State
= BodyParserChunkDataEnd
;
1354 case BodyParserChunkDataEnd
:
1355 if (*Char
== '\r') {
1356 Parser
->State
= BodyParserChunkDataEndCR
;
1358 Parser
->State
= BodyParserStateMax
;
1363 case BodyParserChunkDataEndCR
:
1364 if (*Char
!= '\n') {
1365 Parser
->State
= BodyParserStateMax
;
1369 Parser
->State
= BodyParserChunkSizeStart
;
1378 if (Parser
->State
== BodyParserStateMax
) {
1386 Check whether the message-body is complete or not.
1388 @param[in] MsgParser Pointer to the message parser.
1390 @retval TRUE Message-body is complete.
1391 @retval FALSE Message-body is not complete.
1396 HttpIsMessageComplete (
1400 HTTP_BODY_PARSER
*Parser
;
1402 if (MsgParser
== NULL
) {
1406 Parser
= (HTTP_BODY_PARSER
*) MsgParser
;
1408 if (Parser
->State
== BodyParserComplete
) {
1415 Get the content length of the entity.
1417 Note that in trunk transfer, the entity length is not valid until the whole message body is received.
1419 @param[in] MsgParser Pointer to the message parser.
1420 @param[out] ContentLength Pointer to store the length of the entity.
1422 @retval EFI_SUCCESS Successfully to get the entity length.
1423 @retval EFI_NOT_READY Entity length is not valid yet.
1424 @retval EFI_INVALID_PARAMETER MsgParser is NULL or ContentLength is NULL.
1429 HttpGetEntityLength (
1431 OUT UINTN
*ContentLength
1434 HTTP_BODY_PARSER
*Parser
;
1436 if (MsgParser
== NULL
|| ContentLength
== NULL
) {
1437 return EFI_INVALID_PARAMETER
;
1440 Parser
= (HTTP_BODY_PARSER
*) MsgParser
;
1442 if (!Parser
->ContentLengthIsValid
) {
1443 return EFI_NOT_READY
;
1446 *ContentLength
= Parser
->ContentLength
;
1451 Release the resource of the message parser.
1453 @param[in] MsgParser Pointer to the message parser.
1462 FreePool (MsgParser
);
1467 Get the next string, which is distinguished by specified separator.
1469 @param[in] String Pointer to the string.
1470 @param[in] Separator Specified separator used to distinguish where is the beginning
1473 @return Pointer to the next string.
1474 @return NULL if not find or String is NULL.
1479 AsciiStrGetNextToken (
1480 IN CONST CHAR8
*String
,
1491 if (*Token
== Separator
) {
1492 return (CHAR8
*)(Token
+ 1);
1499 Set FieldName and FieldValue into specified HttpHeader.
1501 @param[in,out] HttpHeader Specified HttpHeader.
1502 @param[in] FieldName FieldName of this HttpHeader, a NULL terminated ASCII string.
1503 @param[in] FieldValue FieldValue of this HttpHeader, a NULL terminated ASCII string.
1506 @retval EFI_SUCCESS The FieldName and FieldValue are set into HttpHeader successfully.
1507 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1508 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
1513 HttpSetFieldNameAndValue (
1514 IN OUT EFI_HTTP_HEADER
*HttpHeader
,
1515 IN CONST CHAR8
*FieldName
,
1516 IN CONST CHAR8
*FieldValue
1519 UINTN FieldNameSize
;
1520 UINTN FieldValueSize
;
1522 if (HttpHeader
== NULL
|| FieldName
== NULL
|| FieldValue
== NULL
) {
1523 return EFI_INVALID_PARAMETER
;
1526 if (HttpHeader
->FieldName
!= NULL
) {
1527 FreePool (HttpHeader
->FieldName
);
1529 if (HttpHeader
->FieldValue
!= NULL
) {
1530 FreePool (HttpHeader
->FieldValue
);
1533 FieldNameSize
= AsciiStrSize (FieldName
);
1534 HttpHeader
->FieldName
= AllocateZeroPool (FieldNameSize
);
1535 if (HttpHeader
->FieldName
== NULL
) {
1536 return EFI_OUT_OF_RESOURCES
;
1538 CopyMem (HttpHeader
->FieldName
, FieldName
, FieldNameSize
);
1539 HttpHeader
->FieldName
[FieldNameSize
- 1] = 0;
1541 FieldValueSize
= AsciiStrSize (FieldValue
);
1542 HttpHeader
->FieldValue
= AllocateZeroPool (FieldValueSize
);
1543 if (HttpHeader
->FieldValue
== NULL
) {
1544 FreePool (HttpHeader
->FieldName
);
1545 return EFI_OUT_OF_RESOURCES
;
1547 CopyMem (HttpHeader
->FieldValue
, FieldValue
, FieldValueSize
);
1548 HttpHeader
->FieldValue
[FieldValueSize
- 1] = 0;
1554 Get one key/value header pair from the raw string.
1556 @param[in] String Pointer to the raw string.
1557 @param[out] FieldName Points directly to field name within 'HttpHeader'.
1558 @param[out] FieldValue Points directly to field value within 'HttpHeader'.
1560 @return Pointer to the next raw string.
1561 @return NULL if no key/value header pair from this raw string.
1566 HttpGetFieldNameAndValue (
1568 OUT CHAR8
**FieldName
,
1569 OUT CHAR8
**FieldValue
1572 CHAR8
*FieldNameStr
;
1573 CHAR8
*FieldValueStr
;
1577 if (String
== NULL
|| FieldName
== NULL
|| FieldValue
== NULL
) {
1583 FieldNameStr
= NULL
;
1584 FieldValueStr
= NULL
;
1590 // Check whether the raw HTTP header string is valid or not.
1592 EndofHeader
= AsciiStrStr (String
, "\r\n\r\n");
1593 if (EndofHeader
== NULL
) {
1598 // Each header field consists of a name followed by a colon (":") and the field value.
1600 FieldNameStr
= String
;
1601 FieldValueStr
= AsciiStrGetNextToken (FieldNameStr
, ':');
1602 if (FieldValueStr
== NULL
) {
1607 // Replace ':' with 0
1609 *(FieldValueStr
- 1) = 0;
1612 // The field value MAY be preceded by any amount of LWS, though a single SP is preferred.
1613 // Note: LWS = [CRLF] 1*(SP|HT), it can be '\r\n ' or '\r\n\t' or ' ' or '\t'.
1619 if (*FieldValueStr
== ' ' || *FieldValueStr
== '\t') {
1621 // Boundary condition check.
1623 if ((UINTN
)EndofHeader
- (UINTN
)(FieldValueStr
) < 1) {
1628 } else if (*FieldValueStr
== '\r') {
1630 // Boundary condition check.
1632 if ((UINTN
)EndofHeader
- (UINTN
)(FieldValueStr
) < 3) {
1636 if (*(FieldValueStr
+ 1) == '\n' && (*(FieldValueStr
+ 2) == ' ' || *(FieldValueStr
+ 2) == '\t')) {
1637 FieldValueStr
= FieldValueStr
+ 3;
1645 // Header fields can be extended over multiple lines by preceding each extra
1646 // line with at least one SP or HT.
1648 StrPtr
= FieldValueStr
;
1650 StrPtr
= AsciiStrGetNextToken (StrPtr
, '\r');
1651 if (StrPtr
== NULL
|| *StrPtr
!= '\n') {
1656 } while (*StrPtr
== ' ' || *StrPtr
== '\t');
1659 // Replace '\r' with 0
1664 // Get FieldName and FieldValue.
1666 *FieldName
= FieldNameStr
;
1667 *FieldValue
= FieldValueStr
;
1673 Free existing HeaderFields.
1675 @param[in] HeaderFields Pointer to array of key/value header pairs waitting for free.
1676 @param[in] FieldCount The number of header pairs in HeaderFields.
1681 HttpFreeHeaderFields (
1682 IN EFI_HTTP_HEADER
*HeaderFields
,
1688 if (HeaderFields
!= NULL
) {
1689 for (Index
= 0; Index
< FieldCount
; Index
++) {
1690 if (HeaderFields
[Index
].FieldName
!= NULL
) {
1691 FreePool (HeaderFields
[Index
].FieldName
);
1693 if (HeaderFields
[Index
].FieldValue
!= NULL
) {
1694 FreePool (HeaderFields
[Index
].FieldValue
);
1698 FreePool (HeaderFields
);
1703 Generate HTTP request message.
1705 This function will allocate memory for the whole HTTP message and generate a
1706 well formatted HTTP Request message in it, include the Request-Line, header
1707 fields and also the message body. It is the caller's responsibility to free
1708 the buffer returned in *RequestMsg.
1710 @param[in] Message Pointer to the EFI_HTTP_MESSAGE structure which
1711 contains the required information to generate
1712 the HTTP request message.
1713 @param[in] Url The URL of a remote host.
1714 @param[out] RequestMsg Pointer to the created HTTP request message.
1715 NULL if any error occured.
1716 @param[out] RequestMsgSize Size of the RequestMsg (in bytes).
1718 @return EFI_SUCCESS If HTTP request string was created successfully
1719 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
1720 @retval EFI_INVALID_PARAMETER The input arguments are invalid
1725 HttpGenRequestMessage (
1726 IN CONST EFI_HTTP_MESSAGE
*Message
,
1727 IN CONST CHAR8
*Url
,
1728 OUT CHAR8
**RequestMsg
,
1729 OUT UINTN
*RequestMsgSize
1739 EFI_HTTP_HEADER
**AppendList
;
1741 EFI_HTTP_UTILITIES_PROTOCOL
*HttpUtilitiesProtocol
;
1743 Status
= EFI_SUCCESS
;
1749 HttpUtilitiesProtocol
= NULL
;
1752 // 1. If we have a Request, we cannot have a NULL Url
1753 // 2. If we have a Request, HeaderCount can not be non-zero
1754 // 3. If we do not have a Request, HeaderCount should be zero
1755 // 4. If we do not have Request and Headers, we need at least a message-body
1757 if ((Message
== NULL
|| RequestMsg
== NULL
|| RequestMsgSize
== NULL
) ||
1758 (Message
->Data
.Request
!= NULL
&& Url
== NULL
) ||
1759 (Message
->Data
.Request
!= NULL
&& Message
->HeaderCount
== 0) ||
1760 (Message
->Data
.Request
== NULL
&& Message
->HeaderCount
!= 0) ||
1761 (Message
->Data
.Request
== NULL
&& Message
->HeaderCount
== 0 && Message
->BodyLength
== 0)) {
1762 return EFI_INVALID_PARAMETER
;
1765 if (Message
->HeaderCount
!= 0) {
1767 // Locate the HTTP_UTILITIES protocol.
1769 Status
= gBS
->LocateProtocol (
1770 &gEfiHttpUtilitiesProtocolGuid
,
1772 (VOID
**)&HttpUtilitiesProtocol
1775 if (EFI_ERROR (Status
)) {
1776 DEBUG ((DEBUG_ERROR
,"Failed to locate Http Utilities protocol. Status = %r.\n", Status
));
1781 // Build AppendList to send into HttpUtilitiesBuild
1783 AppendList
= AllocateZeroPool (sizeof (EFI_HTTP_HEADER
*) * (Message
->HeaderCount
));
1784 if (AppendList
== NULL
) {
1785 return EFI_OUT_OF_RESOURCES
;
1788 for(Index
= 0; Index
< Message
->HeaderCount
; Index
++){
1789 AppendList
[Index
] = &Message
->Headers
[Index
];
1793 // Build raw HTTP Headers
1795 Status
= HttpUtilitiesProtocol
->Build (
1796 HttpUtilitiesProtocol
,
1801 Message
->HeaderCount
,
1807 if (AppendList
!= NULL
) {
1808 FreePool (AppendList
);
1811 if (EFI_ERROR (Status
) || HttpHdr
== NULL
){
1817 // If we have headers to be sent, account for it.
1819 if (Message
->HeaderCount
!= 0) {
1820 MsgSize
= HttpHdrSize
;
1824 // If we have a request line, account for the fields.
1826 if (Message
->Data
.Request
!= NULL
) {
1827 MsgSize
+= HTTP_METHOD_MAXIMUM_LEN
+ AsciiStrLen (HTTP_VERSION_CRLF_STR
) + AsciiStrLen (Url
);
1832 // If we have a message body to be sent, account for it.
1834 MsgSize
+= Message
->BodyLength
;
1837 // memory for the string that needs to be sent to TCP
1840 *RequestMsg
= AllocateZeroPool (MsgSize
);
1841 if (*RequestMsg
== NULL
) {
1842 Status
= EFI_OUT_OF_RESOURCES
;
1846 RequestPtr
= *RequestMsg
;
1848 // Construct header request
1850 if (Message
->Data
.Request
!= NULL
) {
1851 switch (Message
->Data
.Request
->Method
) {
1853 StrLength
= sizeof (HTTP_METHOD_GET
) - 1;
1854 CopyMem (RequestPtr
, HTTP_METHOD_GET
, StrLength
);
1855 RequestPtr
+= StrLength
;
1858 StrLength
= sizeof (HTTP_METHOD_PUT
) - 1;
1859 CopyMem (RequestPtr
, HTTP_METHOD_PUT
, StrLength
);
1860 RequestPtr
+= StrLength
;
1862 case HttpMethodPatch
:
1863 StrLength
= sizeof (HTTP_METHOD_PATCH
) - 1;
1864 CopyMem (RequestPtr
, HTTP_METHOD_PATCH
, StrLength
);
1865 RequestPtr
+= StrLength
;
1867 case HttpMethodPost
:
1868 StrLength
= sizeof (HTTP_METHOD_POST
) - 1;
1869 CopyMem (RequestPtr
, HTTP_METHOD_POST
, StrLength
);
1870 RequestPtr
+= StrLength
;
1872 case HttpMethodHead
:
1873 StrLength
= sizeof (HTTP_METHOD_HEAD
) - 1;
1874 CopyMem (RequestPtr
, HTTP_METHOD_HEAD
, StrLength
);
1875 RequestPtr
+= StrLength
;
1877 case HttpMethodDelete
:
1878 StrLength
= sizeof (HTTP_METHOD_DELETE
) - 1;
1879 CopyMem (RequestPtr
, HTTP_METHOD_DELETE
, StrLength
);
1880 RequestPtr
+= StrLength
;
1884 Status
= EFI_INVALID_PARAMETER
;
1888 StrLength
= AsciiStrLen(EMPTY_SPACE
);
1889 CopyMem (RequestPtr
, EMPTY_SPACE
, StrLength
);
1890 RequestPtr
+= StrLength
;
1892 StrLength
= AsciiStrLen (Url
);
1893 CopyMem (RequestPtr
, Url
, StrLength
);
1894 RequestPtr
+= StrLength
;
1896 StrLength
= sizeof (HTTP_VERSION_CRLF_STR
) - 1;
1897 CopyMem (RequestPtr
, HTTP_VERSION_CRLF_STR
, StrLength
);
1898 RequestPtr
+= StrLength
;
1900 if (HttpHdr
!= NULL
) {
1904 CopyMem (RequestPtr
, HttpHdr
, HttpHdrSize
);
1905 RequestPtr
+= HttpHdrSize
;
1912 if (Message
->Body
!= NULL
) {
1913 CopyMem (RequestPtr
, Message
->Body
, Message
->BodyLength
);
1914 RequestPtr
+= Message
->BodyLength
;
1920 (*RequestMsgSize
) = (UINTN
)(RequestPtr
) - (UINTN
)(*RequestMsg
);
1926 if (*RequestMsg
!= NULL
) {
1927 FreePool (*RequestMsg
);
1933 if (HttpHdr
!= NULL
) {
1941 Translate the status code in HTTP message to EFI_HTTP_STATUS_CODE defined
1942 in UEFI 2.5 specification.
1944 @param[in] StatusCode The status code value in HTTP message.
1946 @return Value defined in EFI_HTTP_STATUS_CODE .
1949 EFI_HTTP_STATUS_CODE
1951 HttpMappingToStatusCode (
1955 switch (StatusCode
) {
1957 return HTTP_STATUS_100_CONTINUE
;
1959 return HTTP_STATUS_101_SWITCHING_PROTOCOLS
;
1961 return HTTP_STATUS_200_OK
;
1963 return HTTP_STATUS_201_CREATED
;
1965 return HTTP_STATUS_202_ACCEPTED
;
1967 return HTTP_STATUS_203_NON_AUTHORITATIVE_INFORMATION
;
1969 return HTTP_STATUS_204_NO_CONTENT
;
1971 return HTTP_STATUS_205_RESET_CONTENT
;
1973 return HTTP_STATUS_206_PARTIAL_CONTENT
;
1975 return HTTP_STATUS_300_MULTIPLE_CHOICES
;
1977 return HTTP_STATUS_301_MOVED_PERMANENTLY
;
1979 return HTTP_STATUS_302_FOUND
;
1981 return HTTP_STATUS_303_SEE_OTHER
;
1983 return HTTP_STATUS_304_NOT_MODIFIED
;
1985 return HTTP_STATUS_305_USE_PROXY
;
1987 return HTTP_STATUS_307_TEMPORARY_REDIRECT
;
1989 return HTTP_STATUS_308_PERMANENT_REDIRECT
;
1991 return HTTP_STATUS_400_BAD_REQUEST
;
1993 return HTTP_STATUS_401_UNAUTHORIZED
;
1995 return HTTP_STATUS_402_PAYMENT_REQUIRED
;
1997 return HTTP_STATUS_403_FORBIDDEN
;
1999 return HTTP_STATUS_404_NOT_FOUND
;
2001 return HTTP_STATUS_405_METHOD_NOT_ALLOWED
;
2003 return HTTP_STATUS_406_NOT_ACCEPTABLE
;
2005 return HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED
;
2007 return HTTP_STATUS_408_REQUEST_TIME_OUT
;
2009 return HTTP_STATUS_409_CONFLICT
;
2011 return HTTP_STATUS_410_GONE
;
2013 return HTTP_STATUS_411_LENGTH_REQUIRED
;
2015 return HTTP_STATUS_412_PRECONDITION_FAILED
;
2017 return HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE
;
2019 return HTTP_STATUS_414_REQUEST_URI_TOO_LARGE
;
2021 return HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE
;
2023 return HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED
;
2025 return HTTP_STATUS_417_EXPECTATION_FAILED
;
2027 return HTTP_STATUS_500_INTERNAL_SERVER_ERROR
;
2029 return HTTP_STATUS_501_NOT_IMPLEMENTED
;
2031 return HTTP_STATUS_502_BAD_GATEWAY
;
2033 return HTTP_STATUS_503_SERVICE_UNAVAILABLE
;
2035 return HTTP_STATUS_504_GATEWAY_TIME_OUT
;
2037 return HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED
;
2040 return HTTP_STATUS_UNSUPPORTED_STATUS
;
2045 Check whether header field called FieldName is in DeleteList.
2047 @param[in] DeleteList Pointer to array of key/value header pairs.
2048 @param[in] DeleteCount The number of header pairs.
2049 @param[in] FieldName Pointer to header field's name.
2051 @return TRUE if FieldName is not in DeleteList, that means this header field is valid.
2052 @return FALSE if FieldName is in DeleteList, that means this header field is invalid.
2057 HttpIsValidHttpHeader (
2058 IN CHAR8
*DeleteList
[],
2059 IN UINTN DeleteCount
,
2065 if (FieldName
== NULL
) {
2069 for (Index
= 0; Index
< DeleteCount
; Index
++) {
2070 if (DeleteList
[Index
] == NULL
) {
2074 if (AsciiStrCmp (FieldName
, DeleteList
[Index
]) == 0) {