2 Implementation of EFI_HTTP_PROTOCOL protocol interfaces.
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 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 HttpConfigData->AccessPoint is NULL.
43 @retval EFI_NOT_STARTED The HTTP instance is not configured.
49 IN EFI_HTTP_PROTOCOL
*This
,
50 OUT EFI_HTTP_CONFIG_DATA
*HttpConfigData
53 HTTP_PROTOCOL
*HttpInstance
;
54 EFI_HTTPv4_ACCESS_POINT
*Http4AccessPoint
;
55 EFI_HTTPv6_ACCESS_POINT
*Http6AccessPoint
;
57 if ((This
== NULL
) || (HttpConfigData
== NULL
)) {
58 return EFI_INVALID_PARAMETER
;
61 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
62 ASSERT (HttpInstance
!= NULL
);
64 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
65 return EFI_NOT_STARTED
;
68 HttpConfigData
->HttpVersion
= HttpInstance
->HttpVersion
;
69 HttpConfigData
->TimeOutMillisec
= HttpInstance
->TimeOutMillisec
;
70 HttpConfigData
->LocalAddressIsIPv6
= HttpInstance
->LocalAddressIsIPv6
;
72 if (HttpInstance
->LocalAddressIsIPv6
) {
73 Http6AccessPoint
= AllocateZeroPool (sizeof (EFI_HTTPv6_ACCESS_POINT
));
76 &HttpInstance
->Ipv6Node
,
77 sizeof (HttpInstance
->Ipv6Node
)
79 HttpConfigData
->AccessPoint
.IPv6Node
= Http6AccessPoint
;
81 Http4AccessPoint
= AllocateZeroPool (sizeof (EFI_HTTPv4_ACCESS_POINT
));
84 &HttpInstance
->IPv4Node
,
85 sizeof (HttpInstance
->IPv4Node
)
87 HttpConfigData
->AccessPoint
.IPv4Node
= Http4AccessPoint
;
94 Initialize or brutally reset the operational parameters for this EFI HTTP instance.
96 The Configure() function does the following:
97 When HttpConfigData is not NULL Initialize this EFI HTTP instance by configuring
98 timeout, local address, port, etc.
99 When HttpConfigData is NULL, reset this EFI HTTP instance by closing all active
100 connections with remote hosts, canceling all asynchronous tokens, and flush request
101 and response buffers without informing the appropriate hosts.
103 Except for GetModeData() and Configure(), No other EFI HTTP function can be executed
104 by this instance until the Configure() function is executed and returns successfully.
106 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
107 @param[in] HttpConfigData Pointer to the configure data to configure the instance.
109 @retval EFI_SUCCESS Operation succeeded.
110 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
112 HttpConfigData->LocalAddressIsIPv6 is FALSE and
113 HttpConfigData->IPv4Node is NULL.
114 HttpConfigData->LocalAddressIsIPv6 is TRUE and
115 HttpConfigData->IPv6Node is NULL.
116 @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling
117 Configure() with NULL to reset it.
118 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
119 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when
120 executing Configure().
121 @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported
122 in the implementation.
127 IN EFI_HTTP_PROTOCOL
*This
,
128 IN EFI_HTTP_CONFIG_DATA
*HttpConfigData
131 HTTP_PROTOCOL
*HttpInstance
;
135 // Check input parameters.
138 (HttpConfigData
!= NULL
&& ((HttpConfigData
->LocalAddressIsIPv6
&& HttpConfigData
->AccessPoint
.IPv6Node
== NULL
) ||
139 (!HttpConfigData
->LocalAddressIsIPv6
&& HttpConfigData
->AccessPoint
.IPv4Node
== NULL
)))) {
140 return EFI_INVALID_PARAMETER
;
143 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
144 ASSERT (HttpInstance
!= NULL
&& HttpInstance
->Service
!= NULL
);
146 if (HttpConfigData
!= NULL
) {
149 // Now configure this HTTP instance.
151 if (HttpInstance
->State
!= HTTP_STATE_UNCONFIGED
) {
152 return EFI_ALREADY_STARTED
;
155 HttpInstance
->HttpVersion
= HttpConfigData
->HttpVersion
;
156 HttpInstance
->TimeOutMillisec
= HttpConfigData
->TimeOutMillisec
;
157 HttpInstance
->LocalAddressIsIPv6
= HttpConfigData
->LocalAddressIsIPv6
;
159 if (HttpConfigData
->LocalAddressIsIPv6
) {
161 &HttpInstance
->Ipv6Node
,
162 HttpConfigData
->AccessPoint
.IPv6Node
,
163 sizeof (HttpInstance
->Ipv6Node
)
167 &HttpInstance
->IPv4Node
,
168 HttpConfigData
->AccessPoint
.IPv4Node
,
169 sizeof (HttpInstance
->IPv4Node
)
175 Status
= HttpInitProtocol (HttpInstance
, HttpInstance
->LocalAddressIsIPv6
);
176 if (EFI_ERROR (Status
)) {
180 HttpInstance
->State
= HTTP_STATE_HTTP_CONFIGED
;
185 // Reset all the resources related to HttpInsance.
187 HttpCleanProtocol (HttpInstance
);
188 HttpInstance
->State
= HTTP_STATE_UNCONFIGED
;
195 The Request() function queues an HTTP request to this HTTP instance.
197 Similar to Transmit() function in the EFI TCP driver. When the HTTP request is sent
198 successfully, or if there is an error, Status in token will be updated and Event will
201 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
202 @param[in] Token Pointer to storage containing HTTP request token.
204 @retval EFI_SUCCESS Outgoing data was processed.
205 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
206 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
207 @retval EFI_TIMEOUT Data was dropped out of the transmit or receive queue.
208 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
209 @retval EFI_UNSUPPORTED The HTTP method is not supported in current
211 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
213 Token->Message is NULL.
214 Token->Message->Body is not NULL,
215 Token->Message->BodyLength is non-zero, and
216 Token->Message->Data is NULL, but a previous call to
217 Request()has not been completed successfully.
222 IN EFI_HTTP_PROTOCOL
*This
,
223 IN EFI_HTTP_TOKEN
*Token
226 EFI_HTTP_MESSAGE
*HttpMsg
;
227 EFI_HTTP_REQUEST_DATA
*Request
;
232 HTTP_PROTOCOL
*HttpInstance
;
239 HTTP_TOKEN_WRAP
*Wrap
;
242 if ((This
== NULL
) || (Token
== NULL
)) {
243 return EFI_INVALID_PARAMETER
;
246 HttpMsg
= Token
->Message
;
247 if ((HttpMsg
== NULL
) || (HttpMsg
->Headers
== NULL
)) {
248 return EFI_INVALID_PARAMETER
;
252 // Current implementation does not support POST/PUT method.
253 // If future version supports these two methods, Request could be NULL for a special case that to send large amounts
254 // of data. For this case, the implementation need check whether previous call to Request() has been completed or not.
257 Request
= HttpMsg
->Data
.Request
;
258 if ((Request
== NULL
) || (Request
->Url
== NULL
)) {
259 return EFI_INVALID_PARAMETER
;
263 // Only support GET and HEAD method in current implementation.
265 if ((Request
->Method
!= HttpMethodGet
) && (Request
->Method
!= HttpMethodHead
)) {
266 return EFI_UNSUPPORTED
;
269 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
270 ASSERT (HttpInstance
!= NULL
);
272 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
273 return EFI_NOT_STARTED
;
277 // Check whether the token already existed.
279 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTokenExist
, Token
))) {
280 return EFI_ACCESS_DENIED
;
288 // Parse the URI of the remote host.
290 Url
= HttpInstance
->Url
;
291 UrlLen
= StrLen (Request
->Url
) + 1;
292 if (UrlLen
> HTTP_URL_BUFFER_LEN
) {
293 Url
= AllocateZeroPool (UrlLen
);
295 return EFI_OUT_OF_RESOURCES
;
297 FreePool (HttpInstance
->Url
);
298 HttpInstance
->Url
= Url
;
302 UnicodeStrToAsciiStr (Request
->Url
, Url
);
304 Status
= HttpParseUrl (Url
, (UINT32
) AsciiStrLen (Url
), FALSE
, &UrlParser
);
305 if (EFI_ERROR (Status
)) {
311 Status
= HttpUrlGetHostName (Url
, UrlParser
, &HostName
);
312 if (EFI_ERROR (Status
)) {
316 Status
= HttpUrlGetPort (Url
, UrlParser
, &RemotePort
);
317 if (EFI_ERROR (Status
)) {
318 RemotePort
= HTTP_DEFAULT_PORT
;
321 // If Configure is TRUE, it indicates the first time to call Request();
322 // If ReConfigure is TRUE, it indicates the request URL is not same
323 // with the previous call to Request();
328 if (HttpInstance
->RemoteHost
== NULL
) {
330 // Request() is called the first time.
334 if ((HttpInstance
->RemotePort
== RemotePort
) &&
335 (AsciiStrCmp (HttpInstance
->RemoteHost
, HostName
) == 0)) {
337 // Host Name and port number of the request URL are the same with previous call to Request().
338 // Check whether previous TCP packet sent out.
340 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTcpNotReady
, NULL
))) {
342 // Wrap the HTTP token in HTTP_TOKEN_WRAP
344 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
346 Status
= EFI_OUT_OF_RESOURCES
;
350 Wrap
->HttpToken
= Token
;
351 Wrap
->HttpInstance
= HttpInstance
;
353 Status
= HttpCreateTcpTxEvent (Wrap
);
354 if (EFI_ERROR (Status
)) {
358 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
359 if (EFI_ERROR (Status
)) {
363 Wrap
->TcpWrap
.Method
= Request
->Method
;
368 // Queue the HTTP token and return.
373 // Use existing TCP instance to transmit the packet.
380 // Need close existing TCP instance and create a new TCP instance for data transmit.
382 if (HttpInstance
->RemoteHost
!= NULL
) {
383 FreePool (HttpInstance
->RemoteHost
);
384 HttpInstance
->RemoteHost
= NULL
;
385 HttpInstance
->RemotePort
= 0;
392 // Parse Url for IPv4 or IPv6 address, if failed, perform DNS resolution.
394 if (!HttpInstance
->LocalAddressIsIPv6
) {
395 Status
= NetLibAsciiStrToIp4 (HostName
, &HttpInstance
->RemoteAddr
);
397 Status
= HttpUrlGetIp6 (Url
, UrlParser
, &HttpInstance
->RemoteIpv6Addr
);
400 if (EFI_ERROR (Status
)) {
401 HostNameStr
= AllocateZeroPool ((AsciiStrLen (HostName
) + 1) * sizeof (CHAR16
));
402 if (HostNameStr
== NULL
) {
403 Status
= EFI_OUT_OF_RESOURCES
;
407 AsciiStrToUnicodeStr (HostName
, HostNameStr
);
408 if (!HttpInstance
->LocalAddressIsIPv6
) {
409 Status
= HttpDns4 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteAddr
);
411 Status
= HttpDns6 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteIpv6Addr
);
414 FreePool (HostNameStr
);
415 if (EFI_ERROR (Status
)) {
422 // Save the RemotePort and RemoteHost.
424 ASSERT (HttpInstance
->RemoteHost
== NULL
);
425 HttpInstance
->RemotePort
= RemotePort
;
426 HttpInstance
->RemoteHost
= HostName
;
432 // The request URL is different from previous calls to Request(), close existing TCP instance.
434 if (!HttpInstance
->LocalAddressIsIPv6
) {
435 ASSERT (HttpInstance
->Tcp4
!= NULL
);
437 ASSERT (HttpInstance
->Tcp6
!= NULL
);
439 HttpCloseConnection (HttpInstance
);
440 EfiHttpCancel (This
, NULL
);
444 // Wrap the HTTP token in HTTP_TOKEN_WRAP
446 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
448 Status
= EFI_OUT_OF_RESOURCES
;
452 Wrap
->HttpToken
= Token
;
453 Wrap
->HttpInstance
= HttpInstance
;
454 Wrap
->TcpWrap
.Method
= Request
->Method
;
456 Status
= HttpInitTcp (HttpInstance
, Wrap
, Configure
);
457 if (EFI_ERROR (Status
)) {
463 // For the new HTTP token, create TX TCP token events.
465 Status
= HttpCreateTcpTxEvent (Wrap
);
466 if (EFI_ERROR (Status
)) {
472 // Create request message.
475 if (*FileUrl
!= '/') {
477 // Convert the absolute-URI to the absolute-path
479 while (*FileUrl
!= ':') {
482 if ((*(FileUrl
+1) == '/') && (*(FileUrl
+2) == '/')) {
484 while (*FileUrl
!= '/') {
488 Status
= EFI_INVALID_PARAMETER
;
492 RequestStr
= HttpGenRequestString (HttpInstance
, HttpMsg
, FileUrl
);
493 if (RequestStr
== NULL
) {
494 Status
= EFI_OUT_OF_RESOURCES
;
498 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
499 if (EFI_ERROR (Status
)) {
504 // Transmit the request message.
506 Status
= HttpTransmitTcp (
510 AsciiStrLen (RequestStr
)
512 if (EFI_ERROR (Status
)) {
518 if (HostName
!= NULL
) {
525 NetMapRemoveTail (&HttpInstance
->TxTokens
, NULL
);
528 if (RequestStr
!= NULL
) {
529 FreePool (RequestStr
);
533 HttpCloseConnection (HttpInstance
);
536 HttpCloseTcpConnCloseEvent (HttpInstance
);
537 if (NULL
!= Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
) {
538 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
);
539 Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
= NULL
;
541 if (NULL
!= Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
) {
542 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
);
543 Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
= NULL
;
548 if (HostName
!= NULL
) {
554 if (UrlParser
!= NULL
) {
555 HttpUrlFreeParser (UrlParser
);
563 Cancel a user's Token.
565 @param[in] Map The HTTP instance's token queue.
566 @param[in] Item Object container for one HTTP token and token's wrap.
567 @param[in] Context The user's token to cancel.
569 @retval EFI_SUCCESS Continue to check the next Item.
570 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
577 IN NET_MAP_ITEM
*Item
,
582 EFI_HTTP_TOKEN
*Token
;
583 HTTP_TOKEN_WRAP
*Wrap
;
584 HTTP_PROTOCOL
*HttpInstance
;
586 Token
= (EFI_HTTP_TOKEN
*) Context
;
589 // Return EFI_SUCCESS to check the next item in the map if
590 // this one doesn't match.
592 if ((Token
!= NULL
) && (Token
!= Item
->Key
)) {
596 Wrap
= (HTTP_TOKEN_WRAP
*) Item
->Value
;
597 ASSERT (Wrap
!= NULL
);
598 HttpInstance
= Wrap
->HttpInstance
;
603 NetMapRemoveItem (Map
, Item
, NULL
);
605 if (!HttpInstance
->LocalAddressIsIPv6
) {
606 if (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
!= NULL
) {
607 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
);
610 if (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
!= NULL
) {
611 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
);
614 if (Wrap
->TcpWrap
.Rx4Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
615 FreePool (Wrap
->TcpWrap
.Rx4Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
619 if (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
!= NULL
) {
620 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
);
623 if (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
!= NULL
) {
624 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
);
627 if (Wrap
->TcpWrap
.Rx6Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
628 FreePool (Wrap
->TcpWrap
.Rx6Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
636 // If only one item is to be cancel, return EFI_ABORTED to stop
637 // iterating the map any more.
647 Cancel the user's receive/transmit request. It is the worker function of
648 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the
651 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
652 @param[in] Token The token to cancel. If NULL, all token will be
655 @retval EFI_SUCCESS The token is cancelled.
656 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.
657 @retval Others Other error as indicated.
662 IN HTTP_PROTOCOL
*HttpInstance
,
663 IN EFI_HTTP_TOKEN
*Token
669 // First check the tokens queued by EfiHttpRequest().
671 Status
= NetMapIterate (&HttpInstance
->TxTokens
, HttpCancelTokens
, Token
);
672 if (EFI_ERROR (Status
)) {
674 if (Status
== EFI_ABORTED
) {
683 // Then check the tokens queued by EfiHttpResponse().
685 Status
= NetMapIterate (&HttpInstance
->RxTokens
, HttpCancelTokens
, Token
);
686 if (EFI_ERROR (Status
)) {
688 if (Status
== EFI_ABORTED
) {
691 return EFI_NOT_FOUND
;
703 Abort an asynchronous HTTP request or response token.
705 The Cancel() function aborts a pending HTTP request or response transaction. If
706 Token is not NULL and the token is in transmit or receive queues when it is being
707 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
708 be signaled. If the token is not in one of the queues, which usually means that the
709 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
710 all asynchronous tokens issued by Request() or Response() will be aborted.
712 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
713 @param[in] Token Point to storage containing HTTP request or response
716 @retval EFI_SUCCESS Request and Response queues are successfully flushed.
717 @retval EFI_INVALID_PARAMETER This is NULL.
718 @retval EFI_NOT_STARTED This instance hasn't been configured.
719 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP,
720 BOOTP, RARP, etc.) hasn't finished yet.
721 @retval EFI_NOT_FOUND The asynchronous request or response token is not
723 @retval EFI_UNSUPPORTED The implementation does not support this function.
729 IN EFI_HTTP_PROTOCOL
*This
,
730 IN EFI_HTTP_TOKEN
*Token
733 HTTP_PROTOCOL
*HttpInstance
;
736 return EFI_INVALID_PARAMETER
;
739 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
740 ASSERT (HttpInstance
!= NULL
);
742 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
743 return EFI_NOT_STARTED
;
746 return HttpCancel (HttpInstance
, Token
);
751 A callback function to intercept events during message parser.
753 This function will be invoked during HttpParseMessageBody() with various events type. An error
754 return status of the callback function will cause the HttpParseMessageBody() aborted.
756 @param[in] EventType Event type of this callback call.
757 @param[in] Data A pointer to data buffer.
758 @param[in] Length Length in bytes of the Data.
759 @param[in] Context Callback context set by HttpInitMsgParser().
761 @retval EFI_SUCCESS Continue to parser the message body.
766 HttpBodyParserCallback (
767 IN HTTP_BODY_PARSE_EVENT EventType
,
773 HTTP_TOKEN_WRAP
*Wrap
;
775 if (EventType
!= BodyParseEventOnComplete
) {
779 if (Data
== NULL
|| Length
!= 0 || Context
== NULL
) {
783 Wrap
= (HTTP_TOKEN_WRAP
*) Context
;
784 Wrap
->HttpInstance
->NextMsg
= Data
;
787 // Free Tx4Token or Tx6Token since already received corrsponding HTTP response.
795 The work function of EfiHttpResponse().
797 @param[in] Wrap Pointer to HTTP token's wrap data.
799 @retval EFI_SUCCESS Allocation succeeded.
800 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
801 @retval EFI_NOT_READY Can't find a corresponding Tx4Token/Tx6Token or
802 the EFI_HTTP_UTILITIES_PROTOCOL is not available.
807 IN HTTP_TOKEN_WRAP
*Wrap
811 EFI_HTTP_MESSAGE
*HttpMsg
;
819 CHAR8
*StatusCodeStr
;
821 HTTP_PROTOCOL
*HttpInstance
;
822 EFI_HTTP_TOKEN
*Token
;
824 HTTP_TOKEN_WRAP
*ValueInItem
;
827 if (Wrap
== NULL
|| Wrap
->HttpInstance
== NULL
) {
828 return EFI_INVALID_PARAMETER
;
831 HttpInstance
= Wrap
->HttpInstance
;
832 Token
= Wrap
->HttpToken
;
833 HttpMsg
= Token
->Message
;
835 HttpInstance
->EndofHeader
= NULL
;
836 HttpInstance
->HttpHeaders
= NULL
;
837 HttpMsg
->Headers
= NULL
;
843 if (HttpMsg
->Data
.Response
!= NULL
) {
845 // Need receive the HTTP headers, prepare buffer.
847 Status
= HttpCreateTcpRxEventForHeader (HttpInstance
);
848 if (EFI_ERROR (Status
)) {
853 // Check whether we have cached header from previous call.
855 if ((HttpInstance
->CacheBody
!= NULL
) && (HttpInstance
->NextMsg
!= NULL
)) {
857 // The data is stored at [NextMsg, CacheBody + CacheLen].
859 HdrLen
= HttpInstance
->CacheBody
+ HttpInstance
->CacheLen
- HttpInstance
->NextMsg
;
860 HttpHeaders
= AllocateZeroPool (HdrLen
);
861 if (HttpHeaders
== NULL
) {
862 Status
= EFI_OUT_OF_RESOURCES
;
866 CopyMem (HttpHeaders
, HttpInstance
->NextMsg
, HdrLen
);
867 FreePool (HttpInstance
->CacheBody
);
868 HttpInstance
->CacheBody
= NULL
;
869 HttpInstance
->NextMsg
= NULL
;
870 HttpInstance
->CacheOffset
= 0;
871 SizeofHeaders
= HdrLen
;
872 BufferSize
= HttpInstance
->CacheLen
;
875 // Check whether we cached the whole HTTP headers.
877 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
880 HttpInstance
->EndofHeader
= &EndofHeader
;
881 HttpInstance
->HttpHeaders
= &HttpHeaders
;
883 Status
= HttpTcpReceiveHeader (HttpInstance
, &SizeofHeaders
, &BufferSize
);
884 if (EFI_ERROR (Status
)) {
889 // Cache the part of body.
891 BodyLen
= BufferSize
- (EndofHeader
- HttpHeaders
);
893 if (HttpInstance
->CacheBody
!= NULL
) {
894 FreePool (HttpInstance
->CacheBody
);
897 HttpInstance
->CacheBody
= AllocateZeroPool (BodyLen
);
898 if (HttpInstance
->CacheBody
== NULL
) {
899 Status
= EFI_OUT_OF_RESOURCES
;
903 CopyMem (HttpInstance
->CacheBody
, EndofHeader
, BodyLen
);
904 HttpInstance
->CacheLen
= BodyLen
;
908 // Search for Status Code.
910 StatusCodeStr
= HttpHeaders
+ AsciiStrLen (HTTP_VERSION_STR
) + 1;
911 if (StatusCodeStr
== NULL
) {
915 StatusCode
= AsciiStrDecimalToUintn (StatusCodeStr
);
918 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
920 Tmp
= AsciiStrStr (HttpHeaders
, HTTP_CRLF_STR
);
925 Tmp
= Tmp
+ AsciiStrLen (HTTP_CRLF_STR
);
926 SizeofHeaders
= SizeofHeaders
- (Tmp
- HttpHeaders
);
927 HeaderTmp
= AllocateZeroPool (SizeofHeaders
);
928 if (HeaderTmp
== NULL
) {
932 CopyMem (HeaderTmp
, Tmp
, SizeofHeaders
);
933 FreePool (HttpHeaders
);
934 HttpHeaders
= HeaderTmp
;
937 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.
939 if (mHttpUtilities
== NULL
) {
940 Status
= EFI_NOT_READY
;
945 // Parse the HTTP header into array of key/value pairs.
947 Status
= mHttpUtilities
->Parse (
952 &HttpMsg
->HeaderCount
954 if (EFI_ERROR (Status
)) {
958 FreePool (HttpHeaders
);
961 HttpMsg
->Data
.Response
->StatusCode
= HttpMappingToStatusCode (StatusCode
);
964 // Init message-body parser by header information.
966 Status
= EFI_NOT_READY
;
968 NetMapRemoveHead (&HttpInstance
->TxTokens
, (VOID
**) &ValueInItem
);
969 if (ValueInItem
== NULL
) {
974 // The first Tx Token not transmitted yet, insert back and return error.
976 if (!ValueInItem
->TcpWrap
.IsTxDone
) {
980 Status
= HttpInitMsgParser (
981 ValueInItem
->TcpWrap
.Method
,
982 HttpMsg
->Data
.Response
->StatusCode
,
983 HttpMsg
->HeaderCount
,
985 HttpBodyParserCallback
,
986 (VOID
*) ValueInItem
,
987 &HttpInstance
->MsgParser
989 if (EFI_ERROR (Status
)) {
994 // Check whether we received a complete HTTP message.
996 if (HttpInstance
->CacheBody
!= NULL
) {
997 Status
= HttpParseMessageBody (HttpInstance
->MsgParser
, HttpInstance
->CacheLen
, HttpInstance
->CacheBody
);
998 if (EFI_ERROR (Status
)) {
1002 if (HttpIsMessageComplete (HttpInstance
->MsgParser
)) {
1004 // Free the MsgParse since we already have a full HTTP message.
1006 HttpFreeMsgParser (HttpInstance
->MsgParser
);
1007 HttpInstance
->MsgParser
= NULL
;
1011 if ((HttpMsg
->Body
== NULL
) || (HttpMsg
->BodyLength
== 0)) {
1012 Status
= EFI_SUCCESS
;
1018 // Receive the response body.
1023 // First check whether we cached some data.
1025 if (HttpInstance
->CacheBody
!= NULL
) {
1027 // Calculate the length of the cached data.
1029 if (HttpInstance
->NextMsg
!= NULL
) {
1031 // We have a cached HTTP message which includes a part of HTTP header of next message.
1033 BodyLen
= HttpInstance
->NextMsg
- (HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
);
1035 BodyLen
= HttpInstance
->CacheLen
- HttpInstance
->CacheOffset
;
1040 // We have some cached data. Just copy the data and return.
1042 if (HttpMsg
->BodyLength
< BodyLen
) {
1043 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, HttpMsg
->BodyLength
);
1044 HttpInstance
->CacheOffset
= HttpInstance
->CacheOffset
+ HttpMsg
->BodyLength
;
1047 // Copy all cached data out.
1049 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, BodyLen
);
1050 HttpInstance
->CacheOffset
= BodyLen
+ HttpInstance
->CacheOffset
;
1051 HttpMsg
->BodyLength
= BodyLen
;
1053 if (HttpInstance
->NextMsg
== NULL
) {
1055 // There is no HTTP header of next message. Just free the cache buffer.
1057 FreePool (HttpInstance
->CacheBody
);
1058 HttpInstance
->CacheBody
= NULL
;
1059 HttpInstance
->NextMsg
= NULL
;
1060 HttpInstance
->CacheOffset
= 0;
1064 // Return since we aready received required data.
1066 Status
= EFI_SUCCESS
;
1070 if (BodyLen
== 0 && HttpInstance
->MsgParser
== NULL
) {
1072 // We received a complete HTTP message, and we don't have more data to return to caller.
1074 HttpMsg
->BodyLength
= 0;
1075 Status
= EFI_SUCCESS
;
1080 ASSERT (HttpInstance
->MsgParser
!= NULL
);
1083 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1085 Status
= HttpTcpReceiveBody (Wrap
, HttpMsg
);
1086 if (EFI_ERROR (Status
)) {
1093 Item
= NetMapFindKey (&Wrap
->HttpInstance
->RxTokens
, Wrap
->HttpToken
);
1095 NetMapRemoveItem (&Wrap
->HttpInstance
->RxTokens
, Item
, NULL
);
1097 Token
->Status
= Status
;
1098 gBS
->SignalEvent (Token
->Event
);
1099 HttpCloseTcpRxEvent (Wrap
);
1104 NetMapInsertHead (&HttpInstance
->TxTokens
, ValueInItem
->HttpToken
, ValueInItem
);
1107 HttpTcpTokenCleanup (Wrap
);
1109 if (HttpHeaders
!= NULL
) {
1110 FreePool (HttpHeaders
);
1113 if (HttpMsg
->Headers
!= NULL
) {
1114 FreePool (HttpMsg
->Headers
);
1117 if (HttpInstance
->CacheBody
!= NULL
) {
1118 FreePool (HttpInstance
->CacheBody
);
1119 HttpInstance
->CacheBody
= NULL
;
1122 Token
->Status
= Status
;
1123 gBS
->SignalEvent (Token
->Event
);
1131 The Response() function queues an HTTP response to this HTTP instance, similar to
1132 Receive() function in the EFI TCP driver. When the HTTP request is sent successfully,
1133 or if there is an error, Status in token will be updated and Event will be signaled.
1135 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1136 is received in the underlying TCP instance, the data will be parsed and Token will
1137 be populated with the response data. If the data received from the remote host
1138 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1139 (asynchronously) for more data to be sent from the remote host before signaling
1142 It is the responsibility of the caller to allocate a buffer for Body and specify the
1143 size in BodyLength. If the remote host provides a response that contains a content
1144 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1145 BodyLength will be updated with the amount of bytes received and copied to Body. This
1146 allows the client to download a large file in chunks instead of into one contiguous
1147 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1148 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1149 token to underlying TCP instance. If data arrives in the receive buffer, up to
1150 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1151 BodyLength with the amount of bytes received and copied to Body.
1153 If the HTTP driver does not have an open underlying TCP connection with the host
1154 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1155 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1156 an open TCP connection between client and host.
1158 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1159 @param[in] Token Pointer to storage containing HTTP response token.
1161 @retval EFI_SUCCESS Allocation succeeded.
1162 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1164 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1167 Token->Message->Headers is NULL.
1168 Token->Message is NULL.
1169 Token->Message->Body is not NULL,
1170 Token->Message->BodyLength is non-zero, and
1171 Token->Message->Data is NULL, but a previous call to
1172 Response() has not been completed successfully.
1173 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1174 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1175 specified by response URL.
1180 IN EFI_HTTP_PROTOCOL
*This
,
1181 IN EFI_HTTP_TOKEN
*Token
1185 EFI_HTTP_MESSAGE
*HttpMsg
;
1186 HTTP_PROTOCOL
*HttpInstance
;
1187 HTTP_TOKEN_WRAP
*Wrap
;
1189 if ((This
== NULL
) || (Token
== NULL
)) {
1190 return EFI_INVALID_PARAMETER
;
1193 HttpMsg
= Token
->Message
;
1194 if (HttpMsg
== NULL
) {
1195 return EFI_INVALID_PARAMETER
;
1198 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1199 ASSERT (HttpInstance
!= NULL
);
1201 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1202 return EFI_NOT_STARTED
;
1206 // Check whether the token already existed.
1208 if (EFI_ERROR (NetMapIterate (&HttpInstance
->RxTokens
, HttpTokenExist
, Token
))) {
1209 return EFI_ACCESS_DENIED
;
1212 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
1214 return EFI_OUT_OF_RESOURCES
;
1217 Wrap
->HttpInstance
= HttpInstance
;
1218 Wrap
->HttpToken
= Token
;
1220 Status
= HttpCreateTcpRxEvent (Wrap
);
1221 if (EFI_ERROR (Status
)) {
1225 Status
= NetMapInsertTail (&HttpInstance
->RxTokens
, Token
, Wrap
);
1226 if (EFI_ERROR (Status
)) {
1231 // If already have pending RxTokens, return directly.
1233 if (NetMapGetCount (&HttpInstance
->RxTokens
) > 1) {
1237 return HttpResponseWorker (Wrap
);
1241 if (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
!= NULL
) {
1242 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
);
1245 if (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
!= NULL
) {
1246 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
);
1255 The Poll() function can be used by network drivers and applications to increase the
1256 rate that data packets are moved between the communication devices and the transmit
1259 In some systems, the periodic timer event in the managed network driver may not poll
1260 the underlying communications device fast enough to transmit and/or receive all data
1261 packets without missing incoming packets or dropping outgoing packets. Drivers and
1262 applications that are experiencing packet loss should try calling the Poll() function
1265 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1267 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1268 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1269 @retval EFI_INVALID_PARAMETER This is NULL.
1270 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1271 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1277 IN EFI_HTTP_PROTOCOL
*This
1281 HTTP_PROTOCOL
*HttpInstance
;
1284 return EFI_INVALID_PARAMETER
;
1287 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1288 ASSERT (HttpInstance
!= NULL
);
1290 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
|| (HttpInstance
->Tcp4
== NULL
&&
1291 HttpInstance
->Tcp6
== NULL
)) {
1292 return EFI_NOT_STARTED
;
1295 if (HttpInstance
->LocalAddressIsIPv6
) {
1296 Status
= HttpInstance
->Tcp6
->Poll (HttpInstance
->Tcp6
);
1298 Status
= HttpInstance
->Tcp4
->Poll (HttpInstance
->Tcp4
);