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
;
782 if (EventType
!= BodyParseEventOnComplete
) {
786 if (Data
== NULL
|| Length
!= 0 || Context
== NULL
) {
790 Wrap
= (HTTP_TOKEN_WRAP
*) Context
;
791 Wrap
->HttpInstance
->NextMsg
= Data
;
794 // Free Tx4Token or Tx6Token since already received corrsponding HTTP response.
802 The work function of EfiHttpResponse().
804 @param[in] Wrap Pointer to HTTP token's wrap data.
806 @retval EFI_SUCCESS Allocation succeeded.
807 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
808 @retval EFI_NOT_READY Can't find a corresponding Tx4Token/Tx6Token or
809 the EFI_HTTP_UTILITIES_PROTOCOL is not available.
814 IN HTTP_TOKEN_WRAP
*Wrap
818 EFI_HTTP_MESSAGE
*HttpMsg
;
826 CHAR8
*StatusCodeStr
;
828 HTTP_PROTOCOL
*HttpInstance
;
829 EFI_HTTP_TOKEN
*Token
;
831 HTTP_TOKEN_WRAP
*ValueInItem
;
834 if (Wrap
== NULL
|| Wrap
->HttpInstance
== NULL
) {
835 return EFI_INVALID_PARAMETER
;
838 HttpInstance
= Wrap
->HttpInstance
;
839 Token
= Wrap
->HttpToken
;
840 HttpMsg
= Token
->Message
;
842 HttpInstance
->EndofHeader
= NULL
;
843 HttpInstance
->HttpHeaders
= NULL
;
844 HttpMsg
->Headers
= NULL
;
850 if (HttpMsg
->Data
.Response
!= NULL
) {
852 // Need receive the HTTP headers, prepare buffer.
854 Status
= HttpCreateTcpRxEventForHeader (HttpInstance
);
855 if (EFI_ERROR (Status
)) {
860 // Check whether we have cached header from previous call.
862 if ((HttpInstance
->CacheBody
!= NULL
) && (HttpInstance
->NextMsg
!= NULL
)) {
864 // The data is stored at [NextMsg, CacheBody + CacheLen].
866 HdrLen
= HttpInstance
->CacheBody
+ HttpInstance
->CacheLen
- HttpInstance
->NextMsg
;
867 HttpHeaders
= AllocateZeroPool (HdrLen
);
868 if (HttpHeaders
== NULL
) {
869 Status
= EFI_OUT_OF_RESOURCES
;
873 CopyMem (HttpHeaders
, HttpInstance
->NextMsg
, HdrLen
);
874 FreePool (HttpInstance
->CacheBody
);
875 HttpInstance
->CacheBody
= NULL
;
876 HttpInstance
->NextMsg
= NULL
;
877 HttpInstance
->CacheOffset
= 0;
878 SizeofHeaders
= HdrLen
;
879 BufferSize
= HttpInstance
->CacheLen
;
882 // Check whether we cached the whole HTTP headers.
884 EndofHeader
= AsciiStrStr (HttpHeaders
, HTTP_END_OF_HDR_STR
);
887 HttpInstance
->EndofHeader
= &EndofHeader
;
888 HttpInstance
->HttpHeaders
= &HttpHeaders
;
890 Status
= HttpTcpReceiveHeader (HttpInstance
, &SizeofHeaders
, &BufferSize
);
891 if (EFI_ERROR (Status
)) {
895 ASSERT (HttpHeaders
!= NULL
);
898 // Cache the part of body.
900 BodyLen
= BufferSize
- (EndofHeader
- HttpHeaders
);
902 if (HttpInstance
->CacheBody
!= NULL
) {
903 FreePool (HttpInstance
->CacheBody
);
906 HttpInstance
->CacheBody
= AllocateZeroPool (BodyLen
);
907 if (HttpInstance
->CacheBody
== NULL
) {
908 Status
= EFI_OUT_OF_RESOURCES
;
912 CopyMem (HttpInstance
->CacheBody
, EndofHeader
, BodyLen
);
913 HttpInstance
->CacheLen
= BodyLen
;
917 // Search for Status Code.
919 StatusCodeStr
= HttpHeaders
+ AsciiStrLen (HTTP_VERSION_STR
) + 1;
920 if (StatusCodeStr
== NULL
) {
924 StatusCode
= AsciiStrDecimalToUintn (StatusCodeStr
);
927 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
929 Tmp
= AsciiStrStr (HttpHeaders
, HTTP_CRLF_STR
);
934 Tmp
= Tmp
+ AsciiStrLen (HTTP_CRLF_STR
);
935 SizeofHeaders
= SizeofHeaders
- (Tmp
- HttpHeaders
);
936 HeaderTmp
= AllocateZeroPool (SizeofHeaders
);
937 if (HeaderTmp
== NULL
) {
941 CopyMem (HeaderTmp
, Tmp
, SizeofHeaders
);
942 FreePool (HttpHeaders
);
943 HttpHeaders
= HeaderTmp
;
946 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.
948 if (mHttpUtilities
== NULL
) {
949 Status
= EFI_NOT_READY
;
954 // Parse the HTTP header into array of key/value pairs.
956 Status
= mHttpUtilities
->Parse (
961 &HttpMsg
->HeaderCount
963 if (EFI_ERROR (Status
)) {
967 FreePool (HttpHeaders
);
970 HttpMsg
->Data
.Response
->StatusCode
= HttpMappingToStatusCode (StatusCode
);
973 // Init message-body parser by header information.
975 Status
= EFI_NOT_READY
;
977 NetMapRemoveHead (&HttpInstance
->TxTokens
, (VOID
**) &ValueInItem
);
978 if (ValueInItem
== NULL
) {
983 // The first Tx Token not transmitted yet, insert back and return error.
985 if (!ValueInItem
->TcpWrap
.IsTxDone
) {
989 Status
= HttpInitMsgParser (
990 ValueInItem
->TcpWrap
.Method
,
991 HttpMsg
->Data
.Response
->StatusCode
,
992 HttpMsg
->HeaderCount
,
994 HttpBodyParserCallback
,
995 (VOID
*) ValueInItem
,
996 &HttpInstance
->MsgParser
998 if (EFI_ERROR (Status
)) {
1003 // Check whether we received a complete HTTP message.
1005 if (HttpInstance
->CacheBody
!= NULL
) {
1006 Status
= HttpParseMessageBody (HttpInstance
->MsgParser
, HttpInstance
->CacheLen
, HttpInstance
->CacheBody
);
1007 if (EFI_ERROR (Status
)) {
1011 if (HttpIsMessageComplete (HttpInstance
->MsgParser
)) {
1013 // Free the MsgParse since we already have a full HTTP message.
1015 HttpFreeMsgParser (HttpInstance
->MsgParser
);
1016 HttpInstance
->MsgParser
= NULL
;
1020 if ((HttpMsg
->Body
== NULL
) || (HttpMsg
->BodyLength
== 0)) {
1021 Status
= EFI_SUCCESS
;
1027 // Receive the response body.
1032 // First check whether we cached some data.
1034 if (HttpInstance
->CacheBody
!= NULL
) {
1036 // Calculate the length of the cached data.
1038 if (HttpInstance
->NextMsg
!= NULL
) {
1040 // We have a cached HTTP message which includes a part of HTTP header of next message.
1042 BodyLen
= HttpInstance
->NextMsg
- (HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
);
1044 BodyLen
= HttpInstance
->CacheLen
- HttpInstance
->CacheOffset
;
1049 // We have some cached data. Just copy the data and return.
1051 if (HttpMsg
->BodyLength
< BodyLen
) {
1052 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, HttpMsg
->BodyLength
);
1053 HttpInstance
->CacheOffset
= HttpInstance
->CacheOffset
+ HttpMsg
->BodyLength
;
1056 // Copy all cached data out.
1058 CopyMem (HttpMsg
->Body
, HttpInstance
->CacheBody
+ HttpInstance
->CacheOffset
, BodyLen
);
1059 HttpInstance
->CacheOffset
= BodyLen
+ HttpInstance
->CacheOffset
;
1060 HttpMsg
->BodyLength
= BodyLen
;
1062 if (HttpInstance
->NextMsg
== NULL
) {
1064 // There is no HTTP header of next message. Just free the cache buffer.
1066 FreePool (HttpInstance
->CacheBody
);
1067 HttpInstance
->CacheBody
= NULL
;
1068 HttpInstance
->NextMsg
= NULL
;
1069 HttpInstance
->CacheOffset
= 0;
1073 // Return since we aready received required data.
1075 Status
= EFI_SUCCESS
;
1079 if (BodyLen
== 0 && HttpInstance
->MsgParser
== NULL
) {
1081 // We received a complete HTTP message, and we don't have more data to return to caller.
1083 HttpMsg
->BodyLength
= 0;
1084 Status
= EFI_SUCCESS
;
1089 ASSERT (HttpInstance
->MsgParser
!= NULL
);
1092 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1094 Status
= HttpTcpReceiveBody (Wrap
, HttpMsg
);
1095 if (EFI_ERROR (Status
)) {
1102 Item
= NetMapFindKey (&Wrap
->HttpInstance
->RxTokens
, Wrap
->HttpToken
);
1104 NetMapRemoveItem (&Wrap
->HttpInstance
->RxTokens
, Item
, NULL
);
1106 Token
->Status
= Status
;
1107 gBS
->SignalEvent (Token
->Event
);
1108 HttpCloseTcpRxEvent (Wrap
);
1113 NetMapInsertHead (&HttpInstance
->TxTokens
, ValueInItem
->HttpToken
, ValueInItem
);
1116 HttpTcpTokenCleanup (Wrap
);
1118 if (HttpHeaders
!= NULL
) {
1119 FreePool (HttpHeaders
);
1122 if (HttpMsg
->Headers
!= NULL
) {
1123 FreePool (HttpMsg
->Headers
);
1126 if (HttpInstance
->CacheBody
!= NULL
) {
1127 FreePool (HttpInstance
->CacheBody
);
1128 HttpInstance
->CacheBody
= NULL
;
1131 Token
->Status
= Status
;
1132 gBS
->SignalEvent (Token
->Event
);
1140 The Response() function queues an HTTP response to this HTTP instance, similar to
1141 Receive() function in the EFI TCP driver. When the HTTP request is sent successfully,
1142 or if there is an error, Status in token will be updated and Event will be signaled.
1144 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1145 is received in the underlying TCP instance, the data will be parsed and Token will
1146 be populated with the response data. If the data received from the remote host
1147 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1148 (asynchronously) for more data to be sent from the remote host before signaling
1151 It is the responsibility of the caller to allocate a buffer for Body and specify the
1152 size in BodyLength. If the remote host provides a response that contains a content
1153 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1154 BodyLength will be updated with the amount of bytes received and copied to Body. This
1155 allows the client to download a large file in chunks instead of into one contiguous
1156 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1157 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1158 token to underlying TCP instance. If data arrives in the receive buffer, up to
1159 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1160 BodyLength with the amount of bytes received and copied to Body.
1162 If the HTTP driver does not have an open underlying TCP connection with the host
1163 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1164 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1165 an open TCP connection between client and host.
1167 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1168 @param[in] Token Pointer to storage containing HTTP response token.
1170 @retval EFI_SUCCESS Allocation succeeded.
1171 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1173 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1176 Token->Message->Headers is NULL.
1177 Token->Message is NULL.
1178 Token->Message->Body is not NULL,
1179 Token->Message->BodyLength is non-zero, and
1180 Token->Message->Data is NULL, but a previous call to
1181 Response() has not been completed successfully.
1182 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1183 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1184 specified by response URL.
1189 IN EFI_HTTP_PROTOCOL
*This
,
1190 IN EFI_HTTP_TOKEN
*Token
1194 EFI_HTTP_MESSAGE
*HttpMsg
;
1195 HTTP_PROTOCOL
*HttpInstance
;
1196 HTTP_TOKEN_WRAP
*Wrap
;
1198 if ((This
== NULL
) || (Token
== NULL
)) {
1199 return EFI_INVALID_PARAMETER
;
1202 HttpMsg
= Token
->Message
;
1203 if (HttpMsg
== NULL
) {
1204 return EFI_INVALID_PARAMETER
;
1207 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1208 ASSERT (HttpInstance
!= NULL
);
1210 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1211 return EFI_NOT_STARTED
;
1215 // Check whether the token already existed.
1217 if (EFI_ERROR (NetMapIterate (&HttpInstance
->RxTokens
, HttpTokenExist
, Token
))) {
1218 return EFI_ACCESS_DENIED
;
1221 Wrap
= AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP
));
1223 return EFI_OUT_OF_RESOURCES
;
1226 Wrap
->HttpInstance
= HttpInstance
;
1227 Wrap
->HttpToken
= Token
;
1229 Status
= HttpCreateTcpRxEvent (Wrap
);
1230 if (EFI_ERROR (Status
)) {
1234 Status
= NetMapInsertTail (&HttpInstance
->RxTokens
, Token
, Wrap
);
1235 if (EFI_ERROR (Status
)) {
1240 // If already have pending RxTokens, return directly.
1242 if (NetMapGetCount (&HttpInstance
->RxTokens
) > 1) {
1246 return HttpResponseWorker (Wrap
);
1250 if (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
!= NULL
) {
1251 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx4Token
.CompletionToken
.Event
);
1254 if (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
!= NULL
) {
1255 gBS
->CloseEvent (Wrap
->TcpWrap
.Rx6Token
.CompletionToken
.Event
);
1264 The Poll() function can be used by network drivers and applications to increase the
1265 rate that data packets are moved between the communication devices and the transmit
1268 In some systems, the periodic timer event in the managed network driver may not poll
1269 the underlying communications device fast enough to transmit and/or receive all data
1270 packets without missing incoming packets or dropping outgoing packets. Drivers and
1271 applications that are experiencing packet loss should try calling the Poll() function
1274 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1276 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1277 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1278 @retval EFI_INVALID_PARAMETER This is NULL.
1279 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1280 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1286 IN EFI_HTTP_PROTOCOL
*This
1290 HTTP_PROTOCOL
*HttpInstance
;
1293 return EFI_INVALID_PARAMETER
;
1296 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (This
);
1297 ASSERT (HttpInstance
!= NULL
);
1299 if (HttpInstance
->State
!= HTTP_STATE_TCP_CONNECTED
) {
1300 return EFI_NOT_STARTED
;
1303 if (HttpInstance
->LocalAddressIsIPv6
) {
1304 if (HttpInstance
->Tcp6
== NULL
) {
1305 return EFI_NOT_STARTED
;
1307 Status
= HttpInstance
->Tcp6
->Poll (HttpInstance
->Tcp6
);
1309 if (HttpInstance
->Tcp4
== NULL
) {
1310 return EFI_NOT_STARTED
;
1312 Status
= HttpInstance
->Tcp4
->Poll (HttpInstance
->Tcp4
);