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