2 Implementation of EFI_HTTP_PROTOCOL protocol interfaces.
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "HttpDriver.h"
18 EFI_HTTP_PROTOCOL mEfiHttpTemplate
= {
28 Returns the operational parameters for the current HTTP child instance.
30 The GetModeData() function is used to read the current mode data (operational
31 parameters) for this HTTP protocol instance.
33 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
34 @param[out] HttpConfigData Point to buffer for operational parameters of this
37 @retval EFI_SUCCESS Operation succeeded.
38 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
40 HttpConfigData is NULL.
41 HttpConfigData->AccessPoint is NULL.
42 @retval EFI_NOT_STARTED The HTTP instance is not configured.
48 IN EFI_HTTP_PROTOCOL
*This
,
49 OUT EFI_HTTP_CONFIG_DATA
*HttpConfigData
52 HTTP_PROTOCOL
*HttpInstance
;
54 if ((This
== NULL
) || (HttpConfigData
== NULL
)) {
55 return EFI_INVALID_PARAMETER
;
58 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
59 ASSERT (HttpInstance
!= NULL
);
61 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
62 return EFI_NOT_STARTED
;
65 if (HttpConfigData
->AccessPoint
.IPv4Node
== NULL
) {
66 return EFI_INVALID_PARAMETER
;
69 HttpConfigData
->HttpVersion
= HttpInstance
->HttpVersion
;
70 HttpConfigData
->TimeOutMillisec
= HttpInstance
->TimeOutMillisec
;
71 HttpConfigData
->LocalAddressIsIPv6
= HttpInstance
->LocalAddressIsIPv6
;
74 HttpConfigData
->AccessPoint
.IPv4Node
,
75 &HttpInstance
->IPv4Node
,
76 sizeof (HttpInstance
->IPv4Node
)
83 Initialize or brutally reset the operational parameters for this EFI HTTP instance.
85 The Configure() function does the following:
86 When HttpConfigData is not NULL Initialize this EFI HTTP instance by configuring
87 timeout, local address, port, etc.
88 When HttpConfigData is NULL, reset this EFI HTTP instance by closing all active
89 connections with remote hosts, canceling all asynchronous tokens, and flush request
90 and response buffers without informing the appropriate hosts.
92 Except for GetModeData() and Configure(), No other EFI HTTP function can be executed
93 by this instance until the Configure() function is executed and returns successfully.
95 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
96 @param[in] HttpConfigData Pointer to the configure data to configure the instance.
98 @retval EFI_SUCCESS Operation succeeded.
99 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
101 HttpConfigData->LocalAddressIsIPv6 is FALSE and
102 HttpConfigData->IPv4Node is NULL.
103 HttpConfigData->LocalAddressIsIPv6 is TRUE and
104 HttpConfigData->IPv6Node is NULL.
105 @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling
106 Configure() with NULL to reset it.
107 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
108 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when
109 executing Configure().
110 @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported
111 in the implementation.
116 IN EFI_HTTP_PROTOCOL
*This
,
117 IN EFI_HTTP_CONFIG_DATA
*HttpConfigData
120 HTTP_PROTOCOL
*HttpInstance
;
124 return EFI_INVALID_PARAMETER
;
127 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
128 ASSERT (HttpInstance
!= NULL
&& HttpInstance
->Service
!= NULL
);
130 if (HttpConfigData
!= NULL
) {
132 // Check input parameters.
134 if (HttpConfigData
->LocalAddressIsIPv6
) {
135 if (HttpConfigData
->AccessPoint
.IPv6Node
== NULL
) {
136 return EFI_INVALID_PARAMETER
;
139 if (HttpConfigData
->AccessPoint
.IPv4Node
== NULL
) {
140 return EFI_INVALID_PARAMETER
;
144 // Now configure this HTTP instance.
146 if (HttpInstance
->State
!= HTTP_STATE_UNCONFIGED
) {
147 return EFI_ALREADY_STARTED
;
150 HttpInstance
->HttpVersion
= HttpConfigData
->HttpVersion
;
151 HttpInstance
->TimeOutMillisec
= HttpConfigData
->TimeOutMillisec
;
152 HttpInstance
->LocalAddressIsIPv6
= HttpConfigData
->LocalAddressIsIPv6
;
154 if (HttpConfigData
->LocalAddressIsIPv6
) {
155 return EFI_UNSUPPORTED
;
158 &HttpInstance
->IPv4Node
,
159 HttpConfigData
->AccessPoint
.IPv4Node
,
160 sizeof (HttpInstance
->IPv4Node
)
163 HttpInstance
->State
= HTTP_STATE_HTTP_CONFIGED
;
168 if (HttpInstance
->LocalAddressIsIPv6
) {
169 return EFI_UNSUPPORTED
;
171 HttpCleanProtocol (HttpInstance
);
172 Status
= HttpInitProtocol (HttpInstance
->Service
, HttpInstance
);
173 if (EFI_ERROR (Status
)) {
177 HttpInstance
->State
= HTTP_STATE_UNCONFIGED
;
185 The Request() function queues an HTTP request to this HTTP instance.
187 Similar to Transmit() function in the EFI TCP driver. When the HTTP request is sent
188 successfully, or if there is an error, Status in token will be updated and Event will
191 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
192 @param[in] Token Pointer to storage containing HTTP request token.
194 @retval EFI_SUCCESS Outgoing data was processed.
195 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
196 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
197 @retval EFI_TIMEOUT Data was dropped out of the transmit or receive queue.
198 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
199 @retval EFI_UNSUPPORTED The HTTP method is not supported in current
201 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
203 Token->Message is NULL.
204 Token->Message->Body is not NULL,
205 Token->Message->BodyLength is non-zero, and
206 Token->Message->Data is NULL, but a previous call to
207 Request()has not been completed successfully.
212 IN EFI_HTTP_PROTOCOL
*This
,
213 IN EFI_HTTP_TOKEN
*Token
216 EFI_HTTP_MESSAGE
*HttpMsg
;
217 EFI_HTTP_REQUEST_DATA
*Request
;
222 HTTP_PROTOCOL
*HttpInstance
;
229 HTTP_TOKEN_WRAP
*Wrap
;
230 HTTP_TCP_TOKEN_WRAP
*TcpWrap
;
233 if ((This
== NULL
) || (Token
== NULL
)) {
234 return EFI_INVALID_PARAMETER
;
237 HttpMsg
= Token
->Message
;
238 if ((HttpMsg
== NULL
) || (HttpMsg
->Headers
== NULL
)) {
239 return EFI_INVALID_PARAMETER
;
243 // Current implementation does not support POST/PUT method.
244 // If future version supports these two methods, Request could be NULL for a special case that to send large amounts
245 // of data. For this case, the implementation need check whether previous call to Request() has been completed or not.
248 Request
= HttpMsg
->Data
.Request
;
249 if ((Request
== NULL
) || (Request
->Url
== NULL
)) {
250 return EFI_INVALID_PARAMETER
;
254 // Only support GET and HEAD method in current implementation.
256 if ((Request
->Method
!= HttpMethodGet
) && (Request
->Method
!= HttpMethodHead
)) {
257 return EFI_UNSUPPORTED
;
260 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
261 ASSERT (HttpInstance
!= NULL
);
263 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
264 return EFI_NOT_STARTED
;
267 if (HttpInstance
->LocalAddressIsIPv6
) {
268 return EFI_UNSUPPORTED
;
272 // Check whether the token already existed.
274 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTokenExist
, Token
))) {
275 return EFI_ACCESS_DENIED
;
284 // Parse the URI of the remote host.
286 Url
= HttpInstance
->Url
;
287 UrlLen
= StrLen (Request
->Url
) + 1;
288 if (UrlLen
> HTTP_URL_BUFFER_LEN
) {
289 Url
= AllocateZeroPool (UrlLen
);
291 return EFI_OUT_OF_RESOURCES
;
293 FreePool (HttpInstance
->Url
);
294 HttpInstance
->Url
= Url
;
297 UnicodeStrToAsciiStr (Request
->Url
, Url
);
299 Status
= HttpParseUrl (Url
, (UINT32
) AsciiStrLen (Url
), FALSE
, &UrlParser
);
300 if (EFI_ERROR (Status
)) {
306 Status
= HttpUrlGetHostName (Url
, UrlParser
, &HostName
);
307 if (EFI_ERROR (Status
)) {
311 Status
= HttpUrlGetPort (Url
, UrlParser
, &RemotePort
);
312 if (EFI_ERROR (Status
)) {
313 RemotePort
= HTTP_DEFAULT_PORT
;
319 if (HttpInstance
->RemoteHost
== NULL
&& HttpInstance
->RemotePort
== 0) {
321 // Request() is called the first time.
325 if ((HttpInstance
->RemotePort
== RemotePort
) &&
326 (AsciiStrCmp (HttpInstance
->RemoteHost
, HostName
) == 0)) {
328 // Host Name and port number of the request URL are the same with previous call to Request().
329 // Check whether previous TCP packet sent out.
331 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTcpNotReady
, NULL
))) {
333 // Wrap the HTTP token in HTTP_TOKEN_WRAP
335 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
337 Status
= EFI_OUT_OF_RESOURCES
;
341 Wrap
->HttpToken
= Token
;
342 Wrap
->HttpInstance
= HttpInstance
;
344 Status
= HttpCreateTcp4TxEvent (Wrap
);
345 if (EFI_ERROR (Status
)) {
349 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
350 if (EFI_ERROR (Status
)) {
354 Wrap
->TcpWrap
.Method
= Request
->Method
;
359 // Queue the HTTP token and return.
364 // Use existing TCP instance to transmit the packet.
371 // Need close existing TCP instance and create a new TCP instance for data transmit.
373 if (HttpInstance
->RemoteHost
!= NULL
) {
374 FreePool (HttpInstance
->RemoteHost
);
375 HttpInstance
->RemoteHost
= NULL
;
382 // Parse Url for IPv4 address, if failed, perform DNS resolution.
384 Status
= NetLibAsciiStrToIp4 (HostName
, &HttpInstance
->RemoteAddr
);
385 if (EFI_ERROR (Status
)) {
386 HostNameStr
= AllocateZeroPool ((AsciiStrLen (HostName
) + 1) * sizeof (UINT16
));
387 if (HostNameStr
== NULL
) {
388 Status
= EFI_OUT_OF_RESOURCES
;
392 AsciiStrToUnicodeStr (HostName
, HostNameStr
);
393 Status
= HttpDns4 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteAddr
);
394 FreePool (HostNameStr
);
395 if (EFI_ERROR (Status
)) {
401 // Save the RemotePort and RemoteHost.
403 ASSERT (HttpInstance
->RemoteHost
== NULL
);
404 HttpInstance
->RemotePort
= RemotePort
;
405 HttpInstance
->RemoteHost
= HostName
;
411 // The request URL is different from previous calls to Request(), close existing TCP instance.
413 ASSERT (HttpInstance
->Tcp4
!= NULL
);
414 HttpCloseConnection (HttpInstance
);
415 EfiHttpCancel (This
, NULL
);
419 // Wrap the HTTP token in HTTP_TOKEN_WRAP
421 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
423 Status
= EFI_OUT_OF_RESOURCES
;
427 Wrap
->HttpToken
= Token
;
428 Wrap
->HttpInstance
= HttpInstance
;
429 Wrap
->TcpWrap
.Method
= Request
->Method
;
433 // Configure TCP instance.
435 Status
= HttpConfigureTcp4 (HttpInstance
, Wrap
);
436 if (EFI_ERROR (Status
)) {
442 Status
= HttpConnectTcp4 (HttpInstance
);
443 if (EFI_ERROR (Status
)) {
448 // For the new HTTP token, create TX TCP token events.
450 Status
= HttpCreateTcp4TxEvent (Wrap
);
451 if (EFI_ERROR (Status
)) {
457 // Create request message.
460 if (*FileUrl
!= '/') {
462 // Convert the absolute-URI to the absolute-path
464 while (*FileUrl
!= ':') {
467 if ((*(FileUrl
+1) == '/') && (*(FileUrl
+2) == '/')) {
469 while (*FileUrl
!= '/') {
473 Status
= EFI_INVALID_PARAMETER
;
477 RequestStr
= HttpGenRequestString (HttpInstance
, HttpMsg
, FileUrl
);
478 if (RequestStr
== NULL
) {
479 Status
= EFI_OUT_OF_RESOURCES
;
483 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
484 if (EFI_ERROR (Status
)) {
488 if (HostName
!= NULL
) {
493 // Transmit the request message.
495 Status
= HttpTransmitTcp4 (
499 AsciiStrLen (RequestStr
)
501 if (EFI_ERROR (Status
)) {
510 NetMapRemoveTail (&HttpInstance
->TxTokens
, NULL
);
513 if (RequestStr
!= NULL
) {
514 FreePool (RequestStr
);
518 HttpCloseConnection (HttpInstance
);
522 HttpCloseTcp4ConnCloseEvent (HttpInstance
);
523 if (NULL
!= Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
) {
524 gBS
->CloseEvent (Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
);
525 Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
= NULL
;
529 if (HostName
!= NULL
) {
535 if (UrlParser
!= NULL
) {
536 HttpUrlFreeParser (UrlParser
);
544 Cancel a TxToken or RxToken.
546 @param[in] Map The HTTP instance's token queue.
547 @param[in] Item Object container for one HTTP token and token's wrap.
548 @param[in] Context The user's token to cancel.
550 @retval EFI_SUCCESS Continue to check the next Item.
551 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
558 IN NET_MAP_ITEM
*Item
,
563 EFI_HTTP_TOKEN
*Token
;
564 HTTP_TOKEN_WRAP
*Wrap
;
566 Token
= (EFI_HTTP_TOKEN
*) Context
;
569 // Return EFI_SUCCESS to check the next item in the map if
570 // this one doesn't match.
572 if ((Token
!= NULL
) && (Token
!= Item
->Key
)) {
576 Wrap
= (HTTP_TOKEN_WRAP
*) Item
->Value
;
577 ASSERT (Wrap
!= NULL
);
582 NetMapRemoveItem (Map
, Item
, NULL
);
584 if (Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
!= NULL
) {
585 gBS
->CloseEvent (Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
);
588 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
589 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
592 if (Wrap
->TcpWrap
.RxToken
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
593 FreePool (Wrap
->TcpWrap
.RxToken
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
599 // If only one item is to be cancel, return EFI_ABORTED to stop
600 // iterating the map any more.
610 Cancel the user's receive/transmit request. It is the worker function of
611 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the
614 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
615 @param[in] Token The token to cancel. If NULL, all token will be
618 @retval EFI_SUCCESS The token is cancelled.
619 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.
620 @retval Others Other error as indicated.
625 IN HTTP_PROTOCOL
*HttpInstance
,
626 IN EFI_HTTP_TOKEN
*Token
632 // First check the tokens queued by EfiHttpRequest().
634 Status
= NetMapIterate (&HttpInstance
->TxTokens
, HttpCancelTokens
, Token
);
635 if (EFI_ERROR (Status
)) {
637 if (Status
== EFI_ABORTED
) {
646 // Then check the tokens queued by EfiHttpResponse().
648 Status
= NetMapIterate (&HttpInstance
->RxTokens
, HttpCancelTokens
, Token
);
649 if (EFI_ERROR (Status
)) {
651 if (Status
== EFI_ABORTED
) {
654 return EFI_NOT_FOUND
;
666 Abort an asynchronous HTTP request or response token.
668 The Cancel() function aborts a pending HTTP request or response transaction. If
669 Token is not NULL and the token is in transmit or receive queues when it is being
670 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
671 be signaled. If the token is not in one of the queues, which usually means that the
672 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
673 all asynchronous tokens issued by Request() or Response() will be aborted.
675 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
676 @param[in] Token Point to storage containing HTTP request or response
679 @retval EFI_SUCCESS Request and Response queues are successfully flushed.
680 @retval EFI_INVALID_PARAMETER This is NULL.
681 @retval EFI_NOT_STARTED This instance hasn't been configured.
682 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP,
683 BOOTP, RARP, etc.) hasn't finished yet.
684 @retval EFI_NOT_FOUND The asynchronous request or response token is not
686 @retval EFI_UNSUPPORTED The implementation does not support this function.
692 IN EFI_HTTP_PROTOCOL
*This
,
693 IN EFI_HTTP_TOKEN
*Token
696 HTTP_PROTOCOL
*HttpInstance
;
699 return EFI_INVALID_PARAMETER
;
702 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
703 ASSERT (HttpInstance
!= NULL
);
705 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
706 return EFI_NOT_STARTED
;
709 return HttpCancel (HttpInstance
, Token
);
714 A callback function to intercept events during message parser.
716 This function will be invoked during HttpParseMessageBody() with various events type. An error
717 return status of the callback function will cause the HttpParseMessageBody() aborted.
719 @param[in] EventType Event type of this callback call.
720 @param[in] Data A pointer to data buffer.
721 @param[in] Length Length in bytes of the Data.
722 @param[in] Context Callback context set by HttpInitMsgParser().
724 @retval EFI_SUCCESS Continue to parser the message body.
729 HttpBodyParserCallback (
730 IN HTTP_BODY_PARSE_EVENT EventType
,
736 HTTP_TOKEN_WRAP
*Wrap
;
738 if (EventType
!= BodyParseEventOnComplete
) {
742 if (Data
== NULL
|| Length
!= 0 || Context
== NULL
) {
746 Wrap
= (HTTP_TOKEN_WRAP
*) Context
;
747 Wrap
->HttpInstance
->NextMsg
= Data
;
750 // Free TxToken since already received corrsponding HTTP response.
758 The work function of EfiHttpResponse().
760 @param[in] Wrap Pointer to HTTP token's wrap data.
762 @retval EFI_SUCCESS Allocation succeeded.
763 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
764 @retval EFI_NOT_READY Can't find a corresponding TxToken or
765 the EFI_HTTP_UTILITIES_PROTOCOL is not available.
770 IN HTTP_TOKEN_WRAP
*Wrap
774 EFI_HTTP_MESSAGE
*HttpMsg
;
775 EFI_TCP4_IO_TOKEN
*RxToken
;
776 EFI_TCP4_PROTOCOL
*Tcp4
;
785 CHAR8
*StatusCodeStr
;
787 HTTP_PROTOCOL
*HttpInstance
;
788 EFI_HTTP_TOKEN
*Token
;
790 HTTP_TOKEN_WRAP
*ValueInItem
;
793 if (Wrap
== NULL
|| Wrap
->HttpInstance
== NULL
) {
794 return EFI_INVALID_PARAMETER
;
797 HttpInstance
= Wrap
->HttpInstance
;
798 Token
= Wrap
->HttpToken
;
800 HttpMsg
= Token
->Message
;
802 Tcp4
= HttpInstance
->Tcp4
;
803 ASSERT (Tcp4
!= NULL
);
804 HttpMsg
->Headers
= NULL
;
811 if (HttpMsg
->Data
.Response
!= NULL
) {
813 // Need receive the HTTP headers, prepare buffer.
815 Status
= HttpCreateTcp4RxEventForHeader (HttpInstance
);
816 if (EFI_ERROR (Status
)) {
821 // Check whether we have cached header from previous call.
823 if ((HttpInstance
->CacheBody
!= NULL
) && (HttpInstance
->NextMsg
!= NULL
)) {
825 // The data is stored at [NextMsg, CacheBody + CacheLen].
827 HdrLen
= HttpInstance
->CacheBody
+ HttpInstance
->CacheLen
- HttpInstance
->NextMsg
;
828 HttpHeaders
= AllocateZeroPool (HdrLen
);
829 if (HttpHeaders
== NULL
) {
830 Status
= EFI_OUT_OF_RESOURCES
;
834 CopyMem (HttpHeaders
, HttpInstance
->NextMsg
, HdrLen
);
835 FreePool (HttpInstance
->CacheBody
);
836 HttpInstance
->CacheBody
= NULL
;
837 HttpInstance
->NextMsg
= NULL
;
838 HttpInstance
->CacheOffset
= 0;
839 SizeofHeaders
= HdrLen
;
840 BufferSize
= HttpInstance
->CacheLen
;
843 // Check whether we cached the whole HTTP headers.
845 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
848 RxToken
= &HttpInstance
->RxToken
;
849 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= AllocateZeroPool (DEF_BUF_LEN
);
850 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
== NULL
) {
851 Status
= EFI_OUT_OF_RESOURCES
;
856 // Receive the HTTP headers only when EFI_HTTP_RESPONSE_DATA is not NULL.
858 while (EndofHeader
== NULL
) {
859 HttpInstance
->IsRxDone
= FALSE
;
860 RxToken
->Packet
.RxData
->DataLength
= DEF_BUF_LEN
;
861 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
= DEF_BUF_LEN
;
862 Status
= Tcp4
->Receive (Tcp4
, RxToken
);
863 if (EFI_ERROR (Status
)) {
864 DEBUG ((EFI_D_ERROR
, "Tcp4 receive failed: %r\n", Status
));
868 while (!HttpInstance
->IsRxDone
) {
872 Status
= RxToken
->CompletionToken
.Status
;
873 if (EFI_ERROR (Status
)) {
878 // Append the response string.
880 BufferSize
= SizeofHeaders
+ RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
;
881 Buffer
= AllocateZeroPool (BufferSize
);
882 if (Buffer
== NULL
) {
883 Status
= EFI_OUT_OF_RESOURCES
;
887 if (HttpHeaders
!= NULL
) {
888 CopyMem (Buffer
, HttpHeaders
, SizeofHeaders
);
889 FreePool (HttpHeaders
);
893 Buffer
+ SizeofHeaders
,
894 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
,
895 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
897 HttpHeaders
= Buffer
;
898 SizeofHeaders
= BufferSize
;
901 // Check whether we received end of HTTP headers.
903 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
907 // Skip the CRLF after the HTTP headers.
909 EndofHeader
= EndofHeader
+ AsciiStrLen (HTTP_END_OF_HDR_STR
);
912 // Cache the part of body.
914 BodyLen
= BufferSize
- (EndofHeader
- HttpHeaders
);
916 if (HttpInstance
->CacheBody
!= NULL
) {
917 FreePool (HttpInstance
->CacheBody
);
920 HttpInstance
->CacheBody
= AllocateZeroPool (BodyLen
);
921 if (HttpInstance
->CacheBody
== NULL
) {
922 Status
= EFI_OUT_OF_RESOURCES
;
926 CopyMem (HttpInstance
->CacheBody
, EndofHeader
, BodyLen
);
927 HttpInstance
->CacheLen
= BodyLen
;
930 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
931 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
934 // Search for Status Code.
936 StatusCodeStr
= HttpHeaders
+ AsciiStrLen (HTTP_VERSION_STR
) + 1;
937 if (StatusCodeStr
== NULL
) {
941 StatusCode
= AsciiStrDecimalToUintn (StatusCodeStr
);
944 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
946 Tmp
= AsciiStrStr (HttpHeaders
, HTTP_CRLF_STR
);
951 Tmp
= Tmp
+ AsciiStrLen (HTTP_CRLF_STR
);
952 SizeofHeaders
= SizeofHeaders
- (Tmp
- HttpHeaders
);
953 HeaderTmp
= AllocateZeroPool (SizeofHeaders
);
954 if (HeaderTmp
== NULL
) {
958 CopyMem (HeaderTmp
, Tmp
, SizeofHeaders
);
959 FreePool (HttpHeaders
);
960 HttpHeaders
= HeaderTmp
;
963 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.
965 if (mHttpUtilities
== NULL
) {
966 Status
= EFI_NOT_READY
;
971 // Parse the HTTP header into array of key/value pairs.
973 Status
= mHttpUtilities
->Parse (
978 &HttpMsg
->HeaderCount
980 if (EFI_ERROR (Status
)) {
984 FreePool (HttpHeaders
);
987 HttpMsg
->Data
.Response
->StatusCode
= HttpMappingToStatusCode (StatusCode
);
990 // Init message-body parser by header information.
992 Status
= EFI_NOT_READY
;
994 NetMapRemoveHead (&HttpInstance
->TxTokens
, (VOID
**) &ValueInItem
);
995 if (ValueInItem
== NULL
) {
1000 // The first TxToken not transmitted yet, insert back and return error.
1002 if (!ValueInItem
->TcpWrap
.IsTxDone
) {
1006 Status
= HttpInitMsgParser (
1007 ValueInItem
->TcpWrap
.Method
,
1008 HttpMsg
->Data
.Response
->StatusCode
,
1009 HttpMsg
->HeaderCount
,
1011 HttpBodyParserCallback
,
1012 (VOID
*) ValueInItem
,
1013 &HttpInstance
->MsgParser
1015 if (EFI_ERROR (Status
)) {
1020 // Check whether we received a complete HTTP message.
1022 if (HttpInstance
->CacheBody
!= NULL
) {
1023 Status
= HttpParseMessageBody (HttpInstance
->MsgParser
, HttpInstance
->CacheLen
, HttpInstance
->CacheBody
);
1024 if (EFI_ERROR (Status
)) {
1028 if (HttpIsMessageComplete (HttpInstance
->MsgParser
)) {
1030 // Free the MsgParse since we already have a full HTTP message.
1032 HttpFreeMsgParser (HttpInstance
->MsgParser
);
1033 HttpInstance
->MsgParser
= NULL
;
1037 if ((HttpMsg
->Body
== NULL
) || (HttpMsg
->BodyLength
== 0)) {
1038 Status
= EFI_SUCCESS
;
1044 // Receive the response body.
1049 // First check whether we cached some data.
1051 if (HttpInstance
->CacheBody
!= NULL
) {
1053 // Calculate the length of the cached data.
1055 if (HttpInstance
->NextMsg
!= NULL
) {
1057 // We have a cached HTTP message which includes a part of HTTP header of next message.
1059 BodyLen
= HttpInstance
->NextMsg
- (HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
);
1061 BodyLen
= HttpInstance
->CacheLen
- HttpInstance
->CacheOffset
;
1066 // We have some cached data. Just copy the data and return.
1068 if (HttpMsg
->BodyLength
< BodyLen
) {
1069 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, HttpMsg
->BodyLength
);
1070 HttpInstance
->CacheOffset
= HttpInstance
->CacheOffset
+ HttpMsg
->BodyLength
;
1073 // Copy all cached data out.
1075 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, BodyLen
);
1076 HttpInstance
->CacheOffset
= BodyLen
+ HttpInstance
->CacheOffset
;
1077 HttpMsg
->BodyLength
= BodyLen
;
1079 if (HttpInstance
->NextMsg
== NULL
) {
1081 // There is no HTTP header of next message. Just free the cache buffer.
1083 FreePool (HttpInstance
->CacheBody
);
1084 HttpInstance
->CacheBody
= NULL
;
1085 HttpInstance
->NextMsg
= NULL
;
1086 HttpInstance
->CacheOffset
= 0;
1090 // Return since we aready received required data.
1092 Status
= EFI_SUCCESS
;
1096 if (BodyLen
== 0 && HttpInstance
->MsgParser
== NULL
) {
1098 // We received a complete HTTP message, and we don't have more data to return to caller.
1100 HttpMsg
->BodyLength
= 0;
1101 Status
= EFI_SUCCESS
;
1106 ASSERT (HttpInstance
->MsgParser
!= NULL
);
1109 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1111 RxToken
= &Wrap
->TcpWrap
.RxToken
;
1113 RxToken
->Packet
.RxData
->DataLength
= (UINT32
) HttpMsg
->BodyLength
;
1114 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
= (UINT32
) HttpMsg
->BodyLength
;
1115 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= (VOID
*) HttpMsg
->Body
;
1117 RxToken
->CompletionToken
.Status
= EFI_NOT_READY
;
1118 Status
= Tcp4
->Receive (Tcp4
, RxToken
);
1119 if (EFI_ERROR (Status
)) {
1120 DEBUG ((EFI_D_ERROR
, "Tcp4 receive failed: %r\n", Status
));
1127 Item
= NetMapFindKey (&Wrap
->HttpInstance
->RxTokens
, Wrap
->HttpToken
);
1129 NetMapRemoveItem (&Wrap
->HttpInstance
->RxTokens
, Item
, NULL
);
1131 Token
->Status
= Status
;
1132 gBS
->SignalEvent (Token
->Event
);
1137 NetMapInsertHead (&HttpInstance
->TxTokens
, ValueInItem
->HttpToken
, ValueInItem
);
1141 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
1142 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
1144 RxToken
= &Wrap
->TcpWrap
.RxToken
;
1145 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
1146 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
1147 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
1152 if (HttpInstance
->RxToken
.CompletionToken
.Event
!= NULL
) {
1153 gBS
->CloseEvent (HttpInstance
->RxToken
.CompletionToken
.Event
);
1154 HttpInstance
->RxToken
.CompletionToken
.Event
= NULL
;
1157 RxToken
= &HttpInstance
->RxToken
;
1158 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
1159 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
1160 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
1163 if (HttpHeaders
!= NULL
) {
1164 FreePool (HttpHeaders
);
1167 if (HttpMsg
->Headers
!= NULL
) {
1168 FreePool (HttpMsg
->Headers
);
1171 if (HttpInstance
->CacheBody
!= NULL
) {
1172 FreePool (HttpInstance
->CacheBody
);
1173 HttpInstance
->CacheBody
= NULL
;
1176 Token
->Status
= Status
;
1177 gBS
->SignalEvent (Token
->Event
);
1185 The Response() function queues an HTTP response to this HTTP instance, similar to
1186 Receive() function in the EFI TCP driver. When the HTTP request is sent successfully,
1187 or if there is an error, Status in token will be updated and Event will be signaled.
1189 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1190 is received in the underlying TCP instance, the data will be parsed and Token will
1191 be populated with the response data. If the data received from the remote host
1192 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1193 (asynchronously) for more data to be sent from the remote host before signaling
1196 It is the responsibility of the caller to allocate a buffer for Body and specify the
1197 size in BodyLength. If the remote host provides a response that contains a content
1198 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1199 BodyLength will be updated with the amount of bytes received and copied to Body. This
1200 allows the client to download a large file in chunks instead of into one contiguous
1201 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1202 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1203 token to underlying TCP instance. If data arrives in the receive buffer, up to
1204 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1205 BodyLength with the amount of bytes received and copied to Body.
1207 If the HTTP driver does not have an open underlying TCP connection with the host
1208 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1209 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1210 an open TCP connection between client and host.
1212 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1213 @param[in] Token Pointer to storage containing HTTP response token.
1215 @retval EFI_SUCCESS Allocation succeeded.
1216 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1218 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1221 Token->Message->Headers is NULL.
1222 Token->Message is NULL.
1223 Token->Message->Body is not NULL,
1224 Token->Message->BodyLength is non-zero, and
1225 Token->Message->Data is NULL, but a previous call to
1226 Response() has not been completed successfully.
1227 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1228 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1229 specified by response URL.
1234 IN EFI_HTTP_PROTOCOL
*This
,
1235 IN EFI_HTTP_TOKEN
*Token
1239 EFI_HTTP_MESSAGE
*HttpMsg
;
1240 HTTP_PROTOCOL
*HttpInstance
;
1241 HTTP_TOKEN_WRAP
*Wrap
;
1243 if ((This
== NULL
) || (Token
== NULL
)) {
1244 return EFI_INVALID_PARAMETER
;
1247 HttpMsg
= Token
->Message
;
1248 if (HttpMsg
== NULL
) {
1249 return EFI_INVALID_PARAMETER
;
1252 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1253 ASSERT (HttpInstance
!= NULL
);
1255 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1256 return EFI_NOT_STARTED
;
1259 if (HttpInstance
->LocalAddressIsIPv6
) {
1260 return EFI_UNSUPPORTED
;
1264 // Check whether the token already existed.
1266 if (EFI_ERROR (NetMapIterate (&HttpInstance
->RxTokens
, HttpTokenExist
, Token
))) {
1267 return EFI_ACCESS_DENIED
;
1270 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
1272 return EFI_OUT_OF_RESOURCES
;
1275 Wrap
->HttpInstance
= HttpInstance
;
1276 Wrap
->HttpToken
= Token
;
1278 Status
= HttpCreateTcp4RxEvent (Wrap
);
1279 if (EFI_ERROR (Status
)) {
1283 Status
= NetMapInsertTail (&HttpInstance
->RxTokens
, Token
, Wrap
);
1284 if (EFI_ERROR (Status
)) {
1289 // If already have pending RxTokens, return directly.
1291 if (NetMapGetCount (&HttpInstance
->RxTokens
) > 1) {
1295 return HttpResponseWorker (Wrap
);
1299 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
1300 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
1309 The Poll() function can be used by network drivers and applications to increase the
1310 rate that data packets are moved between the communication devices and the transmit
1313 In some systems, the periodic timer event in the managed network driver may not poll
1314 the underlying communications device fast enough to transmit and/or receive all data
1315 packets without missing incoming packets or dropping outgoing packets. Drivers and
1316 applications that are experiencing packet loss should try calling the Poll() function
1319 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1321 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1322 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1323 @retval EFI_INVALID_PARAMETER This is NULL.
1324 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1325 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1331 IN EFI_HTTP_PROTOCOL
*This
1334 HTTP_PROTOCOL
*HttpInstance
;
1338 return EFI_INVALID_PARAMETER
;
1341 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1342 ASSERT (HttpInstance
!= NULL
);
1344 if (HttpInstance
->LocalAddressIsIPv6
) {
1345 return EFI_UNSUPPORTED
;
1348 if (HttpInstance
->Tcp4
== NULL
|| HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1349 return EFI_NOT_STARTED
;
1352 Status
= HttpInstance
->Tcp4
->Poll (HttpInstance
->Tcp4
);