2 Implementation of EFI_HTTP_PROTOCOL protocol interfaces.
4 Copyright (c) 2015 - 2016, 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 HttpInstance->LocalAddressIsIPv6 is FALSE and
43 HttpConfigData->IPv4Node is NULL.
44 HttpInstance->LocalAddressIsIPv6 is TRUE and
45 HttpConfigData->IPv6Node is NULL.
46 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
52 IN EFI_HTTP_PROTOCOL
*This
,
53 OUT EFI_HTTP_CONFIG_DATA
*HttpConfigData
56 HTTP_PROTOCOL
*HttpInstance
;
59 // Check input parameters.
61 if ((This
== NULL
) || (HttpConfigData
== NULL
)) {
62 return EFI_INVALID_PARAMETER
;
65 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
66 ASSERT (HttpInstance
!= NULL
);
68 if ((HttpInstance
->LocalAddressIsIPv6
&& HttpConfigData
->AccessPoint
.IPv6Node
== NULL
) ||
69 (!HttpInstance
->LocalAddressIsIPv6
&& HttpConfigData
->AccessPoint
.IPv4Node
== NULL
)) {
70 return EFI_INVALID_PARAMETER
;
73 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
74 return EFI_NOT_STARTED
;
77 HttpConfigData
->HttpVersion
= HttpInstance
->HttpVersion
;
78 HttpConfigData
->TimeOutMillisec
= HttpInstance
->TimeOutMillisec
;
79 HttpConfigData
->LocalAddressIsIPv6
= HttpInstance
->LocalAddressIsIPv6
;
81 if (HttpInstance
->LocalAddressIsIPv6
) {
83 HttpConfigData
->AccessPoint
.IPv6Node
,
84 &HttpInstance
->Ipv6Node
,
85 sizeof (HttpInstance
->Ipv6Node
)
89 HttpConfigData
->AccessPoint
.IPv4Node
,
90 &HttpInstance
->IPv4Node
,
91 sizeof (HttpInstance
->IPv4Node
)
99 Initialize or brutally reset the operational parameters for this EFI HTTP instance.
101 The Configure() function does the following:
102 When HttpConfigData is not NULL Initialize this EFI HTTP instance by configuring
103 timeout, local address, port, etc.
104 When HttpConfigData is NULL, reset this EFI HTTP instance by closing all active
105 connections with remote hosts, canceling all asynchronous tokens, and flush request
106 and response buffers without informing the appropriate hosts.
108 No other EFI HTTP function can be executed by this instance until the Configure()
109 function is executed and returns successfully.
111 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
112 @param[in] HttpConfigData Pointer to the configure data to configure the instance.
114 @retval EFI_SUCCESS Operation succeeded.
115 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
117 HttpConfigData is NULL.
118 HttpConfigData->LocalAddressIsIPv6 is FALSE and
119 HttpConfigData->IPv4Node is NULL.
120 HttpConfigData->LocalAddressIsIPv6 is TRUE and
121 HttpConfigData->IPv6Node is NULL.
122 @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling
123 Configure() with NULL to reset it.
124 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
125 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when
126 executing Configure().
127 @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported
128 in the implementation.
133 IN EFI_HTTP_PROTOCOL
*This
,
134 IN EFI_HTTP_CONFIG_DATA
*HttpConfigData
137 HTTP_PROTOCOL
*HttpInstance
;
141 // Check input parameters.
144 HttpConfigData
== NULL
||
145 ((HttpConfigData
->LocalAddressIsIPv6
&& HttpConfigData
->AccessPoint
.IPv6Node
== NULL
) ||
146 (!HttpConfigData
->LocalAddressIsIPv6
&& HttpConfigData
->AccessPoint
.IPv4Node
== NULL
))) {
147 return EFI_INVALID_PARAMETER
;
150 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
151 ASSERT (HttpInstance
!= NULL
&& HttpInstance
->Service
!= NULL
);
153 if (HttpConfigData
!= NULL
) {
156 // Now configure this HTTP instance.
158 if (HttpInstance
->State
!= HTTP_STATE_UNCONFIGED
) {
159 return EFI_ALREADY_STARTED
;
162 HttpInstance
->HttpVersion
= HttpConfigData
->HttpVersion
;
163 HttpInstance
->TimeOutMillisec
= HttpConfigData
->TimeOutMillisec
;
164 HttpInstance
->LocalAddressIsIPv6
= HttpConfigData
->LocalAddressIsIPv6
;
166 if (HttpConfigData
->LocalAddressIsIPv6
) {
168 &HttpInstance
->Ipv6Node
,
169 HttpConfigData
->AccessPoint
.IPv6Node
,
170 sizeof (HttpInstance
->Ipv6Node
)
174 &HttpInstance
->IPv4Node
,
175 HttpConfigData
->AccessPoint
.IPv4Node
,
176 sizeof (HttpInstance
->IPv4Node
)
182 Status
= HttpInitProtocol (HttpInstance
, HttpInstance
->LocalAddressIsIPv6
);
183 if (EFI_ERROR (Status
)) {
187 HttpInstance
->State
= HTTP_STATE_HTTP_CONFIGED
;
192 // Reset all the resources related to HttpInsance.
194 HttpCleanProtocol (HttpInstance
);
195 HttpInstance
->State
= HTTP_STATE_UNCONFIGED
;
202 The Request() function queues an HTTP request to this HTTP instance.
204 Similar to Transmit() function in the EFI TCP driver. When the HTTP request is sent
205 successfully, or if there is an error, Status in token will be updated and Event will
208 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
209 @param[in] Token Pointer to storage containing HTTP request token.
211 @retval EFI_SUCCESS Outgoing data was processed.
212 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
213 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
214 @retval EFI_TIMEOUT Data was dropped out of the transmit or receive queue.
215 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
216 @retval EFI_UNSUPPORTED The HTTP method is not supported in current
218 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
221 Token->Message is NULL.
222 Token->Message->Body is not NULL,
223 Token->Message->BodyLength is non-zero, and
224 Token->Message->Data is NULL, but a previous call to
225 Request()has not been completed successfully.
230 IN EFI_HTTP_PROTOCOL
*This
,
231 IN EFI_HTTP_TOKEN
*Token
234 EFI_HTTP_MESSAGE
*HttpMsg
;
235 EFI_HTTP_REQUEST_DATA
*Request
;
240 HTTP_PROTOCOL
*HttpInstance
;
247 HTTP_TOKEN_WRAP
*Wrap
;
250 if ((This
== NULL
) || (Token
== NULL
)) {
251 return EFI_INVALID_PARAMETER
;
254 HttpMsg
= Token
->Message
;
255 if ((HttpMsg
== NULL
) || (HttpMsg
->Headers
== NULL
)) {
256 return EFI_INVALID_PARAMETER
;
260 // Current implementation does not support POST/PUT method.
261 // If future version supports these two methods, Request could be NULL for a special case that to send large amounts
262 // of data. For this case, the implementation need check whether previous call to Request() has been completed or not.
265 Request
= HttpMsg
->Data
.Request
;
266 if ((Request
== NULL
) || (Request
->Url
== NULL
)) {
267 return EFI_INVALID_PARAMETER
;
271 // Only support GET and HEAD method in current implementation.
273 if ((Request
->Method
!= HttpMethodGet
) && (Request
->Method
!= HttpMethodHead
)) {
274 return EFI_UNSUPPORTED
;
277 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
278 ASSERT (HttpInstance
!= NULL
);
280 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
281 return EFI_NOT_STARTED
;
285 // Check whether the token already existed.
287 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTokenExist
, Token
))) {
288 return EFI_ACCESS_DENIED
;
296 // Parse the URI of the remote host.
298 Url
= HttpInstance
->Url
;
299 UrlLen
= StrLen (Request
->Url
) + 1;
300 if (UrlLen
> HTTP_URL_BUFFER_LEN
) {
301 Url
= AllocateZeroPool (UrlLen
);
303 return EFI_OUT_OF_RESOURCES
;
305 FreePool (HttpInstance
->Url
);
306 HttpInstance
->Url
= Url
;
310 UnicodeStrToAsciiStr (Request
->Url
, Url
);
312 Status
= HttpParseUrl (Url
, (UINT32
) AsciiStrLen (Url
), FALSE
, &UrlParser
);
313 if (EFI_ERROR (Status
)) {
319 Status
= HttpUrlGetHostName (Url
, UrlParser
, &HostName
);
320 if (EFI_ERROR (Status
)) {
324 Status
= HttpUrlGetPort (Url
, UrlParser
, &RemotePort
);
325 if (EFI_ERROR (Status
)) {
326 RemotePort
= HTTP_DEFAULT_PORT
;
329 // If Configure is TRUE, it indicates the first time to call Request();
330 // If ReConfigure is TRUE, it indicates the request URL is not same
331 // with the previous call to Request();
336 if (HttpInstance
->RemoteHost
== NULL
) {
338 // Request() is called the first time.
342 if ((HttpInstance
->RemotePort
== RemotePort
) &&
343 (AsciiStrCmp (HttpInstance
->RemoteHost
, HostName
) == 0)) {
345 // Host Name and port number of the request URL are the same with previous call to Request().
346 // Check whether previous TCP packet sent out.
348 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTcpNotReady
, NULL
))) {
350 // Wrap the HTTP token in HTTP_TOKEN_WRAP
352 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
354 Status
= EFI_OUT_OF_RESOURCES
;
358 Wrap
->HttpToken
= Token
;
359 Wrap
->HttpInstance
= HttpInstance
;
361 Status
= HttpCreateTcpTxEvent (Wrap
);
362 if (EFI_ERROR (Status
)) {
366 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
367 if (EFI_ERROR (Status
)) {
371 Wrap
->TcpWrap
.Method
= Request
->Method
;
376 // Queue the HTTP token and return.
381 // Use existing TCP instance to transmit the packet.
388 // Need close existing TCP instance and create a new TCP instance for data transmit.
390 if (HttpInstance
->RemoteHost
!= NULL
) {
391 FreePool (HttpInstance
->RemoteHost
);
392 HttpInstance
->RemoteHost
= NULL
;
393 HttpInstance
->RemotePort
= 0;
400 // Parse Url for IPv4 or IPv6 address, if failed, perform DNS resolution.
402 if (!HttpInstance
->LocalAddressIsIPv6
) {
403 Status
= NetLibAsciiStrToIp4 (HostName
, &HttpInstance
->RemoteAddr
);
405 Status
= HttpUrlGetIp6 (Url
, UrlParser
, &HttpInstance
->RemoteIpv6Addr
);
408 if (EFI_ERROR (Status
)) {
409 HostNameStr
= AllocateZeroPool ((AsciiStrLen (HostName
) + 1) * sizeof (CHAR16
));
410 if (HostNameStr
== NULL
) {
411 Status
= EFI_OUT_OF_RESOURCES
;
415 AsciiStrToUnicodeStr (HostName
, HostNameStr
);
416 if (!HttpInstance
->LocalAddressIsIPv6
) {
417 Status
= HttpDns4 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteAddr
);
419 Status
= HttpDns6 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteIpv6Addr
);
422 FreePool (HostNameStr
);
423 if (EFI_ERROR (Status
)) {
430 // Save the RemotePort and RemoteHost.
432 ASSERT (HttpInstance
->RemoteHost
== NULL
);
433 HttpInstance
->RemotePort
= RemotePort
;
434 HttpInstance
->RemoteHost
= HostName
;
440 // The request URL is different from previous calls to Request(), close existing TCP instance.
442 if (!HttpInstance
->LocalAddressIsIPv6
) {
443 ASSERT (HttpInstance
->Tcp4
!= NULL
);
445 ASSERT (HttpInstance
->Tcp6
!= NULL
);
447 HttpCloseConnection (HttpInstance
);
448 EfiHttpCancel (This
, NULL
);
452 // Wrap the HTTP token in HTTP_TOKEN_WRAP
454 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
456 Status
= EFI_OUT_OF_RESOURCES
;
460 Wrap
->HttpToken
= Token
;
461 Wrap
->HttpInstance
= HttpInstance
;
462 Wrap
->TcpWrap
.Method
= Request
->Method
;
464 Status
= HttpInitTcp (HttpInstance
, Wrap
, Configure
);
465 if (EFI_ERROR (Status
)) {
471 // For the new HTTP token, create TX TCP token events.
473 Status
= HttpCreateTcpTxEvent (Wrap
);
474 if (EFI_ERROR (Status
)) {
480 // Create request message.
483 if (*FileUrl
!= '/') {
485 // Convert the absolute-URI to the absolute-path
487 while (*FileUrl
!= ':') {
490 if ((*(FileUrl
+1) == '/') && (*(FileUrl
+2) == '/')) {
492 while (*FileUrl
!= '/') {
496 Status
= EFI_INVALID_PARAMETER
;
500 RequestStr
= HttpGenRequestString (HttpInstance
, HttpMsg
, FileUrl
);
501 if (RequestStr
== NULL
) {
502 Status
= EFI_OUT_OF_RESOURCES
;
506 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
507 if (EFI_ERROR (Status
)) {
512 // Transmit the request message.
514 Status
= HttpTransmitTcp (
518 AsciiStrLen (RequestStr
)
520 if (EFI_ERROR (Status
)) {
526 if (HostName
!= NULL
) {
533 NetMapRemoveTail (&HttpInstance
->TxTokens
, NULL
);
536 if (RequestStr
!= NULL
) {
537 FreePool (RequestStr
);
541 HttpCloseConnection (HttpInstance
);
544 HttpCloseTcpConnCloseEvent (HttpInstance
);
545 if (NULL
!= Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
) {
546 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
);
547 Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
= NULL
;
549 if (NULL
!= Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
) {
550 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
);
551 Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
= NULL
;
556 if (HostName
!= NULL
) {
562 if (UrlParser
!= NULL
) {
563 HttpUrlFreeParser (UrlParser
);
571 Cancel a user's Token.
573 @param[in] Map The HTTP instance's token queue.
574 @param[in] Item Object container for one HTTP token and token's wrap.
575 @param[in] Context The user's token to cancel.
577 @retval EFI_SUCCESS Continue to check the next Item.
578 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
585 IN NET_MAP_ITEM
*Item
,
590 EFI_HTTP_TOKEN
*Token
;
591 HTTP_TOKEN_WRAP
*Wrap
;
592 HTTP_PROTOCOL
*HttpInstance
;
594 Token
= (EFI_HTTP_TOKEN
*) Context
;
597 // Return EFI_SUCCESS to check the next item in the map if
598 // this one doesn't match.
600 if ((Token
!= NULL
) && (Token
!= Item
->Key
)) {
604 Wrap
= (HTTP_TOKEN_WRAP
*) Item
->Value
;
605 ASSERT (Wrap
!= NULL
);
606 HttpInstance
= Wrap
->HttpInstance
;
611 NetMapRemoveItem (Map
, Item
, NULL
);
613 if (!HttpInstance
->LocalAddressIsIPv6
) {
614 if (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
!= NULL
) {
615 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
);
618 if (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
!= NULL
) {
619 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
);
622 if (Wrap
->TcpWrap
.Rx4Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
623 FreePool (Wrap
->TcpWrap
.Rx4Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
627 if (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
!= NULL
) {
628 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
);
631 if (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
!= NULL
) {
632 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
);
635 if (Wrap
->TcpWrap
.Rx6Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
636 FreePool (Wrap
->TcpWrap
.Rx6Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
644 // If only one item is to be cancel, return EFI_ABORTED to stop
645 // iterating the map any more.
655 Cancel the user's receive/transmit request. It is the worker function of
656 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the
659 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
660 @param[in] Token The token to cancel. If NULL, all token will be
663 @retval EFI_SUCCESS The token is cancelled.
664 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.
665 @retval Others Other error as indicated.
670 IN HTTP_PROTOCOL
*HttpInstance
,
671 IN EFI_HTTP_TOKEN
*Token
677 // First check the tokens queued by EfiHttpRequest().
679 Status
= NetMapIterate (&HttpInstance
->TxTokens
, HttpCancelTokens
, Token
);
680 if (EFI_ERROR (Status
)) {
682 if (Status
== EFI_ABORTED
) {
691 // Then check the tokens queued by EfiHttpResponse().
693 Status
= NetMapIterate (&HttpInstance
->RxTokens
, HttpCancelTokens
, Token
);
694 if (EFI_ERROR (Status
)) {
696 if (Status
== EFI_ABORTED
) {
699 return EFI_NOT_FOUND
;
711 Abort an asynchronous HTTP request or response token.
713 The Cancel() function aborts a pending HTTP request or response transaction. If
714 Token is not NULL and the token is in transmit or receive queues when it is being
715 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
716 be signaled. If the token is not in one of the queues, which usually means that the
717 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
718 all asynchronous tokens issued by Request() or Response() will be aborted.
720 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
721 @param[in] Token Point to storage containing HTTP request or response
724 @retval EFI_SUCCESS Request and Response queues are successfully flushed.
725 @retval EFI_INVALID_PARAMETER This is NULL.
726 @retval EFI_NOT_STARTED This instance hasn't been configured.
727 @retval EFI_NOT_FOUND The asynchronous request or response token is not
729 @retval EFI_UNSUPPORTED The implementation does not support this function.
735 IN EFI_HTTP_PROTOCOL
*This
,
736 IN EFI_HTTP_TOKEN
*Token
739 HTTP_PROTOCOL
*HttpInstance
;
742 return EFI_INVALID_PARAMETER
;
745 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
746 ASSERT (HttpInstance
!= NULL
);
748 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
749 return EFI_NOT_STARTED
;
752 return HttpCancel (HttpInstance
, Token
);
757 A callback function to intercept events during message parser.
759 This function will be invoked during HttpParseMessageBody() with various events type. An error
760 return status of the callback function will cause the HttpParseMessageBody() aborted.
762 @param[in] EventType Event type of this callback call.
763 @param[in] Data A pointer to data buffer.
764 @param[in] Length Length in bytes of the Data.
765 @param[in] Context Callback context set by HttpInitMsgParser().
767 @retval EFI_SUCCESS Continue to parser the message body.
772 HttpBodyParserCallback (
773 IN HTTP_BODY_PARSE_EVENT EventType
,
779 HTTP_TOKEN_WRAP
*Wrap
;
783 if (EventType
!= BodyParseEventOnComplete
) {
787 if (Data
== NULL
|| Length
!= 0 || Context
== NULL
) {
791 Wrap
= (HTTP_TOKEN_WRAP
*) Context
;
792 Body
= Wrap
->HttpToken
->Message
->Body
;
793 BodyLength
= Wrap
->HttpToken
->Message
->BodyLength
;
794 if (Data
< Body
+ BodyLength
) {
795 Wrap
->HttpInstance
->NextMsg
= Data
;
797 Wrap
->HttpInstance
->NextMsg
= NULL
;
802 // Free Tx4Token or Tx6Token since already received corrsponding HTTP response.
810 The work function of EfiHttpResponse().
812 @param[in] Wrap Pointer to HTTP token's wrap data.
814 @retval EFI_SUCCESS Allocation succeeded.
815 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
816 @retval EFI_NOT_READY Can't find a corresponding Tx4Token/Tx6Token or
817 the EFI_HTTP_UTILITIES_PROTOCOL is not available.
822 IN HTTP_TOKEN_WRAP
*Wrap
826 EFI_HTTP_MESSAGE
*HttpMsg
;
834 CHAR8
*StatusCodeStr
;
836 HTTP_PROTOCOL
*HttpInstance
;
837 EFI_HTTP_TOKEN
*Token
;
839 HTTP_TOKEN_WRAP
*ValueInItem
;
842 if (Wrap
== NULL
|| Wrap
->HttpInstance
== NULL
) {
843 return EFI_INVALID_PARAMETER
;
846 HttpInstance
= Wrap
->HttpInstance
;
847 Token
= Wrap
->HttpToken
;
848 HttpMsg
= Token
->Message
;
850 HttpInstance
->EndofHeader
= NULL
;
851 HttpInstance
->HttpHeaders
= NULL
;
852 HttpMsg
->Headers
= NULL
;
858 if (HttpMsg
->Data
.Response
!= NULL
) {
860 // Need receive the HTTP headers, prepare buffer.
862 Status
= HttpCreateTcpRxEventForHeader (HttpInstance
);
863 if (EFI_ERROR (Status
)) {
868 // Check whether we have cached header from previous call.
870 if ((HttpInstance
->CacheBody
!= NULL
) && (HttpInstance
->NextMsg
!= NULL
)) {
872 // The data is stored at [NextMsg, CacheBody + CacheLen].
874 HdrLen
= HttpInstance
->CacheBody
+ HttpInstance
->CacheLen
- HttpInstance
->NextMsg
;
875 HttpHeaders
= AllocateZeroPool (HdrLen
);
876 if (HttpHeaders
== NULL
) {
877 Status
= EFI_OUT_OF_RESOURCES
;
881 CopyMem (HttpHeaders
, HttpInstance
->NextMsg
, HdrLen
);
882 FreePool (HttpInstance
->CacheBody
);
883 HttpInstance
->CacheBody
= NULL
;
884 HttpInstance
->NextMsg
= NULL
;
885 HttpInstance
->CacheOffset
= 0;
886 SizeofHeaders
= HdrLen
;
887 BufferSize
= HttpInstance
->CacheLen
;
890 // Check whether we cached the whole HTTP headers.
892 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
895 HttpInstance
->EndofHeader
= &EndofHeader
;
896 HttpInstance
->HttpHeaders
= &HttpHeaders
;
898 Status
= HttpTcpReceiveHeader (HttpInstance
, &SizeofHeaders
, &BufferSize
);
899 if (EFI_ERROR (Status
)) {
903 ASSERT (HttpHeaders
!= NULL
);
906 // Cache the part of body.
908 BodyLen
= BufferSize
- (EndofHeader
- HttpHeaders
);
910 if (HttpInstance
->CacheBody
!= NULL
) {
911 FreePool (HttpInstance
->CacheBody
);
914 HttpInstance
->CacheBody
= AllocateZeroPool (BodyLen
);
915 if (HttpInstance
->CacheBody
== NULL
) {
916 Status
= EFI_OUT_OF_RESOURCES
;
920 CopyMem (HttpInstance
->CacheBody
, EndofHeader
, BodyLen
);
921 HttpInstance
->CacheLen
= BodyLen
;
925 // Search for Status Code.
927 StatusCodeStr
= HttpHeaders
+ AsciiStrLen (HTTP_VERSION_STR
) + 1;
928 if (StatusCodeStr
== NULL
) {
932 StatusCode
= AsciiStrDecimalToUintn (StatusCodeStr
);
935 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
937 Tmp
= AsciiStrStr (HttpHeaders
, HTTP_CRLF_STR
);
942 Tmp
= Tmp
+ AsciiStrLen (HTTP_CRLF_STR
);
943 SizeofHeaders
= SizeofHeaders
- (Tmp
- HttpHeaders
);
944 HeaderTmp
= AllocateZeroPool (SizeofHeaders
);
945 if (HeaderTmp
== NULL
) {
949 CopyMem (HeaderTmp
, Tmp
, SizeofHeaders
);
950 FreePool (HttpHeaders
);
951 HttpHeaders
= HeaderTmp
;
954 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.
956 if (mHttpUtilities
== NULL
) {
957 Status
= EFI_NOT_READY
;
962 // Parse the HTTP header into array of key/value pairs.
964 Status
= mHttpUtilities
->Parse (
969 &HttpMsg
->HeaderCount
971 if (EFI_ERROR (Status
)) {
975 FreePool (HttpHeaders
);
978 HttpMsg
->Data
.Response
->StatusCode
= HttpMappingToStatusCode (StatusCode
);
979 HttpInstance
->StatusCode
= StatusCode
;
981 // Init message-body parser by header information.
983 Status
= EFI_NOT_READY
;
985 NetMapRemoveHead (&HttpInstance
->TxTokens
, (VOID
**) &ValueInItem
);
986 if (ValueInItem
== NULL
) {
991 // The first Tx Token not transmitted yet, insert back and return error.
993 if (!ValueInItem
->TcpWrap
.IsTxDone
) {
997 Status
= HttpInitMsgParser (
998 ValueInItem
->TcpWrap
.Method
,
999 HttpMsg
->Data
.Response
->StatusCode
,
1000 HttpMsg
->HeaderCount
,
1002 HttpBodyParserCallback
,
1003 (VOID
*) ValueInItem
,
1004 &HttpInstance
->MsgParser
1006 if (EFI_ERROR (Status
)) {
1011 // Check whether we received a complete HTTP message.
1013 if (HttpInstance
->CacheBody
!= NULL
) {
1014 Status
= HttpParseMessageBody (HttpInstance
->MsgParser
, HttpInstance
->CacheLen
, HttpInstance
->CacheBody
);
1015 if (EFI_ERROR (Status
)) {
1019 if (HttpIsMessageComplete (HttpInstance
->MsgParser
)) {
1021 // Free the MsgParse since we already have a full HTTP message.
1023 HttpFreeMsgParser (HttpInstance
->MsgParser
);
1024 HttpInstance
->MsgParser
= NULL
;
1028 if ((HttpMsg
->Body
== NULL
) || (HttpMsg
->BodyLength
== 0)) {
1029 Status
= EFI_SUCCESS
;
1035 // Receive the response body.
1040 // First check whether we cached some data.
1042 if (HttpInstance
->CacheBody
!= NULL
) {
1044 // Calculate the length of the cached data.
1046 if (HttpInstance
->NextMsg
!= NULL
) {
1048 // We have a cached HTTP message which includes a part of HTTP header of next message.
1050 BodyLen
= HttpInstance
->NextMsg
- (HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
);
1052 BodyLen
= HttpInstance
->CacheLen
- HttpInstance
->CacheOffset
;
1057 // We have some cached data. Just copy the data and return.
1059 if (HttpMsg
->BodyLength
< BodyLen
) {
1060 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, HttpMsg
->BodyLength
);
1061 HttpInstance
->CacheOffset
= HttpInstance
->CacheOffset
+ HttpMsg
->BodyLength
;
1064 // Copy all cached data out.
1066 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, BodyLen
);
1067 HttpInstance
->CacheOffset
= BodyLen
+ HttpInstance
->CacheOffset
;
1068 HttpMsg
->BodyLength
= BodyLen
;
1070 if (HttpInstance
->NextMsg
== NULL
) {
1072 // There is no HTTP header of next message. Just free the cache buffer.
1074 FreePool (HttpInstance
->CacheBody
);
1075 HttpInstance
->CacheBody
= NULL
;
1076 HttpInstance
->NextMsg
= NULL
;
1077 HttpInstance
->CacheOffset
= 0;
1081 // Return since we aready received required data.
1083 Status
= EFI_SUCCESS
;
1087 if (BodyLen
== 0 && HttpInstance
->MsgParser
== NULL
) {
1089 // We received a complete HTTP message, and we don't have more data to return to caller.
1091 HttpMsg
->BodyLength
= 0;
1092 Status
= EFI_SUCCESS
;
1097 ASSERT (HttpInstance
->MsgParser
!= NULL
);
1100 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1102 Status
= HttpTcpReceiveBody (Wrap
, HttpMsg
);
1103 if (EFI_ERROR (Status
)) {
1110 Item
= NetMapFindKey (&Wrap
->HttpInstance
->RxTokens
, Wrap
->HttpToken
);
1112 NetMapRemoveItem (&Wrap
->HttpInstance
->RxTokens
, Item
, NULL
);
1115 if (HttpInstance
->StatusCode
>= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE
) {
1116 Token
->Status
= EFI_HTTP_ERROR
;
1118 Token
->Status
= Status
;
1121 gBS
->SignalEvent (Token
->Event
);
1122 HttpCloseTcpRxEvent (Wrap
);
1127 NetMapInsertHead (&HttpInstance
->TxTokens
, ValueInItem
->HttpToken
, ValueInItem
);
1130 HttpTcpTokenCleanup (Wrap
);
1132 if (HttpHeaders
!= NULL
) {
1133 FreePool (HttpHeaders
);
1136 if (HttpMsg
->Headers
!= NULL
) {
1137 FreePool (HttpMsg
->Headers
);
1140 if (HttpInstance
->CacheBody
!= NULL
) {
1141 FreePool (HttpInstance
->CacheBody
);
1142 HttpInstance
->CacheBody
= NULL
;
1145 if (HttpInstance
->StatusCode
>= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE
) {
1146 Token
->Status
= EFI_HTTP_ERROR
;
1148 Token
->Status
= Status
;
1151 gBS
->SignalEvent (Token
->Event
);
1159 The Response() function queues an HTTP response to this HTTP instance, similar to
1160 Receive() function in the EFI TCP driver. When the HTTP response is received successfully,
1161 or if there is an error, Status in token will be updated and Event will be signaled.
1163 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1164 is received in the underlying TCP instance, the data will be parsed and Token will
1165 be populated with the response data. If the data received from the remote host
1166 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1167 (asynchronously) for more data to be sent from the remote host before signaling
1170 It is the responsibility of the caller to allocate a buffer for Body and specify the
1171 size in BodyLength. If the remote host provides a response that contains a content
1172 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1173 BodyLength will be updated with the amount of bytes received and copied to Body. This
1174 allows the client to download a large file in chunks instead of into one contiguous
1175 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1176 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1177 token to underlying TCP instance. If data arrives in the receive buffer, up to
1178 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1179 BodyLength with the amount of bytes received and copied to Body.
1181 If the HTTP driver does not have an open underlying TCP connection with the host
1182 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1183 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1184 an open TCP connection between client and host.
1186 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1187 @param[in] Token Pointer to storage containing HTTP response token.
1189 @retval EFI_SUCCESS Allocation succeeded.
1190 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1192 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1195 Token->Message->Headers is NULL.
1196 Token->Message is NULL.
1197 Token->Message->Body is not NULL,
1198 Token->Message->BodyLength is non-zero, and
1199 Token->Message->Data is NULL, but a previous call to
1200 Response() has not been completed successfully.
1201 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1202 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1203 specified by response URL.
1208 IN EFI_HTTP_PROTOCOL
*This
,
1209 IN EFI_HTTP_TOKEN
*Token
1213 EFI_HTTP_MESSAGE
*HttpMsg
;
1214 HTTP_PROTOCOL
*HttpInstance
;
1215 HTTP_TOKEN_WRAP
*Wrap
;
1217 if ((This
== NULL
) || (Token
== NULL
)) {
1218 return EFI_INVALID_PARAMETER
;
1221 HttpMsg
= Token
->Message
;
1222 if (HttpMsg
== NULL
) {
1223 return EFI_INVALID_PARAMETER
;
1226 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1227 ASSERT (HttpInstance
!= NULL
);
1229 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1230 return EFI_NOT_STARTED
;
1234 // Check whether the token already existed.
1236 if (EFI_ERROR (NetMapIterate (&HttpInstance
->RxTokens
, HttpTokenExist
, Token
))) {
1237 return EFI_ACCESS_DENIED
;
1240 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
1242 return EFI_OUT_OF_RESOURCES
;
1245 Wrap
->HttpInstance
= HttpInstance
;
1246 Wrap
->HttpToken
= Token
;
1248 Status
= HttpCreateTcpRxEvent (Wrap
);
1249 if (EFI_ERROR (Status
)) {
1253 Status
= NetMapInsertTail (&HttpInstance
->RxTokens
, Token
, Wrap
);
1254 if (EFI_ERROR (Status
)) {
1259 // If already have pending RxTokens, return directly.
1261 if (NetMapGetCount (&HttpInstance
->RxTokens
) > 1) {
1265 return HttpResponseWorker (Wrap
);
1269 if (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
!= NULL
) {
1270 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
);
1273 if (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
!= NULL
) {
1274 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
);
1283 The Poll() function can be used by network drivers and applications to increase the
1284 rate that data packets are moved between the communication devices and the transmit
1287 In some systems, the periodic timer event in the managed network driver may not poll
1288 the underlying communications device fast enough to transmit and/or receive all data
1289 packets without missing incoming packets or dropping outgoing packets. Drivers and
1290 applications that are experiencing packet loss should try calling the Poll() function
1293 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1295 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1296 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1297 @retval EFI_INVALID_PARAMETER This is NULL.
1298 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1299 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1305 IN EFI_HTTP_PROTOCOL
*This
1309 HTTP_PROTOCOL
*HttpInstance
;
1312 return EFI_INVALID_PARAMETER
;
1315 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1316 ASSERT (HttpInstance
!= NULL
);
1318 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1319 return EFI_NOT_STARTED
;
1322 if (HttpInstance
->LocalAddressIsIPv6
) {
1323 if (HttpInstance
->Tcp6
== NULL
) {
1324 return EFI_NOT_STARTED
;
1326 Status
= HttpInstance
->Tcp6
->Poll (HttpInstance
->Tcp6
);
1328 if (HttpInstance
->Tcp4
== NULL
) {
1329 return EFI_NOT_STARTED
;
1331 Status
= HttpInstance
->Tcp4
->Poll (HttpInstance
->Tcp4
);