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