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
;
285 // Parse the URI of the remote host.
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
)) {
508 NetMapRemoveTail (&HttpInstance
->TxTokens
, NULL
);
511 if (RequestStr
!= NULL
) {
512 FreePool (RequestStr
);
516 HttpCloseConnection (HttpInstance
);
520 HttpCloseTcp4ConnCloseEvent (HttpInstance
);
521 if (NULL
!= Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
) {
522 gBS
->CloseEvent (Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
);
523 Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
= NULL
;
527 if (HostName
!= NULL
) {
533 if (UrlParser
!= NULL
) {
534 HttpUrlFreeParser (UrlParser
);
542 Cancel a TxToken or RxToken.
544 @param[in] Map The HTTP instance's token queue.
545 @param[in] Item Object container for one HTTP token and token's wrap.
546 @param[in] Context The user's token to cancel.
548 @retval EFI_SUCCESS Continue to check the next Item.
549 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
556 IN NET_MAP_ITEM
*Item
,
561 EFI_HTTP_TOKEN
*Token
;
562 HTTP_TOKEN_WRAP
*Wrap
;
564 Token
= (EFI_HTTP_TOKEN
*) Context
;
567 // Return EFI_SUCCESS to check the next item in the map if
568 // this one doesn't match.
570 if ((Token
!= NULL
) && (Token
!= Item
->Key
)) {
574 Wrap
= (HTTP_TOKEN_WRAP
*) Item
->Value
;
575 ASSERT (Wrap
!= NULL
);
580 NetMapRemoveItem (Map
, Item
, NULL
);
582 if (Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
!= NULL
) {
583 gBS
->CloseEvent (Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
);
586 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
587 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
590 if (Wrap
->TcpWrap
.RxToken
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
591 FreePool (Wrap
->TcpWrap
.RxToken
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
597 // If only one item is to be cancel, return EFI_ABORTED to stop
598 // iterating the map any more.
608 Cancel the user's receive/transmit request. It is the worker function of
609 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the
612 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
613 @param[in] Token The token to cancel. If NULL, all token will be
616 @retval EFI_SUCCESS The token is cancelled.
617 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.
618 @retval Others Other error as indicated.
623 IN HTTP_PROTOCOL
*HttpInstance
,
624 IN EFI_HTTP_TOKEN
*Token
630 // First check the tokens queued by EfiHttpRequest().
632 Status
= NetMapIterate (&HttpInstance
->TxTokens
, HttpCancelTokens
, Token
);
633 if (EFI_ERROR (Status
)) {
635 if (Status
== EFI_ABORTED
) {
644 // Then check the tokens queued by EfiHttpResponse().
646 Status
= NetMapIterate (&HttpInstance
->RxTokens
, HttpCancelTokens
, Token
);
647 if (EFI_ERROR (Status
)) {
649 if (Status
== EFI_ABORTED
) {
652 return EFI_NOT_FOUND
;
664 Abort an asynchronous HTTP request or response token.
666 The Cancel() function aborts a pending HTTP request or response transaction. If
667 Token is not NULL and the token is in transmit or receive queues when it is being
668 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
669 be signaled. If the token is not in one of the queues, which usually means that the
670 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
671 all asynchronous tokens issued by Request() or Response() will be aborted.
673 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
674 @param[in] Token Point to storage containing HTTP request or response
677 @retval EFI_SUCCESS Request and Response queues are successfully flushed.
678 @retval EFI_INVALID_PARAMETER This is NULL.
679 @retval EFI_NOT_STARTED This instance hasn't been configured.
680 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP,
681 BOOTP, RARP, etc.) hasn't finished yet.
682 @retval EFI_NOT_FOUND The asynchronous request or response token is not
684 @retval EFI_UNSUPPORTED The implementation does not support this function.
690 IN EFI_HTTP_PROTOCOL
*This
,
691 IN EFI_HTTP_TOKEN
*Token
694 HTTP_PROTOCOL
*HttpInstance
;
697 return EFI_INVALID_PARAMETER
;
700 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
701 ASSERT (HttpInstance
!= NULL
);
703 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
704 return EFI_NOT_STARTED
;
707 return HttpCancel (HttpInstance
, Token
);
712 A callback function to intercept events during message parser.
714 This function will be invoked during HttpParseMessageBody() with various events type. An error
715 return status of the callback function will cause the HttpParseMessageBody() aborted.
717 @param[in] EventType Event type of this callback call.
718 @param[in] Data A pointer to data buffer.
719 @param[in] Length Length in bytes of the Data.
720 @param[in] Context Callback context set by HttpInitMsgParser().
722 @retval EFI_SUCCESS Continue to parser the message body.
727 HttpBodyParserCallback (
728 IN HTTP_BODY_PARSE_EVENT EventType
,
734 HTTP_TOKEN_WRAP
*Wrap
;
736 if (EventType
!= BodyParseEventOnComplete
) {
740 if (Data
== NULL
|| Length
!= 0 || Context
== NULL
) {
744 Wrap
= (HTTP_TOKEN_WRAP
*) Context
;
745 Wrap
->HttpInstance
->NextMsg
= Data
;
748 // Free TxToken since already received corrsponding HTTP response.
756 The work function of EfiHttpResponse().
758 @param[in] Wrap Pointer to HTTP token's wrap data.
760 @retval EFI_SUCCESS Allocation succeeded.
761 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
762 @retval EFI_NOT_READY Can't find a corresponding TxToken or
763 the EFI_HTTP_UTILITIES_PROTOCOL is not available.
768 IN HTTP_TOKEN_WRAP
*Wrap
772 EFI_HTTP_MESSAGE
*HttpMsg
;
773 EFI_TCP4_IO_TOKEN
*RxToken
;
774 EFI_TCP4_PROTOCOL
*Tcp4
;
783 CHAR8
*StatusCodeStr
;
785 HTTP_PROTOCOL
*HttpInstance
;
786 EFI_HTTP_TOKEN
*Token
;
788 HTTP_TOKEN_WRAP
*ValueInItem
;
791 if (Wrap
== NULL
|| Wrap
->HttpInstance
== NULL
) {
792 return EFI_INVALID_PARAMETER
;
795 HttpInstance
= Wrap
->HttpInstance
;
796 Token
= Wrap
->HttpToken
;
798 HttpMsg
= Token
->Message
;
800 Tcp4
= HttpInstance
->Tcp4
;
801 ASSERT (Tcp4
!= NULL
);
802 HttpMsg
->Headers
= NULL
;
809 if (HttpMsg
->Data
.Response
!= NULL
) {
811 // Need receive the HTTP headers, prepare buffer.
813 Status
= HttpCreateTcp4RxEventForHeader (HttpInstance
);
814 if (EFI_ERROR (Status
)) {
819 // Check whether we have cached header from previous call.
821 if ((HttpInstance
->CacheBody
!= NULL
) && (HttpInstance
->NextMsg
!= NULL
)) {
823 // The data is stored at [NextMsg, CacheBody + CacheLen].
825 HdrLen
= HttpInstance
->CacheBody
+ HttpInstance
->CacheLen
- HttpInstance
->NextMsg
;
826 HttpHeaders
= AllocateZeroPool (HdrLen
);
827 if (HttpHeaders
== NULL
) {
828 Status
= EFI_OUT_OF_RESOURCES
;
832 CopyMem (HttpHeaders
, HttpInstance
->NextMsg
, HdrLen
);
833 FreePool (HttpInstance
->CacheBody
);
834 HttpInstance
->CacheBody
= NULL
;
835 HttpInstance
->NextMsg
= NULL
;
836 HttpInstance
->CacheOffset
= 0;
837 SizeofHeaders
= HdrLen
;
838 BufferSize
= HttpInstance
->CacheLen
;
841 // Check whether we cached the whole HTTP headers.
843 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
846 RxToken
= &HttpInstance
->RxToken
;
847 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= AllocateZeroPool (DEF_BUF_LEN
);
848 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
== NULL
) {
849 Status
= EFI_OUT_OF_RESOURCES
;
854 // Receive the HTTP headers only when EFI_HTTP_RESPONSE_DATA is not NULL.
856 while (EndofHeader
== NULL
) {
857 HttpInstance
->IsRxDone
= FALSE
;
858 RxToken
->Packet
.RxData
->DataLength
= DEF_BUF_LEN
;
859 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
= DEF_BUF_LEN
;
860 Status
= Tcp4
->Receive (Tcp4
, RxToken
);
861 if (EFI_ERROR (Status
)) {
862 DEBUG ((EFI_D_ERROR
, "Tcp4 receive failed: %r\n", Status
));
866 while (!HttpInstance
->IsRxDone
) {
870 Status
= RxToken
->CompletionToken
.Status
;
871 if (EFI_ERROR (Status
)) {
876 // Append the response string.
878 BufferSize
= SizeofHeaders
+ RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
;
879 Buffer
= AllocateZeroPool (BufferSize
);
880 if (Buffer
== NULL
) {
881 Status
= EFI_OUT_OF_RESOURCES
;
885 if (HttpHeaders
!= NULL
) {
886 CopyMem (Buffer
, HttpHeaders
, SizeofHeaders
);
887 FreePool (HttpHeaders
);
891 Buffer
+ SizeofHeaders
,
892 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
,
893 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
895 HttpHeaders
= Buffer
;
896 SizeofHeaders
= BufferSize
;
899 // Check whether we received end of HTTP headers.
901 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
905 // Skip the CRLF after the HTTP headers.
907 EndofHeader
= EndofHeader
+ AsciiStrLen (HTTP_END_OF_HDR_STR
);
910 // Cache the part of body.
912 BodyLen
= BufferSize
- (EndofHeader
- HttpHeaders
);
914 if (HttpInstance
->CacheBody
!= NULL
) {
915 FreePool (HttpInstance
->CacheBody
);
918 HttpInstance
->CacheBody
= AllocateZeroPool (BodyLen
);
919 if (HttpInstance
->CacheBody
== NULL
) {
920 Status
= EFI_OUT_OF_RESOURCES
;
924 CopyMem (HttpInstance
->CacheBody
, EndofHeader
, BodyLen
);
925 HttpInstance
->CacheLen
= BodyLen
;
928 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
929 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
932 // Search for Status Code.
934 StatusCodeStr
= HttpHeaders
+ AsciiStrLen (HTTP_VERSION_STR
) + 1;
935 if (StatusCodeStr
== NULL
) {
939 StatusCode
= AsciiStrDecimalToUintn (StatusCodeStr
);
942 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
944 Tmp
= AsciiStrStr (HttpHeaders
, HTTP_CRLF_STR
);
949 Tmp
= Tmp
+ AsciiStrLen (HTTP_CRLF_STR
);
950 SizeofHeaders
= SizeofHeaders
- (Tmp
- HttpHeaders
);
951 HeaderTmp
= AllocateZeroPool (SizeofHeaders
);
952 if (HeaderTmp
== NULL
) {
956 CopyMem (HeaderTmp
, Tmp
, SizeofHeaders
);
957 FreePool (HttpHeaders
);
958 HttpHeaders
= HeaderTmp
;
961 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.
963 if (mHttpUtilities
== NULL
) {
964 Status
= EFI_NOT_READY
;
969 // Parse the HTTP header into array of key/value pairs.
971 Status
= mHttpUtilities
->Parse (
976 &HttpMsg
->HeaderCount
978 if (EFI_ERROR (Status
)) {
982 FreePool (HttpHeaders
);
985 HttpMsg
->Data
.Response
->StatusCode
= HttpMappingToStatusCode (StatusCode
);
988 // Init message-body parser by header information.
990 Status
= EFI_NOT_READY
;
992 NetMapRemoveHead (&HttpInstance
->TxTokens
, (VOID
**) &ValueInItem
);
993 if (ValueInItem
== NULL
) {
998 // The first TxToken not transmitted yet, insert back and return error.
1000 if (!ValueInItem
->TcpWrap
.IsTxDone
) {
1004 Status
= HttpInitMsgParser (
1005 ValueInItem
->TcpWrap
.Method
,
1006 HttpMsg
->Data
.Response
->StatusCode
,
1007 HttpMsg
->HeaderCount
,
1009 HttpBodyParserCallback
,
1010 (VOID
*) ValueInItem
,
1011 &HttpInstance
->MsgParser
1013 if (EFI_ERROR (Status
)) {
1018 // Check whether we received a complete HTTP message.
1020 if (HttpInstance
->CacheBody
!= NULL
) {
1021 Status
= HttpParseMessageBody (HttpInstance
->MsgParser
, HttpInstance
->CacheLen
, HttpInstance
->CacheBody
);
1022 if (EFI_ERROR (Status
)) {
1026 if (HttpIsMessageComplete (HttpInstance
->MsgParser
)) {
1028 // Free the MsgParse since we already have a full HTTP message.
1030 HttpFreeMsgParser (HttpInstance
->MsgParser
);
1031 HttpInstance
->MsgParser
= NULL
;
1035 if ((HttpMsg
->Body
== NULL
) || (HttpMsg
->BodyLength
== 0)) {
1036 Status
= EFI_SUCCESS
;
1042 // Receive the response body.
1047 // First check whether we cached some data.
1049 if (HttpInstance
->CacheBody
!= NULL
) {
1051 // Calculate the length of the cached data.
1053 if (HttpInstance
->NextMsg
!= NULL
) {
1055 // We have a cached HTTP message which includes a part of HTTP header of next message.
1057 BodyLen
= HttpInstance
->NextMsg
- (HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
);
1059 BodyLen
= HttpInstance
->CacheLen
- HttpInstance
->CacheOffset
;
1064 // We have some cached data. Just copy the data and return.
1066 if (HttpMsg
->BodyLength
< BodyLen
) {
1067 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, HttpMsg
->BodyLength
);
1068 HttpInstance
->CacheOffset
= HttpInstance
->CacheOffset
+ HttpMsg
->BodyLength
;
1071 // Copy all cached data out.
1073 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, BodyLen
);
1074 HttpInstance
->CacheOffset
= BodyLen
+ HttpInstance
->CacheOffset
;
1075 HttpMsg
->BodyLength
= BodyLen
;
1077 if (HttpInstance
->NextMsg
== NULL
) {
1079 // There is no HTTP header of next message. Just free the cache buffer.
1081 FreePool (HttpInstance
->CacheBody
);
1082 HttpInstance
->CacheBody
= NULL
;
1083 HttpInstance
->NextMsg
= NULL
;
1084 HttpInstance
->CacheOffset
= 0;
1088 // Return since we aready received required data.
1090 Status
= EFI_SUCCESS
;
1094 if (BodyLen
== 0 && HttpInstance
->MsgParser
== NULL
) {
1096 // We received a complete HTTP message, and we don't have more data to return to caller.
1098 HttpMsg
->BodyLength
= 0;
1099 Status
= EFI_SUCCESS
;
1104 ASSERT (HttpInstance
->MsgParser
!= NULL
);
1107 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1109 RxToken
= &Wrap
->TcpWrap
.RxToken
;
1111 RxToken
->Packet
.RxData
->DataLength
= (UINT32
) HttpMsg
->BodyLength
;
1112 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
= (UINT32
) HttpMsg
->BodyLength
;
1113 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= (VOID
*) HttpMsg
->Body
;
1115 RxToken
->CompletionToken
.Status
= EFI_NOT_READY
;
1116 Status
= Tcp4
->Receive (Tcp4
, RxToken
);
1117 if (EFI_ERROR (Status
)) {
1118 DEBUG ((EFI_D_ERROR
, "Tcp4 receive failed: %r\n", Status
));
1125 Item
= NetMapFindKey (&Wrap
->HttpInstance
->RxTokens
, Wrap
->HttpToken
);
1127 NetMapRemoveItem (&Wrap
->HttpInstance
->RxTokens
, Item
, NULL
);
1129 Token
->Status
= Status
;
1130 gBS
->SignalEvent (Token
->Event
);
1135 NetMapInsertHead (&HttpInstance
->TxTokens
, ValueInItem
->HttpToken
, ValueInItem
);
1139 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
1140 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
1142 RxToken
= &Wrap
->TcpWrap
.RxToken
;
1143 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
1144 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
1145 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
1150 if (HttpInstance
->RxToken
.CompletionToken
.Event
!= NULL
) {
1151 gBS
->CloseEvent (HttpInstance
->RxToken
.CompletionToken
.Event
);
1152 HttpInstance
->RxToken
.CompletionToken
.Event
= NULL
;
1155 RxToken
= &HttpInstance
->RxToken
;
1156 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
1157 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
1158 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
1161 if (HttpHeaders
!= NULL
) {
1162 FreePool (HttpHeaders
);
1165 if (HttpMsg
->Headers
!= NULL
) {
1166 FreePool (HttpMsg
->Headers
);
1169 if (HttpInstance
->CacheBody
!= NULL
) {
1170 FreePool (HttpInstance
->CacheBody
);
1171 HttpInstance
->CacheBody
= NULL
;
1174 Token
->Status
= Status
;
1175 gBS
->SignalEvent (Token
->Event
);
1183 The Response() function queues an HTTP response to this HTTP instance, similar to
1184 Receive() function in the EFI TCP driver. When the HTTP request is sent successfully,
1185 or if there is an error, Status in token will be updated and Event will be signaled.
1187 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1188 is received in the underlying TCP instance, the data will be parsed and Token will
1189 be populated with the response data. If the data received from the remote host
1190 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1191 (asynchronously) for more data to be sent from the remote host before signaling
1194 It is the responsibility of the caller to allocate a buffer for Body and specify the
1195 size in BodyLength. If the remote host provides a response that contains a content
1196 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1197 BodyLength will be updated with the amount of bytes received and copied to Body. This
1198 allows the client to download a large file in chunks instead of into one contiguous
1199 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1200 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1201 token to underlying TCP instance. If data arrives in the receive buffer, up to
1202 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1203 BodyLength with the amount of bytes received and copied to Body.
1205 If the HTTP driver does not have an open underlying TCP connection with the host
1206 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1207 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1208 an open TCP connection between client and host.
1210 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1211 @param[in] Token Pointer to storage containing HTTP response token.
1213 @retval EFI_SUCCESS Allocation succeeded.
1214 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1216 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1219 Token->Message->Headers is NULL.
1220 Token->Message is NULL.
1221 Token->Message->Body is not NULL,
1222 Token->Message->BodyLength is non-zero, and
1223 Token->Message->Data is NULL, but a previous call to
1224 Response() has not been completed successfully.
1225 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1226 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1227 specified by response URL.
1232 IN EFI_HTTP_PROTOCOL
*This
,
1233 IN EFI_HTTP_TOKEN
*Token
1237 EFI_HTTP_MESSAGE
*HttpMsg
;
1238 HTTP_PROTOCOL
*HttpInstance
;
1239 HTTP_TOKEN_WRAP
*Wrap
;
1241 if ((This
== NULL
) || (Token
== NULL
)) {
1242 return EFI_INVALID_PARAMETER
;
1245 HttpMsg
= Token
->Message
;
1246 if (HttpMsg
== NULL
) {
1247 return EFI_INVALID_PARAMETER
;
1250 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1251 ASSERT (HttpInstance
!= NULL
);
1253 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1254 return EFI_NOT_STARTED
;
1257 if (HttpInstance
->LocalAddressIsIPv6
) {
1258 return EFI_UNSUPPORTED
;
1262 // Check whether the token already existed.
1264 if (EFI_ERROR (NetMapIterate (&HttpInstance
->RxTokens
, HttpTokenExist
, Token
))) {
1265 return EFI_ACCESS_DENIED
;
1268 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
1270 return EFI_OUT_OF_RESOURCES
;
1273 Wrap
->HttpInstance
= HttpInstance
;
1274 Wrap
->HttpToken
= Token
;
1276 Status
= HttpCreateTcp4RxEvent (Wrap
);
1277 if (EFI_ERROR (Status
)) {
1281 Status
= NetMapInsertTail (&HttpInstance
->RxTokens
, Token
, Wrap
);
1282 if (EFI_ERROR (Status
)) {
1287 // If already have pending RxTokens, return directly.
1289 if (NetMapGetCount (&HttpInstance
->RxTokens
) > 1) {
1293 return HttpResponseWorker (Wrap
);
1297 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
1298 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
1307 The Poll() function can be used by network drivers and applications to increase the
1308 rate that data packets are moved between the communication devices and the transmit
1311 In some systems, the periodic timer event in the managed network driver may not poll
1312 the underlying communications device fast enough to transmit and/or receive all data
1313 packets without missing incoming packets or dropping outgoing packets. Drivers and
1314 applications that are experiencing packet loss should try calling the Poll() function
1317 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1319 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1320 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1321 @retval EFI_INVALID_PARAMETER This is NULL.
1322 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1323 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1329 IN EFI_HTTP_PROTOCOL
*This
1332 HTTP_PROTOCOL
*HttpInstance
;
1335 return EFI_INVALID_PARAMETER
;
1338 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1339 ASSERT (HttpInstance
!= NULL
);
1341 if (HttpInstance
->LocalAddressIsIPv6
) {
1342 return EFI_UNSUPPORTED
;
1345 if (HttpInstance
->Tcp4
== NULL
|| HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1346 return EFI_NOT_STARTED
;
1349 return HttpInstance
->Tcp4
->Poll (HttpInstance
->Tcp4
);