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 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
;
902 Convert an hexadecimal char to a value of type UINTN.
904 @param[in] Char Ascii character.
906 @return Value translated from Char.
910 HttpIoHexCharToUintn (
914 if (Char
>= '0' && Char
<= '9') {
918 return (10 + AsciiCharToUpper (Char
) - 'A');
922 Get the value of the content length if there is a "Content-Length" header.
924 @param[in] HeaderCount Number of HTTP header structures in Headers.
925 @param[in] Headers Array containing list of HTTP headers.
926 @param[out] ContentLength Pointer to save the value of the content length.
928 @retval EFI_SUCCESS Successfully get the content length.
929 @retval EFI_NOT_FOUND No "Content-Length" header in the Headers.
933 HttpIoParseContentLengthHeader (
934 IN UINTN HeaderCount
,
935 IN EFI_HTTP_HEADER
*Headers
,
936 OUT UINTN
*ContentLength
939 EFI_HTTP_HEADER
*Header
;
941 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_CONTENT_LENGTH
);
942 if (Header
== NULL
) {
943 return EFI_NOT_FOUND
;
946 return AsciiStrDecimalToUintnS (Header
->FieldValue
, (CHAR8
**) NULL
, ContentLength
);
951 Check whether the HTTP message is using the "chunked" transfer-coding.
953 @param[in] HeaderCount Number of HTTP header structures in Headers.
954 @param[in] Headers Array containing list of HTTP headers.
956 @return The message is "chunked" transfer-coding (TRUE) or not (FALSE).
961 IN UINTN HeaderCount
,
962 IN EFI_HTTP_HEADER
*Headers
965 EFI_HTTP_HEADER
*Header
;
968 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_TRANSFER_ENCODING
);
969 if (Header
== NULL
) {
973 if (AsciiStriCmp (Header
->FieldValue
, "identity") != 0) {
981 Check whether the HTTP message should have a message-body.
983 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.
984 @param[in] StatusCode Response status code returned by the remote host.
986 @return The message should have a message-body (FALSE) or not (TRUE).
990 HttpIoNoMessageBody (
991 IN EFI_HTTP_METHOD Method
,
992 IN EFI_HTTP_STATUS_CODE StatusCode
997 // All responses to the HEAD request method
998 // MUST NOT include a message-body, even though the presence of entity-
999 // header fields might lead one to believe they do. All 1xx
1000 // (informational), 204 (no content), and 304 (not modified) responses
1001 // MUST NOT include a message-body. All other responses do include a
1002 // message-body, although it MAY be of zero length.
1004 if (Method
== HttpMethodHead
) {
1008 if ((StatusCode
== HTTP_STATUS_100_CONTINUE
) ||
1009 (StatusCode
== HTTP_STATUS_101_SWITCHING_PROTOCOLS
) ||
1010 (StatusCode
== HTTP_STATUS_204_NO_CONTENT
) ||
1011 (StatusCode
== HTTP_STATUS_304_NOT_MODIFIED
))
1020 Initialize a HTTP message-body parser.
1022 This function will create and initialize a HTTP message parser according to caller provided HTTP message
1023 header information. It is the caller's responsibility to free the buffer returned in *UrlParser by HttpFreeMsgParser().
1025 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.
1026 @param[in] StatusCode Response status code returned by the remote host.
1027 @param[in] HeaderCount Number of HTTP header structures in Headers.
1028 @param[in] Headers Array containing list of HTTP headers.
1029 @param[in] Callback Callback function that is invoked when parsing the HTTP message-body,
1030 set to NULL to ignore all events.
1031 @param[in] Context Pointer to the context that will be passed to Callback.
1032 @param[out] MsgParser Pointer to the returned buffer to store the message parser.
1034 @retval EFI_SUCCESS Successfully initialized the parser.
1035 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
1036 @retval EFI_INVALID_PARAMETER MsgParser is NULL or HeaderCount is not NULL but Headers is NULL.
1037 @retval Others Failed to initialize the parser.
1043 IN EFI_HTTP_METHOD Method
,
1044 IN EFI_HTTP_STATUS_CODE StatusCode
,
1045 IN UINTN HeaderCount
,
1046 IN EFI_HTTP_HEADER
*Headers
,
1047 IN HTTP_BODY_PARSER_CALLBACK Callback
,
1049 OUT VOID
**MsgParser
1053 HTTP_BODY_PARSER
*Parser
;
1055 if (HeaderCount
!= 0 && Headers
== NULL
) {
1056 return EFI_INVALID_PARAMETER
;
1059 if (MsgParser
== NULL
) {
1060 return EFI_INVALID_PARAMETER
;
1063 Parser
= AllocateZeroPool (sizeof (HTTP_BODY_PARSER
));
1064 if (Parser
== NULL
) {
1065 return EFI_OUT_OF_RESOURCES
;
1068 Parser
->State
= BodyParserBodyStart
;
1071 // Determine the message length according to RFC 2616.
1072 // 1. Check whether the message "MUST NOT" have a message-body.
1074 Parser
->IgnoreBody
= HttpIoNoMessageBody (Method
, StatusCode
);
1076 // 2. Check whether the message using "chunked" transfer-coding.
1078 Parser
->IsChunked
= HttpIoIsChunked (HeaderCount
, Headers
);
1080 // 3. Check whether the message has a Content-Length header field.
1082 Status
= HttpIoParseContentLengthHeader (HeaderCount
, Headers
, &Parser
->ContentLength
);
1083 if (!EFI_ERROR (Status
)) {
1084 Parser
->ContentLengthIsValid
= TRUE
;
1087 // 4. Range header is not supported now, so we won't meet media type "multipart/byteranges".
1088 // 5. By server closing the connection
1092 // Set state to skip body parser if the message shouldn't have a message body.
1094 if (Parser
->IgnoreBody
) {
1095 Parser
->State
= BodyParserComplete
;
1097 Parser
->Callback
= Callback
;
1098 Parser
->Context
= Context
;
1101 *MsgParser
= Parser
;
1108 Parse BodyLength of message-body. This function can be called repeatedly to parse the message-body partially.
1110 @param[in, out] MsgParser Pointer to the message parser.
1111 @param[in] BodyLength Length in bytes of the Body.
1112 @param[in] Body Pointer to the buffer of the message-body to be parsed.
1114 @retval EFI_SUCCESS Successfully parse the message-body.
1115 @retval EFI_INVALID_PARAMETER MsgParser is NULL or Body is NULL or BodyLength is 0.
1116 @retval EFI_ABORTED Operation aborted.
1117 @retval Other Error happened while parsing message body.
1122 HttpParseMessageBody (
1123 IN OUT VOID
*MsgParser
,
1124 IN UINTN BodyLength
,
1129 UINTN RemainderLengthInThis
;
1130 UINTN LengthForCallback
;
1132 HTTP_BODY_PARSER
*Parser
;
1134 if (BodyLength
== 0 || Body
== NULL
) {
1135 return EFI_INVALID_PARAMETER
;
1138 if (MsgParser
== NULL
) {
1139 return EFI_INVALID_PARAMETER
;
1142 Parser
= (HTTP_BODY_PARSER
*) MsgParser
;
1144 if (Parser
->IgnoreBody
) {
1145 Parser
->State
= BodyParserComplete
;
1146 if (Parser
->Callback
!= NULL
) {
1147 Status
= Parser
->Callback (
1148 BodyParseEventOnComplete
,
1153 if (EFI_ERROR (Status
)) {
1160 if (Parser
->State
== BodyParserBodyStart
) {
1161 Parser
->ParsedBodyLength
= 0;
1162 if (Parser
->IsChunked
) {
1163 Parser
->State
= BodyParserChunkSizeStart
;
1165 Parser
->State
= BodyParserBodyIdentity
;
1170 // The message body might be truncated in anywhere, so we need to parse is byte-by-byte.
1172 for (Char
= Body
; Char
< Body
+ BodyLength
; ) {
1174 switch (Parser
->State
) {
1175 case BodyParserStateMax
:
1178 case BodyParserBodyIdentity
:
1180 // Identity transfer-coding, just notify user to save the body data.
1182 if (Parser
->Callback
!= NULL
) {
1183 Status
= Parser
->Callback (
1184 BodyParseEventOnData
,
1186 MIN (BodyLength
, Parser
->ContentLength
- Parser
->ParsedBodyLength
),
1189 if (EFI_ERROR (Status
)) {
1193 Char
+= MIN (BodyLength
, Parser
->ContentLength
- Parser
->ParsedBodyLength
);
1194 Parser
->ParsedBodyLength
+= MIN (BodyLength
, Parser
->ContentLength
- Parser
->ParsedBodyLength
);
1195 if (Parser
->ParsedBodyLength
== Parser
->ContentLength
) {
1196 Parser
->State
= BodyParserComplete
;
1197 if (Parser
->Callback
!= NULL
) {
1198 Status
= Parser
->Callback (
1199 BodyParseEventOnComplete
,
1204 if (EFI_ERROR (Status
)) {
1211 case BodyParserChunkSizeStart
:
1213 // First byte of chunk-size, the chunk-size might be truncated.
1215 Parser
->CurrentChunkSize
= 0;
1216 Parser
->State
= BodyParserChunkSize
;
1217 case BodyParserChunkSize
:
1218 if (!NET_IS_HEX_CHAR (*Char
)) {
1220 Parser
->State
= BodyParserChunkExtStart
;
1222 } else if (*Char
== '\r') {
1223 Parser
->State
= BodyParserChunkSizeEndCR
;
1226 Parser
->State
= BodyParserStateMax
;
1231 if (Parser
->CurrentChunkSize
> (((~((UINTN
) 0)) - 16) / 16)) {
1232 return EFI_INVALID_PARAMETER
;
1234 Parser
->CurrentChunkSize
= Parser
->CurrentChunkSize
* 16 + HttpIoHexCharToUintn (*Char
);
1238 case BodyParserChunkExtStart
:
1240 // Ignore all the chunk extensions.
1242 if (*Char
== '\r') {
1243 Parser
->State
= BodyParserChunkSizeEndCR
;
1248 case BodyParserChunkSizeEndCR
:
1249 if (*Char
!= '\n') {
1250 Parser
->State
= BodyParserStateMax
;
1254 if (Parser
->CurrentChunkSize
== 0) {
1256 // The last chunk has been parsed and now assumed the state
1257 // of HttpBodyParse is ParserLastCRLF. So it need to decide
1258 // whether the rest message is trailer or last CRLF in the next round.
1260 Parser
->ContentLengthIsValid
= TRUE
;
1261 Parser
->State
= BodyParserLastCRLF
;
1264 Parser
->State
= BodyParserChunkDataStart
;
1265 Parser
->CurrentChunkParsedSize
= 0;
1268 case BodyParserLastCRLF
:
1270 // Judge the byte is belong to the Last CRLF or trailer, and then
1271 // configure the state of HttpBodyParse to corresponding state.
1273 if (*Char
== '\r') {
1275 Parser
->State
= BodyParserLastCRLFEnd
;
1278 Parser
->State
= BodyParserTrailer
;
1282 case BodyParserLastCRLFEnd
:
1283 if (*Char
== '\n') {
1284 Parser
->State
= BodyParserComplete
;
1286 if (Parser
->Callback
!= NULL
) {
1287 Status
= Parser
->Callback (
1288 BodyParseEventOnComplete
,
1293 if (EFI_ERROR (Status
)) {
1299 Parser
->State
= BodyParserStateMax
;
1303 case BodyParserTrailer
:
1304 if (*Char
== '\r') {
1305 Parser
->State
= BodyParserChunkSizeEndCR
;
1310 case BodyParserChunkDataStart
:
1312 // First byte of chunk-data, the chunk data also might be truncated.
1314 RemainderLengthInThis
= BodyLength
- (Char
- Body
);
1315 LengthForCallback
= MIN (Parser
->CurrentChunkSize
- Parser
->CurrentChunkParsedSize
, RemainderLengthInThis
);
1316 if (Parser
->Callback
!= NULL
) {
1317 Status
= Parser
->Callback (
1318 BodyParseEventOnData
,
1323 if (EFI_ERROR (Status
)) {
1327 Char
+= LengthForCallback
;
1328 Parser
->ContentLength
+= LengthForCallback
;
1329 Parser
->CurrentChunkParsedSize
+= LengthForCallback
;
1330 if (Parser
->CurrentChunkParsedSize
== Parser
->CurrentChunkSize
) {
1331 Parser
->State
= BodyParserChunkDataEnd
;
1335 case BodyParserChunkDataEnd
:
1336 if (*Char
== '\r') {
1337 Parser
->State
= BodyParserChunkDataEndCR
;
1339 Parser
->State
= BodyParserStateMax
;
1344 case BodyParserChunkDataEndCR
:
1345 if (*Char
!= '\n') {
1346 Parser
->State
= BodyParserStateMax
;
1350 Parser
->State
= BodyParserChunkSizeStart
;
1359 if (Parser
->State
== BodyParserStateMax
) {
1367 Check whether the message-body is complete or not.
1369 @param[in] MsgParser Pointer to the message parser.
1371 @retval TRUE Message-body is complete.
1372 @retval FALSE Message-body is not complete.
1377 HttpIsMessageComplete (
1381 HTTP_BODY_PARSER
*Parser
;
1383 if (MsgParser
== NULL
) {
1387 Parser
= (HTTP_BODY_PARSER
*) MsgParser
;
1389 if (Parser
->State
== BodyParserComplete
) {
1396 Get the content length of the entity.
1398 Note that in trunk transfer, the entity length is not valid until the whole message body is received.
1400 @param[in] MsgParser Pointer to the message parser.
1401 @param[out] ContentLength Pointer to store the length of the entity.
1403 @retval EFI_SUCCESS Successfully to get the entity length.
1404 @retval EFI_NOT_READY Entity length is not valid yet.
1405 @retval EFI_INVALID_PARAMETER MsgParser is NULL or ContentLength is NULL.
1410 HttpGetEntityLength (
1412 OUT UINTN
*ContentLength
1415 HTTP_BODY_PARSER
*Parser
;
1417 if (MsgParser
== NULL
|| ContentLength
== NULL
) {
1418 return EFI_INVALID_PARAMETER
;
1421 Parser
= (HTTP_BODY_PARSER
*) MsgParser
;
1423 if (!Parser
->ContentLengthIsValid
) {
1424 return EFI_NOT_READY
;
1427 *ContentLength
= Parser
->ContentLength
;
1432 Release the resource of the message parser.
1434 @param[in] MsgParser Pointer to the message parser.
1443 FreePool (MsgParser
);
1448 Get the next string, which is distinguished by specified separator.
1450 @param[in] String Pointer to the string.
1451 @param[in] Separator Specified separator used to distinguish where is the beginning
1454 @return Pointer to the next string.
1455 @return NULL if not find or String is NULL.
1459 AsciiStrGetNextToken (
1460 IN CONST CHAR8
*String
,
1471 if (*Token
== Separator
) {
1472 return (CHAR8
*)(Token
+ 1);
1479 Set FieldName and FieldValue into specified HttpHeader.
1481 @param[in,out] HttpHeader Specified HttpHeader.
1482 @param[in] FieldName FieldName of this HttpHeader, a NULL terminated ASCII string.
1483 @param[in] FieldValue FieldValue of this HttpHeader, a NULL terminated ASCII string.
1486 @retval EFI_SUCCESS The FieldName and FieldValue are set into HttpHeader successfully.
1487 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1488 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
1493 HttpSetFieldNameAndValue (
1494 IN OUT EFI_HTTP_HEADER
*HttpHeader
,
1495 IN CONST CHAR8
*FieldName
,
1496 IN CONST CHAR8
*FieldValue
1499 UINTN FieldNameSize
;
1500 UINTN FieldValueSize
;
1502 if (HttpHeader
== NULL
|| FieldName
== NULL
|| FieldValue
== NULL
) {
1503 return EFI_INVALID_PARAMETER
;
1506 if (HttpHeader
->FieldName
!= NULL
) {
1507 FreePool (HttpHeader
->FieldName
);
1509 if (HttpHeader
->FieldValue
!= NULL
) {
1510 FreePool (HttpHeader
->FieldValue
);
1513 FieldNameSize
= AsciiStrSize (FieldName
);
1514 HttpHeader
->FieldName
= AllocateZeroPool (FieldNameSize
);
1515 if (HttpHeader
->FieldName
== NULL
) {
1516 return EFI_OUT_OF_RESOURCES
;
1518 CopyMem (HttpHeader
->FieldName
, FieldName
, FieldNameSize
);
1519 HttpHeader
->FieldName
[FieldNameSize
- 1] = 0;
1521 FieldValueSize
= AsciiStrSize (FieldValue
);
1522 HttpHeader
->FieldValue
= AllocateZeroPool (FieldValueSize
);
1523 if (HttpHeader
->FieldValue
== NULL
) {
1524 FreePool (HttpHeader
->FieldName
);
1525 return EFI_OUT_OF_RESOURCES
;
1527 CopyMem (HttpHeader
->FieldValue
, FieldValue
, FieldValueSize
);
1528 HttpHeader
->FieldValue
[FieldValueSize
- 1] = 0;
1534 Get one key/value header pair from the raw string.
1536 @param[in] String Pointer to the raw string.
1537 @param[out] FieldName Points directly to field name within 'HttpHeader'.
1538 @param[out] FieldValue Points directly to field value within 'HttpHeader'.
1540 @return Pointer to the next raw string.
1541 @return NULL if no key/value header pair from this raw string.
1546 HttpGetFieldNameAndValue (
1548 OUT CHAR8
**FieldName
,
1549 OUT CHAR8
**FieldValue
1552 CHAR8
*FieldNameStr
;
1553 CHAR8
*FieldValueStr
;
1557 if (String
== NULL
|| FieldName
== NULL
|| FieldValue
== NULL
) {
1563 FieldNameStr
= NULL
;
1564 FieldValueStr
= NULL
;
1570 // Check whether the raw HTTP header string is valid or not.
1572 EndofHeader
= AsciiStrStr (String
, "\r\n\r\n");
1573 if (EndofHeader
== NULL
) {
1578 // Each header field consists of a name followed by a colon (":") and the field value.
1579 // The field value MAY be preceded by any amount of LWS, though a single SP is preferred.
1581 // message-header = field-name ":" [ field-value ]
1582 // field-name = token
1583 // field-value = *( field-content | LWS )
1585 // Note: "*(element)" allows any number element, including zero; "1*(element)" requires at least one element.
1586 // [element] means element is optional.
1587 // LWS = [CRLF] 1*(SP|HT), it can be ' ' or '\t' or '\r\n ' or '\r\n\t'.
1592 FieldNameStr
= String
;
1593 FieldValueStr
= AsciiStrGetNextToken (FieldNameStr
, ':');
1594 if (FieldValueStr
== NULL
) {
1599 // Replace ':' with 0, then FieldName has been retrived from String.
1601 *(FieldValueStr
- 1) = 0;
1604 // Handle FieldValueStr, skip all the preceded LWS.
1607 if (*FieldValueStr
== ' ' || *FieldValueStr
== '\t') {
1609 // Boundary condition check.
1611 if ((UINTN
) EndofHeader
- (UINTN
) FieldValueStr
< 1) {
1613 // Wrong String format!
1619 } else if (*FieldValueStr
== '\r') {
1621 // Boundary condition check.
1623 if ((UINTN
) EndofHeader
- (UINTN
) FieldValueStr
< 3) {
1625 // No more preceded LWS, so break here.
1630 if (*(FieldValueStr
+ 1) == '\n' ) {
1631 if (*(FieldValueStr
+ 2) == ' ' || *(FieldValueStr
+ 2) == '\t') {
1632 FieldValueStr
= FieldValueStr
+ 3;
1635 // No more preceded LWS, so break here.
1641 // Wrong String format!
1647 // No more preceded LWS, so break here.
1653 StrPtr
= FieldValueStr
;
1656 // Handle the LWS within the field value.
1658 StrPtr
= AsciiStrGetNextToken (StrPtr
, '\r');
1659 if (StrPtr
== NULL
|| *StrPtr
!= '\n') {
1661 // Wrong String format!
1667 } while (*StrPtr
== ' ' || *StrPtr
== '\t');
1670 // Replace '\r' with 0
1675 // Get FieldName and FieldValue.
1677 *FieldName
= FieldNameStr
;
1678 *FieldValue
= FieldValueStr
;
1684 Free existing HeaderFields.
1686 @param[in] HeaderFields Pointer to array of key/value header pairs waitting for free.
1687 @param[in] FieldCount The number of header pairs in HeaderFields.
1692 HttpFreeHeaderFields (
1693 IN EFI_HTTP_HEADER
*HeaderFields
,
1699 if (HeaderFields
!= NULL
) {
1700 for (Index
= 0; Index
< FieldCount
; Index
++) {
1701 if (HeaderFields
[Index
].FieldName
!= NULL
) {
1702 FreePool (HeaderFields
[Index
].FieldName
);
1704 if (HeaderFields
[Index
].FieldValue
!= NULL
) {
1705 FreePool (HeaderFields
[Index
].FieldValue
);
1709 FreePool (HeaderFields
);
1714 Generate HTTP request message.
1716 This function will allocate memory for the whole HTTP message and generate a
1717 well formatted HTTP Request message in it, include the Request-Line, header
1718 fields and also the message body. It is the caller's responsibility to free
1719 the buffer returned in *RequestMsg.
1721 @param[in] Message Pointer to the EFI_HTTP_MESSAGE structure which
1722 contains the required information to generate
1723 the HTTP request message.
1724 @param[in] Url The URL of a remote host.
1725 @param[out] RequestMsg Pointer to the created HTTP request message.
1726 NULL if any error occured.
1727 @param[out] RequestMsgSize Size of the RequestMsg (in bytes).
1729 @retval EFI_SUCCESS If HTTP request string was created successfully.
1730 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
1731 @retval EFI_INVALID_PARAMETER The input arguments are invalid.
1736 HttpGenRequestMessage (
1737 IN CONST EFI_HTTP_MESSAGE
*Message
,
1738 IN CONST CHAR8
*Url
,
1739 OUT CHAR8
**RequestMsg
,
1740 OUT UINTN
*RequestMsgSize
1750 EFI_HTTP_HEADER
**AppendList
;
1752 EFI_HTTP_UTILITIES_PROTOCOL
*HttpUtilitiesProtocol
;
1754 Status
= EFI_SUCCESS
;
1760 HttpUtilitiesProtocol
= NULL
;
1763 // 1. If we have a Request, we cannot have a NULL Url
1764 // 2. If we have a Request, HeaderCount can not be non-zero
1765 // 3. If we do not have a Request, HeaderCount should be zero
1766 // 4. If we do not have Request and Headers, we need at least a message-body
1768 if ((Message
== NULL
|| RequestMsg
== NULL
|| RequestMsgSize
== NULL
) ||
1769 (Message
->Data
.Request
!= NULL
&& Url
== NULL
) ||
1770 (Message
->Data
.Request
!= NULL
&& Message
->HeaderCount
== 0) ||
1771 (Message
->Data
.Request
== NULL
&& Message
->HeaderCount
!= 0) ||
1772 (Message
->Data
.Request
== NULL
&& Message
->HeaderCount
== 0 && Message
->BodyLength
== 0)) {
1773 return EFI_INVALID_PARAMETER
;
1776 if (Message
->HeaderCount
!= 0) {
1778 // Locate the HTTP_UTILITIES protocol.
1780 Status
= gBS
->LocateProtocol (
1781 &gEfiHttpUtilitiesProtocolGuid
,
1783 (VOID
**) &HttpUtilitiesProtocol
1786 if (EFI_ERROR (Status
)) {
1787 DEBUG ((DEBUG_ERROR
,"Failed to locate Http Utilities protocol. Status = %r.\n", Status
));
1792 // Build AppendList to send into HttpUtilitiesBuild
1794 AppendList
= AllocateZeroPool (sizeof (EFI_HTTP_HEADER
*) * (Message
->HeaderCount
));
1795 if (AppendList
== NULL
) {
1796 return EFI_OUT_OF_RESOURCES
;
1799 for(Index
= 0; Index
< Message
->HeaderCount
; Index
++){
1800 AppendList
[Index
] = &Message
->Headers
[Index
];
1804 // Build raw HTTP Headers
1806 Status
= HttpUtilitiesProtocol
->Build (
1807 HttpUtilitiesProtocol
,
1812 Message
->HeaderCount
,
1818 FreePool (AppendList
);
1820 if (EFI_ERROR (Status
) || HttpHdr
== NULL
){
1826 // If we have headers to be sent, account for it.
1828 if (Message
->HeaderCount
!= 0) {
1829 MsgSize
= HttpHdrSize
;
1833 // If we have a request line, account for the fields.
1835 if (Message
->Data
.Request
!= NULL
) {
1836 MsgSize
+= HTTP_METHOD_MAXIMUM_LEN
+ AsciiStrLen (HTTP_VERSION_CRLF_STR
) + AsciiStrLen (Url
);
1841 // If we have a message body to be sent, account for it.
1843 MsgSize
+= Message
->BodyLength
;
1846 // memory for the string that needs to be sent to TCP
1849 *RequestMsg
= AllocateZeroPool (MsgSize
);
1850 if (*RequestMsg
== NULL
) {
1851 Status
= EFI_OUT_OF_RESOURCES
;
1855 RequestPtr
= *RequestMsg
;
1857 // Construct header request
1859 if (Message
->Data
.Request
!= NULL
) {
1860 switch (Message
->Data
.Request
->Method
) {
1862 StrLength
= sizeof (HTTP_METHOD_GET
) - 1;
1863 CopyMem (RequestPtr
, HTTP_METHOD_GET
, StrLength
);
1864 RequestPtr
+= StrLength
;
1867 StrLength
= sizeof (HTTP_METHOD_PUT
) - 1;
1868 CopyMem (RequestPtr
, HTTP_METHOD_PUT
, StrLength
);
1869 RequestPtr
+= StrLength
;
1871 case HttpMethodPatch
:
1872 StrLength
= sizeof (HTTP_METHOD_PATCH
) - 1;
1873 CopyMem (RequestPtr
, HTTP_METHOD_PATCH
, StrLength
);
1874 RequestPtr
+= StrLength
;
1876 case HttpMethodPost
:
1877 StrLength
= sizeof (HTTP_METHOD_POST
) - 1;
1878 CopyMem (RequestPtr
, HTTP_METHOD_POST
, StrLength
);
1879 RequestPtr
+= StrLength
;
1881 case HttpMethodHead
:
1882 StrLength
= sizeof (HTTP_METHOD_HEAD
) - 1;
1883 CopyMem (RequestPtr
, HTTP_METHOD_HEAD
, StrLength
);
1884 RequestPtr
+= StrLength
;
1886 case HttpMethodDelete
:
1887 StrLength
= sizeof (HTTP_METHOD_DELETE
) - 1;
1888 CopyMem (RequestPtr
, HTTP_METHOD_DELETE
, StrLength
);
1889 RequestPtr
+= StrLength
;
1893 Status
= EFI_INVALID_PARAMETER
;
1897 StrLength
= AsciiStrLen(EMPTY_SPACE
);
1898 CopyMem (RequestPtr
, EMPTY_SPACE
, StrLength
);
1899 RequestPtr
+= StrLength
;
1901 StrLength
= AsciiStrLen (Url
);
1902 CopyMem (RequestPtr
, Url
, StrLength
);
1903 RequestPtr
+= StrLength
;
1905 StrLength
= sizeof (HTTP_VERSION_CRLF_STR
) - 1;
1906 CopyMem (RequestPtr
, HTTP_VERSION_CRLF_STR
, StrLength
);
1907 RequestPtr
+= StrLength
;
1909 if (HttpHdr
!= NULL
) {
1913 CopyMem (RequestPtr
, HttpHdr
, HttpHdrSize
);
1914 RequestPtr
+= HttpHdrSize
;
1921 if (Message
->Body
!= NULL
) {
1922 CopyMem (RequestPtr
, Message
->Body
, Message
->BodyLength
);
1923 RequestPtr
+= Message
->BodyLength
;
1929 (*RequestMsgSize
) = (UINTN
)(RequestPtr
) - (UINTN
)(*RequestMsg
);
1935 if (*RequestMsg
!= NULL
) {
1936 FreePool (*RequestMsg
);
1942 if (HttpHdr
!= NULL
) {
1950 Translate the status code in HTTP message to EFI_HTTP_STATUS_CODE defined
1951 in UEFI 2.5 specification.
1953 @param[in] StatusCode The status code value in HTTP message.
1955 @return Value defined in EFI_HTTP_STATUS_CODE .
1958 EFI_HTTP_STATUS_CODE
1960 HttpMappingToStatusCode (
1964 switch (StatusCode
) {
1966 return HTTP_STATUS_100_CONTINUE
;
1968 return HTTP_STATUS_101_SWITCHING_PROTOCOLS
;
1970 return HTTP_STATUS_200_OK
;
1972 return HTTP_STATUS_201_CREATED
;
1974 return HTTP_STATUS_202_ACCEPTED
;
1976 return HTTP_STATUS_203_NON_AUTHORITATIVE_INFORMATION
;
1978 return HTTP_STATUS_204_NO_CONTENT
;
1980 return HTTP_STATUS_205_RESET_CONTENT
;
1982 return HTTP_STATUS_206_PARTIAL_CONTENT
;
1984 return HTTP_STATUS_300_MULTIPLE_CHOICES
;
1986 return HTTP_STATUS_301_MOVED_PERMANENTLY
;
1988 return HTTP_STATUS_302_FOUND
;
1990 return HTTP_STATUS_303_SEE_OTHER
;
1992 return HTTP_STATUS_304_NOT_MODIFIED
;
1994 return HTTP_STATUS_305_USE_PROXY
;
1996 return HTTP_STATUS_307_TEMPORARY_REDIRECT
;
1998 return HTTP_STATUS_308_PERMANENT_REDIRECT
;
2000 return HTTP_STATUS_400_BAD_REQUEST
;
2002 return HTTP_STATUS_401_UNAUTHORIZED
;
2004 return HTTP_STATUS_402_PAYMENT_REQUIRED
;
2006 return HTTP_STATUS_403_FORBIDDEN
;
2008 return HTTP_STATUS_404_NOT_FOUND
;
2010 return HTTP_STATUS_405_METHOD_NOT_ALLOWED
;
2012 return HTTP_STATUS_406_NOT_ACCEPTABLE
;
2014 return HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED
;
2016 return HTTP_STATUS_408_REQUEST_TIME_OUT
;
2018 return HTTP_STATUS_409_CONFLICT
;
2020 return HTTP_STATUS_410_GONE
;
2022 return HTTP_STATUS_411_LENGTH_REQUIRED
;
2024 return HTTP_STATUS_412_PRECONDITION_FAILED
;
2026 return HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE
;
2028 return HTTP_STATUS_414_REQUEST_URI_TOO_LARGE
;
2030 return HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE
;
2032 return HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED
;
2034 return HTTP_STATUS_417_EXPECTATION_FAILED
;
2036 return HTTP_STATUS_500_INTERNAL_SERVER_ERROR
;
2038 return HTTP_STATUS_501_NOT_IMPLEMENTED
;
2040 return HTTP_STATUS_502_BAD_GATEWAY
;
2042 return HTTP_STATUS_503_SERVICE_UNAVAILABLE
;
2044 return HTTP_STATUS_504_GATEWAY_TIME_OUT
;
2046 return HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED
;
2049 return HTTP_STATUS_UNSUPPORTED_STATUS
;
2054 Check whether header field called FieldName is in DeleteList.
2056 @param[in] DeleteList Pointer to array of key/value header pairs.
2057 @param[in] DeleteCount The number of header pairs.
2058 @param[in] FieldName Pointer to header field's name.
2060 @return TRUE if FieldName is not in DeleteList, that means this header field is valid.
2061 @return FALSE if FieldName is in DeleteList, that means this header field is invalid.
2066 HttpIsValidHttpHeader (
2067 IN CHAR8
*DeleteList
[],
2068 IN UINTN DeleteCount
,
2074 if (FieldName
== NULL
) {
2078 for (Index
= 0; Index
< DeleteCount
; Index
++) {
2079 if (DeleteList
[Index
] == NULL
) {
2083 if (AsciiStrCmp (FieldName
, DeleteList
[Index
]) == 0) {