]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/DxeHttpLib/DxeHttpLib.c
MdeModulePkg/DxeHttpLib: Check the input parameters for some APIs.
[mirror_edk2.git] / MdeModulePkg / Library / DxeHttpLib / DxeHttpLib.c
index aeb52d0088578bcabf9f2cd952271cd944f949ce..e53dce5e1bcbadf9917e448bcaab9b2471dece9c 100644 (file)
@@ -2,7 +2,8 @@
   This library is used to share code between UEFI network stack modules.\r
   It provides the helper routines to parse the HTTP message byte stream.\r
 \r
-Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at<BR>\r
@@ -13,67 +14,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-#include <Uefi.h>\r
-#include <Library/NetLib.h>\r
-#include <Library/HttpLib.h>\r
-#include <Library/BaseLib.h>\r
-#include <Library/DebugLib.h>\r
-#include <Library/MemoryAllocationLib.h>\r
-#include <Library/UefiBootServicesTableLib.h>\r
-\r
-#define BIT(x)  (1 << x)\r
-\r
-#define NET_IS_HEX_CHAR(Ch)   \\r
-  ((('0' <= (Ch)) && ((Ch) <= '9')) ||  \\r
-   (('A' <= (Ch)) && ((Ch) <= 'F')) ||  \\r
-   (('a' <= (Ch)) && ((Ch) <= 'f')))\r
-\r
-//\r
-// Field index of the HTTP URL parse result.\r
-//\r
-#define   HTTP_URI_FIELD_SCHEME           0\r
-#define   HTTP_URI_FIELD_AUTHORITY        1\r
-#define   HTTP_URI_FIELD_PATH             2\r
-#define   HTTP_URI_FIELD_QUERY            3\r
-#define   HTTP_URI_FIELD_FRAGMENT         4\r
-#define   HTTP_URI_FIELD_USERINFO         5\r
-#define   HTTP_URI_FIELD_HOST             6\r
-#define   HTTP_URI_FIELD_PORT             7\r
-#define   HTTP_URI_FIELD_MAX              8\r
-\r
-//\r
-// Structure to store the parse result of a HTTP URL.\r
-//\r
-typedef struct {\r
-    UINT32      Offset;\r
-    UINT32      Length;\r
-} HTTP_URL_FILED_DATA;\r
+#include "DxeHttpLib.h"\r
 \r
-typedef struct {\r
-  UINT16                  FieldBitMap;\r
-  HTTP_URL_FILED_DATA     FieldData[HTTP_URI_FIELD_MAX];\r
-} HTTP_URL_PARSER;\r
 \r
-typedef enum {\r
-  UrlParserUrlStart,\r
-  UrlParserScheme,\r
-  UrlParserSchemeColon,            // ":"\r
-  UrlParserSchemeColonSlash,       // ":/"\r
-  UrlParserSchemeColonSlashSlash,  // "://"\r
-  UrlParserAuthority,\r
-  UrlParserAtInAuthority,\r
-  UrlParserPath,\r
-  UrlParserQueryStart,    // "?"\r
-  UrlParserQuery,\r
-  UrlParserFragmentStart, // "#"\r
-  UrlParserFragment,\r
-  UrlParserUserInfo,\r
-  UrlParserHostStart,     // "@"\r
-  UrlParserHost,\r
-  UrlParserPortStart,     // ":"\r
-  UrlParserPort,\r
-  UrlParserStateMax\r
-} HTTP_URL_PARSE_STATE;\r
 \r
 /**\r
   Decode a percent-encoded URI component to the ASCII character.\r
@@ -113,7 +56,7 @@ UriPercentDecode (
   HexStr[2] = '\0';\r
   while (Index < BufferLength) {\r
     if (Buffer[Index] == '%') {\r
-      if (!NET_IS_HEX_CHAR (Buffer[Index+1]) || !NET_IS_HEX_CHAR (Buffer[Index+2])) {\r
+      if (Index + 1 >= BufferLength || Index + 2 >= BufferLength || !NET_IS_HEX_CHAR (Buffer[Index+1]) || !NET_IS_HEX_CHAR (Buffer[Index+2])) {\r
         return EFI_INVALID_PARAMETER;\r
       }\r
       HexStr[0] = Buffer[Index+1];\r
@@ -133,18 +76,21 @@ UriPercentDecode (
 }\r
 \r
 /**\r
-  This function return the updated state accroding to the input state and next character of\r
+  This function return the updated state according to the input state and next character of\r
   the authority.\r
 \r
   @param[in]       Char           Next character.\r
   @param[in]       State          Current value of the parser state machine.\r
+  @param[in]       IsRightBracket TRUE if there is an sign ']' in the authority component and \r
+                                  indicates the next part is ':' before Port.                                \r
 \r
   @return          Updated state value.\r
 **/\r
 HTTP_URL_PARSE_STATE\r
 NetHttpParseAuthorityChar (\r
   IN  CHAR8                  Char,\r
-  IN  HTTP_URL_PARSE_STATE   State\r
+  IN  HTTP_URL_PARSE_STATE   State,\r
+  IN  BOOLEAN                *IsRightBracket\r
   )\r
 {\r
 \r
@@ -169,12 +115,27 @@ NetHttpParseAuthorityChar (
     break;\r
 \r
   case UrlParserHost:\r
-  case UrlParserHostStart:\r
+  case UrlParserHostStart:  \r
+    if (Char == '[') {\r
+      return UrlParserHostIpv6;\r
+    }\r
+    \r
     if (Char == ':') {\r
       return UrlParserPortStart;\r
     }\r
+    \r
     return UrlParserHost;\r
-\r
+    \r
+  case UrlParserHostIpv6:  \r
+    if (Char == ']') {\r
+      *IsRightBracket = TRUE;\r
+    }\r
+    \r
+    if (Char == ':' && *IsRightBracket) {\r
+      return UrlParserPortStart;\r
+    }\r
+    return UrlParserHostIpv6;\r
+    \r
   case UrlParserPort:\r
   case UrlParserPortStart:\r
     return UrlParserPort;\r
@@ -210,6 +171,7 @@ NetHttpParseAuthority (
   HTTP_URL_PARSE_STATE  State;\r
   UINT32                Field;\r
   UINT32                OldField;\r
+  BOOLEAN               IsrightBracket;\r
   \r
   ASSERT ((UrlParser->FieldBitMap & BIT (HTTP_URI_FIELD_AUTHORITY)) != 0);\r
 \r
@@ -222,12 +184,13 @@ NetHttpParseAuthority (
     State = UrlParserHost;\r
   }\r
 \r
+  IsrightBracket = FALSE;\r
   Field = HTTP_URI_FIELD_MAX;\r
   OldField = Field;\r
   Authority = Url + UrlParser->FieldData[HTTP_URI_FIELD_AUTHORITY].Offset;\r
   Length = UrlParser->FieldData[HTTP_URI_FIELD_AUTHORITY].Length;\r
   for (Char = Authority; Char < Authority + Length; Char++) {\r
-    State = NetHttpParseAuthorityChar (*Char, State);\r
+    State = NetHttpParseAuthorityChar (*Char, State, &IsrightBracket);\r
     switch (State) {\r
     case UrlParserStateMax:\r
       return EFI_INVALID_PARAMETER;\r
@@ -243,6 +206,10 @@ NetHttpParseAuthority (
     case UrlParserHost:\r
       Field = HTTP_URI_FIELD_HOST;\r
       break;\r
+\r
+    case UrlParserHostIpv6:\r
+      Field = HTTP_URI_FIELD_HOST;\r
+      break;\r
       \r
     case UrlParserPort:\r
       Field = HTTP_URI_FIELD_PORT;\r
@@ -274,7 +241,7 @@ NetHttpParseAuthority (
 }\r
 \r
 /**\r
-  This function return the updated state accroding to the input state and next character of a URL.\r
+  This function return the updated state according to the input state and next character of a URL.\r
 \r
   @param[in]       Char           Next character.\r
   @param[in]       State          Current value of the parser state machine.\r
@@ -405,6 +372,8 @@ HttpParseUrl (
   BOOLEAN               FoundAt;\r
   EFI_STATUS            Status;\r
   HTTP_URL_PARSER       *Parser;\r
+\r
+  Parser = NULL;\r
   \r
   if (Url == NULL || Length == 0 || UrlParser == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -429,12 +398,13 @@ HttpParseUrl (
   FoundAt = FALSE;\r
   for (Char = Url; Char < Url + Length; Char++) {\r
     //\r
-    // Update state machine accoring to next char.\r
+    // Update state machine according to next char.\r
     //\r
     State = NetHttpParseUrlChar (*Char, State);\r
 \r
     switch (State) {\r
     case UrlParserStateMax:\r
+      FreePool (Parser);\r
       return EFI_INVALID_PARAMETER;\r
       \r
     case UrlParserSchemeColon:\r
@@ -497,6 +467,7 @@ HttpParseUrl (
   if ((Parser->FieldBitMap & BIT (HTTP_URI_FIELD_AUTHORITY)) != 0) {\r
     Status = NetHttpParseAuthority (Url, FoundAt, Parser);\r
     if (EFI_ERROR (Status)) {\r
+      FreePool (Parser);\r
       return Status;\r
     }\r
   }\r
@@ -556,6 +527,7 @@ HttpUrlGetHostName (
              &ResultLength\r
              );\r
   if (EFI_ERROR (Status)) {\r
+    FreePool (Name);\r
     return Status;\r
   }\r
 \r
@@ -615,6 +587,7 @@ HttpUrlGetIp4 (
              &ResultLength\r
              );\r
   if (EFI_ERROR (Status)) {\r
+    FreePool (Ip4String);\r
     return Status;\r
   }\r
 \r
@@ -690,6 +663,7 @@ HttpUrlGetIp6 (
              &ResultLength\r
              );\r
   if (EFI_ERROR (Status)) {\r
+    FreePool (Ip6String);\r
     return Status;\r
   }\r
   \r
@@ -725,6 +699,8 @@ HttpUrlGetPort (
 {\r
   CHAR8         *PortString;\r
   EFI_STATUS    Status;\r
+  UINTN         Index;\r
+  UINTN         Data;\r
   UINT32        ResultLength;\r
   HTTP_URL_PARSER      *Parser;\r
 \r
@@ -732,6 +708,9 @@ HttpUrlGetPort (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  *Port = 0;\r
+  Index = 0;\r
+\r
   Parser = (HTTP_URL_PARSER*) UrlParser;\r
 \r
   if ((Parser->FieldBitMap & BIT (HTTP_URI_FIELD_PORT)) == 0) {\r
@@ -750,13 +729,91 @@ HttpUrlGetPort (
              &ResultLength\r
              );\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto ON_EXIT;\r
   }\r
 \r
   PortString[ResultLength] = '\0';\r
-  *Port = (UINT16) AsciiStrDecimalToUintn (Url + Parser->FieldData[HTTP_URI_FIELD_PORT].Offset);\r
 \r
-  return  EFI_SUCCESS;\r
+  while (Index < ResultLength) {\r
+    if (!NET_IS_DIGIT (PortString[Index])) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto ON_EXIT;\r
+    }\r
+    Index ++;\r
+  }\r
+\r
+  Status =  AsciiStrDecimalToUintnS (Url + Parser->FieldData[HTTP_URI_FIELD_PORT].Offset, (CHAR8 **) NULL, &Data);\r
+\r
+  if (Data > HTTP_URI_PORT_MAX_NUM) {\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  *Port = (UINT16) Data;\r
+\r
+ON_EXIT:\r
+  FreePool (PortString);\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Get the Path from a HTTP URL.\r
+\r
+  This function will return the Path according to the Url and previous parse result,and\r
+  it is the caller's responsibility to free the buffer returned in *Path.\r
+\r
+  @param[in]    Url                The pointer to a HTTP URL string.\r
+  @param[in]    UrlParser          URL Parse result returned by NetHttpParseUrl().\r
+  @param[out]   Path               Pointer to a buffer to store the Path.\r
+\r
+  @retval EFI_SUCCESS              Successfully get the required component.\r
+  @retval EFI_INVALID_PARAMETER    Uri is NULL or HostName is NULL or UrlParser is invalid.\r
+  @retval EFI_NOT_FOUND            No hostName component in the URL.\r
+  @retval EFI_OUT_OF_RESOURCES     Could not allocate needed resources.\r
+  \r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+HttpUrlGetPath (\r
+  IN      CHAR8              *Url,\r
+  IN      VOID               *UrlParser,\r
+     OUT  CHAR8              **Path\r
+  )\r
+{\r
+  CHAR8                *PathStr;\r
+  EFI_STATUS           Status;\r
+  UINT32               ResultLength;\r
+  HTTP_URL_PARSER      *Parser;\r
+\r
+  if (Url == NULL || UrlParser == NULL || Path == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Parser = (HTTP_URL_PARSER*) UrlParser;\r
+\r
+  if ((Parser->FieldBitMap & BIT (HTTP_URI_FIELD_PATH)) == 0) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  PathStr = AllocatePool (Parser->FieldData[HTTP_URI_FIELD_PATH].Length + 1);\r
+  if (PathStr == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  \r
+  Status = UriPercentDecode (\r
+             Url + Parser->FieldData[HTTP_URI_FIELD_PATH].Offset,\r
+             Parser->FieldData[HTTP_URI_FIELD_PATH].Length,\r
+             PathStr,\r
+             &ResultLength\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (PathStr);\r
+    return Status;\r
+  }\r
+\r
+  PathStr[ResultLength] = '\0';\r
+  *Path = PathStr;\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
@@ -785,14 +842,15 @@ HttpUrlFreeParser (
 \r
 **/\r
 EFI_HTTP_HEADER *\r
-HttpIoFindHeader (\r
+EFIAPI\r
+HttpFindHeader (\r
   IN  UINTN                HeaderCount,\r
   IN  EFI_HTTP_HEADER      *Headers,\r
   IN  CHAR8                *FieldName\r
   )\r
 {\r
   UINTN                 Index;\r
-  \r
+\r
   if (HeaderCount == 0 || Headers == NULL || FieldName == NULL) {\r
     return NULL;\r
   }\r
@@ -830,7 +888,7 @@ typedef struct {
   BOOLEAN                       IsChunked;     // "chunked" transfer-coding.\r
   BOOLEAN                       ContentLengthIsValid;\r
   UINTN                         ContentLength; // Entity length (not the message-body length), invalid until ContentLengthIsValid is TRUE\r
-  \r
+\r
   HTTP_BODY_PARSER_CALLBACK     Callback;\r
   VOID                          *Context;\r
   UINTN                         ParsedBodyLength;\r
@@ -899,14 +957,13 @@ HttpIoParseContentLengthHeader (
   )\r
 {\r
   EFI_HTTP_HEADER       *Header;\r
-  \r
-  Header = HttpIoFindHeader (HeaderCount, Headers, "Content-Length");\r
+\r
+  Header = HttpFindHeader (HeaderCount, Headers, HTTP_HEADER_CONTENT_LENGTH);\r
   if (Header == NULL) {\r
     return EFI_NOT_FOUND;\r
   }\r
 \r
-  *ContentLength = AsciiStrDecimalToUintn (Header->FieldValue);\r
-  return EFI_SUCCESS;\r
+  return AsciiStrDecimalToUintnS (Header->FieldValue, (CHAR8 **) NULL, ContentLength);\r
 }\r
 \r
 /**\r
@@ -928,7 +985,7 @@ HttpIoIsChunked (
   EFI_HTTP_HEADER       *Header;\r
 \r
 \r
-  Header = HttpIoFindHeader (HeaderCount, Headers, "Transfer-Encoding");\r
+  Header = HttpFindHeader (HeaderCount, Headers, HTTP_HEADER_TRANSFER_ENCODING);\r
   if (Header == NULL) {\r
     return FALSE;\r
   }\r
@@ -1031,7 +1088,7 @@ HttpInitMsgParser (
   Parser->State = BodyParserBodyStart;\r
   \r
   //\r
-  // Determine the message length accroding to RFC 2616.\r
+  // Determine the message length according to RFC 2616.\r
   // 1. Check whether the message "MUST NOT" have a message-body.\r
   //\r
   Parser->IgnoreBody = HttpIoNoMessageBody (Method, StatusCode);\r
@@ -1136,21 +1193,7 @@ HttpParseMessageBody (
     switch (Parser->State) {\r
     case BodyParserStateMax:\r
       return EFI_ABORTED;\r
-\r
-    case BodyParserComplete:\r
-      if (Parser->Callback != NULL) {\r
-        Status = Parser->Callback (\r
-                   BodyParseEventOnComplete,\r
-                   Char,\r
-                   0,\r
-                   Parser->Context\r
-                   );\r
-        if (EFI_ERROR (Status)) {\r
-          return Status;\r
-        }\r
-      }\r
-      return EFI_SUCCESS;\r
-    \r
\r
     case BodyParserBodyIdentity:\r
       //\r
       // Identity transfer-coding, just notify user to save the body data.\r
@@ -1170,6 +1213,17 @@ HttpParseMessageBody (
       Parser->ParsedBodyLength += MIN (BodyLength, Parser->ContentLength - Parser->ParsedBodyLength);\r
       if (Parser->ParsedBodyLength == Parser->ContentLength) {\r
         Parser->State = BodyParserComplete;\r
+        if (Parser->Callback != NULL) {\r
+          Status = Parser->Callback (\r
+                     BodyParseEventOnComplete,\r
+                     Char,\r
+                     0,\r
+                     Parser->Context\r
+                     );\r
+          if (EFI_ERROR (Status)) {\r
+            return Status;\r
+          }\r
+        }\r
       }\r
       break;\r
 \r
@@ -1247,6 +1301,18 @@ HttpParseMessageBody (
     case BodyParserLastCRLFEnd:\r
       if (*Char == '\n') {\r
         Parser->State = BodyParserComplete;\r
+        Char++;\r
+        if (Parser->Callback != NULL) {\r
+          Status = Parser->Callback (\r
+                     BodyParseEventOnComplete,\r
+                     Char,\r
+                     0,\r
+                     Parser->Context\r
+                     );\r
+          if (EFI_ERROR (Status)) {\r
+            return Status;\r
+          }\r
+        }\r
         break;\r
       } else {\r
         Parser->State = BodyParserStateMax;\r
@@ -1333,6 +1399,10 @@ HttpIsMessageComplete (
 {\r
   HTTP_BODY_PARSER      *Parser;\r
 \r
+  if (MsgParser == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
   Parser = (HTTP_BODY_PARSER*) MsgParser;\r
 \r
   if (Parser->State == BodyParserComplete) {\r
@@ -1391,3 +1461,621 @@ HttpFreeMsgParser (
 {\r
   FreePool (MsgParser);\r
 }\r
+\r
+\r
+/**\r
+  Get the next string, which is distinguished by specified separator.\r
+\r
+  @param[in]  String             Pointer to the string.\r
+  @param[in]  Separator          Specified separator used to distinguish where is the beginning\r
+                                 of next string.\r
+\r
+  @return     Pointer to the next string.\r
+  @return     NULL if not find or String is NULL.\r
+\r
+**/\r
+CHAR8 *\r
+EFIAPI\r
+AsciiStrGetNextToken (\r
+  IN CONST CHAR8 *String,\r
+  IN       CHAR8 Separator\r
+  )\r
+{\r
+  CONST CHAR8 *Token;\r
+\r
+  Token = String;\r
+  while (TRUE) {\r
+    if (*Token == 0) {\r
+      return NULL;\r
+    }\r
+    if (*Token == Separator) {\r
+      return (CHAR8 *)(Token + 1);\r
+    }\r
+    Token++;\r
+  }\r
+}\r
+\r
+/**\r
+  Set FieldName and FieldValue into specified HttpHeader.\r
+\r
+  @param[in,out]  HttpHeader      Specified HttpHeader.\r
+  @param[in]  FieldName           FieldName of this HttpHeader, a NULL terminated ASCII string.\r
+  @param[in]  FieldValue          FieldValue of this HttpHeader, a NULL terminated ASCII string.\r
+\r
+\r
+  @retval EFI_SUCCESS             The FieldName and FieldValue are set into HttpHeader successfully.\r
+  @retval EFI_INVALID_PARAMETER   The parameter is invalid.\r
+  @retval EFI_OUT_OF_RESOURCES    Failed to allocate resources.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+HttpSetFieldNameAndValue (\r
+  IN  OUT   EFI_HTTP_HEADER       *HttpHeader,\r
+  IN  CONST CHAR8                 *FieldName,\r
+  IN  CONST CHAR8                 *FieldValue\r
+  )\r
+{\r
+  UINTN                       FieldNameSize;\r
+  UINTN                       FieldValueSize;\r
+\r
+  if (HttpHeader == NULL || FieldName == NULL || FieldValue == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (HttpHeader->FieldName != NULL) {\r
+    FreePool (HttpHeader->FieldName);\r
+  }\r
+  if (HttpHeader->FieldValue != NULL) {\r
+    FreePool (HttpHeader->FieldValue);\r
+  }\r
+\r
+  FieldNameSize = AsciiStrSize (FieldName);\r
+  HttpHeader->FieldName = AllocateZeroPool (FieldNameSize);\r
+  if (HttpHeader->FieldName == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  CopyMem (HttpHeader->FieldName, FieldName, FieldNameSize);\r
+  HttpHeader->FieldName[FieldNameSize - 1] = 0;\r
+\r
+  FieldValueSize = AsciiStrSize (FieldValue);\r
+  HttpHeader->FieldValue = AllocateZeroPool (FieldValueSize);\r
+  if (HttpHeader->FieldValue == NULL) {\r
+    FreePool (HttpHeader->FieldName);\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  CopyMem (HttpHeader->FieldValue, FieldValue, FieldValueSize);\r
+  HttpHeader->FieldValue[FieldValueSize - 1] = 0;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Get one key/value header pair from the raw string.\r
+\r
+  @param[in]  String             Pointer to the raw string.\r
+  @param[out] FieldName          Points directly to field name within 'HttpHeader'.\r
+  @param[out] FieldValue         Points directly to field value within 'HttpHeader'.\r
+\r
+  @return     Pointer to the next raw string.\r
+  @return     NULL if no key/value header pair from this raw string.\r
+\r
+**/\r
+CHAR8 *\r
+EFIAPI\r
+HttpGetFieldNameAndValue (\r
+  IN     CHAR8   *String,\r
+     OUT CHAR8   **FieldName,\r
+     OUT CHAR8   **FieldValue\r
+  )\r
+{\r
+  CHAR8  *FieldNameStr;\r
+  CHAR8  *FieldValueStr;\r
+  CHAR8  *StrPtr;\r
+  CHAR8  *EndofHeader;\r
+\r
+  if (String == NULL || FieldName == NULL || FieldValue == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  *FieldName    = NULL;\r
+  *FieldValue   = NULL;\r
+  FieldNameStr  = NULL;\r
+  FieldValueStr = NULL;\r
+  StrPtr        = NULL;\r
+  EndofHeader   = NULL;\r
+\r
+\r
+  //\r
+  // Check whether the raw HTTP header string is valid or not.\r
+  //\r
+  EndofHeader = AsciiStrStr (String, "\r\n\r\n");\r
+  if (EndofHeader == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  //\r
+  // Each header field consists of a name followed by a colon (":") and the field value.\r
+  //\r
+  FieldNameStr = String;\r
+  FieldValueStr = AsciiStrGetNextToken (FieldNameStr, ':');\r
+  if (FieldValueStr == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  //\r
+  // Replace ':' with 0\r
+  //\r
+  *(FieldValueStr - 1) = 0;\r
+\r
+  //\r
+  // The field value MAY be preceded by any amount of LWS, though a single SP is preferred.\r
+  // Note: LWS  = [CRLF] 1*(SP|HT), it can be '\r\n ' or '\r\n\t' or ' ' or '\t'.\r
+  //       CRLF = '\r\n'.\r
+  //       SP   = ' '.\r
+  //       HT   = '\t' (Tab).\r
+  //\r
+  while (TRUE) {\r
+    if (*FieldValueStr == ' ' || *FieldValueStr == '\t') {\r
+      //\r
+      // Boundary condition check. \r
+      //\r
+      if ((UINTN)EndofHeader - (UINTN)(FieldValueStr) < 1) {\r
+        return NULL;  \r
+      }\r
+      \r
+      FieldValueStr ++;\r
+    } else if (*FieldValueStr == '\r') {\r
+      //\r
+      // Boundary condition check. \r
+      //\r
+      if ((UINTN)EndofHeader - (UINTN)(FieldValueStr) < 3) {\r
+        return NULL;  \r
+      }\r
+\r
+      if (*(FieldValueStr + 1) == '\n' && (*(FieldValueStr + 2) == ' ' || *(FieldValueStr + 2) == '\t')) {\r
+        FieldValueStr = FieldValueStr + 3;\r
+      }\r
+    } else {\r
+      break;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Header fields can be extended over multiple lines by preceding each extra\r
+  // line with at least one SP or HT.\r
+  //\r
+  StrPtr = FieldValueStr;\r
+  do {\r
+    StrPtr = AsciiStrGetNextToken (StrPtr, '\r');\r
+    if (StrPtr == NULL || *StrPtr != '\n') {\r
+      return NULL;\r
+    }\r
+\r
+    StrPtr++;\r
+  } while (*StrPtr == ' ' || *StrPtr == '\t');\r
+\r
+  //\r
+  // Replace '\r' with 0\r
+  //\r
+  *(StrPtr - 2) = 0;\r
+\r
+  //\r
+  // Get FieldName and FieldValue.\r
+  //\r
+  *FieldName = FieldNameStr;\r
+  *FieldValue = FieldValueStr;\r
+\r
+  return StrPtr;\r
+}\r
+\r
+/**\r
+  Free existing HeaderFields.\r
+\r
+  @param[in]  HeaderFields       Pointer to array of key/value header pairs waitting for free.\r
+  @param[in]  FieldCount         The number of header pairs in HeaderFields.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+HttpFreeHeaderFields (\r
+  IN  EFI_HTTP_HEADER  *HeaderFields,\r
+  IN  UINTN            FieldCount\r
+  )\r
+{\r
+  UINTN                       Index;\r
+\r
+  if (HeaderFields != NULL) {\r
+    for (Index = 0; Index < FieldCount; Index++) {\r
+      if (HeaderFields[Index].FieldName != NULL) {\r
+        FreePool (HeaderFields[Index].FieldName);\r
+      }\r
+      if (HeaderFields[Index].FieldValue != NULL) {\r
+        FreePool (HeaderFields[Index].FieldValue);\r
+      }\r
+    }\r
+\r
+    FreePool (HeaderFields);\r
+  }\r
+}\r
+\r
+/**\r
+  Generate HTTP request message.\r
+\r
+  This function will allocate memory for the whole HTTP message and generate a\r
+  well formatted HTTP Request message in it, include the Request-Line, header\r
+  fields and also the message body. It is the caller's responsibility to free\r
+  the buffer returned in *RequestMsg.\r
+\r
+  @param[in]   Message            Pointer to the EFI_HTTP_MESSAGE structure which\r
+                                  contains the required information to generate\r
+                                  the HTTP request message.\r
+  @param[in]   Url                The URL of a remote host.\r
+  @param[out]  RequestMsg         Pointer to the created HTTP request message.\r
+                                  NULL if any error occured.\r
+  @param[out]  RequestMsgSize     Size of the RequestMsg (in bytes).\r
+\r
+  @return EFI_SUCCESS             If HTTP request string was created successfully\r
+  @retval EFI_OUT_OF_RESOURCES    Failed to allocate resources.\r
+  @retval EFI_INVALID_PARAMETER   The input arguments are invalid\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+HttpGenRequestMessage (\r
+  IN     CONST EFI_HTTP_MESSAGE        *Message,\r
+  IN     CONST CHAR8                   *Url,\r
+     OUT CHAR8                         **RequestMsg,\r
+     OUT UINTN                         *RequestMsgSize\r
+  )\r
+{\r
+  EFI_STATUS                       Status;\r
+  UINTN                            StrLength;\r
+  CHAR8                            *RequestPtr;\r
+  UINTN                            HttpHdrSize;\r
+  UINTN                            MsgSize;\r
+  BOOLEAN                          Success;\r
+  VOID                             *HttpHdr;\r
+  EFI_HTTP_HEADER                  **AppendList;\r
+  UINTN                            Index;\r
+  EFI_HTTP_UTILITIES_PROTOCOL      *HttpUtilitiesProtocol;\r
+\r
+  Status                = EFI_SUCCESS;\r
+  HttpHdrSize           = 0;\r
+  MsgSize               = 0;\r
+  Success               = FALSE;\r
+  HttpHdr               = NULL;\r
+  AppendList            = NULL;\r
+  HttpUtilitiesProtocol = NULL;\r
+\r
+  //\r
+  // 1. If we have a Request, we cannot have a NULL Url\r
+  // 2. If we have a Request, HeaderCount can not be non-zero\r
+  // 3. If we do not have a Request, HeaderCount should be zero\r
+  // 4. If we do not have Request and Headers, we need at least a message-body\r
+  //\r
+  if ((Message == NULL || RequestMsg == NULL || RequestMsgSize == NULL) || \r
+      (Message->Data.Request != NULL && Url == NULL) ||\r
+      (Message->Data.Request != NULL && Message->HeaderCount == 0) ||\r
+      (Message->Data.Request == NULL && Message->HeaderCount != 0) ||\r
+      (Message->Data.Request == NULL && Message->HeaderCount == 0 && Message->BodyLength == 0)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Message->HeaderCount != 0) {\r
+    //\r
+    // Locate the HTTP_UTILITIES protocol.\r
+    //\r
+    Status = gBS->LocateProtocol (\r
+                    &gEfiHttpUtilitiesProtocolGuid,\r
+                    NULL,\r
+                    (VOID **)&HttpUtilitiesProtocol\r
+                    );\r
+\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((DEBUG_ERROR,"Failed to locate Http Utilities protocol. Status = %r.\n", Status));\r
+      return Status;\r
+    }\r
+\r
+    //\r
+    // Build AppendList to send into HttpUtilitiesBuild\r
+    //\r
+    AppendList = AllocateZeroPool (sizeof (EFI_HTTP_HEADER *) * (Message->HeaderCount));\r
+    if (AppendList == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+\r
+    for(Index = 0; Index < Message->HeaderCount; Index++){\r
+      AppendList[Index] = &Message->Headers[Index];\r
+    }\r
+\r
+    //\r
+    // Build raw HTTP Headers\r
+    //\r
+    Status = HttpUtilitiesProtocol->Build (\r
+                HttpUtilitiesProtocol,\r
+                0,\r
+                NULL,\r
+                0,\r
+                NULL,\r
+                Message->HeaderCount,\r
+                AppendList,\r
+                &HttpHdrSize,\r
+                &HttpHdr\r
+                );\r
+\r
+    if (AppendList != NULL) {\r
+      FreePool (AppendList);\r
+    }\r
+\r
+    if (EFI_ERROR (Status) || HttpHdr == NULL){\r
+      return Status;\r
+    }\r
+  }\r
+\r
+  //\r
+  // If we have headers to be sent, account for it.\r
+  //\r
+  if (Message->HeaderCount != 0) {\r
+    MsgSize = HttpHdrSize;\r
+  }\r
+\r
+  //\r
+  // If we have a request line, account for the fields.\r
+  //\r
+  if (Message->Data.Request != NULL) {\r
+    MsgSize += HTTP_METHOD_MAXIMUM_LEN + AsciiStrLen (HTTP_VERSION_CRLF_STR) + AsciiStrLen (Url);\r
+  }\r
+\r
+\r
+  //\r
+  // If we have a message body to be sent, account for it.\r
+  //\r
+  MsgSize += Message->BodyLength;\r
+\r
+  //\r
+  // memory for the string that needs to be sent to TCP\r
+  //\r
+  *RequestMsg           = NULL;\r
+  *RequestMsg = AllocateZeroPool (MsgSize);\r
+  if (*RequestMsg == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Exit;\r
+  }\r
+\r
+  RequestPtr = *RequestMsg;\r
+  //\r
+  // Construct header request\r
+  //\r
+  if (Message->Data.Request != NULL) {\r
+    switch (Message->Data.Request->Method) {\r
+    case HttpMethodGet:\r
+      StrLength = sizeof (HTTP_METHOD_GET) - 1;\r
+      CopyMem (RequestPtr, HTTP_METHOD_GET, StrLength);\r
+      RequestPtr += StrLength;\r
+      break;\r
+    case HttpMethodPut:\r
+      StrLength = sizeof (HTTP_METHOD_PUT) - 1;\r
+      CopyMem (RequestPtr, HTTP_METHOD_PUT, StrLength);\r
+      RequestPtr += StrLength;\r
+      break;\r
+    case HttpMethodPatch:\r
+      StrLength = sizeof (HTTP_METHOD_PATCH) - 1;\r
+      CopyMem (RequestPtr, HTTP_METHOD_PATCH, StrLength);\r
+      RequestPtr += StrLength;\r
+      break;\r
+    case HttpMethodPost:\r
+      StrLength = sizeof (HTTP_METHOD_POST) - 1;\r
+      CopyMem (RequestPtr, HTTP_METHOD_POST, StrLength);\r
+      RequestPtr += StrLength;\r
+      break;\r
+    case HttpMethodHead:\r
+      StrLength = sizeof (HTTP_METHOD_HEAD) - 1;\r
+      CopyMem (RequestPtr, HTTP_METHOD_HEAD, StrLength);\r
+      RequestPtr += StrLength;\r
+      break;\r
+    case HttpMethodDelete:\r
+      StrLength = sizeof (HTTP_METHOD_DELETE) - 1;\r
+      CopyMem (RequestPtr, HTTP_METHOD_DELETE, StrLength);\r
+      RequestPtr += StrLength;\r
+      break;\r
+    default:\r
+      ASSERT (FALSE);\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto Exit;\r
+    }\r
+\r
+    StrLength = AsciiStrLen(EMPTY_SPACE);\r
+    CopyMem (RequestPtr, EMPTY_SPACE, StrLength);\r
+    RequestPtr += StrLength;\r
+\r
+    StrLength = AsciiStrLen (Url);\r
+    CopyMem (RequestPtr, Url, StrLength);\r
+    RequestPtr += StrLength;\r
+\r
+    StrLength = sizeof (HTTP_VERSION_CRLF_STR) - 1;\r
+    CopyMem (RequestPtr, HTTP_VERSION_CRLF_STR, StrLength);\r
+    RequestPtr += StrLength;\r
+\r
+    if (HttpHdr != NULL) {\r
+      //\r
+      // Construct header\r
+      //\r
+      CopyMem (RequestPtr, HttpHdr, HttpHdrSize);\r
+      RequestPtr += HttpHdrSize;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Construct body\r
+  //\r
+  if (Message->Body != NULL) {\r
+    CopyMem (RequestPtr, Message->Body, Message->BodyLength);\r
+    RequestPtr += Message->BodyLength;\r
+  }\r
+\r
+  //\r
+  // Done\r
+  //\r
+  (*RequestMsgSize) = (UINTN)(RequestPtr) - (UINTN)(*RequestMsg);\r
+  Success     = TRUE;\r
+\r
+Exit:\r
+\r
+  if (!Success) {\r
+    if (*RequestMsg != NULL) {\r
+      FreePool (*RequestMsg);\r
+    }\r
+    *RequestMsg = NULL;\r
+    return Status;\r
+  }\r
+\r
+  if (HttpHdr != NULL) {\r
+    FreePool (HttpHdr);\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Translate the status code in HTTP message to EFI_HTTP_STATUS_CODE defined\r
+  in UEFI 2.5 specification.\r
+\r
+  @param[in]  StatusCode         The status code value in HTTP message.\r
+\r
+  @return                        Value defined in EFI_HTTP_STATUS_CODE .\r
+\r
+**/\r
+EFI_HTTP_STATUS_CODE\r
+EFIAPI\r
+HttpMappingToStatusCode (\r
+  IN UINTN                  StatusCode\r
+  )\r
+{\r
+  switch (StatusCode) {\r
+  case 100:\r
+    return HTTP_STATUS_100_CONTINUE;\r
+  case 101:\r
+    return HTTP_STATUS_101_SWITCHING_PROTOCOLS;\r
+  case 200:\r
+    return HTTP_STATUS_200_OK;\r
+  case 201:\r
+    return HTTP_STATUS_201_CREATED;\r
+  case 202:\r
+    return HTTP_STATUS_202_ACCEPTED;\r
+  case 203:\r
+    return HTTP_STATUS_203_NON_AUTHORITATIVE_INFORMATION;\r
+  case 204:\r
+    return HTTP_STATUS_204_NO_CONTENT;\r
+  case 205:\r
+    return HTTP_STATUS_205_RESET_CONTENT;\r
+  case 206:\r
+    return HTTP_STATUS_206_PARTIAL_CONTENT;\r
+  case 300:\r
+    return HTTP_STATUS_300_MULTIPLE_CHOICES;\r
+  case 301:\r
+    return HTTP_STATUS_301_MOVED_PERMANENTLY;\r
+  case 302:\r
+    return HTTP_STATUS_302_FOUND;\r
+  case 303:\r
+    return HTTP_STATUS_303_SEE_OTHER;\r
+  case 304:\r
+    return HTTP_STATUS_304_NOT_MODIFIED;\r
+  case 305:\r
+    return HTTP_STATUS_305_USE_PROXY;\r
+  case 307:\r
+    return HTTP_STATUS_307_TEMPORARY_REDIRECT;\r
+  case 308:\r
+    return HTTP_STATUS_308_PERMANENT_REDIRECT;\r
+  case 400:\r
+    return HTTP_STATUS_400_BAD_REQUEST;\r
+  case 401:\r
+    return HTTP_STATUS_401_UNAUTHORIZED;\r
+  case 402:\r
+    return HTTP_STATUS_402_PAYMENT_REQUIRED;\r
+  case 403:\r
+    return HTTP_STATUS_403_FORBIDDEN;\r
+  case 404:\r
+    return HTTP_STATUS_404_NOT_FOUND;\r
+  case 405:\r
+    return HTTP_STATUS_405_METHOD_NOT_ALLOWED;\r
+  case 406:\r
+    return HTTP_STATUS_406_NOT_ACCEPTABLE;\r
+  case 407:\r
+    return HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED;\r
+  case 408:\r
+    return HTTP_STATUS_408_REQUEST_TIME_OUT;\r
+  case 409:\r
+    return HTTP_STATUS_409_CONFLICT;\r
+  case 410:\r
+    return HTTP_STATUS_410_GONE;\r
+  case 411:\r
+    return HTTP_STATUS_411_LENGTH_REQUIRED;\r
+  case 412:\r
+    return HTTP_STATUS_412_PRECONDITION_FAILED;\r
+  case 413:\r
+    return HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE;\r
+  case 414:\r
+    return HTTP_STATUS_414_REQUEST_URI_TOO_LARGE;\r
+  case 415:\r
+    return HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE;\r
+  case 416:\r
+    return HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED;\r
+  case 417:\r
+    return HTTP_STATUS_417_EXPECTATION_FAILED;\r
+  case 500:\r
+    return HTTP_STATUS_500_INTERNAL_SERVER_ERROR;\r
+  case 501:\r
+    return HTTP_STATUS_501_NOT_IMPLEMENTED;\r
+  case 502:\r
+    return HTTP_STATUS_502_BAD_GATEWAY;\r
+  case 503:\r
+    return HTTP_STATUS_503_SERVICE_UNAVAILABLE;\r
+  case 504:\r
+    return HTTP_STATUS_504_GATEWAY_TIME_OUT;\r
+  case 505:\r
+    return HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED;\r
+\r
+  default:\r
+    return HTTP_STATUS_UNSUPPORTED_STATUS;\r
+  }\r
+}\r
+\r
+/**\r
+  Check whether header field called FieldName is in DeleteList.\r
+\r
+  @param[in]  DeleteList        Pointer to array of key/value header pairs.\r
+  @param[in]  DeleteCount       The number of header pairs.\r
+  @param[in]  FieldName         Pointer to header field's name.\r
+\r
+  @return     TRUE if FieldName is not in DeleteList, that means this header field is valid.\r
+  @return     FALSE if FieldName is in DeleteList, that means this header field is invalid.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+HttpIsValidHttpHeader (\r
+  IN  CHAR8            *DeleteList[],\r
+  IN  UINTN            DeleteCount,\r
+  IN  CHAR8            *FieldName\r
+  )\r
+{\r
+  UINTN                       Index;\r
+\r
+  if (FieldName == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  for (Index = 0; Index < DeleteCount; Index++) {\r
+    if (DeleteList[Index] == NULL) {\r
+      continue;\r
+    }\r
+    \r
+    if (AsciiStrCmp (FieldName, DeleteList[Index]) == 0) {\r
+      return FALSE;\r
+    }\r
+  }\r
+\r
+  return TRUE;\r
+}\r
+\r