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 (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 Others Operation aborted. 290 291 **/ 292 EFI_STATUS 293 EFIAPI 294 HttpParseMessageBody ( 295 IN OUT VOID *MsgParser, 296 IN UINTN BodyLength, 297 IN CHAR8 *Body 298 ); 299 300 /** 301 Check whether the message-body is complete or not. 302 303 @param[in] MsgParser Pointer to the message parser. 304 305 @retval TRUE Message-body is complete. 306 @retval FALSE Message-body is not complete. 307 308 **/ 309 BOOLEAN 310 EFIAPI 311 HttpIsMessageComplete ( 312 IN VOID *MsgParser 313 ); 314 315 /** 316 Get the content length of the entity. 317 318 Note that in trunk transfer, the entity length is not valid until the whole message body is received. 319 320 @param[in] MsgParser Pointer to the message parser. 321 @param[out] ContentLength Pointer to store the length of the entity. 322 323 @retval EFI_SUCCESS Successfully to get the entity length. 324 @retval EFI_NOT_READY Entity length is not valid yet. 325 @retval EFI_INVALID_PARAMETER MsgParser is NULL or ContentLength is NULL. 326 327 **/ 328 EFI_STATUS 329 EFIAPI 330 HttpGetEntityLength ( 331 IN VOID *MsgParser, 332 OUT UINTN *ContentLength 333 ); 334 335 /** 336 Release the resource of the message parser. 337 338 @param[in] MsgParser Pointer to the message parser. 339 340 **/ 341 VOID 342 EFIAPI 343 HttpFreeMsgParser ( 344 IN VOID *MsgParser 345 ); 346 347 348 /** 349 Find a specified header field according to the field name. 350 351 @param[in] HeaderCount Number of HTTP header structures in Headers list. 352 @param[in] Headers Array containing list of HTTP headers. 353 @param[in] FieldName Null terminated string which describes a field name. 354 355 @return Pointer to the found header or NULL. 356 357 **/ 358 EFI_HTTP_HEADER * 359 EFIAPI 360 HttpFindHeader ( 361 IN UINTN HeaderCount, 362 IN EFI_HTTP_HEADER *Headers, 363 IN CHAR8 *FieldName 364 ); 365 366 /** 367 Set FieldName and FieldValue into specified HttpHeader. 368 369 @param[in,out] HttpHeader Specified HttpHeader. 370 @param[in] FieldName FieldName of this HttpHeader, a NULL terminated ASCII string. 371 @param[in] FieldValue FieldValue of this HttpHeader, a NULL terminated ASCII string. 372 373 374 @retval EFI_SUCCESS The FieldName and FieldValue are set into HttpHeader successfully. 375 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. 376 377 **/ 378 EFI_STATUS 379 EFIAPI 380 HttpSetFieldNameAndValue ( 381 IN OUT EFI_HTTP_HEADER *HttpHeader, 382 IN CONST CHAR8 *FieldName, 383 IN CONST CHAR8 *FieldValue 384 ); 385 386 /** 387 Get one key/value header pair from the raw string. 388 389 @param[in] String Pointer to the raw string. 390 @param[out] FieldName Points directly to field name within 'HttpHeader'. 391 @param[out] FieldValue Points directly to field value within 'HttpHeader'. 392 393 @return Pointer to the next raw string. 394 @return NULL if no key/value header pair from this raw string. 395 396 **/ 397 CHAR8 * 398 EFIAPI 399 HttpGetFieldNameAndValue ( 400 IN CHAR8 *String, 401 OUT CHAR8 **FieldName, 402 OUT CHAR8 **FieldValue 403 ); 404 405 /** 406 Free existing HeaderFields. 407 408 @param[in] HeaderFields Pointer to array of key/value header pairs waiting for free. 409 @param[in] FieldCount The number of header pairs in HeaderFields. 410 411 **/ 412 VOID 413 EFIAPI 414 HttpFreeHeaderFields ( 415 IN EFI_HTTP_HEADER *HeaderFields, 416 IN UINTN FieldCount 417 ); 418 419 /** 420 Generate HTTP request message. 421 422 This function will allocate memory for the whole HTTP message and generate a 423 well formatted HTTP Request message in it, include the Request-Line, header 424 fields and also the message body. It is the caller's responsibility to free 425 the buffer returned in *RequestMsg. 426 427 @param[in] Message Pointer to the EFI_HTTP_MESSAGE structure which 428 contains the required information to generate 429 the HTTP request message. 430 @param[in] Url The URL of a remote host. 431 @param[out] RequestMsg Pointer to the created HTTP request message. 432 NULL if any error occured. 433 @param[out] RequestMsgSize Size of the RequestMsg (in bytes). 434 435 @return EFI_SUCCESS If HTTP request string was created successfully 436 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. 437 @retval EFI_INVALID_PARAMETER The input arguments are invalid 438 439 **/ 440 EFI_STATUS 441 EFIAPI 442 HttpGenRequestMessage ( 443 IN CONST EFI_HTTP_MESSAGE *Message, 444 IN CONST CHAR8 *Url, 445 OUT CHAR8 **RequestMsg, 446 OUT UINTN *RequestMsgSize 447 ); 448 449 /** 450 Translate the status code in HTTP message to EFI_HTTP_STATUS_CODE defined 451 in UEFI 2.5 specification. 452 453 @param[in] StatusCode The status code value in HTTP message. 454 455 @return Value defined in EFI_HTTP_STATUS_CODE . 456 457 **/ 458 EFI_HTTP_STATUS_CODE 459 EFIAPI 460 HttpMappingToStatusCode ( 461 IN UINTN StatusCode 462 ); 463 464 /** 465 Check whether header field called FieldName is in DeleteList. 466 467 @param[in] DeleteList Pointer to array of key/value header pairs. 468 @param[in] DeleteCount The number of header pairs. 469 @param[in] FieldName Pointer to header field's name. 470 471 @return TRUE if FieldName is not in DeleteList, that means this header field is valid. 472 @return FALSE if FieldName is in DeleteList, that means this header field is invalid. 473 474 **/ 475 BOOLEAN 476 EFIAPI 477 HttpIsValidHttpHeader ( 478 IN CHAR8 *DeleteList[], 479 IN UINTN DeleteCount, 480 IN CHAR8 *FieldName 481 ); 482 483 484 #endif 485 486