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
;
249 UINTN RequestMsgSize
;
251 if ((This
== NULL
) || (Token
== NULL
)) {
252 return EFI_INVALID_PARAMETER
;
255 HttpMsg
= Token
->Message
;
256 if ((HttpMsg
== NULL
) || (HttpMsg
->Headers
== NULL
)) {
257 return EFI_INVALID_PARAMETER
;
261 // Current implementation does not support POST/PUT method.
262 // If future version supports these two methods, Request could be NULL for a special case that to send large amounts
263 // of data. For this case, the implementation need check whether previous call to Request() has been completed or not.
266 Request
= HttpMsg
->Data
.Request
;
267 if ((Request
== NULL
) || (Request
->Url
== NULL
)) {
268 return EFI_INVALID_PARAMETER
;
272 // Only support GET and HEAD method in current implementation.
274 if ((Request
->Method
!= HttpMethodGet
) && (Request
->Method
!= HttpMethodHead
)) {
275 return EFI_UNSUPPORTED
;
278 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
279 ASSERT (HttpInstance
!= NULL
);
281 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
282 return EFI_NOT_STARTED
;
286 // Check whether the token already existed.
288 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTokenExist
, Token
))) {
289 return EFI_ACCESS_DENIED
;
297 // Parse the URI of the remote host.
299 Url
= HttpInstance
->Url
;
300 UrlLen
= StrLen (Request
->Url
) + 1;
301 if (UrlLen
> HTTP_URL_BUFFER_LEN
) {
302 Url
= AllocateZeroPool (UrlLen
);
304 return EFI_OUT_OF_RESOURCES
;
306 FreePool (HttpInstance
->Url
);
307 HttpInstance
->Url
= Url
;
311 UnicodeStrToAsciiStr (Request
->Url
, Url
);
313 Status
= HttpParseUrl (Url
, (UINT32
) AsciiStrLen (Url
), FALSE
, &UrlParser
);
314 if (EFI_ERROR (Status
)) {
320 Status
= HttpUrlGetHostName (Url
, UrlParser
, &HostName
);
321 if (EFI_ERROR (Status
)) {
325 Status
= HttpUrlGetPort (Url
, UrlParser
, &RemotePort
);
326 if (EFI_ERROR (Status
)) {
327 RemotePort
= HTTP_DEFAULT_PORT
;
330 // If Configure is TRUE, it indicates the first time to call Request();
331 // If ReConfigure is TRUE, it indicates the request URL is not same
332 // with the previous call to Request();
337 if (HttpInstance
->RemoteHost
== NULL
) {
339 // Request() is called the first time.
343 if ((HttpInstance
->RemotePort
== RemotePort
) &&
344 (AsciiStrCmp (HttpInstance
->RemoteHost
, HostName
) == 0)) {
346 // Host Name and port number of the request URL are the same with previous call to Request().
347 // Check whether previous TCP packet sent out.
349 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTcpNotReady
, NULL
))) {
351 // Wrap the HTTP token in HTTP_TOKEN_WRAP
353 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
355 Status
= EFI_OUT_OF_RESOURCES
;
359 Wrap
->HttpToken
= Token
;
360 Wrap
->HttpInstance
= HttpInstance
;
362 Status
= HttpCreateTcpTxEvent (Wrap
);
363 if (EFI_ERROR (Status
)) {
367 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
368 if (EFI_ERROR (Status
)) {
372 Wrap
->TcpWrap
.Method
= Request
->Method
;
377 // Queue the HTTP token and return.
382 // Use existing TCP instance to transmit the packet.
389 // Need close existing TCP instance and create a new TCP instance for data transmit.
391 if (HttpInstance
->RemoteHost
!= NULL
) {
392 FreePool (HttpInstance
->RemoteHost
);
393 HttpInstance
->RemoteHost
= NULL
;
394 HttpInstance
->RemotePort
= 0;
401 // Parse Url for IPv4 or IPv6 address, if failed, perform DNS resolution.
403 if (!HttpInstance
->LocalAddressIsIPv6
) {
404 Status
= NetLibAsciiStrToIp4 (HostName
, &HttpInstance
->RemoteAddr
);
406 Status
= HttpUrlGetIp6 (Url
, UrlParser
, &HttpInstance
->RemoteIpv6Addr
);
409 if (EFI_ERROR (Status
)) {
410 HostNameStr
= AllocateZeroPool ((AsciiStrLen (HostName
) + 1) * sizeof (CHAR16
));
411 if (HostNameStr
== NULL
) {
412 Status
= EFI_OUT_OF_RESOURCES
;
416 AsciiStrToUnicodeStr (HostName
, HostNameStr
);
417 if (!HttpInstance
->LocalAddressIsIPv6
) {
418 Status
= HttpDns4 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteAddr
);
420 Status
= HttpDns6 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteIpv6Addr
);
423 FreePool (HostNameStr
);
424 if (EFI_ERROR (Status
)) {
431 // Save the RemotePort and RemoteHost.
433 ASSERT (HttpInstance
->RemoteHost
== NULL
);
434 HttpInstance
->RemotePort
= RemotePort
;
435 HttpInstance
->RemoteHost
= HostName
;
441 // The request URL is different from previous calls to Request(), close existing TCP instance.
443 if (!HttpInstance
->LocalAddressIsIPv6
) {
444 ASSERT (HttpInstance
->Tcp4
!= NULL
);
446 ASSERT (HttpInstance
->Tcp6
!= NULL
);
448 HttpCloseConnection (HttpInstance
);
449 EfiHttpCancel (This
, NULL
);
453 // Wrap the HTTP token in HTTP_TOKEN_WRAP
455 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
457 Status
= EFI_OUT_OF_RESOURCES
;
461 Wrap
->HttpToken
= Token
;
462 Wrap
->HttpInstance
= HttpInstance
;
463 Wrap
->TcpWrap
.Method
= Request
->Method
;
465 Status
= HttpInitTcp (HttpInstance
, Wrap
, Configure
);
466 if (EFI_ERROR (Status
)) {
472 // For the new HTTP token, create TX TCP token events.
474 Status
= HttpCreateTcpTxEvent (Wrap
);
475 if (EFI_ERROR (Status
)) {
481 // Create request message.
484 if (*FileUrl
!= '/') {
486 // Convert the absolute-URI to the absolute-path
488 while (*FileUrl
!= ':') {
491 if ((*(FileUrl
+1) == '/') && (*(FileUrl
+2) == '/')) {
493 while (*FileUrl
!= '/') {
497 Status
= EFI_INVALID_PARAMETER
;
502 Status
= HttpGenRequestMessage (HttpMsg
, FileUrl
, &RequestMsg
, &RequestMsgSize
);
504 if (EFI_ERROR (Status
)) {
508 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
509 if (EFI_ERROR (Status
)) {
514 // Transmit the request message.
516 Status
= HttpTransmitTcp (
522 if (EFI_ERROR (Status
)) {
528 if (HostName
!= NULL
) {
535 NetMapRemoveTail (&HttpInstance
->TxTokens
, NULL
);
538 if (RequestMsg
!= NULL
) {
539 FreePool (RequestMsg
);
543 HttpCloseConnection (HttpInstance
);
546 HttpCloseTcpConnCloseEvent (HttpInstance
);
547 if (NULL
!= Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
) {
548 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
);
549 Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
= NULL
;
551 if (NULL
!= Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
) {
552 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
);
553 Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
= NULL
;
558 if (HostName
!= NULL
) {
564 if (UrlParser
!= NULL
) {
565 HttpUrlFreeParser (UrlParser
);
573 Cancel a user's Token.
575 @param[in] Map The HTTP instance's token queue.
576 @param[in] Item Object container for one HTTP token and token's wrap.
577 @param[in] Context The user's token to cancel.
579 @retval EFI_SUCCESS Continue to check the next Item.
580 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
587 IN NET_MAP_ITEM
*Item
,
592 EFI_HTTP_TOKEN
*Token
;
593 HTTP_TOKEN_WRAP
*Wrap
;
594 HTTP_PROTOCOL
*HttpInstance
;
596 Token
= (EFI_HTTP_TOKEN
*) Context
;
599 // Return EFI_SUCCESS to check the next item in the map if
600 // this one doesn't match.
602 if ((Token
!= NULL
) && (Token
!= Item
->Key
)) {
606 Wrap
= (HTTP_TOKEN_WRAP
*) Item
->Value
;
607 ASSERT (Wrap
!= NULL
);
608 HttpInstance
= Wrap
->HttpInstance
;
613 NetMapRemoveItem (Map
, Item
, NULL
);
615 if (!HttpInstance
->LocalAddressIsIPv6
) {
616 if (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
!= NULL
) {
617 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
);
620 if (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
!= NULL
) {
621 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
);
624 if (Wrap
->TcpWrap
.Rx4Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
625 FreePool (Wrap
->TcpWrap
.Rx4Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
629 if (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
!= NULL
) {
630 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
);
633 if (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
!= NULL
) {
634 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
);
637 if (Wrap
->TcpWrap
.Rx6Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
638 FreePool (Wrap
->TcpWrap
.Rx6Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
646 // If only one item is to be cancel, return EFI_ABORTED to stop
647 // iterating the map any more.
657 Cancel the user's receive/transmit request. It is the worker function of
658 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the
661 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
662 @param[in] Token The token to cancel. If NULL, all token will be
665 @retval EFI_SUCCESS The token is cancelled.
666 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.
667 @retval Others Other error as indicated.
672 IN HTTP_PROTOCOL
*HttpInstance
,
673 IN EFI_HTTP_TOKEN
*Token
679 // First check the tokens queued by EfiHttpRequest().
681 Status
= NetMapIterate (&HttpInstance
->TxTokens
, HttpCancelTokens
, Token
);
682 if (EFI_ERROR (Status
)) {
684 if (Status
== EFI_ABORTED
) {
693 // Then check the tokens queued by EfiHttpResponse().
695 Status
= NetMapIterate (&HttpInstance
->RxTokens
, HttpCancelTokens
, Token
);
696 if (EFI_ERROR (Status
)) {
698 if (Status
== EFI_ABORTED
) {
701 return EFI_NOT_FOUND
;
713 Abort an asynchronous HTTP request or response token.
715 The Cancel() function aborts a pending HTTP request or response transaction. If
716 Token is not NULL and the token is in transmit or receive queues when it is being
717 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
718 be signaled. If the token is not in one of the queues, which usually means that the
719 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
720 all asynchronous tokens issued by Request() or Response() will be aborted.
722 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
723 @param[in] Token Point to storage containing HTTP request or response
726 @retval EFI_SUCCESS Request and Response queues are successfully flushed.
727 @retval EFI_INVALID_PARAMETER This is NULL.
728 @retval EFI_NOT_STARTED This instance hasn't been configured.
729 @retval EFI_NOT_FOUND The asynchronous request or response token is not
731 @retval EFI_UNSUPPORTED The implementation does not support this function.
737 IN EFI_HTTP_PROTOCOL
*This
,
738 IN EFI_HTTP_TOKEN
*Token
741 HTTP_PROTOCOL
*HttpInstance
;
744 return EFI_INVALID_PARAMETER
;
747 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
748 ASSERT (HttpInstance
!= NULL
);
750 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
751 return EFI_NOT_STARTED
;
754 return HttpCancel (HttpInstance
, Token
);
759 A callback function to intercept events during message parser.
761 This function will be invoked during HttpParseMessageBody() with various events type. An error
762 return status of the callback function will cause the HttpParseMessageBody() aborted.
764 @param[in] EventType Event type of this callback call.
765 @param[in] Data A pointer to data buffer.
766 @param[in] Length Length in bytes of the Data.
767 @param[in] Context Callback context set by HttpInitMsgParser().
769 @retval EFI_SUCCESS Continue to parser the message body.
774 HttpBodyParserCallback (
775 IN HTTP_BODY_PARSE_EVENT EventType
,
781 HTTP_TOKEN_WRAP
*Wrap
;
785 if (EventType
!= BodyParseEventOnComplete
) {
789 if (Data
== NULL
|| Length
!= 0 || Context
== NULL
) {
793 Wrap
= (HTTP_TOKEN_WRAP
*) Context
;
794 Body
= Wrap
->HttpToken
->Message
->Body
;
795 BodyLength
= Wrap
->HttpToken
->Message
->BodyLength
;
796 if (Data
< Body
+ BodyLength
) {
797 Wrap
->HttpInstance
->NextMsg
= Data
;
799 Wrap
->HttpInstance
->NextMsg
= NULL
;
804 // Free Tx4Token or Tx6Token since already received corrsponding HTTP response.
812 The work function of EfiHttpResponse().
814 @param[in] Wrap Pointer to HTTP token's wrap data.
816 @retval EFI_SUCCESS Allocation succeeded.
817 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
818 @retval EFI_NOT_READY Can't find a corresponding Tx4Token/Tx6Token or
819 the EFI_HTTP_UTILITIES_PROTOCOL is not available.
824 IN HTTP_TOKEN_WRAP
*Wrap
828 EFI_HTTP_MESSAGE
*HttpMsg
;
836 CHAR8
*StatusCodeStr
;
838 HTTP_PROTOCOL
*HttpInstance
;
839 EFI_HTTP_TOKEN
*Token
;
841 HTTP_TOKEN_WRAP
*ValueInItem
;
844 if (Wrap
== NULL
|| Wrap
->HttpInstance
== NULL
) {
845 return EFI_INVALID_PARAMETER
;
848 HttpInstance
= Wrap
->HttpInstance
;
849 Token
= Wrap
->HttpToken
;
850 HttpMsg
= Token
->Message
;
852 HttpInstance
->EndofHeader
= NULL
;
853 HttpInstance
->HttpHeaders
= NULL
;
854 HttpMsg
->Headers
= NULL
;
860 if (HttpMsg
->Data
.Response
!= NULL
) {
862 // Need receive the HTTP headers, prepare buffer.
864 Status
= HttpCreateTcpRxEventForHeader (HttpInstance
);
865 if (EFI_ERROR (Status
)) {
870 // Check whether we have cached header from previous call.
872 if ((HttpInstance
->CacheBody
!= NULL
) && (HttpInstance
->NextMsg
!= NULL
)) {
874 // The data is stored at [NextMsg, CacheBody + CacheLen].
876 HdrLen
= HttpInstance
->CacheBody
+ HttpInstance
->CacheLen
- HttpInstance
->NextMsg
;
877 HttpHeaders
= AllocateZeroPool (HdrLen
);
878 if (HttpHeaders
== NULL
) {
879 Status
= EFI_OUT_OF_RESOURCES
;
883 CopyMem (HttpHeaders
, HttpInstance
->NextMsg
, HdrLen
);
884 FreePool (HttpInstance
->CacheBody
);
885 HttpInstance
->CacheBody
= NULL
;
886 HttpInstance
->NextMsg
= NULL
;
887 HttpInstance
->CacheOffset
= 0;
888 SizeofHeaders
= HdrLen
;
889 BufferSize
= HttpInstance
->CacheLen
;
892 // Check whether we cached the whole HTTP headers.
894 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
897 HttpInstance
->EndofHeader
= &EndofHeader
;
898 HttpInstance
->HttpHeaders
= &HttpHeaders
;
900 Status
= HttpTcpReceiveHeader (HttpInstance
, &SizeofHeaders
, &BufferSize
);
901 if (EFI_ERROR (Status
)) {
905 ASSERT (HttpHeaders
!= NULL
);
908 // Cache the part of body.
910 BodyLen
= BufferSize
- (EndofHeader
- HttpHeaders
);
912 if (HttpInstance
->CacheBody
!= NULL
) {
913 FreePool (HttpInstance
->CacheBody
);
916 HttpInstance
->CacheBody
= AllocateZeroPool (BodyLen
);
917 if (HttpInstance
->CacheBody
== NULL
) {
918 Status
= EFI_OUT_OF_RESOURCES
;
922 CopyMem (HttpInstance
->CacheBody
, EndofHeader
, BodyLen
);
923 HttpInstance
->CacheLen
= BodyLen
;
927 // Search for Status Code.
929 StatusCodeStr
= HttpHeaders
+ AsciiStrLen (HTTP_VERSION_STR
) + 1;
930 if (StatusCodeStr
== NULL
) {
934 StatusCode
= AsciiStrDecimalToUintn (StatusCodeStr
);
937 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
939 Tmp
= AsciiStrStr (HttpHeaders
, HTTP_CRLF_STR
);
944 Tmp
= Tmp
+ AsciiStrLen (HTTP_CRLF_STR
);
945 SizeofHeaders
= SizeofHeaders
- (Tmp
- HttpHeaders
);
946 HeaderTmp
= AllocateZeroPool (SizeofHeaders
);
947 if (HeaderTmp
== NULL
) {
951 CopyMem (HeaderTmp
, Tmp
, SizeofHeaders
);
952 FreePool (HttpHeaders
);
953 HttpHeaders
= HeaderTmp
;
956 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.
958 if (mHttpUtilities
== NULL
) {
959 Status
= EFI_NOT_READY
;
964 // Parse the HTTP header into array of key/value pairs.
966 Status
= mHttpUtilities
->Parse (
971 &HttpMsg
->HeaderCount
973 if (EFI_ERROR (Status
)) {
977 FreePool (HttpHeaders
);
980 HttpMsg
->Data
.Response
->StatusCode
= HttpMappingToStatusCode (StatusCode
);
981 HttpInstance
->StatusCode
= StatusCode
;
983 // Init message-body parser by header information.
985 Status
= EFI_NOT_READY
;
987 NetMapRemoveHead (&HttpInstance
->TxTokens
, (VOID
**) &ValueInItem
);
988 if (ValueInItem
== NULL
) {
993 // The first Tx Token not transmitted yet, insert back and return error.
995 if (!ValueInItem
->TcpWrap
.IsTxDone
) {
999 Status
= HttpInitMsgParser (
1000 ValueInItem
->TcpWrap
.Method
,
1001 HttpMsg
->Data
.Response
->StatusCode
,
1002 HttpMsg
->HeaderCount
,
1004 HttpBodyParserCallback
,
1005 (VOID
*) ValueInItem
,
1006 &HttpInstance
->MsgParser
1008 if (EFI_ERROR (Status
)) {
1013 // Check whether we received a complete HTTP message.
1015 if (HttpInstance
->CacheBody
!= NULL
) {
1016 Status
= HttpParseMessageBody (HttpInstance
->MsgParser
, HttpInstance
->CacheLen
, HttpInstance
->CacheBody
);
1017 if (EFI_ERROR (Status
)) {
1021 if (HttpIsMessageComplete (HttpInstance
->MsgParser
)) {
1023 // Free the MsgParse since we already have a full HTTP message.
1025 HttpFreeMsgParser (HttpInstance
->MsgParser
);
1026 HttpInstance
->MsgParser
= NULL
;
1030 if ((HttpMsg
->Body
== NULL
) || (HttpMsg
->BodyLength
== 0)) {
1031 Status
= EFI_SUCCESS
;
1037 // Receive the response body.
1042 // First check whether we cached some data.
1044 if (HttpInstance
->CacheBody
!= NULL
) {
1046 // Calculate the length of the cached data.
1048 if (HttpInstance
->NextMsg
!= NULL
) {
1050 // We have a cached HTTP message which includes a part of HTTP header of next message.
1052 BodyLen
= HttpInstance
->NextMsg
- (HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
);
1054 BodyLen
= HttpInstance
->CacheLen
- HttpInstance
->CacheOffset
;
1059 // We have some cached data. Just copy the data and return.
1061 if (HttpMsg
->BodyLength
< BodyLen
) {
1062 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, HttpMsg
->BodyLength
);
1063 HttpInstance
->CacheOffset
= HttpInstance
->CacheOffset
+ HttpMsg
->BodyLength
;
1066 // Copy all cached data out.
1068 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, BodyLen
);
1069 HttpInstance
->CacheOffset
= BodyLen
+ HttpInstance
->CacheOffset
;
1070 HttpMsg
->BodyLength
= BodyLen
;
1072 if (HttpInstance
->NextMsg
== NULL
) {
1074 // There is no HTTP header of next message. Just free the cache buffer.
1076 FreePool (HttpInstance
->CacheBody
);
1077 HttpInstance
->CacheBody
= NULL
;
1078 HttpInstance
->NextMsg
= NULL
;
1079 HttpInstance
->CacheOffset
= 0;
1083 // Return since we aready received required data.
1085 Status
= EFI_SUCCESS
;
1089 if (BodyLen
== 0 && HttpInstance
->MsgParser
== NULL
) {
1091 // We received a complete HTTP message, and we don't have more data to return to caller.
1093 HttpMsg
->BodyLength
= 0;
1094 Status
= EFI_SUCCESS
;
1099 ASSERT (HttpInstance
->MsgParser
!= NULL
);
1102 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1104 Status
= HttpTcpReceiveBody (Wrap
, HttpMsg
);
1105 if (EFI_ERROR (Status
)) {
1112 Item
= NetMapFindKey (&Wrap
->HttpInstance
->RxTokens
, Wrap
->HttpToken
);
1114 NetMapRemoveItem (&Wrap
->HttpInstance
->RxTokens
, Item
, NULL
);
1117 if (HttpInstance
->StatusCode
>= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE
) {
1118 Token
->Status
= EFI_HTTP_ERROR
;
1120 Token
->Status
= Status
;
1123 gBS
->SignalEvent (Token
->Event
);
1124 HttpCloseTcpRxEvent (Wrap
);
1129 NetMapInsertHead (&HttpInstance
->TxTokens
, ValueInItem
->HttpToken
, ValueInItem
);
1132 HttpTcpTokenCleanup (Wrap
);
1134 if (HttpHeaders
!= NULL
) {
1135 FreePool (HttpHeaders
);
1138 if (HttpMsg
->Headers
!= NULL
) {
1139 FreePool (HttpMsg
->Headers
);
1142 if (HttpInstance
->CacheBody
!= NULL
) {
1143 FreePool (HttpInstance
->CacheBody
);
1144 HttpInstance
->CacheBody
= NULL
;
1147 if (HttpInstance
->StatusCode
>= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE
) {
1148 Token
->Status
= EFI_HTTP_ERROR
;
1150 Token
->Status
= Status
;
1153 gBS
->SignalEvent (Token
->Event
);
1161 The Response() function queues an HTTP response to this HTTP instance, similar to
1162 Receive() function in the EFI TCP driver. When the HTTP response is received successfully,
1163 or if there is an error, Status in token will be updated and Event will be signaled.
1165 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1166 is received in the underlying TCP instance, the data will be parsed and Token will
1167 be populated with the response data. If the data received from the remote host
1168 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1169 (asynchronously) for more data to be sent from the remote host before signaling
1172 It is the responsibility of the caller to allocate a buffer for Body and specify the
1173 size in BodyLength. If the remote host provides a response that contains a content
1174 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1175 BodyLength will be updated with the amount of bytes received and copied to Body. This
1176 allows the client to download a large file in chunks instead of into one contiguous
1177 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1178 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1179 token to underlying TCP instance. If data arrives in the receive buffer, up to
1180 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1181 BodyLength with the amount of bytes received and copied to Body.
1183 If the HTTP driver does not have an open underlying TCP connection with the host
1184 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1185 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1186 an open TCP connection between client and host.
1188 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1189 @param[in] Token Pointer to storage containing HTTP response token.
1191 @retval EFI_SUCCESS Allocation succeeded.
1192 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1194 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1197 Token->Message->Headers is NULL.
1198 Token->Message is NULL.
1199 Token->Message->Body is not NULL,
1200 Token->Message->BodyLength is non-zero, and
1201 Token->Message->Data is NULL, but a previous call to
1202 Response() has not been completed successfully.
1203 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1204 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1205 specified by response URL.
1210 IN EFI_HTTP_PROTOCOL
*This
,
1211 IN EFI_HTTP_TOKEN
*Token
1215 EFI_HTTP_MESSAGE
*HttpMsg
;
1216 HTTP_PROTOCOL
*HttpInstance
;
1217 HTTP_TOKEN_WRAP
*Wrap
;
1219 if ((This
== NULL
) || (Token
== NULL
)) {
1220 return EFI_INVALID_PARAMETER
;
1223 HttpMsg
= Token
->Message
;
1224 if (HttpMsg
== NULL
) {
1225 return EFI_INVALID_PARAMETER
;
1228 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1229 ASSERT (HttpInstance
!= NULL
);
1231 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1232 return EFI_NOT_STARTED
;
1236 // Check whether the token already existed.
1238 if (EFI_ERROR (NetMapIterate (&HttpInstance
->RxTokens
, HttpTokenExist
, Token
))) {
1239 return EFI_ACCESS_DENIED
;
1242 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
1244 return EFI_OUT_OF_RESOURCES
;
1247 Wrap
->HttpInstance
= HttpInstance
;
1248 Wrap
->HttpToken
= Token
;
1250 Status
= HttpCreateTcpRxEvent (Wrap
);
1251 if (EFI_ERROR (Status
)) {
1255 Status
= NetMapInsertTail (&HttpInstance
->RxTokens
, Token
, Wrap
);
1256 if (EFI_ERROR (Status
)) {
1261 // If already have pending RxTokens, return directly.
1263 if (NetMapGetCount (&HttpInstance
->RxTokens
) > 1) {
1267 return HttpResponseWorker (Wrap
);
1271 if (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
!= NULL
) {
1272 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
);
1275 if (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
!= NULL
) {
1276 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
);
1285 The Poll() function can be used by network drivers and applications to increase the
1286 rate that data packets are moved between the communication devices and the transmit
1289 In some systems, the periodic timer event in the managed network driver may not poll
1290 the underlying communications device fast enough to transmit and/or receive all data
1291 packets without missing incoming packets or dropping outgoing packets. Drivers and
1292 applications that are experiencing packet loss should try calling the Poll() function
1295 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1297 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1298 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1299 @retval EFI_INVALID_PARAMETER This is NULL.
1300 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1301 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1307 IN EFI_HTTP_PROTOCOL
*This
1311 HTTP_PROTOCOL
*HttpInstance
;
1314 return EFI_INVALID_PARAMETER
;
1317 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1318 ASSERT (HttpInstance
!= NULL
);
1320 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1321 return EFI_NOT_STARTED
;
1324 if (HttpInstance
->LocalAddressIsIPv6
) {
1325 if (HttpInstance
->Tcp6
== NULL
) {
1326 return EFI_NOT_STARTED
;
1328 Status
= HttpInstance
->Tcp6
->Poll (HttpInstance
->Tcp6
);
1330 if (HttpInstance
->Tcp4
== NULL
) {
1331 return EFI_NOT_STARTED
;
1333 Status
= HttpInstance
->Tcp4
->Poll (HttpInstance
->Tcp4
);