2 Implementation of EFI_HTTP_PROTOCOL protocol interfaces.
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "HttpDriver.h"
19 EFI_HTTP_PROTOCOL mEfiHttpTemplate
= {
29 Returns the operational parameters for the current HTTP child instance.
31 The GetModeData() function is used to read the current mode data (operational
32 parameters) for this HTTP protocol instance.
34 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
35 @param[out] HttpConfigData Point to buffer for operational parameters of this
38 @retval EFI_SUCCESS Operation succeeded.
39 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
41 HttpConfigData is NULL.
42 HttpConfigData->AccessPoint is NULL.
43 @retval EFI_NOT_STARTED The HTTP instance is not configured.
49 IN EFI_HTTP_PROTOCOL
*This
,
50 OUT EFI_HTTP_CONFIG_DATA
*HttpConfigData
53 HTTP_PROTOCOL
*HttpInstance
;
55 if ((This
== NULL
) || (HttpConfigData
== NULL
)) {
56 return EFI_INVALID_PARAMETER
;
59 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
60 ASSERT (HttpInstance
!= NULL
);
62 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
63 return EFI_NOT_STARTED
;
66 if (HttpConfigData
->AccessPoint
.IPv4Node
== NULL
) {
67 return EFI_INVALID_PARAMETER
;
70 HttpConfigData
->HttpVersion
= HttpInstance
->HttpVersion
;
71 HttpConfigData
->TimeOutMillisec
= HttpInstance
->TimeOutMillisec
;
72 HttpConfigData
->LocalAddressIsIPv6
= HttpInstance
->LocalAddressIsIPv6
;
75 HttpConfigData
->AccessPoint
.IPv4Node
,
76 &HttpInstance
->IPv4Node
,
77 sizeof (HttpInstance
->IPv4Node
)
84 Initialize or brutally reset the operational parameters for this EFI HTTP instance.
86 The Configure() function does the following:
87 When HttpConfigData is not NULL Initialize this EFI HTTP instance by configuring
88 timeout, local address, port, etc.
89 When HttpConfigData is NULL, reset this EFI HTTP instance by closing all active
90 connections with remote hosts, canceling all asynchronous tokens, and flush request
91 and response buffers without informing the appropriate hosts.
93 Except for GetModeData() and Configure(), No other EFI HTTP function can be executed
94 by this instance until the Configure() function is executed and returns successfully.
96 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
97 @param[in] HttpConfigData Pointer to the configure data to configure the instance.
99 @retval EFI_SUCCESS Operation succeeded.
100 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
102 HttpConfigData->LocalAddressIsIPv6 is FALSE and
103 HttpConfigData->IPv4Node is NULL.
104 HttpConfigData->LocalAddressIsIPv6 is TRUE and
105 HttpConfigData->IPv6Node is NULL.
106 @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling
107 Configure() with NULL to reset it.
108 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
109 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when
110 executing Configure().
111 @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported
112 in the implementation.
117 IN EFI_HTTP_PROTOCOL
*This
,
118 IN EFI_HTTP_CONFIG_DATA
*HttpConfigData
121 HTTP_PROTOCOL
*HttpInstance
;
125 return EFI_INVALID_PARAMETER
;
128 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
129 ASSERT (HttpInstance
!= NULL
&& HttpInstance
->Service
!= NULL
);
131 if (HttpConfigData
!= NULL
) {
133 // Check input parameters.
135 if (HttpConfigData
->LocalAddressIsIPv6
) {
136 if (HttpConfigData
->AccessPoint
.IPv6Node
== NULL
) {
137 return EFI_INVALID_PARAMETER
;
140 if (HttpConfigData
->AccessPoint
.IPv4Node
== NULL
) {
141 return EFI_INVALID_PARAMETER
;
145 // Now configure this HTTP instance.
147 if (HttpInstance
->State
!= HTTP_STATE_UNCONFIGED
) {
148 return EFI_ALREADY_STARTED
;
151 HttpInstance
->HttpVersion
= HttpConfigData
->HttpVersion
;
152 HttpInstance
->TimeOutMillisec
= HttpConfigData
->TimeOutMillisec
;
153 HttpInstance
->LocalAddressIsIPv6
= HttpConfigData
->LocalAddressIsIPv6
;
155 if (HttpConfigData
->LocalAddressIsIPv6
) {
156 return EFI_UNSUPPORTED
;
159 &HttpInstance
->IPv4Node
,
160 HttpConfigData
->AccessPoint
.IPv4Node
,
161 sizeof (HttpInstance
->IPv4Node
)
164 HttpInstance
->State
= HTTP_STATE_HTTP_CONFIGED
;
169 if (HttpInstance
->LocalAddressIsIPv6
) {
170 return EFI_UNSUPPORTED
;
172 HttpCleanProtocol (HttpInstance
);
173 Status
= HttpInitProtocol (HttpInstance
->Service
, HttpInstance
);
174 if (EFI_ERROR (Status
)) {
178 HttpInstance
->State
= HTTP_STATE_UNCONFIGED
;
186 The Request() function queues an HTTP request to this HTTP instance.
188 Similar to Transmit() function in the EFI TCP driver. When the HTTP request is sent
189 successfully, or if there is an error, Status in token will be updated and Event will
192 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
193 @param[in] Token Pointer to storage containing HTTP request token.
195 @retval EFI_SUCCESS Outgoing data was processed.
196 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
197 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
198 @retval EFI_TIMEOUT Data was dropped out of the transmit or receive queue.
199 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
200 @retval EFI_UNSUPPORTED The HTTP method is not supported in current
202 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
204 Token->Message is NULL.
205 Token->Message->Body is not NULL,
206 Token->Message->BodyLength is non-zero, and
207 Token->Message->Data is NULL, but a previous call to
208 Request()has not been completed successfully.
213 IN EFI_HTTP_PROTOCOL
*This
,
214 IN EFI_HTTP_TOKEN
*Token
217 EFI_HTTP_MESSAGE
*HttpMsg
;
218 EFI_HTTP_REQUEST_DATA
*Request
;
223 HTTP_PROTOCOL
*HttpInstance
;
230 HTTP_TOKEN_WRAP
*Wrap
;
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
;
283 // Parse the URI of the remote host.
285 Url
= HttpInstance
->Url
;
286 UrlLen
= StrLen (Request
->Url
) + 1;
287 if (UrlLen
> HTTP_URL_BUFFER_LEN
) {
288 Url
= AllocateZeroPool (UrlLen
);
290 return EFI_OUT_OF_RESOURCES
;
292 FreePool (HttpInstance
->Url
);
293 HttpInstance
->Url
= Url
;
296 UnicodeStrToAsciiStr (Request
->Url
, Url
);
298 Status
= HttpParseUrl (Url
, (UINT32
) AsciiStrLen (Url
), FALSE
, &UrlParser
);
299 if (EFI_ERROR (Status
)) {
305 Status
= HttpUrlGetHostName (Url
, UrlParser
, &HostName
);
306 if (EFI_ERROR (Status
)) {
310 Status
= HttpUrlGetPort (Url
, UrlParser
, &RemotePort
);
311 if (EFI_ERROR (Status
)) {
312 RemotePort
= HTTP_DEFAULT_PORT
;
318 if (HttpInstance
->RemoteHost
== NULL
) {
320 // Request() is called the first time.
324 if ((HttpInstance
->RemotePort
== RemotePort
) &&
325 (AsciiStrCmp (HttpInstance
->RemoteHost
, HostName
) == 0)) {
327 // Host Name and port number of the request URL are the same with previous call to Request().
328 // Check whether previous TCP packet sent out.
330 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTcpNotReady
, NULL
))) {
332 // Wrap the HTTP token in HTTP_TOKEN_WRAP
334 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
336 Status
= EFI_OUT_OF_RESOURCES
;
340 Wrap
->HttpToken
= Token
;
341 Wrap
->HttpInstance
= HttpInstance
;
343 Status
= HttpCreateTcp4TxEvent (Wrap
);
344 if (EFI_ERROR (Status
)) {
348 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
349 if (EFI_ERROR (Status
)) {
353 Wrap
->TcpWrap
.Method
= Request
->Method
;
358 // Queue the HTTP token and return.
363 // Use existing TCP instance to transmit the packet.
370 // Need close existing TCP instance and create a new TCP instance for data transmit.
372 if (HttpInstance
->RemoteHost
!= NULL
) {
373 FreePool (HttpInstance
->RemoteHost
);
374 HttpInstance
->RemoteHost
= NULL
;
375 HttpInstance
->RemotePort
= 0;
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
)) {
489 // Transmit the request message.
491 Status
= HttpTransmitTcp4 (
495 AsciiStrLen (RequestStr
)
497 if (EFI_ERROR (Status
)) {
503 if (HostName
!= NULL
) {
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
);
1135 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
1136 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
1140 if (HttpInstance
->RxToken
.CompletionToken
.Event
!= NULL
) {
1141 gBS
->CloseEvent (HttpInstance
->RxToken
.CompletionToken
.Event
);
1142 HttpInstance
->RxToken
.CompletionToken
.Event
= NULL
;
1149 NetMapInsertHead (&HttpInstance
->TxTokens
, ValueInItem
->HttpToken
, ValueInItem
);
1153 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
1154 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
1156 RxToken
= &Wrap
->TcpWrap
.RxToken
;
1157 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
1158 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
1159 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
1164 if (HttpInstance
->RxToken
.CompletionToken
.Event
!= NULL
) {
1165 gBS
->CloseEvent (HttpInstance
->RxToken
.CompletionToken
.Event
);
1166 HttpInstance
->RxToken
.CompletionToken
.Event
= NULL
;
1169 RxToken
= &HttpInstance
->RxToken
;
1170 if (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
1171 FreePool (RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
1172 RxToken
->Packet
.RxData
->FragmentTable
[0].FragmentBuffer
= NULL
;
1175 if (HttpHeaders
!= NULL
) {
1176 FreePool (HttpHeaders
);
1179 if (HttpMsg
->Headers
!= NULL
) {
1180 FreePool (HttpMsg
->Headers
);
1183 if (HttpInstance
->CacheBody
!= NULL
) {
1184 FreePool (HttpInstance
->CacheBody
);
1185 HttpInstance
->CacheBody
= NULL
;
1188 Token
->Status
= Status
;
1189 gBS
->SignalEvent (Token
->Event
);
1197 The Response() function queues an HTTP response to this HTTP instance, similar to
1198 Receive() function in the EFI TCP driver. When the HTTP request is sent successfully,
1199 or if there is an error, Status in token will be updated and Event will be signaled.
1201 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1202 is received in the underlying TCP instance, the data will be parsed and Token will
1203 be populated with the response data. If the data received from the remote host
1204 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1205 (asynchronously) for more data to be sent from the remote host before signaling
1208 It is the responsibility of the caller to allocate a buffer for Body and specify the
1209 size in BodyLength. If the remote host provides a response that contains a content
1210 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1211 BodyLength will be updated with the amount of bytes received and copied to Body. This
1212 allows the client to download a large file in chunks instead of into one contiguous
1213 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1214 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1215 token to underlying TCP instance. If data arrives in the receive buffer, up to
1216 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1217 BodyLength with the amount of bytes received and copied to Body.
1219 If the HTTP driver does not have an open underlying TCP connection with the host
1220 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1221 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1222 an open TCP connection between client and host.
1224 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1225 @param[in] Token Pointer to storage containing HTTP response token.
1227 @retval EFI_SUCCESS Allocation succeeded.
1228 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1230 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1233 Token->Message->Headers is NULL.
1234 Token->Message is NULL.
1235 Token->Message->Body is not NULL,
1236 Token->Message->BodyLength is non-zero, and
1237 Token->Message->Data is NULL, but a previous call to
1238 Response() has not been completed successfully.
1239 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1240 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1241 specified by response URL.
1246 IN EFI_HTTP_PROTOCOL
*This
,
1247 IN EFI_HTTP_TOKEN
*Token
1251 EFI_HTTP_MESSAGE
*HttpMsg
;
1252 HTTP_PROTOCOL
*HttpInstance
;
1253 HTTP_TOKEN_WRAP
*Wrap
;
1255 if ((This
== NULL
) || (Token
== NULL
)) {
1256 return EFI_INVALID_PARAMETER
;
1259 HttpMsg
= Token
->Message
;
1260 if (HttpMsg
== NULL
) {
1261 return EFI_INVALID_PARAMETER
;
1264 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1265 ASSERT (HttpInstance
!= NULL
);
1267 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1268 return EFI_NOT_STARTED
;
1271 if (HttpInstance
->LocalAddressIsIPv6
) {
1272 return EFI_UNSUPPORTED
;
1276 // Check whether the token already existed.
1278 if (EFI_ERROR (NetMapIterate (&HttpInstance
->RxTokens
, HttpTokenExist
, Token
))) {
1279 return EFI_ACCESS_DENIED
;
1282 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
1284 return EFI_OUT_OF_RESOURCES
;
1287 Wrap
->HttpInstance
= HttpInstance
;
1288 Wrap
->HttpToken
= Token
;
1290 Status
= HttpCreateTcp4RxEvent (Wrap
);
1291 if (EFI_ERROR (Status
)) {
1295 Status
= NetMapInsertTail (&HttpInstance
->RxTokens
, Token
, Wrap
);
1296 if (EFI_ERROR (Status
)) {
1301 // If already have pending RxTokens, return directly.
1303 if (NetMapGetCount (&HttpInstance
->RxTokens
) > 1) {
1307 return HttpResponseWorker (Wrap
);
1311 if (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
!= NULL
) {
1312 gBS
->CloseEvent (Wrap
->TcpWrap
.RxToken
.CompletionToken
.Event
);
1321 The Poll() function can be used by network drivers and applications to increase the
1322 rate that data packets are moved between the communication devices and the transmit
1325 In some systems, the periodic timer event in the managed network driver may not poll
1326 the underlying communications device fast enough to transmit and/or receive all data
1327 packets without missing incoming packets or dropping outgoing packets. Drivers and
1328 applications that are experiencing packet loss should try calling the Poll() function
1331 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1333 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1334 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1335 @retval EFI_INVALID_PARAMETER This is NULL.
1336 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1337 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1343 IN EFI_HTTP_PROTOCOL
*This
1346 HTTP_PROTOCOL
*HttpInstance
;
1350 return EFI_INVALID_PARAMETER
;
1353 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1354 ASSERT (HttpInstance
!= NULL
);
1356 if (HttpInstance
->LocalAddressIsIPv6
) {
1357 return EFI_UNSUPPORTED
;
1360 if (HttpInstance
->Tcp4
== NULL
|| HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1361 return EFI_NOT_STARTED
;
1364 Status
= HttpInstance
->Tcp4
->Poll (HttpInstance
->Tcp4
);