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
;
231 if ((This
== NULL
) || (Token
== NULL
)) {
232 return EFI_INVALID_PARAMETER
;
235 HttpMsg
= Token
->Message
;
236 if ((HttpMsg
== NULL
) || (HttpMsg
->Headers
== NULL
)) {
237 return EFI_INVALID_PARAMETER
;
241 // Current implementation does not support POST/PUT method.
242 // If future version supports these two methods, Request could be NULL for a special case that to send large amounts
243 // of data. For this case, the implementation need check whether previous call to Request() has been completed or not.
246 Request
= HttpMsg
->Data
.Request
;
247 if ((Request
== NULL
) || (Request
->Url
== NULL
)) {
248 return EFI_INVALID_PARAMETER
;
252 // Only support GET and HEAD method in current implementation.
254 if ((Request
->Method
!= HttpMethodGet
) && (Request
->Method
!= HttpMethodHead
)) {
255 return EFI_UNSUPPORTED
;
258 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
259 ASSERT (HttpInstance
!= NULL
);
261 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
262 return EFI_NOT_STARTED
;
265 if (HttpInstance
->LocalAddressIsIPv6
) {
266 return EFI_UNSUPPORTED
;
270 // Check whether the token already existed.
272 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTokenExist
, Token
))) {
273 return EFI_ACCESS_DENIED
;
283 // Parse the URI of the remote host.
285 Url
= AllocatePool (StrLen (Request
->Url
) + 1);
287 return EFI_OUT_OF_RESOURCES
;
290 UnicodeStrToAsciiStr (Request
->Url
, Url
);
292 Status
= HttpParseUrl (Url
, (UINT32
) AsciiStrLen (Url
), FALSE
, &UrlParser
);
293 if (EFI_ERROR (Status
)) {
299 Status
= HttpUrlGetHostName (Url
, UrlParser
, &HostName
);
300 if (EFI_ERROR (Status
)) {
304 Status
= HttpUrlGetPort (Url
, UrlParser
, &RemotePort
);
305 if (EFI_ERROR (Status
)) {
306 RemotePort
= HTTP_DEFAULT_PORT
;
312 if (HttpInstance
->RemoteHost
== NULL
&& HttpInstance
->RemotePort
== 0) {
314 // Request() is called the first time.
318 if ((HttpInstance
->RemotePort
== RemotePort
) &&
319 (AsciiStrCmp (HttpInstance
->RemoteHost
, HostName
) == 0)) {
321 // Host Name and port number of the request URL are the same with previous call to Request().
322 // Check whether previous TCP packet sent out.
324 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTcpNotReady
, NULL
))) {
326 // Wrap the HTTP token in HTTP_TOKEN_WRAP
328 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
330 Status
= EFI_OUT_OF_RESOURCES
;
334 Wrap
->HttpToken
= Token
;
335 Wrap
->HttpInstance
= HttpInstance
;
337 Status
= HttpCreateTcp4TxEvent (Wrap
);
338 if (EFI_ERROR (Status
)) {
342 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
343 if (EFI_ERROR (Status
)) {
347 Wrap
->TcpWrap
.Method
= Request
->Method
;
353 // Queue the HTTP token and return.
358 // Use existing TCP instance to transmit the packet.
365 // Need close existing TCP instance and create a new TCP instance for data transmit.
367 if (HttpInstance
->RemoteHost
!= NULL
) {
368 FreePool (HttpInstance
->RemoteHost
);
369 HttpInstance
->RemoteHost
= NULL
;
376 // Parse Url for IPv4 address, if failed, perform DNS resolution.
378 Status
= NetLibAsciiStrToIp4 (HostName
, &HttpInstance
->RemoteAddr
);
379 if (EFI_ERROR (Status
)) {
380 HostNameStr
= AllocateZeroPool ((AsciiStrLen (HostName
) + 1) * sizeof (UINT16
));
381 if (HostNameStr
== NULL
) {
382 Status
= EFI_OUT_OF_RESOURCES
;
386 AsciiStrToUnicodeStr (HostName
, HostNameStr
);
387 Status
= HttpDns4 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteAddr
);
388 FreePool (HostNameStr
);
389 if (EFI_ERROR (Status
)) {
395 // Save the RemotePort and RemoteHost.
397 ASSERT (HttpInstance
->RemoteHost
== NULL
);
398 HttpInstance
->RemotePort
= RemotePort
;
399 HttpInstance
->RemoteHost
= HostName
;
405 // The request URL is different from previous calls to Request(), close existing TCP instance.
407 ASSERT (HttpInstance
->Tcp4
!= NULL
);
408 HttpCloseConnection (HttpInstance
);
409 EfiHttpCancel (This
, NULL
);
413 // Wrap the HTTP token in HTTP_TOKEN_WRAP
415 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
417 Status
= EFI_OUT_OF_RESOURCES
;
421 Wrap
->HttpToken
= Token
;
422 Wrap
->HttpInstance
= HttpInstance
;
423 Wrap
->TcpWrap
.Method
= Request
->Method
;
427 // Configure TCP instance.
429 Status
= HttpConfigureTcp4 (HttpInstance
, Wrap
);
430 if (EFI_ERROR (Status
)) {
436 Status
= HttpConnectTcp4 (HttpInstance
);
437 if (EFI_ERROR (Status
)) {
442 // For the new HTTP token, create TX TCP token events.
444 Status
= HttpCreateTcp4TxEvent (Wrap
);
445 if (EFI_ERROR (Status
)) {
451 // Create request message.
453 RequestStr
= HttpGenRequestString (HttpInstance
, HttpMsg
, Url
);
454 if (RequestStr
== NULL
) {
455 Status
= EFI_OUT_OF_RESOURCES
;
459 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
460 if (EFI_ERROR (Status
)) {
465 if (HostName
!= NULL
) {
470 // Transmit the request message.
472 Status
= HttpTransmitTcp4 (
476 AsciiStrLen (RequestStr
)
478 if (EFI_ERROR (Status
)) {
485 NetMapRemoveTail (&HttpInstance
->TxTokens
, NULL
);
488 if (RequestStr
!= NULL
) {
489 FreePool (RequestStr
);
493 HttpCloseConnection (HttpInstance
);
497 HttpCloseTcp4ConnCloseEvent (HttpInstance
);
498 if (NULL
!= Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
) {
499 gBS
->CloseEvent (Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
);
506 if (HostName
!= NULL
) {
512 if (UrlParser
!= NULL
) {
513 HttpUrlFreeParser (UrlParser
);
521 Cancel a TxToken or RxToken.
523 @param[in] Map The HTTP instance's token queue.
524 @param[in] Item Object container for one HTTP token and token's wrap.
525 @param[in] Context The user's token to cancel.
527 @retval EFI_SUCCESS Continue to check the next Item.
528 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
535 IN NET_MAP_ITEM
*Item
,
540 EFI_HTTP_TOKEN
*Token
;
541 HTTP_TOKEN_WRAP
*Wrap
;
543 Token
= (EFI_HTTP_TOKEN
*) Context
;
546 // Return EFI_SUCCESS to check the next item in the map if
547 // this one doesn't match.
549 if ((Token
!= NULL
) && (Token
!= Item
->Key
)) {
553 Wrap
= (HTTP_TOKEN_WRAP
*) Item
->Value
;
554 ASSERT (Wrap
!= NULL
);
559 NetMapRemoveItem (Map
, Item
, NULL
);
561 if (Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
!= NULL
) {
562 gBS
->CloseEvent (Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
);
565 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
566 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
569 if (Wrap
->TcpWrap
.RxToken
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
570 FreePool (Wrap
->TcpWrap
.RxToken
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
576 // If only one item is to be cancel, return EFI_ABORTED to stop
577 // iterating the map any more.
587 Cancel the user's receive/transmit request. It is the worker function of
588 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the
591 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
592 @param[in] Token The token to cancel. If NULL, all token will be
595 @retval EFI_SUCCESS The token is cancelled.
596 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.
597 @retval Others Other error as indicated.
602 IN HTTP_PROTOCOL
*HttpInstance
,
603 IN EFI_HTTP_TOKEN
*Token
609 // First check the tokens queued by EfiHttpRequest().
611 Status
= NetMapIterate (&HttpInstance
->TxTokens
, HttpCancelTokens
, Token
);
612 if (EFI_ERROR (Status
)) {
614 if (Status
== EFI_ABORTED
) {
623 // Then check the tokens queued by EfiHttpResponse().
625 Status
= NetMapIterate (&HttpInstance
->RxTokens
, HttpCancelTokens
, Token
);
626 if (EFI_ERROR (Status
)) {
628 if (Status
== EFI_ABORTED
) {
631 return EFI_NOT_FOUND
;
643 Abort an asynchronous HTTP request or response token.
645 The Cancel() function aborts a pending HTTP request or response transaction. If
646 Token is not NULL and the token is in transmit or receive queues when it is being
647 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
648 be signaled. If the token is not in one of the queues, which usually means that the
649 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
650 all asynchronous tokens issued by Request() or Response() will be aborted.
652 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
653 @param[in] Token Point to storage containing HTTP request or response
656 @retval EFI_SUCCESS Request and Response queues are successfully flushed.
657 @retval EFI_INVALID_PARAMETER This is NULL.
658 @retval EFI_NOT_STARTED This instance hasn't been configured.
659 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP,
660 BOOTP, RARP, etc.) hasn't finished yet.
661 @retval EFI_NOT_FOUND The asynchronous request or response token is not
663 @retval EFI_UNSUPPORTED The implementation does not support this function.
669 IN EFI_HTTP_PROTOCOL
*This
,
670 IN EFI_HTTP_TOKEN
*Token
673 HTTP_PROTOCOL
*HttpInstance
;
676 return EFI_INVALID_PARAMETER
;
679 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
680 ASSERT (HttpInstance
!= NULL
);
682 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
683 return EFI_NOT_STARTED
;
686 return HttpCancel (HttpInstance
, Token
);
691 A callback function to intercept events during message parser.
693 This function will be invoked during HttpParseMessageBody() with various events type. An error
694 return status of the callback function will cause the HttpParseMessageBody() aborted.
696 @param[in] EventType Event type of this callback call.
697 @param[in] Data A pointer to data buffer.
698 @param[in] Length Length in bytes of the Data.
699 @param[in] Context Callback context set by HttpInitMsgParser().
701 @retval EFI_SUCCESS Continue to parser the message body.
706 HttpBodyParserCallback (
707 IN HTTP_BODY_PARSE_EVENT EventType
,
713 HTTP_TOKEN_WRAP
*Wrap
;
715 if (EventType
!= BodyParseEventOnComplete
) {
719 if (Data
== NULL
|| Length
!= 0 || Context
== NULL
) {
723 Wrap
= (HTTP_TOKEN_WRAP
*) Context
;
724 Wrap
->HttpInstance
->NextMsg
= Data
;
727 // Free TxToken since already received corrsponding HTTP response.
735 The work function of EfiHttpResponse().
737 @param[in] Wrap Pointer to HTTP token's wrap data.
739 @retval EFI_SUCCESS Allocation succeeded.
740 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
741 @retval EFI_NOT_READY Can't find a corresponding TxToken.
746 IN HTTP_TOKEN_WRAP
*Wrap
750 EFI_HTTP_MESSAGE
*HttpMsg
;
751 EFI_TCP4_IO_TOKEN
*RxToken
;
752 EFI_TCP4_PROTOCOL
*Tcp4
;
761 CHAR8
*StatusCodeStr
;
763 HTTP_PROTOCOL
*HttpInstance
;
764 EFI_HTTP_TOKEN
*Token
;
766 HTTP_TOKEN_WRAP
*ValueInItem
;
769 HttpInstance
= Wrap
->HttpInstance
;
770 Token
= Wrap
->HttpToken
;
772 HttpMsg
= Token
->Message
;
774 Tcp4
= HttpInstance
->Tcp4
;
775 ASSERT (Tcp4
!= NULL
);
776 HttpMsg
->Headers
= NULL
;
783 if (HttpMsg
->Data
.Response
!= NULL
) {
785 // Need receive the HTTP headers, prepare buffer.
787 Status
= HttpCreateTcp4RxEventForHeader (HttpInstance
);
788 if (EFI_ERROR (Status
)) {
793 // Check whether we have cached header from previous call.
795 if ((HttpInstance
->CacheBody
!= NULL
) && (HttpInstance
->NextMsg
!= NULL
)) {
797 // The data is stored at [NextMsg, CacheBody + CacheLen].
799 HdrLen
= HttpInstance
->CacheBody
+ HttpInstance
->CacheLen
- HttpInstance
->NextMsg
;
800 HttpHeaders
= AllocateZeroPool (HdrLen
);
801 if (HttpHeaders
== NULL
) {
802 Status
= EFI_OUT_OF_RESOURCES
;
806 CopyMem (HttpHeaders
, HttpInstance
->NextMsg
, HdrLen
);
807 FreePool (HttpInstance
->CacheBody
);
808 HttpInstance
->CacheBody
= NULL
;
809 HttpInstance
->NextMsg
= NULL
;
810 HttpInstance
->CacheOffset
= 0;
811 SizeofHeaders
= HdrLen
;
812 BufferSize
= HttpInstance
->CacheLen
;
815 // Check whether we cached the whole HTTP headers.
817 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
820 RxToken
= &HttpInstance
->RxToken
;
821 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= AllocateZeroPool (DEF_BUF_LEN
);
822 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
== NULL
) {
823 Status
= EFI_OUT_OF_RESOURCES
;
828 // Receive the HTTP headers only when EFI_HTTP_RESPONSE_DATA is not NULL.
830 while (EndofHeader
== NULL
) {
831 HttpInstance
->IsRxDone
= FALSE
;
832 RxToken
->Packet
.RxData
->DataLength
= DEF_BUF_LEN
;
833 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
= DEF_BUF_LEN
;
834 Status
= Tcp4
->Receive (Tcp4
, RxToken
);
835 if (EFI_ERROR (Status
)) {
836 DEBUG ((EFI_D_ERROR
, "Tcp4 receive failed: %r\n", Status
));
840 while (!HttpInstance
->IsRxDone
) {
844 Status
= RxToken
->CompletionToken
.Status
;
845 if (EFI_ERROR (Status
)) {
850 // Append the response string.
852 BufferSize
= SizeofHeaders
+ RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
;
853 Buffer
= AllocateZeroPool (BufferSize
);
854 if (Buffer
== NULL
) {
855 Status
= EFI_OUT_OF_RESOURCES
;
859 if (HttpHeaders
!= NULL
) {
860 CopyMem (Buffer
, HttpHeaders
, SizeofHeaders
);
861 FreePool (HttpHeaders
);
865 Buffer
+ SizeofHeaders
,
866 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
,
867 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
869 HttpHeaders
= Buffer
;
870 SizeofHeaders
= BufferSize
;
873 // Check whether we received end of HTTP headers.
875 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
879 // Skip the CRLF after the HTTP headers.
881 EndofHeader
= EndofHeader
+ AsciiStrLen (HTTP_END_OF_HDR_STR
);
884 // Cache the part of body.
886 BodyLen
= BufferSize
- (EndofHeader
- HttpHeaders
);
888 if (HttpInstance
->CacheBody
!= NULL
) {
889 FreePool (HttpInstance
->CacheBody
);
892 HttpInstance
->CacheBody
= AllocateZeroPool (BodyLen
);
893 if (HttpInstance
->CacheBody
== NULL
) {
894 Status
= EFI_OUT_OF_RESOURCES
;
898 CopyMem (HttpInstance
->CacheBody
, EndofHeader
, BodyLen
);
899 HttpInstance
->CacheLen
= BodyLen
;
902 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
903 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
906 // Search for Status Code.
908 StatusCodeStr
= HttpHeaders
+ AsciiStrLen (HTTP_VERSION_STR
) + 1;
909 if (StatusCodeStr
== NULL
) {
913 StatusCode
= AsciiStrDecimalToUintn (StatusCodeStr
);
916 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
918 Tmp
= AsciiStrStr (HttpHeaders
, HTTP_CRLF_STR
);
923 Tmp
= Tmp
+ AsciiStrLen (HTTP_CRLF_STR
);
924 SizeofHeaders
= SizeofHeaders
- (Tmp
- HttpHeaders
);
925 HeaderTmp
= AllocateZeroPool (SizeofHeaders
);
926 if (HeaderTmp
== NULL
) {
930 CopyMem (HeaderTmp
, Tmp
, SizeofHeaders
);
931 FreePool (HttpHeaders
);
932 HttpHeaders
= HeaderTmp
;
934 // Parse the HTTP header into array of key/value pairs.
936 Status
= HttpUtilitiesParse (HttpHeaders
, SizeofHeaders
, &HttpMsg
->Headers
, &HttpMsg
->HeaderCount
);
937 if (EFI_ERROR (Status
)) {
941 FreePool (HttpHeaders
);
944 HttpMsg
->Data
.Response
->StatusCode
= HttpMappingToStatusCode (StatusCode
);
947 // Init message-body parser by header information.
949 Status
= EFI_NOT_READY
;
951 NetMapRemoveHead (&HttpInstance
->TxTokens
, (VOID
**) &ValueInItem
);
952 if (ValueInItem
== NULL
) {
957 // The first TxToken not transmitted yet, insert back and return error.
959 if (!ValueInItem
->TcpWrap
.IsTxDone
) {
963 Status
= HttpInitMsgParser (
964 ValueInItem
->TcpWrap
.Method
,
965 HttpMsg
->Data
.Response
->StatusCode
,
966 HttpMsg
->HeaderCount
,
968 HttpBodyParserCallback
,
969 (VOID
*) ValueInItem
,
970 &HttpInstance
->MsgParser
972 if (EFI_ERROR (Status
)) {
977 // Check whether we received a complete HTTP message.
979 if (HttpInstance
->CacheBody
!= NULL
) {
980 Status
= HttpParseMessageBody (HttpInstance
->MsgParser
, HttpInstance
->CacheLen
, HttpInstance
->CacheBody
);
981 if (EFI_ERROR (Status
)) {
985 if (HttpIsMessageComplete (HttpInstance
->MsgParser
)) {
987 // Free the MsgParse since we already have a full HTTP message.
989 HttpFreeMsgParser (HttpInstance
->MsgParser
);
990 HttpInstance
->MsgParser
= NULL
;
994 if ((HttpMsg
->Body
== NULL
) || (HttpMsg
->BodyLength
== 0)) {
995 Status
= EFI_SUCCESS
;
1001 // Receive the response body.
1006 // First check whether we cached some data.
1008 if (HttpInstance
->CacheBody
!= NULL
) {
1010 // Calculate the length of the cached data.
1012 if (HttpInstance
->NextMsg
!= NULL
) {
1014 // We have a cached HTTP message which includes a part of HTTP header of next message.
1016 BodyLen
= HttpInstance
->NextMsg
- (HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
);
1018 BodyLen
= HttpInstance
->CacheLen
- HttpInstance
->CacheOffset
;
1023 // We have some cached data. Just copy the data and return.
1025 if (HttpMsg
->BodyLength
< BodyLen
) {
1026 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, HttpMsg
->BodyLength
);
1027 HttpInstance
->CacheOffset
= HttpInstance
->CacheOffset
+ HttpMsg
->BodyLength
;
1030 // Copy all cached data out.
1032 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, BodyLen
);
1033 HttpInstance
->CacheOffset
= BodyLen
+ HttpInstance
->CacheOffset
;
1034 HttpMsg
->BodyLength
= BodyLen
;
1036 if (HttpInstance
->NextMsg
== NULL
) {
1038 // There is no HTTP header of next message. Just free the cache buffer.
1040 FreePool (HttpInstance
->CacheBody
);
1041 HttpInstance
->CacheBody
= NULL
;
1042 HttpInstance
->NextMsg
= NULL
;
1043 HttpInstance
->CacheOffset
= 0;
1047 // Return since we aready received required data.
1049 Status
= EFI_SUCCESS
;
1053 if (BodyLen
== 0 && HttpInstance
->MsgParser
== NULL
) {
1055 // We received a complete HTTP message, and we don't have more data to return to caller.
1057 HttpMsg
->BodyLength
= 0;
1058 Status
= EFI_SUCCESS
;
1063 ASSERT (HttpInstance
->MsgParser
!= NULL
);
1066 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1068 RxToken
= &Wrap
->TcpWrap
.RxToken
;
1070 RxToken
->Packet
.RxData
->DataLength
= (UINT32
) HttpMsg
->BodyLength
;
1071 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
= (UINT32
) HttpMsg
->BodyLength
;
1072 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= (VOID
*) HttpMsg
->Body
;
1074 RxToken
->CompletionToken
.Status
= EFI_NOT_READY
;
1075 Status
= Tcp4
->Receive (Tcp4
, RxToken
);
1076 if (EFI_ERROR (Status
)) {
1077 DEBUG ((EFI_D_ERROR
, "Tcp4 receive failed: %r\n", Status
));
1084 Item
= NetMapFindKey (&Wrap
->HttpInstance
->RxTokens
, Wrap
->HttpToken
);
1086 NetMapRemoveItem (&Wrap
->HttpInstance
->RxTokens
, Item
, NULL
);
1088 Token
->Status
= Status
;
1089 gBS
->SignalEvent (Token
->Event
);
1094 NetMapInsertHead (&HttpInstance
->TxTokens
, ValueInItem
->HttpToken
, ValueInItem
);
1098 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
1099 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
1101 RxToken
= &Wrap
->TcpWrap
.RxToken
;
1102 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
1103 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
1104 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
1109 if (HttpInstance
->RxToken
.CompletionToken
.Event
!= NULL
) {
1110 gBS
->CloseEvent (HttpInstance
->RxToken
.CompletionToken
.Event
);
1111 HttpInstance
->RxToken
.CompletionToken
.Event
= NULL
;
1114 RxToken
= &HttpInstance
->RxToken
;
1115 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
1116 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
1117 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
1120 if (HttpHeaders
!= NULL
) {
1121 FreePool (HttpHeaders
);
1124 if (HttpMsg
->Headers
!= NULL
) {
1125 FreePool (HttpMsg
->Headers
);
1128 if (HttpInstance
->CacheBody
!= NULL
) {
1129 FreePool (HttpInstance
->CacheBody
);
1130 HttpInstance
->CacheBody
= NULL
;
1133 Token
->Status
= Status
;
1134 gBS
->SignalEvent (Token
->Event
);
1142 The Response() function queues an HTTP response to this HTTP instance, similar to
1143 Receive() function in the EFI TCP driver. When the HTTP request is sent successfully,
1144 or if there is an error, Status in token will be updated and Event will be signaled.
1146 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1147 is received in the underlying TCP instance, the data will be parsed and Token will
1148 be populated with the response data. If the data received from the remote host
1149 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1150 (asynchronously) for more data to be sent from the remote host before signaling
1153 It is the responsibility of the caller to allocate a buffer for Body and specify the
1154 size in BodyLength. If the remote host provides a response that contains a content
1155 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1156 BodyLength will be updated with the amount of bytes received and copied to Body. This
1157 allows the client to download a large file in chunks instead of into one contiguous
1158 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1159 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1160 token to underlying TCP instance. If data arrives in the receive buffer, up to
1161 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1162 BodyLength with the amount of bytes received and copied to Body.
1164 If the HTTP driver does not have an open underlying TCP connection with the host
1165 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1166 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1167 an open TCP connection between client and host.
1169 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1170 @param[in] Token Pointer to storage containing HTTP response token.
1172 @retval EFI_SUCCESS Allocation succeeded.
1173 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1175 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1178 Token->Message->Headers is NULL.
1179 Token->Message is NULL.
1180 Token->Message->Body is not NULL,
1181 Token->Message->BodyLength is non-zero, and
1182 Token->Message->Data is NULL, but a previous call to
1183 Response() has not been completed successfully.
1184 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1185 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1186 specified by response URL.
1191 IN EFI_HTTP_PROTOCOL
*This
,
1192 IN EFI_HTTP_TOKEN
*Token
1196 EFI_HTTP_MESSAGE
*HttpMsg
;
1197 HTTP_PROTOCOL
*HttpInstance
;
1198 HTTP_TOKEN_WRAP
*Wrap
;
1200 if ((This
== NULL
) || (Token
== NULL
)) {
1201 return EFI_INVALID_PARAMETER
;
1204 HttpMsg
= Token
->Message
;
1205 if (HttpMsg
== NULL
) {
1206 return EFI_INVALID_PARAMETER
;
1209 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1210 ASSERT (HttpInstance
!= NULL
);
1212 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1213 return EFI_NOT_STARTED
;
1216 if (HttpInstance
->LocalAddressIsIPv6
) {
1217 return EFI_UNSUPPORTED
;
1221 // Check whether the token already existed.
1223 if (EFI_ERROR (NetMapIterate (&HttpInstance
->RxTokens
, HttpTokenExist
, Token
))) {
1224 return EFI_ACCESS_DENIED
;
1227 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
1229 return EFI_OUT_OF_RESOURCES
;
1232 Wrap
->HttpInstance
= HttpInstance
;
1233 Wrap
->HttpToken
= Token
;
1235 Status
= HttpCreateTcp4RxEvent (Wrap
);
1236 if (EFI_ERROR (Status
)) {
1240 Status
= NetMapInsertTail (&HttpInstance
->RxTokens
, Token
, Wrap
);
1241 if (EFI_ERROR (Status
)) {
1246 // If already have pending RxTokens, return directly.
1248 if (NetMapGetCount (&HttpInstance
->RxTokens
) > 1) {
1252 return HttpResponseWorker (Wrap
);
1256 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
1257 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
1266 The Poll() function can be used by network drivers and applications to increase the
1267 rate that data packets are moved between the communication devices and the transmit
1270 In some systems, the periodic timer event in the managed network driver may not poll
1271 the underlying communications device fast enough to transmit and/or receive all data
1272 packets without missing incoming packets or dropping outgoing packets. Drivers and
1273 applications that are experiencing packet loss should try calling the Poll() function
1276 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1278 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1279 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1280 @retval EFI_INVALID_PARAMETER This is NULL.
1281 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1282 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1288 IN EFI_HTTP_PROTOCOL
*This
1291 HTTP_PROTOCOL
*HttpInstance
;
1294 return EFI_INVALID_PARAMETER
;
1297 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1298 ASSERT (HttpInstance
!= NULL
);
1300 if (HttpInstance
->LocalAddressIsIPv6
) {
1301 return EFI_UNSUPPORTED
;
1304 if (HttpInstance
->Tcp4
== NULL
|| HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1305 return EFI_NOT_STARTED
;
1308 return HttpInstance
->Tcp4
->Poll (HttpInstance
->Tcp4
);