]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Include/Library/HttpLib.h
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / 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 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
11
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.
14
15 **/
16
17 #ifndef _HTTP_LIB_H_
18 #define _HTTP_LIB_H_
19
20 #include <Protocol/Http.h>
21
22
23 /**
24 Decode a percent-encoded URI component to the ASCII character.
25
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))
28 in bytes.
29
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.
34
35 @retval EFI_SUCCESS Successfully decoded the URI.
36 @retval EFI_INVALID_PARAMETER Buffer is not a valid percent-encoded string.
37
38 **/
39 EFI_STATUS
40 EFIAPI
41 UriPercentDecode (
42 IN CHAR8 *Buffer,
43 IN UINT32 BufferLength,
44 OUT CHAR8 *ResultBuffer,
45 OUT UINT32 *ResultLength
46 );
47
48 /**
49 Create a URL parser for the input URL string.
50
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().
55
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.
60
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.
64
65 **/
66 EFI_STATUS
67 EFIAPI
68 HttpParseUrl (
69 IN CHAR8 *Url,
70 IN UINT32 Length,
71 IN BOOLEAN IsConnectMethod,
72 OUT VOID **UrlParser
73 );
74
75 /**
76 Get the Hostname from a HTTP URL.
77
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.
80
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.
84
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.
89
90 **/
91 EFI_STATUS
92 EFIAPI
93 HttpUrlGetHostName (
94 IN CHAR8 *Url,
95 IN VOID *UrlParser,
96 OUT CHAR8 **HostName
97 );
98
99 /**
100 Get the IPv4 address from a HTTP URL.
101
102 This function will return the IPv4 address according to the Url and previous parse result.
103
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.
107
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.
112
113 **/
114 EFI_STATUS
115 EFIAPI
116 HttpUrlGetIp4 (
117 IN CHAR8 *Url,
118 IN VOID *UrlParser,
119 OUT EFI_IPv4_ADDRESS *Ip4Address
120 );
121
122 /**
123 Get the IPv6 address from a HTTP URL.
124
125 This function will return the IPv6 address according to the Url and previous parse result.
126
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.
130
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.
135
136 **/
137 EFI_STATUS
138 EFIAPI
139 HttpUrlGetIp6 (
140 IN CHAR8 *Url,
141 IN VOID *UrlParser,
142 OUT EFI_IPv6_ADDRESS *Ip6Address
143 );
144
145 /**
146 Get the port number from a HTTP URL.
147
148 This function will return the port number according to the Url and previous parse result.
149
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.
153
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.
158
159 **/
160 EFI_STATUS
161 EFIAPI
162 HttpUrlGetPort (
163 IN CHAR8 *Url,
164 IN VOID *UrlParser,
165 OUT UINT16 *Port
166 );
167
168 /**
169 Get the Path from a HTTP URL.
170
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.
173
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.
177
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.
182
183 **/
184 EFI_STATUS
185 EFIAPI
186 HttpUrlGetPath (
187 IN CHAR8 *Url,
188 IN VOID *UrlParser,
189 OUT CHAR8 **Path
190 );
191
192 /**
193 Release the resource of the URL parser.
194
195 @param[in] UrlParser Pointer to the parser.
196
197 **/
198 VOID
199 EFIAPI
200 HttpUrlFreeParser (
201 IN VOID *UrlParser
202 );
203
204 //
205 // HTTP body parser interface.
206 //
207
208 typedef enum {
209 //
210 // Part of entity data.
211 // Length of entity body in Data.
212 //
213 BodyParseEventOnData,
214 //
215 // End of message body.
216 // Length is 0 and Data points to next byte after the end of the message.
217 //
218 BodyParseEventOnComplete
219 } HTTP_BODY_PARSE_EVENT;
220
221 /**
222 A callback function to intercept events during message parser.
223
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.
226
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().
231
232 @retval EFI_SUCCESS Continue to parser the message body.
233 @retval Others Abort the parse.
234
235 **/
236 typedef
237 EFI_STATUS
238 (EFIAPI *HTTP_BODY_PARSER_CALLBACK) (
239 IN HTTP_BODY_PARSE_EVENT EventType,
240 IN CHAR8 *Data,
241 IN UINTN Length,
242 IN VOID *Context
243 );
244
245 /**
246 Initialize a HTTP message-body parser.
247
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().
250
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.
259
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.
264
265 **/
266 EFI_STATUS
267 EFIAPI
268 HttpInitMsgParser (
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,
274 IN VOID *Context,
275 OUT VOID **MsgParser
276 );
277
278 /**
279 Parse message body.
280
281 Parse BodyLength of message-body. This function can be called repeatedly to parse the message-body partially.
282
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.
286
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.
291
292 **/
293 EFI_STATUS
294 EFIAPI
295 HttpParseMessageBody (
296 IN OUT VOID *MsgParser,
297 IN UINTN BodyLength,
298 IN CHAR8 *Body
299 );
300
301 /**
302 Check whether the message-body is complete or not.
303
304 @param[in] MsgParser Pointer to the message parser.
305
306 @retval TRUE Message-body is complete.
307 @retval FALSE Message-body is not complete.
308
309 **/
310 BOOLEAN
311 EFIAPI
312 HttpIsMessageComplete (
313 IN VOID *MsgParser
314 );
315
316 /**
317 Get the content length of the entity.
318
319 Note that in trunk transfer, the entity length is not valid until the whole message body is received.
320
321 @param[in] MsgParser Pointer to the message parser.
322 @param[out] ContentLength Pointer to store the length of the entity.
323
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.
327
328 **/
329 EFI_STATUS
330 EFIAPI
331 HttpGetEntityLength (
332 IN VOID *MsgParser,
333 OUT UINTN *ContentLength
334 );
335
336 /**
337 Release the resource of the message parser.
338
339 @param[in] MsgParser Pointer to the message parser.
340
341 **/
342 VOID
343 EFIAPI
344 HttpFreeMsgParser (
345 IN VOID *MsgParser
346 );
347
348
349 /**
350 Find a specified header field according to the field name.
351
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.
355
356 @return Pointer to the found header or NULL.
357
358 **/
359 EFI_HTTP_HEADER *
360 EFIAPI
361 HttpFindHeader (
362 IN UINTN HeaderCount,
363 IN EFI_HTTP_HEADER *Headers,
364 IN CHAR8 *FieldName
365 );
366
367 /**
368 Set FieldName and FieldValue into specified HttpHeader.
369
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.
373
374
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.
378
379 **/
380 EFI_STATUS
381 EFIAPI
382 HttpSetFieldNameAndValue (
383 IN OUT EFI_HTTP_HEADER *HttpHeader,
384 IN CONST CHAR8 *FieldName,
385 IN CONST CHAR8 *FieldValue
386 );
387
388 /**
389 Get one key/value header pair from the raw string.
390
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'.
394
395 @return Pointer to the next raw string.
396 @return NULL if no key/value header pair from this raw string.
397
398 **/
399 CHAR8 *
400 EFIAPI
401 HttpGetFieldNameAndValue (
402 IN CHAR8 *String,
403 OUT CHAR8 **FieldName,
404 OUT CHAR8 **FieldValue
405 );
406
407 /**
408 Free existing HeaderFields.
409
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.
412
413 **/
414 VOID
415 EFIAPI
416 HttpFreeHeaderFields (
417 IN EFI_HTTP_HEADER *HeaderFields,
418 IN UINTN FieldCount
419 );
420
421 /**
422 Generate HTTP request message.
423
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.
428
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).
436
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.
440
441 **/
442 EFI_STATUS
443 EFIAPI
444 HttpGenRequestMessage (
445 IN CONST EFI_HTTP_MESSAGE *Message,
446 IN CONST CHAR8 *Url,
447 OUT CHAR8 **RequestMsg,
448 OUT UINTN *RequestMsgSize
449 );
450
451 /**
452 Translate the status code in HTTP message to EFI_HTTP_STATUS_CODE defined
453 in UEFI 2.5 specification.
454
455 @param[in] StatusCode The status code value in HTTP message.
456
457 @return Value defined in EFI_HTTP_STATUS_CODE .
458
459 **/
460 EFI_HTTP_STATUS_CODE
461 EFIAPI
462 HttpMappingToStatusCode (
463 IN UINTN StatusCode
464 );
465
466 /**
467 Check whether header field called FieldName is in DeleteList.
468
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.
472
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.
475
476 **/
477 BOOLEAN
478 EFIAPI
479 HttpIsValidHttpHeader (
480 IN CHAR8 *DeleteList[],
481 IN UINTN DeleteCount,
482 IN CHAR8 *FieldName
483 );
484
485
486 #endif
487