MdeModulePkg: Add new API HttpUrlGetPath() to HttpLib.h
[mirror_edk2.git] / MdeModulePkg / 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
6796629d 5Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
f9a14916
JW
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at<BR>\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#ifndef _HTTP_LIB_H_\r
17#define _HTTP_LIB_H_\r
18\r
19#include <Protocol/Http.h>\r
20\r
21/**\r
22 Decode a percent-encoded URI component to the ASCII character.\r
23 \r
24 Decode the input component in Buffer according to RFC 3986. The caller is responsible to make \r
25 sure ResultBuffer points to a buffer with size equal or greater than ((AsciiStrSize (Buffer))\r
26 in bytes. \r
27\r
28 @param[in] Buffer The pointer to a percent-encoded URI component.\r
29 @param[in] BufferLength Length of Buffer in bytes.\r
30 @param[out] ResultBuffer Point to the buffer to store the decode result.\r
31 @param[out] ResultLength Length of decoded string in ResultBuffer in bytes.\r
32\r
33 @retval EFI_SUCCESS Successfully decoded the URI.\r
34 @retval EFI_INVALID_PARAMETER Buffer is not a valid percent-encoded string.\r
35 \r
36**/\r
37EFI_STATUS\r
38EFIAPI\r
39UriPercentDecode (\r
40 IN CHAR8 *Buffer,\r
41 IN UINT32 BufferLength,\r
42 OUT CHAR8 *ResultBuffer,\r
43 OUT UINT32 *ResultLength\r
44 );\r
45\r
46/**\r
47 Create a URL parser for the input URL string.\r
48\r
49 This function will parse and dereference the input HTTP URL into it components. The original\r
50 content of the URL won't be modified and the result will be returned in UrlParser, which can\r
51 be used in other functions like NetHttpUrlGetHostName(). It is the caller's responsibility to\r
52 free the buffer returned in *UrlParser by HttpUrlFreeParser().\r
53\r
54 @param[in] Url The pointer to a HTTP URL string.\r
55 @param[in] Length Length of Url in bytes.\r
56 @param[in] IsConnectMethod Whether the Url is used in HTTP CONNECT method or not.\r
57 @param[out] UrlParser Pointer to the returned buffer to store the parse result.\r
58\r
59 @retval EFI_SUCCESS Successfully dereferenced the HTTP URL.\r
60 @retval EFI_INVALID_PARAMETER UrlParser is NULL or Url is not a valid HTTP URL.\r
61 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r
62 \r
63**/\r
64EFI_STATUS\r
65EFIAPI\r
66HttpParseUrl (\r
67 IN CHAR8 *Url,\r
68 IN UINT32 Length,\r
69 IN BOOLEAN IsConnectMethod,\r
70 OUT VOID **UrlParser\r
71 );\r
72\r
73/**\r
74 Get the Hostname from a HTTP URL.\r
75\r
76 This function will return the HostName according to the Url and previous parse result ,and\r
77 it is the caller's responsibility to free the buffer returned in *HostName.\r
78\r
79 @param[in] Url The pointer to a HTTP URL string.\r
80 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().\r
81 @param[out] HostName Pointer to a buffer to store the HostName.\r
82\r
83 @retval EFI_SUCCESS Successfully get the required component.\r
84 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.\r
85 @retval EFI_NOT_FOUND No hostName component in the URL.\r
86 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r
87 \r
88**/\r
89EFI_STATUS\r
90EFIAPI\r
91HttpUrlGetHostName (\r
92 IN CHAR8 *Url,\r
93 IN VOID *UrlParser,\r
94 OUT CHAR8 **HostName\r
95 );\r
96\r
97/**\r
98 Get the IPv4 address from a HTTP URL.\r
99\r
100 This function will return the IPv4 address according to the Url and previous parse result.\r
101\r
102 @param[in] Url The pointer to a HTTP URL string.\r
103 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().\r
104 @param[out] Ip4Address Pointer to a buffer to store the IP address.\r
105\r
106 @retval EFI_SUCCESS Successfully get the required component.\r
107 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip4Address is NULL or UrlParser is invalid.\r
108 @retval EFI_NOT_FOUND No IPv4 address component in the URL.\r
109 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r
110 \r
111**/\r
112EFI_STATUS\r
113EFIAPI\r
114HttpUrlGetIp4 (\r
115 IN CHAR8 *Url,\r
116 IN VOID *UrlParser,\r
117 OUT EFI_IPv4_ADDRESS *Ip4Address\r
118 );\r
119\r
120/**\r
121 Get the IPv6 address from a HTTP URL.\r
122\r
123 This function will return the IPv6 address according to the Url and previous parse result.\r
124\r
125 @param[in] Url The pointer to a HTTP URL string.\r
126 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().\r
127 @param[out] Ip6Address Pointer to a buffer to store the IP address.\r
128\r
129 @retval EFI_SUCCESS Successfully get the required component.\r
130 @retval EFI_INVALID_PARAMETER Uri is NULL or Ip6Address is NULL or UrlParser is invalid.\r
131 @retval EFI_NOT_FOUND No IPv6 address component in the URL.\r
132 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r
133 \r
134**/\r
135EFI_STATUS\r
136EFIAPI\r
137HttpUrlGetIp6 (\r
138 IN CHAR8 *Url,\r
139 IN VOID *UrlParser,\r
140 OUT EFI_IPv6_ADDRESS *Ip6Address\r
141 );\r
142\r
143/**\r
144 Get the port number from a HTTP URL.\r
145\r
146 This function will return the port number according to the Url and previous parse result.\r
147\r
148 @param[in] Url The pointer to a HTTP URL string.\r
149 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().\r
150 @param[out] Port Pointer to a buffer to store the port number.\r
151\r
152 @retval EFI_SUCCESS Successfully get the required component.\r
153 @retval EFI_INVALID_PARAMETER Uri is NULL or Port is NULL or UrlParser is invalid.\r
154 @retval EFI_NOT_FOUND No port number in the URL.\r
155 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r
156 \r
157**/\r
158EFI_STATUS\r
159EFIAPI\r
160HttpUrlGetPort (\r
161 IN CHAR8 *Url,\r
162 IN VOID *UrlParser,\r
163 OUT UINT16 *Port\r
164 );\r
165\r
6796629d
FS
166/**\r
167 Get the Path from a HTTP URL.\r
168\r
169 This function will return the Path according to the Url and previous parse result,and\r
170 it is the caller's responsibility to free the buffer returned in *Path.\r
171\r
172 @param[in] Url The pointer to a HTTP URL string.\r
173 @param[in] UrlParser URL Parse result returned by NetHttpParseUrl().\r
174 @param[out] Path Pointer to a buffer to store the Path.\r
175\r
176 @retval EFI_SUCCESS Successfully get the required component.\r
177 @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid.\r
178 @retval EFI_NOT_FOUND No hostName component in the URL.\r
179 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r
180 \r
181**/\r
182EFI_STATUS\r
183EFIAPI\r
184HttpUrlGetPath (\r
185 IN CHAR8 *Url,\r
186 IN VOID *UrlParser,\r
187 OUT CHAR8 **Path\r
188 );\r
189\r
f9a14916
JW
190/**\r
191 Release the resource of the URL parser.\r
192\r
193 @param[in] UrlParser Pointer to the parser.\r
194 \r
195**/\r
196VOID\r
197EFIAPI\r
198HttpUrlFreeParser (\r
199 IN VOID *UrlParser\r
200 );\r
201\r
202//\r
203// HTTP body parser interface.\r
204//\r
205\r
206typedef enum {\r
207 //\r
208 // Part of entity data.\r
209 // Length of entity body in Data.\r
210 //\r
211 BodyParseEventOnData,\r
212 //\r
213 // End of message body.\r
214 // Length is 0 and Data points to next byte after the end of the message.\r
215 //\r
216 BodyParseEventOnComplete\r
217} HTTP_BODY_PARSE_EVENT;\r
218\r
219/**\r
220 A callback function to intercept events during message parser.\r
221\r
222 This function will be invoked during HttpParseMessageBody() with various events type. An error\r
223 return status of the callback function will cause the HttpParseMessageBody() aborted.\r
224\r
225 @param[in] EventType Event type of this callback call.\r
226 @param[in] Data A pointer to data buffer.\r
227 @param[in] Length Length in bytes of the Data.\r
228 @param[in] Context Callback context set by HttpInitMsgParser().\r
229\r
230 @retval EFI_SUCCESS Continue to parser the message body.\r
231 @retval Others Abort the parse.\r
232 \r
233**/\r
234typedef\r
235EFI_STATUS\r
236(EFIAPI *HTTP_BODY_PARSER_CALLBACK) (\r
237 IN HTTP_BODY_PARSE_EVENT EventType,\r
238 IN CHAR8 *Data,\r
239 IN UINTN Length,\r
240 IN VOID *Context\r
241);\r
242\r
243/**\r
244 Initialize a HTTP message-body parser.\r
245\r
246 This function will create and initialize a HTTP message parser according to caller provided HTTP message\r
247 header information. It is the caller's responsibility to free the buffer returned in *UrlParser by HttpFreeMsgParser().\r
248\r
249 @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message.\r
250 @param[in] StatusCode Response status code returned by the remote host.\r
251 @param[in] HeaderCount Number of HTTP header structures in Headers.\r
252 @param[in] Headers Array containing list of HTTP headers.\r
253 @param[in] Callback Callback function that is invoked when parsing the HTTP message-body,\r
254 set to NULL to ignore all events.\r
255 @param[in] Context Pointer to the context that will be passed to Callback.\r
256 @param[out] MsgParser Pointer to the returned buffer to store the message parser.\r
257\r
258 @retval EFI_SUCCESS Successfully initialized the parser.\r
259 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r
260 @retval EFI_INVALID_PARAMETER MsgParser is NULL or HeaderCount is not NULL but Headers is NULL.\r
261 @retval Others Failed to initialize the parser.\r
262\r
263**/\r
264EFI_STATUS\r
265EFIAPI\r
266HttpInitMsgParser (\r
267 IN EFI_HTTP_METHOD Method,\r
268 IN EFI_HTTP_STATUS_CODE StatusCode,\r
269 IN UINTN HeaderCount,\r
270 IN EFI_HTTP_HEADER *Headers,\r
271 IN HTTP_BODY_PARSER_CALLBACK Callback,\r
272 IN VOID *Context,\r
273 OUT VOID **MsgParser\r
274 );\r
275\r
276/**\r
277 Parse message body.\r
278\r
279 Parse BodyLength of message-body. This function can be called repeatedly to parse the message-body partially.\r
280\r
281 @param[in, out] MsgParser Pointer to the message parser.\r
282 @param[in] BodyLength Length in bytes of the Body.\r
283 @param[in] Body Pointer to the buffer of the message-body to be parsed.\r
284\r
285 @retval EFI_SUCCESS Successfully parse the message-body.\r
286 @retval EFI_INVALID_PARAMETER MsgParser is NULL or Body is NULL or BodyLength is 0.\r
287 @retval Others Operation aborted.\r
288\r
289**/\r
290EFI_STATUS\r
291EFIAPI\r
292HttpParseMessageBody (\r
293 IN OUT VOID *MsgParser,\r
294 IN UINTN BodyLength,\r
295 IN CHAR8 *Body\r
296 );\r
297\r
298/**\r
299 Check whether the message-body is complete or not.\r
300\r
301 @param[in] MsgParser Pointer to the message parser.\r
302\r
303 @retval TRUE Message-body is complete.\r
304 @retval FALSE Message-body is not complete.\r
305\r
306**/\r
307BOOLEAN\r
308EFIAPI\r
309HttpIsMessageComplete (\r
310 IN VOID *MsgParser\r
311 );\r
312\r
313/**\r
314 Get the content length of the entity.\r
315\r
316 Note that in trunk transfer, the entity length is not valid until the whole message body is received.\r
317\r
318 @param[in] MsgParser Pointer to the message parser.\r
319 @param[out] ContentLength Pointer to store the length of the entity.\r
320\r
321 @retval EFI_SUCCESS Successfully to get the entity length.\r
322 @retval EFI_NOT_READY Entity length is not valid yet.\r
323 @retval EFI_INVALID_PARAMETER MsgParser is NULL or ContentLength is NULL.\r
324 \r
325**/\r
326EFI_STATUS\r
327EFIAPI\r
328HttpGetEntityLength (\r
329 IN VOID *MsgParser,\r
330 OUT UINTN *ContentLength\r
331 );\r
332\r
333/**\r
334 Release the resource of the message parser.\r
335\r
336 @param[in] MsgParser Pointer to the message parser.\r
337 \r
338**/\r
339VOID\r
340EFIAPI\r
341HttpFreeMsgParser (\r
342 IN VOID *MsgParser\r
343 );\r
344\r
345\r
346#endif\r
347\r