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