]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/HttpDxe/HttpImpl.c
NetworkPkg/HttpDxe: Detect 'Connection: close' header
[mirror_edk2.git] / NetworkPkg / HttpDxe / HttpImpl.c
index 77aa64a2b99668ef6b65920e6820d3c665615332..4ec2e8f6679505946dbfdd6a5efdb7290daae0da 100644 (file)
@@ -1,16 +1,10 @@
 /** @file\r
   Implementation of EFI_HTTP_PROTOCOL protocol interfaces.\r
 \r
-  Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.<BR>\r
   (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>\r
 \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\r
-  http://opensource.org/licenses/bsd-license.php.\r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -33,27 +27,29 @@ EFI_HTTP_PROTOCOL  mEfiHttpTemplate = {
 \r
   @param[in]  This                Pointer to EFI_HTTP_PROTOCOL instance.\r
   @param[out] HttpConfigData      Point to buffer for operational parameters of this\r
-                                  HTTP instance.\r
+                                  HTTP instance. It is the responsibility of the caller\r
+                                  to allocate the memory for HttpConfigData and\r
+                                  HttpConfigData->AccessPoint.IPv6Node/IPv4Node. In fact,\r
+                                  it is recommended to allocate sufficient memory to record\r
+                                  IPv6Node since it is big enough for all possibilities.\r
 \r
   @retval EFI_SUCCESS             Operation succeeded.\r
   @retval EFI_INVALID_PARAMETER   One or more of the following conditions is TRUE:\r
                                   This is NULL.\r
                                   HttpConfigData is NULL.\r
-                                  HttpInstance->LocalAddressIsIPv6 is FALSE and\r
-                                  HttpConfigData->IPv4Node is NULL.\r
-                                  HttpInstance->LocalAddressIsIPv6 is TRUE and\r
-                                  HttpConfigData->IPv6Node is NULL.\r
+                                  HttpConfigData->AccessPoint.IPv4Node or\r
+                                  HttpConfigData->AccessPoint.IPv6Node is NULL.\r
   @retval EFI_NOT_STARTED         This EFI HTTP Protocol instance has not been started.\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 EfiHttpGetModeData (\r
-  IN  EFI_HTTP_PROTOCOL         *This,\r
-  OUT EFI_HTTP_CONFIG_DATA      *HttpConfigData\r
+  IN  EFI_HTTP_PROTOCOL     *This,\r
+  OUT EFI_HTTP_CONFIG_DATA  *HttpConfigData\r
   )\r
 {\r
-  HTTP_PROTOCOL                 *HttpInstance;\r
+  HTTP_PROTOCOL  *HttpInstance;\r
 \r
   //\r
   // Check input parameters.\r
@@ -63,10 +59,10 @@ EfiHttpGetModeData (
   }\r
 \r
   HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
-  ASSERT (HttpInstance != NULL);\r
 \r
-  if ((HttpInstance->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv6Node == NULL) ||\r
-      (!HttpInstance->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv4Node == NULL)) {\r
+  if ((HttpConfigData->AccessPoint.IPv6Node == NULL) ||\r
+      (HttpConfigData->AccessPoint.IPv4Node == NULL))\r
+  {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -83,7 +79,7 @@ EfiHttpGetModeData (
       HttpConfigData->AccessPoint.IPv6Node,\r
       &HttpInstance->Ipv6Node,\r
       sizeof (HttpInstance->Ipv6Node)\r
-    );\r
+      );\r
   } else {\r
     CopyMem (\r
       HttpConfigData->AccessPoint.IPv4Node,\r
@@ -115,9 +111,9 @@ EfiHttpGetModeData (
   @retval EFI_INVALID_PARAMETER   One or more of the following conditions is TRUE:\r
                                   This is NULL.\r
                                   HttpConfigData->LocalAddressIsIPv6 is FALSE and\r
-                                  HttpConfigData->IPv4Node is NULL.\r
+                                  HttpConfigData->AccessPoint.IPv4Node is NULL.\r
                                   HttpConfigData->LocalAddressIsIPv6 is TRUE and\r
-                                  HttpConfigData->IPv6Node is NULL.\r
+                                  HttpConfigData->AccessPoint.IPv6Node is NULL.\r
   @retval EFI_ALREADY_STARTED     Reinitialize this HTTP instance without calling\r
                                   Configure() with NULL to reset it.\r
   @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred.\r
@@ -129,27 +125,31 @@ EfiHttpGetModeData (
 EFI_STATUS\r
 EFIAPI\r
 EfiHttpConfigure (\r
-  IN  EFI_HTTP_PROTOCOL         *This,\r
-  IN  EFI_HTTP_CONFIG_DATA      *HttpConfigData\r
-  ) \r
+  IN  EFI_HTTP_PROTOCOL     *This,\r
+  IN  EFI_HTTP_CONFIG_DATA  *HttpConfigData OPTIONAL\r
+  )\r
 {\r
-  HTTP_PROTOCOL                 *HttpInstance;\r
-  EFI_STATUS                    Status;\r
-  \r
+  HTTP_PROTOCOL  *HttpInstance;\r
+  EFI_STATUS     Status;\r
+\r
   //\r
   // Check input parameters.\r
   //\r
-  if (This == NULL ||\r
-      (HttpConfigData != NULL && \r
-       ((HttpConfigData->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv6Node == NULL) ||\r
-        (!HttpConfigData->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv4Node == NULL)))) {\r
+  if ((This == NULL) ||\r
+      ((HttpConfigData != NULL) &&\r
+       ((HttpConfigData->LocalAddressIsIPv6 && (HttpConfigData->AccessPoint.IPv6Node == NULL)) ||\r
+        (!HttpConfigData->LocalAddressIsIPv6 && (HttpConfigData->AccessPoint.IPv4Node == NULL)))))\r
+  {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
-  ASSERT (HttpInstance != NULL && HttpInstance->Service != NULL);\r
+  ASSERT (HttpInstance->Service != NULL);\r
 \r
   if (HttpConfigData != NULL) {\r
+    if (HttpConfigData->HttpVersion >= HttpVersionUnsupported) {\r
+      return EFI_UNSUPPORTED;\r
+    }\r
 \r
     //\r
     // Now configure this HTTP instance.\r
@@ -161,8 +161,9 @@ EfiHttpConfigure (
     HttpInstance->HttpVersion        = HttpConfigData->HttpVersion;\r
     HttpInstance->TimeOutMillisec    = HttpConfigData->TimeOutMillisec;\r
     HttpInstance->LocalAddressIsIPv6 = HttpConfigData->LocalAddressIsIPv6;\r
-    \r
-    if (HttpConfigData->LocalAddressIsIPv6) { \r
+    HttpInstance->ConnectionClose    = FALSE;\r
+\r
+    if (HttpConfigData->LocalAddressIsIPv6) {\r
       CopyMem (\r
         &HttpInstance->Ipv6Node,\r
         HttpConfigData->AccessPoint.IPv6Node,\r
@@ -175,7 +176,7 @@ EfiHttpConfigure (
         sizeof (HttpInstance->IPv4Node)\r
         );\r
     }\r
-    \r
+\r
     //\r
     // Creat Tcp child\r
     //\r
@@ -183,20 +184,18 @@ EfiHttpConfigure (
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
-    \r
+\r
     HttpInstance->State = HTTP_STATE_HTTP_CONFIGED;\r
     return EFI_SUCCESS;\r
-\r
   } else {\r
     //\r
-    // Reset all the resources related to HttpInsance.\r
+    // Reset all the resources related to HttpInstance.\r
     //\r
     HttpCleanProtocol (HttpInstance);\r
     HttpInstance->State = HTTP_STATE_UNCONFIGED;\r
     return EFI_SUCCESS;\r
   }\r
 }\r
\r
 \r
 /**\r
   The Request() function queues an HTTP request to this HTTP instance.\r
@@ -227,40 +226,41 @@ EfiHttpConfigure (
 EFI_STATUS\r
 EFIAPI\r
 EfiHttpRequest (\r
-  IN  EFI_HTTP_PROTOCOL         *This,\r
-  IN  EFI_HTTP_TOKEN            *Token\r
+  IN  EFI_HTTP_PROTOCOL  *This,\r
+  IN  EFI_HTTP_TOKEN     *Token\r
   )\r
 {\r
-  EFI_HTTP_MESSAGE              *HttpMsg;\r
-  EFI_HTTP_REQUEST_DATA         *Request;\r
-  VOID                          *UrlParser;\r
-  EFI_STATUS                    Status;\r
-  CHAR8                         *HostName;\r
-  UINTN                         HostNameSize;\r
-  UINT16                        RemotePort;\r
-  HTTP_PROTOCOL                 *HttpInstance;\r
-  BOOLEAN                       Configure;\r
-  BOOLEAN                       ReConfigure;\r
-  BOOLEAN                       TlsConfigure;\r
-  CHAR8                         *RequestMsg;\r
-  CHAR8                         *Url;\r
-  UINTN                         UrlLen;\r
-  CHAR16                        *HostNameStr;\r
-  HTTP_TOKEN_WRAP               *Wrap;\r
-  CHAR8                         *FileUrl;\r
-  UINTN                         RequestMsgSize;\r
+  EFI_HTTP_MESSAGE       *HttpMsg;\r
+  EFI_HTTP_REQUEST_DATA  *Request;\r
+  VOID                   *UrlParser;\r
+  EFI_STATUS             Status;\r
+  CHAR8                  *HostName;\r
+  UINTN                  HostNameSize;\r
+  UINT16                 RemotePort;\r
+  HTTP_PROTOCOL          *HttpInstance;\r
+  BOOLEAN                Configure;\r
+  BOOLEAN                ReConfigure;\r
+  BOOLEAN                TlsConfigure;\r
+  CHAR8                  *RequestMsg;\r
+  CHAR8                  *Url;\r
+  UINTN                  UrlLen;\r
+  CHAR16                 *HostNameStr;\r
+  HTTP_TOKEN_WRAP        *Wrap;\r
+  CHAR8                  *FileUrl;\r
+  UINTN                  RequestMsgSize;\r
+  EFI_HANDLE             ImageHandle;\r
 \r
   //\r
   // Initializations\r
   //\r
-  Url = NULL;\r
-  UrlParser = NULL;\r
-  RemotePort = 0;\r
-  HostName = NULL;\r
-  RequestMsg = NULL;\r
-  HostNameStr = NULL;\r
-  Wrap = NULL;\r
-  FileUrl = NULL;\r
+  Url          = NULL;\r
+  UrlParser    = NULL;\r
+  RemotePort   = 0;\r
+  HostName     = NULL;\r
+  RequestMsg   = NULL;\r
+  HostNameStr  = NULL;\r
+  Wrap         = NULL;\r
+  FileUrl      = NULL;\r
   TlsConfigure = FALSE;\r
 \r
   if ((This == NULL) || (Token == NULL)) {\r
@@ -275,15 +275,17 @@ EfiHttpRequest (
   Request = HttpMsg->Data.Request;\r
 \r
   //\r
-  // Only support GET, HEAD, PUT and POST method in current implementation.\r
+  // Only support GET, HEAD, DELETE, PATCH, PUT and POST method in current implementation.\r
   //\r
   if ((Request != NULL) && (Request->Method != HttpMethodGet) &&\r
-      (Request->Method != HttpMethodHead) && (Request->Method != HttpMethodPut) && (Request->Method != HttpMethodPost)) {\r
+      (Request->Method != HttpMethodHead) && (Request->Method != HttpMethodDelete) &&\r
+      (Request->Method != HttpMethodPut) && (Request->Method != HttpMethodPost) &&\r
+      (Request->Method != HttpMethodPatch))\r
+  {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
   HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
-  ASSERT (HttpInstance != NULL);\r
 \r
   //\r
   // Capture the method into HttpInstance.\r
@@ -298,14 +300,17 @@ EfiHttpRequest (
 \r
   if (Request == NULL) {\r
     //\r
-    // Request would be NULL only for PUT/POST operation (in the current implementation)\r
+    // Request would be NULL only for PUT/POST/PATCH operation (in the current implementation)\r
     //\r
-    if ((HttpInstance->Method != HttpMethodPut) && (HttpInstance->Method != HttpMethodPost)) {\r
+    if ((HttpInstance->Method != HttpMethodPut) &&\r
+        (HttpInstance->Method != HttpMethodPost) &&\r
+        (HttpInstance->Method != HttpMethodPatch))\r
+    {\r
       return EFI_INVALID_PARAMETER;\r
     }\r
 \r
     //\r
-    // For PUT/POST, we need to have the TCP already configured. Bail out if it is not!\r
+    // For PUT/POST/PATCH, we need to have the TCP already configured. Bail out if it is not!\r
     //\r
     if (HttpInstance->State < HTTP_STATE_TCP_CONFIGED) {\r
       return EFI_INVALID_PARAMETER;\r
@@ -314,7 +319,7 @@ EfiHttpRequest (
     //\r
     // We need to have the Message Body for sending the HTTP message across in these cases.\r
     //\r
-    if (HttpMsg->Body == NULL || HttpMsg->BodyLength == 0) {\r
+    if ((HttpMsg->Body == NULL) || (HttpMsg->BodyLength == 0)) {\r
       return EFI_INVALID_PARAMETER;\r
     }\r
 \r
@@ -334,35 +339,51 @@ EfiHttpRequest (
     //\r
     // Parse the URI of the remote host.\r
     //\r
-    Url = HttpInstance->Url;\r
+    Url    = HttpInstance->Url;\r
     UrlLen = StrLen (Request->Url) + 1;\r
     if (UrlLen > HTTP_URL_BUFFER_LEN) {\r
       Url = AllocateZeroPool (UrlLen);\r
       if (Url == NULL) {\r
         return EFI_OUT_OF_RESOURCES;\r
       }\r
+\r
       FreePool (HttpInstance->Url);\r
       HttpInstance->Url = Url;\r
     }\r
 \r
-\r
     UnicodeStrToAsciiStrS (Request->Url, Url, UrlLen);\r
 \r
     //\r
-    // From the information in Url, the HTTP instance will \r
+    // From the information in Url, the HTTP instance will\r
     // be able to determine whether to use http or https.\r
     //\r
     HttpInstance->UseHttps = IsHttpsUrl (Url);\r
 \r
+    //\r
+    // HTTP is disabled, return directly if the URI is not HTTPS.\r
+    //\r
+    if (!PcdGetBool (PcdAllowHttpConnections) && !(HttpInstance->UseHttps)) {\r
+      DEBUG ((DEBUG_ERROR, "EfiHttpRequest: HTTP is disabled.\n"));\r
+\r
+      return EFI_ACCESS_DENIED;\r
+    }\r
+\r
     //\r
     // Check whether we need to create Tls child and open the TLS protocol.\r
     //\r
-    if (HttpInstance->UseHttps && HttpInstance->TlsChildHandle == NULL) {\r
+    if (HttpInstance->UseHttps && (HttpInstance->TlsChildHandle == NULL)) {\r
       //\r
       // Use TlsSb to create Tls child and open the TLS protocol.\r
       //\r
+      if (HttpInstance->LocalAddressIsIPv6) {\r
+        ImageHandle = HttpInstance->Service->Ip6DriverBindingHandle;\r
+      } else {\r
+        ImageHandle = HttpInstance->Service->Ip4DriverBindingHandle;\r
+      }\r
+\r
       HttpInstance->TlsChildHandle = TlsCreateChild (\r
-                                       HttpInstance->Service->ImageHandle,\r
+                                       ImageHandle,\r
+                                       &(HttpInstance->TlsSb),\r
                                        &(HttpInstance->Tls),\r
                                        &(HttpInstance->TlsConfiguration)\r
                                        );\r
@@ -374,15 +395,27 @@ EfiHttpRequest (
     }\r
 \r
     UrlParser = NULL;\r
-    Status = HttpParseUrl (Url, (UINT32) AsciiStrLen (Url), FALSE, &UrlParser);\r
+    Status    = HttpParseUrl (Url, (UINT32)AsciiStrLen (Url), FALSE, &UrlParser);\r
     if (EFI_ERROR (Status)) {\r
       goto Error1;\r
     }\r
 \r
-    HostName   = NULL;\r
-    Status     = HttpUrlGetHostName (Url, UrlParser, &HostName);\r
+    Status = HttpUrlGetHostName (Url, UrlParser, &HostName);\r
     if (EFI_ERROR (Status)) {\r
-     goto Error1;\r
+      goto Error1;\r
+    }\r
+\r
+    if (HttpInstance->LocalAddressIsIPv6) {\r
+      HostNameSize = AsciiStrSize (HostName);\r
+\r
+      if ((HostNameSize > 2) && (HostName[0] == '[') && (HostName[HostNameSize - 2] == ']')) {\r
+        //\r
+        // HostName format is expressed as IPv6, so, remove '[' and ']'.\r
+        //\r
+        HostNameSize -= 2;\r
+        CopyMem (HostName, HostName + 1, HostNameSize - 1);\r
+        HostName[HostNameSize - 1] = '\0';\r
+      }\r
     }\r
 \r
     Status = HttpUrlGetPort (Url, UrlParser, &RemotePort);\r
@@ -393,6 +426,7 @@ EfiHttpRequest (
         RemotePort = HTTP_DEFAULT_PORT;\r
       }\r
     }\r
+\r
     //\r
     // If Configure is TRUE, it indicates the first time to call Request();\r
     // If ReConfigure is TRUE, it indicates the request URL is not same\r
@@ -407,11 +441,13 @@ EfiHttpRequest (
       //\r
       ReConfigure = FALSE;\r
     } else {\r
-      if ((HttpInstance->RemotePort == RemotePort) &&\r
-          (AsciiStrCmp (HttpInstance->RemoteHost, HostName) == 0) && \r
-          (!HttpInstance->UseHttps || (HttpInstance->UseHttps && \r
-                                       !TlsConfigure && \r
-                                       HttpInstance->TlsSessionState == EfiTlsSessionDataTransferring))) {\r
+      if ((HttpInstance->ConnectionClose == FALSE) &&\r
+          (HttpInstance->RemotePort == RemotePort) &&\r
+          (AsciiStrCmp (HttpInstance->RemoteHost, HostName) == 0) &&\r
+          (!HttpInstance->UseHttps || (HttpInstance->UseHttps &&\r
+                                       !TlsConfigure &&\r
+                                       (HttpInstance->TlsSessionState == EfiTlsSessionDataTransferring))))\r
+      {\r
         //\r
         // Host Name and port number of the request URL are the same with previous call to Request().\r
         // If Https protocol used, the corresponding SessionState is EfiTlsSessionDataTransferring.\r
@@ -445,6 +481,8 @@ EfiHttpRequest (
 \r
           FreePool (HostName);\r
 \r
+          HttpUrlFreeParser (UrlParser);\r
+\r
           //\r
           // Queue the HTTP token and return.\r
           //\r
@@ -467,7 +505,7 @@ EfiHttpRequest (
         }\r
       }\r
     }\r
-  } \r
+  }\r
 \r
   if (Configure) {\r
     //\r
@@ -481,21 +519,24 @@ EfiHttpRequest (
 \r
     if (EFI_ERROR (Status)) {\r
       HostNameSize = AsciiStrSize (HostName);\r
-      HostNameStr = AllocateZeroPool (HostNameSize * sizeof (CHAR16));\r
+      HostNameStr  = AllocateZeroPool (HostNameSize * sizeof (CHAR16));\r
       if (HostNameStr == NULL) {\r
         Status = EFI_OUT_OF_RESOURCES;\r
         goto Error1;\r
       }\r
-      \r
+\r
       AsciiStrToUnicodeStrS (HostName, HostNameStr, HostNameSize);\r
       if (!HttpInstance->LocalAddressIsIPv6) {\r
         Status = HttpDns4 (HttpInstance, HostNameStr, &HttpInstance->RemoteAddr);\r
       } else {\r
         Status = HttpDns6 (HttpInstance, HostNameStr, &HttpInstance->RemoteIpv6Addr);\r
       }\r
-      \r
+\r
+      HttpNotify (HttpEventDns, Status);\r
+\r
       FreePool (HostNameStr);\r
       if (EFI_ERROR (Status)) {\r
+        DEBUG ((DEBUG_ERROR, "Error: Could not retrieve the host address from DNS server.\n"));\r
         goto Error1;\r
       }\r
     }\r
@@ -506,7 +547,7 @@ EfiHttpRequest (
     ASSERT (HttpInstance->RemoteHost == NULL);\r
     HttpInstance->RemotePort = RemotePort;\r
     HttpInstance->RemoteHost = HostName;\r
-    HostName = NULL;\r
+    HostName                 = NULL;\r
   }\r
 \r
   if (ReConfigure) {\r
@@ -524,10 +565,10 @@ EfiHttpRequest (
       if (EFI_ERROR (Status)) {\r
         goto Error1;\r
       }\r
-      \r
+\r
       TlsCloseTxRxEvent (HttpInstance);\r
     }\r
-    \r
+\r
     HttpCloseConnection (HttpInstance);\r
     EfiHttpCancel (This, NULL);\r
   }\r
@@ -541,43 +582,45 @@ EfiHttpRequest (
     goto Error1;\r
   }\r
 \r
-  Wrap->HttpToken      = Token;\r
-  Wrap->HttpInstance   = HttpInstance;\r
+  Wrap->HttpToken    = Token;\r
+  Wrap->HttpInstance = HttpInstance;\r
   if (Request != NULL) {\r
     Wrap->TcpWrap.Method = Request->Method;\r
   }\r
-  \r
+\r
   Status = HttpInitSession (\r
-             HttpInstance, \r
-             Wrap, \r
-             Configure || ReConfigure, \r
+             HttpInstance,\r
+             Wrap,\r
+             Configure || ReConfigure,\r
              TlsConfigure\r
              );\r
+  HttpNotify (HttpEventInitSession, Status);\r
   if (EFI_ERROR (Status)) {\r
     goto Error2;\r
   }\r
 \r
   if (!Configure && !ReConfigure && !TlsConfigure) {\r
     //\r
-    // For the new HTTP token, create TX TCP token events.    \r
+    // For the new HTTP token, create TX TCP token events.\r
     //\r
     Status = HttpCreateTcpTxEvent (Wrap);\r
     if (EFI_ERROR (Status)) {\r
       goto Error1;\r
     }\r
   }\r
-  \r
+\r
   //\r
   // Create request message.\r
   //\r
   FileUrl = Url;\r
-  if (Url != NULL && *FileUrl != '/') {\r
+  if ((Url != NULL) && (*FileUrl != '/')) {\r
     //\r
     // Convert the absolute-URI to the absolute-path\r
     //\r
     while (*FileUrl != ':') {\r
       FileUrl++;\r
     }\r
+\r
     if ((*(FileUrl+1) == '/') && (*(FileUrl+2) == '/')) {\r
       FileUrl += 3;\r
       while (*FileUrl != '/') {\r
@@ -591,13 +634,13 @@ EfiHttpRequest (
 \r
   Status = HttpGenRequestMessage (HttpMsg, FileUrl, &RequestMsg, &RequestMsgSize);\r
 \r
-  if (EFI_ERROR (Status)) {\r
+  if (EFI_ERROR (Status) || (NULL == RequestMsg)) {\r
     goto Error3;\r
   }\r
 \r
   //\r
   // Every request we insert a TxToken and a response call would remove the TxToken.\r
-  // In cases of PUT/POST, after an initial request-response pair, we would do a\r
+  // In cases of PUT/POST/PATCH, after an initial request-response pair, we would do a\r
   // continuous request without a response call. So, in such cases, where Request\r
   // structure is NULL, we would not insert a TxToken.\r
   //\r
@@ -608,25 +651,31 @@ EfiHttpRequest (
     }\r
   }\r
 \r
+  HttpInstance->ConnectionClose = FALSE;\r
+\r
   //\r
   // Transmit the request message.\r
   //\r
   Status = HttpTransmitTcp (\r
              HttpInstance,\r
              Wrap,\r
-             (UINT8*) RequestMsg,\r
+             (UINT8 *)RequestMsg,\r
              RequestMsgSize\r
              );\r
   if (EFI_ERROR (Status)) {\r
-    goto Error5;    \r
+    goto Error5;\r
   }\r
 \r
   DispatchDpc ();\r
-  \r
+\r
   if (HostName != NULL) {\r
     FreePool (HostName);\r
   }\r
-  \r
+\r
+  if (UrlParser != NULL) {\r
+    HttpUrlFreeParser (UrlParser);\r
+  }\r
+\r
   return EFI_SUCCESS;\r
 \r
 Error5:\r
@@ -641,7 +690,7 @@ Error5:
 Error4:\r
   if (RequestMsg != NULL) {\r
     FreePool (RequestMsg);\r
-  }  \r
+  }\r
 \r
 Error3:\r
   if (HttpInstance->UseHttps) {\r
@@ -651,12 +700,13 @@ Error3:
 \r
 Error2:\r
   HttpCloseConnection (HttpInstance);\r
-  \r
+\r
   HttpCloseTcpConnCloseEvent (HttpInstance);\r
   if (NULL != Wrap->TcpWrap.Tx4Token.CompletionToken.Event) {\r
     gBS->CloseEvent (Wrap->TcpWrap.Tx4Token.CompletionToken.Event);\r
     Wrap->TcpWrap.Tx4Token.CompletionToken.Event = NULL;\r
   }\r
+\r
   if (NULL != Wrap->TcpWrap.Tx6Token.CompletionToken.Event) {\r
     gBS->CloseEvent (Wrap->TcpWrap.Tx6Token.CompletionToken.Event);\r
     Wrap->TcpWrap.Tx6Token.CompletionToken.Event = NULL;\r
@@ -666,20 +716,21 @@ Error1:
   if (HostName != NULL) {\r
     FreePool (HostName);\r
   }\r
+\r
   if (Wrap != NULL) {\r
     FreePool (Wrap);\r
   }\r
-  if (UrlParser!= NULL) {\r
+\r
+  if (UrlParser != NULL) {\r
     HttpUrlFreeParser (UrlParser);\r
   }\r
 \r
   return Status;\r
-  \r
 }\r
 \r
 /**\r
-  Cancel a user's Token. \r
\r
+  Cancel a user's Token.\r
+\r
   @param[in]  Map                The HTTP instance's token queue.\r
   @param[in]  Item               Object container for one HTTP token and token's wrap.\r
   @param[in]  Context            The user's token to cancel.\r
@@ -691,16 +742,16 @@ Error1:
 EFI_STATUS\r
 EFIAPI\r
 HttpCancelTokens (\r
-  IN NET_MAP                *Map,\r
-  IN NET_MAP_ITEM           *Item,\r
-  IN VOID                   *Context\r
+  IN NET_MAP       *Map,\r
+  IN NET_MAP_ITEM  *Item,\r
+  IN VOID          *Context\r
   )\r
 {\r
-  EFI_HTTP_TOKEN            *Token;\r
-  HTTP_TOKEN_WRAP           *Wrap;\r
-  HTTP_PROTOCOL             *HttpInstance;\r
+  EFI_HTTP_TOKEN   *Token;\r
+  HTTP_TOKEN_WRAP  *Wrap;\r
+  HTTP_PROTOCOL    *HttpInstance;\r
 \r
-  Token = (EFI_HTTP_TOKEN *) Context;\r
+  Token = (EFI_HTTP_TOKEN *)Context;\r
 \r
   //\r
   // Return EFI_SUCCESS to check the next item in the map if\r
@@ -710,14 +761,14 @@ HttpCancelTokens (
     return EFI_SUCCESS;\r
   }\r
 \r
-  Wrap = (HTTP_TOKEN_WRAP *) Item->Value;\r
+  Wrap = (HTTP_TOKEN_WRAP *)Item->Value;\r
   ASSERT (Wrap != NULL);\r
   HttpInstance = Wrap->HttpInstance;\r
-  \r
+\r
   if (!HttpInstance->LocalAddressIsIPv6) {\r
     if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) {\r
       //\r
-      // Cancle the Token before close its Event.\r
+      // Cancel the Token before close its Event.\r
       //\r
       HttpInstance->Tcp4->Cancel (HttpInstance->Tcp4, &Wrap->TcpWrap.Rx4Token.CompletionToken);\r
 \r
@@ -729,7 +780,7 @@ HttpCancelTokens (
   } else {\r
     if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {\r
       //\r
-      // Cancle the Token before close its Event.\r
+      // Cancel the Token before close its Event.\r
       //\r
       HttpInstance->Tcp6->Cancel (HttpInstance->Tcp6, &Wrap->TcpWrap.Rx6Token.CompletionToken);\r
 \r
@@ -748,7 +799,7 @@ HttpCancelTokens (
     return EFI_ABORTED;\r
   }\r
 \r
-  return EFI_SUCCESS; \r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
@@ -761,17 +812,17 @@ HttpCancelTokens (
                                  cancelled.\r
 \r
   @retval EFI_SUCCESS            The token is cancelled.\r
-  @retval EFI_NOT_FOUND          The asynchronous request or response token is not found.                                 \r
+  @retval EFI_NOT_FOUND          The asynchronous request or response token is not found.\r
   @retval Others                 Other error as indicated.\r
 \r
 **/\r
 EFI_STATUS\r
 HttpCancel (\r
-  IN  HTTP_PROTOCOL             *HttpInstance,\r
-  IN  EFI_HTTP_TOKEN            *Token\r
+  IN  HTTP_PROTOCOL   *HttpInstance,\r
+  IN  EFI_HTTP_TOKEN  *Token\r
   )\r
 {\r
-  EFI_STATUS                    Status;\r
+  EFI_STATUS  Status;\r
 \r
   //\r
   // First check the tokens queued by EfiHttpRequest().\r
@@ -781,7 +832,7 @@ HttpCancel (
     if (Token != NULL) {\r
       if (Status == EFI_ABORTED) {\r
         return EFI_SUCCESS;\r
-      } \r
+      }\r
     } else {\r
       return Status;\r
     }\r
@@ -810,11 +861,10 @@ HttpCancel (
       HttpInstance->Tcp6->Cancel (HttpInstance->Tcp6, &HttpInstance->Tcp6TlsRxToken.CompletionToken);\r
     }\r
   }\r
-  \r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Abort an asynchronous HTTP request or response token.\r
 \r
@@ -840,25 +890,23 @@ HttpCancel (
 EFI_STATUS\r
 EFIAPI\r
 EfiHttpCancel (\r
-  IN  EFI_HTTP_PROTOCOL         *This,\r
-  IN  EFI_HTTP_TOKEN            *Token\r
+  IN  EFI_HTTP_PROTOCOL  *This,\r
+  IN  EFI_HTTP_TOKEN     *Token\r
   )\r
 {\r
-  HTTP_PROTOCOL                 *HttpInstance;\r
+  HTTP_PROTOCOL  *HttpInstance;\r
 \r
   if (This == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
-  ASSERT (HttpInstance != NULL);\r
 \r
   if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r
     return EFI_NOT_STARTED;\r
   }\r
 \r
   return HttpCancel (HttpInstance, Token);\r
-\r
 }\r
 \r
 /**\r
@@ -878,38 +926,36 @@ EfiHttpCancel (
 EFI_STATUS\r
 EFIAPI\r
 HttpBodyParserCallback (\r
-  IN HTTP_BODY_PARSE_EVENT      EventType,\r
-  IN CHAR8                      *Data,\r
-  IN UINTN                      Length,\r
-  IN VOID                       *Context\r
+  IN HTTP_BODY_PARSE_EVENT  EventType,\r
+  IN CHAR8                  *Data,\r
+  IN UINTN                  Length,\r
+  IN VOID                   *Context\r
   )\r
 {\r
-  HTTP_TOKEN_WRAP               *Wrap;\r
-  UINTN                         BodyLength;\r
-  CHAR8                         *Body;\r
+  HTTP_CALLBACK_DATA  *CallbackData;\r
+  HTTP_TOKEN_WRAP     *Wrap;\r
+  UINTN               BodyLength;\r
+  CHAR8               *Body;\r
 \r
   if (EventType != BodyParseEventOnComplete) {\r
     return EFI_SUCCESS;\r
   }\r
 \r
-  if (Data == NULL || Length != 0 || Context == NULL) {\r
+  if ((Data == NULL) || (Length != 0) || (Context == NULL)) {\r
     return EFI_SUCCESS;\r
   }\r
 \r
-  Wrap = (HTTP_TOKEN_WRAP *) Context;\r
-  Body = Wrap->HttpToken->Message->Body;\r
-  BodyLength = Wrap->HttpToken->Message->BodyLength;\r
+  CallbackData = (HTTP_CALLBACK_DATA *)Context;\r
+\r
+  Wrap       = (HTTP_TOKEN_WRAP *)(CallbackData->Wrap);\r
+  Body       = CallbackData->ParseData;\r
+  BodyLength = CallbackData->ParseDataLength;\r
+\r
   if (Data < Body + BodyLength) {\r
     Wrap->HttpInstance->NextMsg = Data;\r
   } else {\r
     Wrap->HttpInstance->NextMsg = NULL;\r
   }\r
-  \r
-\r
-  //\r
-  // Free Tx4Token or Tx6Token since already received corrsponding HTTP response.\r
-  //\r
-  FreePool (Wrap);\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -920,41 +966,43 @@ HttpBodyParserCallback (
   @param[in]  Wrap                Pointer to HTTP token's wrap data.\r
 \r
   @retval EFI_SUCCESS             Allocation succeeded.\r
-  @retval EFI_OUT_OF_RESOURCES    Failed to complete the opration due to lack of resources.\r
-  @retval EFI_NOT_READY           Can't find a corresponding Tx4Token/Tx6Token or \r
+  @retval EFI_OUT_OF_RESOURCES    Failed to complete the operation due to lack of resources.\r
+  @retval EFI_NOT_READY           Can't find a corresponding Tx4Token/Tx6Token or\r
                                   the EFI_HTTP_UTILITIES_PROTOCOL is not available.\r
 \r
 **/\r
 EFI_STATUS\r
 HttpResponseWorker (\r
-  IN  HTTP_TOKEN_WRAP           *Wrap\r
+  IN  HTTP_TOKEN_WRAP  *Wrap\r
   )\r
 {\r
-  EFI_STATUS                    Status;\r
-  EFI_HTTP_MESSAGE              *HttpMsg;\r
-  CHAR8                         *EndofHeader;\r
-  CHAR8                         *HttpHeaders;\r
-  UINTN                         SizeofHeaders;\r
-  UINTN                         BufferSize;\r
-  UINTN                         StatusCode;\r
-  CHAR8                         *Tmp;\r
-  CHAR8                         *HeaderTmp;\r
-  CHAR8                         *StatusCodeStr;\r
-  UINTN                         BodyLen;\r
-  HTTP_PROTOCOL                 *HttpInstance;\r
-  EFI_HTTP_TOKEN                *Token;\r
-  NET_MAP_ITEM                  *Item;\r
-  HTTP_TOKEN_WRAP               *ValueInItem;\r
-  UINTN                         HdrLen;\r
-  NET_FRAGMENT                  Fragment;\r
-\r
-  if (Wrap == NULL || Wrap->HttpInstance == NULL) {\r
+  EFI_STATUS        Status;\r
+  EFI_HTTP_MESSAGE  *HttpMsg;\r
+  CHAR8             *EndofHeader;\r
+  CHAR8             *HttpHeaders;\r
+  UINTN             SizeofHeaders;\r
+  UINTN             BufferSize;\r
+  UINTN             StatusCode;\r
+  CHAR8             *Tmp;\r
+  CHAR8             *HeaderTmp;\r
+  CHAR8             *StatusCodeStr;\r
+  UINTN             BodyLen;\r
+  HTTP_PROTOCOL     *HttpInstance;\r
+  EFI_HTTP_TOKEN    *Token;\r
+  NET_MAP_ITEM      *Item;\r
+  HTTP_TOKEN_WRAP   *ValueInItem;\r
+  UINTN             HdrLen;\r
+  NET_FRAGMENT      Fragment;\r
+  UINT32            TimeoutValue;\r
+  UINTN             Index;\r
+\r
+  if ((Wrap == NULL) || (Wrap->HttpInstance == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   HttpInstance = Wrap->HttpInstance;\r
-  Token = Wrap->HttpToken;\r
-  HttpMsg = Token->Message;\r
+  Token        = Wrap->HttpToken;\r
+  HttpMsg      = Token->Message;\r
 \r
   HttpInstance->EndofHeader = NULL;\r
   HttpInstance->HttpHeaders = NULL;\r
@@ -966,7 +1014,7 @@ HttpResponseWorker (
   ValueInItem               = NULL;\r
   Fragment.Len              = 0;\r
   Fragment.Bulk             = NULL;\r
\r
+\r
   if (HttpMsg->Data.Response != NULL) {\r
     //\r
     // Check whether we have cached header from previous call.\r
@@ -975,7 +1023,7 @@ HttpResponseWorker (
       //\r
       // The data is stored at [NextMsg, CacheBody + CacheLen].\r
       //\r
-      HdrLen = HttpInstance->CacheBody + HttpInstance->CacheLen - HttpInstance->NextMsg;\r
+      HdrLen      = HttpInstance->CacheBody + HttpInstance->CacheLen - HttpInstance->NextMsg;\r
       HttpHeaders = AllocateZeroPool (HdrLen);\r
       if (HttpHeaders == NULL) {\r
         Status = EFI_OUT_OF_RESOURCES;\r
@@ -987,19 +1035,18 @@ HttpResponseWorker (
       HttpInstance->CacheBody   = NULL;\r
       HttpInstance->NextMsg     = NULL;\r
       HttpInstance->CacheOffset = 0;\r
-      SizeofHeaders = HdrLen;\r
-      BufferSize = HttpInstance->CacheLen;\r
+      SizeofHeaders             = HdrLen;\r
+      BufferSize                = HttpInstance->CacheLen;\r
 \r
       //\r
       // Check whether we cached the whole HTTP headers.\r
       //\r
-      EndofHeader = AsciiStrStr (HttpHeaders, HTTP_END_OF_HDR_STR); \r
-    }   \r
+      EndofHeader = AsciiStrStr (HttpHeaders, HTTP_END_OF_HDR_STR);\r
+    }\r
 \r
     HttpInstance->EndofHeader = &EndofHeader;\r
     HttpInstance->HttpHeaders = &HttpHeaders;\r
 \r
-\r
     if (HttpInstance->TimeoutEvent == NULL) {\r
       //\r
       // Create TimeoutEvent for response\r
@@ -1016,10 +1063,15 @@ HttpResponseWorker (
       }\r
     }\r
 \r
+    //\r
+    // Get HTTP timeout value\r
+    //\r
+    TimeoutValue = PcdGet32 (PcdHttpIoTimeout);\r
+\r
     //\r
     // Start the timer, and wait Timeout seconds to receive the header packet.\r
     //\r
-    Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_RESPONSE_TIMEOUT * TICKS_PER_SECOND);\r
+    Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, TimeoutValue * TICKS_PER_MS);\r
     if (EFI_ERROR (Status)) {\r
       goto Error;\r
     }\r
@@ -1087,21 +1139,21 @@ HttpResponseWorker (
     }\r
 \r
     HttpMsg->Data.Response->StatusCode = HttpMappingToStatusCode (StatusCode);\r
-    HttpInstance->StatusCode = StatusCode;\r
+    HttpInstance->StatusCode           = StatusCode;\r
 \r
-    Status = EFI_NOT_READY;\r
+    Status      = EFI_NOT_READY;\r
     ValueInItem = NULL;\r
 \r
     //\r
-    // In cases of PUT/POST, after an initial request-response pair, we would do a\r
+    // In cases of PUT/POST/PATCH, after an initial request-response pair, we would do a\r
     // continuous request without a response call. So, we would not do an insert of\r
     // TxToken. After we have sent the complete file, we will call a response to get\r
     // a final response from server. In such a case, we would not have any TxTokens.\r
     // Hence, check that case before doing a NetMapRemoveHead.\r
     //\r
     if (!NetMapIsEmpty (&HttpInstance->TxTokens)) {\r
-      NetMapRemoveHead (&HttpInstance->TxTokens, (VOID**) &ValueInItem);\r
-      if (ValueInItem == NULL)  {\r
+      NetMapRemoveHead (&HttpInstance->TxTokens, (VOID **)&ValueInItem);\r
+      if (ValueInItem == NULL) {\r
         goto Error;\r
       }\r
 \r
@@ -1149,6 +1201,15 @@ HttpResponseWorker (
       FreePool (HttpHeaders);\r
       HttpHeaders = NULL;\r
 \r
+      for (Index = 0; Index < HttpMsg->HeaderCount; ++Index) {\r
+        if ((AsciiStriCmp ("Connection", HttpMsg->Headers[Index].FieldName) == 0) &&\r
+            (AsciiStriCmp ("close", HttpMsg->Headers[Index].FieldValue) == 0))\r
+        {\r
+          DEBUG ((DEBUG_VERBOSE, "Http: 'Connection: close' header received.\n"));\r
+          HttpInstance->ConnectionClose = TRUE;\r
+          break;\r
+        }\r
+      }\r
 \r
       //\r
       // Init message-body parser by header information.\r
@@ -1159,7 +1220,7 @@ HttpResponseWorker (
                  HttpMsg->HeaderCount,\r
                  HttpMsg->Headers,\r
                  HttpBodyParserCallback,\r
-                 (VOID *) ValueInItem,\r
+                 (VOID *)(&HttpInstance->CallbackData),\r
                  &HttpInstance->MsgParser\r
                  );\r
       if (EFI_ERROR (Status)) {\r
@@ -1170,18 +1231,28 @@ HttpResponseWorker (
       // Check whether we received a complete HTTP message.\r
       //\r
       if (HttpInstance->CacheBody != NULL) {\r
+        //\r
+        // Record the CallbackData data.\r
+        //\r
+        HttpInstance->CallbackData.Wrap            = (VOID *)Wrap;\r
+        HttpInstance->CallbackData.ParseData       = (VOID *)HttpInstance->CacheBody;\r
+        HttpInstance->CallbackData.ParseDataLength = HttpInstance->CacheLen;\r
+\r
+        //\r
+        // Parse message with CallbackData data.\r
+        //\r
         Status = HttpParseMessageBody (HttpInstance->MsgParser, HttpInstance->CacheLen, HttpInstance->CacheBody);\r
         if (EFI_ERROR (Status)) {\r
           goto Error2;\r
         }\r
+      }\r
 \r
-        if (HttpIsMessageComplete (HttpInstance->MsgParser)) {\r
-          //\r
-          // Free the MsgParse since we already have a full HTTP message.\r
-          //\r
-          HttpFreeMsgParser (HttpInstance->MsgParser);\r
-          HttpInstance->MsgParser = NULL;\r
-        }\r
+      if (HttpIsMessageComplete (HttpInstance->MsgParser)) {\r
+        //\r
+        // Free the MsgParse since we already have a full HTTP message.\r
+        //\r
+        HttpFreeMsgParser (HttpInstance->MsgParser);\r
+        HttpInstance->MsgParser = NULL;\r
       }\r
     }\r
 \r
@@ -1207,7 +1278,7 @@ HttpResponseWorker (
       //\r
       // We have a cached HTTP message which includes a part of HTTP header of next message.\r
       //\r
-      BodyLen = HttpInstance->NextMsg - (HttpInstance->CacheBody + HttpInstance->CacheOffset);      \r
+      BodyLen = HttpInstance->NextMsg - (HttpInstance->CacheBody + HttpInstance->CacheOffset);\r
     } else {\r
       BodyLen = HttpInstance->CacheLen - HttpInstance->CacheOffset;\r
     }\r
@@ -1225,7 +1296,7 @@ HttpResponseWorker (
         //\r
         CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, BodyLen);\r
         HttpInstance->CacheOffset = BodyLen + HttpInstance->CacheOffset;\r
-        HttpMsg->BodyLength = BodyLen;\r
+        HttpMsg->BodyLength       = BodyLen;\r
 \r
         if (HttpInstance->NextMsg == NULL) {\r
           //\r
@@ -1237,21 +1308,22 @@ HttpResponseWorker (
           HttpInstance->CacheOffset = 0;\r
         }\r
       }\r
+\r
       //\r
-      // Return since we aready received required data.\r
+      // Return since we already received required data.\r
       //\r
       Status = EFI_SUCCESS;\r
       goto Exit;\r
-    } \r
+    }\r
 \r
-    if (BodyLen == 0 && HttpInstance->MsgParser == NULL) {\r
+    if ((BodyLen == 0) && (HttpInstance->MsgParser == NULL)) {\r
       //\r
       // We received a complete HTTP message, and we don't have more data to return to caller.\r
       //\r
       HttpMsg->BodyLength = 0;\r
-      Status = EFI_SUCCESS;\r
-      goto Exit;      \r
-    }    \r
+      Status              = EFI_SUCCESS;\r
+      goto Exit;\r
+    }\r
   }\r
 \r
   ASSERT (HttpInstance->MsgParser != NULL);\r
@@ -1265,7 +1337,6 @@ HttpResponseWorker (
     if (EFI_ERROR (Status)) {\r
       goto Error2;\r
     }\r
-    \r
   } else {\r
     if (HttpInstance->TimeoutEvent == NULL) {\r
       //\r
@@ -1283,29 +1354,48 @@ HttpResponseWorker (
       }\r
     }\r
 \r
+    //\r
+    // Get HTTP timeout value\r
+    //\r
+    TimeoutValue = PcdGet32 (PcdHttpIoTimeout);\r
+\r
     //\r
     // Start the timer, and wait Timeout seconds to receive the body packet.\r
     //\r
-    Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_RESPONSE_TIMEOUT * TICKS_PER_SECOND);\r
+    Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, TimeoutValue * TICKS_PER_MS);\r
     if (EFI_ERROR (Status)) {\r
       goto Error2;\r
     }\r
-  \r
+\r
     Status = HttpsReceive (HttpInstance, &Fragment, HttpInstance->TimeoutEvent);\r
 \r
     gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0);\r
-    \r
+\r
     if (EFI_ERROR (Status)) {\r
       goto Error2;\r
     }\r
 \r
     //\r
-    // Check whether we receive a complete HTTP message.\r
+    // Process the received the body packet.\r
+    //\r
+    HttpMsg->BodyLength = MIN ((UINTN)Fragment.Len, HttpMsg->BodyLength);\r
+\r
+    CopyMem (HttpMsg->Body, Fragment.Bulk, HttpMsg->BodyLength);\r
+\r
+    //\r
+    // Record the CallbackData data.\r
+    //\r
+    HttpInstance->CallbackData.Wrap            = (VOID *)Wrap;\r
+    HttpInstance->CallbackData.ParseData       = HttpMsg->Body;\r
+    HttpInstance->CallbackData.ParseDataLength = HttpMsg->BodyLength;\r
+\r
+    //\r
+    // Parse Body with CallbackData data.\r
     //\r
     Status = HttpParseMessageBody (\r
                HttpInstance->MsgParser,\r
-               (UINTN) Fragment.Len,\r
-               (CHAR8 *) Fragment.Bulk\r
+               HttpMsg->BodyLength,\r
+               HttpMsg->Body\r
                );\r
     if (EFI_ERROR (Status)) {\r
       goto Error2;\r
@@ -1320,46 +1410,28 @@ HttpResponseWorker (
     }\r
 \r
     //\r
-    // We receive part of header of next HTTP msg.\r
+    // Check whether there is the next message header in the HttpMsg->Body.\r
     //\r
     if (HttpInstance->NextMsg != NULL) {\r
-      HttpMsg->BodyLength = MIN ((UINTN) (HttpInstance->NextMsg - (CHAR8 *) Fragment.Bulk), HttpMsg->BodyLength);\r
-      CopyMem (HttpMsg->Body, Fragment.Bulk, HttpMsg->BodyLength);\r
-      \r
-      HttpInstance->CacheLen = Fragment.Len - HttpMsg->BodyLength;\r
-      if (HttpInstance->CacheLen != 0) {\r
-        if (HttpInstance->CacheBody != NULL) {\r
-          FreePool (HttpInstance->CacheBody);\r
-        }\r
-        \r
-        HttpInstance->CacheBody = AllocateZeroPool (HttpInstance->CacheLen);\r
-        if (HttpInstance->CacheBody == NULL) {\r
-          Status = EFI_OUT_OF_RESOURCES;\r
-          goto Error2;\r
-        }\r
-        \r
-        CopyMem (HttpInstance->CacheBody, Fragment.Bulk + HttpMsg->BodyLength, HttpInstance->CacheLen);\r
-        HttpInstance->CacheOffset = 0;\r
+      HttpMsg->BodyLength = HttpInstance->NextMsg - (CHAR8 *)HttpMsg->Body;\r
+    }\r
 \r
-        HttpInstance->NextMsg = HttpInstance->CacheBody + (UINTN) (HttpInstance->NextMsg - (CHAR8 *) (Fragment.Bulk + HttpMsg->BodyLength));\r
+    HttpInstance->CacheLen = Fragment.Len - HttpMsg->BodyLength;\r
+    if (HttpInstance->CacheLen != 0) {\r
+      if (HttpInstance->CacheBody != NULL) {\r
+        FreePool (HttpInstance->CacheBody);\r
       }\r
-    } else {\r
-      HttpMsg->BodyLength = MIN (Fragment.Len, (UINT32) HttpMsg->BodyLength);\r
-      CopyMem (HttpMsg->Body, Fragment.Bulk, HttpMsg->BodyLength);\r
-      HttpInstance->CacheLen = Fragment.Len - HttpMsg->BodyLength;\r
-      if (HttpInstance->CacheLen != 0) {\r
-        if (HttpInstance->CacheBody != NULL) {\r
-          FreePool (HttpInstance->CacheBody);\r
-        }\r
-        \r
-        HttpInstance->CacheBody = AllocateZeroPool (HttpInstance->CacheLen);\r
-        if (HttpInstance->CacheBody == NULL) {\r
-          Status = EFI_OUT_OF_RESOURCES;\r
-          goto Error2;\r
-        }\r
 \r
-        CopyMem (HttpInstance->CacheBody, Fragment.Bulk + HttpMsg->BodyLength, HttpInstance->CacheLen);\r
-        HttpInstance->CacheOffset = 0;\r
+      HttpInstance->CacheBody = AllocateZeroPool (HttpInstance->CacheLen);\r
+      if (HttpInstance->CacheBody == NULL) {\r
+        Status = EFI_OUT_OF_RESOURCES;\r
+        goto Error2;\r
+      }\r
+\r
+      CopyMem (HttpInstance->CacheBody, Fragment.Bulk + HttpMsg->BodyLength, HttpInstance->CacheLen);\r
+      HttpInstance->CacheOffset = 0;\r
+      if (HttpInstance->NextMsg != NULL) {\r
+        HttpInstance->NextMsg = HttpInstance->CacheBody;\r
       }\r
     }\r
 \r
@@ -1406,7 +1478,7 @@ Error:
   } else {\r
     FreePool (Wrap);\r
   }\r
-  \r
+\r
   if (HttpHeaders != NULL) {\r
     FreePool (HttpHeaders);\r
     HttpHeaders = NULL;\r
@@ -1435,11 +1507,9 @@ Error:
 \r
   gBS->SignalEvent (Token->Event);\r
 \r
-  return Status;  \r
-\r
+  return Status;\r
 }\r
 \r
-\r
 /**\r
   The Response() function queues an HTTP response to this HTTP instance, similar to\r
   Receive() function in the EFI TCP driver. When the HTTP response is received successfully,\r
@@ -1490,14 +1560,14 @@ Error:
 EFI_STATUS\r
 EFIAPI\r
 EfiHttpResponse (\r
-  IN  EFI_HTTP_PROTOCOL         *This,\r
-  IN  EFI_HTTP_TOKEN            *Token\r
+  IN  EFI_HTTP_PROTOCOL  *This,\r
+  IN  EFI_HTTP_TOKEN     *Token\r
   )\r
 {\r
-  EFI_STATUS                    Status;\r
-  EFI_HTTP_MESSAGE              *HttpMsg;\r
-  HTTP_PROTOCOL                 *HttpInstance;\r
-  HTTP_TOKEN_WRAP               *Wrap;\r
+  EFI_STATUS        Status;\r
+  EFI_HTTP_MESSAGE  *HttpMsg;\r
+  HTTP_PROTOCOL     *HttpInstance;\r
+  HTTP_TOKEN_WRAP   *Wrap;\r
 \r
   if ((This == NULL) || (Token == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -1507,9 +1577,8 @@ EfiHttpResponse (
   if (HttpMsg == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
-  ASSERT (HttpInstance != NULL);\r
 \r
   if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r
     return EFI_NOT_STARTED;\r
@@ -1519,7 +1588,7 @@ EfiHttpResponse (
   // Check whether the token already existed.\r
   //\r
   if (EFI_ERROR (NetMapIterate (&HttpInstance->RxTokens, HttpTokenExist, Token))) {\r
-    return EFI_ACCESS_DENIED;   \r
+    return EFI_ACCESS_DENIED;\r
   }\r
 \r
   Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r
@@ -1531,8 +1600,8 @@ EfiHttpResponse (
   Wrap->HttpToken    = Token;\r
 \r
   //\r
-  // Notes: For Https, receive token wrapped in HTTP_TOKEN_WRAP is not used to \r
-  // receive the https response. A special TlsRxToken is used for receiving TLS \r
+  // Notes: For Https, receive token wrapped in HTTP_TOKEN_WRAP is not used to\r
+  // receive the https response. A special TlsRxToken is used for receiving TLS\r
   // related messages. It should be a blocking response.\r
   //\r
   if (!HttpInstance->UseHttps) {\r
@@ -1565,10 +1634,11 @@ Error:
     if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {\r
       gBS->CloseEvent (Wrap->TcpWrap.Rx6Token.CompletionToken.Event);\r
     }\r
+\r
     FreePool (Wrap);\r
-  }  \r
+  }\r
 \r
-  return Status;  \r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -1594,36 +1664,37 @@ Error:
 EFI_STATUS\r
 EFIAPI\r
 EfiHttpPoll (\r
-  IN  EFI_HTTP_PROTOCOL         *This\r
+  IN  EFI_HTTP_PROTOCOL  *This\r
   )\r
 {\r
-  EFI_STATUS                    Status;\r
-  HTTP_PROTOCOL                 *HttpInstance;\r
+  EFI_STATUS     Status;\r
+  HTTP_PROTOCOL  *HttpInstance;\r
 \r
   if (This == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
-  ASSERT (HttpInstance != NULL);\r
 \r
   if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r
     return EFI_NOT_STARTED;\r
   }\r
-  \r
+\r
   if (HttpInstance->LocalAddressIsIPv6) {\r
     if (HttpInstance->Tcp6 == NULL) {\r
       return EFI_NOT_STARTED;\r
     }\r
+\r
     Status = HttpInstance->Tcp6->Poll (HttpInstance->Tcp6);\r
   } else {\r
     if (HttpInstance->Tcp4 == NULL) {\r
       return EFI_NOT_STARTED;\r
     }\r
+\r
     Status = HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);\r
   }\r
-  \r
+\r
   DispatchDpc ();\r
\r
+\r
   return Status;\r
 }\r