]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/Include/Library/HttpLib.h
NetworkPkg/Library: Fix various typos
[mirror_edk2.git] / NetworkPkg / Include / Library / HttpLib.h
CommitLineData
f9a14916
JW
1/** @file\r
2 This library is used to share code between UEFI network stack modules.\r
3 It provides the helper routines to parse the HTTP message byte stream.\r
4\r
d1102dba 5Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>\r
558b99a6 6(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
9d510e61 7SPDX-License-Identifier: BSD-2-Clause-Patent\r
f9a14916
JW
8\r
9**/\r
10\r
11#ifndef _HTTP_LIB_H_\r
12#define _HTTP_LIB_H_\r
13\r
14#include <Protocol/Http.h>\r
15\r
558b99a6 16\r
f9a14916
JW
17/**\r
18 Decode a percent-encoded URI component to the ASCII character.\r
d1102dba
LG
19\r
20 Decode the input component in Buffer according to RFC 3986. The caller is responsible to make\r
f9a14916 21 sure ResultBuffer points to a buffer with size equal or greater than ((AsciiStrSize (Buffer))\r
d1102dba 22 in bytes.\r
f9a14916
JW
23\r
24 @param[in] Buffer The pointer to a percent-encoded URI component.\r
25 @param[in] BufferLength Length of Buffer in bytes.\r
26 @param[out] ResultBuffer Point to the buffer to store the decode result.\r
27 @param[out] ResultLength Length of decoded string in ResultBuffer in bytes.\r
28\r
29 @retval EFI_SUCCESS Successfully decoded the URI.\r
30 @retval EFI_INVALID_PARAMETER Buffer is not a valid percent-encoded string.\r
d1102dba 31\r
f9a14916
JW
32**/\r
33EFI_STATUS\r
34EFIAPI\r
35UriPercentDecode (\r
36 IN CHAR8 *Buffer,\r
37 IN UINT32 BufferLength,\r
38 OUT CHAR8 *ResultBuffer,\r
39 OUT UINT32 *ResultLength\r
40 );\r
41\r
42/**\r
43 Create a URL parser for the input URL string.\r
44\r
45 This function will parse and dereference the input HTTP URL into it components. The original\r
46 content of the URL won't be modified and the result will be returned in UrlParser, which can\r
47 be used in other functions like NetHttpUrlGetHostName(). It is the caller's responsibility to\r
48 free the buffer returned in *UrlParser by HttpUrlFreeParser().\r
49\r
50 @param[in] Url The pointer to a HTTP URL string.\r
51 @param[in] Length Length of Url in bytes.\r
52 @param[in] IsConnectMethod Whether the Url is used in HTTP CONNECT method or not.\r
53 @param[out] UrlParser Pointer to the returned buffer to store the parse result.\r
54\r
55 @retval EFI_SUCCESS Successfully dereferenced the HTTP URL.\r
56 @retval EFI_INVALID_PARAMETER UrlParser is NULL or Url is not a valid HTTP URL.\r
57 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r
d1102dba 58\r
f9a14916
JW
59**/\r
60EFI_STATUS\r
61EFIAPI\r
62HttpParseUrl (\r
63 IN CHAR8 *Url,\r
64 IN UINT32 Length,\r
65 IN BOOLEAN IsConnectMethod,\r
66 OUT VOID **UrlParser\r
67 );\r
68\r
69/**\r
70 Get the Hostname from a HTTP URL.\r
71\r
72 This function will return the HostName according to the Url and previous parse result ,and\r
73 it is the caller's responsibility to free the buffer returned in *HostName.\r
74\r
75 @param[in] Url The pointer to a HTTP URL string.\r
76 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().\r
77 @param[out] HostName Pointer to a buffer to store the HostName.\r
78\r
79 @retval EFI_SUCCESS Successfully get the required component.\r
80 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.\r
81 @retval EFI_NOT_FOUND No hostName component in the URL.\r
82 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r
d1102dba 83\r
f9a14916
JW
84**/\r
85EFI_STATUS\r
86EFIAPI\r
87HttpUrlGetHostName (\r
88 IN CHAR8 *Url,\r
89 IN VOID *UrlParser,\r
90 OUT CHAR8 **HostName\r
91 );\r
92\r
93/**\r
94 Get the IPv4 address from a HTTP URL.\r
95\r
96 This function will return the IPv4 address according to the Url and previous parse result.\r
97\r
98 @param[in] Url The pointer to a HTTP URL string.\r
99 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().\r
100 @param[out] Ip4Address Pointer to a buffer to store the IP address.\r
101\r
102 @retval EFI_SUCCESS Successfully get the required component.\r
103 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip4Address is NULL or UrlParser is invalid.\r
104 @retval EFI_NOT_FOUND No IPv4 address component in the URL.\r
105 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r
d1102dba 106\r
f9a14916
JW
107**/\r
108EFI_STATUS\r
109EFIAPI\r
110HttpUrlGetIp4 (\r
111 IN CHAR8 *Url,\r
112 IN VOID *UrlParser,\r
113 OUT EFI_IPv4_ADDRESS *Ip4Address\r
114 );\r
115\r
116/**\r
117 Get the IPv6 address from a HTTP URL.\r
118\r
119 This function will return the IPv6 address according to the Url and previous parse result.\r
120\r
121 @param[in] Url The pointer to a HTTP URL string.\r
122 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().\r
123 @param[out] Ip6Address Pointer to a buffer to store the IP address.\r
124\r
125 @retval EFI_SUCCESS Successfully get the required component.\r
126 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip6Address is NULL or UrlParser is invalid.\r
127 @retval EFI_NOT_FOUND No IPv6 address component in the URL.\r
128 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r
d1102dba 129\r
f9a14916
JW
130**/\r
131EFI_STATUS\r
132EFIAPI\r
133HttpUrlGetIp6 (\r
134 IN CHAR8 *Url,\r
135 IN VOID *UrlParser,\r
136 OUT EFI_IPv6_ADDRESS *Ip6Address\r
137 );\r
138\r
139/**\r
140 Get the port number from a HTTP URL.\r
141\r
142 This function will return the port number according to the Url and previous parse result.\r
143\r
144 @param[in] Url The pointer to a HTTP URL string.\r
145 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().\r
146 @param[out] Port Pointer to a buffer to store the port number.\r
147\r
148 @retval EFI_SUCCESS Successfully get the required component.\r
149 @retval EFI_INVALID_PARAMETER Uri is NULL or Port is NULL or UrlParser is invalid.\r
150 @retval EFI_NOT_FOUND No port number in the URL.\r
151 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r
d1102dba 152\r
f9a14916
JW
153**/\r
154EFI_STATUS\r
155EFIAPI\r
156HttpUrlGetPort (\r
157 IN CHAR8 *Url,\r
158 IN VOID *UrlParser,\r
159 OUT UINT16 *Port\r
160 );\r
161\r
6796629d
FS
162/**\r
163 Get the Path from a HTTP URL.\r
164\r
165 This function will return the Path according to the Url and previous parse result,and\r
166 it is the caller's responsibility to free the buffer returned in *Path.\r
167\r
168 @param[in] Url The pointer to a HTTP URL string.\r
169 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().\r
170 @param[out] Path Pointer to a buffer to store the Path.\r
171\r
172 @retval EFI_SUCCESS Successfully get the required component.\r
173 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.\r
174 @retval EFI_NOT_FOUND No hostName component in the URL.\r
175 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r
d1102dba 176\r
6796629d
FS
177**/\r
178EFI_STATUS\r
179EFIAPI\r
180HttpUrlGetPath (\r
181 IN CHAR8 *Url,\r
182 IN VOID *UrlParser,\r
183 OUT CHAR8 **Path\r
184 );\r
185\r
f9a14916
JW
186/**\r
187 Release the resource of the URL parser.\r
188\r
189 @param[in] UrlParser Pointer to the parser.\r
d1102dba 190\r
f9a14916
JW
191**/\r
192VOID\r
193EFIAPI\r
194HttpUrlFreeParser (\r
195 IN VOID *UrlParser\r
196 );\r
197\r
198//\r
199// HTTP body parser interface.\r
200//\r
201\r
202typedef enum {\r
203 //\r
204 // Part of entity data.\r
205 // Length of entity body in Data.\r
206 //\r
207 BodyParseEventOnData,\r
208 //\r
209 // End of message body.\r
210 // Length is 0 and Data points to next byte after the end of the message.\r
211 //\r
212 BodyParseEventOnComplete\r
213} HTTP_BODY_PARSE_EVENT;\r
214\r
215/**\r
216 A callback function to intercept events during message parser.\r
217\r
218 This function will be invoked during HttpParseMessageBody() with various events type. An error\r
219 return status of the callback function will cause the HttpParseMessageBody() aborted.\r
220\r
221 @param[in] EventType Event type of this callback call.\r
222 @param[in] Data A pointer to data buffer.\r
223 @param[in] Length Length in bytes of the Data.\r
224 @param[in] Context Callback context set by HttpInitMsgParser().\r
225\r
226 @retval EFI_SUCCESS Continue to parser the message body.\r
227 @retval Others Abort the parse.\r
d1102dba 228\r
f9a14916
JW
229**/\r
230typedef\r
231EFI_STATUS\r
232(EFIAPI *HTTP_BODY_PARSER_CALLBACK) (\r
233 IN HTTP_BODY_PARSE_EVENT EventType,\r
234 IN CHAR8 *Data,\r
235 IN UINTN Length,\r
236 IN VOID *Context\r
237);\r
238\r
239/**\r
240 Initialize a HTTP message-body parser.\r
241\r
242 This function will create and initialize a HTTP message parser according to caller provided HTTP message\r
243 header information. It is the caller's responsibility to free the buffer returned in *UrlParser by HttpFreeMsgParser().\r
244\r
245 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.\r
246 @param[in] StatusCode Response status code returned by the remote host.\r
247 @param[in] HeaderCount Number of HTTP header structures in Headers.\r
248 @param[in] Headers Array containing list of HTTP headers.\r
249 @param[in] Callback Callback function that is invoked when parsing the HTTP message-body,\r
250 set to NULL to ignore all events.\r
251 @param[in] Context Pointer to the context that will be passed to Callback.\r
252 @param[out] MsgParser Pointer to the returned buffer to store the message parser.\r
253\r
254 @retval EFI_SUCCESS Successfully initialized the parser.\r
255 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r
256 @retval EFI_INVALID_PARAMETER MsgParser is NULL or HeaderCount is not NULL but Headers is NULL.\r
257 @retval Others Failed to initialize the parser.\r
258\r
259**/\r
260EFI_STATUS\r
261EFIAPI\r
262HttpInitMsgParser (\r
263 IN EFI_HTTP_METHOD Method,\r
264 IN EFI_HTTP_STATUS_CODE StatusCode,\r
265 IN UINTN HeaderCount,\r
266 IN EFI_HTTP_HEADER *Headers,\r
267 IN HTTP_BODY_PARSER_CALLBACK Callback,\r
268 IN VOID *Context,\r
269 OUT VOID **MsgParser\r
270 );\r
271\r
272/**\r
273 Parse message body.\r
274\r
275 Parse BodyLength of message-body. This function can be called repeatedly to parse the message-body partially.\r
276\r
277 @param[in, out] MsgParser Pointer to the message parser.\r
278 @param[in] BodyLength Length in bytes of the Body.\r
279 @param[in] Body Pointer to the buffer of the message-body to be parsed.\r
280\r
281 @retval EFI_SUCCESS Successfully parse the message-body.\r
282 @retval EFI_INVALID_PARAMETER MsgParser is NULL or Body is NULL or BodyLength is 0.\r
4a6f440f
JW
283 @retval EFI_ABORTED Operation aborted.\r
284 @retval Other Error happened while parsing message body.\r
d1102dba 285\r
f9a14916
JW
286**/\r
287EFI_STATUS\r
288EFIAPI\r
289HttpParseMessageBody (\r
290 IN OUT VOID *MsgParser,\r
291 IN UINTN BodyLength,\r
292 IN CHAR8 *Body\r
293 );\r
294\r
295/**\r
296 Check whether the message-body is complete or not.\r
297\r
298 @param[in] MsgParser Pointer to the message parser.\r
299\r
300 @retval TRUE Message-body is complete.\r
301 @retval FALSE Message-body is not complete.\r
302\r
303**/\r
304BOOLEAN\r
305EFIAPI\r
306HttpIsMessageComplete (\r
307 IN VOID *MsgParser\r
308 );\r
309\r
310/**\r
311 Get the content length of the entity.\r
312\r
313 Note that in trunk transfer, the entity length is not valid until the whole message body is received.\r
314\r
315 @param[in] MsgParser Pointer to the message parser.\r
316 @param[out] ContentLength Pointer to store the length of the entity.\r
317\r
318 @retval EFI_SUCCESS Successfully to get the entity length.\r
319 @retval EFI_NOT_READY Entity length is not valid yet.\r
320 @retval EFI_INVALID_PARAMETER MsgParser is NULL or ContentLength is NULL.\r
d1102dba 321\r
f9a14916
JW
322**/\r
323EFI_STATUS\r
324EFIAPI\r
325HttpGetEntityLength (\r
326 IN VOID *MsgParser,\r
327 OUT UINTN *ContentLength\r
328 );\r
329\r
330/**\r
331 Release the resource of the message parser.\r
332\r
333 @param[in] MsgParser Pointer to the message parser.\r
d1102dba 334\r
f9a14916
JW
335**/\r
336VOID\r
337EFIAPI\r
338HttpFreeMsgParser (\r
339 IN VOID *MsgParser\r
340 );\r
341\r
342\r
558b99a6
GB
343/**\r
344 Find a specified header field according to the field name.\r
345\r
346 @param[in] HeaderCount Number of HTTP header structures in Headers list.\r
347 @param[in] Headers Array containing list of HTTP headers.\r
348 @param[in] FieldName Null terminated string which describes a field name.\r
349\r
350 @return Pointer to the found header or NULL.\r
351\r
352**/\r
353EFI_HTTP_HEADER *\r
354EFIAPI\r
355HttpFindHeader (\r
356 IN UINTN HeaderCount,\r
357 IN EFI_HTTP_HEADER *Headers,\r
358 IN CHAR8 *FieldName\r
359 );\r
360\r
361/**\r
362 Set FieldName and FieldValue into specified HttpHeader.\r
363\r
364 @param[in,out] HttpHeader Specified HttpHeader.\r
365 @param[in] FieldName FieldName of this HttpHeader, a NULL terminated ASCII string.\r
366 @param[in] FieldValue FieldValue of this HttpHeader, a NULL terminated ASCII string.\r
367\r
368\r
369 @retval EFI_SUCCESS The FieldName and FieldValue are set into HttpHeader successfully.\r
0efeec8e 370 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
558b99a6
GB
371 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.\r
372\r
373**/\r
374EFI_STATUS\r
375EFIAPI\r
376HttpSetFieldNameAndValue (\r
377 IN OUT EFI_HTTP_HEADER *HttpHeader,\r
378 IN CONST CHAR8 *FieldName,\r
379 IN CONST CHAR8 *FieldValue\r
380 );\r
381\r
382/**\r
383 Get one key/value header pair from the raw string.\r
384\r
385 @param[in] String Pointer to the raw string.\r
386 @param[out] FieldName Points directly to field name within 'HttpHeader'.\r
387 @param[out] FieldValue Points directly to field value within 'HttpHeader'.\r
388\r
389 @return Pointer to the next raw string.\r
390 @return NULL if no key/value header pair from this raw string.\r
391\r
392**/\r
393CHAR8 *\r
394EFIAPI\r
395HttpGetFieldNameAndValue (\r
396 IN CHAR8 *String,\r
397 OUT CHAR8 **FieldName,\r
398 OUT CHAR8 **FieldValue\r
399 );\r
400\r
401/**\r
402 Free existing HeaderFields.\r
403\r
404 @param[in] HeaderFields Pointer to array of key/value header pairs waiting for free.\r
405 @param[in] FieldCount The number of header pairs in HeaderFields.\r
406\r
407**/\r
408VOID\r
409EFIAPI\r
410HttpFreeHeaderFields (\r
411 IN EFI_HTTP_HEADER *HeaderFields,\r
412 IN UINTN FieldCount\r
413 );\r
414\r
415/**\r
e297a0a4 416 Generate HTTP request message.\r
558b99a6 417\r
e297a0a4
NH
418 This function will allocate memory for the whole HTTP message and generate a\r
419 well formatted HTTP Request message in it, include the Request-Line, header\r
420 fields and also the message body. It is the caller's responsibility to free\r
421 the buffer returned in *RequestMsg.\r
422\r
423 @param[in] Message Pointer to the EFI_HTTP_MESSAGE structure which\r
424 contains the required information to generate\r
425 the HTTP request message.\r
558b99a6 426 @param[in] Url The URL of a remote host.\r
e297a0a4 427 @param[out] RequestMsg Pointer to the created HTTP request message.\r
6deb4baa 428 NULL if any error occurred.\r
e297a0a4 429 @param[out] RequestMsgSize Size of the RequestMsg (in bytes).\r
558b99a6 430\r
0d07e6fb 431 @retval EFI_SUCCESS If HTTP request string was created successfully.\r
558b99a6 432 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.\r
0d07e6fb 433 @retval EFI_INVALID_PARAMETER The input arguments are invalid.\r
558b99a6
GB
434\r
435**/\r
436EFI_STATUS\r
437EFIAPI\r
e297a0a4 438HttpGenRequestMessage (\r
558b99a6
GB
439 IN CONST EFI_HTTP_MESSAGE *Message,\r
440 IN CONST CHAR8 *Url,\r
e297a0a4
NH
441 OUT CHAR8 **RequestMsg,\r
442 OUT UINTN *RequestMsgSize\r
558b99a6
GB
443 );\r
444\r
445/**\r
446 Translate the status code in HTTP message to EFI_HTTP_STATUS_CODE defined\r
447 in UEFI 2.5 specification.\r
448\r
449 @param[in] StatusCode The status code value in HTTP message.\r
450\r
451 @return Value defined in EFI_HTTP_STATUS_CODE .\r
452\r
453**/\r
454EFI_HTTP_STATUS_CODE\r
455EFIAPI\r
456HttpMappingToStatusCode (\r
457 IN UINTN StatusCode\r
458 );\r
459\r
460/**\r
461 Check whether header field called FieldName is in DeleteList.\r
462\r
463 @param[in] DeleteList Pointer to array of key/value header pairs.\r
464 @param[in] DeleteCount The number of header pairs.\r
465 @param[in] FieldName Pointer to header field's name.\r
466\r
467 @return TRUE if FieldName is not in DeleteList, that means this header field is valid.\r
468 @return FALSE if FieldName is in DeleteList, that means this header field is invalid.\r
469\r
470**/\r
471BOOLEAN\r
472EFIAPI\r
473HttpIsValidHttpHeader (\r
474 IN CHAR8 *DeleteList[],\r
475 IN UINTN DeleteCount,\r
476 IN CHAR8 *FieldName\r
477 );\r
478\r
479\r
f9a14916
JW
480#endif\r
481\r