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