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 - 2020 Hewlett Packard Enterprise Development LP<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "DxeHttpLib.h"
14 Decode a percent-encoded URI component to the ASCII character.
16 Decode the input component in Buffer according to RFC 3986. The caller is responsible to make
17 sure ResultBuffer points to a buffer with size equal or greater than ((AsciiStrSize (Buffer))
20 @param[in] Buffer The pointer to a percent-encoded URI component.
21 @param[in] BufferLength Length of Buffer in bytes.
22 @param[out] ResultBuffer Point to the buffer to store the decode result.
23 @param[out] ResultLength Length of decoded string in ResultBuffer in bytes.
25 @retval EFI_SUCCESS Successfully decoded the URI.
26 @retval EFI_INVALID_PARAMETER Buffer is not a valid percent-encoded string.
33 IN UINT32 BufferLength
,
34 OUT CHAR8
*ResultBuffer
,
35 OUT UINT32
*ResultLength
42 if ((Buffer
== NULL
) || (BufferLength
== 0) || (ResultBuffer
== NULL
)) {
43 return EFI_INVALID_PARAMETER
;
49 while (Index
< BufferLength
) {
50 if (Buffer
[Index
] == '%') {
51 if ((Index
+ 1 >= BufferLength
) || (Index
+ 2 >= BufferLength
) ||
52 !NET_IS_HEX_CHAR (Buffer
[Index
+1]) || !NET_IS_HEX_CHAR (Buffer
[Index
+2]))
54 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
];
69 *ResultLength
= (UINT32
)Offset
;
75 This function return the updated state according to the input state and next character of
78 @param[in] Char Next character.
79 @param[in] State Current value of the parser state machine.
80 @param[in] IsRightBracket TRUE if there is an sign ']' in the authority component and
81 indicates the next part is ':' before Port.
83 @return Updated state value.
86 NetHttpParseAuthorityChar (
88 IN HTTP_URL_PARSE_STATE State
,
89 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
;
114 case UrlParserHostStart
:
116 return UrlParserHostIpv6
;
120 return UrlParserPortStart
;
123 return UrlParserHost
;
125 case UrlParserHostIpv6
:
127 *IsRightBracket
= TRUE
;
130 if ((Char
== ':') && *IsRightBracket
) {
131 return UrlParserPortStart
;
134 return UrlParserHostIpv6
;
137 case UrlParserPortStart
:
138 return UrlParserPort
;
148 This function parse the authority component of the input URL and update the parser.
150 @param[in] Url The pointer to a HTTP URL string.
151 @param[in] FoundAt TRUE if there is an at sign ('@') in the authority component.
152 @param[in, out] UrlParser Pointer to the buffer of the parse result.
154 @retval EFI_SUCCESS Successfully parse the authority.
155 @retval EFI_INVALID_PARAMETER The Url is invalid to parse the authority component.
159 NetHttpParseAuthority (
162 IN OUT HTTP_URL_PARSER
*UrlParser
168 HTTP_URL_PARSE_STATE State
;
171 BOOLEAN IsrightBracket
;
173 ASSERT ((UrlParser
->FieldBitMap
& BIT (HTTP_URI_FIELD_AUTHORITY
)) != 0);
176 // authority = [ userinfo "@" ] host [ ":" port ]
179 State
= UrlParserUserInfo
;
181 State
= UrlParserHost
;
184 IsrightBracket
= FALSE
;
185 Field
= HTTP_URI_FIELD_MAX
;
187 Authority
= Url
+ UrlParser
->FieldData
[HTTP_URI_FIELD_AUTHORITY
].Offset
;
188 Length
= UrlParser
->FieldData
[HTTP_URI_FIELD_AUTHORITY
].Length
;
189 for (Char
= Authority
; Char
< Authority
+ Length
; Char
++) {
190 State
= NetHttpParseAuthorityChar (*Char
, State
, &IsrightBracket
);
192 case UrlParserStateMax
:
193 return EFI_INVALID_PARAMETER
;
195 case UrlParserHostStart
:
196 case UrlParserPortStart
:
199 case UrlParserUserInfo
:
200 Field
= HTTP_URI_FIELD_USERINFO
;
204 Field
= HTTP_URI_FIELD_HOST
;
207 case UrlParserHostIpv6
:
208 Field
= HTTP_URI_FIELD_HOST
;
212 Field
= HTTP_URI_FIELD_PORT
;
220 // Field not changed, count the length.
222 ASSERT (Field
< HTTP_URI_FIELD_MAX
);
223 if (Field
== OldField
) {
224 UrlParser
->FieldData
[Field
].Length
++;
231 UrlParser
->FieldBitMap
|= BIT (Field
);
232 UrlParser
->FieldData
[Field
].Offset
= (UINT32
)(Char
- Url
);
233 UrlParser
->FieldData
[Field
].Length
= 1;
241 This function return the updated state according to the input state and next character of a URL.
243 @param[in] Char Next character.
244 @param[in] State Current value of the parser state machine.
246 @return Updated state value.
250 NetHttpParseUrlChar (
252 IN HTTP_URL_PARSE_STATE State
255 if ((Char
== ' ') || (Char
== '\r') || (Char
== '\n')) {
256 return UrlParserStateMax
;
260 // http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]
262 // Request-URI = "*" | absolute-URI | path-absolute | authority
264 // absolute-URI = scheme ":" hier-part [ "?" query ]
265 // path-absolute = "/" [ segment-nz *( "/" segment ) ]
266 // authority = [ userinfo "@" ] host [ ":" port ]
269 case UrlParserUrlStart
:
270 if ((Char
== '*') || (Char
== '/')) {
271 return UrlParserPath
;
274 return UrlParserScheme
;
276 case UrlParserScheme
:
278 return UrlParserSchemeColon
;
283 case UrlParserSchemeColon
:
285 return UrlParserSchemeColonSlash
;
290 case UrlParserSchemeColonSlash
:
292 return UrlParserSchemeColonSlashSlash
;
297 case UrlParserAtInAuthority
:
299 return UrlParserStateMax
;
302 case UrlParserAuthority
:
303 case UrlParserSchemeColonSlashSlash
:
305 return UrlParserAtInAuthority
;
309 return UrlParserPath
;
313 return UrlParserQueryStart
;
317 return UrlParserFragmentStart
;
320 return UrlParserAuthority
;
324 return UrlParserQueryStart
;
328 return UrlParserFragmentStart
;
334 case UrlParserQueryStart
:
336 return UrlParserFragmentStart
;
339 return UrlParserQuery
;
341 case UrlParserFragmentStart
:
342 return UrlParserFragment
;
352 Create a URL parser for the input URL string.
354 This function will parse and dereference the input HTTP URL into it components. The original
355 content of the URL won't be modified and the result will be returned in UrlParser, which can
356 be used in other functions like NetHttpUrlGetHostName().
358 @param[in] Url The pointer to a HTTP URL string.
359 @param[in] Length Length of Url in bytes.
360 @param[in] IsConnectMethod Whether the Url is used in HTTP CONNECT method or not.
361 @param[out] UrlParser Pointer to the returned buffer to store the parse result.
363 @retval EFI_SUCCESS Successfully dereferenced the HTTP URL.
364 @retval EFI_INVALID_PARAMETER UrlParser is NULL or Url is not a valid HTTP URL.
365 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
373 IN BOOLEAN IsConnectMethod
,
377 HTTP_URL_PARSE_STATE State
;
383 HTTP_URL_PARSER
*Parser
;
387 if ((Url
== NULL
) || (Length
== 0) || (UrlParser
== NULL
)) {
388 return EFI_INVALID_PARAMETER
;
391 Parser
= AllocateZeroPool (sizeof (HTTP_URL_PARSER
));
392 if (Parser
== NULL
) {
393 return EFI_OUT_OF_RESOURCES
;
396 if (IsConnectMethod
) {
398 // According to RFC 2616, the authority form is only used by the CONNECT method.
400 State
= UrlParserAuthority
;
402 State
= UrlParserUrlStart
;
405 Field
= HTTP_URI_FIELD_MAX
;
408 for (Char
= Url
; Char
< Url
+ Length
; Char
++) {
410 // Update state machine according to next char.
412 State
= NetHttpParseUrlChar (*Char
, State
);
415 case UrlParserStateMax
:
417 return EFI_INVALID_PARAMETER
;
419 case UrlParserSchemeColon
:
420 case UrlParserSchemeColonSlash
:
421 case UrlParserSchemeColonSlashSlash
:
422 case UrlParserQueryStart
:
423 case UrlParserFragmentStart
:
425 // Skip all the delimiting char: "://" "?" "@"
429 case UrlParserScheme
:
430 Field
= HTTP_URI_FIELD_SCHEME
;
433 case UrlParserAtInAuthority
:
435 case UrlParserAuthority
:
436 Field
= HTTP_URI_FIELD_AUTHORITY
;
440 Field
= HTTP_URI_FIELD_PATH
;
444 Field
= HTTP_URI_FIELD_QUERY
;
447 case UrlParserFragment
:
448 Field
= HTTP_URI_FIELD_FRAGMENT
;
456 // Field not changed, count the length.
458 ASSERT (Field
< HTTP_URI_FIELD_MAX
);
459 if (Field
== OldField
) {
460 Parser
->FieldData
[Field
].Length
++;
467 Parser
->FieldBitMap
|= BIT (Field
);
468 Parser
->FieldData
[Field
].Offset
= (UINT32
)(Char
- Url
);
469 Parser
->FieldData
[Field
].Length
= 1;
474 // If has authority component, continue to parse the username, host and port.
476 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_AUTHORITY
)) != 0) {
477 Status
= NetHttpParseAuthority (Url
, FoundAt
, Parser
);
478 if (EFI_ERROR (Status
)) {
489 Get the Hostname from a HTTP URL.
491 This function will return the HostName according to the Url and previous parse result ,and
492 it is the caller's responsibility to free the buffer returned in *HostName.
494 @param[in] Url The pointer to a HTTP URL string.
495 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
496 @param[out] HostName Pointer to a buffer to store the HostName.
498 @retval EFI_SUCCESS Successfully get the required component.
499 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.
500 @retval EFI_NOT_FOUND No hostName component in the URL.
501 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
515 HTTP_URL_PARSER
*Parser
;
517 if ((Url
== NULL
) || (UrlParser
== NULL
) || (HostName
== NULL
)) {
518 return EFI_INVALID_PARAMETER
;
521 Parser
= (HTTP_URL_PARSER
*)UrlParser
;
523 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_HOST
)) == 0) {
524 return EFI_NOT_FOUND
;
527 Name
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
+ 1);
529 return EFI_OUT_OF_RESOURCES
;
532 Status
= UriPercentDecode (
533 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Offset
,
534 Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
,
538 if (EFI_ERROR (Status
)) {
543 Name
[ResultLength
] = '\0';
549 Get the IPv4 address from a HTTP URL.
551 This function will return the IPv4 address according to the Url and previous parse result.
553 @param[in] Url The pointer to a HTTP URL string.
554 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
555 @param[out] Ip4Address Pointer to a buffer to store the IP address.
557 @retval EFI_SUCCESS Successfully get the required component.
558 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip4Address is NULL or UrlParser is invalid.
559 @retval EFI_NOT_FOUND No IPv4 address component in the URL.
560 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
568 OUT EFI_IPv4_ADDRESS
*Ip4Address
574 HTTP_URL_PARSER
*Parser
;
576 if ((Url
== NULL
) || (UrlParser
== NULL
) || (Ip4Address
== NULL
)) {
577 return EFI_INVALID_PARAMETER
;
580 Parser
= (HTTP_URL_PARSER
*)UrlParser
;
582 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_HOST
)) == 0) {
583 return EFI_NOT_FOUND
;
586 Ip4String
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
+ 1);
587 if (Ip4String
== NULL
) {
588 return EFI_OUT_OF_RESOURCES
;
591 Status
= UriPercentDecode (
592 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Offset
,
593 Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
,
597 if (EFI_ERROR (Status
)) {
598 FreePool (Ip4String
);
602 Ip4String
[ResultLength
] = '\0';
603 Status
= NetLibAsciiStrToIp4 (Ip4String
, Ip4Address
);
604 FreePool (Ip4String
);
610 Get the IPv6 address from a HTTP URL.
612 This function will return the IPv6 address according to the Url and previous parse result.
614 @param[in] Url The pointer to a HTTP URL string.
615 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
616 @param[out] Ip6Address Pointer to a buffer to store the IP address.
618 @retval EFI_SUCCESS Successfully get the required component.
619 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip6Address is NULL or UrlParser is invalid.
620 @retval EFI_NOT_FOUND No IPv6 address component in the URL.
621 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
629 OUT EFI_IPv6_ADDRESS
*Ip6Address
637 HTTP_URL_PARSER
*Parser
;
639 if ((Url
== NULL
) || (UrlParser
== NULL
) || (Ip6Address
== NULL
)) {
640 return EFI_INVALID_PARAMETER
;
643 Parser
= (HTTP_URL_PARSER
*)UrlParser
;
645 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_HOST
)) == 0) {
646 return EFI_NOT_FOUND
;
650 // IP-literal = "[" ( IPv6address / IPvFuture ) "]"
652 Length
= Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Length
;
654 return EFI_INVALID_PARAMETER
;
657 Ptr
= Url
+ Parser
->FieldData
[HTTP_URI_FIELD_HOST
].Offset
;
658 if ((Ptr
[0] != '[') || (Ptr
[Length
- 1] != ']')) {
659 return EFI_INVALID_PARAMETER
;
662 Ip6String
= AllocatePool (Length
);
663 if (Ip6String
== NULL
) {
664 return EFI_OUT_OF_RESOURCES
;
667 Status
= UriPercentDecode (
673 if (EFI_ERROR (Status
)) {
674 FreePool (Ip6String
);
678 Ip6String
[ResultLength
] = '\0';
679 Status
= NetLibAsciiStrToIp6 (Ip6String
, Ip6Address
);
680 FreePool (Ip6String
);
686 Get the port number from a HTTP URL.
688 This function will return the port number according to the Url and previous parse result.
690 @param[in] Url The pointer to a HTTP URL string.
691 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
692 @param[out] Port Pointer to a buffer to store the port number.
694 @retval EFI_SUCCESS Successfully get the required component.
695 @retval EFI_INVALID_PARAMETER Uri is NULL or Port is NULL or UrlParser is invalid.
696 @retval EFI_NOT_FOUND No port number in the URL.
697 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
713 HTTP_URL_PARSER
*Parser
;
715 if ((Url
== NULL
) || (UrlParser
== NULL
) || (Port
== NULL
)) {
716 return EFI_INVALID_PARAMETER
;
722 Parser
= (HTTP_URL_PARSER
*)UrlParser
;
724 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_PORT
)) == 0) {
725 return EFI_NOT_FOUND
;
728 PortString
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Length
+ 1);
729 if (PortString
== NULL
) {
730 return EFI_OUT_OF_RESOURCES
;
733 Status
= UriPercentDecode (
734 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Offset
,
735 Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Length
,
739 if (EFI_ERROR (Status
)) {
743 PortString
[ResultLength
] = '\0';
745 while (Index
< ResultLength
) {
746 if (!NET_IS_DIGIT (PortString
[Index
])) {
747 Status
= EFI_INVALID_PARAMETER
;
754 Status
= AsciiStrDecimalToUintnS (Url
+ Parser
->FieldData
[HTTP_URI_FIELD_PORT
].Offset
, (CHAR8
**)NULL
, &Data
);
756 if (Data
> HTTP_URI_PORT_MAX_NUM
) {
757 Status
= EFI_INVALID_PARAMETER
;
761 *Port
= (UINT16
)Data
;
764 FreePool (PortString
);
769 Get the Path from a HTTP URL.
771 This function will return the Path according to the Url and previous parse result,and
772 it is the caller's responsibility to free the buffer returned in *Path.
774 @param[in] Url The pointer to a HTTP URL string.
775 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
776 @param[out] Path Pointer to a buffer to store the Path.
778 @retval EFI_SUCCESS Successfully get the required component.
779 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.
780 @retval EFI_NOT_FOUND No hostName component in the URL.
781 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
795 HTTP_URL_PARSER
*Parser
;
797 if ((Url
== NULL
) || (UrlParser
== NULL
) || (Path
== NULL
)) {
798 return EFI_INVALID_PARAMETER
;
801 Parser
= (HTTP_URL_PARSER
*)UrlParser
;
803 if ((Parser
->FieldBitMap
& BIT (HTTP_URI_FIELD_PATH
)) == 0) {
804 return EFI_NOT_FOUND
;
807 PathStr
= AllocatePool (Parser
->FieldData
[HTTP_URI_FIELD_PATH
].Length
+ 1);
808 if (PathStr
== NULL
) {
809 return EFI_OUT_OF_RESOURCES
;
812 Status
= UriPercentDecode (
813 Url
+ Parser
->FieldData
[HTTP_URI_FIELD_PATH
].Offset
,
814 Parser
->FieldData
[HTTP_URI_FIELD_PATH
].Length
,
818 if (EFI_ERROR (Status
)) {
823 PathStr
[ResultLength
] = '\0';
829 Release the resource of the URL parser.
831 @param[in] UrlParser Pointer to the parser.
840 FreePool (UrlParser
);
844 Find a specified header field according to the field name.
846 @param[in] HeaderCount Number of HTTP header structures in Headers list.
847 @param[in] Headers Array containing list of HTTP headers.
848 @param[in] FieldName Null terminated string which describes a field name.
850 @return Pointer to the found header or NULL.
856 IN UINTN HeaderCount
,
857 IN EFI_HTTP_HEADER
*Headers
,
863 if ((HeaderCount
== 0) || (Headers
== NULL
) || (FieldName
== NULL
)) {
867 for (Index
= 0; Index
< HeaderCount
; Index
++) {
869 // Field names are case-insensitive (RFC 2616).
871 if (AsciiStriCmp (Headers
[Index
].FieldName
, FieldName
) == 0) {
872 return &Headers
[Index
];
881 BodyParserBodyIdentity
,
882 BodyParserChunkSizeStart
,
884 BodyParserChunkSizeEndCR
,
885 BodyParserChunkExtStart
,
886 BodyParserChunkDataStart
,
887 BodyParserChunkDataEnd
,
888 BodyParserChunkDataEndCR
,
891 BodyParserLastCRLFEnd
,
894 } HTTP_BODY_PARSE_STATE
;
897 BOOLEAN IgnoreBody
; // "MUST NOT" include a message-body
898 BOOLEAN IsChunked
; // "chunked" transfer-coding.
899 BOOLEAN ContentLengthIsValid
;
900 UINTN ContentLength
; // Entity length (not the message-body length), invalid until ContentLengthIsValid is TRUE
902 HTTP_BODY_PARSER_CALLBACK Callback
;
904 UINTN ParsedBodyLength
;
905 HTTP_BODY_PARSE_STATE State
;
906 UINTN CurrentChunkSize
;
907 UINTN CurrentChunkParsedSize
;
911 Convert an hexadecimal char to a value of type UINTN.
913 @param[in] Char Ascii character.
915 @return Value translated from Char.
919 HttpIoHexCharToUintn (
923 if ((Char
>= '0') && (Char
<= '9')) {
927 return (10 + AsciiCharToUpper (Char
) - 'A');
931 Get the value of the content length if there is a "Content-Length" header.
933 @param[in] HeaderCount Number of HTTP header structures in Headers.
934 @param[in] Headers Array containing list of HTTP headers.
935 @param[out] ContentLength Pointer to save the value of the content length.
937 @retval EFI_SUCCESS Successfully get the content length.
938 @retval EFI_NOT_FOUND No "Content-Length" header in the Headers.
942 HttpIoParseContentLengthHeader (
943 IN UINTN HeaderCount
,
944 IN EFI_HTTP_HEADER
*Headers
,
945 OUT UINTN
*ContentLength
948 EFI_HTTP_HEADER
*Header
;
950 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_CONTENT_LENGTH
);
951 if (Header
== NULL
) {
952 return EFI_NOT_FOUND
;
955 return AsciiStrDecimalToUintnS (Header
->FieldValue
, (CHAR8
**)NULL
, ContentLength
);
960 Check whether the HTTP message is using the "chunked" transfer-coding.
962 @param[in] HeaderCount Number of HTTP header structures in Headers.
963 @param[in] Headers Array containing list of HTTP headers.
965 @return The message is "chunked" transfer-coding (TRUE) or not (FALSE).
970 IN UINTN HeaderCount
,
971 IN EFI_HTTP_HEADER
*Headers
974 EFI_HTTP_HEADER
*Header
;
976 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_TRANSFER_ENCODING
);
977 if (Header
== NULL
) {
981 if (AsciiStriCmp (Header
->FieldValue
, "identity") != 0) {
989 Check whether the HTTP message should have a message-body.
991 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.
992 @param[in] StatusCode Response status code returned by the remote host.
994 @return The message should have a message-body (FALSE) or not (TRUE).
998 HttpIoNoMessageBody (
999 IN EFI_HTTP_METHOD Method
,
1000 IN EFI_HTTP_STATUS_CODE StatusCode
1005 // All responses to the HEAD request method
1006 // MUST NOT include a message-body, even though the presence of entity-
1007 // header fields might lead one to believe they do. All 1xx
1008 // (informational), 204 (no content), and 304 (not modified) responses
1009 // MUST NOT include a message-body. All other responses do include a
1010 // message-body, although it MAY be of zero length.
1012 if (Method
== HttpMethodHead
) {
1016 if ((StatusCode
== HTTP_STATUS_100_CONTINUE
) ||
1017 (StatusCode
== HTTP_STATUS_101_SWITCHING_PROTOCOLS
) ||
1018 (StatusCode
== HTTP_STATUS_204_NO_CONTENT
) ||
1019 (StatusCode
== HTTP_STATUS_304_NOT_MODIFIED
))
1028 Initialize a HTTP message-body parser.
1030 This function will create and initialize a HTTP message parser according to caller provided HTTP message
1031 header information. It is the caller's responsibility to free the buffer returned in *UrlParser by HttpFreeMsgParser().
1033 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.
1034 @param[in] StatusCode Response status code returned by the remote host.
1035 @param[in] HeaderCount Number of HTTP header structures in Headers.
1036 @param[in] Headers Array containing list of HTTP headers.
1037 @param[in] Callback Callback function that is invoked when parsing the HTTP message-body,
1038 set to NULL to ignore all events.
1039 @param[in] Context Pointer to the context that will be passed to Callback.
1040 @param[out] MsgParser Pointer to the returned buffer to store the message parser.
1042 @retval EFI_SUCCESS Successfully initialized the parser.
1043 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
1044 @retval EFI_INVALID_PARAMETER MsgParser is NULL or HeaderCount is not NULL but Headers is NULL.
1045 @retval Others Failed to initialize the parser.
1051 IN EFI_HTTP_METHOD Method
,
1052 IN EFI_HTTP_STATUS_CODE StatusCode
,
1053 IN UINTN HeaderCount
,
1054 IN EFI_HTTP_HEADER
*Headers
,
1055 IN HTTP_BODY_PARSER_CALLBACK Callback
,
1057 OUT VOID
**MsgParser
1061 HTTP_BODY_PARSER
*Parser
;
1063 if ((HeaderCount
!= 0) && (Headers
== NULL
)) {
1064 return EFI_INVALID_PARAMETER
;
1067 if (MsgParser
== NULL
) {
1068 return EFI_INVALID_PARAMETER
;
1071 Parser
= AllocateZeroPool (sizeof (HTTP_BODY_PARSER
));
1072 if (Parser
== NULL
) {
1073 return EFI_OUT_OF_RESOURCES
;
1076 Parser
->State
= BodyParserBodyStart
;
1079 // Determine the message length according to RFC 2616.
1080 // 1. Check whether the message "MUST NOT" have a message-body.
1082 Parser
->IgnoreBody
= HttpIoNoMessageBody (Method
, StatusCode
);
1084 // 2. Check whether the message using "chunked" transfer-coding.
1086 Parser
->IsChunked
= HttpIoIsChunked (HeaderCount
, Headers
);
1088 // 3. Check whether the message has a Content-Length header field.
1090 Status
= HttpIoParseContentLengthHeader (HeaderCount
, Headers
, &Parser
->ContentLength
);
1091 if (!EFI_ERROR (Status
)) {
1092 Parser
->ContentLengthIsValid
= TRUE
;
1096 // 4. Range header is not supported now, so we won't meet media type "multipart/byteranges".
1097 // 5. By server closing the connection
1101 // Set state to skip body parser if the message shouldn't have a message body.
1103 if (Parser
->IgnoreBody
) {
1104 Parser
->State
= BodyParserComplete
;
1106 Parser
->Callback
= Callback
;
1107 Parser
->Context
= Context
;
1110 *MsgParser
= Parser
;
1117 Parse BodyLength of message-body. This function can be called repeatedly to parse the message-body partially.
1119 @param[in, out] MsgParser Pointer to the message parser.
1120 @param[in] BodyLength Length in bytes of the Body.
1121 @param[in] Body Pointer to the buffer of the message-body to be parsed.
1123 @retval EFI_SUCCESS Successfully parse the message-body.
1124 @retval EFI_INVALID_PARAMETER MsgParser is NULL or Body is NULL or BodyLength is 0.
1125 @retval EFI_ABORTED Operation aborted.
1126 @retval Other Error happened while parsing message body.
1131 HttpParseMessageBody (
1132 IN OUT VOID
*MsgParser
,
1133 IN UINTN BodyLength
,
1138 UINTN RemainderLengthInThis
;
1139 UINTN LengthForCallback
;
1140 UINTN PortionLength
;
1142 HTTP_BODY_PARSER
*Parser
;
1144 if ((BodyLength
== 0) || (Body
== NULL
)) {
1145 return EFI_INVALID_PARAMETER
;
1148 if (MsgParser
== NULL
) {
1149 return EFI_INVALID_PARAMETER
;
1152 Parser
= (HTTP_BODY_PARSER
*)MsgParser
;
1154 if (Parser
->IgnoreBody
) {
1155 Parser
->State
= BodyParserComplete
;
1156 if (Parser
->Callback
!= NULL
) {
1157 Status
= Parser
->Callback (
1158 BodyParseEventOnComplete
,
1163 if (EFI_ERROR (Status
)) {
1171 if (Parser
->State
== BodyParserBodyStart
) {
1172 Parser
->ParsedBodyLength
= 0;
1173 if (Parser
->IsChunked
) {
1174 Parser
->State
= BodyParserChunkSizeStart
;
1176 Parser
->State
= BodyParserBodyIdentity
;
1181 // The message body might be truncated in anywhere, so we need to parse is byte-by-byte.
1183 for (Char
= Body
; Char
< Body
+ BodyLength
; ) {
1184 switch (Parser
->State
) {
1185 case BodyParserStateMax
:
1188 case BodyParserBodyIdentity
:
1190 // Identity transfer-coding, just notify user to save the body data.
1192 PortionLength
= MIN (
1194 Parser
->ContentLength
- Parser
->ParsedBodyLength
1196 if (PortionLength
== 0) {
1198 // Got BodyLength, but no ContentLength. Use BodyLength.
1200 PortionLength
= BodyLength
;
1201 Parser
->ContentLength
= PortionLength
;
1204 if (Parser
->Callback
!= NULL
) {
1205 Status
= Parser
->Callback (
1206 BodyParseEventOnData
,
1211 if (EFI_ERROR (Status
)) {
1216 Char
+= PortionLength
;
1217 Parser
->ParsedBodyLength
+= PortionLength
;
1218 if (Parser
->ParsedBodyLength
== Parser
->ContentLength
) {
1219 Parser
->State
= BodyParserComplete
;
1220 if (Parser
->Callback
!= NULL
) {
1221 Status
= Parser
->Callback (
1222 BodyParseEventOnComplete
,
1227 if (EFI_ERROR (Status
)) {
1235 case BodyParserChunkSizeStart
:
1237 // First byte of chunk-size, the chunk-size might be truncated.
1239 Parser
->CurrentChunkSize
= 0;
1240 Parser
->State
= BodyParserChunkSize
;
1241 case BodyParserChunkSize
:
1242 if (!NET_IS_HEX_CHAR (*Char
)) {
1244 Parser
->State
= BodyParserChunkExtStart
;
1246 } else if (*Char
== '\r') {
1247 Parser
->State
= BodyParserChunkSizeEndCR
;
1250 Parser
->State
= BodyParserStateMax
;
1256 if (Parser
->CurrentChunkSize
> (((~((UINTN
)0)) - 16) / 16)) {
1257 return EFI_INVALID_PARAMETER
;
1260 Parser
->CurrentChunkSize
= Parser
->CurrentChunkSize
* 16 + HttpIoHexCharToUintn (*Char
);
1264 case BodyParserChunkExtStart
:
1266 // Ignore all the chunk extensions.
1268 if (*Char
== '\r') {
1269 Parser
->State
= BodyParserChunkSizeEndCR
;
1275 case BodyParserChunkSizeEndCR
:
1276 if (*Char
!= '\n') {
1277 Parser
->State
= BodyParserStateMax
;
1282 if (Parser
->CurrentChunkSize
== 0) {
1284 // The last chunk has been parsed and now assumed the state
1285 // of HttpBodyParse is ParserLastCRLF. So it need to decide
1286 // whether the rest message is trailer or last CRLF in the next round.
1288 Parser
->ContentLengthIsValid
= TRUE
;
1289 Parser
->State
= BodyParserLastCRLF
;
1293 Parser
->State
= BodyParserChunkDataStart
;
1294 Parser
->CurrentChunkParsedSize
= 0;
1297 case BodyParserLastCRLF
:
1299 // Judge the byte is belong to the Last CRLF or trailer, and then
1300 // configure the state of HttpBodyParse to corresponding state.
1302 if (*Char
== '\r') {
1304 Parser
->State
= BodyParserLastCRLFEnd
;
1307 Parser
->State
= BodyParserTrailer
;
1311 case BodyParserLastCRLFEnd
:
1312 if (*Char
== '\n') {
1313 Parser
->State
= BodyParserComplete
;
1315 if (Parser
->Callback
!= NULL
) {
1316 Status
= Parser
->Callback (
1317 BodyParseEventOnComplete
,
1322 if (EFI_ERROR (Status
)) {
1329 Parser
->State
= BodyParserStateMax
;
1333 case BodyParserTrailer
:
1334 if (*Char
== '\r') {
1335 Parser
->State
= BodyParserChunkSizeEndCR
;
1341 case BodyParserChunkDataStart
:
1343 // First byte of chunk-data, the chunk data also might be truncated.
1345 RemainderLengthInThis
= BodyLength
- (Char
- Body
);
1346 LengthForCallback
= MIN (Parser
->CurrentChunkSize
- Parser
->CurrentChunkParsedSize
, RemainderLengthInThis
);
1347 if (Parser
->Callback
!= NULL
) {
1348 Status
= Parser
->Callback (
1349 BodyParseEventOnData
,
1354 if (EFI_ERROR (Status
)) {
1359 Char
+= LengthForCallback
;
1360 Parser
->ContentLength
+= LengthForCallback
;
1361 Parser
->CurrentChunkParsedSize
+= LengthForCallback
;
1362 if (Parser
->CurrentChunkParsedSize
== Parser
->CurrentChunkSize
) {
1363 Parser
->State
= BodyParserChunkDataEnd
;
1368 case BodyParserChunkDataEnd
:
1369 if (*Char
== '\r') {
1370 Parser
->State
= BodyParserChunkDataEndCR
;
1372 Parser
->State
= BodyParserStateMax
;
1378 case BodyParserChunkDataEndCR
:
1379 if (*Char
!= '\n') {
1380 Parser
->State
= BodyParserStateMax
;
1385 Parser
->State
= BodyParserChunkSizeStart
;
1393 if (Parser
->State
== BodyParserStateMax
) {
1401 Check whether the message-body is complete or not.
1403 @param[in] MsgParser Pointer to the message parser.
1405 @retval TRUE Message-body is complete.
1406 @retval FALSE Message-body is not complete.
1411 HttpIsMessageComplete (
1415 HTTP_BODY_PARSER
*Parser
;
1417 if (MsgParser
== NULL
) {
1421 Parser
= (HTTP_BODY_PARSER
*)MsgParser
;
1423 if (Parser
->State
== BodyParserComplete
) {
1431 Get the content length of the entity.
1433 Note that in trunk transfer, the entity length is not valid until the whole message body is received.
1435 @param[in] MsgParser Pointer to the message parser.
1436 @param[out] ContentLength Pointer to store the length of the entity.
1438 @retval EFI_SUCCESS Successfully to get the entity length.
1439 @retval EFI_NOT_READY Entity length is not valid yet.
1440 @retval EFI_INVALID_PARAMETER MsgParser is NULL or ContentLength is NULL.
1445 HttpGetEntityLength (
1447 OUT UINTN
*ContentLength
1450 HTTP_BODY_PARSER
*Parser
;
1452 if ((MsgParser
== NULL
) || (ContentLength
== NULL
)) {
1453 return EFI_INVALID_PARAMETER
;
1456 Parser
= (HTTP_BODY_PARSER
*)MsgParser
;
1458 if (!Parser
->ContentLengthIsValid
) {
1459 return EFI_NOT_READY
;
1462 *ContentLength
= Parser
->ContentLength
;
1467 Release the resource of the message parser.
1469 @param[in] MsgParser Pointer to the message parser.
1478 FreePool (MsgParser
);
1482 Get the next string, which is distinguished by specified separator.
1484 @param[in] String Pointer to the string.
1485 @param[in] Separator Specified separator used to distinguish where is the beginning
1488 @return Pointer to the next string.
1489 @return NULL if not find or String is NULL.
1493 AsciiStrGetNextToken (
1494 IN CONST CHAR8
*String
,
1506 if (*Token
== Separator
) {
1507 return (CHAR8
*)(Token
+ 1);
1515 Set FieldName and FieldValue into specified HttpHeader.
1517 @param[in,out] HttpHeader Specified HttpHeader.
1518 @param[in] FieldName FieldName of this HttpHeader, a NULL terminated ASCII string.
1519 @param[in] FieldValue FieldValue of this HttpHeader, a NULL terminated ASCII string.
1522 @retval EFI_SUCCESS The FieldName and FieldValue are set into HttpHeader successfully.
1523 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1524 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
1529 HttpSetFieldNameAndValue (
1530 IN OUT EFI_HTTP_HEADER
*HttpHeader
,
1531 IN CONST CHAR8
*FieldName
,
1532 IN CONST CHAR8
*FieldValue
1535 UINTN FieldNameSize
;
1536 UINTN FieldValueSize
;
1538 if ((HttpHeader
== NULL
) || (FieldName
== NULL
) || (FieldValue
== NULL
)) {
1539 return EFI_INVALID_PARAMETER
;
1542 if (HttpHeader
->FieldName
!= NULL
) {
1543 FreePool (HttpHeader
->FieldName
);
1546 if (HttpHeader
->FieldValue
!= NULL
) {
1547 FreePool (HttpHeader
->FieldValue
);
1550 FieldNameSize
= AsciiStrSize (FieldName
);
1551 HttpHeader
->FieldName
= AllocateZeroPool (FieldNameSize
);
1552 if (HttpHeader
->FieldName
== NULL
) {
1553 return EFI_OUT_OF_RESOURCES
;
1556 CopyMem (HttpHeader
->FieldName
, FieldName
, FieldNameSize
);
1557 HttpHeader
->FieldName
[FieldNameSize
- 1] = 0;
1559 FieldValueSize
= AsciiStrSize (FieldValue
);
1560 HttpHeader
->FieldValue
= AllocateZeroPool (FieldValueSize
);
1561 if (HttpHeader
->FieldValue
== NULL
) {
1562 FreePool (HttpHeader
->FieldName
);
1563 return EFI_OUT_OF_RESOURCES
;
1566 CopyMem (HttpHeader
->FieldValue
, FieldValue
, FieldValueSize
);
1567 HttpHeader
->FieldValue
[FieldValueSize
- 1] = 0;
1573 Get one key/value header pair from the raw string.
1575 @param[in] String Pointer to the raw string.
1576 @param[out] FieldName Points directly to field name within 'HttpHeader'.
1577 @param[out] FieldValue Points directly to field value within 'HttpHeader'.
1579 @return Pointer to the next raw string.
1580 @return NULL if no key/value header pair from this raw string.
1585 HttpGetFieldNameAndValue (
1587 OUT CHAR8
**FieldName
,
1588 OUT CHAR8
**FieldValue
1591 CHAR8
*FieldNameStr
;
1592 CHAR8
*FieldValueStr
;
1596 if ((String
== NULL
) || (FieldName
== NULL
) || (FieldValue
== NULL
)) {
1602 FieldNameStr
= NULL
;
1603 FieldValueStr
= NULL
;
1608 // Check whether the raw HTTP header string is valid or not.
1610 EndofHeader
= AsciiStrStr (String
, "\r\n\r\n");
1611 if (EndofHeader
== NULL
) {
1616 // Each header field consists of a name followed by a colon (":") and the field value.
1617 // The field value MAY be preceded by any amount of LWS, though a single SP is preferred.
1619 // message-header = field-name ":" [ field-value ]
1620 // field-name = token
1621 // field-value = *( field-content | LWS )
1623 // Note: "*(element)" allows any number element, including zero; "1*(element)" requires at least one element.
1624 // [element] means element is optional.
1625 // LWS = [CRLF] 1*(SP|HT), it can be ' ' or '\t' or '\r\n ' or '\r\n\t'.
1630 FieldNameStr
= String
;
1631 FieldValueStr
= AsciiStrGetNextToken (FieldNameStr
, ':');
1632 if (FieldValueStr
== NULL
) {
1637 // Replace ':' with 0, then FieldName has been retrived from String.
1639 *(FieldValueStr
- 1) = 0;
1642 // Handle FieldValueStr, skip all the preceded LWS.
1645 if ((*FieldValueStr
== ' ') || (*FieldValueStr
== '\t')) {
1647 // Boundary condition check.
1649 if ((UINTN
)EndofHeader
- (UINTN
)FieldValueStr
< 1) {
1651 // Wrong String format!
1657 } else if (*FieldValueStr
== '\r') {
1659 // Boundary condition check.
1661 if ((UINTN
)EndofHeader
- (UINTN
)FieldValueStr
< 3) {
1663 // No more preceded LWS, so break here.
1668 if (*(FieldValueStr
+ 1) == '\n' ) {
1669 if ((*(FieldValueStr
+ 2) == ' ') || (*(FieldValueStr
+ 2) == '\t')) {
1670 FieldValueStr
= FieldValueStr
+ 3;
1673 // No more preceded LWS, so break here.
1679 // Wrong String format!
1685 // No more preceded LWS, so break here.
1691 StrPtr
= FieldValueStr
;
1694 // Handle the LWS within the field value.
1696 StrPtr
= AsciiStrGetNextToken (StrPtr
, '\r');
1697 if ((StrPtr
== NULL
) || (*StrPtr
!= '\n')) {
1699 // Wrong String format!
1705 } while (*StrPtr
== ' ' || *StrPtr
== '\t');
1708 // Replace '\r' with 0
1713 // Get FieldName and FieldValue.
1715 *FieldName
= FieldNameStr
;
1716 *FieldValue
= FieldValueStr
;
1722 Free existing HeaderFields.
1724 @param[in] HeaderFields Pointer to array of key/value header pairs waiting for free.
1725 @param[in] FieldCount The number of header pairs in HeaderFields.
1730 HttpFreeHeaderFields (
1731 IN EFI_HTTP_HEADER
*HeaderFields
,
1737 if (HeaderFields
!= NULL
) {
1738 for (Index
= 0; Index
< FieldCount
; Index
++) {
1739 if (HeaderFields
[Index
].FieldName
!= NULL
) {
1740 FreePool (HeaderFields
[Index
].FieldName
);
1743 if (HeaderFields
[Index
].FieldValue
!= NULL
) {
1744 FreePool (HeaderFields
[Index
].FieldValue
);
1748 FreePool (HeaderFields
);
1753 Generate HTTP request message.
1755 This function will allocate memory for the whole HTTP message and generate a
1756 well formatted HTTP Request message in it, include the Request-Line, header
1757 fields and also the message body. It is the caller's responsibility to free
1758 the buffer returned in *RequestMsg.
1760 @param[in] Message Pointer to the EFI_HTTP_MESSAGE structure which
1761 contains the required information to generate
1762 the HTTP request message.
1763 @param[in] Url The URL of a remote host.
1764 @param[out] RequestMsg Pointer to the created HTTP request message.
1765 NULL if any error occurred.
1766 @param[out] RequestMsgSize Size of the RequestMsg (in bytes).
1768 @retval EFI_SUCCESS If HTTP request string was created successfully.
1769 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
1770 @retval EFI_INVALID_PARAMETER The input arguments are invalid.
1775 HttpGenRequestMessage (
1776 IN CONST EFI_HTTP_MESSAGE
*Message
,
1777 IN CONST CHAR8
*Url
,
1778 OUT CHAR8
**RequestMsg
,
1779 OUT UINTN
*RequestMsgSize
1789 EFI_HTTP_HEADER
**AppendList
;
1791 EFI_HTTP_UTILITIES_PROTOCOL
*HttpUtilitiesProtocol
;
1793 Status
= EFI_SUCCESS
;
1799 HttpUtilitiesProtocol
= NULL
;
1802 // 1. If we have a Request, we cannot have a NULL Url
1803 // 2. If we have a Request, HeaderCount can not be non-zero
1804 // 3. If we do not have a Request, HeaderCount should be zero
1805 // 4. If we do not have Request and Headers, we need at least a message-body
1807 if (((Message
== NULL
) || (RequestMsg
== NULL
) || (RequestMsgSize
== NULL
)) ||
1808 ((Message
->Data
.Request
!= NULL
) && (Url
== NULL
)) ||
1809 ((Message
->Data
.Request
!= NULL
) && (Message
->HeaderCount
== 0)) ||
1810 ((Message
->Data
.Request
== NULL
) && (Message
->HeaderCount
!= 0)) ||
1811 ((Message
->Data
.Request
== NULL
) && (Message
->HeaderCount
== 0) && (Message
->BodyLength
== 0)))
1813 return EFI_INVALID_PARAMETER
;
1816 if (Message
->HeaderCount
!= 0) {
1818 // Locate the HTTP_UTILITIES protocol.
1820 Status
= gBS
->LocateProtocol (
1821 &gEfiHttpUtilitiesProtocolGuid
,
1823 (VOID
**)&HttpUtilitiesProtocol
1826 if (EFI_ERROR (Status
)) {
1827 DEBUG ((DEBUG_ERROR
, "Failed to locate Http Utilities protocol. Status = %r.\n", Status
));
1832 // Build AppendList to send into HttpUtilitiesBuild
1834 AppendList
= AllocateZeroPool (sizeof (EFI_HTTP_HEADER
*) * (Message
->HeaderCount
));
1835 if (AppendList
== NULL
) {
1836 return EFI_OUT_OF_RESOURCES
;
1839 for (Index
= 0; Index
< Message
->HeaderCount
; Index
++) {
1840 AppendList
[Index
] = &Message
->Headers
[Index
];
1844 // Build raw HTTP Headers
1846 Status
= HttpUtilitiesProtocol
->Build (
1847 HttpUtilitiesProtocol
,
1852 Message
->HeaderCount
,
1858 FreePool (AppendList
);
1860 if (EFI_ERROR (Status
) || (HttpHdr
== NULL
)) {
1866 // If we have headers to be sent, account for it.
1868 if (Message
->HeaderCount
!= 0) {
1869 MsgSize
= HttpHdrSize
;
1873 // If we have a request line, account for the fields.
1875 if (Message
->Data
.Request
!= NULL
) {
1876 MsgSize
+= HTTP_METHOD_MAXIMUM_LEN
+ AsciiStrLen (HTTP_VERSION_CRLF_STR
) + AsciiStrLen (Url
);
1880 // If we have a message body to be sent, account for it.
1882 MsgSize
+= Message
->BodyLength
;
1885 // memory for the string that needs to be sent to TCP
1888 *RequestMsg
= AllocateZeroPool (MsgSize
);
1889 if (*RequestMsg
== NULL
) {
1890 Status
= EFI_OUT_OF_RESOURCES
;
1894 RequestPtr
= *RequestMsg
;
1896 // Construct header request
1898 if (Message
->Data
.Request
!= NULL
) {
1899 switch (Message
->Data
.Request
->Method
) {
1901 StrLength
= sizeof (HTTP_METHOD_GET
) - 1;
1902 CopyMem (RequestPtr
, HTTP_METHOD_GET
, StrLength
);
1903 RequestPtr
+= StrLength
;
1906 StrLength
= sizeof (HTTP_METHOD_PUT
) - 1;
1907 CopyMem (RequestPtr
, HTTP_METHOD_PUT
, StrLength
);
1908 RequestPtr
+= StrLength
;
1910 case HttpMethodPatch
:
1911 StrLength
= sizeof (HTTP_METHOD_PATCH
) - 1;
1912 CopyMem (RequestPtr
, HTTP_METHOD_PATCH
, StrLength
);
1913 RequestPtr
+= StrLength
;
1915 case HttpMethodPost
:
1916 StrLength
= sizeof (HTTP_METHOD_POST
) - 1;
1917 CopyMem (RequestPtr
, HTTP_METHOD_POST
, StrLength
);
1918 RequestPtr
+= StrLength
;
1920 case HttpMethodHead
:
1921 StrLength
= sizeof (HTTP_METHOD_HEAD
) - 1;
1922 CopyMem (RequestPtr
, HTTP_METHOD_HEAD
, StrLength
);
1923 RequestPtr
+= StrLength
;
1925 case HttpMethodDelete
:
1926 StrLength
= sizeof (HTTP_METHOD_DELETE
) - 1;
1927 CopyMem (RequestPtr
, HTTP_METHOD_DELETE
, StrLength
);
1928 RequestPtr
+= StrLength
;
1932 Status
= EFI_INVALID_PARAMETER
;
1936 StrLength
= AsciiStrLen (EMPTY_SPACE
);
1937 CopyMem (RequestPtr
, EMPTY_SPACE
, StrLength
);
1938 RequestPtr
+= StrLength
;
1940 StrLength
= AsciiStrLen (Url
);
1941 CopyMem (RequestPtr
, Url
, StrLength
);
1942 RequestPtr
+= StrLength
;
1944 StrLength
= sizeof (HTTP_VERSION_CRLF_STR
) - 1;
1945 CopyMem (RequestPtr
, HTTP_VERSION_CRLF_STR
, StrLength
);
1946 RequestPtr
+= StrLength
;
1948 if (HttpHdr
!= NULL
) {
1952 CopyMem (RequestPtr
, HttpHdr
, HttpHdrSize
);
1953 RequestPtr
+= HttpHdrSize
;
1960 if (Message
->Body
!= NULL
) {
1961 CopyMem (RequestPtr
, Message
->Body
, Message
->BodyLength
);
1962 RequestPtr
+= Message
->BodyLength
;
1968 (*RequestMsgSize
) = (UINTN
)(RequestPtr
) - (UINTN
)(*RequestMsg
);
1974 if (*RequestMsg
!= NULL
) {
1975 FreePool (*RequestMsg
);
1982 if (HttpHdr
!= NULL
) {
1990 Translate the status code in HTTP message to EFI_HTTP_STATUS_CODE defined
1991 in UEFI 2.5 specification.
1993 @param[in] StatusCode The status code value in HTTP message.
1995 @return Value defined in EFI_HTTP_STATUS_CODE .
1998 EFI_HTTP_STATUS_CODE
2000 HttpMappingToStatusCode (
2004 switch (StatusCode
) {
2006 return HTTP_STATUS_100_CONTINUE
;
2008 return HTTP_STATUS_101_SWITCHING_PROTOCOLS
;
2010 return HTTP_STATUS_200_OK
;
2012 return HTTP_STATUS_201_CREATED
;
2014 return HTTP_STATUS_202_ACCEPTED
;
2016 return HTTP_STATUS_203_NON_AUTHORITATIVE_INFORMATION
;
2018 return HTTP_STATUS_204_NO_CONTENT
;
2020 return HTTP_STATUS_205_RESET_CONTENT
;
2022 return HTTP_STATUS_206_PARTIAL_CONTENT
;
2024 return HTTP_STATUS_300_MULTIPLE_CHOICES
;
2026 return HTTP_STATUS_301_MOVED_PERMANENTLY
;
2028 return HTTP_STATUS_302_FOUND
;
2030 return HTTP_STATUS_303_SEE_OTHER
;
2032 return HTTP_STATUS_304_NOT_MODIFIED
;
2034 return HTTP_STATUS_305_USE_PROXY
;
2036 return HTTP_STATUS_307_TEMPORARY_REDIRECT
;
2038 return HTTP_STATUS_308_PERMANENT_REDIRECT
;
2040 return HTTP_STATUS_400_BAD_REQUEST
;
2042 return HTTP_STATUS_401_UNAUTHORIZED
;
2044 return HTTP_STATUS_402_PAYMENT_REQUIRED
;
2046 return HTTP_STATUS_403_FORBIDDEN
;
2048 return HTTP_STATUS_404_NOT_FOUND
;
2050 return HTTP_STATUS_405_METHOD_NOT_ALLOWED
;
2052 return HTTP_STATUS_406_NOT_ACCEPTABLE
;
2054 return HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED
;
2056 return HTTP_STATUS_408_REQUEST_TIME_OUT
;
2058 return HTTP_STATUS_409_CONFLICT
;
2060 return HTTP_STATUS_410_GONE
;
2062 return HTTP_STATUS_411_LENGTH_REQUIRED
;
2064 return HTTP_STATUS_412_PRECONDITION_FAILED
;
2066 return HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE
;
2068 return HTTP_STATUS_414_REQUEST_URI_TOO_LARGE
;
2070 return HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE
;
2072 return HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED
;
2074 return HTTP_STATUS_417_EXPECTATION_FAILED
;
2076 return HTTP_STATUS_500_INTERNAL_SERVER_ERROR
;
2078 return HTTP_STATUS_501_NOT_IMPLEMENTED
;
2080 return HTTP_STATUS_502_BAD_GATEWAY
;
2082 return HTTP_STATUS_503_SERVICE_UNAVAILABLE
;
2084 return HTTP_STATUS_504_GATEWAY_TIME_OUT
;
2086 return HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED
;
2089 return HTTP_STATUS_UNSUPPORTED_STATUS
;
2094 Check whether header field called FieldName is in DeleteList.
2096 @param[in] DeleteList Pointer to array of key/value header pairs.
2097 @param[in] DeleteCount The number of header pairs.
2098 @param[in] FieldName Pointer to header field's name.
2100 @return TRUE if FieldName is not in DeleteList, that means this header field is valid.
2101 @return FALSE if FieldName is in DeleteList, that means this header field is invalid.
2106 HttpIsValidHttpHeader (
2107 IN CHAR8
*DeleteList
[],
2108 IN UINTN DeleteCount
,
2114 if (FieldName
== NULL
) {
2118 for (Index
= 0; Index
< DeleteCount
; Index
++) {
2119 if (DeleteList
[Index
] == NULL
) {
2123 if (AsciiStrCmp (FieldName
, DeleteList
[Index
]) == 0) {
2132 Create a HTTP_IO_HEADER to hold the HTTP header items.
2134 @param[in] MaxHeaderCount The maximun number of HTTP header in this holder.
2136 @return A pointer of the HTTP header holder or NULL if failed.
2140 HttpIoCreateHeader (
2141 UINTN MaxHeaderCount
2144 HTTP_IO_HEADER
*HttpIoHeader
;
2146 if (MaxHeaderCount
== 0) {
2150 HttpIoHeader
= AllocateZeroPool (sizeof (HTTP_IO_HEADER
) + MaxHeaderCount
* sizeof (EFI_HTTP_HEADER
));
2151 if (HttpIoHeader
== NULL
) {
2155 HttpIoHeader
->MaxHeaderCount
= MaxHeaderCount
;
2156 HttpIoHeader
->Headers
= (EFI_HTTP_HEADER
*)(HttpIoHeader
+ 1);
2158 return HttpIoHeader
;
2162 Destroy the HTTP_IO_HEADER and release the resources.
2164 @param[in] HttpIoHeader Point to the HTTP header holder to be destroyed.
2169 IN HTTP_IO_HEADER
*HttpIoHeader
2174 if (HttpIoHeader
!= NULL
) {
2175 if (HttpIoHeader
->HeaderCount
!= 0) {
2176 for (Index
= 0; Index
< HttpIoHeader
->HeaderCount
; Index
++) {
2177 FreePool (HttpIoHeader
->Headers
[Index
].FieldName
);
2178 ZeroMem (HttpIoHeader
->Headers
[Index
].FieldValue
, AsciiStrSize (HttpIoHeader
->Headers
[Index
].FieldValue
));
2179 FreePool (HttpIoHeader
->Headers
[Index
].FieldValue
);
2183 FreePool (HttpIoHeader
);
2188 Set or update a HTTP header with the field name and corresponding value.
2190 @param[in] HttpIoHeader Point to the HTTP header holder.
2191 @param[in] FieldName Null terminated string which describes a field name.
2192 @param[in] FieldValue Null terminated string which describes the corresponding field value.
2194 @retval EFI_SUCCESS The HTTP header has been set or updated.
2195 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
2196 @retval EFI_OUT_OF_RESOURCES Insufficient resource to complete the operation.
2197 @retval Other Unexpected error happened.
2202 IN HTTP_IO_HEADER
*HttpIoHeader
,
2203 IN CHAR8
*FieldName
,
2204 IN CHAR8
*FieldValue
2207 EFI_HTTP_HEADER
*Header
;
2209 CHAR8
*NewFieldValue
;
2211 if ((HttpIoHeader
== NULL
) || (FieldName
== NULL
) || (FieldValue
== NULL
)) {
2212 return EFI_INVALID_PARAMETER
;
2215 Header
= HttpFindHeader (HttpIoHeader
->HeaderCount
, HttpIoHeader
->Headers
, FieldName
);
2216 if (Header
== NULL
) {
2218 // Add a new header.
2220 if (HttpIoHeader
->HeaderCount
>= HttpIoHeader
->MaxHeaderCount
) {
2221 return EFI_OUT_OF_RESOURCES
;
2224 Header
= &HttpIoHeader
->Headers
[HttpIoHeader
->HeaderCount
];
2226 StrSize
= AsciiStrSize (FieldName
);
2227 Header
->FieldName
= AllocatePool (StrSize
);
2228 if (Header
->FieldName
== NULL
) {
2229 return EFI_OUT_OF_RESOURCES
;
2232 CopyMem (Header
->FieldName
, FieldName
, StrSize
);
2233 Header
->FieldName
[StrSize
-1] = '\0';
2235 StrSize
= AsciiStrSize (FieldValue
);
2236 Header
->FieldValue
= AllocatePool (StrSize
);
2237 if (Header
->FieldValue
== NULL
) {
2238 FreePool (Header
->FieldName
);
2239 return EFI_OUT_OF_RESOURCES
;
2242 CopyMem (Header
->FieldValue
, FieldValue
, StrSize
);
2243 Header
->FieldValue
[StrSize
-1] = '\0';
2245 HttpIoHeader
->HeaderCount
++;
2248 // Update an existing one.
2250 StrSize
= AsciiStrSize (FieldValue
);
2251 NewFieldValue
= AllocatePool (StrSize
);
2252 if (NewFieldValue
== NULL
) {
2253 return EFI_OUT_OF_RESOURCES
;
2256 CopyMem (NewFieldValue
, FieldValue
, StrSize
);
2257 NewFieldValue
[StrSize
-1] = '\0';
2259 if (Header
->FieldValue
!= NULL
) {
2260 FreePool (Header
->FieldValue
);
2263 Header
->FieldValue
= NewFieldValue
;