2 This library is used to share code between UEFI network stack modules.
3 It provides the helper routines to parse the HTTP message byte stream.
5 Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
6 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "DxeHttpLib.h"
16 Decode a percent-encoded URI component to the ASCII character.
18 Decode the input component in Buffer according to RFC 3986. The caller is responsible to make
19 sure ResultBuffer points to a buffer with size equal or greater than ((AsciiStrSize (Buffer))
22 @param[in] Buffer The pointer to a percent-encoded URI component.
23 @param[in] BufferLength Length of Buffer in bytes.
24 @param[out] ResultBuffer Point to the buffer to store the decode result.
25 @param[out] ResultLength Length of decoded string in ResultBuffer in bytes.
27 @retval EFI_SUCCESS Successfully decoded the URI.
28 @retval EFI_INVALID_PARAMETER Buffer is not a valid percent-encoded string.
35 IN UINT32 BufferLength
,
36 OUT CHAR8
*ResultBuffer
,
37 OUT UINT32
*ResultLength
44 if (Buffer
== NULL
|| BufferLength
== 0 || ResultBuffer
== NULL
) {
45 return EFI_INVALID_PARAMETER
;
51 while (Index
< BufferLength
) {
52 if (Buffer
[Index
] == '%') {
53 if (Index
+ 1 >= BufferLength
|| Index
+ 2 >= BufferLength
||
54 !NET_IS_HEX_CHAR (Buffer
[Index
+1]) || !NET_IS_HEX_CHAR (Buffer
[Index
+2])) {
55 return EFI_INVALID_PARAMETER
;
57 HexStr
[0] = Buffer
[Index
+1];
58 HexStr
[1] = Buffer
[Index
+2];
59 ResultBuffer
[Offset
] = (CHAR8
) AsciiStrHexToUintn (HexStr
);
62 ResultBuffer
[Offset
] = Buffer
[Index
];
68 *ResultLength
= (UINT32
) Offset
;
74 This function return the updated state according to the input state and next character of
77 @param[in] Char Next character.
78 @param[in] State Current value of the parser state machine.
79 @param[in] IsRightBracket TRUE if there is an sign ']' in the authority component and
80 indicates the next part is ':' before Port.
82 @return Updated state value.
85 NetHttpParseAuthorityChar (
87 IN HTTP_URL_PARSE_STATE State
,
88 IN BOOLEAN
*IsRightBracket
94 // The authority component is preceded by a double slash ("//") and is
95 // terminated by the next slash ("/"), question mark ("?"), or number
96 // sign ("#") character, or by the end of the URI.
98 if (Char
== ' ' || Char
== '\r' || Char
== '\n') {
99 return UrlParserStateMax
;
103 // authority = [ userinfo "@" ] host [ ":" port ]
106 case UrlParserUserInfo
:
108 return UrlParserHostStart
;
113 case UrlParserHostStart
:
115 return UrlParserHostIpv6
;
119 return UrlParserPortStart
;
122 return UrlParserHost
;
124 case UrlParserHostIpv6
:
126 *IsRightBracket
= TRUE
;
129 if (Char
== ':' && *IsRightBracket
) {
130 return UrlParserPortStart
;
132 return UrlParserHostIpv6
;
135 case UrlParserPortStart
:
136 return UrlParserPort
;
146 This function parse the authority component of the input URL and update the parser.
148 @param[in] Url The pointer to a HTTP URL string.
149 @param[in] FoundAt TRUE if there is an at sign ('@') in the authority component.
150 @param[in, out] UrlParser Pointer to the buffer of the parse result.
152 @retval EFI_SUCCESS Successfully parse the authority.
153 @retval EFI_INVALID_PARAMETER The Url is invalid to parse the authority component.
157 NetHttpParseAuthority (
160 IN OUT HTTP_URL_PARSER
*UrlParser
166 HTTP_URL_PARSE_STATE State
;
169 BOOLEAN IsrightBracket
;
171 ASSERT ((UrlParser
->FieldBitMap
& BIT (HTTP_URI_FIELD_AUTHORITY
)) != 0);
174 // authority = [ userinfo "@" ] host [ ":" port ]
177 State
= UrlParserUserInfo
;
179 State
= UrlParserHost
;
182 IsrightBracket
= FALSE
;
183 Field
= HTTP_URI_FIELD_MAX
;
185 Authority
= Url
+ UrlParser
->FieldData
[HTTP_URI_FIELD_AUTHORITY
].Offset
;
186 Length
= UrlParser
->FieldData
[HTTP_URI_FIELD_AUTHORITY
].Length
;
187 for (Char
= Authority
; Char
< Authority
+ Length
; Char
++) {
188 State
= NetHttpParseAuthorityChar (*Char
, State
, &IsrightBracket
);
190 case UrlParserStateMax
:
191 return EFI_INVALID_PARAMETER
;
193 case UrlParserHostStart
:
194 case UrlParserPortStart
:
197 case UrlParserUserInfo
:
198 Field
= HTTP_URI_FIELD_USERINFO
;
202 Field
= HTTP_URI_FIELD_HOST
;
205 case UrlParserHostIpv6
:
206 Field
= HTTP_URI_FIELD_HOST
;
210 Field
= HTTP_URI_FIELD_PORT
;
218 // Field not changed, count the length.
220 ASSERT (Field
< HTTP_URI_FIELD_MAX
);
221 if (Field
== OldField
) {
222 UrlParser
->FieldData
[Field
].Length
++;
229 UrlParser
->FieldBitMap
|= BIT (Field
);
230 UrlParser
->FieldData
[Field
].Offset
= (UINT32
) (Char
- Url
);
231 UrlParser
->FieldData
[Field
].Length
= 1;
239 This function return the updated state according to the input state and next character of a URL.
241 @param[in] Char Next character.
242 @param[in] State Current value of the parser state machine.
244 @return Updated state value.
248 NetHttpParseUrlChar (
250 IN HTTP_URL_PARSE_STATE State
253 if (Char
== ' ' || Char
== '\r' || Char
== '\n') {
254 return UrlParserStateMax
;
258 // http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]
260 // Request-URI = "*" | absolute-URI | path-absolute | authority
262 // absolute-URI = scheme ":" hier-part [ "?" query ]
263 // path-absolute = "/" [ segment-nz *( "/" segment ) ]
264 // authority = [ userinfo "@" ] host [ ":" port ]
267 case UrlParserUrlStart
:
268 if (Char
== '*' || Char
== '/') {
269 return UrlParserPath
;
271 return UrlParserScheme
;
273 case UrlParserScheme
:
275 return UrlParserSchemeColon
;
279 case UrlParserSchemeColon
:
281 return UrlParserSchemeColonSlash
;
285 case UrlParserSchemeColonSlash
:
287 return UrlParserSchemeColonSlashSlash
;
291 case UrlParserAtInAuthority
:
293 return UrlParserStateMax
;
296 case UrlParserAuthority
:
297 case UrlParserSchemeColonSlashSlash
:
299 return UrlParserAtInAuthority
;
302 return UrlParserPath
;
305 return UrlParserQueryStart
;
308 return UrlParserFragmentStart
;
310 return UrlParserAuthority
;
314 return UrlParserQueryStart
;
317 return UrlParserFragmentStart
;
322 case UrlParserQueryStart
:
324 return UrlParserFragmentStart
;
326 return UrlParserQuery
;
328 case UrlParserFragmentStart
:
329 return UrlParserFragment
;
338 Create a URL parser for the input URL string.
340 This function will parse and dereference the input HTTP URL into it components. The original
341 content of the URL won't be modified and the result will be returned in UrlParser, which can
342 be used in other functions like NetHttpUrlGetHostName().
344 @param[in] Url The pointer to a HTTP URL string.
345 @param[in] Length Length of Url in bytes.
346 @param[in] IsConnectMethod Whether the Url is used in HTTP CONNECT method or not.
347 @param[out] UrlParser Pointer to the returned buffer to store the parse result.
349 @retval EFI_SUCCESS Successfully dereferenced the HTTP URL.
350 @retval EFI_INVALID_PARAMETER UrlParser is NULL or Url is not a valid HTTP URL.
351 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
359 IN BOOLEAN IsConnectMethod
,
363 HTTP_URL_PARSE_STATE State
;
369 HTTP_URL_PARSER
*Parser
;
373 if (Url
== NULL
|| Length
== 0 || UrlParser
== NULL
) {
374 return EFI_INVALID_PARAMETER
;
377 Parser
= AllocateZeroPool (sizeof (HTTP_URL_PARSER
));
378 if (Parser
== NULL
) {
379 return EFI_OUT_OF_RESOURCES
;
382 if (IsConnectMethod
) {
384 // According to RFC 2616, the authority form is only used by the CONNECT method.
386 State
= UrlParserAuthority
;
388 State
= UrlParserUrlStart
;
391 Field
= HTTP_URI_FIELD_MAX
;
394 for (Char
= Url
; Char
< Url
+ Length
; Char
++) {
396 // Update state machine according to next char.
398 State
= NetHttpParseUrlChar (*Char
, State
);
401 case UrlParserStateMax
:
403 return EFI_INVALID_PARAMETER
;
405 case UrlParserSchemeColon
:
406 case UrlParserSchemeColonSlash
:
407 case UrlParserSchemeColonSlashSlash
:
408 case UrlParserQueryStart
:
409 case UrlParserFragmentStart
:
411 // Skip all the delimiting char: "://" "?" "@"
415 case UrlParserScheme
:
416 Field
= HTTP_URI_FIELD_SCHEME
;
419 case UrlParserAtInAuthority
:
421 case UrlParserAuthority
:
422 Field
= HTTP_URI_FIELD_AUTHORITY
;
426 Field
= HTTP_URI_FIELD_PATH
;
430 Field
= HTTP_URI_FIELD_QUERY
;
433 case UrlParserFragment
:
434 Field
= HTTP_URI_FIELD_FRAGMENT
;
442 // Field not changed, count the length.
444 ASSERT (Field
< HTTP_URI_FIELD_MAX
);
445 if (Field
== OldField
) {
446 Parser
->FieldData
[Field
].Length
++;
453 Parser
->FieldBitMap
|= BIT (Field
);
454 Parser
->FieldData
[Field
].Offset
= (UINT32
) (Char
- Url
);
455 Parser
->FieldData
[Field
].Length
= 1;
460 // If has authority component, continue to parse the username, host and port.
462 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_AUTHORITY
)) != 0) {
463 Status
= NetHttpParseAuthority (Url
, FoundAt
, Parser
);
464 if (EFI_ERROR (Status
)) {
475 Get the Hostname from a HTTP URL.
477 This function will return the HostName according to the Url and previous parse result ,and
478 it is the caller's responsibility to free the buffer returned in *HostName.
480 @param[in] Url The pointer to a HTTP URL string.
481 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
482 @param[out] HostName Pointer to a buffer to store the HostName.
484 @retval EFI_SUCCESS Successfully get the required component.
485 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.
486 @retval EFI_NOT_FOUND No hostName component in the URL.
487 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
501 HTTP_URL_PARSER
*Parser
;
503 if (Url
== NULL
|| UrlParser
== NULL
|| HostName
== NULL
) {
504 return EFI_INVALID_PARAMETER
;
507 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
509 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_HOST
)) == 0) {
510 return EFI_NOT_FOUND
;
513 Name
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
+ 1);
515 return EFI_OUT_OF_RESOURCES
;
518 Status
= UriPercentDecode (
519 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Offset
,
520 Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
,
524 if (EFI_ERROR (Status
)) {
529 Name
[ResultLength
] = '\0';
536 Get the IPv4 address from a HTTP URL.
538 This function will return the IPv4 address according to the Url and previous parse result.
540 @param[in] Url The pointer to a HTTP URL string.
541 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
542 @param[out] Ip4Address Pointer to a buffer to store the IP address.
544 @retval EFI_SUCCESS Successfully get the required component.
545 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip4Address is NULL or UrlParser is invalid.
546 @retval EFI_NOT_FOUND No IPv4 address component in the URL.
547 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
555 OUT EFI_IPv4_ADDRESS
*Ip4Address
561 HTTP_URL_PARSER
*Parser
;
563 if (Url
== NULL
|| UrlParser
== NULL
|| Ip4Address
== NULL
) {
564 return EFI_INVALID_PARAMETER
;
567 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
569 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_HOST
)) == 0) {
570 return EFI_NOT_FOUND
;
573 Ip4String
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
+ 1);
574 if (Ip4String
== NULL
) {
575 return EFI_OUT_OF_RESOURCES
;
578 Status
= UriPercentDecode (
579 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Offset
,
580 Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
,
584 if (EFI_ERROR (Status
)) {
585 FreePool (Ip4String
);
589 Ip4String
[ResultLength
] = '\0';
590 Status
= NetLibAsciiStrToIp4 (Ip4String
, Ip4Address
);
591 FreePool (Ip4String
);
597 Get the IPv6 address from a HTTP URL.
599 This function will return the IPv6 address according to the Url and previous parse result.
601 @param[in] Url The pointer to a HTTP URL string.
602 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
603 @param[out] Ip6Address Pointer to a buffer to store the IP address.
605 @retval EFI_SUCCESS Successfully get the required component.
606 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip6Address is NULL or UrlParser is invalid.
607 @retval EFI_NOT_FOUND No IPv6 address component in the URL.
608 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
616 OUT EFI_IPv6_ADDRESS
*Ip6Address
624 HTTP_URL_PARSER
*Parser
;
626 if (Url
== NULL
|| UrlParser
== NULL
|| Ip6Address
== NULL
) {
627 return EFI_INVALID_PARAMETER
;
630 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
632 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_HOST
)) == 0) {
633 return EFI_NOT_FOUND
;
637 // IP-literal = "[" ( IPv6address / IPvFuture ) "]"
639 Length
= Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
;
641 return EFI_INVALID_PARAMETER
;
644 Ptr
= Url
+ Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Offset
;
645 if ((Ptr
[0] != '[') || (Ptr
[Length
- 1] != ']')) {
646 return EFI_INVALID_PARAMETER
;
649 Ip6String
= AllocatePool (Length
);
650 if (Ip6String
== NULL
) {
651 return EFI_OUT_OF_RESOURCES
;
654 Status
= UriPercentDecode (
660 if (EFI_ERROR (Status
)) {
661 FreePool (Ip6String
);
665 Ip6String
[ResultLength
] = '\0';
666 Status
= NetLibAsciiStrToIp6 (Ip6String
, Ip6Address
);
667 FreePool (Ip6String
);
673 Get the port number from a HTTP URL.
675 This function will return the port number according to the Url and previous parse result.
677 @param[in] Url The pointer to a HTTP URL string.
678 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
679 @param[out] Port Pointer to a buffer to store the port number.
681 @retval EFI_SUCCESS Successfully get the required component.
682 @retval EFI_INVALID_PARAMETER Uri is NULL or Port is NULL or UrlParser is invalid.
683 @retval EFI_NOT_FOUND No port number in the URL.
684 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
700 HTTP_URL_PARSER
*Parser
;
702 if (Url
== NULL
|| UrlParser
== NULL
|| Port
== NULL
) {
703 return EFI_INVALID_PARAMETER
;
709 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
711 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_PORT
)) == 0) {
712 return EFI_NOT_FOUND
;
715 PortString
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Length
+ 1);
716 if (PortString
== NULL
) {
717 return EFI_OUT_OF_RESOURCES
;
720 Status
= UriPercentDecode (
721 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Offset
,
722 Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Length
,
726 if (EFI_ERROR (Status
)) {
730 PortString
[ResultLength
] = '\0';
732 while (Index
< ResultLength
) {
733 if (!NET_IS_DIGIT (PortString
[Index
])) {
734 Status
= EFI_INVALID_PARAMETER
;
740 Status
= AsciiStrDecimalToUintnS (Url
+ Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Offset
, (CHAR8
**) NULL
, &Data
);
742 if (Data
> HTTP_URI_PORT_MAX_NUM
) {
743 Status
= EFI_INVALID_PARAMETER
;
747 *Port
= (UINT16
) Data
;
750 FreePool (PortString
);
755 Get the Path from a HTTP URL.
757 This function will return the Path according to the Url and previous parse result,and
758 it is the caller's responsibility to free the buffer returned in *Path.
760 @param[in] Url The pointer to a HTTP URL string.
761 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
762 @param[out] Path Pointer to a buffer to store the Path.
764 @retval EFI_SUCCESS Successfully get the required component.
765 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.
766 @retval EFI_NOT_FOUND No hostName component in the URL.
767 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
781 HTTP_URL_PARSER
*Parser
;
783 if (Url
== NULL
|| UrlParser
== NULL
|| Path
== NULL
) {
784 return EFI_INVALID_PARAMETER
;
787 Parser
= (HTTP_URL_PARSER
*) UrlParser
;
789 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_PATH
)) == 0) {
790 return EFI_NOT_FOUND
;
793 PathStr
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_PATH
].Length
+ 1);
794 if (PathStr
== NULL
) {
795 return EFI_OUT_OF_RESOURCES
;
798 Status
= UriPercentDecode (
799 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_PATH
].Offset
,
800 Parser
->FieldData
[HTTP_URI_FIELD_PATH
].Length
,
804 if (EFI_ERROR (Status
)) {
809 PathStr
[ResultLength
] = '\0';
815 Release the resource of the URL parser.
817 @param[in] UrlParser Pointer to the parser.
826 FreePool (UrlParser
);
830 Find a specified header field according to the field name.
832 @param[in] HeaderCount Number of HTTP header structures in Headers list.
833 @param[in] Headers Array containing list of HTTP headers.
834 @param[in] FieldName Null terminated string which describes a field name.
836 @return Pointer to the found header or NULL.
842 IN UINTN HeaderCount
,
843 IN EFI_HTTP_HEADER
*Headers
,
849 if (HeaderCount
== 0 || Headers
== NULL
|| FieldName
== NULL
) {
853 for (Index
= 0; Index
< HeaderCount
; Index
++){
855 // Field names are case-insensitive (RFC 2616).
857 if (AsciiStriCmp (Headers
[Index
].FieldName
, FieldName
) == 0) {
858 return &Headers
[Index
];
866 BodyParserBodyIdentity
,
867 BodyParserChunkSizeStart
,
869 BodyParserChunkSizeEndCR
,
870 BodyParserChunkExtStart
,
871 BodyParserChunkDataStart
,
872 BodyParserChunkDataEnd
,
873 BodyParserChunkDataEndCR
,
876 BodyParserLastCRLFEnd
,
879 } HTTP_BODY_PARSE_STATE
;
882 BOOLEAN IgnoreBody
; // "MUST NOT" include a message-body
883 BOOLEAN IsChunked
; // "chunked" transfer-coding.
884 BOOLEAN ContentLengthIsValid
;
885 UINTN ContentLength
; // Entity length (not the message-body length), invalid until ContentLengthIsValid is TRUE
887 HTTP_BODY_PARSER_CALLBACK Callback
;
889 UINTN ParsedBodyLength
;
890 HTTP_BODY_PARSE_STATE State
;
891 UINTN CurrentChunkSize
;
892 UINTN CurrentChunkParsedSize
;
896 Convert an hexadecimal char to a value of type UINTN.
898 @param[in] Char Ascii character.
900 @return Value translated from Char.
904 HttpIoHexCharToUintn (
908 if (Char
>= '0' && Char
<= '9') {
912 return (10 + AsciiCharToUpper (Char
) - 'A');
916 Get the value of the content length if there is a "Content-Length" header.
918 @param[in] HeaderCount Number of HTTP header structures in Headers.
919 @param[in] Headers Array containing list of HTTP headers.
920 @param[out] ContentLength Pointer to save the value of the content length.
922 @retval EFI_SUCCESS Successfully get the content length.
923 @retval EFI_NOT_FOUND No "Content-Length" header in the Headers.
927 HttpIoParseContentLengthHeader (
928 IN UINTN HeaderCount
,
929 IN EFI_HTTP_HEADER
*Headers
,
930 OUT UINTN
*ContentLength
933 EFI_HTTP_HEADER
*Header
;
935 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_CONTENT_LENGTH
);
936 if (Header
== NULL
) {
937 return EFI_NOT_FOUND
;
940 return AsciiStrDecimalToUintnS (Header
->FieldValue
, (CHAR8
**) NULL
, ContentLength
);
945 Check whether the HTTP message is using the "chunked" transfer-coding.
947 @param[in] HeaderCount Number of HTTP header structures in Headers.
948 @param[in] Headers Array containing list of HTTP headers.
950 @return The message is "chunked" transfer-coding (TRUE) or not (FALSE).
955 IN UINTN HeaderCount
,
956 IN EFI_HTTP_HEADER
*Headers
959 EFI_HTTP_HEADER
*Header
;
962 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_TRANSFER_ENCODING
);
963 if (Header
== NULL
) {
967 if (AsciiStriCmp (Header
->FieldValue
, "identity") != 0) {
975 Check whether the HTTP message should have a message-body.
977 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.
978 @param[in] StatusCode Response status code returned by the remote host.
980 @return The message should have a message-body (FALSE) or not (TRUE).
984 HttpIoNoMessageBody (
985 IN EFI_HTTP_METHOD Method
,
986 IN EFI_HTTP_STATUS_CODE StatusCode
991 // All responses to the HEAD request method
992 // MUST NOT include a message-body, even though the presence of entity-
993 // header fields might lead one to believe they do. All 1xx
994 // (informational), 204 (no content), and 304 (not modified) responses
995 // MUST NOT include a message-body. All other responses do include a
996 // message-body, although it MAY be of zero length.
998 if (Method
== HttpMethodHead
) {
1002 if ((StatusCode
== HTTP_STATUS_100_CONTINUE
) ||
1003 (StatusCode
== HTTP_STATUS_101_SWITCHING_PROTOCOLS
) ||
1004 (StatusCode
== HTTP_STATUS_204_NO_CONTENT
) ||
1005 (StatusCode
== HTTP_STATUS_304_NOT_MODIFIED
))
1014 Initialize a HTTP message-body parser.
1016 This function will create and initialize a HTTP message parser according to caller provided HTTP message
1017 header information. It is the caller's responsibility to free the buffer returned in *UrlParser by HttpFreeMsgParser().
1019 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.
1020 @param[in] StatusCode Response status code returned by the remote host.
1021 @param[in] HeaderCount Number of HTTP header structures in Headers.
1022 @param[in] Headers Array containing list of HTTP headers.
1023 @param[in] Callback Callback function that is invoked when parsing the HTTP message-body,
1024 set to NULL to ignore all events.
1025 @param[in] Context Pointer to the context that will be passed to Callback.
1026 @param[out] MsgParser Pointer to the returned buffer to store the message parser.
1028 @retval EFI_SUCCESS Successfully initialized the parser.
1029 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
1030 @retval EFI_INVALID_PARAMETER MsgParser is NULL or HeaderCount is not NULL but Headers is NULL.
1031 @retval Others Failed to initialize the parser.
1037 IN EFI_HTTP_METHOD Method
,
1038 IN EFI_HTTP_STATUS_CODE StatusCode
,
1039 IN UINTN HeaderCount
,
1040 IN EFI_HTTP_HEADER
*Headers
,
1041 IN HTTP_BODY_PARSER_CALLBACK Callback
,
1043 OUT VOID
**MsgParser
1047 HTTP_BODY_PARSER
*Parser
;
1049 if (HeaderCount
!= 0 && Headers
== NULL
) {
1050 return EFI_INVALID_PARAMETER
;
1053 if (MsgParser
== NULL
) {
1054 return EFI_INVALID_PARAMETER
;
1057 Parser
= AllocateZeroPool (sizeof (HTTP_BODY_PARSER
));
1058 if (Parser
== NULL
) {
1059 return EFI_OUT_OF_RESOURCES
;
1062 Parser
->State
= BodyParserBodyStart
;
1065 // Determine the message length according to RFC 2616.
1066 // 1. Check whether the message "MUST NOT" have a message-body.
1068 Parser
->IgnoreBody
= HttpIoNoMessageBody (Method
, StatusCode
);
1070 // 2. Check whether the message using "chunked" transfer-coding.
1072 Parser
->IsChunked
= HttpIoIsChunked (HeaderCount
, Headers
);
1074 // 3. Check whether the message has a Content-Length header field.
1076 Status
= HttpIoParseContentLengthHeader (HeaderCount
, Headers
, &Parser
->ContentLength
);
1077 if (!EFI_ERROR (Status
)) {
1078 Parser
->ContentLengthIsValid
= TRUE
;
1081 // 4. Range header is not supported now, so we won't meet media type "multipart/byteranges".
1082 // 5. By server closing the connection
1086 // Set state to skip body parser if the message shouldn't have a message body.
1088 if (Parser
->IgnoreBody
) {
1089 Parser
->State
= BodyParserComplete
;
1091 Parser
->Callback
= Callback
;
1092 Parser
->Context
= Context
;
1095 *MsgParser
= Parser
;
1102 Parse BodyLength of message-body. This function can be called repeatedly to parse the message-body partially.
1104 @param[in, out] MsgParser Pointer to the message parser.
1105 @param[in] BodyLength Length in bytes of the Body.
1106 @param[in] Body Pointer to the buffer of the message-body to be parsed.
1108 @retval EFI_SUCCESS Successfully parse the message-body.
1109 @retval EFI_INVALID_PARAMETER MsgParser is NULL or Body is NULL or BodyLength is 0.
1110 @retval EFI_ABORTED Operation aborted.
1111 @retval Other Error happened while parsing message body.
1116 HttpParseMessageBody (
1117 IN OUT VOID
*MsgParser
,
1118 IN UINTN BodyLength
,
1123 UINTN RemainderLengthInThis
;
1124 UINTN LengthForCallback
;
1125 UINTN PortionLength
;
1127 HTTP_BODY_PARSER
*Parser
;
1129 if (BodyLength
== 0 || Body
== NULL
) {
1130 return EFI_INVALID_PARAMETER
;
1133 if (MsgParser
== NULL
) {
1134 return EFI_INVALID_PARAMETER
;
1137 Parser
= (HTTP_BODY_PARSER
*) MsgParser
;
1139 if (Parser
->IgnoreBody
) {
1140 Parser
->State
= BodyParserComplete
;
1141 if (Parser
->Callback
!= NULL
) {
1142 Status
= Parser
->Callback (
1143 BodyParseEventOnComplete
,
1148 if (EFI_ERROR (Status
)) {
1155 if (Parser
->State
== BodyParserBodyStart
) {
1156 Parser
->ParsedBodyLength
= 0;
1157 if (Parser
->IsChunked
) {
1158 Parser
->State
= BodyParserChunkSizeStart
;
1160 Parser
->State
= BodyParserBodyIdentity
;
1165 // The message body might be truncated in anywhere, so we need to parse is byte-by-byte.
1167 for (Char
= Body
; Char
< Body
+ BodyLength
; ) {
1169 switch (Parser
->State
) {
1170 case BodyParserStateMax
:
1173 case BodyParserBodyIdentity
:
1175 // Identity transfer-coding, just notify user to save the body data.
1177 PortionLength
= MIN (
1179 Parser
->ContentLength
- Parser
->ParsedBodyLength
1181 if (PortionLength
== 0) {
1183 // Got BodyLength, but no ContentLength. Use BodyLength.
1185 PortionLength
= BodyLength
;
1186 Parser
->ContentLength
= PortionLength
;
1189 if (Parser
->Callback
!= NULL
) {
1190 Status
= Parser
->Callback (
1191 BodyParseEventOnData
,
1196 if (EFI_ERROR (Status
)) {
1200 Char
+= PortionLength
;
1201 Parser
->ParsedBodyLength
+= PortionLength
;
1202 if (Parser
->ParsedBodyLength
== Parser
->ContentLength
) {
1203 Parser
->State
= BodyParserComplete
;
1204 if (Parser
->Callback
!= NULL
) {
1205 Status
= Parser
->Callback (
1206 BodyParseEventOnComplete
,
1211 if (EFI_ERROR (Status
)) {
1218 case BodyParserChunkSizeStart
:
1220 // First byte of chunk-size, the chunk-size might be truncated.
1222 Parser
->CurrentChunkSize
= 0;
1223 Parser
->State
= BodyParserChunkSize
;
1224 case BodyParserChunkSize
:
1225 if (!NET_IS_HEX_CHAR (*Char
)) {
1227 Parser
->State
= BodyParserChunkExtStart
;
1229 } else if (*Char
== '\r') {
1230 Parser
->State
= BodyParserChunkSizeEndCR
;
1233 Parser
->State
= BodyParserStateMax
;
1238 if (Parser
->CurrentChunkSize
> (((~((UINTN
) 0)) - 16) / 16)) {
1239 return EFI_INVALID_PARAMETER
;
1241 Parser
->CurrentChunkSize
= Parser
->CurrentChunkSize
* 16 + HttpIoHexCharToUintn (*Char
);
1245 case BodyParserChunkExtStart
:
1247 // Ignore all the chunk extensions.
1249 if (*Char
== '\r') {
1250 Parser
->State
= BodyParserChunkSizeEndCR
;
1255 case BodyParserChunkSizeEndCR
:
1256 if (*Char
!= '\n') {
1257 Parser
->State
= BodyParserStateMax
;
1261 if (Parser
->CurrentChunkSize
== 0) {
1263 // The last chunk has been parsed and now assumed the state
1264 // of HttpBodyParse is ParserLastCRLF. So it need to decide
1265 // whether the rest message is trailer or last CRLF in the next round.
1267 Parser
->ContentLengthIsValid
= TRUE
;
1268 Parser
->State
= BodyParserLastCRLF
;
1271 Parser
->State
= BodyParserChunkDataStart
;
1272 Parser
->CurrentChunkParsedSize
= 0;
1275 case BodyParserLastCRLF
:
1277 // Judge the byte is belong to the Last CRLF or trailer, and then
1278 // configure the state of HttpBodyParse to corresponding state.
1280 if (*Char
== '\r') {
1282 Parser
->State
= BodyParserLastCRLFEnd
;
1285 Parser
->State
= BodyParserTrailer
;
1289 case BodyParserLastCRLFEnd
:
1290 if (*Char
== '\n') {
1291 Parser
->State
= BodyParserComplete
;
1293 if (Parser
->Callback
!= NULL
) {
1294 Status
= Parser
->Callback (
1295 BodyParseEventOnComplete
,
1300 if (EFI_ERROR (Status
)) {
1306 Parser
->State
= BodyParserStateMax
;
1310 case BodyParserTrailer
:
1311 if (*Char
== '\r') {
1312 Parser
->State
= BodyParserChunkSizeEndCR
;
1317 case BodyParserChunkDataStart
:
1319 // First byte of chunk-data, the chunk data also might be truncated.
1321 RemainderLengthInThis
= BodyLength
- (Char
- Body
);
1322 LengthForCallback
= MIN (Parser
->CurrentChunkSize
- Parser
->CurrentChunkParsedSize
, RemainderLengthInThis
);
1323 if (Parser
->Callback
!= NULL
) {
1324 Status
= Parser
->Callback (
1325 BodyParseEventOnData
,
1330 if (EFI_ERROR (Status
)) {
1334 Char
+= LengthForCallback
;
1335 Parser
->ContentLength
+= LengthForCallback
;
1336 Parser
->CurrentChunkParsedSize
+= LengthForCallback
;
1337 if (Parser
->CurrentChunkParsedSize
== Parser
->CurrentChunkSize
) {
1338 Parser
->State
= BodyParserChunkDataEnd
;
1342 case BodyParserChunkDataEnd
:
1343 if (*Char
== '\r') {
1344 Parser
->State
= BodyParserChunkDataEndCR
;
1346 Parser
->State
= BodyParserStateMax
;
1351 case BodyParserChunkDataEndCR
:
1352 if (*Char
!= '\n') {
1353 Parser
->State
= BodyParserStateMax
;
1357 Parser
->State
= BodyParserChunkSizeStart
;
1366 if (Parser
->State
== BodyParserStateMax
) {
1374 Check whether the message-body is complete or not.
1376 @param[in] MsgParser Pointer to the message parser.
1378 @retval TRUE Message-body is complete.
1379 @retval FALSE Message-body is not complete.
1384 HttpIsMessageComplete (
1388 HTTP_BODY_PARSER
*Parser
;
1390 if (MsgParser
== NULL
) {
1394 Parser
= (HTTP_BODY_PARSER
*) MsgParser
;
1396 if (Parser
->State
== BodyParserComplete
) {
1403 Get the content length of the entity.
1405 Note that in trunk transfer, the entity length is not valid until the whole message body is received.
1407 @param[in] MsgParser Pointer to the message parser.
1408 @param[out] ContentLength Pointer to store the length of the entity.
1410 @retval EFI_SUCCESS Successfully to get the entity length.
1411 @retval EFI_NOT_READY Entity length is not valid yet.
1412 @retval EFI_INVALID_PARAMETER MsgParser is NULL or ContentLength is NULL.
1417 HttpGetEntityLength (
1419 OUT UINTN
*ContentLength
1422 HTTP_BODY_PARSER
*Parser
;
1424 if (MsgParser
== NULL
|| ContentLength
== NULL
) {
1425 return EFI_INVALID_PARAMETER
;
1428 Parser
= (HTTP_BODY_PARSER
*) MsgParser
;
1430 if (!Parser
->ContentLengthIsValid
) {
1431 return EFI_NOT_READY
;
1434 *ContentLength
= Parser
->ContentLength
;
1439 Release the resource of the message parser.
1441 @param[in] MsgParser Pointer to the message parser.
1450 FreePool (MsgParser
);
1455 Get the next string, which is distinguished by specified separator.
1457 @param[in] String Pointer to the string.
1458 @param[in] Separator Specified separator used to distinguish where is the beginning
1461 @return Pointer to the next string.
1462 @return NULL if not find or String is NULL.
1466 AsciiStrGetNextToken (
1467 IN CONST CHAR8
*String
,
1478 if (*Token
== Separator
) {
1479 return (CHAR8
*)(Token
+ 1);
1486 Set FieldName and FieldValue into specified HttpHeader.
1488 @param[in,out] HttpHeader Specified HttpHeader.
1489 @param[in] FieldName FieldName of this HttpHeader, a NULL terminated ASCII string.
1490 @param[in] FieldValue FieldValue of this HttpHeader, a NULL terminated ASCII string.
1493 @retval EFI_SUCCESS The FieldName and FieldValue are set into HttpHeader successfully.
1494 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1495 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
1500 HttpSetFieldNameAndValue (
1501 IN OUT EFI_HTTP_HEADER
*HttpHeader
,
1502 IN CONST CHAR8
*FieldName
,
1503 IN CONST CHAR8
*FieldValue
1506 UINTN FieldNameSize
;
1507 UINTN FieldValueSize
;
1509 if (HttpHeader
== NULL
|| FieldName
== NULL
|| FieldValue
== NULL
) {
1510 return EFI_INVALID_PARAMETER
;
1513 if (HttpHeader
->FieldName
!= NULL
) {
1514 FreePool (HttpHeader
->FieldName
);
1516 if (HttpHeader
->FieldValue
!= NULL
) {
1517 FreePool (HttpHeader
->FieldValue
);
1520 FieldNameSize
= AsciiStrSize (FieldName
);
1521 HttpHeader
->FieldName
= AllocateZeroPool (FieldNameSize
);
1522 if (HttpHeader
->FieldName
== NULL
) {
1523 return EFI_OUT_OF_RESOURCES
;
1525 CopyMem (HttpHeader
->FieldName
, FieldName
, FieldNameSize
);
1526 HttpHeader
->FieldName
[FieldNameSize
- 1] = 0;
1528 FieldValueSize
= AsciiStrSize (FieldValue
);
1529 HttpHeader
->FieldValue
= AllocateZeroPool (FieldValueSize
);
1530 if (HttpHeader
->FieldValue
== NULL
) {
1531 FreePool (HttpHeader
->FieldName
);
1532 return EFI_OUT_OF_RESOURCES
;
1534 CopyMem (HttpHeader
->FieldValue
, FieldValue
, FieldValueSize
);
1535 HttpHeader
->FieldValue
[FieldValueSize
- 1] = 0;
1541 Get one key/value header pair from the raw string.
1543 @param[in] String Pointer to the raw string.
1544 @param[out] FieldName Points directly to field name within 'HttpHeader'.
1545 @param[out] FieldValue Points directly to field value within 'HttpHeader'.
1547 @return Pointer to the next raw string.
1548 @return NULL if no key/value header pair from this raw string.
1553 HttpGetFieldNameAndValue (
1555 OUT CHAR8
**FieldName
,
1556 OUT CHAR8
**FieldValue
1559 CHAR8
*FieldNameStr
;
1560 CHAR8
*FieldValueStr
;
1564 if (String
== NULL
|| FieldName
== NULL
|| FieldValue
== NULL
) {
1570 FieldNameStr
= NULL
;
1571 FieldValueStr
= NULL
;
1577 // Check whether the raw HTTP header string is valid or not.
1579 EndofHeader
= AsciiStrStr (String
, "\r\n\r\n");
1580 if (EndofHeader
== NULL
) {
1585 // Each header field consists of a name followed by a colon (":") and the field value.
1586 // The field value MAY be preceded by any amount of LWS, though a single SP is preferred.
1588 // message-header = field-name ":" [ field-value ]
1589 // field-name = token
1590 // field-value = *( field-content | LWS )
1592 // Note: "*(element)" allows any number element, including zero; "1*(element)" requires at least one element.
1593 // [element] means element is optional.
1594 // LWS = [CRLF] 1*(SP|HT), it can be ' ' or '\t' or '\r\n ' or '\r\n\t'.
1599 FieldNameStr
= String
;
1600 FieldValueStr
= AsciiStrGetNextToken (FieldNameStr
, ':');
1601 if (FieldValueStr
== NULL
) {
1606 // Replace ':' with 0, then FieldName has been retrived from String.
1608 *(FieldValueStr
- 1) = 0;
1611 // Handle FieldValueStr, skip all the preceded LWS.
1614 if (*FieldValueStr
== ' ' || *FieldValueStr
== '\t') {
1616 // Boundary condition check.
1618 if ((UINTN
) EndofHeader
- (UINTN
) FieldValueStr
< 1) {
1620 // Wrong String format!
1626 } else if (*FieldValueStr
== '\r') {
1628 // Boundary condition check.
1630 if ((UINTN
) EndofHeader
- (UINTN
) FieldValueStr
< 3) {
1632 // No more preceded LWS, so break here.
1637 if (*(FieldValueStr
+ 1) == '\n' ) {
1638 if (*(FieldValueStr
+ 2) == ' ' || *(FieldValueStr
+ 2) == '\t') {
1639 FieldValueStr
= FieldValueStr
+ 3;
1642 // No more preceded LWS, so break here.
1648 // Wrong String format!
1654 // No more preceded LWS, so break here.
1660 StrPtr
= FieldValueStr
;
1663 // Handle the LWS within the field value.
1665 StrPtr
= AsciiStrGetNextToken (StrPtr
, '\r');
1666 if (StrPtr
== NULL
|| *StrPtr
!= '\n') {
1668 // Wrong String format!
1674 } while (*StrPtr
== ' ' || *StrPtr
== '\t');
1677 // Replace '\r' with 0
1682 // Get FieldName and FieldValue.
1684 *FieldName
= FieldNameStr
;
1685 *FieldValue
= FieldValueStr
;
1691 Free existing HeaderFields.
1693 @param[in] HeaderFields Pointer to array of key/value header pairs waiting for free.
1694 @param[in] FieldCount The number of header pairs in HeaderFields.
1699 HttpFreeHeaderFields (
1700 IN EFI_HTTP_HEADER
*HeaderFields
,
1706 if (HeaderFields
!= NULL
) {
1707 for (Index
= 0; Index
< FieldCount
; Index
++) {
1708 if (HeaderFields
[Index
].FieldName
!= NULL
) {
1709 FreePool (HeaderFields
[Index
].FieldName
);
1711 if (HeaderFields
[Index
].FieldValue
!= NULL
) {
1712 FreePool (HeaderFields
[Index
].FieldValue
);
1716 FreePool (HeaderFields
);
1721 Generate HTTP request message.
1723 This function will allocate memory for the whole HTTP message and generate a
1724 well formatted HTTP Request message in it, include the Request-Line, header
1725 fields and also the message body. It is the caller's responsibility to free
1726 the buffer returned in *RequestMsg.
1728 @param[in] Message Pointer to the EFI_HTTP_MESSAGE structure which
1729 contains the required information to generate
1730 the HTTP request message.
1731 @param[in] Url The URL of a remote host.
1732 @param[out] RequestMsg Pointer to the created HTTP request message.
1733 NULL if any error occurred.
1734 @param[out] RequestMsgSize Size of the RequestMsg (in bytes).
1736 @retval EFI_SUCCESS If HTTP request string was created successfully.
1737 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
1738 @retval EFI_INVALID_PARAMETER The input arguments are invalid.
1743 HttpGenRequestMessage (
1744 IN CONST EFI_HTTP_MESSAGE
*Message
,
1745 IN CONST CHAR8
*Url
,
1746 OUT CHAR8
**RequestMsg
,
1747 OUT UINTN
*RequestMsgSize
1757 EFI_HTTP_HEADER
**AppendList
;
1759 EFI_HTTP_UTILITIES_PROTOCOL
*HttpUtilitiesProtocol
;
1761 Status
= EFI_SUCCESS
;
1767 HttpUtilitiesProtocol
= NULL
;
1770 // 1. If we have a Request, we cannot have a NULL Url
1771 // 2. If we have a Request, HeaderCount can not be non-zero
1772 // 3. If we do not have a Request, HeaderCount should be zero
1773 // 4. If we do not have Request and Headers, we need at least a message-body
1775 if ((Message
== NULL
|| RequestMsg
== NULL
|| RequestMsgSize
== NULL
) ||
1776 (Message
->Data
.Request
!= NULL
&& Url
== NULL
) ||
1777 (Message
->Data
.Request
!= NULL
&& Message
->HeaderCount
== 0) ||
1778 (Message
->Data
.Request
== NULL
&& Message
->HeaderCount
!= 0) ||
1779 (Message
->Data
.Request
== NULL
&& Message
->HeaderCount
== 0 && Message
->BodyLength
== 0)) {
1780 return EFI_INVALID_PARAMETER
;
1783 if (Message
->HeaderCount
!= 0) {
1785 // Locate the HTTP_UTILITIES protocol.
1787 Status
= gBS
->LocateProtocol (
1788 &gEfiHttpUtilitiesProtocolGuid
,
1790 (VOID
**) &HttpUtilitiesProtocol
1793 if (EFI_ERROR (Status
)) {
1794 DEBUG ((DEBUG_ERROR
,"Failed to locate Http Utilities protocol. Status = %r.\n", Status
));
1799 // Build AppendList to send into HttpUtilitiesBuild
1801 AppendList
= AllocateZeroPool (sizeof (EFI_HTTP_HEADER
*) * (Message
->HeaderCount
));
1802 if (AppendList
== NULL
) {
1803 return EFI_OUT_OF_RESOURCES
;
1806 for(Index
= 0; Index
< Message
->HeaderCount
; Index
++){
1807 AppendList
[Index
] = &Message
->Headers
[Index
];
1811 // Build raw HTTP Headers
1813 Status
= HttpUtilitiesProtocol
->Build (
1814 HttpUtilitiesProtocol
,
1819 Message
->HeaderCount
,
1825 FreePool (AppendList
);
1827 if (EFI_ERROR (Status
) || HttpHdr
== NULL
){
1833 // If we have headers to be sent, account for it.
1835 if (Message
->HeaderCount
!= 0) {
1836 MsgSize
= HttpHdrSize
;
1840 // If we have a request line, account for the fields.
1842 if (Message
->Data
.Request
!= NULL
) {
1843 MsgSize
+= HTTP_METHOD_MAXIMUM_LEN
+ AsciiStrLen (HTTP_VERSION_CRLF_STR
) + AsciiStrLen (Url
);
1848 // If we have a message body to be sent, account for it.
1850 MsgSize
+= Message
->BodyLength
;
1853 // memory for the string that needs to be sent to TCP
1856 *RequestMsg
= AllocateZeroPool (MsgSize
);
1857 if (*RequestMsg
== NULL
) {
1858 Status
= EFI_OUT_OF_RESOURCES
;
1862 RequestPtr
= *RequestMsg
;
1864 // Construct header request
1866 if (Message
->Data
.Request
!= NULL
) {
1867 switch (Message
->Data
.Request
->Method
) {
1869 StrLength
= sizeof (HTTP_METHOD_GET
) - 1;
1870 CopyMem (RequestPtr
, HTTP_METHOD_GET
, StrLength
);
1871 RequestPtr
+= StrLength
;
1874 StrLength
= sizeof (HTTP_METHOD_PUT
) - 1;
1875 CopyMem (RequestPtr
, HTTP_METHOD_PUT
, StrLength
);
1876 RequestPtr
+= StrLength
;
1878 case HttpMethodPatch
:
1879 StrLength
= sizeof (HTTP_METHOD_PATCH
) - 1;
1880 CopyMem (RequestPtr
, HTTP_METHOD_PATCH
, StrLength
);
1881 RequestPtr
+= StrLength
;
1883 case HttpMethodPost
:
1884 StrLength
= sizeof (HTTP_METHOD_POST
) - 1;
1885 CopyMem (RequestPtr
, HTTP_METHOD_POST
, StrLength
);
1886 RequestPtr
+= StrLength
;
1888 case HttpMethodHead
:
1889 StrLength
= sizeof (HTTP_METHOD_HEAD
) - 1;
1890 CopyMem (RequestPtr
, HTTP_METHOD_HEAD
, StrLength
);
1891 RequestPtr
+= StrLength
;
1893 case HttpMethodDelete
:
1894 StrLength
= sizeof (HTTP_METHOD_DELETE
) - 1;
1895 CopyMem (RequestPtr
, HTTP_METHOD_DELETE
, StrLength
);
1896 RequestPtr
+= StrLength
;
1900 Status
= EFI_INVALID_PARAMETER
;
1904 StrLength
= AsciiStrLen(EMPTY_SPACE
);
1905 CopyMem (RequestPtr
, EMPTY_SPACE
, StrLength
);
1906 RequestPtr
+= StrLength
;
1908 StrLength
= AsciiStrLen (Url
);
1909 CopyMem (RequestPtr
, Url
, StrLength
);
1910 RequestPtr
+= StrLength
;
1912 StrLength
= sizeof (HTTP_VERSION_CRLF_STR
) - 1;
1913 CopyMem (RequestPtr
, HTTP_VERSION_CRLF_STR
, StrLength
);
1914 RequestPtr
+= StrLength
;
1916 if (HttpHdr
!= NULL
) {
1920 CopyMem (RequestPtr
, HttpHdr
, HttpHdrSize
);
1921 RequestPtr
+= HttpHdrSize
;
1928 if (Message
->Body
!= NULL
) {
1929 CopyMem (RequestPtr
, Message
->Body
, Message
->BodyLength
);
1930 RequestPtr
+= Message
->BodyLength
;
1936 (*RequestMsgSize
) = (UINTN
)(RequestPtr
) - (UINTN
)(*RequestMsg
);
1942 if (*RequestMsg
!= NULL
) {
1943 FreePool (*RequestMsg
);
1949 if (HttpHdr
!= NULL
) {
1957 Translate the status code in HTTP message to EFI_HTTP_STATUS_CODE defined
1958 in UEFI 2.5 specification.
1960 @param[in] StatusCode The status code value in HTTP message.
1962 @return Value defined in EFI_HTTP_STATUS_CODE .
1965 EFI_HTTP_STATUS_CODE
1967 HttpMappingToStatusCode (
1971 switch (StatusCode
) {
1973 return HTTP_STATUS_100_CONTINUE
;
1975 return HTTP_STATUS_101_SWITCHING_PROTOCOLS
;
1977 return HTTP_STATUS_200_OK
;
1979 return HTTP_STATUS_201_CREATED
;
1981 return HTTP_STATUS_202_ACCEPTED
;
1983 return HTTP_STATUS_203_NON_AUTHORITATIVE_INFORMATION
;
1985 return HTTP_STATUS_204_NO_CONTENT
;
1987 return HTTP_STATUS_205_RESET_CONTENT
;
1989 return HTTP_STATUS_206_PARTIAL_CONTENT
;
1991 return HTTP_STATUS_300_MULTIPLE_CHOICES
;
1993 return HTTP_STATUS_301_MOVED_PERMANENTLY
;
1995 return HTTP_STATUS_302_FOUND
;
1997 return HTTP_STATUS_303_SEE_OTHER
;
1999 return HTTP_STATUS_304_NOT_MODIFIED
;
2001 return HTTP_STATUS_305_USE_PROXY
;
2003 return HTTP_STATUS_307_TEMPORARY_REDIRECT
;
2005 return HTTP_STATUS_308_PERMANENT_REDIRECT
;
2007 return HTTP_STATUS_400_BAD_REQUEST
;
2009 return HTTP_STATUS_401_UNAUTHORIZED
;
2011 return HTTP_STATUS_402_PAYMENT_REQUIRED
;
2013 return HTTP_STATUS_403_FORBIDDEN
;
2015 return HTTP_STATUS_404_NOT_FOUND
;
2017 return HTTP_STATUS_405_METHOD_NOT_ALLOWED
;
2019 return HTTP_STATUS_406_NOT_ACCEPTABLE
;
2021 return HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED
;
2023 return HTTP_STATUS_408_REQUEST_TIME_OUT
;
2025 return HTTP_STATUS_409_CONFLICT
;
2027 return HTTP_STATUS_410_GONE
;
2029 return HTTP_STATUS_411_LENGTH_REQUIRED
;
2031 return HTTP_STATUS_412_PRECONDITION_FAILED
;
2033 return HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE
;
2035 return HTTP_STATUS_414_REQUEST_URI_TOO_LARGE
;
2037 return HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE
;
2039 return HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED
;
2041 return HTTP_STATUS_417_EXPECTATION_FAILED
;
2043 return HTTP_STATUS_500_INTERNAL_SERVER_ERROR
;
2045 return HTTP_STATUS_501_NOT_IMPLEMENTED
;
2047 return HTTP_STATUS_502_BAD_GATEWAY
;
2049 return HTTP_STATUS_503_SERVICE_UNAVAILABLE
;
2051 return HTTP_STATUS_504_GATEWAY_TIME_OUT
;
2053 return HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED
;
2056 return HTTP_STATUS_UNSUPPORTED_STATUS
;
2061 Check whether header field called FieldName is in DeleteList.
2063 @param[in] DeleteList Pointer to array of key/value header pairs.
2064 @param[in] DeleteCount The number of header pairs.
2065 @param[in] FieldName Pointer to header field's name.
2067 @return TRUE if FieldName is not in DeleteList, that means this header field is valid.
2068 @return FALSE if FieldName is in DeleteList, that means this header field is invalid.
2073 HttpIsValidHttpHeader (
2074 IN CHAR8
*DeleteList
[],
2075 IN UINTN DeleteCount
,
2081 if (FieldName
== NULL
) {
2085 for (Index
= 0; Index
< DeleteCount
; Index
++) {
2086 if (DeleteList
[Index
] == NULL
) {
2090 if (AsciiStrCmp (FieldName
, DeleteList
[Index
]) == 0) {