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 is NULL.
118 HttpConfigData->LocalAddressIsIPv6 is FALSE and
119 HttpConfigData->IPv4Node is NULL.
120 HttpConfigData->LocalAddressIsIPv6 is TRUE and
121 HttpConfigData->IPv6Node is NULL.
122 @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling
123 Configure() with NULL to reset it.
124 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
125 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when
126 executing Configure().
127 @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported
128 in the implementation.
133 IN EFI_HTTP_PROTOCOL
*This
,
134 IN EFI_HTTP_CONFIG_DATA
*HttpConfigData
137 HTTP_PROTOCOL
*HttpInstance
;
141 // Check input parameters.
144 HttpConfigData
== NULL
||
145 ((HttpConfigData
->LocalAddressIsIPv6
&& HttpConfigData
->AccessPoint
.IPv6Node
== NULL
) ||
146 (!HttpConfigData
->LocalAddressIsIPv6
&& HttpConfigData
->AccessPoint
.IPv4Node
== NULL
))) {
147 return EFI_INVALID_PARAMETER
;
150 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
151 ASSERT (HttpInstance
!= NULL
&& HttpInstance
->Service
!= NULL
);
153 if (HttpConfigData
!= NULL
) {
156 // Now configure this HTTP instance.
158 if (HttpInstance
->State
!= HTTP_STATE_UNCONFIGED
) {
159 return EFI_ALREADY_STARTED
;
162 HttpInstance
->HttpVersion
= HttpConfigData
->HttpVersion
;
163 HttpInstance
->TimeOutMillisec
= HttpConfigData
->TimeOutMillisec
;
164 HttpInstance
->LocalAddressIsIPv6
= HttpConfigData
->LocalAddressIsIPv6
;
166 if (HttpConfigData
->LocalAddressIsIPv6
) {
168 &HttpInstance
->Ipv6Node
,
169 HttpConfigData
->AccessPoint
.IPv6Node
,
170 sizeof (HttpInstance
->Ipv6Node
)
174 &HttpInstance
->IPv4Node
,
175 HttpConfigData
->AccessPoint
.IPv4Node
,
176 sizeof (HttpInstance
->IPv4Node
)
183 Status
= HttpInitProtocol (HttpInstance
, HttpInstance
->LocalAddressIsIPv6
);
184 if (EFI_ERROR (Status
)) {
188 HttpInstance
->State
= HTTP_STATE_HTTP_CONFIGED
;
193 // Reset all the resources related to HttpInsance.
195 HttpCleanProtocol (HttpInstance
);
196 HttpInstance
->State
= HTTP_STATE_UNCONFIGED
;
203 The Request() function queues an HTTP request to this HTTP instance.
205 Similar to Transmit() function in the EFI TCP driver. When the HTTP request is sent
206 successfully, or if there is an error, Status in token will be updated and Event will
209 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
210 @param[in] Token Pointer to storage containing HTTP request token.
212 @retval EFI_SUCCESS Outgoing data was processed.
213 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
214 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
215 @retval EFI_TIMEOUT Data was dropped out of the transmit or receive queue.
216 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
217 @retval EFI_UNSUPPORTED The HTTP method is not supported in current
219 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
222 Token->Message is NULL.
223 Token->Message->Body is not NULL,
224 Token->Message->BodyLength is non-zero, and
225 Token->Message->Data is NULL, but a previous call to
226 Request()has not been completed successfully.
231 IN EFI_HTTP_PROTOCOL
*This
,
232 IN EFI_HTTP_TOKEN
*Token
235 EFI_HTTP_MESSAGE
*HttpMsg
;
236 EFI_HTTP_REQUEST_DATA
*Request
;
241 HTTP_PROTOCOL
*HttpInstance
;
248 HTTP_TOKEN_WRAP
*Wrap
;
250 UINTN RequestMsgSize
;
263 if ((This
== NULL
) || (Token
== NULL
)) {
264 return EFI_INVALID_PARAMETER
;
267 HttpMsg
= Token
->Message
;
268 if (HttpMsg
== NULL
) {
269 return EFI_INVALID_PARAMETER
;
272 Request
= HttpMsg
->Data
.Request
;
275 // Only support GET, HEAD, PUT and POST method in current implementation.
277 if ((Request
!= NULL
) && (Request
->Method
!= HttpMethodGet
) &&
278 (Request
->Method
!= HttpMethodHead
) && (Request
->Method
!= HttpMethodPut
) && (Request
->Method
!= HttpMethodPost
)) {
279 return EFI_UNSUPPORTED
;
282 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
283 ASSERT (HttpInstance
!= NULL
);
286 // Capture the method into HttpInstance.
288 if (Request
!= NULL
) {
289 HttpInstance
->Method
= Request
->Method
;
292 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
293 return EFI_NOT_STARTED
;
296 if (Request
== NULL
) {
298 // Request would be NULL only for PUT/POST operation (in the current implementation)
300 if ((HttpInstance
->Method
!= HttpMethodPut
) && (HttpInstance
->Method
!= HttpMethodPost
)) {
301 return EFI_INVALID_PARAMETER
;
305 // For PUT/POST, we need to have the TCP already configured. Bail out if it is not!
307 if (HttpInstance
->State
< HTTP_STATE_TCP_CONFIGED
) {
308 return EFI_INVALID_PARAMETER
;
312 // We need to have the Message Body for sending the HTTP message across in these cases.
314 if (HttpMsg
->Body
== NULL
|| HttpMsg
->BodyLength
== 0) {
315 return EFI_INVALID_PARAMETER
;
319 // Use existing TCP instance to transmit the packet.
325 // Check whether the token already existed.
327 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTokenExist
, Token
))) {
328 return EFI_ACCESS_DENIED
;
332 // Parse the URI of the remote host.
334 Url
= HttpInstance
->Url
;
335 UrlLen
= StrLen (Request
->Url
) + 1;
336 if (UrlLen
> HTTP_URL_BUFFER_LEN
) {
337 Url
= AllocateZeroPool (UrlLen
);
339 return EFI_OUT_OF_RESOURCES
;
341 FreePool (HttpInstance
->Url
);
342 HttpInstance
->Url
= Url
;
346 UnicodeStrToAsciiStr (Request
->Url
, Url
);
348 Status
= HttpParseUrl (Url
, (UINT32
) AsciiStrLen (Url
), FALSE
, &UrlParser
);
349 if (EFI_ERROR (Status
)) {
354 Status
= HttpUrlGetHostName (Url
, UrlParser
, &HostName
);
355 if (EFI_ERROR (Status
)) {
359 Status
= HttpUrlGetPort (Url
, UrlParser
, &RemotePort
);
360 if (EFI_ERROR (Status
)) {
361 RemotePort
= HTTP_DEFAULT_PORT
;
364 // If Configure is TRUE, it indicates the first time to call Request();
365 // If ReConfigure is TRUE, it indicates the request URL is not same
366 // with the previous call to Request();
371 if (HttpInstance
->RemoteHost
== NULL
) {
373 // Request() is called the first time.
377 if ((HttpInstance
->RemotePort
== RemotePort
) &&
378 (AsciiStrCmp (HttpInstance
->RemoteHost
, HostName
) == 0)) {
380 // Host Name and port number of the request URL are the same with previous call to Request().
381 // Check whether previous TCP packet sent out.
384 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTcpNotReady
, NULL
))) {
386 // Wrap the HTTP token in HTTP_TOKEN_WRAP
388 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
390 Status
= EFI_OUT_OF_RESOURCES
;
394 Wrap
->HttpToken
= Token
;
395 Wrap
->HttpInstance
= HttpInstance
;
397 Status
= HttpCreateTcpTxEvent (Wrap
);
398 if (EFI_ERROR (Status
)) {
402 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
403 if (EFI_ERROR (Status
)) {
407 Wrap
->TcpWrap
.Method
= Request
->Method
;
412 // Queue the HTTP token and return.
417 // Use existing TCP instance to transmit the packet.
424 // Need close existing TCP instance and create a new TCP instance for data transmit.
426 if (HttpInstance
->RemoteHost
!= NULL
) {
427 FreePool (HttpInstance
->RemoteHost
);
428 HttpInstance
->RemoteHost
= NULL
;
429 HttpInstance
->RemotePort
= 0;
437 // Parse Url for IPv4 or IPv6 address, if failed, perform DNS resolution.
439 if (!HttpInstance
->LocalAddressIsIPv6
) {
440 Status
= NetLibAsciiStrToIp4 (HostName
, &HttpInstance
->RemoteAddr
);
442 Status
= HttpUrlGetIp6 (Url
, UrlParser
, &HttpInstance
->RemoteIpv6Addr
);
445 if (EFI_ERROR (Status
)) {
446 HostNameStr
= AllocateZeroPool ((AsciiStrLen (HostName
) + 1) * sizeof (CHAR16
));
447 if (HostNameStr
== NULL
) {
448 Status
= EFI_OUT_OF_RESOURCES
;
452 AsciiStrToUnicodeStr (HostName
, HostNameStr
);
453 if (!HttpInstance
->LocalAddressIsIPv6
) {
454 Status
= HttpDns4 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteAddr
);
456 Status
= HttpDns6 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteIpv6Addr
);
459 FreePool (HostNameStr
);
460 if (EFI_ERROR (Status
)) {
467 // Save the RemotePort and RemoteHost.
469 ASSERT (HttpInstance
->RemoteHost
== NULL
);
470 HttpInstance
->RemotePort
= RemotePort
;
471 HttpInstance
->RemoteHost
= HostName
;
477 // The request URL is different from previous calls to Request(), close existing TCP instance.
479 if (!HttpInstance
->LocalAddressIsIPv6
) {
480 ASSERT (HttpInstance
->Tcp4
!= NULL
);
482 ASSERT (HttpInstance
->Tcp6
!= NULL
);
484 HttpCloseConnection (HttpInstance
);
485 EfiHttpCancel (This
, NULL
);
489 // Wrap the HTTP token in HTTP_TOKEN_WRAP
491 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
493 Status
= EFI_OUT_OF_RESOURCES
;
497 Wrap
->HttpToken
= Token
;
498 Wrap
->HttpInstance
= HttpInstance
;
499 if (Request
!= NULL
) {
500 Wrap
->TcpWrap
.Method
= Request
->Method
;
503 Status
= HttpInitTcp (HttpInstance
, Wrap
, Configure
);
504 if (EFI_ERROR (Status
)) {
510 // For the new HTTP token, create TX TCP token events.
512 Status
= HttpCreateTcpTxEvent (Wrap
);
513 if (EFI_ERROR (Status
)) {
519 // Create request message.
522 if (Url
!= NULL
&& *FileUrl
!= '/') {
524 // Convert the absolute-URI to the absolute-path
526 while (*FileUrl
!= ':') {
529 if ((*(FileUrl
+1) == '/') && (*(FileUrl
+2) == '/')) {
531 while (*FileUrl
!= '/') {
535 Status
= EFI_INVALID_PARAMETER
;
540 Status
= HttpGenRequestMessage (HttpMsg
, FileUrl
, &RequestMsg
, &RequestMsgSize
);
542 if (EFI_ERROR (Status
)) {
547 // Every request we insert a TxToken and a response call would remove the TxToken.
548 // In cases of PUT/POST, after an initial request-response pair, we would do a
549 // continuous request without a response call. So, in such cases, where Request
550 // structure is NULL, we would not insert a TxToken.
552 if (Request
!= NULL
) {
553 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
554 if (EFI_ERROR (Status
)) {
560 // Transmit the request message.
562 Status
= HttpTransmitTcp (
568 if (EFI_ERROR (Status
)) {
574 if (HostName
!= NULL
) {
582 // We would have inserted a TxToken only if Request structure is not NULL.
583 // Hence check before we do a remove in this error case.
585 if (Request
!= NULL
) {
586 NetMapRemoveTail (&HttpInstance
->TxTokens
, NULL
);
590 if (RequestMsg
!= NULL
) {
591 FreePool (RequestMsg
);
595 HttpCloseConnection (HttpInstance
);
598 HttpCloseTcpConnCloseEvent (HttpInstance
);
599 if (NULL
!= Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
) {
600 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
);
601 Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
= NULL
;
603 if (NULL
!= Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
) {
604 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
);
605 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
,
644 EFI_HTTP_TOKEN
*Token
;
645 HTTP_TOKEN_WRAP
*Wrap
;
646 HTTP_PROTOCOL
*HttpInstance
;
648 Token
= (EFI_HTTP_TOKEN
*) Context
;
651 // Return EFI_SUCCESS to check the next item in the map if
652 // this one doesn't match.
654 if ((Token
!= NULL
) && (Token
!= Item
->Key
)) {
658 Wrap
= (HTTP_TOKEN_WRAP
*) Item
->Value
;
659 ASSERT (Wrap
!= NULL
);
660 HttpInstance
= Wrap
->HttpInstance
;
665 NetMapRemoveItem (Map
, Item
, NULL
);
667 if (!HttpInstance
->LocalAddressIsIPv6
) {
668 if (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
!= NULL
) {
669 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
);
672 if (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
!= NULL
) {
673 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
);
676 if (Wrap
->TcpWrap
.Rx4Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
677 FreePool (Wrap
->TcpWrap
.Rx4Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
681 if (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
!= NULL
) {
682 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
);
685 if (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
!= NULL
) {
686 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
);
689 if (Wrap
->TcpWrap
.Rx6Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
690 FreePool (Wrap
->TcpWrap
.Rx6Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
698 // If only one item is to be cancel, return EFI_ABORTED to stop
699 // iterating the map any more.
709 Cancel the user's receive/transmit request. It is the worker function of
710 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the
713 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
714 @param[in] Token The token to cancel. If NULL, all token will be
717 @retval EFI_SUCCESS The token is cancelled.
718 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.
719 @retval Others Other error as indicated.
724 IN HTTP_PROTOCOL
*HttpInstance
,
725 IN EFI_HTTP_TOKEN
*Token
731 // First check the tokens queued by EfiHttpRequest().
733 Status
= NetMapIterate (&HttpInstance
->TxTokens
, HttpCancelTokens
, Token
);
734 if (EFI_ERROR (Status
)) {
736 if (Status
== EFI_ABORTED
) {
745 // Then check the tokens queued by EfiHttpResponse().
747 Status
= NetMapIterate (&HttpInstance
->RxTokens
, HttpCancelTokens
, Token
);
748 if (EFI_ERROR (Status
)) {
750 if (Status
== EFI_ABORTED
) {
753 return EFI_NOT_FOUND
;
765 Abort an asynchronous HTTP request or response token.
767 The Cancel() function aborts a pending HTTP request or response transaction. If
768 Token is not NULL and the token is in transmit or receive queues when it is being
769 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
770 be signaled. If the token is not in one of the queues, which usually means that the
771 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
772 all asynchronous tokens issued by Request() or Response() will be aborted.
774 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
775 @param[in] Token Point to storage containing HTTP request or response
778 @retval EFI_SUCCESS Request and Response queues are successfully flushed.
779 @retval EFI_INVALID_PARAMETER This is NULL.
780 @retval EFI_NOT_STARTED This instance hasn't been configured.
781 @retval EFI_NOT_FOUND The asynchronous request or response token is not
783 @retval EFI_UNSUPPORTED The implementation does not support this function.
789 IN EFI_HTTP_PROTOCOL
*This
,
790 IN EFI_HTTP_TOKEN
*Token
793 HTTP_PROTOCOL
*HttpInstance
;
796 return EFI_INVALID_PARAMETER
;
799 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
800 ASSERT (HttpInstance
!= NULL
);
802 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
803 return EFI_NOT_STARTED
;
806 return HttpCancel (HttpInstance
, Token
);
811 A callback function to intercept events during message parser.
813 This function will be invoked during HttpParseMessageBody() with various events type. An error
814 return status of the callback function will cause the HttpParseMessageBody() aborted.
816 @param[in] EventType Event type of this callback call.
817 @param[in] Data A pointer to data buffer.
818 @param[in] Length Length in bytes of the Data.
819 @param[in] Context Callback context set by HttpInitMsgParser().
821 @retval EFI_SUCCESS Continue to parser the message body.
826 HttpBodyParserCallback (
827 IN HTTP_BODY_PARSE_EVENT EventType
,
833 HTTP_TOKEN_WRAP
*Wrap
;
837 if (EventType
!= BodyParseEventOnComplete
) {
841 if (Data
== NULL
|| Length
!= 0 || Context
== NULL
) {
845 Wrap
= (HTTP_TOKEN_WRAP
*) Context
;
846 Body
= Wrap
->HttpToken
->Message
->Body
;
847 BodyLength
= Wrap
->HttpToken
->Message
->BodyLength
;
848 if (Data
< Body
+ BodyLength
) {
849 Wrap
->HttpInstance
->NextMsg
= Data
;
851 Wrap
->HttpInstance
->NextMsg
= NULL
;
856 // Free Tx4Token or Tx6Token since already received corrsponding HTTP response.
864 The work function of EfiHttpResponse().
866 @param[in] Wrap Pointer to HTTP token's wrap data.
868 @retval EFI_SUCCESS Allocation succeeded.
869 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
870 @retval EFI_NOT_READY Can't find a corresponding Tx4Token/Tx6Token or
871 the EFI_HTTP_UTILITIES_PROTOCOL is not available.
876 IN HTTP_TOKEN_WRAP
*Wrap
880 EFI_HTTP_MESSAGE
*HttpMsg
;
888 CHAR8
*StatusCodeStr
;
890 HTTP_PROTOCOL
*HttpInstance
;
891 EFI_HTTP_TOKEN
*Token
;
893 HTTP_TOKEN_WRAP
*ValueInItem
;
896 if (Wrap
== NULL
|| Wrap
->HttpInstance
== NULL
) {
897 return EFI_INVALID_PARAMETER
;
900 HttpInstance
= Wrap
->HttpInstance
;
901 Token
= Wrap
->HttpToken
;
902 HttpMsg
= Token
->Message
;
904 HttpInstance
->EndofHeader
= NULL
;
905 HttpInstance
->HttpHeaders
= NULL
;
906 HttpMsg
->Headers
= NULL
;
912 if (HttpMsg
->Data
.Response
!= NULL
) {
914 // Need receive the HTTP headers, prepare buffer.
916 Status
= HttpCreateTcpRxEventForHeader (HttpInstance
);
917 if (EFI_ERROR (Status
)) {
922 // Check whether we have cached header from previous call.
924 if ((HttpInstance
->CacheBody
!= NULL
) && (HttpInstance
->NextMsg
!= NULL
)) {
926 // The data is stored at [NextMsg, CacheBody + CacheLen].
928 HdrLen
= HttpInstance
->CacheBody
+ HttpInstance
->CacheLen
- HttpInstance
->NextMsg
;
929 HttpHeaders
= AllocateZeroPool (HdrLen
);
930 if (HttpHeaders
== NULL
) {
931 Status
= EFI_OUT_OF_RESOURCES
;
935 CopyMem (HttpHeaders
, HttpInstance
->NextMsg
, HdrLen
);
936 FreePool (HttpInstance
->CacheBody
);
937 HttpInstance
->CacheBody
= NULL
;
938 HttpInstance
->NextMsg
= NULL
;
939 HttpInstance
->CacheOffset
= 0;
940 SizeofHeaders
= HdrLen
;
941 BufferSize
= HttpInstance
->CacheLen
;
944 // Check whether we cached the whole HTTP headers.
946 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
949 HttpInstance
->EndofHeader
= &EndofHeader
;
950 HttpInstance
->HttpHeaders
= &HttpHeaders
;
953 if (HttpInstance
->TimeoutEvent
== NULL
) {
955 // Create TimeoutEvent for response
957 Status
= gBS
->CreateEvent (
962 &HttpInstance
->TimeoutEvent
964 if (EFI_ERROR (Status
)) {
970 // Start the timer, and wait Timeout seconds to receive the header packet.
972 Status
= gBS
->SetTimer (HttpInstance
->TimeoutEvent
, TimerRelative
, HTTP_RESPONSE_TIMEOUT
* TICKS_PER_SECOND
);
973 if (EFI_ERROR (Status
)) {
977 Status
= HttpTcpReceiveHeader (HttpInstance
, &SizeofHeaders
, &BufferSize
, HttpInstance
->TimeoutEvent
);
979 gBS
->SetTimer (HttpInstance
->TimeoutEvent
, TimerCancel
, 0);
981 if (EFI_ERROR (Status
)) {
985 ASSERT (HttpHeaders
!= NULL
);
988 // Cache the part of body.
990 BodyLen
= BufferSize
- (EndofHeader
- HttpHeaders
);
992 if (HttpInstance
->CacheBody
!= NULL
) {
993 FreePool (HttpInstance
->CacheBody
);
996 HttpInstance
->CacheBody
= AllocateZeroPool (BodyLen
);
997 if (HttpInstance
->CacheBody
== NULL
) {
998 Status
= EFI_OUT_OF_RESOURCES
;
1002 CopyMem (HttpInstance
->CacheBody
, EndofHeader
, BodyLen
);
1003 HttpInstance
->CacheLen
= BodyLen
;
1007 // Search for Status Code.
1009 StatusCodeStr
= HttpHeaders
+ AsciiStrLen (HTTP_VERSION_STR
) + 1;
1010 if (StatusCodeStr
== NULL
) {
1014 StatusCode
= AsciiStrDecimalToUintn (StatusCodeStr
);
1017 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
1019 Tmp
= AsciiStrStr (HttpHeaders
, HTTP_CRLF_STR
);
1025 // We could have response with just a HTTP message and no headers. For Example,
1026 // "100 Continue". In such cases, we would not want to unnecessarily call a Parse
1027 // method. A "\r\n" following Tmp string again would indicate an end. Compare and
1028 // set SizeofHeaders to 0.
1030 Tmp
= Tmp
+ AsciiStrLen (HTTP_CRLF_STR
);
1031 if (CompareMem (Tmp
, HTTP_CRLF_STR
, AsciiStrLen (HTTP_CRLF_STR
)) == 0) {
1034 SizeofHeaders
= SizeofHeaders
- (Tmp
- HttpHeaders
);
1037 HttpMsg
->Data
.Response
->StatusCode
= HttpMappingToStatusCode (StatusCode
);
1038 HttpInstance
->StatusCode
= StatusCode
;
1040 Status
= EFI_NOT_READY
;
1044 // In cases of PUT/POST, after an initial request-response pair, we would do a
1045 // continuous request without a response call. So, we would not do an insert of
1046 // TxToken. After we have sent the complete file, we will call a response to get
1047 // a final response from server. In such a case, we would not have any TxTokens.
1048 // Hence, check that case before doing a NetMapRemoveHead.
1050 if (!NetMapIsEmpty (&HttpInstance
->TxTokens
)) {
1051 NetMapRemoveHead (&HttpInstance
->TxTokens
, (VOID
**) &ValueInItem
);
1052 if (ValueInItem
== NULL
) {
1057 // The first Tx Token not transmitted yet, insert back and return error.
1059 if (!ValueInItem
->TcpWrap
.IsTxDone
) {
1064 if (SizeofHeaders
!= 0) {
1065 HeaderTmp
= AllocateZeroPool (SizeofHeaders
);
1066 if (HeaderTmp
== NULL
) {
1070 CopyMem (HeaderTmp
, Tmp
, SizeofHeaders
);
1071 FreePool (HttpHeaders
);
1072 HttpHeaders
= HeaderTmp
;
1075 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.
1077 if (mHttpUtilities
== NULL
) {
1078 Status
= EFI_NOT_READY
;
1083 // Parse the HTTP header into array of key/value pairs.
1085 Status
= mHttpUtilities
->Parse (
1090 &HttpMsg
->HeaderCount
1092 if (EFI_ERROR (Status
)) {
1096 FreePool (HttpHeaders
);
1101 // Init message-body parser by header information.
1103 Status
= HttpInitMsgParser (
1104 HttpInstance
->Method
,
1105 HttpMsg
->Data
.Response
->StatusCode
,
1106 HttpMsg
->HeaderCount
,
1108 HttpBodyParserCallback
,
1109 (VOID
*) ValueInItem
,
1110 &HttpInstance
->MsgParser
1112 if (EFI_ERROR (Status
)) {
1117 // Check whether we received a complete HTTP message.
1119 if (HttpInstance
->CacheBody
!= NULL
) {
1120 Status
= HttpParseMessageBody (HttpInstance
->MsgParser
, HttpInstance
->CacheLen
, HttpInstance
->CacheBody
);
1121 if (EFI_ERROR (Status
)) {
1125 if (HttpIsMessageComplete (HttpInstance
->MsgParser
)) {
1127 // Free the MsgParse since we already have a full HTTP message.
1129 HttpFreeMsgParser (HttpInstance
->MsgParser
);
1130 HttpInstance
->MsgParser
= NULL
;
1135 if ((HttpMsg
->Body
== NULL
) || (HttpMsg
->BodyLength
== 0)) {
1136 Status
= EFI_SUCCESS
;
1142 // Receive the response body.
1147 // First check whether we cached some data.
1149 if (HttpInstance
->CacheBody
!= NULL
) {
1151 // Calculate the length of the cached data.
1153 if (HttpInstance
->NextMsg
!= NULL
) {
1155 // We have a cached HTTP message which includes a part of HTTP header of next message.
1157 BodyLen
= HttpInstance
->NextMsg
- (HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
);
1159 BodyLen
= HttpInstance
->CacheLen
- HttpInstance
->CacheOffset
;
1164 // We have some cached data. Just copy the data and return.
1166 if (HttpMsg
->BodyLength
< BodyLen
) {
1167 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, HttpMsg
->BodyLength
);
1168 HttpInstance
->CacheOffset
= HttpInstance
->CacheOffset
+ HttpMsg
->BodyLength
;
1171 // Copy all cached data out.
1173 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, BodyLen
);
1174 HttpInstance
->CacheOffset
= BodyLen
+ HttpInstance
->CacheOffset
;
1175 HttpMsg
->BodyLength
= BodyLen
;
1177 if (HttpInstance
->NextMsg
== NULL
) {
1179 // There is no HTTP header of next message. Just free the cache buffer.
1181 FreePool (HttpInstance
->CacheBody
);
1182 HttpInstance
->CacheBody
= NULL
;
1183 HttpInstance
->NextMsg
= NULL
;
1184 HttpInstance
->CacheOffset
= 0;
1188 // Return since we aready received required data.
1190 Status
= EFI_SUCCESS
;
1194 if (BodyLen
== 0 && HttpInstance
->MsgParser
== NULL
) {
1196 // We received a complete HTTP message, and we don't have more data to return to caller.
1198 HttpMsg
->BodyLength
= 0;
1199 Status
= EFI_SUCCESS
;
1204 ASSERT (HttpInstance
->MsgParser
!= NULL
);
1206 if (HttpInstance
->TimeoutEvent
== NULL
) {
1208 // Create TimeoutEvent for response
1210 Status
= gBS
->CreateEvent (
1215 &HttpInstance
->TimeoutEvent
1217 if (EFI_ERROR (Status
)) {
1223 // Start the timer, and wait Timeout seconds to receive the body packet.
1225 Status
= gBS
->SetTimer (HttpInstance
->TimeoutEvent
, TimerRelative
, HTTP_RESPONSE_TIMEOUT
* TICKS_PER_SECOND
);
1226 if (EFI_ERROR (Status
)) {
1231 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1233 Status
= HttpTcpReceiveBody (Wrap
, HttpMsg
, HttpInstance
->TimeoutEvent
);
1235 gBS
->SetTimer (HttpInstance
->TimeoutEvent
, TimerCancel
, 0);
1237 if (EFI_ERROR (Status
)) {
1245 Item
= NetMapFindKey (&Wrap
->HttpInstance
->RxTokens
, Wrap
->HttpToken
);
1247 NetMapRemoveItem (&Wrap
->HttpInstance
->RxTokens
, Item
, NULL
);
1250 if (HttpInstance
->StatusCode
>= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE
) {
1251 Token
->Status
= EFI_HTTP_ERROR
;
1253 Token
->Status
= Status
;
1256 gBS
->SignalEvent (Token
->Event
);
1257 HttpCloseTcpRxEvent (Wrap
);
1262 if (ValueInItem
!= NULL
) {
1263 NetMapInsertHead (&HttpInstance
->TxTokens
, ValueInItem
->HttpToken
, ValueInItem
);
1267 HttpTcpTokenCleanup (Wrap
);
1269 if (HttpHeaders
!= NULL
) {
1270 FreePool (HttpHeaders
);
1273 if (HttpMsg
->Headers
!= NULL
) {
1274 FreePool (HttpMsg
->Headers
);
1277 if (HttpInstance
->CacheBody
!= NULL
) {
1278 FreePool (HttpInstance
->CacheBody
);
1279 HttpInstance
->CacheBody
= NULL
;
1282 if (HttpInstance
->StatusCode
>= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE
) {
1283 Token
->Status
= EFI_HTTP_ERROR
;
1285 Token
->Status
= Status
;
1288 gBS
->SignalEvent (Token
->Event
);
1296 The Response() function queues an HTTP response to this HTTP instance, similar to
1297 Receive() function in the EFI TCP driver. When the HTTP response is received successfully,
1298 or if there is an error, Status in token will be updated and Event will be signaled.
1300 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1301 is received in the underlying TCP instance, the data will be parsed and Token will
1302 be populated with the response data. If the data received from the remote host
1303 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1304 (asynchronously) for more data to be sent from the remote host before signaling
1307 It is the responsibility of the caller to allocate a buffer for Body and specify the
1308 size in BodyLength. If the remote host provides a response that contains a content
1309 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1310 BodyLength will be updated with the amount of bytes received and copied to Body. This
1311 allows the client to download a large file in chunks instead of into one contiguous
1312 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1313 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1314 token to underlying TCP instance. If data arrives in the receive buffer, up to
1315 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1316 BodyLength with the amount of bytes received and copied to Body.
1318 If the HTTP driver does not have an open underlying TCP connection with the host
1319 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1320 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1321 an open TCP connection between client and host.
1323 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1324 @param[in] Token Pointer to storage containing HTTP response token.
1326 @retval EFI_SUCCESS Allocation succeeded.
1327 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1329 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1332 Token->Message->Headers is NULL.
1333 Token->Message is NULL.
1334 Token->Message->Body is not NULL,
1335 Token->Message->BodyLength is non-zero, and
1336 Token->Message->Data is NULL, but a previous call to
1337 Response() has not been completed successfully.
1338 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1339 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1340 specified by response URL.
1345 IN EFI_HTTP_PROTOCOL
*This
,
1346 IN EFI_HTTP_TOKEN
*Token
1350 EFI_HTTP_MESSAGE
*HttpMsg
;
1351 HTTP_PROTOCOL
*HttpInstance
;
1352 HTTP_TOKEN_WRAP
*Wrap
;
1354 if ((This
== NULL
) || (Token
== NULL
)) {
1355 return EFI_INVALID_PARAMETER
;
1358 HttpMsg
= Token
->Message
;
1359 if (HttpMsg
== NULL
) {
1360 return EFI_INVALID_PARAMETER
;
1363 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1364 ASSERT (HttpInstance
!= NULL
);
1366 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1367 return EFI_NOT_STARTED
;
1371 // Check whether the token already existed.
1373 if (EFI_ERROR (NetMapIterate (&HttpInstance
->RxTokens
, HttpTokenExist
, Token
))) {
1374 return EFI_ACCESS_DENIED
;
1377 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
1379 return EFI_OUT_OF_RESOURCES
;
1382 Wrap
->HttpInstance
= HttpInstance
;
1383 Wrap
->HttpToken
= Token
;
1385 Status
= HttpCreateTcpRxEvent (Wrap
);
1386 if (EFI_ERROR (Status
)) {
1390 Status
= NetMapInsertTail (&HttpInstance
->RxTokens
, Token
, Wrap
);
1391 if (EFI_ERROR (Status
)) {
1396 // If already have pending RxTokens, return directly.
1398 if (NetMapGetCount (&HttpInstance
->RxTokens
) > 1) {
1402 return HttpResponseWorker (Wrap
);
1406 if (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
!= NULL
) {
1407 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
);
1410 if (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
!= NULL
) {
1411 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
);
1420 The Poll() function can be used by network drivers and applications to increase the
1421 rate that data packets are moved between the communication devices and the transmit
1424 In some systems, the periodic timer event in the managed network driver may not poll
1425 the underlying communications device fast enough to transmit and/or receive all data
1426 packets without missing incoming packets or dropping outgoing packets. Drivers and
1427 applications that are experiencing packet loss should try calling the Poll() function
1430 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1432 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1433 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1434 @retval EFI_INVALID_PARAMETER This is NULL.
1435 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1436 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1442 IN EFI_HTTP_PROTOCOL
*This
1446 HTTP_PROTOCOL
*HttpInstance
;
1449 return EFI_INVALID_PARAMETER
;
1452 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1453 ASSERT (HttpInstance
!= NULL
);
1455 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1456 return EFI_NOT_STARTED
;
1459 if (HttpInstance
->LocalAddressIsIPv6
) {
1460 if (HttpInstance
->Tcp6
== NULL
) {
1461 return EFI_NOT_STARTED
;
1463 Status
= HttpInstance
->Tcp6
->Poll (HttpInstance
->Tcp6
);
1465 if (HttpInstance
->Tcp4
== NULL
) {
1466 return EFI_NOT_STARTED
;
1468 Status
= HttpInstance
->Tcp4
->Poll (HttpInstance
->Tcp4
);