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
)
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
;
240 HTTP_PROTOCOL
*HttpInstance
;
247 HTTP_TOKEN_WRAP
*Wrap
;
250 if ((This
== NULL
) || (Token
== NULL
)) {
251 return EFI_INVALID_PARAMETER
;
254 HttpMsg
= Token
->Message
;
255 if ((HttpMsg
== NULL
) || (HttpMsg
->Headers
== NULL
)) {
256 return EFI_INVALID_PARAMETER
;
260 // Current implementation does not support POST/PUT method.
261 // If future version supports these two methods, Request could be NULL for a special case that to send large amounts
262 // of data. For this case, the implementation need check whether previous call to Request() has been completed or not.
265 Request
= HttpMsg
->Data
.Request
;
266 if ((Request
== NULL
) || (Request
->Url
== NULL
)) {
267 return EFI_INVALID_PARAMETER
;
271 // Only support GET and HEAD method in current implementation.
273 if ((Request
->Method
!= HttpMethodGet
) && (Request
->Method
!= HttpMethodHead
)) {
274 return EFI_UNSUPPORTED
;
277 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
278 ASSERT (HttpInstance
!= NULL
);
280 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
281 return EFI_NOT_STARTED
;
285 // Check whether the token already existed.
287 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTokenExist
, Token
))) {
288 return EFI_ACCESS_DENIED
;
296 // Parse the URI of the remote host.
298 Url
= HttpInstance
->Url
;
299 UrlLen
= StrLen (Request
->Url
) + 1;
300 if (UrlLen
> HTTP_URL_BUFFER_LEN
) {
301 Url
= AllocateZeroPool (UrlLen
);
303 return EFI_OUT_OF_RESOURCES
;
305 FreePool (HttpInstance
->Url
);
306 HttpInstance
->Url
= Url
;
310 UnicodeStrToAsciiStr (Request
->Url
, Url
);
312 Status
= HttpParseUrl (Url
, (UINT32
) AsciiStrLen (Url
), FALSE
, &UrlParser
);
313 if (EFI_ERROR (Status
)) {
319 Status
= HttpUrlGetHostName (Url
, UrlParser
, &HostName
);
320 if (EFI_ERROR (Status
)) {
324 Status
= HttpUrlGetPort (Url
, UrlParser
, &RemotePort
);
325 if (EFI_ERROR (Status
)) {
326 RemotePort
= HTTP_DEFAULT_PORT
;
329 // If Configure is TRUE, it indicates the first time to call Request();
330 // If ReConfigure is TRUE, it indicates the request URL is not same
331 // with the previous call to Request();
336 if (HttpInstance
->RemoteHost
== NULL
) {
338 // Request() is called the first time.
342 if ((HttpInstance
->RemotePort
== RemotePort
) &&
343 (AsciiStrCmp (HttpInstance
->RemoteHost
, HostName
) == 0)) {
345 // Host Name and port number of the request URL are the same with previous call to Request().
346 // Check whether previous TCP packet sent out.
348 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTcpNotReady
, NULL
))) {
350 // Wrap the HTTP token in HTTP_TOKEN_WRAP
352 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
354 Status
= EFI_OUT_OF_RESOURCES
;
358 Wrap
->HttpToken
= Token
;
359 Wrap
->HttpInstance
= HttpInstance
;
361 Status
= HttpCreateTcpTxEvent (Wrap
);
362 if (EFI_ERROR (Status
)) {
366 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
367 if (EFI_ERROR (Status
)) {
371 Wrap
->TcpWrap
.Method
= Request
->Method
;
376 // Queue the HTTP token and return.
381 // Use existing TCP instance to transmit the packet.
388 // Need close existing TCP instance and create a new TCP instance for data transmit.
390 if (HttpInstance
->RemoteHost
!= NULL
) {
391 FreePool (HttpInstance
->RemoteHost
);
392 HttpInstance
->RemoteHost
= NULL
;
393 HttpInstance
->RemotePort
= 0;
400 // Parse Url for IPv4 or IPv6 address, if failed, perform DNS resolution.
402 if (!HttpInstance
->LocalAddressIsIPv6
) {
403 Status
= NetLibAsciiStrToIp4 (HostName
, &HttpInstance
->RemoteAddr
);
405 Status
= HttpUrlGetIp6 (Url
, UrlParser
, &HttpInstance
->RemoteIpv6Addr
);
408 if (EFI_ERROR (Status
)) {
409 HostNameStr
= AllocateZeroPool ((AsciiStrLen (HostName
) + 1) * sizeof (CHAR16
));
410 if (HostNameStr
== NULL
) {
411 Status
= EFI_OUT_OF_RESOURCES
;
415 AsciiStrToUnicodeStr (HostName
, HostNameStr
);
416 if (!HttpInstance
->LocalAddressIsIPv6
) {
417 Status
= HttpDns4 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteAddr
);
419 Status
= HttpDns6 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteIpv6Addr
);
422 FreePool (HostNameStr
);
423 if (EFI_ERROR (Status
)) {
430 // Save the RemotePort and RemoteHost.
432 ASSERT (HttpInstance
->RemoteHost
== NULL
);
433 HttpInstance
->RemotePort
= RemotePort
;
434 HttpInstance
->RemoteHost
= HostName
;
440 // The request URL is different from previous calls to Request(), close existing TCP instance.
442 if (!HttpInstance
->LocalAddressIsIPv6
) {
443 ASSERT (HttpInstance
->Tcp4
!= NULL
);
445 ASSERT (HttpInstance
->Tcp6
!= NULL
);
447 HttpCloseConnection (HttpInstance
);
448 EfiHttpCancel (This
, NULL
);
452 // Wrap the HTTP token in HTTP_TOKEN_WRAP
454 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
456 Status
= EFI_OUT_OF_RESOURCES
;
460 Wrap
->HttpToken
= Token
;
461 Wrap
->HttpInstance
= HttpInstance
;
462 Wrap
->TcpWrap
.Method
= Request
->Method
;
464 Status
= HttpInitTcp (HttpInstance
, Wrap
, Configure
);
465 if (EFI_ERROR (Status
)) {
471 // For the new HTTP token, create TX TCP token events.
473 Status
= HttpCreateTcpTxEvent (Wrap
);
474 if (EFI_ERROR (Status
)) {
480 // Create request message.
483 if (*FileUrl
!= '/') {
485 // Convert the absolute-URI to the absolute-path
487 while (*FileUrl
!= ':') {
490 if ((*(FileUrl
+1) == '/') && (*(FileUrl
+2) == '/')) {
492 while (*FileUrl
!= '/') {
496 Status
= EFI_INVALID_PARAMETER
;
501 Status
= HttpGenRequestString (HttpMsg
, FileUrl
, &RequestStr
);
503 if (EFI_ERROR (Status
)) {
507 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
508 if (EFI_ERROR (Status
)) {
513 // Transmit the request message.
515 Status
= HttpTransmitTcp (
519 AsciiStrLen (RequestStr
)
521 if (EFI_ERROR (Status
)) {
527 if (HostName
!= NULL
) {
534 NetMapRemoveTail (&HttpInstance
->TxTokens
, NULL
);
537 if (RequestStr
!= NULL
) {
538 FreePool (RequestStr
);
542 HttpCloseConnection (HttpInstance
);
545 HttpCloseTcpConnCloseEvent (HttpInstance
);
546 if (NULL
!= Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
) {
547 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
);
548 Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
= NULL
;
550 if (NULL
!= Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
) {
551 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
);
552 Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
= NULL
;
557 if (HostName
!= NULL
) {
563 if (UrlParser
!= NULL
) {
564 HttpUrlFreeParser (UrlParser
);
572 Cancel a user's Token.
574 @param[in] Map The HTTP instance's token queue.
575 @param[in] Item Object container for one HTTP token and token's wrap.
576 @param[in] Context The user's token to cancel.
578 @retval EFI_SUCCESS Continue to check the next Item.
579 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
586 IN NET_MAP_ITEM
*Item
,
591 EFI_HTTP_TOKEN
*Token
;
592 HTTP_TOKEN_WRAP
*Wrap
;
593 HTTP_PROTOCOL
*HttpInstance
;
595 Token
= (EFI_HTTP_TOKEN
*) Context
;
598 // Return EFI_SUCCESS to check the next item in the map if
599 // this one doesn't match.
601 if ((Token
!= NULL
) && (Token
!= Item
->Key
)) {
605 Wrap
= (HTTP_TOKEN_WRAP
*) Item
->Value
;
606 ASSERT (Wrap
!= NULL
);
607 HttpInstance
= Wrap
->HttpInstance
;
612 NetMapRemoveItem (Map
, Item
, NULL
);
614 if (!HttpInstance
->LocalAddressIsIPv6
) {
615 if (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
!= NULL
) {
616 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
);
619 if (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
!= NULL
) {
620 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
);
623 if (Wrap
->TcpWrap
.Rx4Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
624 FreePool (Wrap
->TcpWrap
.Rx4Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
628 if (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
!= NULL
) {
629 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
);
632 if (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
!= NULL
) {
633 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
);
636 if (Wrap
->TcpWrap
.Rx6Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
637 FreePool (Wrap
->TcpWrap
.Rx6Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
645 // If only one item is to be cancel, return EFI_ABORTED to stop
646 // iterating the map any more.
656 Cancel the user's receive/transmit request. It is the worker function of
657 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the
660 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
661 @param[in] Token The token to cancel. If NULL, all token will be
664 @retval EFI_SUCCESS The token is cancelled.
665 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.
666 @retval Others Other error as indicated.
671 IN HTTP_PROTOCOL
*HttpInstance
,
672 IN EFI_HTTP_TOKEN
*Token
678 // First check the tokens queued by EfiHttpRequest().
680 Status
= NetMapIterate (&HttpInstance
->TxTokens
, HttpCancelTokens
, Token
);
681 if (EFI_ERROR (Status
)) {
683 if (Status
== EFI_ABORTED
) {
692 // Then check the tokens queued by EfiHttpResponse().
694 Status
= NetMapIterate (&HttpInstance
->RxTokens
, HttpCancelTokens
, Token
);
695 if (EFI_ERROR (Status
)) {
697 if (Status
== EFI_ABORTED
) {
700 return EFI_NOT_FOUND
;
712 Abort an asynchronous HTTP request or response token.
714 The Cancel() function aborts a pending HTTP request or response transaction. If
715 Token is not NULL and the token is in transmit or receive queues when it is being
716 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
717 be signaled. If the token is not in one of the queues, which usually means that the
718 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
719 all asynchronous tokens issued by Request() or Response() will be aborted.
721 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
722 @param[in] Token Point to storage containing HTTP request or response
725 @retval EFI_SUCCESS Request and Response queues are successfully flushed.
726 @retval EFI_INVALID_PARAMETER This is NULL.
727 @retval EFI_NOT_STARTED This instance hasn't been configured.
728 @retval EFI_NOT_FOUND The asynchronous request or response token is not
730 @retval EFI_UNSUPPORTED The implementation does not support this function.
736 IN EFI_HTTP_PROTOCOL
*This
,
737 IN EFI_HTTP_TOKEN
*Token
740 HTTP_PROTOCOL
*HttpInstance
;
743 return EFI_INVALID_PARAMETER
;
746 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
747 ASSERT (HttpInstance
!= NULL
);
749 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
750 return EFI_NOT_STARTED
;
753 return HttpCancel (HttpInstance
, Token
);
758 A callback function to intercept events during message parser.
760 This function will be invoked during HttpParseMessageBody() with various events type. An error
761 return status of the callback function will cause the HttpParseMessageBody() aborted.
763 @param[in] EventType Event type of this callback call.
764 @param[in] Data A pointer to data buffer.
765 @param[in] Length Length in bytes of the Data.
766 @param[in] Context Callback context set by HttpInitMsgParser().
768 @retval EFI_SUCCESS Continue to parser the message body.
773 HttpBodyParserCallback (
774 IN HTTP_BODY_PARSE_EVENT EventType
,
780 HTTP_TOKEN_WRAP
*Wrap
;
784 if (EventType
!= BodyParseEventOnComplete
) {
788 if (Data
== NULL
|| Length
!= 0 || Context
== NULL
) {
792 Wrap
= (HTTP_TOKEN_WRAP
*) Context
;
793 Body
= Wrap
->HttpToken
->Message
->Body
;
794 BodyLength
= Wrap
->HttpToken
->Message
->BodyLength
;
795 if (Data
< Body
+ BodyLength
) {
796 Wrap
->HttpInstance
->NextMsg
= Data
;
798 Wrap
->HttpInstance
->NextMsg
= NULL
;
803 // Free Tx4Token or Tx6Token since already received corrsponding HTTP response.
811 The work function of EfiHttpResponse().
813 @param[in] Wrap Pointer to HTTP token's wrap data.
815 @retval EFI_SUCCESS Allocation succeeded.
816 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
817 @retval EFI_NOT_READY Can't find a corresponding Tx4Token/Tx6Token or
818 the EFI_HTTP_UTILITIES_PROTOCOL is not available.
823 IN HTTP_TOKEN_WRAP
*Wrap
827 EFI_HTTP_MESSAGE
*HttpMsg
;
835 CHAR8
*StatusCodeStr
;
837 HTTP_PROTOCOL
*HttpInstance
;
838 EFI_HTTP_TOKEN
*Token
;
840 HTTP_TOKEN_WRAP
*ValueInItem
;
843 if (Wrap
== NULL
|| Wrap
->HttpInstance
== NULL
) {
844 return EFI_INVALID_PARAMETER
;
847 HttpInstance
= Wrap
->HttpInstance
;
848 Token
= Wrap
->HttpToken
;
849 HttpMsg
= Token
->Message
;
851 HttpInstance
->EndofHeader
= NULL
;
852 HttpInstance
->HttpHeaders
= NULL
;
853 HttpMsg
->Headers
= NULL
;
859 if (HttpMsg
->Data
.Response
!= NULL
) {
861 // Need receive the HTTP headers, prepare buffer.
863 Status
= HttpCreateTcpRxEventForHeader (HttpInstance
);
864 if (EFI_ERROR (Status
)) {
869 // Check whether we have cached header from previous call.
871 if ((HttpInstance
->CacheBody
!= NULL
) && (HttpInstance
->NextMsg
!= NULL
)) {
873 // The data is stored at [NextMsg, CacheBody + CacheLen].
875 HdrLen
= HttpInstance
->CacheBody
+ HttpInstance
->CacheLen
- HttpInstance
->NextMsg
;
876 HttpHeaders
= AllocateZeroPool (HdrLen
);
877 if (HttpHeaders
== NULL
) {
878 Status
= EFI_OUT_OF_RESOURCES
;
882 CopyMem (HttpHeaders
, HttpInstance
->NextMsg
, HdrLen
);
883 FreePool (HttpInstance
->CacheBody
);
884 HttpInstance
->CacheBody
= NULL
;
885 HttpInstance
->NextMsg
= NULL
;
886 HttpInstance
->CacheOffset
= 0;
887 SizeofHeaders
= HdrLen
;
888 BufferSize
= HttpInstance
->CacheLen
;
891 // Check whether we cached the whole HTTP headers.
893 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
896 HttpInstance
->EndofHeader
= &EndofHeader
;
897 HttpInstance
->HttpHeaders
= &HttpHeaders
;
899 Status
= HttpTcpReceiveHeader (HttpInstance
, &SizeofHeaders
, &BufferSize
);
900 if (EFI_ERROR (Status
)) {
904 ASSERT (HttpHeaders
!= NULL
);
907 // Cache the part of body.
909 BodyLen
= BufferSize
- (EndofHeader
- HttpHeaders
);
911 if (HttpInstance
->CacheBody
!= NULL
) {
912 FreePool (HttpInstance
->CacheBody
);
915 HttpInstance
->CacheBody
= AllocateZeroPool (BodyLen
);
916 if (HttpInstance
->CacheBody
== NULL
) {
917 Status
= EFI_OUT_OF_RESOURCES
;
921 CopyMem (HttpInstance
->CacheBody
, EndofHeader
, BodyLen
);
922 HttpInstance
->CacheLen
= BodyLen
;
926 // Search for Status Code.
928 StatusCodeStr
= HttpHeaders
+ AsciiStrLen (HTTP_VERSION_STR
) + 1;
929 if (StatusCodeStr
== NULL
) {
933 StatusCode
= AsciiStrDecimalToUintn (StatusCodeStr
);
936 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
938 Tmp
= AsciiStrStr (HttpHeaders
, HTTP_CRLF_STR
);
943 Tmp
= Tmp
+ AsciiStrLen (HTTP_CRLF_STR
);
944 SizeofHeaders
= SizeofHeaders
- (Tmp
- HttpHeaders
);
945 HeaderTmp
= AllocateZeroPool (SizeofHeaders
);
946 if (HeaderTmp
== NULL
) {
950 CopyMem (HeaderTmp
, Tmp
, SizeofHeaders
);
951 FreePool (HttpHeaders
);
952 HttpHeaders
= HeaderTmp
;
955 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.
957 if (mHttpUtilities
== NULL
) {
958 Status
= EFI_NOT_READY
;
963 // Parse the HTTP header into array of key/value pairs.
965 Status
= mHttpUtilities
->Parse (
970 &HttpMsg
->HeaderCount
972 if (EFI_ERROR (Status
)) {
976 FreePool (HttpHeaders
);
979 HttpMsg
->Data
.Response
->StatusCode
= HttpMappingToStatusCode (StatusCode
);
980 HttpInstance
->StatusCode
= StatusCode
;
982 // Init message-body parser by header information.
984 Status
= EFI_NOT_READY
;
986 NetMapRemoveHead (&HttpInstance
->TxTokens
, (VOID
**) &ValueInItem
);
987 if (ValueInItem
== NULL
) {
992 // The first Tx Token not transmitted yet, insert back and return error.
994 if (!ValueInItem
->TcpWrap
.IsTxDone
) {
998 Status
= HttpInitMsgParser (
999 ValueInItem
->TcpWrap
.Method
,
1000 HttpMsg
->Data
.Response
->StatusCode
,
1001 HttpMsg
->HeaderCount
,
1003 HttpBodyParserCallback
,
1004 (VOID
*) ValueInItem
,
1005 &HttpInstance
->MsgParser
1007 if (EFI_ERROR (Status
)) {
1012 // Check whether we received a complete HTTP message.
1014 if (HttpInstance
->CacheBody
!= NULL
) {
1015 Status
= HttpParseMessageBody (HttpInstance
->MsgParser
, HttpInstance
->CacheLen
, HttpInstance
->CacheBody
);
1016 if (EFI_ERROR (Status
)) {
1020 if (HttpIsMessageComplete (HttpInstance
->MsgParser
)) {
1022 // Free the MsgParse since we already have a full HTTP message.
1024 HttpFreeMsgParser (HttpInstance
->MsgParser
);
1025 HttpInstance
->MsgParser
= NULL
;
1029 if ((HttpMsg
->Body
== NULL
) || (HttpMsg
->BodyLength
== 0)) {
1030 Status
= EFI_SUCCESS
;
1036 // Receive the response body.
1041 // First check whether we cached some data.
1043 if (HttpInstance
->CacheBody
!= NULL
) {
1045 // Calculate the length of the cached data.
1047 if (HttpInstance
->NextMsg
!= NULL
) {
1049 // We have a cached HTTP message which includes a part of HTTP header of next message.
1051 BodyLen
= HttpInstance
->NextMsg
- (HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
);
1053 BodyLen
= HttpInstance
->CacheLen
- HttpInstance
->CacheOffset
;
1058 // We have some cached data. Just copy the data and return.
1060 if (HttpMsg
->BodyLength
< BodyLen
) {
1061 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, HttpMsg
->BodyLength
);
1062 HttpInstance
->CacheOffset
= HttpInstance
->CacheOffset
+ HttpMsg
->BodyLength
;
1065 // Copy all cached data out.
1067 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, BodyLen
);
1068 HttpInstance
->CacheOffset
= BodyLen
+ HttpInstance
->CacheOffset
;
1069 HttpMsg
->BodyLength
= BodyLen
;
1071 if (HttpInstance
->NextMsg
== NULL
) {
1073 // There is no HTTP header of next message. Just free the cache buffer.
1075 FreePool (HttpInstance
->CacheBody
);
1076 HttpInstance
->CacheBody
= NULL
;
1077 HttpInstance
->NextMsg
= NULL
;
1078 HttpInstance
->CacheOffset
= 0;
1082 // Return since we aready received required data.
1084 Status
= EFI_SUCCESS
;
1088 if (BodyLen
== 0 && HttpInstance
->MsgParser
== NULL
) {
1090 // We received a complete HTTP message, and we don't have more data to return to caller.
1092 HttpMsg
->BodyLength
= 0;
1093 Status
= EFI_SUCCESS
;
1098 ASSERT (HttpInstance
->MsgParser
!= NULL
);
1101 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1103 Status
= HttpTcpReceiveBody (Wrap
, HttpMsg
);
1104 if (EFI_ERROR (Status
)) {
1111 Item
= NetMapFindKey (&Wrap
->HttpInstance
->RxTokens
, Wrap
->HttpToken
);
1113 NetMapRemoveItem (&Wrap
->HttpInstance
->RxTokens
, Item
, NULL
);
1116 if (HttpInstance
->StatusCode
>= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE
) {
1117 Token
->Status
= EFI_HTTP_ERROR
;
1119 Token
->Status
= Status
;
1122 gBS
->SignalEvent (Token
->Event
);
1123 HttpCloseTcpRxEvent (Wrap
);
1128 NetMapInsertHead (&HttpInstance
->TxTokens
, ValueInItem
->HttpToken
, ValueInItem
);
1131 HttpTcpTokenCleanup (Wrap
);
1133 if (HttpHeaders
!= NULL
) {
1134 FreePool (HttpHeaders
);
1137 if (HttpMsg
->Headers
!= NULL
) {
1138 FreePool (HttpMsg
->Headers
);
1141 if (HttpInstance
->CacheBody
!= NULL
) {
1142 FreePool (HttpInstance
->CacheBody
);
1143 HttpInstance
->CacheBody
= NULL
;
1146 if (HttpInstance
->StatusCode
>= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE
) {
1147 Token
->Status
= EFI_HTTP_ERROR
;
1149 Token
->Status
= Status
;
1152 gBS
->SignalEvent (Token
->Event
);
1160 The Response() function queues an HTTP response to this HTTP instance, similar to
1161 Receive() function in the EFI TCP driver. When the HTTP response is received successfully,
1162 or if there is an error, Status in token will be updated and Event will be signaled.
1164 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1165 is received in the underlying TCP instance, the data will be parsed and Token will
1166 be populated with the response data. If the data received from the remote host
1167 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1168 (asynchronously) for more data to be sent from the remote host before signaling
1171 It is the responsibility of the caller to allocate a buffer for Body and specify the
1172 size in BodyLength. If the remote host provides a response that contains a content
1173 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1174 BodyLength will be updated with the amount of bytes received and copied to Body. This
1175 allows the client to download a large file in chunks instead of into one contiguous
1176 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1177 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1178 token to underlying TCP instance. If data arrives in the receive buffer, up to
1179 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1180 BodyLength with the amount of bytes received and copied to Body.
1182 If the HTTP driver does not have an open underlying TCP connection with the host
1183 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1184 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1185 an open TCP connection between client and host.
1187 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1188 @param[in] Token Pointer to storage containing HTTP response token.
1190 @retval EFI_SUCCESS Allocation succeeded.
1191 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1193 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1196 Token->Message->Headers is NULL.
1197 Token->Message is NULL.
1198 Token->Message->Body is not NULL,
1199 Token->Message->BodyLength is non-zero, and
1200 Token->Message->Data is NULL, but a previous call to
1201 Response() has not been completed successfully.
1202 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1203 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1204 specified by response URL.
1209 IN EFI_HTTP_PROTOCOL
*This
,
1210 IN EFI_HTTP_TOKEN
*Token
1214 EFI_HTTP_MESSAGE
*HttpMsg
;
1215 HTTP_PROTOCOL
*HttpInstance
;
1216 HTTP_TOKEN_WRAP
*Wrap
;
1218 if ((This
== NULL
) || (Token
== NULL
)) {
1219 return EFI_INVALID_PARAMETER
;
1222 HttpMsg
= Token
->Message
;
1223 if (HttpMsg
== NULL
) {
1224 return EFI_INVALID_PARAMETER
;
1227 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1228 ASSERT (HttpInstance
!= NULL
);
1230 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1231 return EFI_NOT_STARTED
;
1235 // Check whether the token already existed.
1237 if (EFI_ERROR (NetMapIterate (&HttpInstance
->RxTokens
, HttpTokenExist
, Token
))) {
1238 return EFI_ACCESS_DENIED
;
1241 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
1243 return EFI_OUT_OF_RESOURCES
;
1246 Wrap
->HttpInstance
= HttpInstance
;
1247 Wrap
->HttpToken
= Token
;
1249 Status
= HttpCreateTcpRxEvent (Wrap
);
1250 if (EFI_ERROR (Status
)) {
1254 Status
= NetMapInsertTail (&HttpInstance
->RxTokens
, Token
, Wrap
);
1255 if (EFI_ERROR (Status
)) {
1260 // If already have pending RxTokens, return directly.
1262 if (NetMapGetCount (&HttpInstance
->RxTokens
) > 1) {
1266 return HttpResponseWorker (Wrap
);
1270 if (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
!= NULL
) {
1271 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
);
1274 if (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
!= NULL
) {
1275 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
);
1284 The Poll() function can be used by network drivers and applications to increase the
1285 rate that data packets are moved between the communication devices and the transmit
1288 In some systems, the periodic timer event in the managed network driver may not poll
1289 the underlying communications device fast enough to transmit and/or receive all data
1290 packets without missing incoming packets or dropping outgoing packets. Drivers and
1291 applications that are experiencing packet loss should try calling the Poll() function
1294 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1296 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1297 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1298 @retval EFI_INVALID_PARAMETER This is NULL.
1299 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1300 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1306 IN EFI_HTTP_PROTOCOL
*This
1310 HTTP_PROTOCOL
*HttpInstance
;
1313 return EFI_INVALID_PARAMETER
;
1316 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1317 ASSERT (HttpInstance
!= NULL
);
1319 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1320 return EFI_NOT_STARTED
;
1323 if (HttpInstance
->LocalAddressIsIPv6
) {
1324 if (HttpInstance
->Tcp6
== NULL
) {
1325 return EFI_NOT_STARTED
;
1327 Status
= HttpInstance
->Tcp6
->Poll (HttpInstance
->Tcp6
);
1329 if (HttpInstance
->Tcp4
== NULL
) {
1330 return EFI_NOT_STARTED
;
1332 Status
= HttpInstance
->Tcp4
->Poll (HttpInstance
->Tcp4
);