2 Implementation of EFI_HTTP_PROTOCOL protocol interfaces.
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "HttpDriver.h"
18 EFI_HTTP_PROTOCOL mEfiHttpTemplate
= {
28 Returns the operational parameters for the current HTTP child instance.
30 The GetModeData() function is used to read the current mode data (operational
31 parameters) for this HTTP protocol instance.
33 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
34 @param[out] HttpConfigData Point to buffer for operational parameters of this
37 @retval EFI_SUCCESS Operation succeeded.
38 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
40 HttpConfigData is NULL.
41 HttpConfigData->AccessPoint is NULL.
42 @retval EFI_NOT_STARTED The HTTP instance is not configured.
48 IN EFI_HTTP_PROTOCOL
*This
,
49 OUT EFI_HTTP_CONFIG_DATA
*HttpConfigData
52 HTTP_PROTOCOL
*HttpInstance
;
54 if ((This
== NULL
) || (HttpConfigData
== NULL
)) {
55 return EFI_INVALID_PARAMETER
;
58 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
59 ASSERT (HttpInstance
!= NULL
);
61 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
62 return EFI_NOT_STARTED
;
65 if (HttpConfigData
->AccessPoint
.IPv4Node
== NULL
) {
66 return EFI_INVALID_PARAMETER
;
69 HttpConfigData
->HttpVersion
= HttpInstance
->HttpVersion
;
70 HttpConfigData
->TimeOutMillisec
= HttpInstance
->TimeOutMillisec
;
71 HttpConfigData
->LocalAddressIsIPv6
= HttpInstance
->LocalAddressIsIPv6
;
74 HttpConfigData
->AccessPoint
.IPv4Node
,
75 &HttpInstance
->IPv4Node
,
76 sizeof (HttpInstance
->IPv4Node
)
83 Initialize or brutally reset the operational parameters for this EFI HTTP instance.
85 The Configure() function does the following:
86 When HttpConfigData is not NULL Initialize this EFI HTTP instance by configuring
87 timeout, local address, port, etc.
88 When HttpConfigData is NULL, reset this EFI HTTP instance by closing all active
89 connections with remote hosts, canceling all asynchronous tokens, and flush request
90 and response buffers without informing the appropriate hosts.
92 Except for GetModeData() and Configure(), No other EFI HTTP function can be executed
93 by this instance until the Configure() function is executed and returns successfully.
95 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
96 @param[in] HttpConfigData Pointer to the configure data to configure the instance.
98 @retval EFI_SUCCESS Operation succeeded.
99 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
101 HttpConfigData->LocalAddressIsIPv6 is FALSE and
102 HttpConfigData->IPv4Node is NULL.
103 HttpConfigData->LocalAddressIsIPv6 is TRUE and
104 HttpConfigData->IPv6Node is NULL.
105 @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling
106 Configure() with NULL to reset it.
107 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
108 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when
109 executing Configure().
110 @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported
111 in the implementation.
116 IN EFI_HTTP_PROTOCOL
*This
,
117 IN EFI_HTTP_CONFIG_DATA
*HttpConfigData
120 HTTP_PROTOCOL
*HttpInstance
;
124 return EFI_INVALID_PARAMETER
;
127 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
128 ASSERT (HttpInstance
!= NULL
&& HttpInstance
->Service
!= NULL
);
130 if (HttpConfigData
!= NULL
) {
132 // Check input parameters.
134 if (HttpConfigData
->LocalAddressIsIPv6
) {
135 if (HttpConfigData
->AccessPoint
.IPv6Node
== NULL
) {
136 return EFI_INVALID_PARAMETER
;
139 if (HttpConfigData
->AccessPoint
.IPv4Node
== NULL
) {
140 return EFI_INVALID_PARAMETER
;
144 // Now configure this HTTP instance.
146 if (HttpInstance
->State
!= HTTP_STATE_UNCONFIGED
) {
147 return EFI_ALREADY_STARTED
;
150 HttpInstance
->HttpVersion
= HttpConfigData
->HttpVersion
;
151 HttpInstance
->TimeOutMillisec
= HttpConfigData
->TimeOutMillisec
;
152 HttpInstance
->LocalAddressIsIPv6
= HttpConfigData
->LocalAddressIsIPv6
;
154 if (HttpConfigData
->LocalAddressIsIPv6
) {
155 return EFI_UNSUPPORTED
;
158 &HttpInstance
->IPv4Node
,
159 HttpConfigData
->AccessPoint
.IPv4Node
,
160 sizeof (HttpInstance
->IPv4Node
)
163 HttpInstance
->State
= HTTP_STATE_HTTP_CONFIGED
;
168 if (HttpInstance
->LocalAddressIsIPv6
) {
169 return EFI_UNSUPPORTED
;
171 HttpCleanProtocol (HttpInstance
);
172 Status
= HttpInitProtocol (HttpInstance
->Service
, HttpInstance
);
173 if (EFI_ERROR (Status
)) {
177 HttpInstance
->State
= HTTP_STATE_UNCONFIGED
;
185 The Request() function queues an HTTP request to this HTTP instance.
187 Similar to Transmit() function in the EFI TCP driver. When the HTTP request is sent
188 successfully, or if there is an error, Status in token will be updated and Event will
191 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
192 @param[in] Token Pointer to storage containing HTTP request token.
194 @retval EFI_SUCCESS Outgoing data was processed.
195 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
196 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
197 @retval EFI_TIMEOUT Data was dropped out of the transmit or receive queue.
198 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
199 @retval EFI_UNSUPPORTED The HTTP method is not supported in current
201 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
203 Token->Message is NULL.
204 Token->Message->Body is not NULL,
205 Token->Message->BodyLength is non-zero, and
206 Token->Message->Data is NULL, but a previous call to
207 Request()has not been completed successfully.
212 IN EFI_HTTP_PROTOCOL
*This
,
213 IN EFI_HTTP_TOKEN
*Token
216 EFI_HTTP_MESSAGE
*HttpMsg
;
217 EFI_HTTP_REQUEST_DATA
*Request
;
222 HTTP_PROTOCOL
*HttpInstance
;
228 HTTP_TOKEN_WRAP
*Wrap
;
229 HTTP_TCP_TOKEN_WRAP
*TcpWrap
;
232 if ((This
== NULL
) || (Token
== NULL
)) {
233 return EFI_INVALID_PARAMETER
;
236 HttpMsg
= Token
->Message
;
237 if ((HttpMsg
== NULL
) || (HttpMsg
->Headers
== NULL
)) {
238 return EFI_INVALID_PARAMETER
;
242 // Current implementation does not support POST/PUT method.
243 // If future version supports these two methods, Request could be NULL for a special case that to send large amounts
244 // of data. For this case, the implementation need check whether previous call to Request() has been completed or not.
247 Request
= HttpMsg
->Data
.Request
;
248 if ((Request
== NULL
) || (Request
->Url
== NULL
)) {
249 return EFI_INVALID_PARAMETER
;
253 // Only support GET and HEAD method in current implementation.
255 if ((Request
->Method
!= HttpMethodGet
) && (Request
->Method
!= HttpMethodHead
)) {
256 return EFI_UNSUPPORTED
;
259 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
260 ASSERT (HttpInstance
!= NULL
);
262 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
263 return EFI_NOT_STARTED
;
266 if (HttpInstance
->LocalAddressIsIPv6
) {
267 return EFI_UNSUPPORTED
;
271 // Check whether the token already existed.
273 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTokenExist
, Token
))) {
274 return EFI_ACCESS_DENIED
;
284 // Parse the URI of the remote host.
286 Url
= AllocatePool (StrLen (Request
->Url
) + 1);
288 return EFI_OUT_OF_RESOURCES
;
291 UnicodeStrToAsciiStr (Request
->Url
, Url
);
293 Status
= HttpParseUrl (Url
, (UINT32
) AsciiStrLen (Url
), FALSE
, &UrlParser
);
294 if (EFI_ERROR (Status
)) {
300 Status
= HttpUrlGetHostName (Url
, UrlParser
, &HostName
);
301 if (EFI_ERROR (Status
)) {
305 Status
= HttpUrlGetPort (Url
, UrlParser
, &RemotePort
);
306 if (EFI_ERROR (Status
)) {
307 RemotePort
= HTTP_DEFAULT_PORT
;
313 if (HttpInstance
->RemoteHost
== NULL
&& HttpInstance
->RemotePort
== 0) {
315 // Request() is called the first time.
319 if ((HttpInstance
->RemotePort
== RemotePort
) &&
320 (AsciiStrCmp (HttpInstance
->RemoteHost
, HostName
) == 0)) {
322 // Host Name and port number of the request URL are the same with previous call to Request().
323 // Check whether previous TCP packet sent out.
325 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTcpNotReady
, NULL
))) {
327 // Wrap the HTTP token in HTTP_TOKEN_WRAP
329 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
331 Status
= EFI_OUT_OF_RESOURCES
;
335 Wrap
->HttpToken
= Token
;
336 Wrap
->HttpInstance
= HttpInstance
;
338 Status
= HttpCreateTcp4TxEvent (Wrap
);
339 if (EFI_ERROR (Status
)) {
343 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
344 if (EFI_ERROR (Status
)) {
348 Wrap
->TcpWrap
.Method
= Request
->Method
;
354 // Queue the HTTP token and return.
359 // Use existing TCP instance to transmit the packet.
366 // Need close existing TCP instance and create a new TCP instance for data transmit.
368 if (HttpInstance
->RemoteHost
!= NULL
) {
369 FreePool (HttpInstance
->RemoteHost
);
370 HttpInstance
->RemoteHost
= NULL
;
377 // Parse Url for IPv4 address, if failed, perform DNS resolution.
379 Status
= NetLibAsciiStrToIp4 (HostName
, &HttpInstance
->RemoteAddr
);
380 if (EFI_ERROR (Status
)) {
381 HostNameStr
= AllocateZeroPool ((AsciiStrLen (HostName
) + 1) * sizeof (UINT16
));
382 if (HostNameStr
== NULL
) {
383 Status
= EFI_OUT_OF_RESOURCES
;
387 AsciiStrToUnicodeStr (HostName
, HostNameStr
);
388 Status
= HttpDns4 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteAddr
);
389 FreePool (HostNameStr
);
390 if (EFI_ERROR (Status
)) {
396 // Save the RemotePort and RemoteHost.
398 ASSERT (HttpInstance
->RemoteHost
== NULL
);
399 HttpInstance
->RemotePort
= RemotePort
;
400 HttpInstance
->RemoteHost
= HostName
;
406 // The request URL is different from previous calls to Request(), close existing TCP instance.
408 ASSERT (HttpInstance
->Tcp4
!= NULL
);
409 HttpCloseConnection (HttpInstance
);
410 EfiHttpCancel (This
, NULL
);
414 // Wrap the HTTP token in HTTP_TOKEN_WRAP
416 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
418 Status
= EFI_OUT_OF_RESOURCES
;
422 Wrap
->HttpToken
= Token
;
423 Wrap
->HttpInstance
= HttpInstance
;
424 Wrap
->TcpWrap
.Method
= Request
->Method
;
428 // Configure TCP instance.
430 Status
= HttpConfigureTcp4 (HttpInstance
, Wrap
);
431 if (EFI_ERROR (Status
)) {
437 Status
= HttpConnectTcp4 (HttpInstance
);
438 if (EFI_ERROR (Status
)) {
443 // For the new HTTP token, create TX TCP token events.
445 Status
= HttpCreateTcp4TxEvent (Wrap
);
446 if (EFI_ERROR (Status
)) {
452 // Create request message.
455 if (*FileUrl
!= '/') {
457 // Convert the absolute-URI to the absolute-path
459 while (*FileUrl
!= ':') {
462 if ((*(FileUrl
+1) == '/') && (*(FileUrl
+2) == '/')) {
464 while (*FileUrl
!= '/') {
468 Status
= EFI_INVALID_PARAMETER
;
472 RequestStr
= HttpGenRequestString (HttpInstance
, HttpMsg
, FileUrl
);
473 if (RequestStr
== NULL
) {
474 Status
= EFI_OUT_OF_RESOURCES
;
478 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
479 if (EFI_ERROR (Status
)) {
484 if (HostName
!= NULL
) {
489 // Transmit the request message.
491 Status
= HttpTransmitTcp4 (
495 AsciiStrLen (RequestStr
)
497 if (EFI_ERROR (Status
)) {
504 NetMapRemoveTail (&HttpInstance
->TxTokens
, NULL
);
507 if (RequestStr
!= NULL
) {
508 FreePool (RequestStr
);
512 HttpCloseConnection (HttpInstance
);
516 HttpCloseTcp4ConnCloseEvent (HttpInstance
);
517 if (NULL
!= Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
) {
518 gBS
->CloseEvent (Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
);
525 if (HostName
!= NULL
) {
531 if (UrlParser
!= NULL
) {
532 HttpUrlFreeParser (UrlParser
);
540 Cancel a TxToken or RxToken.
542 @param[in] Map The HTTP instance's token queue.
543 @param[in] Item Object container for one HTTP token and token's wrap.
544 @param[in] Context The user's token to cancel.
546 @retval EFI_SUCCESS Continue to check the next Item.
547 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
554 IN NET_MAP_ITEM
*Item
,
559 EFI_HTTP_TOKEN
*Token
;
560 HTTP_TOKEN_WRAP
*Wrap
;
562 Token
= (EFI_HTTP_TOKEN
*) Context
;
565 // Return EFI_SUCCESS to check the next item in the map if
566 // this one doesn't match.
568 if ((Token
!= NULL
) && (Token
!= Item
->Key
)) {
572 Wrap
= (HTTP_TOKEN_WRAP
*) Item
->Value
;
573 ASSERT (Wrap
!= NULL
);
578 NetMapRemoveItem (Map
, Item
, NULL
);
580 if (Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
!= NULL
) {
581 gBS
->CloseEvent (Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
);
584 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
585 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
588 if (Wrap
->TcpWrap
.RxToken
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
589 FreePool (Wrap
->TcpWrap
.RxToken
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
595 // If only one item is to be cancel, return EFI_ABORTED to stop
596 // iterating the map any more.
606 Cancel the user's receive/transmit request. It is the worker function of
607 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the
610 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
611 @param[in] Token The token to cancel. If NULL, all token will be
614 @retval EFI_SUCCESS The token is cancelled.
615 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.
616 @retval Others Other error as indicated.
621 IN HTTP_PROTOCOL
*HttpInstance
,
622 IN EFI_HTTP_TOKEN
*Token
628 // First check the tokens queued by EfiHttpRequest().
630 Status
= NetMapIterate (&HttpInstance
->TxTokens
, HttpCancelTokens
, Token
);
631 if (EFI_ERROR (Status
)) {
633 if (Status
== EFI_ABORTED
) {
642 // Then check the tokens queued by EfiHttpResponse().
644 Status
= NetMapIterate (&HttpInstance
->RxTokens
, HttpCancelTokens
, Token
);
645 if (EFI_ERROR (Status
)) {
647 if (Status
== EFI_ABORTED
) {
650 return EFI_NOT_FOUND
;
662 Abort an asynchronous HTTP request or response token.
664 The Cancel() function aborts a pending HTTP request or response transaction. If
665 Token is not NULL and the token is in transmit or receive queues when it is being
666 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
667 be signaled. If the token is not in one of the queues, which usually means that the
668 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
669 all asynchronous tokens issued by Request() or Response() will be aborted.
671 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
672 @param[in] Token Point to storage containing HTTP request or response
675 @retval EFI_SUCCESS Request and Response queues are successfully flushed.
676 @retval EFI_INVALID_PARAMETER This is NULL.
677 @retval EFI_NOT_STARTED This instance hasn't been configured.
678 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP,
679 BOOTP, RARP, etc.) hasn't finished yet.
680 @retval EFI_NOT_FOUND The asynchronous request or response token is not
682 @retval EFI_UNSUPPORTED The implementation does not support this function.
688 IN EFI_HTTP_PROTOCOL
*This
,
689 IN EFI_HTTP_TOKEN
*Token
692 HTTP_PROTOCOL
*HttpInstance
;
695 return EFI_INVALID_PARAMETER
;
698 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
699 ASSERT (HttpInstance
!= NULL
);
701 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
702 return EFI_NOT_STARTED
;
705 return HttpCancel (HttpInstance
, Token
);
710 A callback function to intercept events during message parser.
712 This function will be invoked during HttpParseMessageBody() with various events type. An error
713 return status of the callback function will cause the HttpParseMessageBody() aborted.
715 @param[in] EventType Event type of this callback call.
716 @param[in] Data A pointer to data buffer.
717 @param[in] Length Length in bytes of the Data.
718 @param[in] Context Callback context set by HttpInitMsgParser().
720 @retval EFI_SUCCESS Continue to parser the message body.
725 HttpBodyParserCallback (
726 IN HTTP_BODY_PARSE_EVENT EventType
,
732 HTTP_TOKEN_WRAP
*Wrap
;
734 if (EventType
!= BodyParseEventOnComplete
) {
738 if (Data
== NULL
|| Length
!= 0 || Context
== NULL
) {
742 Wrap
= (HTTP_TOKEN_WRAP
*) Context
;
743 Wrap
->HttpInstance
->NextMsg
= Data
;
746 // Free TxToken since already received corrsponding HTTP response.
754 The work function of EfiHttpResponse().
756 @param[in] Wrap Pointer to HTTP token's wrap data.
758 @retval EFI_SUCCESS Allocation succeeded.
759 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
760 @retval EFI_NOT_READY Can't find a corresponding TxToken.
765 IN HTTP_TOKEN_WRAP
*Wrap
769 EFI_HTTP_MESSAGE
*HttpMsg
;
770 EFI_TCP4_IO_TOKEN
*RxToken
;
771 EFI_TCP4_PROTOCOL
*Tcp4
;
780 CHAR8
*StatusCodeStr
;
782 HTTP_PROTOCOL
*HttpInstance
;
783 EFI_HTTP_TOKEN
*Token
;
785 HTTP_TOKEN_WRAP
*ValueInItem
;
788 if (Wrap
== NULL
|| Wrap
->HttpInstance
== NULL
) {
789 return EFI_INVALID_PARAMETER
;
792 HttpInstance
= Wrap
->HttpInstance
;
793 Token
= Wrap
->HttpToken
;
795 HttpMsg
= Token
->Message
;
797 Tcp4
= HttpInstance
->Tcp4
;
798 ASSERT (Tcp4
!= NULL
);
799 HttpMsg
->Headers
= NULL
;
806 if (HttpMsg
->Data
.Response
!= NULL
) {
808 // Need receive the HTTP headers, prepare buffer.
810 Status
= HttpCreateTcp4RxEventForHeader (HttpInstance
);
811 if (EFI_ERROR (Status
)) {
816 // Check whether we have cached header from previous call.
818 if ((HttpInstance
->CacheBody
!= NULL
) && (HttpInstance
->NextMsg
!= NULL
)) {
820 // The data is stored at [NextMsg, CacheBody + CacheLen].
822 HdrLen
= HttpInstance
->CacheBody
+ HttpInstance
->CacheLen
- HttpInstance
->NextMsg
;
823 HttpHeaders
= AllocateZeroPool (HdrLen
);
824 if (HttpHeaders
== NULL
) {
825 Status
= EFI_OUT_OF_RESOURCES
;
829 CopyMem (HttpHeaders
, HttpInstance
->NextMsg
, HdrLen
);
830 FreePool (HttpInstance
->CacheBody
);
831 HttpInstance
->CacheBody
= NULL
;
832 HttpInstance
->NextMsg
= NULL
;
833 HttpInstance
->CacheOffset
= 0;
834 SizeofHeaders
= HdrLen
;
835 BufferSize
= HttpInstance
->CacheLen
;
838 // Check whether we cached the whole HTTP headers.
840 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
843 RxToken
= &HttpInstance
->RxToken
;
844 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= AllocateZeroPool (DEF_BUF_LEN
);
845 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
== NULL
) {
846 Status
= EFI_OUT_OF_RESOURCES
;
851 // Receive the HTTP headers only when EFI_HTTP_RESPONSE_DATA is not NULL.
853 while (EndofHeader
== NULL
) {
854 HttpInstance
->IsRxDone
= FALSE
;
855 RxToken
->Packet
.RxData
->DataLength
= DEF_BUF_LEN
;
856 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
= DEF_BUF_LEN
;
857 Status
= Tcp4
->Receive (Tcp4
, RxToken
);
858 if (EFI_ERROR (Status
)) {
859 DEBUG ((EFI_D_ERROR
, "Tcp4 receive failed: %r\n", Status
));
863 while (!HttpInstance
->IsRxDone
) {
867 Status
= RxToken
->CompletionToken
.Status
;
868 if (EFI_ERROR (Status
)) {
873 // Append the response string.
875 BufferSize
= SizeofHeaders
+ RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
;
876 Buffer
= AllocateZeroPool (BufferSize
);
877 if (Buffer
== NULL
) {
878 Status
= EFI_OUT_OF_RESOURCES
;
882 if (HttpHeaders
!= NULL
) {
883 CopyMem (Buffer
, HttpHeaders
, SizeofHeaders
);
884 FreePool (HttpHeaders
);
888 Buffer
+ SizeofHeaders
,
889 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
,
890 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
892 HttpHeaders
= Buffer
;
893 SizeofHeaders
= BufferSize
;
896 // Check whether we received end of HTTP headers.
898 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
902 // Skip the CRLF after the HTTP headers.
904 EndofHeader
= EndofHeader
+ AsciiStrLen (HTTP_END_OF_HDR_STR
);
907 // Cache the part of body.
909 BodyLen
= BufferSize
- (EndofHeader
- HttpHeaders
);
911 if (HttpInstance
->CacheBody
!= NULL
) {
912 FreePool (HttpInstance
->CacheBody
);
915 HttpInstance
->CacheBody
= AllocateZeroPool (BodyLen
);
916 if (HttpInstance
->CacheBody
== NULL
) {
917 Status
= EFI_OUT_OF_RESOURCES
;
921 CopyMem (HttpInstance
->CacheBody
, EndofHeader
, BodyLen
);
922 HttpInstance
->CacheLen
= BodyLen
;
925 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
926 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
929 // Search for Status Code.
931 StatusCodeStr
= HttpHeaders
+ AsciiStrLen (HTTP_VERSION_STR
) + 1;
932 if (StatusCodeStr
== NULL
) {
936 StatusCode
= AsciiStrDecimalToUintn (StatusCodeStr
);
939 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
941 Tmp
= AsciiStrStr (HttpHeaders
, HTTP_CRLF_STR
);
946 Tmp
= Tmp
+ AsciiStrLen (HTTP_CRLF_STR
);
947 SizeofHeaders
= SizeofHeaders
- (Tmp
- HttpHeaders
);
948 HeaderTmp
= AllocateZeroPool (SizeofHeaders
);
949 if (HeaderTmp
== NULL
) {
953 CopyMem (HeaderTmp
, Tmp
, SizeofHeaders
);
954 FreePool (HttpHeaders
);
955 HttpHeaders
= HeaderTmp
;
957 // Parse the HTTP header into array of key/value pairs.
959 Status
= HttpUtilitiesParse (HttpHeaders
, SizeofHeaders
, &HttpMsg
->Headers
, &HttpMsg
->HeaderCount
);
960 if (EFI_ERROR (Status
)) {
964 FreePool (HttpHeaders
);
967 HttpMsg
->Data
.Response
->StatusCode
= HttpMappingToStatusCode (StatusCode
);
970 // Init message-body parser by header information.
972 Status
= EFI_NOT_READY
;
974 NetMapRemoveHead (&HttpInstance
->TxTokens
, (VOID
**) &ValueInItem
);
975 if (ValueInItem
== NULL
) {
980 // The first TxToken not transmitted yet, insert back and return error.
982 if (!ValueInItem
->TcpWrap
.IsTxDone
) {
986 Status
= HttpInitMsgParser (
987 ValueInItem
->TcpWrap
.Method
,
988 HttpMsg
->Data
.Response
->StatusCode
,
989 HttpMsg
->HeaderCount
,
991 HttpBodyParserCallback
,
992 (VOID
*) ValueInItem
,
993 &HttpInstance
->MsgParser
995 if (EFI_ERROR (Status
)) {
1000 // Check whether we received a complete HTTP message.
1002 if (HttpInstance
->CacheBody
!= NULL
) {
1003 Status
= HttpParseMessageBody (HttpInstance
->MsgParser
, HttpInstance
->CacheLen
, HttpInstance
->CacheBody
);
1004 if (EFI_ERROR (Status
)) {
1008 if (HttpIsMessageComplete (HttpInstance
->MsgParser
)) {
1010 // Free the MsgParse since we already have a full HTTP message.
1012 HttpFreeMsgParser (HttpInstance
->MsgParser
);
1013 HttpInstance
->MsgParser
= NULL
;
1017 if ((HttpMsg
->Body
== NULL
) || (HttpMsg
->BodyLength
== 0)) {
1018 Status
= EFI_SUCCESS
;
1024 // Receive the response body.
1029 // First check whether we cached some data.
1031 if (HttpInstance
->CacheBody
!= NULL
) {
1033 // Calculate the length of the cached data.
1035 if (HttpInstance
->NextMsg
!= NULL
) {
1037 // We have a cached HTTP message which includes a part of HTTP header of next message.
1039 BodyLen
= HttpInstance
->NextMsg
- (HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
);
1041 BodyLen
= HttpInstance
->CacheLen
- HttpInstance
->CacheOffset
;
1046 // We have some cached data. Just copy the data and return.
1048 if (HttpMsg
->BodyLength
< BodyLen
) {
1049 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, HttpMsg
->BodyLength
);
1050 HttpInstance
->CacheOffset
= HttpInstance
->CacheOffset
+ HttpMsg
->BodyLength
;
1053 // Copy all cached data out.
1055 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, BodyLen
);
1056 HttpInstance
->CacheOffset
= BodyLen
+ HttpInstance
->CacheOffset
;
1057 HttpMsg
->BodyLength
= BodyLen
;
1059 if (HttpInstance
->NextMsg
== NULL
) {
1061 // There is no HTTP header of next message. Just free the cache buffer.
1063 FreePool (HttpInstance
->CacheBody
);
1064 HttpInstance
->CacheBody
= NULL
;
1065 HttpInstance
->NextMsg
= NULL
;
1066 HttpInstance
->CacheOffset
= 0;
1070 // Return since we aready received required data.
1072 Status
= EFI_SUCCESS
;
1076 if (BodyLen
== 0 && HttpInstance
->MsgParser
== NULL
) {
1078 // We received a complete HTTP message, and we don't have more data to return to caller.
1080 HttpMsg
->BodyLength
= 0;
1081 Status
= EFI_SUCCESS
;
1086 ASSERT (HttpInstance
->MsgParser
!= NULL
);
1089 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1091 RxToken
= &Wrap
->TcpWrap
.RxToken
;
1093 RxToken
->Packet
.RxData
->DataLength
= (UINT32
) HttpMsg
->BodyLength
;
1094 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
= (UINT32
) HttpMsg
->BodyLength
;
1095 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= (VOID
*) HttpMsg
->Body
;
1097 RxToken
->CompletionToken
.Status
= EFI_NOT_READY
;
1098 Status
= Tcp4
->Receive (Tcp4
, RxToken
);
1099 if (EFI_ERROR (Status
)) {
1100 DEBUG ((EFI_D_ERROR
, "Tcp4 receive failed: %r\n", Status
));
1107 Item
= NetMapFindKey (&Wrap
->HttpInstance
->RxTokens
, Wrap
->HttpToken
);
1109 NetMapRemoveItem (&Wrap
->HttpInstance
->RxTokens
, Item
, NULL
);
1111 Token
->Status
= Status
;
1112 gBS
->SignalEvent (Token
->Event
);
1117 NetMapInsertHead (&HttpInstance
->TxTokens
, ValueInItem
->HttpToken
, ValueInItem
);
1121 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
1122 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
1124 RxToken
= &Wrap
->TcpWrap
.RxToken
;
1125 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
1126 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
1127 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
1132 if (HttpInstance
->RxToken
.CompletionToken
.Event
!= NULL
) {
1133 gBS
->CloseEvent (HttpInstance
->RxToken
.CompletionToken
.Event
);
1134 HttpInstance
->RxToken
.CompletionToken
.Event
= NULL
;
1137 RxToken
= &HttpInstance
->RxToken
;
1138 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
1139 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
1140 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
1143 if (HttpHeaders
!= NULL
) {
1144 FreePool (HttpHeaders
);
1147 if (HttpMsg
->Headers
!= NULL
) {
1148 FreePool (HttpMsg
->Headers
);
1151 if (HttpInstance
->CacheBody
!= NULL
) {
1152 FreePool (HttpInstance
->CacheBody
);
1153 HttpInstance
->CacheBody
= NULL
;
1156 Token
->Status
= Status
;
1157 gBS
->SignalEvent (Token
->Event
);
1165 The Response() function queues an HTTP response to this HTTP instance, similar to
1166 Receive() function in the EFI TCP driver. When the HTTP request is sent successfully,
1167 or if there is an error, Status in token will be updated and Event will be signaled.
1169 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1170 is received in the underlying TCP instance, the data will be parsed and Token will
1171 be populated with the response data. If the data received from the remote host
1172 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1173 (asynchronously) for more data to be sent from the remote host before signaling
1176 It is the responsibility of the caller to allocate a buffer for Body and specify the
1177 size in BodyLength. If the remote host provides a response that contains a content
1178 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1179 BodyLength will be updated with the amount of bytes received and copied to Body. This
1180 allows the client to download a large file in chunks instead of into one contiguous
1181 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1182 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1183 token to underlying TCP instance. If data arrives in the receive buffer, up to
1184 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1185 BodyLength with the amount of bytes received and copied to Body.
1187 If the HTTP driver does not have an open underlying TCP connection with the host
1188 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1189 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1190 an open TCP connection between client and host.
1192 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1193 @param[in] Token Pointer to storage containing HTTP response token.
1195 @retval EFI_SUCCESS Allocation succeeded.
1196 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1198 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1201 Token->Message->Headers is NULL.
1202 Token->Message is NULL.
1203 Token->Message->Body is not NULL,
1204 Token->Message->BodyLength is non-zero, and
1205 Token->Message->Data is NULL, but a previous call to
1206 Response() has not been completed successfully.
1207 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1208 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1209 specified by response URL.
1214 IN EFI_HTTP_PROTOCOL
*This
,
1215 IN EFI_HTTP_TOKEN
*Token
1219 EFI_HTTP_MESSAGE
*HttpMsg
;
1220 HTTP_PROTOCOL
*HttpInstance
;
1221 HTTP_TOKEN_WRAP
*Wrap
;
1223 if ((This
== NULL
) || (Token
== NULL
)) {
1224 return EFI_INVALID_PARAMETER
;
1227 HttpMsg
= Token
->Message
;
1228 if (HttpMsg
== NULL
) {
1229 return EFI_INVALID_PARAMETER
;
1232 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1233 ASSERT (HttpInstance
!= NULL
);
1235 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1236 return EFI_NOT_STARTED
;
1239 if (HttpInstance
->LocalAddressIsIPv6
) {
1240 return EFI_UNSUPPORTED
;
1244 // Check whether the token already existed.
1246 if (EFI_ERROR (NetMapIterate (&HttpInstance
->RxTokens
, HttpTokenExist
, Token
))) {
1247 return EFI_ACCESS_DENIED
;
1250 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
1252 return EFI_OUT_OF_RESOURCES
;
1255 Wrap
->HttpInstance
= HttpInstance
;
1256 Wrap
->HttpToken
= Token
;
1258 Status
= HttpCreateTcp4RxEvent (Wrap
);
1259 if (EFI_ERROR (Status
)) {
1263 Status
= NetMapInsertTail (&HttpInstance
->RxTokens
, Token
, Wrap
);
1264 if (EFI_ERROR (Status
)) {
1269 // If already have pending RxTokens, return directly.
1271 if (NetMapGetCount (&HttpInstance
->RxTokens
) > 1) {
1275 return HttpResponseWorker (Wrap
);
1279 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
1280 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
1289 The Poll() function can be used by network drivers and applications to increase the
1290 rate that data packets are moved between the communication devices and the transmit
1293 In some systems, the periodic timer event in the managed network driver may not poll
1294 the underlying communications device fast enough to transmit and/or receive all data
1295 packets without missing incoming packets or dropping outgoing packets. Drivers and
1296 applications that are experiencing packet loss should try calling the Poll() function
1299 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1301 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1302 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1303 @retval EFI_INVALID_PARAMETER This is NULL.
1304 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1305 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1311 IN EFI_HTTP_PROTOCOL
*This
1314 HTTP_PROTOCOL
*HttpInstance
;
1317 return EFI_INVALID_PARAMETER
;
1320 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1321 ASSERT (HttpInstance
!= NULL
);
1323 if (HttpInstance
->LocalAddressIsIPv6
) {
1324 return EFI_UNSUPPORTED
;
1327 if (HttpInstance
->Tcp4
== NULL
|| HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1328 return EFI_NOT_STARTED
;
1331 return HttpInstance
->Tcp4
->Poll (HttpInstance
->Tcp4
);