2 Implementation of EFI_HTTP_PROTOCOL protocol interfaces.
4 Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "HttpDriver.h"
19 EFI_HTTP_PROTOCOL mEfiHttpTemplate
= {
29 Returns the operational parameters for the current HTTP child instance.
31 The GetModeData() function is used to read the current mode data (operational
32 parameters) for this HTTP protocol instance.
34 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
35 @param[out] HttpConfigData Point to buffer for operational parameters of this
38 @retval EFI_SUCCESS Operation succeeded.
39 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
41 HttpConfigData is NULL.
42 HttpInstance->LocalAddressIsIPv6 is FALSE and
43 HttpConfigData->IPv4Node is NULL.
44 HttpInstance->LocalAddressIsIPv6 is TRUE and
45 HttpConfigData->IPv6Node is NULL.
46 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
52 IN EFI_HTTP_PROTOCOL
*This
,
53 OUT EFI_HTTP_CONFIG_DATA
*HttpConfigData
56 HTTP_PROTOCOL
*HttpInstance
;
59 // Check input parameters.
61 if ((This
== NULL
) || (HttpConfigData
== NULL
)) {
62 return EFI_INVALID_PARAMETER
;
65 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
66 ASSERT (HttpInstance
!= NULL
);
68 if ((HttpInstance
->LocalAddressIsIPv6
&& HttpConfigData
->AccessPoint
.IPv6Node
== NULL
) ||
69 (!HttpInstance
->LocalAddressIsIPv6
&& HttpConfigData
->AccessPoint
.IPv4Node
== NULL
)) {
70 return EFI_INVALID_PARAMETER
;
73 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
74 return EFI_NOT_STARTED
;
77 HttpConfigData
->HttpVersion
= HttpInstance
->HttpVersion
;
78 HttpConfigData
->TimeOutMillisec
= HttpInstance
->TimeOutMillisec
;
79 HttpConfigData
->LocalAddressIsIPv6
= HttpInstance
->LocalAddressIsIPv6
;
81 if (HttpInstance
->LocalAddressIsIPv6
) {
83 HttpConfigData
->AccessPoint
.IPv6Node
,
84 &HttpInstance
->Ipv6Node
,
85 sizeof (HttpInstance
->Ipv6Node
)
89 HttpConfigData
->AccessPoint
.IPv4Node
,
90 &HttpInstance
->IPv4Node
,
91 sizeof (HttpInstance
->IPv4Node
)
99 Initialize or brutally reset the operational parameters for this EFI HTTP instance.
101 The Configure() function does the following:
102 When HttpConfigData is not NULL Initialize this EFI HTTP instance by configuring
103 timeout, local address, port, etc.
104 When HttpConfigData is NULL, reset this EFI HTTP instance by closing all active
105 connections with remote hosts, canceling all asynchronous tokens, and flush request
106 and response buffers without informing the appropriate hosts.
108 No other EFI HTTP function can be executed by this instance until the Configure()
109 function is executed and returns successfully.
111 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
112 @param[in] HttpConfigData Pointer to the configure data to configure the instance.
114 @retval EFI_SUCCESS Operation succeeded.
115 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
117 HttpConfigData->LocalAddressIsIPv6 is FALSE and
118 HttpConfigData->IPv4Node is NULL.
119 HttpConfigData->LocalAddressIsIPv6 is TRUE and
120 HttpConfigData->IPv6Node is NULL.
121 @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling
122 Configure() with NULL to reset it.
123 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
124 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when
125 executing Configure().
126 @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported
127 in the implementation.
132 IN EFI_HTTP_PROTOCOL
*This
,
133 IN EFI_HTTP_CONFIG_DATA
*HttpConfigData
136 HTTP_PROTOCOL
*HttpInstance
;
140 // Check input parameters.
143 (HttpConfigData
!= NULL
&&
144 ((HttpConfigData
->LocalAddressIsIPv6
&& HttpConfigData
->AccessPoint
.IPv6Node
== NULL
) ||
145 (!HttpConfigData
->LocalAddressIsIPv6
&& HttpConfigData
->AccessPoint
.IPv4Node
== NULL
)))) {
146 return EFI_INVALID_PARAMETER
;
149 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
150 ASSERT (HttpInstance
!= NULL
&& HttpInstance
->Service
!= NULL
);
152 if (HttpConfigData
!= NULL
) {
155 // Now configure this HTTP instance.
157 if (HttpInstance
->State
!= HTTP_STATE_UNCONFIGED
) {
158 return EFI_ALREADY_STARTED
;
161 HttpInstance
->HttpVersion
= HttpConfigData
->HttpVersion
;
162 HttpInstance
->TimeOutMillisec
= HttpConfigData
->TimeOutMillisec
;
163 HttpInstance
->LocalAddressIsIPv6
= HttpConfigData
->LocalAddressIsIPv6
;
165 if (HttpConfigData
->LocalAddressIsIPv6
) {
167 &HttpInstance
->Ipv6Node
,
168 HttpConfigData
->AccessPoint
.IPv6Node
,
169 sizeof (HttpInstance
->Ipv6Node
)
173 &HttpInstance
->IPv4Node
,
174 HttpConfigData
->AccessPoint
.IPv4Node
,
175 sizeof (HttpInstance
->IPv4Node
)
182 Status
= HttpInitProtocol (HttpInstance
, HttpInstance
->LocalAddressIsIPv6
);
183 if (EFI_ERROR (Status
)) {
187 HttpInstance
->State
= HTTP_STATE_HTTP_CONFIGED
;
192 // Reset all the resources related to HttpInsance.
194 HttpCleanProtocol (HttpInstance
);
195 HttpInstance
->State
= HTTP_STATE_UNCONFIGED
;
202 The Request() function queues an HTTP request to this HTTP instance.
204 Similar to Transmit() function in the EFI TCP driver. When the HTTP request is sent
205 successfully, or if there is an error, Status in token will be updated and Event will
208 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
209 @param[in] Token Pointer to storage containing HTTP request token.
211 @retval EFI_SUCCESS Outgoing data was processed.
212 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
213 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
214 @retval EFI_TIMEOUT Data was dropped out of the transmit or receive queue.
215 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
216 @retval EFI_UNSUPPORTED The HTTP method is not supported in current
218 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
221 Token->Message is NULL.
222 Token->Message->Body is not NULL,
223 Token->Message->BodyLength is non-zero, and
224 Token->Message->Data is NULL, but a previous call to
225 Request()has not been completed successfully.
230 IN EFI_HTTP_PROTOCOL
*This
,
231 IN EFI_HTTP_TOKEN
*Token
234 EFI_HTTP_MESSAGE
*HttpMsg
;
235 EFI_HTTP_REQUEST_DATA
*Request
;
241 HTTP_PROTOCOL
*HttpInstance
;
248 HTTP_TOKEN_WRAP
*Wrap
;
250 UINTN RequestMsgSize
;
264 if ((This
== NULL
) || (Token
== NULL
)) {
265 return EFI_INVALID_PARAMETER
;
268 HttpMsg
= Token
->Message
;
269 if (HttpMsg
== NULL
) {
270 return EFI_INVALID_PARAMETER
;
273 Request
= HttpMsg
->Data
.Request
;
276 // Only support GET, HEAD, PUT and POST method in current implementation.
278 if ((Request
!= NULL
) && (Request
->Method
!= HttpMethodGet
) &&
279 (Request
->Method
!= HttpMethodHead
) && (Request
->Method
!= HttpMethodPut
) && (Request
->Method
!= HttpMethodPost
)) {
280 return EFI_UNSUPPORTED
;
283 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
284 ASSERT (HttpInstance
!= NULL
);
287 // Capture the method into HttpInstance.
289 if (Request
!= NULL
) {
290 HttpInstance
->Method
= Request
->Method
;
293 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
294 return EFI_NOT_STARTED
;
297 if (Request
== NULL
) {
299 // Request would be NULL only for PUT/POST operation (in the current implementation)
301 if ((HttpInstance
->Method
!= HttpMethodPut
) && (HttpInstance
->Method
!= HttpMethodPost
)) {
302 return EFI_INVALID_PARAMETER
;
306 // For PUT/POST, we need to have the TCP already configured. Bail out if it is not!
308 if (HttpInstance
->State
< HTTP_STATE_TCP_CONFIGED
) {
309 return EFI_INVALID_PARAMETER
;
313 // We need to have the Message Body for sending the HTTP message across in these cases.
315 if (HttpMsg
->Body
== NULL
|| HttpMsg
->BodyLength
== 0) {
316 return EFI_INVALID_PARAMETER
;
320 // Use existing TCP instance to transmit the packet.
326 // Check whether the token already existed.
328 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTokenExist
, Token
))) {
329 return EFI_ACCESS_DENIED
;
333 // Parse the URI of the remote host.
335 Url
= HttpInstance
->Url
;
336 UrlLen
= StrLen (Request
->Url
) + 1;
337 if (UrlLen
> HTTP_URL_BUFFER_LEN
) {
338 Url
= AllocateZeroPool (UrlLen
);
340 return EFI_OUT_OF_RESOURCES
;
342 FreePool (HttpInstance
->Url
);
343 HttpInstance
->Url
= Url
;
347 UnicodeStrToAsciiStrS (Request
->Url
, Url
, UrlLen
);
349 Status
= HttpParseUrl (Url
, (UINT32
) AsciiStrLen (Url
), FALSE
, &UrlParser
);
350 if (EFI_ERROR (Status
)) {
355 Status
= HttpUrlGetHostName (Url
, UrlParser
, &HostName
);
356 if (EFI_ERROR (Status
)) {
360 Status
= HttpUrlGetPort (Url
, UrlParser
, &RemotePort
);
361 if (EFI_ERROR (Status
)) {
362 RemotePort
= HTTP_DEFAULT_PORT
;
365 // If Configure is TRUE, it indicates the first time to call Request();
366 // If ReConfigure is TRUE, it indicates the request URL is not same
367 // with the previous call to Request();
372 if (HttpInstance
->RemoteHost
== NULL
) {
374 // Request() is called the first time.
378 if ((HttpInstance
->RemotePort
== RemotePort
) &&
379 (AsciiStrCmp (HttpInstance
->RemoteHost
, HostName
) == 0)) {
381 // Host Name and port number of the request URL are the same with previous call to Request().
382 // Check whether previous TCP packet sent out.
385 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTcpNotReady
, NULL
))) {
387 // Wrap the HTTP token in HTTP_TOKEN_WRAP
389 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
391 Status
= EFI_OUT_OF_RESOURCES
;
395 Wrap
->HttpToken
= Token
;
396 Wrap
->HttpInstance
= HttpInstance
;
398 Status
= HttpCreateTcpTxEvent (Wrap
);
399 if (EFI_ERROR (Status
)) {
403 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
404 if (EFI_ERROR (Status
)) {
408 Wrap
->TcpWrap
.Method
= Request
->Method
;
413 // Queue the HTTP token and return.
418 // Use existing TCP instance to transmit the packet.
425 // Need close existing TCP instance and create a new TCP instance for data transmit.
427 if (HttpInstance
->RemoteHost
!= NULL
) {
428 FreePool (HttpInstance
->RemoteHost
);
429 HttpInstance
->RemoteHost
= NULL
;
430 HttpInstance
->RemotePort
= 0;
438 // Parse Url for IPv4 or IPv6 address, if failed, perform DNS resolution.
440 if (!HttpInstance
->LocalAddressIsIPv6
) {
441 Status
= NetLibAsciiStrToIp4 (HostName
, &HttpInstance
->RemoteAddr
);
443 Status
= HttpUrlGetIp6 (Url
, UrlParser
, &HttpInstance
->RemoteIpv6Addr
);
446 if (EFI_ERROR (Status
)) {
447 HostNameSize
= AsciiStrSize (HostName
);
448 HostNameStr
= AllocateZeroPool (HostNameSize
* sizeof (CHAR16
));
449 if (HostNameStr
== NULL
) {
450 Status
= EFI_OUT_OF_RESOURCES
;
454 AsciiStrToUnicodeStrS (HostName
, HostNameStr
, HostNameSize
);
455 if (!HttpInstance
->LocalAddressIsIPv6
) {
456 Status
= HttpDns4 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteAddr
);
458 Status
= HttpDns6 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteIpv6Addr
);
461 FreePool (HostNameStr
);
462 if (EFI_ERROR (Status
)) {
468 // Save the RemotePort and RemoteHost.
470 ASSERT (HttpInstance
->RemoteHost
== NULL
);
471 HttpInstance
->RemotePort
= RemotePort
;
472 HttpInstance
->RemoteHost
= HostName
;
478 // The request URL is different from previous calls to Request(), close existing TCP instance.
480 if (!HttpInstance
->LocalAddressIsIPv6
) {
481 ASSERT (HttpInstance
->Tcp4
!= NULL
);
483 ASSERT (HttpInstance
->Tcp6
!= NULL
);
485 HttpCloseConnection (HttpInstance
);
486 EfiHttpCancel (This
, NULL
);
490 // Wrap the HTTP token in HTTP_TOKEN_WRAP
492 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
494 Status
= EFI_OUT_OF_RESOURCES
;
498 Wrap
->HttpToken
= Token
;
499 Wrap
->HttpInstance
= HttpInstance
;
500 if (Request
!= NULL
) {
501 Wrap
->TcpWrap
.Method
= Request
->Method
;
504 Status
= HttpInitTcp (HttpInstance
, Wrap
, Configure
);
505 if (EFI_ERROR (Status
)) {
511 // For the new HTTP token, create TX TCP token events.
513 Status
= HttpCreateTcpTxEvent (Wrap
);
514 if (EFI_ERROR (Status
)) {
520 // Create request message.
523 if (Url
!= NULL
&& *FileUrl
!= '/') {
525 // Convert the absolute-URI to the absolute-path
527 while (*FileUrl
!= ':') {
530 if ((*(FileUrl
+1) == '/') && (*(FileUrl
+2) == '/')) {
532 while (*FileUrl
!= '/') {
536 Status
= EFI_INVALID_PARAMETER
;
541 Status
= HttpGenRequestMessage (HttpMsg
, FileUrl
, &RequestMsg
, &RequestMsgSize
);
543 if (EFI_ERROR (Status
)) {
548 // Every request we insert a TxToken and a response call would remove the TxToken.
549 // In cases of PUT/POST, after an initial request-response pair, we would do a
550 // continuous request without a response call. So, in such cases, where Request
551 // structure is NULL, we would not insert a TxToken.
553 if (Request
!= NULL
) {
554 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
555 if (EFI_ERROR (Status
)) {
561 // Transmit the request message.
563 Status
= HttpTransmitTcp (
569 if (EFI_ERROR (Status
)) {
575 if (HostName
!= NULL
) {
583 // We would have inserted a TxToken only if Request structure is not NULL.
584 // Hence check before we do a remove in this error case.
586 if (Request
!= NULL
) {
587 NetMapRemoveTail (&HttpInstance
->TxTokens
, NULL
);
591 if (RequestMsg
!= NULL
) {
592 FreePool (RequestMsg
);
596 HttpCloseConnection (HttpInstance
);
599 HttpCloseTcpConnCloseEvent (HttpInstance
);
600 if (NULL
!= Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
) {
601 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
);
602 Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
= NULL
;
604 if (NULL
!= Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
) {
605 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
);
606 Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
= NULL
;
610 if (HostName
!= NULL
) {
616 if (UrlParser
!= NULL
) {
617 HttpUrlFreeParser (UrlParser
);
625 Cancel a user's Token.
627 @param[in] Map The HTTP instance's token queue.
628 @param[in] Item Object container for one HTTP token and token's wrap.
629 @param[in] Context The user's token to cancel.
631 @retval EFI_SUCCESS Continue to check the next Item.
632 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
639 IN NET_MAP_ITEM
*Item
,
643 EFI_HTTP_TOKEN
*Token
;
644 HTTP_TOKEN_WRAP
*Wrap
;
645 HTTP_PROTOCOL
*HttpInstance
;
647 Token
= (EFI_HTTP_TOKEN
*) Context
;
650 // Return EFI_SUCCESS to check the next item in the map if
651 // this one doesn't match.
653 if ((Token
!= NULL
) && (Token
!= Item
->Key
)) {
657 Wrap
= (HTTP_TOKEN_WRAP
*) Item
->Value
;
658 ASSERT (Wrap
!= NULL
);
659 HttpInstance
= Wrap
->HttpInstance
;
661 if (!HttpInstance
->LocalAddressIsIPv6
) {
662 if (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
!= NULL
) {
664 // Cancle the Token before close its Event.
666 HttpInstance
->Tcp4
->Cancel (HttpInstance
->Tcp4
, &Wrap
->TcpWrap
.Rx4Token
.CompletionToken
);
669 // Dispatch the DPC queued by the NotifyFunction of the canceled token's events.
674 if (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
!= NULL
) {
676 // Cancle the Token before close its Event.
678 HttpInstance
->Tcp6
->Cancel (HttpInstance
->Tcp6
, &Wrap
->TcpWrap
.Rx6Token
.CompletionToken
);
681 // Dispatch the DPC queued by the NotifyFunction of the canceled token's events.
688 // If only one item is to be cancel, return EFI_ABORTED to stop
689 // iterating the map any more.
699 Cancel the user's receive/transmit request. It is the worker function of
700 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the
703 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
704 @param[in] Token The token to cancel. If NULL, all token will be
707 @retval EFI_SUCCESS The token is cancelled.
708 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.
709 @retval Others Other error as indicated.
714 IN HTTP_PROTOCOL
*HttpInstance
,
715 IN EFI_HTTP_TOKEN
*Token
721 // First check the tokens queued by EfiHttpRequest().
723 Status
= NetMapIterate (&HttpInstance
->TxTokens
, HttpCancelTokens
, Token
);
724 if (EFI_ERROR (Status
)) {
726 if (Status
== EFI_ABORTED
) {
735 // Then check the tokens queued by EfiHttpResponse().
737 Status
= NetMapIterate (&HttpInstance
->RxTokens
, HttpCancelTokens
, Token
);
738 if (EFI_ERROR (Status
)) {
740 if (Status
== EFI_ABORTED
) {
743 return EFI_NOT_FOUND
;
755 Abort an asynchronous HTTP request or response token.
757 The Cancel() function aborts a pending HTTP request or response transaction. If
758 Token is not NULL and the token is in transmit or receive queues when it is being
759 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
760 be signaled. If the token is not in one of the queues, which usually means that the
761 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
762 all asynchronous tokens issued by Request() or Response() will be aborted.
764 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
765 @param[in] Token Point to storage containing HTTP request or response
768 @retval EFI_SUCCESS Request and Response queues are successfully flushed.
769 @retval EFI_INVALID_PARAMETER This is NULL.
770 @retval EFI_NOT_STARTED This instance hasn't been configured.
771 @retval EFI_NOT_FOUND The asynchronous request or response token is not
773 @retval EFI_UNSUPPORTED The implementation does not support this function.
779 IN EFI_HTTP_PROTOCOL
*This
,
780 IN EFI_HTTP_TOKEN
*Token
783 HTTP_PROTOCOL
*HttpInstance
;
786 return EFI_INVALID_PARAMETER
;
789 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
790 ASSERT (HttpInstance
!= NULL
);
792 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
793 return EFI_NOT_STARTED
;
796 return HttpCancel (HttpInstance
, Token
);
801 A callback function to intercept events during message parser.
803 This function will be invoked during HttpParseMessageBody() with various events type. An error
804 return status of the callback function will cause the HttpParseMessageBody() aborted.
806 @param[in] EventType Event type of this callback call.
807 @param[in] Data A pointer to data buffer.
808 @param[in] Length Length in bytes of the Data.
809 @param[in] Context Callback context set by HttpInitMsgParser().
811 @retval EFI_SUCCESS Continue to parser the message body.
816 HttpBodyParserCallback (
817 IN HTTP_BODY_PARSE_EVENT EventType
,
823 HTTP_TOKEN_WRAP
*Wrap
;
827 if (EventType
!= BodyParseEventOnComplete
) {
831 if (Data
== NULL
|| Length
!= 0 || Context
== NULL
) {
835 Wrap
= (HTTP_TOKEN_WRAP
*) Context
;
836 Body
= Wrap
->HttpToken
->Message
->Body
;
837 BodyLength
= Wrap
->HttpToken
->Message
->BodyLength
;
838 if (Data
< Body
+ BodyLength
) {
839 Wrap
->HttpInstance
->NextMsg
= Data
;
841 Wrap
->HttpInstance
->NextMsg
= NULL
;
846 // Free Tx4Token or Tx6Token since already received corrsponding HTTP response.
854 The work function of EfiHttpResponse().
856 @param[in] Wrap Pointer to HTTP token's wrap data.
858 @retval EFI_SUCCESS Allocation succeeded.
859 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
860 @retval EFI_NOT_READY Can't find a corresponding Tx4Token/Tx6Token or
861 the EFI_HTTP_UTILITIES_PROTOCOL is not available.
866 IN HTTP_TOKEN_WRAP
*Wrap
870 EFI_HTTP_MESSAGE
*HttpMsg
;
878 CHAR8
*StatusCodeStr
;
880 HTTP_PROTOCOL
*HttpInstance
;
881 EFI_HTTP_TOKEN
*Token
;
883 HTTP_TOKEN_WRAP
*ValueInItem
;
886 if (Wrap
== NULL
|| Wrap
->HttpInstance
== NULL
) {
887 return EFI_INVALID_PARAMETER
;
890 HttpInstance
= Wrap
->HttpInstance
;
891 Token
= Wrap
->HttpToken
;
892 HttpMsg
= Token
->Message
;
894 HttpInstance
->EndofHeader
= NULL
;
895 HttpInstance
->HttpHeaders
= NULL
;
896 HttpMsg
->Headers
= NULL
;
903 if (HttpMsg
->Data
.Response
!= NULL
) {
905 // Need receive the HTTP headers, prepare buffer.
907 Status
= HttpCreateTcpRxEventForHeader (HttpInstance
);
908 if (EFI_ERROR (Status
)) {
913 // Check whether we have cached header from previous call.
915 if ((HttpInstance
->CacheBody
!= NULL
) && (HttpInstance
->NextMsg
!= NULL
)) {
917 // The data is stored at [NextMsg, CacheBody + CacheLen].
919 HdrLen
= HttpInstance
->CacheBody
+ HttpInstance
->CacheLen
- HttpInstance
->NextMsg
;
920 HttpHeaders
= AllocateZeroPool (HdrLen
);
921 if (HttpHeaders
== NULL
) {
922 Status
= EFI_OUT_OF_RESOURCES
;
926 CopyMem (HttpHeaders
, HttpInstance
->NextMsg
, HdrLen
);
927 FreePool (HttpInstance
->CacheBody
);
928 HttpInstance
->CacheBody
= NULL
;
929 HttpInstance
->NextMsg
= NULL
;
930 HttpInstance
->CacheOffset
= 0;
931 SizeofHeaders
= HdrLen
;
932 BufferSize
= HttpInstance
->CacheLen
;
935 // Check whether we cached the whole HTTP headers.
937 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
940 HttpInstance
->EndofHeader
= &EndofHeader
;
941 HttpInstance
->HttpHeaders
= &HttpHeaders
;
944 if (HttpInstance
->TimeoutEvent
== NULL
) {
946 // Create TimeoutEvent for response
948 Status
= gBS
->CreateEvent (
953 &HttpInstance
->TimeoutEvent
955 if (EFI_ERROR (Status
)) {
961 // Start the timer, and wait Timeout seconds to receive the header packet.
963 Status
= gBS
->SetTimer (HttpInstance
->TimeoutEvent
, TimerRelative
, HTTP_RESPONSE_TIMEOUT
* TICKS_PER_SECOND
);
964 if (EFI_ERROR (Status
)) {
968 Status
= HttpTcpReceiveHeader (HttpInstance
, &SizeofHeaders
, &BufferSize
, HttpInstance
->TimeoutEvent
);
970 gBS
->SetTimer (HttpInstance
->TimeoutEvent
, TimerCancel
, 0);
972 if (EFI_ERROR (Status
)) {
976 ASSERT (HttpHeaders
!= NULL
);
979 // Cache the part of body.
981 BodyLen
= BufferSize
- (EndofHeader
- HttpHeaders
);
983 if (HttpInstance
->CacheBody
!= NULL
) {
984 FreePool (HttpInstance
->CacheBody
);
987 HttpInstance
->CacheBody
= AllocateZeroPool (BodyLen
);
988 if (HttpInstance
->CacheBody
== NULL
) {
989 Status
= EFI_OUT_OF_RESOURCES
;
993 CopyMem (HttpInstance
->CacheBody
, EndofHeader
, BodyLen
);
994 HttpInstance
->CacheLen
= BodyLen
;
998 // Search for Status Code.
1000 StatusCodeStr
= HttpHeaders
+ AsciiStrLen (HTTP_VERSION_STR
) + 1;
1001 if (StatusCodeStr
== NULL
) {
1002 Status
= EFI_NOT_READY
;
1006 StatusCode
= AsciiStrDecimalToUintn (StatusCodeStr
);
1009 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
1011 Tmp
= AsciiStrStr (HttpHeaders
, HTTP_CRLF_STR
);
1013 Status
= EFI_NOT_READY
;
1018 // We could have response with just a HTTP message and no headers. For Example,
1019 // "100 Continue". In such cases, we would not want to unnecessarily call a Parse
1020 // method. A "\r\n" following Tmp string again would indicate an end. Compare and
1021 // set SizeofHeaders to 0.
1023 Tmp
= Tmp
+ AsciiStrLen (HTTP_CRLF_STR
);
1024 if (CompareMem (Tmp
, HTTP_CRLF_STR
, AsciiStrLen (HTTP_CRLF_STR
)) == 0) {
1027 SizeofHeaders
= SizeofHeaders
- (Tmp
- HttpHeaders
);
1030 HttpMsg
->Data
.Response
->StatusCode
= HttpMappingToStatusCode (StatusCode
);
1031 HttpInstance
->StatusCode
= StatusCode
;
1033 Status
= EFI_NOT_READY
;
1037 // In cases of PUT/POST, after an initial request-response pair, we would do a
1038 // continuous request without a response call. So, we would not do an insert of
1039 // TxToken. After we have sent the complete file, we will call a response to get
1040 // a final response from server. In such a case, we would not have any TxTokens.
1041 // Hence, check that case before doing a NetMapRemoveHead.
1043 if (!NetMapIsEmpty (&HttpInstance
->TxTokens
)) {
1044 NetMapRemoveHead (&HttpInstance
->TxTokens
, (VOID
**) &ValueInItem
);
1045 if (ValueInItem
== NULL
) {
1050 // The first Tx Token not transmitted yet, insert back and return error.
1052 if (!ValueInItem
->TcpWrap
.IsTxDone
) {
1057 if (SizeofHeaders
!= 0) {
1058 HeaderTmp
= AllocateZeroPool (SizeofHeaders
);
1059 if (HeaderTmp
== NULL
) {
1060 Status
= EFI_OUT_OF_RESOURCES
;
1064 CopyMem (HeaderTmp
, Tmp
, SizeofHeaders
);
1065 FreePool (HttpHeaders
);
1066 HttpHeaders
= HeaderTmp
;
1069 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.
1071 if (mHttpUtilities
== NULL
) {
1072 Status
= EFI_NOT_READY
;
1077 // Parse the HTTP header into array of key/value pairs.
1079 Status
= mHttpUtilities
->Parse (
1084 &HttpMsg
->HeaderCount
1086 if (EFI_ERROR (Status
)) {
1090 FreePool (HttpHeaders
);
1095 // Init message-body parser by header information.
1097 Status
= HttpInitMsgParser (
1098 HttpInstance
->Method
,
1099 HttpMsg
->Data
.Response
->StatusCode
,
1100 HttpMsg
->HeaderCount
,
1102 HttpBodyParserCallback
,
1103 (VOID
*) ValueInItem
,
1104 &HttpInstance
->MsgParser
1106 if (EFI_ERROR (Status
)) {
1111 // Check whether we received a complete HTTP message.
1113 if (HttpInstance
->CacheBody
!= NULL
) {
1114 Status
= HttpParseMessageBody (HttpInstance
->MsgParser
, HttpInstance
->CacheLen
, HttpInstance
->CacheBody
);
1115 if (EFI_ERROR (Status
)) {
1119 if (HttpIsMessageComplete (HttpInstance
->MsgParser
)) {
1121 // Free the MsgParse since we already have a full HTTP message.
1123 HttpFreeMsgParser (HttpInstance
->MsgParser
);
1124 HttpInstance
->MsgParser
= NULL
;
1129 if ((HttpMsg
->Body
== NULL
) || (HttpMsg
->BodyLength
== 0)) {
1130 Status
= EFI_SUCCESS
;
1136 // Receive the response body.
1141 // First check whether we cached some data.
1143 if (HttpInstance
->CacheBody
!= NULL
) {
1145 // Calculate the length of the cached data.
1147 if (HttpInstance
->NextMsg
!= NULL
) {
1149 // We have a cached HTTP message which includes a part of HTTP header of next message.
1151 BodyLen
= HttpInstance
->NextMsg
- (HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
);
1153 BodyLen
= HttpInstance
->CacheLen
- HttpInstance
->CacheOffset
;
1158 // We have some cached data. Just copy the data and return.
1160 if (HttpMsg
->BodyLength
< BodyLen
) {
1161 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, HttpMsg
->BodyLength
);
1162 HttpInstance
->CacheOffset
= HttpInstance
->CacheOffset
+ HttpMsg
->BodyLength
;
1165 // Copy all cached data out.
1167 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, BodyLen
);
1168 HttpInstance
->CacheOffset
= BodyLen
+ HttpInstance
->CacheOffset
;
1169 HttpMsg
->BodyLength
= BodyLen
;
1171 if (HttpInstance
->NextMsg
== NULL
) {
1173 // There is no HTTP header of next message. Just free the cache buffer.
1175 FreePool (HttpInstance
->CacheBody
);
1176 HttpInstance
->CacheBody
= NULL
;
1177 HttpInstance
->NextMsg
= NULL
;
1178 HttpInstance
->CacheOffset
= 0;
1182 // Return since we aready received required data.
1184 Status
= EFI_SUCCESS
;
1188 if (BodyLen
== 0 && HttpInstance
->MsgParser
== NULL
) {
1190 // We received a complete HTTP message, and we don't have more data to return to caller.
1192 HttpMsg
->BodyLength
= 0;
1193 Status
= EFI_SUCCESS
;
1198 ASSERT (HttpInstance
->MsgParser
!= NULL
);
1201 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1203 Status
= HttpTcpReceiveBody (Wrap
, HttpMsg
);
1204 if (EFI_ERROR (Status
)) {
1211 Item
= NetMapFindKey (&Wrap
->HttpInstance
->RxTokens
, Wrap
->HttpToken
);
1213 NetMapRemoveItem (&Wrap
->HttpInstance
->RxTokens
, Item
, NULL
);
1216 if (HttpInstance
->StatusCode
>= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE
) {
1217 Token
->Status
= EFI_HTTP_ERROR
;
1219 Token
->Status
= Status
;
1222 gBS
->SignalEvent (Token
->Event
);
1223 HttpCloseTcpRxEvent (Wrap
);
1228 if (ValueInItem
!= NULL
) {
1229 NetMapInsertHead (&HttpInstance
->TxTokens
, ValueInItem
->HttpToken
, ValueInItem
);
1233 Item
= NetMapFindKey (&Wrap
->HttpInstance
->RxTokens
, Wrap
->HttpToken
);
1235 NetMapRemoveItem (&Wrap
->HttpInstance
->RxTokens
, Item
, NULL
);
1238 HttpTcpTokenCleanup (Wrap
);
1240 if (HttpHeaders
!= NULL
) {
1241 FreePool (HttpHeaders
);
1244 if (HttpMsg
->Headers
!= NULL
) {
1245 FreePool (HttpMsg
->Headers
);
1248 if (HttpInstance
->CacheBody
!= NULL
) {
1249 FreePool (HttpInstance
->CacheBody
);
1250 HttpInstance
->CacheBody
= NULL
;
1253 if (HttpInstance
->StatusCode
>= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE
) {
1254 Token
->Status
= EFI_HTTP_ERROR
;
1256 Token
->Status
= Status
;
1259 gBS
->SignalEvent (Token
->Event
);
1267 The Response() function queues an HTTP response to this HTTP instance, similar to
1268 Receive() function in the EFI TCP driver. When the HTTP response is received successfully,
1269 or if there is an error, Status in token will be updated and Event will be signaled.
1271 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1272 is received in the underlying TCP instance, the data will be parsed and Token will
1273 be populated with the response data. If the data received from the remote host
1274 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1275 (asynchronously) for more data to be sent from the remote host before signaling
1278 It is the responsibility of the caller to allocate a buffer for Body and specify the
1279 size in BodyLength. If the remote host provides a response that contains a content
1280 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1281 BodyLength will be updated with the amount of bytes received and copied to Body. This
1282 allows the client to download a large file in chunks instead of into one contiguous
1283 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1284 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1285 token to underlying TCP instance. If data arrives in the receive buffer, up to
1286 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1287 BodyLength with the amount of bytes received and copied to Body.
1289 If the HTTP driver does not have an open underlying TCP connection with the host
1290 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1291 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1292 an open TCP connection between client and host.
1294 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1295 @param[in] Token Pointer to storage containing HTTP response token.
1297 @retval EFI_SUCCESS Allocation succeeded.
1298 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1300 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1303 Token->Message->Headers is NULL.
1304 Token->Message is NULL.
1305 Token->Message->Body is not NULL,
1306 Token->Message->BodyLength is non-zero, and
1307 Token->Message->Data is NULL, but a previous call to
1308 Response() has not been completed successfully.
1309 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1310 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1311 specified by response URL.
1316 IN EFI_HTTP_PROTOCOL
*This
,
1317 IN EFI_HTTP_TOKEN
*Token
1321 EFI_HTTP_MESSAGE
*HttpMsg
;
1322 HTTP_PROTOCOL
*HttpInstance
;
1323 HTTP_TOKEN_WRAP
*Wrap
;
1325 if ((This
== NULL
) || (Token
== NULL
)) {
1326 return EFI_INVALID_PARAMETER
;
1329 HttpMsg
= Token
->Message
;
1330 if (HttpMsg
== NULL
) {
1331 return EFI_INVALID_PARAMETER
;
1334 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1335 ASSERT (HttpInstance
!= NULL
);
1337 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1338 return EFI_NOT_STARTED
;
1342 // Check whether the token already existed.
1344 if (EFI_ERROR (NetMapIterate (&HttpInstance
->RxTokens
, HttpTokenExist
, Token
))) {
1345 return EFI_ACCESS_DENIED
;
1348 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
1350 return EFI_OUT_OF_RESOURCES
;
1353 Wrap
->HttpInstance
= HttpInstance
;
1354 Wrap
->HttpToken
= Token
;
1356 Status
= HttpCreateTcpRxEvent (Wrap
);
1357 if (EFI_ERROR (Status
)) {
1361 Status
= NetMapInsertTail (&HttpInstance
->RxTokens
, Token
, Wrap
);
1362 if (EFI_ERROR (Status
)) {
1367 // If already have pending RxTokens, return directly.
1369 if (NetMapGetCount (&HttpInstance
->RxTokens
) > 1) {
1373 return HttpResponseWorker (Wrap
);
1377 if (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
!= NULL
) {
1378 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
);
1381 if (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
!= NULL
) {
1382 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
);
1391 The Poll() function can be used by network drivers and applications to increase the
1392 rate that data packets are moved between the communication devices and the transmit
1395 In some systems, the periodic timer event in the managed network driver may not poll
1396 the underlying communications device fast enough to transmit and/or receive all data
1397 packets without missing incoming packets or dropping outgoing packets. Drivers and
1398 applications that are experiencing packet loss should try calling the Poll() function
1401 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1403 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1404 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1405 @retval EFI_INVALID_PARAMETER This is NULL.
1406 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1407 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1413 IN EFI_HTTP_PROTOCOL
*This
1417 HTTP_PROTOCOL
*HttpInstance
;
1420 return EFI_INVALID_PARAMETER
;
1423 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1424 ASSERT (HttpInstance
!= NULL
);
1426 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1427 return EFI_NOT_STARTED
;
1430 if (HttpInstance
->LocalAddressIsIPv6
) {
1431 if (HttpInstance
->Tcp6
== NULL
) {
1432 return EFI_NOT_STARTED
;
1434 Status
= HttpInstance
->Tcp6
->Poll (HttpInstance
->Tcp6
);
1436 if (HttpInstance
->Tcp4
== NULL
) {
1437 return EFI_NOT_STARTED
;
1439 Status
= HttpInstance
->Tcp4
->Poll (HttpInstance
->Tcp4
);