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
||
60 !NET_IS_HEX_CHAR (Buffer
[Index
+1]) || !NET_IS_HEX_CHAR (Buffer
[Index
+2])) {
61 return EFI_INVALID_PARAMETER
;
63 HexStr
[0] = Buffer
[Index
+1];
64 HexStr
[1] = Buffer
[Index
+2];
65 ResultBuffer
[Offset
] = (CHAR8
) AsciiStrHexToUintn (HexStr
);
68 ResultBuffer
[Offset
] = Buffer
[Index
];
74 *ResultLength
= (UINT32
) Offset
;
80 This function return the updated state according to the input state and next character of
83 @param[in] Char Next character.
84 @param[in] State Current value of the parser state machine.
85 @param[in] IsRightBracket TRUE if there is an sign ']' in the authority component and
86 indicates the next part is ':' before Port.
88 @return Updated state value.
91 NetHttpParseAuthorityChar (
93 IN HTTP_URL_PARSE_STATE State
,
94 IN BOOLEAN
*IsRightBracket
100 // The authority component is preceded by a double slash ("//") and is
101 // terminated by the next slash ("/"), question mark ("?"), or number
102 // sign ("#") character, or by the end of the URI.
104 if (Char
== ' ' || Char
== '\r' || Char
== '\n') {
105 return UrlParserStateMax
;
109 // authority = [ userinfo "@" ] host [ ":" port ]
112 case UrlParserUserInfo
:
114 return UrlParserHostStart
;
119 case UrlParserHostStart
:
121 return UrlParserHostIpv6
;
125 return UrlParserPortStart
;
128 return UrlParserHost
;
130 case UrlParserHostIpv6
:
132 *IsRightBracket
= TRUE
;
135 if (Char
== ':' && *IsRightBracket
) {
136 return UrlParserPortStart
;
138 return UrlParserHostIpv6
;
141 case UrlParserPortStart
:
142 return UrlParserPort
;
152 This function parse the authority component of the input URL and update the parser.
154 @param[in] Url The pointer to a HTTP URL string.
155 @param[in] FoundAt TRUE if there is an at sign ('@') in the authority component.
156 @param[in, out] UrlParser Pointer to the buffer of the parse result.
158 @retval EFI_SUCCESS Successfully parse the authority.
159 @retval EFI_INVALID_PARAMETER The Url is invalid to parse the authority component.
163 NetHttpParseAuthority (
166 IN OUT HTTP_URL_PARSER
*UrlParser
172 HTTP_URL_PARSE_STATE State
;
175 BOOLEAN IsrightBracket
;
177 ASSERT ((UrlParser
->FieldBitMap
& BIT (HTTP_URI_FIELD_AUTHORITY
)) != 0);
180 // authority = [ userinfo "@" ] host [ ":" port ]
183 State
= UrlParserUserInfo
;
185 State
= UrlParserHost
;
188 IsrightBracket
= FALSE
;
189 Field
= HTTP_URI_FIELD_MAX
;
191 Authority
= Url
+ UrlParser
->FieldData
[HTTP_URI_FIELD_AUTHORITY
].Offset
;
192 Length
= UrlParser
->FieldData
[HTTP_URI_FIELD_AUTHORITY
].Length
;
193 for (Char
= Authority
; Char
< Authority
+ Length
; Char
++) {
194 State
= NetHttpParseAuthorityChar (*Char
, State
, &IsrightBracket
);
196 case UrlParserStateMax
:
197 return EFI_INVALID_PARAMETER
;
199 case UrlParserHostStart
:
200 case UrlParserPortStart
:
203 case UrlParserUserInfo
:
204 Field
= HTTP_URI_FIELD_USERINFO
;
208 Field
= HTTP_URI_FIELD_HOST
;
211 case UrlParserHostIpv6
:
212 Field
= HTTP_URI_FIELD_HOST
;
216 Field
= HTTP_URI_FIELD_PORT
;
224 // Field not changed, count the length.
226 ASSERT (Field
< HTTP_URI_FIELD_MAX
);
227 if (Field
== OldField
) {
228 UrlParser
->FieldData
[Field
].Length
++;
235 UrlParser
->FieldBitMap
|= BIT (Field
);
236 UrlParser
->FieldData
[Field
].Offset
= (UINT32
) (Char
- Url
);
237 UrlParser
->FieldData
[Field
].Length
= 1;
245 This function return the updated state according to the input state and next character of a URL.
247 @param[in] Char Next character.
248 @param[in] State Current value of the parser state machine.
250 @return Updated state value.
254 NetHttpParseUrlChar (
256 IN HTTP_URL_PARSE_STATE State
259 if (Char
== ' ' || Char
== '\r' || Char
== '\n') {
260 return UrlParserStateMax
;
264 // http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]
266 // Request-URI = "*" | absolute-URI | path-absolute | authority
268 // absolute-URI = scheme ":" hier-part [ "?" query ]
269 // path-absolute = "/" [ segment-nz *( "/" segment ) ]
270 // authority = [ userinfo "@" ] host [ ":" port ]
273 case UrlParserUrlStart
:
274 if (Char
== '*' || Char
== '/') {
275 return UrlParserPath
;
277 return UrlParserScheme
;
279 case UrlParserScheme
:
281 return UrlParserSchemeColon
;
285 case UrlParserSchemeColon
:
287 return UrlParserSchemeColonSlash
;
291 case UrlParserSchemeColonSlash
:
293 return UrlParserSchemeColonSlashSlash
;
297 case UrlParserAtInAuthority
:
299 return UrlParserStateMax
;
302 case UrlParserAuthority
:
303 case UrlParserSchemeColonSlashSlash
:
305 return UrlParserAtInAuthority
;
308 return UrlParserPath
;
311 return UrlParserQueryStart
;
314 return UrlParserFragmentStart
;
316 return UrlParserAuthority
;
320 return UrlParserQueryStart
;
323 return UrlParserFragmentStart
;
328 case UrlParserQueryStart
:
330 return UrlParserFragmentStart
;
332 return UrlParserQuery
;
334 case UrlParserFragmentStart
:
335 return UrlParserFragment
;
344 Create a URL parser for the input URL string.
346 This function will parse and dereference the input HTTP URL into it components. The original
347 content of the URL won't be modified and the result will be returned in UrlParser, which can
348 be used in other functions like NetHttpUrlGetHostName().
350 @param[in] Url The pointer to a HTTP URL string.
351 @param[in] Length Length of Url in bytes.
352 @param[in] IsConnectMethod Whether the Url is used in HTTP CONNECT method or not.
353 @param[out] UrlParser Pointer to the returned buffer to store the parse result.
355 @retval EFI_SUCCESS Successfully dereferenced the HTTP URL.
356 @retval EFI_INVALID_PARAMETER UrlParser is NULL or Url is not a valid HTTP URL.
357 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
365 IN BOOLEAN IsConnectMethod
,
369 HTTP_URL_PARSE_STATE State
;
375 HTTP_URL_PARSER
*Parser
;
379 if (Url
== NULL
|| Length
== 0 || UrlParser
== NULL
) {
380 return EFI_INVALID_PARAMETER
;
383 Parser
= AllocateZeroPool (sizeof (HTTP_URL_PARSER
));
384 if (Parser
== NULL
) {
385 return EFI_OUT_OF_RESOURCES
;
388 if (IsConnectMethod
) {
390 // According to RFC 2616, the authority form is only used by the CONNECT method.
392 State
= UrlParserAuthority
;
394 State
= UrlParserUrlStart
;
397 Field
= HTTP_URI_FIELD_MAX
;
400 for (Char
= Url
; Char
< Url
+ Length
; Char
++) {
402 // Update state machine according to next char.
404 State
= NetHttpParseUrlChar (*Char
, State
);
407 case UrlParserStateMax
:
409 return EFI_INVALID_PARAMETER
;
411 case UrlParserSchemeColon
:
412 case UrlParserSchemeColonSlash
:
413 case UrlParserSchemeColonSlashSlash
:
414 case UrlParserQueryStart
:
415 case UrlParserFragmentStart
:
417 // Skip all the delimiting char: "://" "?" "@"
421 case UrlParserScheme
:
422 Field
= HTTP_URI_FIELD_SCHEME
;
425 case UrlParserAtInAuthority
:
427 case UrlParserAuthority
:
428 Field
= HTTP_URI_FIELD_AUTHORITY
;
432 Field
= HTTP_URI_FIELD_PATH
;
436 Field
= HTTP_URI_FIELD_QUERY
;
439 case UrlParserFragment
:
440 Field
= HTTP_URI_FIELD_FRAGMENT
;
448 // Field not changed, count the length.
450 ASSERT (Field
< HTTP_URI_FIELD_MAX
);
451 if (Field
== OldField
) {
452 Parser
->FieldData
[Field
].Length
++;
459 Parser
->FieldBitMap
|= BIT (Field
);
460 Parser
->FieldData
[Field
].Offset
= (UINT32
) (Char
- Url
);
461 Parser
->FieldData
[Field
].Length
= 1;
466 // If has authority component, continue to parse the username, host and port.
468 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_AUTHORITY
)) != 0) {
469 Status
= NetHttpParseAuthority (Url
, FoundAt
, Parser
);
470 if (EFI_ERROR (Status
)) {
481 Get the Hostname from a HTTP URL.
483 This function will return the HostName according to the Url and previous parse result ,and
484 it is the caller's responsibility to free the buffer returned in *HostName.
486 @param[in] Url The pointer to a HTTP URL string.
487 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
488 @param[out] HostName Pointer to a buffer to store the HostName.
490 @retval EFI_SUCCESS Successfully get the required component.
491 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.
492 @retval EFI_NOT_FOUND No hostName component in the URL.
493 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
507 HTTP_URL_PARSER
*Parser
;
509 if (Url
== NULL
|| UrlParser
== NULL
|| HostName
== NULL
) {
510 return EFI_INVALID_PARAMETER
;
513 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
515 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_HOST
)) == 0) {
516 return EFI_NOT_FOUND
;
519 Name
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
+ 1);
521 return EFI_OUT_OF_RESOURCES
;
524 Status
= UriPercentDecode (
525 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Offset
,
526 Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
,
530 if (EFI_ERROR (Status
)) {
535 Name
[ResultLength
] = '\0';
542 Get the IPv4 address from a HTTP URL.
544 This function will return the IPv4 address according to the Url and previous parse result.
546 @param[in] Url The pointer to a HTTP URL string.
547 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
548 @param[out] Ip4Address Pointer to a buffer to store the IP address.
550 @retval EFI_SUCCESS Successfully get the required component.
551 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip4Address is NULL or UrlParser is invalid.
552 @retval EFI_NOT_FOUND No IPv4 address component in the URL.
553 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
561 OUT EFI_IPv4_ADDRESS
*Ip4Address
567 HTTP_URL_PARSER
*Parser
;
569 if (Url
== NULL
|| UrlParser
== NULL
|| Ip4Address
== NULL
) {
570 return EFI_INVALID_PARAMETER
;
573 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
575 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_HOST
)) == 0) {
576 return EFI_NOT_FOUND
;
579 Ip4String
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
+ 1);
580 if (Ip4String
== NULL
) {
581 return EFI_OUT_OF_RESOURCES
;
584 Status
= UriPercentDecode (
585 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Offset
,
586 Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
,
590 if (EFI_ERROR (Status
)) {
591 FreePool (Ip4String
);
595 Ip4String
[ResultLength
] = '\0';
596 Status
= NetLibAsciiStrToIp4 (Ip4String
, Ip4Address
);
597 FreePool (Ip4String
);
603 Get the IPv6 address from a HTTP URL.
605 This function will return the IPv6 address according to the Url and previous parse result.
607 @param[in] Url The pointer to a HTTP URL string.
608 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
609 @param[out] Ip6Address Pointer to a buffer to store the IP address.
611 @retval EFI_SUCCESS Successfully get the required component.
612 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip6Address is NULL or UrlParser is invalid.
613 @retval EFI_NOT_FOUND No IPv6 address component in the URL.
614 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
622 OUT EFI_IPv6_ADDRESS
*Ip6Address
630 HTTP_URL_PARSER
*Parser
;
632 if (Url
== NULL
|| UrlParser
== NULL
|| Ip6Address
== NULL
) {
633 return EFI_INVALID_PARAMETER
;
636 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
638 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_HOST
)) == 0) {
639 return EFI_NOT_FOUND
;
643 // IP-literal = "[" ( IPv6address / IPvFuture ) "]"
645 Length
= Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
;
647 return EFI_INVALID_PARAMETER
;
650 Ptr
= Url
+ Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Offset
;
651 if ((Ptr
[0] != '[') || (Ptr
[Length
- 1] != ']')) {
652 return EFI_INVALID_PARAMETER
;
655 Ip6String
= AllocatePool (Length
);
656 if (Ip6String
== NULL
) {
657 return EFI_OUT_OF_RESOURCES
;
660 Status
= UriPercentDecode (
666 if (EFI_ERROR (Status
)) {
667 FreePool (Ip6String
);
671 Ip6String
[ResultLength
] = '\0';
672 Status
= NetLibAsciiStrToIp6 (Ip6String
, Ip6Address
);
673 FreePool (Ip6String
);
679 Get the port number from a HTTP URL.
681 This function will return the port number according to the Url and previous parse result.
683 @param[in] Url The pointer to a HTTP URL string.
684 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
685 @param[out] Port Pointer to a buffer to store the port number.
687 @retval EFI_SUCCESS Successfully get the required component.
688 @retval EFI_INVALID_PARAMETER Uri is NULL or Port is NULL or UrlParser is invalid.
689 @retval EFI_NOT_FOUND No port number in the URL.
690 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
706 HTTP_URL_PARSER
*Parser
;
708 if (Url
== NULL
|| UrlParser
== NULL
|| Port
== NULL
) {
709 return EFI_INVALID_PARAMETER
;
715 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
717 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_PORT
)) == 0) {
718 return EFI_NOT_FOUND
;
721 PortString
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Length
+ 1);
722 if (PortString
== NULL
) {
723 return EFI_OUT_OF_RESOURCES
;
726 Status
= UriPercentDecode (
727 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Offset
,
728 Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Length
,
732 if (EFI_ERROR (Status
)) {
736 PortString
[ResultLength
] = '\0';
738 while (Index
< ResultLength
) {
739 if (!NET_IS_DIGIT (PortString
[Index
])) {
740 Status
= EFI_INVALID_PARAMETER
;
746 Status
= AsciiStrDecimalToUintnS (Url
+ Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Offset
, (CHAR8
**) NULL
, &Data
);
748 if (Data
> HTTP_URI_PORT_MAX_NUM
) {
749 Status
= EFI_INVALID_PARAMETER
;
753 *Port
= (UINT16
) Data
;
756 FreePool (PortString
);
761 Get the Path from a HTTP URL.
763 This function will return the Path according to the Url and previous parse result,and
764 it is the caller's responsibility to free the buffer returned in *Path.
766 @param[in] Url The pointer to a HTTP URL string.
767 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
768 @param[out] Path Pointer to a buffer to store the Path.
770 @retval EFI_SUCCESS Successfully get the required component.
771 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.
772 @retval EFI_NOT_FOUND No hostName component in the URL.
773 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
787 HTTP_URL_PARSER
*Parser
;
789 if (Url
== NULL
|| UrlParser
== NULL
|| Path
== NULL
) {
790 return EFI_INVALID_PARAMETER
;
793 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
795 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_PATH
)) == 0) {
796 return EFI_NOT_FOUND
;
799 PathStr
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_PATH
].Length
+ 1);
800 if (PathStr
== NULL
) {
801 return EFI_OUT_OF_RESOURCES
;
804 Status
= UriPercentDecode (
805 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_PATH
].Offset
,
806 Parser
->FieldData
[HTTP_URI_FIELD_PATH
].Length
,
810 if (EFI_ERROR (Status
)) {
815 PathStr
[ResultLength
] = '\0';
821 Release the resource of the URL parser.
823 @param[in] UrlParser Pointer to the parser.
832 FreePool (UrlParser
);
836 Find a specified header field according to the field name.
838 @param[in] HeaderCount Number of HTTP header structures in Headers list.
839 @param[in] Headers Array containing list of HTTP headers.
840 @param[in] FieldName Null terminated string which describes a field name.
842 @return Pointer to the found header or NULL.
848 IN UINTN HeaderCount
,
849 IN EFI_HTTP_HEADER
*Headers
,
855 if (HeaderCount
== 0 || Headers
== NULL
|| FieldName
== NULL
) {
859 for (Index
= 0; Index
< HeaderCount
; Index
++){
861 // Field names are case-insensitive (RFC 2616).
863 if (AsciiStriCmp (Headers
[Index
].FieldName
, FieldName
) == 0) {
864 return &Headers
[Index
];
872 BodyParserBodyIdentity
,
873 BodyParserChunkSizeStart
,
875 BodyParserChunkSizeEndCR
,
876 BodyParserChunkExtStart
,
877 BodyParserChunkDataStart
,
878 BodyParserChunkDataEnd
,
879 BodyParserChunkDataEndCR
,
882 BodyParserLastCRLFEnd
,
885 } HTTP_BODY_PARSE_STATE
;
888 BOOLEAN IgnoreBody
; // "MUST NOT" include a message-body
889 BOOLEAN IsChunked
; // "chunked" transfer-coding.
890 BOOLEAN ContentLengthIsValid
;
891 UINTN ContentLength
; // Entity length (not the message-body length), invalid until ContentLengthIsValid is TRUE
893 HTTP_BODY_PARSER_CALLBACK Callback
;
895 UINTN ParsedBodyLength
;
896 HTTP_BODY_PARSE_STATE State
;
897 UINTN CurrentChunkSize
;
898 UINTN CurrentChunkParsedSize
;
903 Convert an Ascii char to its uppercase.
905 @param[in] Char Ascii character.
907 @return Uppercase value of the input Char.
915 if (Char
>= 'a' && Char
<= 'z') {
916 return Char
- ('a' - 'A');
923 Convert an hexadecimal char to a value of type UINTN.
925 @param[in] Char Ascii character.
927 @return Value translated from Char.
931 HttpIoHexCharToUintn (
935 if (Char
>= '0' && Char
<= '9') {
939 return (10 + HttpIoCharToUpper (Char
) - 'A');
943 Get the value of the content length if there is a "Content-Length" header.
945 @param[in] HeaderCount Number of HTTP header structures in Headers.
946 @param[in] Headers Array containing list of HTTP headers.
947 @param[out] ContentLength Pointer to save the value of the content length.
949 @retval EFI_SUCCESS Successfully get the content length.
950 @retval EFI_NOT_FOUND No "Content-Length" header in the Headers.
954 HttpIoParseContentLengthHeader (
955 IN UINTN HeaderCount
,
956 IN EFI_HTTP_HEADER
*Headers
,
957 OUT UINTN
*ContentLength
960 EFI_HTTP_HEADER
*Header
;
962 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_CONTENT_LENGTH
);
963 if (Header
== NULL
) {
964 return EFI_NOT_FOUND
;
967 return AsciiStrDecimalToUintnS (Header
->FieldValue
, (CHAR8
**) NULL
, ContentLength
);
972 Check whether the HTTP message is using the "chunked" transfer-coding.
974 @param[in] HeaderCount Number of HTTP header structures in Headers.
975 @param[in] Headers Array containing list of HTTP headers.
977 @return The message is "chunked" transfer-coding (TRUE) or not (FALSE).
982 IN UINTN HeaderCount
,
983 IN EFI_HTTP_HEADER
*Headers
986 EFI_HTTP_HEADER
*Header
;
989 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_TRANSFER_ENCODING
);
990 if (Header
== NULL
) {
994 if (AsciiStriCmp (Header
->FieldValue
, "identity") != 0) {
1002 Check whether the HTTP message should have a message-body.
1004 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.
1005 @param[in] StatusCode Response status code returned by the remote host.
1007 @return The message should have a message-body (FALSE) or not (TRUE).
1011 HttpIoNoMessageBody (
1012 IN EFI_HTTP_METHOD Method
,
1013 IN EFI_HTTP_STATUS_CODE StatusCode
1018 // All responses to the HEAD request method
1019 // MUST NOT include a message-body, even though the presence of entity-
1020 // header fields might lead one to believe they do. All 1xx
1021 // (informational), 204 (no content), and 304 (not modified) responses
1022 // MUST NOT include a message-body. All other responses do include a
1023 // message-body, although it MAY be of zero length.
1025 if (Method
== HttpMethodHead
) {
1029 if ((StatusCode
== HTTP_STATUS_100_CONTINUE
) ||
1030 (StatusCode
== HTTP_STATUS_101_SWITCHING_PROTOCOLS
) ||
1031 (StatusCode
== HTTP_STATUS_204_NO_CONTENT
) ||
1032 (StatusCode
== HTTP_STATUS_304_NOT_MODIFIED
))
1041 Initialize a HTTP message-body parser.
1043 This function will create and initialize a HTTP message parser according to caller provided HTTP message
1044 header information. It is the caller's responsibility to free the buffer returned in *UrlParser by HttpFreeMsgParser().
1046 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.
1047 @param[in] StatusCode Response status code returned by the remote host.
1048 @param[in] HeaderCount Number of HTTP header structures in Headers.
1049 @param[in] Headers Array containing list of HTTP headers.
1050 @param[in] Callback Callback function that is invoked when parsing the HTTP message-body,
1051 set to NULL to ignore all events.
1052 @param[in] Context Pointer to the context that will be passed to Callback.
1053 @param[out] MsgParser Pointer to the returned buffer to store the message parser.
1055 @retval EFI_SUCCESS Successfully initialized the parser.
1056 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
1057 @retval EFI_INVALID_PARAMETER MsgParser is NULL or HeaderCount is not NULL but Headers is NULL.
1058 @retval Others Failed to initialize the parser.
1064 IN EFI_HTTP_METHOD Method
,
1065 IN EFI_HTTP_STATUS_CODE StatusCode
,
1066 IN UINTN HeaderCount
,
1067 IN EFI_HTTP_HEADER
*Headers
,
1068 IN HTTP_BODY_PARSER_CALLBACK Callback
,
1070 OUT VOID
**MsgParser
1074 HTTP_BODY_PARSER
*Parser
;
1076 if (HeaderCount
!= 0 && Headers
== NULL
) {
1077 return EFI_INVALID_PARAMETER
;
1080 if (MsgParser
== NULL
) {
1081 return EFI_INVALID_PARAMETER
;
1084 Parser
= AllocateZeroPool (sizeof (HTTP_BODY_PARSER
));
1085 if (Parser
== NULL
) {
1086 return EFI_OUT_OF_RESOURCES
;
1089 Parser
->State
= BodyParserBodyStart
;
1092 // Determine the message length according to RFC 2616.
1093 // 1. Check whether the message "MUST NOT" have a message-body.
1095 Parser
->IgnoreBody
= HttpIoNoMessageBody (Method
, StatusCode
);
1097 // 2. Check whether the message using "chunked" transfer-coding.
1099 Parser
->IsChunked
= HttpIoIsChunked (HeaderCount
, Headers
);
1101 // 3. Check whether the message has a Content-Length header field.
1103 Status
= HttpIoParseContentLengthHeader (HeaderCount
, Headers
, &Parser
->ContentLength
);
1104 if (!EFI_ERROR (Status
)) {
1105 Parser
->ContentLengthIsValid
= TRUE
;
1108 // 4. Range header is not supported now, so we won't meet media type "multipart/byteranges".
1109 // 5. By server closing the connection
1113 // Set state to skip body parser if the message shouldn't have a message body.
1115 if (Parser
->IgnoreBody
) {
1116 Parser
->State
= BodyParserComplete
;
1118 Parser
->Callback
= Callback
;
1119 Parser
->Context
= Context
;
1122 *MsgParser
= Parser
;
1129 Parse BodyLength of message-body. This function can be called repeatedly to parse the message-body partially.
1131 @param[in, out] MsgParser Pointer to the message parser.
1132 @param[in] BodyLength Length in bytes of the Body.
1133 @param[in] Body Pointer to the buffer of the message-body to be parsed.
1135 @retval EFI_SUCCESS Successfully parse the message-body.
1136 @retval EFI_INVALID_PARAMETER MsgParser is NULL or Body is NULL or BodyLength is 0.
1137 @retval EFI_ABORTED Operation aborted.
1138 @retval Other Error happened while parsing message body.
1143 HttpParseMessageBody (
1144 IN OUT VOID
*MsgParser
,
1145 IN UINTN BodyLength
,
1150 UINTN RemainderLengthInThis
;
1151 UINTN LengthForCallback
;
1153 HTTP_BODY_PARSER
*Parser
;
1155 if (BodyLength
== 0 || Body
== NULL
) {
1156 return EFI_INVALID_PARAMETER
;
1159 if (MsgParser
== NULL
) {
1160 return EFI_INVALID_PARAMETER
;
1163 Parser
= (HTTP_BODY_PARSER
*) MsgParser
;
1165 if (Parser
->IgnoreBody
) {
1166 Parser
->State
= BodyParserComplete
;
1167 if (Parser
->Callback
!= NULL
) {
1168 Status
= Parser
->Callback (
1169 BodyParseEventOnComplete
,
1174 if (EFI_ERROR (Status
)) {
1181 if (Parser
->State
== BodyParserBodyStart
) {
1182 Parser
->ParsedBodyLength
= 0;
1183 if (Parser
->IsChunked
) {
1184 Parser
->State
= BodyParserChunkSizeStart
;
1186 Parser
->State
= BodyParserBodyIdentity
;
1191 // The message body might be truncated in anywhere, so we need to parse is byte-by-byte.
1193 for (Char
= Body
; Char
< Body
+ BodyLength
; ) {
1195 switch (Parser
->State
) {
1196 case BodyParserStateMax
:
1199 case BodyParserBodyIdentity
:
1201 // Identity transfer-coding, just notify user to save the body data.
1203 if (Parser
->Callback
!= NULL
) {
1204 Status
= Parser
->Callback (
1205 BodyParseEventOnData
,
1207 MIN (BodyLength
, Parser
->ContentLength
- Parser
->ParsedBodyLength
),
1210 if (EFI_ERROR (Status
)) {
1214 Char
+= MIN (BodyLength
, Parser
->ContentLength
- Parser
->ParsedBodyLength
);
1215 Parser
->ParsedBodyLength
+= MIN (BodyLength
, Parser
->ContentLength
- Parser
->ParsedBodyLength
);
1216 if (Parser
->ParsedBodyLength
== Parser
->ContentLength
) {
1217 Parser
->State
= BodyParserComplete
;
1218 if (Parser
->Callback
!= NULL
) {
1219 Status
= Parser
->Callback (
1220 BodyParseEventOnComplete
,
1225 if (EFI_ERROR (Status
)) {
1232 case BodyParserChunkSizeStart
:
1234 // First byte of chunk-size, the chunk-size might be truncated.
1236 Parser
->CurrentChunkSize
= 0;
1237 Parser
->State
= BodyParserChunkSize
;
1238 case BodyParserChunkSize
:
1239 if (!NET_IS_HEX_CHAR (*Char
)) {
1241 Parser
->State
= BodyParserChunkExtStart
;
1243 } else if (*Char
== '\r') {
1244 Parser
->State
= BodyParserChunkSizeEndCR
;
1247 Parser
->State
= BodyParserStateMax
;
1252 if (Parser
->CurrentChunkSize
> (((~((UINTN
) 0)) - 16) / 16)) {
1253 return EFI_INVALID_PARAMETER
;
1255 Parser
->CurrentChunkSize
= Parser
->CurrentChunkSize
* 16 + HttpIoHexCharToUintn (*Char
);
1259 case BodyParserChunkExtStart
:
1261 // Ignore all the chunk extensions.
1263 if (*Char
== '\r') {
1264 Parser
->State
= BodyParserChunkSizeEndCR
;
1269 case BodyParserChunkSizeEndCR
:
1270 if (*Char
!= '\n') {
1271 Parser
->State
= BodyParserStateMax
;
1275 if (Parser
->CurrentChunkSize
== 0) {
1277 // The last chunk has been parsed and now assumed the state
1278 // of HttpBodyParse is ParserLastCRLF. So it need to decide
1279 // whether the rest message is trailer or last CRLF in the next round.
1281 Parser
->ContentLengthIsValid
= TRUE
;
1282 Parser
->State
= BodyParserLastCRLF
;
1285 Parser
->State
= BodyParserChunkDataStart
;
1286 Parser
->CurrentChunkParsedSize
= 0;
1289 case BodyParserLastCRLF
:
1291 // Judge the byte is belong to the Last CRLF or trailer, and then
1292 // configure the state of HttpBodyParse to corresponding state.
1294 if (*Char
== '\r') {
1296 Parser
->State
= BodyParserLastCRLFEnd
;
1299 Parser
->State
= BodyParserTrailer
;
1303 case BodyParserLastCRLFEnd
:
1304 if (*Char
== '\n') {
1305 Parser
->State
= BodyParserComplete
;
1307 if (Parser
->Callback
!= NULL
) {
1308 Status
= Parser
->Callback (
1309 BodyParseEventOnComplete
,
1314 if (EFI_ERROR (Status
)) {
1320 Parser
->State
= BodyParserStateMax
;
1324 case BodyParserTrailer
:
1325 if (*Char
== '\r') {
1326 Parser
->State
= BodyParserChunkSizeEndCR
;
1331 case BodyParserChunkDataStart
:
1333 // First byte of chunk-data, the chunk data also might be truncated.
1335 RemainderLengthInThis
= BodyLength
- (Char
- Body
);
1336 LengthForCallback
= MIN (Parser
->CurrentChunkSize
- Parser
->CurrentChunkParsedSize
, RemainderLengthInThis
);
1337 if (Parser
->Callback
!= NULL
) {
1338 Status
= Parser
->Callback (
1339 BodyParseEventOnData
,
1344 if (EFI_ERROR (Status
)) {
1348 Char
+= LengthForCallback
;
1349 Parser
->ContentLength
+= LengthForCallback
;
1350 Parser
->CurrentChunkParsedSize
+= LengthForCallback
;
1351 if (Parser
->CurrentChunkParsedSize
== Parser
->CurrentChunkSize
) {
1352 Parser
->State
= BodyParserChunkDataEnd
;
1356 case BodyParserChunkDataEnd
:
1357 if (*Char
== '\r') {
1358 Parser
->State
= BodyParserChunkDataEndCR
;
1360 Parser
->State
= BodyParserStateMax
;
1365 case BodyParserChunkDataEndCR
:
1366 if (*Char
!= '\n') {
1367 Parser
->State
= BodyParserStateMax
;
1371 Parser
->State
= BodyParserChunkSizeStart
;
1380 if (Parser
->State
== BodyParserStateMax
) {
1388 Check whether the message-body is complete or not.
1390 @param[in] MsgParser Pointer to the message parser.
1392 @retval TRUE Message-body is complete.
1393 @retval FALSE Message-body is not complete.
1398 HttpIsMessageComplete (
1402 HTTP_BODY_PARSER
*Parser
;
1404 if (MsgParser
== NULL
) {
1408 Parser
= (HTTP_BODY_PARSER
*) MsgParser
;
1410 if (Parser
->State
== BodyParserComplete
) {
1417 Get the content length of the entity.
1419 Note that in trunk transfer, the entity length is not valid until the whole message body is received.
1421 @param[in] MsgParser Pointer to the message parser.
1422 @param[out] ContentLength Pointer to store the length of the entity.
1424 @retval EFI_SUCCESS Successfully to get the entity length.
1425 @retval EFI_NOT_READY Entity length is not valid yet.
1426 @retval EFI_INVALID_PARAMETER MsgParser is NULL or ContentLength is NULL.
1431 HttpGetEntityLength (
1433 OUT UINTN
*ContentLength
1436 HTTP_BODY_PARSER
*Parser
;
1438 if (MsgParser
== NULL
|| ContentLength
== NULL
) {
1439 return EFI_INVALID_PARAMETER
;
1442 Parser
= (HTTP_BODY_PARSER
*) MsgParser
;
1444 if (!Parser
->ContentLengthIsValid
) {
1445 return EFI_NOT_READY
;
1448 *ContentLength
= Parser
->ContentLength
;
1453 Release the resource of the message parser.
1455 @param[in] MsgParser Pointer to the message parser.
1464 FreePool (MsgParser
);
1469 Get the next string, which is distinguished by specified separator.
1471 @param[in] String Pointer to the string.
1472 @param[in] Separator Specified separator used to distinguish where is the beginning
1475 @return Pointer to the next string.
1476 @return NULL if not find or String is NULL.
1480 AsciiStrGetNextToken (
1481 IN CONST CHAR8
*String
,
1492 if (*Token
== Separator
) {
1493 return (CHAR8
*)(Token
+ 1);
1500 Set FieldName and FieldValue into specified HttpHeader.
1502 @param[in,out] HttpHeader Specified HttpHeader.
1503 @param[in] FieldName FieldName of this HttpHeader, a NULL terminated ASCII string.
1504 @param[in] FieldValue FieldValue of this HttpHeader, a NULL terminated ASCII string.
1507 @retval EFI_SUCCESS The FieldName and FieldValue are set into HttpHeader successfully.
1508 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1509 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
1514 HttpSetFieldNameAndValue (
1515 IN OUT EFI_HTTP_HEADER
*HttpHeader
,
1516 IN CONST CHAR8
*FieldName
,
1517 IN CONST CHAR8
*FieldValue
1520 UINTN FieldNameSize
;
1521 UINTN FieldValueSize
;
1523 if (HttpHeader
== NULL
|| FieldName
== NULL
|| FieldValue
== NULL
) {
1524 return EFI_INVALID_PARAMETER
;
1527 if (HttpHeader
->FieldName
!= NULL
) {
1528 FreePool (HttpHeader
->FieldName
);
1530 if (HttpHeader
->FieldValue
!= NULL
) {
1531 FreePool (HttpHeader
->FieldValue
);
1534 FieldNameSize
= AsciiStrSize (FieldName
);
1535 HttpHeader
->FieldName
= AllocateZeroPool (FieldNameSize
);
1536 if (HttpHeader
->FieldName
== NULL
) {
1537 return EFI_OUT_OF_RESOURCES
;
1539 CopyMem (HttpHeader
->FieldName
, FieldName
, FieldNameSize
);
1540 HttpHeader
->FieldName
[FieldNameSize
- 1] = 0;
1542 FieldValueSize
= AsciiStrSize (FieldValue
);
1543 HttpHeader
->FieldValue
= AllocateZeroPool (FieldValueSize
);
1544 if (HttpHeader
->FieldValue
== NULL
) {
1545 FreePool (HttpHeader
->FieldName
);
1546 return EFI_OUT_OF_RESOURCES
;
1548 CopyMem (HttpHeader
->FieldValue
, FieldValue
, FieldValueSize
);
1549 HttpHeader
->FieldValue
[FieldValueSize
- 1] = 0;
1555 Get one key/value header pair from the raw string.
1557 @param[in] String Pointer to the raw string.
1558 @param[out] FieldName Points directly to field name within 'HttpHeader'.
1559 @param[out] FieldValue Points directly to field value within 'HttpHeader'.
1561 @return Pointer to the next raw string.
1562 @return NULL if no key/value header pair from this raw string.
1567 HttpGetFieldNameAndValue (
1569 OUT CHAR8
**FieldName
,
1570 OUT CHAR8
**FieldValue
1573 CHAR8
*FieldNameStr
;
1574 CHAR8
*FieldValueStr
;
1578 if (String
== NULL
|| FieldName
== NULL
|| FieldValue
== NULL
) {
1584 FieldNameStr
= NULL
;
1585 FieldValueStr
= NULL
;
1591 // Check whether the raw HTTP header string is valid or not.
1593 EndofHeader
= AsciiStrStr (String
, "\r\n\r\n");
1594 if (EndofHeader
== NULL
) {
1599 // Each header field consists of a name followed by a colon (":") and the field value.
1601 FieldNameStr
= String
;
1602 FieldValueStr
= AsciiStrGetNextToken (FieldNameStr
, ':');
1603 if (FieldValueStr
== NULL
) {
1608 // Replace ':' with 0
1610 *(FieldValueStr
- 1) = 0;
1613 // The field value MAY be preceded by any amount of LWS, though a single SP is preferred.
1614 // Note: LWS = [CRLF] 1*(SP|HT), it can be '\r\n ' or '\r\n\t' or ' ' or '\t'.
1620 if (*FieldValueStr
== ' ' || *FieldValueStr
== '\t') {
1622 // Boundary condition check.
1624 if ((UINTN
) EndofHeader
- (UINTN
) FieldValueStr
< 1) {
1629 } else if (*FieldValueStr
== '\r') {
1631 // Boundary condition check.
1633 if ((UINTN
) EndofHeader
- (UINTN
) FieldValueStr
< 3) {
1637 if (*(FieldValueStr
+ 1) == '\n' && (*(FieldValueStr
+ 2) == ' ' || *(FieldValueStr
+ 2) == '\t')) {
1638 FieldValueStr
= FieldValueStr
+ 3;
1646 // Header fields can be extended over multiple lines by preceding each extra
1647 // line with at least one SP or HT.
1649 StrPtr
= FieldValueStr
;
1651 StrPtr
= AsciiStrGetNextToken (StrPtr
, '\r');
1652 if (StrPtr
== NULL
|| *StrPtr
!= '\n') {
1657 } while (*StrPtr
== ' ' || *StrPtr
== '\t');
1660 // Replace '\r' with 0
1665 // Get FieldName and FieldValue.
1667 *FieldName
= FieldNameStr
;
1668 *FieldValue
= FieldValueStr
;
1674 Free existing HeaderFields.
1676 @param[in] HeaderFields Pointer to array of key/value header pairs waitting for free.
1677 @param[in] FieldCount The number of header pairs in HeaderFields.
1682 HttpFreeHeaderFields (
1683 IN EFI_HTTP_HEADER
*HeaderFields
,
1689 if (HeaderFields
!= NULL
) {
1690 for (Index
= 0; Index
< FieldCount
; Index
++) {
1691 if (HeaderFields
[Index
].FieldName
!= NULL
) {
1692 FreePool (HeaderFields
[Index
].FieldName
);
1694 if (HeaderFields
[Index
].FieldValue
!= NULL
) {
1695 FreePool (HeaderFields
[Index
].FieldValue
);
1699 FreePool (HeaderFields
);
1704 Generate HTTP request message.
1706 This function will allocate memory for the whole HTTP message and generate a
1707 well formatted HTTP Request message in it, include the Request-Line, header
1708 fields and also the message body. It is the caller's responsibility to free
1709 the buffer returned in *RequestMsg.
1711 @param[in] Message Pointer to the EFI_HTTP_MESSAGE structure which
1712 contains the required information to generate
1713 the HTTP request message.
1714 @param[in] Url The URL of a remote host.
1715 @param[out] RequestMsg Pointer to the created HTTP request message.
1716 NULL if any error occured.
1717 @param[out] RequestMsgSize Size of the RequestMsg (in bytes).
1719 @retval EFI_SUCCESS If HTTP request string was created successfully.
1720 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
1721 @retval EFI_INVALID_PARAMETER The input arguments are invalid.
1726 HttpGenRequestMessage (
1727 IN CONST EFI_HTTP_MESSAGE
*Message
,
1728 IN CONST CHAR8
*Url
,
1729 OUT CHAR8
**RequestMsg
,
1730 OUT UINTN
*RequestMsgSize
1740 EFI_HTTP_HEADER
**AppendList
;
1742 EFI_HTTP_UTILITIES_PROTOCOL
*HttpUtilitiesProtocol
;
1744 Status
= EFI_SUCCESS
;
1750 HttpUtilitiesProtocol
= NULL
;
1753 // 1. If we have a Request, we cannot have a NULL Url
1754 // 2. If we have a Request, HeaderCount can not be non-zero
1755 // 3. If we do not have a Request, HeaderCount should be zero
1756 // 4. If we do not have Request and Headers, we need at least a message-body
1758 if ((Message
== NULL
|| RequestMsg
== NULL
|| RequestMsgSize
== NULL
) ||
1759 (Message
->Data
.Request
!= NULL
&& Url
== NULL
) ||
1760 (Message
->Data
.Request
!= NULL
&& Message
->HeaderCount
== 0) ||
1761 (Message
->Data
.Request
== NULL
&& Message
->HeaderCount
!= 0) ||
1762 (Message
->Data
.Request
== NULL
&& Message
->HeaderCount
== 0 && Message
->BodyLength
== 0)) {
1763 return EFI_INVALID_PARAMETER
;
1766 if (Message
->HeaderCount
!= 0) {
1768 // Locate the HTTP_UTILITIES protocol.
1770 Status
= gBS
->LocateProtocol (
1771 &gEfiHttpUtilitiesProtocolGuid
,
1773 (VOID
**) &HttpUtilitiesProtocol
1776 if (EFI_ERROR (Status
)) {
1777 DEBUG ((DEBUG_ERROR
,"Failed to locate Http Utilities protocol. Status = %r.\n", Status
));
1782 // Build AppendList to send into HttpUtilitiesBuild
1784 AppendList
= AllocateZeroPool (sizeof (EFI_HTTP_HEADER
*) * (Message
->HeaderCount
));
1785 if (AppendList
== NULL
) {
1786 return EFI_OUT_OF_RESOURCES
;
1789 for(Index
= 0; Index
< Message
->HeaderCount
; Index
++){
1790 AppendList
[Index
] = &Message
->Headers
[Index
];
1794 // Build raw HTTP Headers
1796 Status
= HttpUtilitiesProtocol
->Build (
1797 HttpUtilitiesProtocol
,
1802 Message
->HeaderCount
,
1808 FreePool (AppendList
);
1810 if (EFI_ERROR (Status
) || HttpHdr
== NULL
){
1816 // If we have headers to be sent, account for it.
1818 if (Message
->HeaderCount
!= 0) {
1819 MsgSize
= HttpHdrSize
;
1823 // If we have a request line, account for the fields.
1825 if (Message
->Data
.Request
!= NULL
) {
1826 MsgSize
+= HTTP_METHOD_MAXIMUM_LEN
+ AsciiStrLen (HTTP_VERSION_CRLF_STR
) + AsciiStrLen (Url
);
1831 // If we have a message body to be sent, account for it.
1833 MsgSize
+= Message
->BodyLength
;
1836 // memory for the string that needs to be sent to TCP
1839 *RequestMsg
= AllocateZeroPool (MsgSize
);
1840 if (*RequestMsg
== NULL
) {
1841 Status
= EFI_OUT_OF_RESOURCES
;
1845 RequestPtr
= *RequestMsg
;
1847 // Construct header request
1849 if (Message
->Data
.Request
!= NULL
) {
1850 switch (Message
->Data
.Request
->Method
) {
1852 StrLength
= sizeof (HTTP_METHOD_GET
) - 1;
1853 CopyMem (RequestPtr
, HTTP_METHOD_GET
, StrLength
);
1854 RequestPtr
+= StrLength
;
1857 StrLength
= sizeof (HTTP_METHOD_PUT
) - 1;
1858 CopyMem (RequestPtr
, HTTP_METHOD_PUT
, StrLength
);
1859 RequestPtr
+= StrLength
;
1861 case HttpMethodPatch
:
1862 StrLength
= sizeof (HTTP_METHOD_PATCH
) - 1;
1863 CopyMem (RequestPtr
, HTTP_METHOD_PATCH
, StrLength
);
1864 RequestPtr
+= StrLength
;
1866 case HttpMethodPost
:
1867 StrLength
= sizeof (HTTP_METHOD_POST
) - 1;
1868 CopyMem (RequestPtr
, HTTP_METHOD_POST
, StrLength
);
1869 RequestPtr
+= StrLength
;
1871 case HttpMethodHead
:
1872 StrLength
= sizeof (HTTP_METHOD_HEAD
) - 1;
1873 CopyMem (RequestPtr
, HTTP_METHOD_HEAD
, StrLength
);
1874 RequestPtr
+= StrLength
;
1876 case HttpMethodDelete
:
1877 StrLength
= sizeof (HTTP_METHOD_DELETE
) - 1;
1878 CopyMem (RequestPtr
, HTTP_METHOD_DELETE
, StrLength
);
1879 RequestPtr
+= StrLength
;
1883 Status
= EFI_INVALID_PARAMETER
;
1887 StrLength
= AsciiStrLen(EMPTY_SPACE
);
1888 CopyMem (RequestPtr
, EMPTY_SPACE
, StrLength
);
1889 RequestPtr
+= StrLength
;
1891 StrLength
= AsciiStrLen (Url
);
1892 CopyMem (RequestPtr
, Url
, StrLength
);
1893 RequestPtr
+= StrLength
;
1895 StrLength
= sizeof (HTTP_VERSION_CRLF_STR
) - 1;
1896 CopyMem (RequestPtr
, HTTP_VERSION_CRLF_STR
, StrLength
);
1897 RequestPtr
+= StrLength
;
1899 if (HttpHdr
!= NULL
) {
1903 CopyMem (RequestPtr
, HttpHdr
, HttpHdrSize
);
1904 RequestPtr
+= HttpHdrSize
;
1911 if (Message
->Body
!= NULL
) {
1912 CopyMem (RequestPtr
, Message
->Body
, Message
->BodyLength
);
1913 RequestPtr
+= Message
->BodyLength
;
1919 (*RequestMsgSize
) = (UINTN
)(RequestPtr
) - (UINTN
)(*RequestMsg
);
1925 if (*RequestMsg
!= NULL
) {
1926 FreePool (*RequestMsg
);
1932 if (HttpHdr
!= NULL
) {
1940 Translate the status code in HTTP message to EFI_HTTP_STATUS_CODE defined
1941 in UEFI 2.5 specification.
1943 @param[in] StatusCode The status code value in HTTP message.
1945 @return Value defined in EFI_HTTP_STATUS_CODE .
1948 EFI_HTTP_STATUS_CODE
1950 HttpMappingToStatusCode (
1954 switch (StatusCode
) {
1956 return HTTP_STATUS_100_CONTINUE
;
1958 return HTTP_STATUS_101_SWITCHING_PROTOCOLS
;
1960 return HTTP_STATUS_200_OK
;
1962 return HTTP_STATUS_201_CREATED
;
1964 return HTTP_STATUS_202_ACCEPTED
;
1966 return HTTP_STATUS_203_NON_AUTHORITATIVE_INFORMATION
;
1968 return HTTP_STATUS_204_NO_CONTENT
;
1970 return HTTP_STATUS_205_RESET_CONTENT
;
1972 return HTTP_STATUS_206_PARTIAL_CONTENT
;
1974 return HTTP_STATUS_300_MULTIPLE_CHOICES
;
1976 return HTTP_STATUS_301_MOVED_PERMANENTLY
;
1978 return HTTP_STATUS_302_FOUND
;
1980 return HTTP_STATUS_303_SEE_OTHER
;
1982 return HTTP_STATUS_304_NOT_MODIFIED
;
1984 return HTTP_STATUS_305_USE_PROXY
;
1986 return HTTP_STATUS_307_TEMPORARY_REDIRECT
;
1988 return HTTP_STATUS_308_PERMANENT_REDIRECT
;
1990 return HTTP_STATUS_400_BAD_REQUEST
;
1992 return HTTP_STATUS_401_UNAUTHORIZED
;
1994 return HTTP_STATUS_402_PAYMENT_REQUIRED
;
1996 return HTTP_STATUS_403_FORBIDDEN
;
1998 return HTTP_STATUS_404_NOT_FOUND
;
2000 return HTTP_STATUS_405_METHOD_NOT_ALLOWED
;
2002 return HTTP_STATUS_406_NOT_ACCEPTABLE
;
2004 return HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED
;
2006 return HTTP_STATUS_408_REQUEST_TIME_OUT
;
2008 return HTTP_STATUS_409_CONFLICT
;
2010 return HTTP_STATUS_410_GONE
;
2012 return HTTP_STATUS_411_LENGTH_REQUIRED
;
2014 return HTTP_STATUS_412_PRECONDITION_FAILED
;
2016 return HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE
;
2018 return HTTP_STATUS_414_REQUEST_URI_TOO_LARGE
;
2020 return HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE
;
2022 return HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED
;
2024 return HTTP_STATUS_417_EXPECTATION_FAILED
;
2026 return HTTP_STATUS_500_INTERNAL_SERVER_ERROR
;
2028 return HTTP_STATUS_501_NOT_IMPLEMENTED
;
2030 return HTTP_STATUS_502_BAD_GATEWAY
;
2032 return HTTP_STATUS_503_SERVICE_UNAVAILABLE
;
2034 return HTTP_STATUS_504_GATEWAY_TIME_OUT
;
2036 return HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED
;
2039 return HTTP_STATUS_UNSUPPORTED_STATUS
;
2044 Check whether header field called FieldName is in DeleteList.
2046 @param[in] DeleteList Pointer to array of key/value header pairs.
2047 @param[in] DeleteCount The number of header pairs.
2048 @param[in] FieldName Pointer to header field's name.
2050 @return TRUE if FieldName is not in DeleteList, that means this header field is valid.
2051 @return FALSE if FieldName is in DeleteList, that means this header field is invalid.
2056 HttpIsValidHttpHeader (
2057 IN CHAR8
*DeleteList
[],
2058 IN UINTN DeleteCount
,
2064 if (FieldName
== NULL
) {
2068 for (Index
= 0; Index
< DeleteCount
; Index
++) {
2069 if (DeleteList
[Index
] == NULL
) {
2073 if (AsciiStrCmp (FieldName
, DeleteList
[Index
]) == 0) {