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 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at<BR>
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include <Protocol/Http.h>
24 Decode a percent-encoded URI component to the ASCII character.
26 Decode the input component in Buffer according to RFC 3986. The caller is responsible to make
27 sure ResultBuffer points to a buffer with size equal or greater than ((AsciiStrSize (Buffer))
30 @param[in] Buffer The pointer to a percent-encoded URI component.
31 @param[in] BufferLength Length of Buffer in bytes.
32 @param[out] ResultBuffer Point to the buffer to store the decode result.
33 @param[out] ResultLength Length of decoded string in ResultBuffer in bytes.
35 @retval EFI_SUCCESS Successfully decoded the URI.
36 @retval EFI_INVALID_PARAMETER Buffer is not a valid percent-encoded string.
43 IN UINT32 BufferLength
,
44 OUT CHAR8
*ResultBuffer
,
45 OUT UINT32
*ResultLength
49 Create a URL parser for the input URL string.
51 This function will parse and dereference the input HTTP URL into it components. The original
52 content of the URL won't be modified and the result will be returned in UrlParser, which can
53 be used in other functions like NetHttpUrlGetHostName(). It is the caller's responsibility to
54 free the buffer returned in *UrlParser by HttpUrlFreeParser().
56 @param[in] Url The pointer to a HTTP URL string.
57 @param[in] Length Length of Url in bytes.
58 @param[in] IsConnectMethod Whether the Url is used in HTTP CONNECT method or not.
59 @param[out] UrlParser Pointer to the returned buffer to store the parse result.
61 @retval EFI_SUCCESS Successfully dereferenced the HTTP URL.
62 @retval EFI_INVALID_PARAMETER UrlParser is NULL or Url is not a valid HTTP URL.
63 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
71 IN BOOLEAN IsConnectMethod
,
76 Get the Hostname from a HTTP URL.
78 This function will return the HostName according to the Url and previous parse result ,and
79 it is the caller's responsibility to free the buffer returned in *HostName.
81 @param[in] Url The pointer to a HTTP URL string.
82 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
83 @param[out] HostName Pointer to a buffer to store the HostName.
85 @retval EFI_SUCCESS Successfully get the required component.
86 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.
87 @retval EFI_NOT_FOUND No hostName component in the URL.
88 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
100 Get the IPv4 address from a HTTP URL.
102 This function will return the IPv4 address according to the Url and previous parse result.
104 @param[in] Url The pointer to a HTTP URL string.
105 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
106 @param[out] Ip4Address Pointer to a buffer to store the IP address.
108 @retval EFI_SUCCESS Successfully get the required component.
109 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip4Address is NULL or UrlParser is invalid.
110 @retval EFI_NOT_FOUND No IPv4 address component in the URL.
111 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
119 OUT EFI_IPv4_ADDRESS
*Ip4Address
123 Get the IPv6 address from a HTTP URL.
125 This function will return the IPv6 address according to the Url and previous parse result.
127 @param[in] Url The pointer to a HTTP URL string.
128 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
129 @param[out] Ip6Address Pointer to a buffer to store the IP address.
131 @retval EFI_SUCCESS Successfully get the required component.
132 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip6Address is NULL or UrlParser is invalid.
133 @retval EFI_NOT_FOUND No IPv6 address component in the URL.
134 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
142 OUT EFI_IPv6_ADDRESS
*Ip6Address
146 Get the port number from a HTTP URL.
148 This function will return the port number according to the Url and previous parse result.
150 @param[in] Url The pointer to a HTTP URL string.
151 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
152 @param[out] Port Pointer to a buffer to store the port number.
154 @retval EFI_SUCCESS Successfully get the required component.
155 @retval EFI_INVALID_PARAMETER Uri is NULL or Port is NULL or UrlParser is invalid.
156 @retval EFI_NOT_FOUND No port number in the URL.
157 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
169 Get the Path from a HTTP URL.
171 This function will return the Path according to the Url and previous parse result,and
172 it is the caller's responsibility to free the buffer returned in *Path.
174 @param[in] Url The pointer to a HTTP URL string.
175 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().
176 @param[out] Path Pointer to a buffer to store the Path.
178 @retval EFI_SUCCESS Successfully get the required component.
179 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.
180 @retval EFI_NOT_FOUND No hostName component in the URL.
181 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
193 Release the resource of the URL parser.
195 @param[in] UrlParser Pointer to the parser.
205 // HTTP body parser interface.
210 // Part of entity data.
211 // Length of entity body in Data.
213 BodyParseEventOnData
,
215 // End of message body.
216 // Length is 0 and Data points to next byte after the end of the message.
218 BodyParseEventOnComplete
219 } HTTP_BODY_PARSE_EVENT
;
222 A callback function to intercept events during message parser.
224 This function will be invoked during HttpParseMessageBody() with various events type. An error
225 return status of the callback function will cause the HttpParseMessageBody() aborted.
227 @param[in] EventType Event type of this callback call.
228 @param[in] Data A pointer to data buffer.
229 @param[in] Length Length in bytes of the Data.
230 @param[in] Context Callback context set by HttpInitMsgParser().
232 @retval EFI_SUCCESS Continue to parser the message body.
233 @retval Others Abort the parse.
238 (EFIAPI
*HTTP_BODY_PARSER_CALLBACK
) (
239 IN HTTP_BODY_PARSE_EVENT EventType
,
246 Initialize a HTTP message-body parser.
248 This function will create and initialize a HTTP message parser according to caller provided HTTP message
249 header information. It is the caller's responsibility to free the buffer returned in *UrlParser by HttpFreeMsgParser().
251 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.
252 @param[in] StatusCode Response status code returned by the remote host.
253 @param[in] HeaderCount Number of HTTP header structures in Headers.
254 @param[in] Headers Array containing list of HTTP headers.
255 @param[in] Callback Callback function that is invoked when parsing the HTTP message-body,
256 set to NULL to ignore all events.
257 @param[in] Context Pointer to the context that will be passed to Callback.
258 @param[out] MsgParser Pointer to the returned buffer to store the message parser.
260 @retval EFI_SUCCESS Successfully initialized the parser.
261 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
262 @retval EFI_INVALID_PARAMETER MsgParser is NULL or HeaderCount is not NULL but Headers is NULL.
263 @retval Others Failed to initialize the parser.
269 IN EFI_HTTP_METHOD Method
,
270 IN EFI_HTTP_STATUS_CODE StatusCode
,
271 IN UINTN HeaderCount
,
272 IN EFI_HTTP_HEADER
*Headers
,
273 IN HTTP_BODY_PARSER_CALLBACK Callback
,
281 Parse BodyLength of message-body. This function can be called repeatedly to parse the message-body partially.
283 @param[in, out] MsgParser Pointer to the message parser.
284 @param[in] BodyLength Length in bytes of the Body.
285 @param[in] Body Pointer to the buffer of the message-body to be parsed.
287 @retval EFI_SUCCESS Successfully parse the message-body.
288 @retval EFI_INVALID_PARAMETER MsgParser is NULL or Body is NULL or BodyLength is 0.
289 @retval EFI_ABORTED Operation aborted.
290 @retval Other Error happened while parsing message body.
295 HttpParseMessageBody (
296 IN OUT VOID
*MsgParser
,
302 Check whether the message-body is complete or not.
304 @param[in] MsgParser Pointer to the message parser.
306 @retval TRUE Message-body is complete.
307 @retval FALSE Message-body is not complete.
312 HttpIsMessageComplete (
317 Get the content length of the entity.
319 Note that in trunk transfer, the entity length is not valid until the whole message body is received.
321 @param[in] MsgParser Pointer to the message parser.
322 @param[out] ContentLength Pointer to store the length of the entity.
324 @retval EFI_SUCCESS Successfully to get the entity length.
325 @retval EFI_NOT_READY Entity length is not valid yet.
326 @retval EFI_INVALID_PARAMETER MsgParser is NULL or ContentLength is NULL.
331 HttpGetEntityLength (
333 OUT UINTN
*ContentLength
337 Release the resource of the message parser.
339 @param[in] MsgParser Pointer to the message parser.
350 Find a specified header field according to the field name.
352 @param[in] HeaderCount Number of HTTP header structures in Headers list.
353 @param[in] Headers Array containing list of HTTP headers.
354 @param[in] FieldName Null terminated string which describes a field name.
356 @return Pointer to the found header or NULL.
362 IN UINTN HeaderCount
,
363 IN EFI_HTTP_HEADER
*Headers
,
368 Set FieldName and FieldValue into specified HttpHeader.
370 @param[in,out] HttpHeader Specified HttpHeader.
371 @param[in] FieldName FieldName of this HttpHeader, a NULL terminated ASCII string.
372 @param[in] FieldValue FieldValue of this HttpHeader, a NULL terminated ASCII string.
375 @retval EFI_SUCCESS The FieldName and FieldValue are set into HttpHeader successfully.
376 @retval EFI_INVALID_PARAMETER The parameter is invalid.
377 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
382 HttpSetFieldNameAndValue (
383 IN OUT EFI_HTTP_HEADER
*HttpHeader
,
384 IN CONST CHAR8
*FieldName
,
385 IN CONST CHAR8
*FieldValue
389 Get one key/value header pair from the raw string.
391 @param[in] String Pointer to the raw string.
392 @param[out] FieldName Points directly to field name within 'HttpHeader'.
393 @param[out] FieldValue Points directly to field value within 'HttpHeader'.
395 @return Pointer to the next raw string.
396 @return NULL if no key/value header pair from this raw string.
401 HttpGetFieldNameAndValue (
403 OUT CHAR8
**FieldName
,
404 OUT CHAR8
**FieldValue
408 Free existing HeaderFields.
410 @param[in] HeaderFields Pointer to array of key/value header pairs waiting for free.
411 @param[in] FieldCount The number of header pairs in HeaderFields.
416 HttpFreeHeaderFields (
417 IN EFI_HTTP_HEADER
*HeaderFields
,
422 Generate HTTP request message.
424 This function will allocate memory for the whole HTTP message and generate a
425 well formatted HTTP Request message in it, include the Request-Line, header
426 fields and also the message body. It is the caller's responsibility to free
427 the buffer returned in *RequestMsg.
429 @param[in] Message Pointer to the EFI_HTTP_MESSAGE structure which
430 contains the required information to generate
431 the HTTP request message.
432 @param[in] Url The URL of a remote host.
433 @param[out] RequestMsg Pointer to the created HTTP request message.
434 NULL if any error occured.
435 @param[out] RequestMsgSize Size of the RequestMsg (in bytes).
437 @retval EFI_SUCCESS If HTTP request string was created successfully.
438 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
439 @retval EFI_INVALID_PARAMETER The input arguments are invalid.
444 HttpGenRequestMessage (
445 IN CONST EFI_HTTP_MESSAGE
*Message
,
447 OUT CHAR8
**RequestMsg
,
448 OUT UINTN
*RequestMsgSize
452 Translate the status code in HTTP message to EFI_HTTP_STATUS_CODE defined
453 in UEFI 2.5 specification.
455 @param[in] StatusCode The status code value in HTTP message.
457 @return Value defined in EFI_HTTP_STATUS_CODE .
462 HttpMappingToStatusCode (
467 Check whether header field called FieldName is in DeleteList.
469 @param[in] DeleteList Pointer to array of key/value header pairs.
470 @param[in] DeleteCount The number of header pairs.
471 @param[in] FieldName Pointer to header field's name.
473 @return TRUE if FieldName is not in DeleteList, that means this header field is valid.
474 @return FALSE if FieldName is in DeleteList, that means this header field is invalid.
479 HttpIsValidHttpHeader (
480 IN CHAR8
*DeleteList
[],
481 IN UINTN DeleteCount
,