]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/HttpDxe/HttpImpl.c
NetworkPkg: Avoid memory allocation for each HTTP message exchange.
[mirror_edk2.git] / NetworkPkg / HttpDxe / HttpImpl.c
index 4bd4ac884fdcffdcbe303d998c35178d70202fe9..c5b2be430e29261f7e6eab37585347af95d3e673 100644 (file)
@@ -224,10 +224,12 @@ EfiHttpRequest (
   BOOLEAN                       ReConfigure;\r
   CHAR8                         *RequestStr;\r
   CHAR8                         *Url;\r
+  UINTN                         UrlLen;\r
   CHAR16                        *HostNameStr;\r
   HTTP_TOKEN_WRAP               *Wrap;\r
   HTTP_TCP_TOKEN_WRAP           *TcpWrap;\r
-\r
+  CHAR8                         *FileUrl;\r
+  \r
   if ((This == NULL) || (Token == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
@@ -282,10 +284,15 @@ EfiHttpRequest (
   //\r
   // Parse the URI of the remote host.\r
   //\r
-  Url = AllocatePool (StrLen (Request->Url) + 1);\r
-  if (Url == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\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
+    FreePool (HttpInstance->Url);\r
+    HttpInstance->Url = Url;    \r
+  }  \r
 \r
   UnicodeStrToAsciiStr (Request->Url, Url);\r
   UrlParser = NULL;\r
@@ -346,7 +353,6 @@ EfiHttpRequest (
 \r
         Wrap->TcpWrap.Method = Request->Method;\r
 \r
-        FreePool (Url);\r
         FreePool (HostName);\r
         \r
         //\r
@@ -450,7 +456,25 @@ EfiHttpRequest (
   //\r
   // Create request message.\r
   //\r
-  RequestStr = HttpGenRequestString (HttpInstance, HttpMsg, Url);\r
+  FileUrl = Url;\r
+  if (*FileUrl != '/') {\r
+    //\r
+    // Convert the absolute-URI to the absolute-path\r
+    //\r
+    while (*FileUrl != ':') {\r
+      FileUrl++;\r
+    }\r
+    if ((*(FileUrl+1) == '/') && (*(FileUrl+2) == '/')) {\r
+      FileUrl += 3;\r
+      while (*FileUrl != '/') {\r
+        FileUrl++;\r
+      }\r
+    } else {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto Error3;\r
+    }\r
+  }\r
+  RequestStr = HttpGenRequestString (HttpInstance, HttpMsg, FileUrl);\r
   if (RequestStr == NULL) {\r
     Status = EFI_OUT_OF_RESOURCES;\r
     goto Error3;\r
@@ -461,7 +485,6 @@ EfiHttpRequest (
     goto Error4;\r
   }\r
 \r
-  FreePool (Url);\r
   if (HostName != NULL) {\r
     FreePool (HostName);\r
   }\r
@@ -497,12 +520,10 @@ Error2:
   HttpCloseTcp4ConnCloseEvent (HttpInstance);\r
   if (NULL != Wrap->TcpWrap.TxToken.CompletionToken.Event) {\r
     gBS->CloseEvent (Wrap->TcpWrap.TxToken.CompletionToken.Event);\r
+    Wrap->TcpWrap.TxToken.CompletionToken.Event = NULL;\r
   }\r
 \r
 Error1:\r
-  if (Url != NULL) {\r
-    FreePool (Url);\r
-  }\r
   if (HostName != NULL) {\r
     FreePool (HostName);\r
   }\r
@@ -738,7 +759,8 @@ HttpBodyParserCallback (
 \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 TxToken.\r
+  @retval EFI_NOT_READY           Can't find a corresponding TxToken or \r
+                                  the EFI_HTTP_UTILITIES_PROTOCOL is not available.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -766,6 +788,10 @@ HttpResponseWorker (
   HTTP_TOKEN_WRAP               *ValueInItem;\r
   UINTN                         HdrLen;\r
 \r
+  if (Wrap == NULL || Wrap->HttpInstance == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  \r
   HttpInstance = Wrap->HttpInstance;\r
   Token = Wrap->HttpToken;\r
 \r
@@ -930,10 +956,25 @@ HttpResponseWorker (
     CopyMem (HeaderTmp, Tmp, SizeofHeaders);\r
     FreePool (HttpHeaders);\r
     HttpHeaders = HeaderTmp;\r
+\r
+    //\r
+    // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.\r
+    //\r
+    if (mHttpUtilities == NULL) {\r
+      Status = EFI_NOT_READY;\r
+      goto Error;\r
+    }\r
+    \r
     //\r
     // Parse the HTTP header into array of key/value pairs.\r
     //\r
-    Status = HttpUtilitiesParse (HttpHeaders, SizeofHeaders, &HttpMsg->Headers, &HttpMsg->HeaderCount);\r
+    Status = mHttpUtilities->Parse (\r
+                               mHttpUtilities, \r
+                               HttpHeaders, \r
+                               SizeofHeaders, \r
+                               &HttpMsg->Headers, \r
+                               &HttpMsg->HeaderCount\r
+                               );\r
     if (EFI_ERROR (Status)) {\r
       goto Error;\r
     }\r