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_OUT_OF_RESOURCES Could not allocate enough system resources.
44 @retval EFI_NOT_STARTED The HTTP instance is not configured.
50 IN EFI_HTTP_PROTOCOL
*This
,
51 OUT EFI_HTTP_CONFIG_DATA
*HttpConfigData
54 HTTP_PROTOCOL
*HttpInstance
;
55 EFI_HTTPv4_ACCESS_POINT
*Http4AccessPoint
;
56 EFI_HTTPv6_ACCESS_POINT
*Http6AccessPoint
;
58 if ((This
== NULL
) || (HttpConfigData
== NULL
)) {
59 return EFI_INVALID_PARAMETER
;
62 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
63 ASSERT (HttpInstance
!= NULL
);
65 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
66 return EFI_NOT_STARTED
;
69 HttpConfigData
->HttpVersion
= HttpInstance
->HttpVersion
;
70 HttpConfigData
->TimeOutMillisec
= HttpInstance
->TimeOutMillisec
;
71 HttpConfigData
->LocalAddressIsIPv6
= HttpInstance
->LocalAddressIsIPv6
;
73 if (HttpInstance
->LocalAddressIsIPv6
) {
74 Http6AccessPoint
= AllocateZeroPool (sizeof (EFI_HTTPv6_ACCESS_POINT
));
75 if (Http6AccessPoint
== NULL
) {
76 return EFI_OUT_OF_RESOURCES
;
80 &HttpInstance
->Ipv6Node
,
81 sizeof (HttpInstance
->Ipv6Node
)
83 HttpConfigData
->AccessPoint
.IPv6Node
= Http6AccessPoint
;
85 Http4AccessPoint
= AllocateZeroPool (sizeof (EFI_HTTPv4_ACCESS_POINT
));
86 if (Http4AccessPoint
== NULL
) {
87 return EFI_OUT_OF_RESOURCES
;
91 &HttpInstance
->IPv4Node
,
92 sizeof (HttpInstance
->IPv4Node
)
94 HttpConfigData
->AccessPoint
.IPv4Node
= Http4AccessPoint
;
101 Initialize or brutally reset the operational parameters for this EFI HTTP instance.
103 The Configure() function does the following:
104 When HttpConfigData is not NULL Initialize this EFI HTTP instance by configuring
105 timeout, local address, port, etc.
106 When HttpConfigData is NULL, reset this EFI HTTP instance by closing all active
107 connections with remote hosts, canceling all asynchronous tokens, and flush request
108 and response buffers without informing the appropriate hosts.
110 Except for GetModeData() and Configure(), No other EFI HTTP function can be executed
111 by this instance until the Configure() function is executed and returns successfully.
113 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
114 @param[in] HttpConfigData Pointer to the configure data to configure the instance.
116 @retval EFI_SUCCESS Operation succeeded.
117 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
119 HttpConfigData->LocalAddressIsIPv6 is FALSE and
120 HttpConfigData->IPv4Node is NULL.
121 HttpConfigData->LocalAddressIsIPv6 is TRUE and
122 HttpConfigData->IPv6Node is NULL.
123 @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling
124 Configure() with NULL to reset it.
125 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
126 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when
127 executing Configure().
128 @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported
129 in the implementation.
134 IN EFI_HTTP_PROTOCOL
*This
,
135 IN EFI_HTTP_CONFIG_DATA
*HttpConfigData
138 HTTP_PROTOCOL
*HttpInstance
;
142 // Check input parameters.
145 (HttpConfigData
!= NULL
&& ((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:
220 Token->Message is NULL.
221 Token->Message->Body is not NULL,
222 Token->Message->BodyLength is non-zero, and
223 Token->Message->Data is NULL, but a previous call to
224 Request()has not been completed successfully.
229 IN EFI_HTTP_PROTOCOL
*This
,
230 IN EFI_HTTP_TOKEN
*Token
233 EFI_HTTP_MESSAGE
*HttpMsg
;
234 EFI_HTTP_REQUEST_DATA
*Request
;
239 HTTP_PROTOCOL
*HttpInstance
;
246 HTTP_TOKEN_WRAP
*Wrap
;
249 if ((This
== NULL
) || (Token
== NULL
)) {
250 return EFI_INVALID_PARAMETER
;
253 HttpMsg
= Token
->Message
;
254 if ((HttpMsg
== NULL
) || (HttpMsg
->Headers
== NULL
)) {
255 return EFI_INVALID_PARAMETER
;
259 // Current implementation does not support POST/PUT method.
260 // If future version supports these two methods, Request could be NULL for a special case that to send large amounts
261 // of data. For this case, the implementation need check whether previous call to Request() has been completed or not.
264 Request
= HttpMsg
->Data
.Request
;
265 if ((Request
== NULL
) || (Request
->Url
== NULL
)) {
266 return EFI_INVALID_PARAMETER
;
270 // Only support GET and HEAD method in current implementation.
272 if ((Request
->Method
!= HttpMethodGet
) && (Request
->Method
!= HttpMethodHead
)) {
273 return EFI_UNSUPPORTED
;
276 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
277 ASSERT (HttpInstance
!= NULL
);
279 if (HttpInstance
->State
< HTTP_STATE_HTTP_CONFIGED
) {
280 return EFI_NOT_STARTED
;
284 // Check whether the token already existed.
286 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTokenExist
, Token
))) {
287 return EFI_ACCESS_DENIED
;
295 // Parse the URI of the remote host.
297 Url
= HttpInstance
->Url
;
298 UrlLen
= StrLen (Request
->Url
) + 1;
299 if (UrlLen
> HTTP_URL_BUFFER_LEN
) {
300 Url
= AllocateZeroPool (UrlLen
);
302 return EFI_OUT_OF_RESOURCES
;
304 FreePool (HttpInstance
->Url
);
305 HttpInstance
->Url
= Url
;
309 UnicodeStrToAsciiStr (Request
->Url
, Url
);
311 Status
= HttpParseUrl (Url
, (UINT32
) AsciiStrLen (Url
), FALSE
, &UrlParser
);
312 if (EFI_ERROR (Status
)) {
318 Status
= HttpUrlGetHostName (Url
, UrlParser
, &HostName
);
319 if (EFI_ERROR (Status
)) {
323 Status
= HttpUrlGetPort (Url
, UrlParser
, &RemotePort
);
324 if (EFI_ERROR (Status
)) {
325 RemotePort
= HTTP_DEFAULT_PORT
;
328 // If Configure is TRUE, it indicates the first time to call Request();
329 // If ReConfigure is TRUE, it indicates the request URL is not same
330 // with the previous call to Request();
335 if (HttpInstance
->RemoteHost
== NULL
) {
337 // Request() is called the first time.
341 if ((HttpInstance
->RemotePort
== RemotePort
) &&
342 (AsciiStrCmp (HttpInstance
->RemoteHost
, HostName
) == 0)) {
344 // Host Name and port number of the request URL are the same with previous call to Request().
345 // Check whether previous TCP packet sent out.
347 if (EFI_ERROR (NetMapIterate (&HttpInstance
->TxTokens
, HttpTcpNotReady
, NULL
))) {
349 // Wrap the HTTP token in HTTP_TOKEN_WRAP
351 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
353 Status
= EFI_OUT_OF_RESOURCES
;
357 Wrap
->HttpToken
= Token
;
358 Wrap
->HttpInstance
= HttpInstance
;
360 Status
= HttpCreateTcpTxEvent (Wrap
);
361 if (EFI_ERROR (Status
)) {
365 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
366 if (EFI_ERROR (Status
)) {
370 Wrap
->TcpWrap
.Method
= Request
->Method
;
375 // Queue the HTTP token and return.
380 // Use existing TCP instance to transmit the packet.
387 // Need close existing TCP instance and create a new TCP instance for data transmit.
389 if (HttpInstance
->RemoteHost
!= NULL
) {
390 FreePool (HttpInstance
->RemoteHost
);
391 HttpInstance
->RemoteHost
= NULL
;
392 HttpInstance
->RemotePort
= 0;
399 // Parse Url for IPv4 or IPv6 address, if failed, perform DNS resolution.
401 if (!HttpInstance
->LocalAddressIsIPv6
) {
402 Status
= NetLibAsciiStrToIp4 (HostName
, &HttpInstance
->RemoteAddr
);
404 Status
= HttpUrlGetIp6 (Url
, UrlParser
, &HttpInstance
->RemoteIpv6Addr
);
407 if (EFI_ERROR (Status
)) {
408 HostNameStr
= AllocateZeroPool ((AsciiStrLen (HostName
) + 1) * sizeof (CHAR16
));
409 if (HostNameStr
== NULL
) {
410 Status
= EFI_OUT_OF_RESOURCES
;
414 AsciiStrToUnicodeStr (HostName
, HostNameStr
);
415 if (!HttpInstance
->LocalAddressIsIPv6
) {
416 Status
= HttpDns4 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteAddr
);
418 Status
= HttpDns6 (HttpInstance
, HostNameStr
, &HttpInstance
->RemoteIpv6Addr
);
421 FreePool (HostNameStr
);
422 if (EFI_ERROR (Status
)) {
429 // Save the RemotePort and RemoteHost.
431 ASSERT (HttpInstance
->RemoteHost
== NULL
);
432 HttpInstance
->RemotePort
= RemotePort
;
433 HttpInstance
->RemoteHost
= HostName
;
439 // The request URL is different from previous calls to Request(), close existing TCP instance.
441 if (!HttpInstance
->LocalAddressIsIPv6
) {
442 ASSERT (HttpInstance
->Tcp4
!= NULL
);
444 ASSERT (HttpInstance
->Tcp6
!= NULL
);
446 HttpCloseConnection (HttpInstance
);
447 EfiHttpCancel (This
, NULL
);
451 // Wrap the HTTP token in HTTP_TOKEN_WRAP
453 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
455 Status
= EFI_OUT_OF_RESOURCES
;
459 Wrap
->HttpToken
= Token
;
460 Wrap
->HttpInstance
= HttpInstance
;
461 Wrap
->TcpWrap
.Method
= Request
->Method
;
463 Status
= HttpInitTcp (HttpInstance
, Wrap
, Configure
);
464 if (EFI_ERROR (Status
)) {
470 // For the new HTTP token, create TX TCP token events.
472 Status
= HttpCreateTcpTxEvent (Wrap
);
473 if (EFI_ERROR (Status
)) {
479 // Create request message.
482 if (*FileUrl
!= '/') {
484 // Convert the absolute-URI to the absolute-path
486 while (*FileUrl
!= ':') {
489 if ((*(FileUrl
+1) == '/') && (*(FileUrl
+2) == '/')) {
491 while (*FileUrl
!= '/') {
495 Status
= EFI_INVALID_PARAMETER
;
499 RequestStr
= HttpGenRequestString (HttpInstance
, HttpMsg
, FileUrl
);
500 if (RequestStr
== NULL
) {
501 Status
= EFI_OUT_OF_RESOURCES
;
505 Status
= NetMapInsertTail (&HttpInstance
->TxTokens
, Token
, Wrap
);
506 if (EFI_ERROR (Status
)) {
511 // Transmit the request message.
513 Status
= HttpTransmitTcp (
517 AsciiStrLen (RequestStr
)
519 if (EFI_ERROR (Status
)) {
525 if (HostName
!= NULL
) {
532 NetMapRemoveTail (&HttpInstance
->TxTokens
, NULL
);
535 if (RequestStr
!= NULL
) {
536 FreePool (RequestStr
);
540 HttpCloseConnection (HttpInstance
);
543 HttpCloseTcpConnCloseEvent (HttpInstance
);
544 if (NULL
!= Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
) {
545 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
);
546 Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
= NULL
;
548 if (NULL
!= Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
) {
549 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
);
550 Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
= NULL
;
555 if (HostName
!= NULL
) {
561 if (UrlParser
!= NULL
) {
562 HttpUrlFreeParser (UrlParser
);
570 Cancel a user's Token.
572 @param[in] Map The HTTP instance's token queue.
573 @param[in] Item Object container for one HTTP token and token's wrap.
574 @param[in] Context The user's token to cancel.
576 @retval EFI_SUCCESS Continue to check the next Item.
577 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
584 IN NET_MAP_ITEM
*Item
,
589 EFI_HTTP_TOKEN
*Token
;
590 HTTP_TOKEN_WRAP
*Wrap
;
591 HTTP_PROTOCOL
*HttpInstance
;
593 Token
= (EFI_HTTP_TOKEN
*) Context
;
596 // Return EFI_SUCCESS to check the next item in the map if
597 // this one doesn't match.
599 if ((Token
!= NULL
) && (Token
!= Item
->Key
)) {
603 Wrap
= (HTTP_TOKEN_WRAP
*) Item
->Value
;
604 ASSERT (Wrap
!= NULL
);
605 HttpInstance
= Wrap
->HttpInstance
;
610 NetMapRemoveItem (Map
, Item
, NULL
);
612 if (!HttpInstance
->LocalAddressIsIPv6
) {
613 if (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
!= NULL
) {
614 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx4Token
.CompletionToken
.Event
);
617 if (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
!= NULL
) {
618 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
);
621 if (Wrap
->TcpWrap
.Rx4Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
622 FreePool (Wrap
->TcpWrap
.Rx4Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
626 if (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
!= NULL
) {
627 gBS
->CloseEvent (Wrap
->TcpWrap
.Tx6Token
.CompletionToken
.Event
);
630 if (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
!= NULL
) {
631 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
);
634 if (Wrap
->TcpWrap
.Rx6Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
!= NULL
) {
635 FreePool (Wrap
->TcpWrap
.Rx6Token
.Packet
.RxData
->FragmentTable
[0].FragmentBuffer
);
643 // If only one item is to be cancel, return EFI_ABORTED to stop
644 // iterating the map any more.
654 Cancel the user's receive/transmit request. It is the worker function of
655 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the
658 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
659 @param[in] Token The token to cancel. If NULL, all token will be
662 @retval EFI_SUCCESS The token is cancelled.
663 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.
664 @retval Others Other error as indicated.
669 IN HTTP_PROTOCOL
*HttpInstance
,
670 IN EFI_HTTP_TOKEN
*Token
676 // First check the tokens queued by EfiHttpRequest().
678 Status
= NetMapIterate (&HttpInstance
->TxTokens
, HttpCancelTokens
, Token
);
679 if (EFI_ERROR (Status
)) {
681 if (Status
== EFI_ABORTED
) {
690 // Then check the tokens queued by EfiHttpResponse().
692 Status
= NetMapIterate (&HttpInstance
->RxTokens
, HttpCancelTokens
, Token
);
693 if (EFI_ERROR (Status
)) {
695 if (Status
== EFI_ABORTED
) {
698 return EFI_NOT_FOUND
;
710 Abort an asynchronous HTTP request or response token.
712 The Cancel() function aborts a pending HTTP request or response transaction. If
713 Token is not NULL and the token is in transmit or receive queues when it is being
714 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
715 be signaled. If the token is not in one of the queues, which usually means that the
716 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
717 all asynchronous tokens issued by Request() or Response() will be aborted.
719 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
720 @param[in] Token Point to storage containing HTTP request or response
723 @retval EFI_SUCCESS Request and Response queues are successfully flushed.
724 @retval EFI_INVALID_PARAMETER This is NULL.
725 @retval EFI_NOT_STARTED This instance hasn't been configured.
726 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP,
727 BOOTP, RARP, etc.) hasn't finished yet.
728 @retval EFI_NOT_FOUND The asynchronous request or response token is not
730 @retval EFI_UNSUPPORTED The implementation does not support this function.
736 IN EFI_HTTP_PROTOCOL
*This
,
737 IN EFI_HTTP_TOKEN
*Token
740 HTTP_PROTOCOL
*HttpInstance
;
743 return EFI_INVALID_PARAMETER
;
746 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
747 ASSERT (HttpInstance
!= NULL
);
749 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
750 return EFI_NOT_STARTED
;
753 return HttpCancel (HttpInstance
, Token
);
758 A callback function to intercept events during message parser.
760 This function will be invoked during HttpParseMessageBody() with various events type. An error
761 return status of the callback function will cause the HttpParseMessageBody() aborted.
763 @param[in] EventType Event type of this callback call.
764 @param[in] Data A pointer to data buffer.
765 @param[in] Length Length in bytes of the Data.
766 @param[in] Context Callback context set by HttpInitMsgParser().
768 @retval EFI_SUCCESS Continue to parser the message body.
773 HttpBodyParserCallback (
774 IN HTTP_BODY_PARSE_EVENT EventType
,
780 HTTP_TOKEN_WRAP
*Wrap
;
784 if (EventType
!= BodyParseEventOnComplete
) {
788 if (Data
== NULL
|| Length
!= 0 || Context
== NULL
) {
792 Wrap
= (HTTP_TOKEN_WRAP
*) Context
;
793 Body
= Wrap
->HttpToken
->Message
->Body
;
794 BodyLength
= Wrap
->HttpToken
->Message
->BodyLength
;
795 if (Data
< Body
+ BodyLength
) {
796 Wrap
->HttpInstance
->NextMsg
= Data
;
798 Wrap
->HttpInstance
->NextMsg
= NULL
;
803 // Free Tx4Token or Tx6Token since already received corrsponding HTTP response.
811 The work function of EfiHttpResponse().
813 @param[in] Wrap Pointer to HTTP token's wrap data.
815 @retval EFI_SUCCESS Allocation succeeded.
816 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
817 @retval EFI_NOT_READY Can't find a corresponding Tx4Token/Tx6Token or
818 the EFI_HTTP_UTILITIES_PROTOCOL is not available.
823 IN HTTP_TOKEN_WRAP
*Wrap
827 EFI_HTTP_MESSAGE
*HttpMsg
;
835 CHAR8
*StatusCodeStr
;
837 HTTP_PROTOCOL
*HttpInstance
;
838 EFI_HTTP_TOKEN
*Token
;
840 HTTP_TOKEN_WRAP
*ValueInItem
;
843 if (Wrap
== NULL
|| Wrap
->HttpInstance
== NULL
) {
844 return EFI_INVALID_PARAMETER
;
847 HttpInstance
= Wrap
->HttpInstance
;
848 Token
= Wrap
->HttpToken
;
849 HttpMsg
= Token
->Message
;
851 HttpInstance
->EndofHeader
= NULL
;
852 HttpInstance
->HttpHeaders
= NULL
;
853 HttpMsg
->Headers
= NULL
;
859 if (HttpMsg
->Data
.Response
!= NULL
) {
861 // Need receive the HTTP headers, prepare buffer.
863 Status
= HttpCreateTcpRxEventForHeader (HttpInstance
);
864 if (EFI_ERROR (Status
)) {
869 // Check whether we have cached header from previous call.
871 if ((HttpInstance
->CacheBody
!= NULL
) && (HttpInstance
->NextMsg
!= NULL
)) {
873 // The data is stored at [NextMsg, CacheBody + CacheLen].
875 HdrLen
= HttpInstance
->CacheBody
+ HttpInstance
->CacheLen
- HttpInstance
->NextMsg
;
876 HttpHeaders
= AllocateZeroPool (HdrLen
);
877 if (HttpHeaders
== NULL
) {
878 Status
= EFI_OUT_OF_RESOURCES
;
882 CopyMem (HttpHeaders
, HttpInstance
->NextMsg
, HdrLen
);
883 FreePool (HttpInstance
->CacheBody
);
884 HttpInstance
->CacheBody
= NULL
;
885 HttpInstance
->NextMsg
= NULL
;
886 HttpInstance
->CacheOffset
= 0;
887 SizeofHeaders
= HdrLen
;
888 BufferSize
= HttpInstance
->CacheLen
;
891 // Check whether we cached the whole HTTP headers.
893 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
896 HttpInstance
->EndofHeader
= &EndofHeader
;
897 HttpInstance
->HttpHeaders
= &HttpHeaders
;
899 Status
= HttpTcpReceiveHeader (HttpInstance
, &SizeofHeaders
, &BufferSize
);
900 if (EFI_ERROR (Status
)) {
904 ASSERT (HttpHeaders
!= NULL
);
907 // Cache the part of body.
909 BodyLen
= BufferSize
- (EndofHeader
- HttpHeaders
);
911 if (HttpInstance
->CacheBody
!= NULL
) {
912 FreePool (HttpInstance
->CacheBody
);
915 HttpInstance
->CacheBody
= AllocateZeroPool (BodyLen
);
916 if (HttpInstance
->CacheBody
== NULL
) {
917 Status
= EFI_OUT_OF_RESOURCES
;
921 CopyMem (HttpInstance
->CacheBody
, EndofHeader
, BodyLen
);
922 HttpInstance
->CacheLen
= BodyLen
;
926 // Search for Status Code.
928 StatusCodeStr
= HttpHeaders
+ AsciiStrLen (HTTP_VERSION_STR
) + 1;
929 if (StatusCodeStr
== NULL
) {
933 StatusCode
= AsciiStrDecimalToUintn (StatusCodeStr
);
936 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
938 Tmp
= AsciiStrStr (HttpHeaders
, HTTP_CRLF_STR
);
943 Tmp
= Tmp
+ AsciiStrLen (HTTP_CRLF_STR
);
944 SizeofHeaders
= SizeofHeaders
- (Tmp
- HttpHeaders
);
945 HeaderTmp
= AllocateZeroPool (SizeofHeaders
);
946 if (HeaderTmp
== NULL
) {
950 CopyMem (HeaderTmp
, Tmp
, SizeofHeaders
);
951 FreePool (HttpHeaders
);
952 HttpHeaders
= HeaderTmp
;
955 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.
957 if (mHttpUtilities
== NULL
) {
958 Status
= EFI_NOT_READY
;
963 // Parse the HTTP header into array of key/value pairs.
965 Status
= mHttpUtilities
->Parse (
970 &HttpMsg
->HeaderCount
972 if (EFI_ERROR (Status
)) {
976 FreePool (HttpHeaders
);
979 HttpMsg
->Data
.Response
->StatusCode
= HttpMappingToStatusCode (StatusCode
);
982 // Init message-body parser by header information.
984 Status
= EFI_NOT_READY
;
986 NetMapRemoveHead (&HttpInstance
->TxTokens
, (VOID
**) &ValueInItem
);
987 if (ValueInItem
== NULL
) {
992 // The first Tx Token not transmitted yet, insert back and return error.
994 if (!ValueInItem
->TcpWrap
.IsTxDone
) {
998 Status
= HttpInitMsgParser (
999 ValueInItem
->TcpWrap
.Method
,
1000 HttpMsg
->Data
.Response
->StatusCode
,
1001 HttpMsg
->HeaderCount
,
1003 HttpBodyParserCallback
,
1004 (VOID
*) ValueInItem
,
1005 &HttpInstance
->MsgParser
1007 if (EFI_ERROR (Status
)) {
1012 // Check whether we received a complete HTTP message.
1014 if (HttpInstance
->CacheBody
!= NULL
) {
1015 Status
= HttpParseMessageBody (HttpInstance
->MsgParser
, HttpInstance
->CacheLen
, HttpInstance
->CacheBody
);
1016 if (EFI_ERROR (Status
)) {
1020 if (HttpIsMessageComplete (HttpInstance
->MsgParser
)) {
1022 // Free the MsgParse since we already have a full HTTP message.
1024 HttpFreeMsgParser (HttpInstance
->MsgParser
);
1025 HttpInstance
->MsgParser
= NULL
;
1029 if ((HttpMsg
->Body
== NULL
) || (HttpMsg
->BodyLength
== 0)) {
1030 Status
= EFI_SUCCESS
;
1036 // Receive the response body.
1041 // First check whether we cached some data.
1043 if (HttpInstance
->CacheBody
!= NULL
) {
1045 // Calculate the length of the cached data.
1047 if (HttpInstance
->NextMsg
!= NULL
) {
1049 // We have a cached HTTP message which includes a part of HTTP header of next message.
1051 BodyLen
= HttpInstance
->NextMsg
- (HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
);
1053 BodyLen
= HttpInstance
->CacheLen
- HttpInstance
->CacheOffset
;
1058 // We have some cached data. Just copy the data and return.
1060 if (HttpMsg
->BodyLength
< BodyLen
) {
1061 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, HttpMsg
->BodyLength
);
1062 HttpInstance
->CacheOffset
= HttpInstance
->CacheOffset
+ HttpMsg
->BodyLength
;
1065 // Copy all cached data out.
1067 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, BodyLen
);
1068 HttpInstance
->CacheOffset
= BodyLen
+ HttpInstance
->CacheOffset
;
1069 HttpMsg
->BodyLength
= BodyLen
;
1071 if (HttpInstance
->NextMsg
== NULL
) {
1073 // There is no HTTP header of next message. Just free the cache buffer.
1075 FreePool (HttpInstance
->CacheBody
);
1076 HttpInstance
->CacheBody
= NULL
;
1077 HttpInstance
->NextMsg
= NULL
;
1078 HttpInstance
->CacheOffset
= 0;
1082 // Return since we aready received required data.
1084 Status
= EFI_SUCCESS
;
1088 if (BodyLen
== 0 && HttpInstance
->MsgParser
== NULL
) {
1090 // We received a complete HTTP message, and we don't have more data to return to caller.
1092 HttpMsg
->BodyLength
= 0;
1093 Status
= EFI_SUCCESS
;
1098 ASSERT (HttpInstance
->MsgParser
!= NULL
);
1101 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1103 Status
= HttpTcpReceiveBody (Wrap
, HttpMsg
);
1104 if (EFI_ERROR (Status
)) {
1111 Item
= NetMapFindKey (&Wrap
->HttpInstance
->RxTokens
, Wrap
->HttpToken
);
1113 NetMapRemoveItem (&Wrap
->HttpInstance
->RxTokens
, Item
, NULL
);
1115 Token
->Status
= Status
;
1116 gBS
->SignalEvent (Token
->Event
);
1117 HttpCloseTcpRxEvent (Wrap
);
1122 NetMapInsertHead (&HttpInstance
->TxTokens
, ValueInItem
->HttpToken
, ValueInItem
);
1125 HttpTcpTokenCleanup (Wrap
);
1127 if (HttpHeaders
!= NULL
) {
1128 FreePool (HttpHeaders
);
1131 if (HttpMsg
->Headers
!= NULL
) {
1132 FreePool (HttpMsg
->Headers
);
1135 if (HttpInstance
->CacheBody
!= NULL
) {
1136 FreePool (HttpInstance
->CacheBody
);
1137 HttpInstance
->CacheBody
= NULL
;
1140 Token
->Status
= Status
;
1141 gBS
->SignalEvent (Token
->Event
);
1149 The Response() function queues an HTTP response to this HTTP instance, similar to
1150 Receive() function in the EFI TCP driver. When the HTTP request is sent successfully,
1151 or if there is an error, Status in token will be updated and Event will be signaled.
1153 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1154 is received in the underlying TCP instance, the data will be parsed and Token will
1155 be populated with the response data. If the data received from the remote host
1156 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1157 (asynchronously) for more data to be sent from the remote host before signaling
1160 It is the responsibility of the caller to allocate a buffer for Body and specify the
1161 size in BodyLength. If the remote host provides a response that contains a content
1162 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1163 BodyLength will be updated with the amount of bytes received and copied to Body. This
1164 allows the client to download a large file in chunks instead of into one contiguous
1165 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1166 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1167 token to underlying TCP instance. If data arrives in the receive buffer, up to
1168 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1169 BodyLength with the amount of bytes received and copied to Body.
1171 If the HTTP driver does not have an open underlying TCP connection with the host
1172 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1173 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1174 an open TCP connection between client and host.
1176 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1177 @param[in] Token Pointer to storage containing HTTP response token.
1179 @retval EFI_SUCCESS Allocation succeeded.
1180 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1182 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1185 Token->Message->Headers is NULL.
1186 Token->Message is NULL.
1187 Token->Message->Body is not NULL,
1188 Token->Message->BodyLength is non-zero, and
1189 Token->Message->Data is NULL, but a previous call to
1190 Response() has not been completed successfully.
1191 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1192 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1193 specified by response URL.
1198 IN EFI_HTTP_PROTOCOL
*This
,
1199 IN EFI_HTTP_TOKEN
*Token
1203 EFI_HTTP_MESSAGE
*HttpMsg
;
1204 HTTP_PROTOCOL
*HttpInstance
;
1205 HTTP_TOKEN_WRAP
*Wrap
;
1207 if ((This
== NULL
) || (Token
== NULL
)) {
1208 return EFI_INVALID_PARAMETER
;
1211 HttpMsg
= Token
->Message
;
1212 if (HttpMsg
== NULL
) {
1213 return EFI_INVALID_PARAMETER
;
1216 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1217 ASSERT (HttpInstance
!= NULL
);
1219 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1220 return EFI_NOT_STARTED
;
1224 // Check whether the token already existed.
1226 if (EFI_ERROR (NetMapIterate (&HttpInstance
->RxTokens
, HttpTokenExist
, Token
))) {
1227 return EFI_ACCESS_DENIED
;
1230 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
1232 return EFI_OUT_OF_RESOURCES
;
1235 Wrap
->HttpInstance
= HttpInstance
;
1236 Wrap
->HttpToken
= Token
;
1238 Status
= HttpCreateTcpRxEvent (Wrap
);
1239 if (EFI_ERROR (Status
)) {
1243 Status
= NetMapInsertTail (&HttpInstance
->RxTokens
, Token
, Wrap
);
1244 if (EFI_ERROR (Status
)) {
1249 // If already have pending RxTokens, return directly.
1251 if (NetMapGetCount (&HttpInstance
->RxTokens
) > 1) {
1255 return HttpResponseWorker (Wrap
);
1259 if (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
!= NULL
) {
1260 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
);
1263 if (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
!= NULL
) {
1264 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
);
1273 The Poll() function can be used by network drivers and applications to increase the
1274 rate that data packets are moved between the communication devices and the transmit
1277 In some systems, the periodic timer event in the managed network driver may not poll
1278 the underlying communications device fast enough to transmit and/or receive all data
1279 packets without missing incoming packets or dropping outgoing packets. Drivers and
1280 applications that are experiencing packet loss should try calling the Poll() function
1283 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1285 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1286 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1287 @retval EFI_INVALID_PARAMETER This is NULL.
1288 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1289 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1295 IN EFI_HTTP_PROTOCOL
*This
1299 HTTP_PROTOCOL
*HttpInstance
;
1302 return EFI_INVALID_PARAMETER
;
1305 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1306 ASSERT (HttpInstance
!= NULL
);
1308 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1309 return EFI_NOT_STARTED
;
1312 if (HttpInstance
->LocalAddressIsIPv6
) {
1313 if (HttpInstance
->Tcp6
== NULL
) {
1314 return EFI_NOT_STARTED
;
1316 Status
= HttpInstance
->Tcp6
->Poll (HttpInstance
->Tcp6
);
1318 if (HttpInstance
->Tcp4
== NULL
) {
1319 return EFI_NOT_STARTED
;
1321 Status
= HttpInstance
->Tcp4
->Poll (HttpInstance
->Tcp4
);