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 - 2018, Intel Corporation. All rights reserved.<BR>
6 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include <Protocol/Http.h>
18 Decode a percent-encoded URI component to the ASCII character.
20 Decode the input component in Buffer according to RFC 3986. The caller is responsible to make
21 sure ResultBuffer points to a buffer with size equal or greater than ((AsciiStrSize (Buffer))
24 @param[in] Buffer The pointer to a percent-encoded URI component.
25 @param[in] BufferLength Length of Buffer in bytes.
26 @param[out] ResultBuffer Point to the buffer to store the decode result.
27 @param[out] ResultLength Length of decoded string in ResultBuffer in bytes.
29 @retval EFI_SUCCESS Successfully decoded the URI.
30 @retval EFI_INVALID_PARAMETER Buffer is not a valid percent-encoded string.
37 IN UINT32 BufferLength
,
38 OUT CHAR8
*ResultBuffer
,
39 OUT UINT32
*ResultLength
43 Create a URL parser for the input URL string.
45 This function will parse and dereference the input HTTP URL into it components. The original
46 content of the URL won't be modified and the result will be returned in UrlParser, which can
47 be used in other functions like NetHttpUrlGetHostName(). It is the caller's responsibility to
48 free the buffer returned in *UrlParser by HttpUrlFreeParser().
50 @param[in] Url The pointer to a HTTP URL string.
51 @param[in] Length Length of Url in bytes.
52 @param[in] IsConnectMethod Whether the Url is used in HTTP CONNECT method or not.
53 @param[out] UrlParser Pointer to the returned buffer to store the parse result.
55 @retval EFI_SUCCESS Successfully dereferenced the HTTP URL.
56 @retval EFI_INVALID_PARAMETER UrlParser is NULL or Url is not a valid HTTP URL.
57 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
65 IN BOOLEAN IsConnectMethod
,
70 Get the Hostname from a HTTP URL.
72 This function will return the HostName according to the Url and previous parse result ,and
73 it is the caller's responsibility to free the buffer returned in *HostName.
75 @param[in] Url The pointer to a HTTP URL string.
76 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
77 @param[out] HostName Pointer to a buffer to store the HostName.
79 @retval EFI_SUCCESS Successfully get the required component.
80 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.
81 @retval EFI_NOT_FOUND No hostName component in the URL.
82 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
94 Get the IPv4 address from a HTTP URL.
96 This function will return the IPv4 address according to the Url and previous parse result.
98 @param[in] Url The pointer to a HTTP URL string.
99 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
100 @param[out] Ip4Address Pointer to a buffer to store the IP address.
102 @retval EFI_SUCCESS Successfully get the required component.
103 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip4Address is NULL or UrlParser is invalid.
104 @retval EFI_NOT_FOUND No IPv4 address component in the URL.
105 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
113 OUT EFI_IPv4_ADDRESS
*Ip4Address
117 Get the IPv6 address from a HTTP URL.
119 This function will return the IPv6 address according to the Url and previous parse result.
121 @param[in] Url The pointer to a HTTP URL string.
122 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
123 @param[out] Ip6Address Pointer to a buffer to store the IP address.
125 @retval EFI_SUCCESS Successfully get the required component.
126 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip6Address is NULL or UrlParser is invalid.
127 @retval EFI_NOT_FOUND No IPv6 address component in the URL.
128 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
136 OUT EFI_IPv6_ADDRESS
*Ip6Address
140 Get the port number from a HTTP URL.
142 This function will return the port number according to the Url and previous parse result.
144 @param[in] Url The pointer to a HTTP URL string.
145 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
146 @param[out] Port Pointer to a buffer to store the port number.
148 @retval EFI_SUCCESS Successfully get the required component.
149 @retval EFI_INVALID_PARAMETER Uri is NULL or Port is NULL or UrlParser is invalid.
150 @retval EFI_NOT_FOUND No port number in the URL.
151 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
163 Get the Path from a HTTP URL.
165 This function will return the Path according to the Url and previous parse result,and
166 it is the caller's responsibility to free the buffer returned in *Path.
168 @param[in] Url The pointer to a HTTP URL string.
169 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
170 @param[out] Path Pointer to a buffer to store the Path.
172 @retval EFI_SUCCESS Successfully get the required component.
173 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.
174 @retval EFI_NOT_FOUND No hostName component in the URL.
175 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
187 Release the resource of the URL parser.
189 @param[in] UrlParser Pointer to the parser.
199 // HTTP body parser interface.
204 // Part of entity data.
205 // Length of entity body in Data.
207 BodyParseEventOnData
,
209 // End of message body.
210 // Length is 0 and Data points to next byte after the end of the message.
212 BodyParseEventOnComplete
213 } HTTP_BODY_PARSE_EVENT
;
216 A callback function to intercept events during message parser.
218 This function will be invoked during HttpParseMessageBody() with various events type. An error
219 return status of the callback function will cause the HttpParseMessageBody() aborted.
221 @param[in] EventType Event type of this callback call.
222 @param[in] Data A pointer to data buffer.
223 @param[in] Length Length in bytes of the Data.
224 @param[in] Context Callback context set by HttpInitMsgParser().
226 @retval EFI_SUCCESS Continue to parser the message body.
227 @retval Others Abort the parse.
232 (EFIAPI
*HTTP_BODY_PARSER_CALLBACK
) (
233 IN HTTP_BODY_PARSE_EVENT EventType
,
240 Initialize a HTTP message-body parser.
242 This function will create and initialize a HTTP message parser according to caller provided HTTP message
243 header information. It is the caller's responsibility to free the buffer returned in *UrlParser by HttpFreeMsgParser().
245 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.
246 @param[in] StatusCode Response status code returned by the remote host.
247 @param[in] HeaderCount Number of HTTP header structures in Headers.
248 @param[in] Headers Array containing list of HTTP headers.
249 @param[in] Callback Callback function that is invoked when parsing the HTTP message-body,
250 set to NULL to ignore all events.
251 @param[in] Context Pointer to the context that will be passed to Callback.
252 @param[out] MsgParser Pointer to the returned buffer to store the message parser.
254 @retval EFI_SUCCESS Successfully initialized the parser.
255 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
256 @retval EFI_INVALID_PARAMETER MsgParser is NULL or HeaderCount is not NULL but Headers is NULL.
257 @retval Others Failed to initialize the parser.
263 IN EFI_HTTP_METHOD Method
,
264 IN EFI_HTTP_STATUS_CODE StatusCode
,
265 IN UINTN HeaderCount
,
266 IN EFI_HTTP_HEADER
*Headers
,
267 IN HTTP_BODY_PARSER_CALLBACK Callback
,
275 Parse BodyLength of message-body. This function can be called repeatedly to parse the message-body partially.
277 @param[in, out] MsgParser Pointer to the message parser.
278 @param[in] BodyLength Length in bytes of the Body.
279 @param[in] Body Pointer to the buffer of the message-body to be parsed.
281 @retval EFI_SUCCESS Successfully parse the message-body.
282 @retval EFI_INVALID_PARAMETER MsgParser is NULL or Body is NULL or BodyLength is 0.
283 @retval EFI_ABORTED Operation aborted.
284 @retval Other Error happened while parsing message body.
289 HttpParseMessageBody (
290 IN OUT VOID
*MsgParser
,
296 Check whether the message-body is complete or not.
298 @param[in] MsgParser Pointer to the message parser.
300 @retval TRUE Message-body is complete.
301 @retval FALSE Message-body is not complete.
306 HttpIsMessageComplete (
311 Get the content length of the entity.
313 Note that in trunk transfer, the entity length is not valid until the whole message body is received.
315 @param[in] MsgParser Pointer to the message parser.
316 @param[out] ContentLength Pointer to store the length of the entity.
318 @retval EFI_SUCCESS Successfully to get the entity length.
319 @retval EFI_NOT_READY Entity length is not valid yet.
320 @retval EFI_INVALID_PARAMETER MsgParser is NULL or ContentLength is NULL.
325 HttpGetEntityLength (
327 OUT UINTN
*ContentLength
331 Release the resource of the message parser.
333 @param[in] MsgParser Pointer to the message parser.
344 Find a specified header field according to the field name.
346 @param[in] HeaderCount Number of HTTP header structures in Headers list.
347 @param[in] Headers Array containing list of HTTP headers.
348 @param[in] FieldName Null terminated string which describes a field name.
350 @return Pointer to the found header or NULL.
356 IN UINTN HeaderCount
,
357 IN EFI_HTTP_HEADER
*Headers
,
362 Set FieldName and FieldValue into specified HttpHeader.
364 @param[in,out] HttpHeader Specified HttpHeader.
365 @param[in] FieldName FieldName of this HttpHeader, a NULL terminated ASCII string.
366 @param[in] FieldValue FieldValue of this HttpHeader, a NULL terminated ASCII string.
369 @retval EFI_SUCCESS The FieldName and FieldValue are set into HttpHeader successfully.
370 @retval EFI_INVALID_PARAMETER The parameter is invalid.
371 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
376 HttpSetFieldNameAndValue (
377 IN OUT EFI_HTTP_HEADER
*HttpHeader
,
378 IN CONST CHAR8
*FieldName
,
379 IN CONST CHAR8
*FieldValue
383 Get one key/value header pair from the raw string.
385 @param[in] String Pointer to the raw string.
386 @param[out] FieldName Points directly to field name within 'HttpHeader'.
387 @param[out] FieldValue Points directly to field value within 'HttpHeader'.
389 @return Pointer to the next raw string.
390 @return NULL if no key/value header pair from this raw string.
395 HttpGetFieldNameAndValue (
397 OUT CHAR8
**FieldName
,
398 OUT CHAR8
**FieldValue
402 Free existing HeaderFields.
404 @param[in] HeaderFields Pointer to array of key/value header pairs waiting for free.
405 @param[in] FieldCount The number of header pairs in HeaderFields.
410 HttpFreeHeaderFields (
411 IN EFI_HTTP_HEADER
*HeaderFields
,
416 Generate HTTP request message.
418 This function will allocate memory for the whole HTTP message and generate a
419 well formatted HTTP Request message in it, include the Request-Line, header
420 fields and also the message body. It is the caller's responsibility to free
421 the buffer returned in *RequestMsg.
423 @param[in] Message Pointer to the EFI_HTTP_MESSAGE structure which
424 contains the required information to generate
425 the HTTP request message.
426 @param[in] Url The URL of a remote host.
427 @param[out] RequestMsg Pointer to the created HTTP request message.
428 NULL if any error occured.
429 @param[out] RequestMsgSize Size of the RequestMsg (in bytes).
431 @retval EFI_SUCCESS If HTTP request string was created successfully.
432 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
433 @retval EFI_INVALID_PARAMETER The input arguments are invalid.
438 HttpGenRequestMessage (
439 IN CONST EFI_HTTP_MESSAGE
*Message
,
441 OUT CHAR8
**RequestMsg
,
442 OUT UINTN
*RequestMsgSize
446 Translate the status code in HTTP message to EFI_HTTP_STATUS_CODE defined
447 in UEFI 2.5 specification.
449 @param[in] StatusCode The status code value in HTTP message.
451 @return Value defined in EFI_HTTP_STATUS_CODE .
456 HttpMappingToStatusCode (
461 Check whether header field called FieldName is in DeleteList.
463 @param[in] DeleteList Pointer to array of key/value header pairs.
464 @param[in] DeleteCount The number of header pairs.
465 @param[in] FieldName Pointer to header field's name.
467 @return TRUE if FieldName is not in DeleteList, that means this header field is valid.
468 @return FALSE if FieldName is in DeleteList, that means this header field is invalid.
473 HttpIsValidHttpHeader (
474 IN CHAR8
*DeleteList
[],
475 IN UINTN DeleteCount
,