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
) {
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
;
376 HttpInstance
->RemotePort
= 0;
383 // Parse Url for IPv4 address, if failed, perform DNS resolution.
385 Status
= NetLibAsciiStrToIp4 (HostName
, &HttpInstance
->RemoteAddr
);
386 if (EFI_ERROR (Status
)) {
387 HostNameStr
= AllocateZeroPool ((AsciiStrLen (HostName
) + 1) * sizeof (UINT16
));
388 if (HostNameStr
== NULL
) {
389 Status
= EFI_OUT_OF_RESOURCES
;
393 AsciiStrToUnicodeStr (HostName
, HostNameStr
);
394 Status
= HttpDns4 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteAddr
);
395 FreePool (HostNameStr
);
396 if (EFI_ERROR (Status
)) {
402 // Save the RemotePort and RemoteHost.
404 ASSERT (HttpInstance
->RemoteHost
== NULL
);
405 HttpInstance
->RemotePort
= RemotePort
;
406 HttpInstance
->RemoteHost
= HostName
;
412 // The request URL is different from previous calls to Request(), close existing TCP instance.
414 ASSERT (HttpInstance
->Tcp4
!= NULL
);
415 HttpCloseConnection (HttpInstance
);
416 EfiHttpCancel (This
, NULL
);
420 // Wrap the HTTP token in HTTP_TOKEN_WRAP
422 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
424 Status
= EFI_OUT_OF_RESOURCES
;
428 Wrap
->HttpToken
= Token
;
429 Wrap
->HttpInstance
= HttpInstance
;
430 Wrap
->TcpWrap
.Method
= Request
->Method
;
434 // Configure TCP instance.
436 Status
= HttpConfigureTcp4 (HttpInstance
, Wrap
);
437 if (EFI_ERROR (Status
)) {
443 Status
= HttpConnectTcp4 (HttpInstance
);
444 if (EFI_ERROR (Status
)) {
449 // For the new HTTP token, create TX TCP token events.
451 Status
= HttpCreateTcp4TxEvent (Wrap
);
452 if (EFI_ERROR (Status
)) {
458 // Create request message.
461 if (*FileUrl
!= '/') {
463 // Convert the absolute-URI to the absolute-path
465 while (*FileUrl
!= ':') {
468 if ((*(FileUrl
+1) == '/') && (*(FileUrl
+2) == '/')) {
470 while (*FileUrl
!= '/') {
474 Status
= EFI_INVALID_PARAMETER
;
478 RequestStr
= HttpGenRequestString (HttpInstance
, HttpMsg
, FileUrl
);
479 if (RequestStr
== NULL
) {
480 Status
= EFI_OUT_OF_RESOURCES
;
484 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
485 if (EFI_ERROR (Status
)) {
489 if (HostName
!= NULL
) {
494 // Transmit the request message.
496 Status
= HttpTransmitTcp4 (
500 AsciiStrLen (RequestStr
)
502 if (EFI_ERROR (Status
)) {
511 NetMapRemoveTail (&HttpInstance
->TxTokens
, NULL
);
514 if (RequestStr
!= NULL
) {
515 FreePool (RequestStr
);
519 HttpCloseConnection (HttpInstance
);
523 HttpCloseTcp4ConnCloseEvent (HttpInstance
);
524 if (NULL
!= Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
) {
525 gBS
->CloseEvent (Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
);
526 Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
= NULL
;
530 if (HostName
!= NULL
) {
536 if (UrlParser
!= NULL
) {
537 HttpUrlFreeParser (UrlParser
);
545 Cancel a TxToken or RxToken.
547 @param[in] Map The HTTP instance's token queue.
548 @param[in] Item Object container for one HTTP token and token's wrap.
549 @param[in] Context The user's token to cancel.
551 @retval EFI_SUCCESS Continue to check the next Item.
552 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
559 IN NET_MAP_ITEM
*Item
,
564 EFI_HTTP_TOKEN
*Token
;
565 HTTP_TOKEN_WRAP
*Wrap
;
567 Token
= (EFI_HTTP_TOKEN
*) Context
;
570 // Return EFI_SUCCESS to check the next item in the map if
571 // this one doesn't match.
573 if ((Token
!= NULL
) && (Token
!= Item
->Key
)) {
577 Wrap
= (HTTP_TOKEN_WRAP
*) Item
->Value
;
578 ASSERT (Wrap
!= NULL
);
583 NetMapRemoveItem (Map
, Item
, NULL
);
585 if (Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
!= NULL
) {
586 gBS
->CloseEvent (Wrap
->TcpWrap
.TxToken
.CompletionToken
.Event
);
589 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
590 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
593 if (Wrap
->TcpWrap
.RxToken
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
594 FreePool (Wrap
->TcpWrap
.RxToken
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
600 // If only one item is to be cancel, return EFI_ABORTED to stop
601 // iterating the map any more.
611 Cancel the user's receive/transmit request. It is the worker function of
612 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the
615 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
616 @param[in] Token The token to cancel. If NULL, all token will be
619 @retval EFI_SUCCESS The token is cancelled.
620 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.
621 @retval Others Other error as indicated.
626 IN HTTP_PROTOCOL
*HttpInstance
,
627 IN EFI_HTTP_TOKEN
*Token
633 // First check the tokens queued by EfiHttpRequest().
635 Status
= NetMapIterate (&HttpInstance
->TxTokens
, HttpCancelTokens
, Token
);
636 if (EFI_ERROR (Status
)) {
638 if (Status
== EFI_ABORTED
) {
647 // Then check the tokens queued by EfiHttpResponse().
649 Status
= NetMapIterate (&HttpInstance
->RxTokens
, HttpCancelTokens
, Token
);
650 if (EFI_ERROR (Status
)) {
652 if (Status
== EFI_ABORTED
) {
655 return EFI_NOT_FOUND
;
667 Abort an asynchronous HTTP request or response token.
669 The Cancel() function aborts a pending HTTP request or response transaction. If
670 Token is not NULL and the token is in transmit or receive queues when it is being
671 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
672 be signaled. If the token is not in one of the queues, which usually means that the
673 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
674 all asynchronous tokens issued by Request() or Response() will be aborted.
676 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
677 @param[in] Token Point to storage containing HTTP request or response
680 @retval EFI_SUCCESS Request and Response queues are successfully flushed.
681 @retval EFI_INVALID_PARAMETER This is NULL.
682 @retval EFI_NOT_STARTED This instance hasn't been configured.
683 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP,
684 BOOTP, RARP, etc.) hasn't finished yet.
685 @retval EFI_NOT_FOUND The asynchronous request or response token is not
687 @retval EFI_UNSUPPORTED The implementation does not support this function.
693 IN EFI_HTTP_PROTOCOL
*This
,
694 IN EFI_HTTP_TOKEN
*Token
697 HTTP_PROTOCOL
*HttpInstance
;
700 return EFI_INVALID_PARAMETER
;
703 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
704 ASSERT (HttpInstance
!= NULL
);
706 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
707 return EFI_NOT_STARTED
;
710 return HttpCancel (HttpInstance
, Token
);
715 A callback function to intercept events during message parser.
717 This function will be invoked during HttpParseMessageBody() with various events type. An error
718 return status of the callback function will cause the HttpParseMessageBody() aborted.
720 @param[in] EventType Event type of this callback call.
721 @param[in] Data A pointer to data buffer.
722 @param[in] Length Length in bytes of the Data.
723 @param[in] Context Callback context set by HttpInitMsgParser().
725 @retval EFI_SUCCESS Continue to parser the message body.
730 HttpBodyParserCallback (
731 IN HTTP_BODY_PARSE_EVENT EventType
,
737 HTTP_TOKEN_WRAP
*Wrap
;
739 if (EventType
!= BodyParseEventOnComplete
) {
743 if (Data
== NULL
|| Length
!= 0 || Context
== NULL
) {
747 Wrap
= (HTTP_TOKEN_WRAP
*) Context
;
748 Wrap
->HttpInstance
->NextMsg
= Data
;
751 // Free TxToken since already received corrsponding HTTP response.
759 The work function of EfiHttpResponse().
761 @param[in] Wrap Pointer to HTTP token's wrap data.
763 @retval EFI_SUCCESS Allocation succeeded.
764 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
765 @retval EFI_NOT_READY Can't find a corresponding TxToken or
766 the EFI_HTTP_UTILITIES_PROTOCOL is not available.
771 IN HTTP_TOKEN_WRAP
*Wrap
775 EFI_HTTP_MESSAGE
*HttpMsg
;
776 EFI_TCP4_IO_TOKEN
*RxToken
;
777 EFI_TCP4_PROTOCOL
*Tcp4
;
786 CHAR8
*StatusCodeStr
;
788 HTTP_PROTOCOL
*HttpInstance
;
789 EFI_HTTP_TOKEN
*Token
;
791 HTTP_TOKEN_WRAP
*ValueInItem
;
794 if (Wrap
== NULL
|| Wrap
->HttpInstance
== NULL
) {
795 return EFI_INVALID_PARAMETER
;
798 HttpInstance
= Wrap
->HttpInstance
;
799 Token
= Wrap
->HttpToken
;
801 HttpMsg
= Token
->Message
;
803 Tcp4
= HttpInstance
->Tcp4
;
804 ASSERT (Tcp4
!= NULL
);
805 HttpMsg
->Headers
= NULL
;
812 if (HttpMsg
->Data
.Response
!= NULL
) {
814 // Need receive the HTTP headers, prepare buffer.
816 Status
= HttpCreateTcp4RxEventForHeader (HttpInstance
);
817 if (EFI_ERROR (Status
)) {
822 // Check whether we have cached header from previous call.
824 if ((HttpInstance
->CacheBody
!= NULL
) && (HttpInstance
->NextMsg
!= NULL
)) {
826 // The data is stored at [NextMsg, CacheBody + CacheLen].
828 HdrLen
= HttpInstance
->CacheBody
+ HttpInstance
->CacheLen
- HttpInstance
->NextMsg
;
829 HttpHeaders
= AllocateZeroPool (HdrLen
);
830 if (HttpHeaders
== NULL
) {
831 Status
= EFI_OUT_OF_RESOURCES
;
835 CopyMem (HttpHeaders
, HttpInstance
->NextMsg
, HdrLen
);
836 FreePool (HttpInstance
->CacheBody
);
837 HttpInstance
->CacheBody
= NULL
;
838 HttpInstance
->NextMsg
= NULL
;
839 HttpInstance
->CacheOffset
= 0;
840 SizeofHeaders
= HdrLen
;
841 BufferSize
= HttpInstance
->CacheLen
;
844 // Check whether we cached the whole HTTP headers.
846 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
849 RxToken
= &HttpInstance
->RxToken
;
850 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= AllocateZeroPool (DEF_BUF_LEN
);
851 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
== NULL
) {
852 Status
= EFI_OUT_OF_RESOURCES
;
857 // Receive the HTTP headers only when EFI_HTTP_RESPONSE_DATA is not NULL.
859 while (EndofHeader
== NULL
) {
860 HttpInstance
->IsRxDone
= FALSE
;
861 RxToken
->Packet
.RxData
->DataLength
= DEF_BUF_LEN
;
862 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
= DEF_BUF_LEN
;
863 Status
= Tcp4
->Receive (Tcp4
, RxToken
);
864 if (EFI_ERROR (Status
)) {
865 DEBUG ((EFI_D_ERROR
, "Tcp4 receive failed: %r\n", Status
));
869 while (!HttpInstance
->IsRxDone
) {
873 Status
= RxToken
->CompletionToken
.Status
;
874 if (EFI_ERROR (Status
)) {
879 // Append the response string.
881 BufferSize
= SizeofHeaders
+ RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
;
882 Buffer
= AllocateZeroPool (BufferSize
);
883 if (Buffer
== NULL
) {
884 Status
= EFI_OUT_OF_RESOURCES
;
888 if (HttpHeaders
!= NULL
) {
889 CopyMem (Buffer
, HttpHeaders
, SizeofHeaders
);
890 FreePool (HttpHeaders
);
894 Buffer
+ SizeofHeaders
,
895 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
,
896 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
898 HttpHeaders
= Buffer
;
899 SizeofHeaders
= BufferSize
;
902 // Check whether we received end of HTTP headers.
904 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
908 // Skip the CRLF after the HTTP headers.
910 EndofHeader
= EndofHeader
+ AsciiStrLen (HTTP_END_OF_HDR_STR
);
913 // Cache the part of body.
915 BodyLen
= BufferSize
- (EndofHeader
- HttpHeaders
);
917 if (HttpInstance
->CacheBody
!= NULL
) {
918 FreePool (HttpInstance
->CacheBody
);
921 HttpInstance
->CacheBody
= AllocateZeroPool (BodyLen
);
922 if (HttpInstance
->CacheBody
== NULL
) {
923 Status
= EFI_OUT_OF_RESOURCES
;
927 CopyMem (HttpInstance
->CacheBody
, EndofHeader
, BodyLen
);
928 HttpInstance
->CacheLen
= BodyLen
;
931 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
932 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
935 // Search for Status Code.
937 StatusCodeStr
= HttpHeaders
+ AsciiStrLen (HTTP_VERSION_STR
) + 1;
938 if (StatusCodeStr
== NULL
) {
942 StatusCode
= AsciiStrDecimalToUintn (StatusCodeStr
);
945 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
947 Tmp
= AsciiStrStr (HttpHeaders
, HTTP_CRLF_STR
);
952 Tmp
= Tmp
+ AsciiStrLen (HTTP_CRLF_STR
);
953 SizeofHeaders
= SizeofHeaders
- (Tmp
- HttpHeaders
);
954 HeaderTmp
= AllocateZeroPool (SizeofHeaders
);
955 if (HeaderTmp
== NULL
) {
959 CopyMem (HeaderTmp
, Tmp
, SizeofHeaders
);
960 FreePool (HttpHeaders
);
961 HttpHeaders
= HeaderTmp
;
964 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.
966 if (mHttpUtilities
== NULL
) {
967 Status
= EFI_NOT_READY
;
972 // Parse the HTTP header into array of key/value pairs.
974 Status
= mHttpUtilities
->Parse (
979 &HttpMsg
->HeaderCount
981 if (EFI_ERROR (Status
)) {
985 FreePool (HttpHeaders
);
988 HttpMsg
->Data
.Response
->StatusCode
= HttpMappingToStatusCode (StatusCode
);
991 // Init message-body parser by header information.
993 Status
= EFI_NOT_READY
;
995 NetMapRemoveHead (&HttpInstance
->TxTokens
, (VOID
**) &ValueInItem
);
996 if (ValueInItem
== NULL
) {
1001 // The first TxToken not transmitted yet, insert back and return error.
1003 if (!ValueInItem
->TcpWrap
.IsTxDone
) {
1007 Status
= HttpInitMsgParser (
1008 ValueInItem
->TcpWrap
.Method
,
1009 HttpMsg
->Data
.Response
->StatusCode
,
1010 HttpMsg
->HeaderCount
,
1012 HttpBodyParserCallback
,
1013 (VOID
*) ValueInItem
,
1014 &HttpInstance
->MsgParser
1016 if (EFI_ERROR (Status
)) {
1021 // Check whether we received a complete HTTP message.
1023 if (HttpInstance
->CacheBody
!= NULL
) {
1024 Status
= HttpParseMessageBody (HttpInstance
->MsgParser
, HttpInstance
->CacheLen
, HttpInstance
->CacheBody
);
1025 if (EFI_ERROR (Status
)) {
1029 if (HttpIsMessageComplete (HttpInstance
->MsgParser
)) {
1031 // Free the MsgParse since we already have a full HTTP message.
1033 HttpFreeMsgParser (HttpInstance
->MsgParser
);
1034 HttpInstance
->MsgParser
= NULL
;
1038 if ((HttpMsg
->Body
== NULL
) || (HttpMsg
->BodyLength
== 0)) {
1039 Status
= EFI_SUCCESS
;
1045 // Receive the response body.
1050 // First check whether we cached some data.
1052 if (HttpInstance
->CacheBody
!= NULL
) {
1054 // Calculate the length of the cached data.
1056 if (HttpInstance
->NextMsg
!= NULL
) {
1058 // We have a cached HTTP message which includes a part of HTTP header of next message.
1060 BodyLen
= HttpInstance
->NextMsg
- (HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
);
1062 BodyLen
= HttpInstance
->CacheLen
- HttpInstance
->CacheOffset
;
1067 // We have some cached data. Just copy the data and return.
1069 if (HttpMsg
->BodyLength
< BodyLen
) {
1070 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, HttpMsg
->BodyLength
);
1071 HttpInstance
->CacheOffset
= HttpInstance
->CacheOffset
+ HttpMsg
->BodyLength
;
1074 // Copy all cached data out.
1076 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, BodyLen
);
1077 HttpInstance
->CacheOffset
= BodyLen
+ HttpInstance
->CacheOffset
;
1078 HttpMsg
->BodyLength
= BodyLen
;
1080 if (HttpInstance
->NextMsg
== NULL
) {
1082 // There is no HTTP header of next message. Just free the cache buffer.
1084 FreePool (HttpInstance
->CacheBody
);
1085 HttpInstance
->CacheBody
= NULL
;
1086 HttpInstance
->NextMsg
= NULL
;
1087 HttpInstance
->CacheOffset
= 0;
1091 // Return since we aready received required data.
1093 Status
= EFI_SUCCESS
;
1097 if (BodyLen
== 0 && HttpInstance
->MsgParser
== NULL
) {
1099 // We received a complete HTTP message, and we don't have more data to return to caller.
1101 HttpMsg
->BodyLength
= 0;
1102 Status
= EFI_SUCCESS
;
1107 ASSERT (HttpInstance
->MsgParser
!= NULL
);
1110 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1112 RxToken
= &Wrap
->TcpWrap
.RxToken
;
1114 RxToken
->Packet
.RxData
->DataLength
= (UINT32
) HttpMsg
->BodyLength
;
1115 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentLength
= (UINT32
) HttpMsg
->BodyLength
;
1116 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= (VOID
*) HttpMsg
->Body
;
1118 RxToken
->CompletionToken
.Status
= EFI_NOT_READY
;
1119 Status
= Tcp4
->Receive (Tcp4
, RxToken
);
1120 if (EFI_ERROR (Status
)) {
1121 DEBUG ((EFI_D_ERROR
, "Tcp4 receive failed: %r\n", Status
));
1128 Item
= NetMapFindKey (&Wrap
->HttpInstance
->RxTokens
, Wrap
->HttpToken
);
1130 NetMapRemoveItem (&Wrap
->HttpInstance
->RxTokens
, Item
, NULL
);
1132 Token
->Status
= Status
;
1133 gBS
->SignalEvent (Token
->Event
);
1138 NetMapInsertHead (&HttpInstance
->TxTokens
, ValueInItem
->HttpToken
, ValueInItem
);
1142 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
1143 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
1145 RxToken
= &Wrap
->TcpWrap
.RxToken
;
1146 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
1147 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
1148 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
1153 if (HttpInstance
->RxToken
.CompletionToken
.Event
!= NULL
) {
1154 gBS
->CloseEvent (HttpInstance
->RxToken
.CompletionToken
.Event
);
1155 HttpInstance
->RxToken
.CompletionToken
.Event
= NULL
;
1158 RxToken
= &HttpInstance
->RxToken
;
1159 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
1160 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
1161 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
1164 if (HttpHeaders
!= NULL
) {
1165 FreePool (HttpHeaders
);
1168 if (HttpMsg
->Headers
!= NULL
) {
1169 FreePool (HttpMsg
->Headers
);
1172 if (HttpInstance
->CacheBody
!= NULL
) {
1173 FreePool (HttpInstance
->CacheBody
);
1174 HttpInstance
->CacheBody
= NULL
;
1177 Token
->Status
= Status
;
1178 gBS
->SignalEvent (Token
->Event
);
1186 The Response() function queues an HTTP response to this HTTP instance, similar to
1187 Receive() function in the EFI TCP driver. When the HTTP request is sent successfully,
1188 or if there is an error, Status in token will be updated and Event will be signaled.
1190 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1191 is received in the underlying TCP instance, the data will be parsed and Token will
1192 be populated with the response data. If the data received from the remote host
1193 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1194 (asynchronously) for more data to be sent from the remote host before signaling
1197 It is the responsibility of the caller to allocate a buffer for Body and specify the
1198 size in BodyLength. If the remote host provides a response that contains a content
1199 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1200 BodyLength will be updated with the amount of bytes received and copied to Body. This
1201 allows the client to download a large file in chunks instead of into one contiguous
1202 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1203 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1204 token to underlying TCP instance. If data arrives in the receive buffer, up to
1205 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1206 BodyLength with the amount of bytes received and copied to Body.
1208 If the HTTP driver does not have an open underlying TCP connection with the host
1209 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1210 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1211 an open TCP connection between client and host.
1213 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1214 @param[in] Token Pointer to storage containing HTTP response token.
1216 @retval EFI_SUCCESS Allocation succeeded.
1217 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1219 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1222 Token->Message->Headers is NULL.
1223 Token->Message is NULL.
1224 Token->Message->Body is not NULL,
1225 Token->Message->BodyLength is non-zero, and
1226 Token->Message->Data is NULL, but a previous call to
1227 Response() has not been completed successfully.
1228 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1229 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1230 specified by response URL.
1235 IN EFI_HTTP_PROTOCOL
*This
,
1236 IN EFI_HTTP_TOKEN
*Token
1240 EFI_HTTP_MESSAGE
*HttpMsg
;
1241 HTTP_PROTOCOL
*HttpInstance
;
1242 HTTP_TOKEN_WRAP
*Wrap
;
1244 if ((This
== NULL
) || (Token
== NULL
)) {
1245 return EFI_INVALID_PARAMETER
;
1248 HttpMsg
= Token
->Message
;
1249 if (HttpMsg
== NULL
) {
1250 return EFI_INVALID_PARAMETER
;
1253 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1254 ASSERT (HttpInstance
!= NULL
);
1256 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1257 return EFI_NOT_STARTED
;
1260 if (HttpInstance
->LocalAddressIsIPv6
) {
1261 return EFI_UNSUPPORTED
;
1265 // Check whether the token already existed.
1267 if (EFI_ERROR (NetMapIterate (&HttpInstance
->RxTokens
, HttpTokenExist
, Token
))) {
1268 return EFI_ACCESS_DENIED
;
1271 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
1273 return EFI_OUT_OF_RESOURCES
;
1276 Wrap
->HttpInstance
= HttpInstance
;
1277 Wrap
->HttpToken
= Token
;
1279 Status
= HttpCreateTcp4RxEvent (Wrap
);
1280 if (EFI_ERROR (Status
)) {
1284 Status
= NetMapInsertTail (&HttpInstance
->RxTokens
, Token
, Wrap
);
1285 if (EFI_ERROR (Status
)) {
1290 // If already have pending RxTokens, return directly.
1292 if (NetMapGetCount (&HttpInstance
->RxTokens
) > 1) {
1296 return HttpResponseWorker (Wrap
);
1300 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
1301 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
1310 The Poll() function can be used by network drivers and applications to increase the
1311 rate that data packets are moved between the communication devices and the transmit
1314 In some systems, the periodic timer event in the managed network driver may not poll
1315 the underlying communications device fast enough to transmit and/or receive all data
1316 packets without missing incoming packets or dropping outgoing packets. Drivers and
1317 applications that are experiencing packet loss should try calling the Poll() function
1320 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1322 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1323 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1324 @retval EFI_INVALID_PARAMETER This is NULL.
1325 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1326 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1332 IN EFI_HTTP_PROTOCOL
*This
1335 HTTP_PROTOCOL
*HttpInstance
;
1339 return EFI_INVALID_PARAMETER
;
1342 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1343 ASSERT (HttpInstance
!= NULL
);
1345 if (HttpInstance
->LocalAddressIsIPv6
) {
1346 return EFI_UNSUPPORTED
;
1349 if (HttpInstance
->Tcp4
== NULL
|| HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1350 return EFI_NOT_STARTED
;
1353 Status
= HttpInstance
->Tcp4
->Poll (HttpInstance
->Tcp4
);