]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/HttpDxe/HttpImpl.c
NetworkPkg: Add PCD to enable the HTTP connections switch
[mirror_edk2.git] / NetworkPkg / HttpDxe / HttpImpl.c
1 /** @file
2 Implementation of EFI_HTTP_PROTOCOL protocol interfaces.
3
4 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>
6
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.
11
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.
14
15 **/
16
17 #include "HttpDriver.h"
18
19 EFI_HTTP_PROTOCOL mEfiHttpTemplate = {
20 EfiHttpGetModeData,
21 EfiHttpConfigure,
22 EfiHttpRequest,
23 EfiHttpCancel,
24 EfiHttpResponse,
25 EfiHttpPoll
26 };
27
28 /**
29 Returns the operational parameters for the current HTTP child instance.
30
31 The GetModeData() function is used to read the current mode data (operational
32 parameters) for this HTTP protocol instance.
33
34 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
35 @param[out] HttpConfigData Point to buffer for operational parameters of this
36 HTTP instance.
37
38 @retval EFI_SUCCESS Operation succeeded.
39 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
40 This is NULL.
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.
47
48 **/
49 EFI_STATUS
50 EFIAPI
51 EfiHttpGetModeData (
52 IN EFI_HTTP_PROTOCOL *This,
53 OUT EFI_HTTP_CONFIG_DATA *HttpConfigData
54 )
55 {
56 HTTP_PROTOCOL *HttpInstance;
57
58 //
59 // Check input parameters.
60 //
61 if ((This == NULL) || (HttpConfigData == NULL)) {
62 return EFI_INVALID_PARAMETER;
63 }
64
65 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
66 ASSERT (HttpInstance != NULL);
67
68 if ((HttpInstance->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv6Node == NULL) ||
69 (!HttpInstance->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv4Node == NULL)) {
70 return EFI_INVALID_PARAMETER;
71 }
72
73 if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) {
74 return EFI_NOT_STARTED;
75 }
76
77 HttpConfigData->HttpVersion = HttpInstance->HttpVersion;
78 HttpConfigData->TimeOutMillisec = HttpInstance->TimeOutMillisec;
79 HttpConfigData->LocalAddressIsIPv6 = HttpInstance->LocalAddressIsIPv6;
80
81 if (HttpInstance->LocalAddressIsIPv6) {
82 CopyMem (
83 HttpConfigData->AccessPoint.IPv6Node,
84 &HttpInstance->Ipv6Node,
85 sizeof (HttpInstance->Ipv6Node)
86 );
87 } else {
88 CopyMem (
89 HttpConfigData->AccessPoint.IPv4Node,
90 &HttpInstance->IPv4Node,
91 sizeof (HttpInstance->IPv4Node)
92 );
93 }
94
95 return EFI_SUCCESS;
96 }
97
98 /**
99 Initialize or brutally reset the operational parameters for this EFI HTTP instance.
100
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.
107
108 No other EFI HTTP function can be executed by this instance until the Configure()
109 function is executed and returns successfully.
110
111 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
112 @param[in] HttpConfigData Pointer to the configure data to configure the instance.
113
114 @retval EFI_SUCCESS Operation succeeded.
115 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
116 This is NULL.
117 HttpConfigData->LocalAddressIsIPv6 is FALSE and
118 HttpConfigData->IPv4Node is NULL.
119 HttpConfigData->LocalAddressIsIPv6 is TRUE and
120 HttpConfigData->IPv6Node is NULL.
121 @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling
122 Configure() with NULL to reset it.
123 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
124 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when
125 executing Configure().
126 @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported
127 in the implementation.
128 **/
129 EFI_STATUS
130 EFIAPI
131 EfiHttpConfigure (
132 IN EFI_HTTP_PROTOCOL *This,
133 IN EFI_HTTP_CONFIG_DATA *HttpConfigData
134 )
135 {
136 HTTP_PROTOCOL *HttpInstance;
137 EFI_STATUS Status;
138
139 //
140 // Check input parameters.
141 //
142 if (This == NULL ||
143 (HttpConfigData != NULL &&
144 ((HttpConfigData->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv6Node == NULL) ||
145 (!HttpConfigData->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv4Node == NULL)))) {
146 return EFI_INVALID_PARAMETER;
147 }
148
149 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
150 ASSERT (HttpInstance != NULL && HttpInstance->Service != NULL);
151
152 if (HttpConfigData != NULL) {
153
154 //
155 // Now configure this HTTP instance.
156 //
157 if (HttpInstance->State != HTTP_STATE_UNCONFIGED) {
158 return EFI_ALREADY_STARTED;
159 }
160
161 HttpInstance->HttpVersion = HttpConfigData->HttpVersion;
162 HttpInstance->TimeOutMillisec = HttpConfigData->TimeOutMillisec;
163 HttpInstance->LocalAddressIsIPv6 = HttpConfigData->LocalAddressIsIPv6;
164
165 if (HttpConfigData->LocalAddressIsIPv6) {
166 CopyMem (
167 &HttpInstance->Ipv6Node,
168 HttpConfigData->AccessPoint.IPv6Node,
169 sizeof (HttpInstance->Ipv6Node)
170 );
171 } else {
172 CopyMem (
173 &HttpInstance->IPv4Node,
174 HttpConfigData->AccessPoint.IPv4Node,
175 sizeof (HttpInstance->IPv4Node)
176 );
177 }
178
179 //
180 // Creat Tcp child
181 //
182 Status = HttpInitProtocol (HttpInstance, HttpInstance->LocalAddressIsIPv6);
183 if (EFI_ERROR (Status)) {
184 return Status;
185 }
186
187 HttpInstance->State = HTTP_STATE_HTTP_CONFIGED;
188 return EFI_SUCCESS;
189
190 } else {
191 //
192 // Reset all the resources related to HttpInsance.
193 //
194 HttpCleanProtocol (HttpInstance);
195 HttpInstance->State = HTTP_STATE_UNCONFIGED;
196 return EFI_SUCCESS;
197 }
198 }
199
200
201 /**
202 The Request() function queues an HTTP request to this HTTP instance.
203
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
206 be signaled.
207
208 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
209 @param[in] Token Pointer to storage containing HTTP request token.
210
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
217 implementation.
218 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
219 This is NULL.
220 Token is NULL.
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.
226 **/
227 EFI_STATUS
228 EFIAPI
229 EfiHttpRequest (
230 IN EFI_HTTP_PROTOCOL *This,
231 IN EFI_HTTP_TOKEN *Token
232 )
233 {
234 EFI_HTTP_MESSAGE *HttpMsg;
235 EFI_HTTP_REQUEST_DATA *Request;
236 VOID *UrlParser;
237 EFI_STATUS Status;
238 CHAR8 *HostName;
239 UINTN HostNameSize;
240 UINT16 RemotePort;
241 HTTP_PROTOCOL *HttpInstance;
242 BOOLEAN Configure;
243 BOOLEAN ReConfigure;
244 BOOLEAN TlsConfigure;
245 CHAR8 *RequestMsg;
246 CHAR8 *Url;
247 UINTN UrlLen;
248 CHAR16 *HostNameStr;
249 HTTP_TOKEN_WRAP *Wrap;
250 CHAR8 *FileUrl;
251 UINTN RequestMsgSize;
252
253 //
254 // Initializations
255 //
256 Url = NULL;
257 UrlParser = NULL;
258 RemotePort = 0;
259 HostName = NULL;
260 RequestMsg = NULL;
261 HostNameStr = NULL;
262 Wrap = NULL;
263 FileUrl = NULL;
264 TlsConfigure = FALSE;
265
266 if ((This == NULL) || (Token == NULL)) {
267 return EFI_INVALID_PARAMETER;
268 }
269
270 HttpMsg = Token->Message;
271 if (HttpMsg == NULL) {
272 return EFI_INVALID_PARAMETER;
273 }
274
275 Request = HttpMsg->Data.Request;
276
277 //
278 // Only support GET, HEAD, PUT and POST method in current implementation.
279 //
280 if ((Request != NULL) && (Request->Method != HttpMethodGet) &&
281 (Request->Method != HttpMethodHead) && (Request->Method != HttpMethodPut) && (Request->Method != HttpMethodPost)) {
282 return EFI_UNSUPPORTED;
283 }
284
285 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
286 ASSERT (HttpInstance != NULL);
287
288 //
289 // Capture the method into HttpInstance.
290 //
291 if (Request != NULL) {
292 HttpInstance->Method = Request->Method;
293 }
294
295 if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) {
296 return EFI_NOT_STARTED;
297 }
298
299 if (Request == NULL) {
300 //
301 // Request would be NULL only for PUT/POST operation (in the current implementation)
302 //
303 if ((HttpInstance->Method != HttpMethodPut) && (HttpInstance->Method != HttpMethodPost)) {
304 return EFI_INVALID_PARAMETER;
305 }
306
307 //
308 // For PUT/POST, we need to have the TCP already configured. Bail out if it is not!
309 //
310 if (HttpInstance->State < HTTP_STATE_TCP_CONFIGED) {
311 return EFI_INVALID_PARAMETER;
312 }
313
314 //
315 // We need to have the Message Body for sending the HTTP message across in these cases.
316 //
317 if (HttpMsg->Body == NULL || HttpMsg->BodyLength == 0) {
318 return EFI_INVALID_PARAMETER;
319 }
320
321 //
322 // Use existing TCP instance to transmit the packet.
323 //
324 Configure = FALSE;
325 ReConfigure = FALSE;
326 } else {
327 //
328 // Check whether the token already existed.
329 //
330 if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTokenExist, Token))) {
331 return EFI_ACCESS_DENIED;
332 }
333
334 //
335 // Parse the URI of the remote host.
336 //
337 Url = HttpInstance->Url;
338 UrlLen = StrLen (Request->Url) + 1;
339 if (UrlLen > HTTP_URL_BUFFER_LEN) {
340 Url = AllocateZeroPool (UrlLen);
341 if (Url == NULL) {
342 return EFI_OUT_OF_RESOURCES;
343 }
344 FreePool (HttpInstance->Url);
345 HttpInstance->Url = Url;
346 }
347
348
349 UnicodeStrToAsciiStrS (Request->Url, Url, UrlLen);
350
351 //
352 // From the information in Url, the HTTP instance will
353 // be able to determine whether to use http or https.
354 //
355 HttpInstance->UseHttps = IsHttpsUrl (Url);
356
357 //
358 // HTTP is disabled, return directly if the URI is not HTTPS.
359 //
360 if (!PcdGetBool (PcdAllowHttpConnections) && !(HttpInstance->UseHttps)) {
361
362 DEBUG ((EFI_D_ERROR, "EfiHttpRequest: HTTP is disabled.\n"));
363
364 return EFI_ACCESS_DENIED;
365 }
366
367 //
368 // Check whether we need to create Tls child and open the TLS protocol.
369 //
370 if (HttpInstance->UseHttps && HttpInstance->TlsChildHandle == NULL) {
371 //
372 // Use TlsSb to create Tls child and open the TLS protocol.
373 //
374 HttpInstance->TlsChildHandle = TlsCreateChild (
375 HttpInstance->Service->ImageHandle,
376 &(HttpInstance->Tls),
377 &(HttpInstance->TlsConfiguration)
378 );
379 if (HttpInstance->TlsChildHandle == NULL) {
380 return EFI_DEVICE_ERROR;
381 }
382
383 TlsConfigure = TRUE;
384 }
385
386 UrlParser = NULL;
387 Status = HttpParseUrl (Url, (UINT32) AsciiStrLen (Url), FALSE, &UrlParser);
388 if (EFI_ERROR (Status)) {
389 goto Error1;
390 }
391
392 HostName = NULL;
393 Status = HttpUrlGetHostName (Url, UrlParser, &HostName);
394 if (EFI_ERROR (Status)) {
395 goto Error1;
396 }
397
398 Status = HttpUrlGetPort (Url, UrlParser, &RemotePort);
399 if (EFI_ERROR (Status)) {
400 if (HttpInstance->UseHttps) {
401 RemotePort = HTTPS_DEFAULT_PORT;
402 } else {
403 RemotePort = HTTP_DEFAULT_PORT;
404 }
405 }
406 //
407 // If Configure is TRUE, it indicates the first time to call Request();
408 // If ReConfigure is TRUE, it indicates the request URL is not same
409 // with the previous call to Request();
410 //
411 Configure = TRUE;
412 ReConfigure = TRUE;
413
414 if (HttpInstance->RemoteHost == NULL) {
415 //
416 // Request() is called the first time.
417 //
418 ReConfigure = FALSE;
419 } else {
420 if ((HttpInstance->RemotePort == RemotePort) &&
421 (AsciiStrCmp (HttpInstance->RemoteHost, HostName) == 0) &&
422 (!HttpInstance->UseHttps || (HttpInstance->UseHttps &&
423 !TlsConfigure &&
424 HttpInstance->TlsSessionState == EfiTlsSessionDataTransferring))) {
425 //
426 // Host Name and port number of the request URL are the same with previous call to Request().
427 // If Https protocol used, the corresponding SessionState is EfiTlsSessionDataTransferring.
428 // Check whether previous TCP packet sent out.
429 //
430
431 if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTcpNotReady, NULL))) {
432 //
433 // Wrap the HTTP token in HTTP_TOKEN_WRAP
434 //
435 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));
436 if (Wrap == NULL) {
437 Status = EFI_OUT_OF_RESOURCES;
438 goto Error1;
439 }
440
441 Wrap->HttpToken = Token;
442 Wrap->HttpInstance = HttpInstance;
443
444 Status = HttpCreateTcpTxEvent (Wrap);
445 if (EFI_ERROR (Status)) {
446 goto Error1;
447 }
448
449 Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap);
450 if (EFI_ERROR (Status)) {
451 goto Error1;
452 }
453
454 Wrap->TcpWrap.Method = Request->Method;
455
456 FreePool (HostName);
457
458 //
459 // Queue the HTTP token and return.
460 //
461 return EFI_SUCCESS;
462 } else {
463 //
464 // Use existing TCP instance to transmit the packet.
465 //
466 Configure = FALSE;
467 ReConfigure = FALSE;
468 }
469 } else {
470 //
471 // Need close existing TCP instance and create a new TCP instance for data transmit.
472 //
473 if (HttpInstance->RemoteHost != NULL) {
474 FreePool (HttpInstance->RemoteHost);
475 HttpInstance->RemoteHost = NULL;
476 HttpInstance->RemotePort = 0;
477 }
478 }
479 }
480 }
481
482 if (Configure) {
483 //
484 // Parse Url for IPv4 or IPv6 address, if failed, perform DNS resolution.
485 //
486 if (!HttpInstance->LocalAddressIsIPv6) {
487 Status = NetLibAsciiStrToIp4 (HostName, &HttpInstance->RemoteAddr);
488 } else {
489 Status = HttpUrlGetIp6 (Url, UrlParser, &HttpInstance->RemoteIpv6Addr);
490 }
491
492 if (EFI_ERROR (Status)) {
493 HostNameSize = AsciiStrSize (HostName);
494 HostNameStr = AllocateZeroPool (HostNameSize * sizeof (CHAR16));
495 if (HostNameStr == NULL) {
496 Status = EFI_OUT_OF_RESOURCES;
497 goto Error1;
498 }
499
500 AsciiStrToUnicodeStrS (HostName, HostNameStr, HostNameSize);
501 if (!HttpInstance->LocalAddressIsIPv6) {
502 Status = HttpDns4 (HttpInstance, HostNameStr, &HttpInstance->RemoteAddr);
503 } else {
504 Status = HttpDns6 (HttpInstance, HostNameStr, &HttpInstance->RemoteIpv6Addr);
505 }
506
507 FreePool (HostNameStr);
508 if (EFI_ERROR (Status)) {
509 goto Error1;
510 }
511 }
512
513 //
514 // Save the RemotePort and RemoteHost.
515 //
516 ASSERT (HttpInstance->RemoteHost == NULL);
517 HttpInstance->RemotePort = RemotePort;
518 HttpInstance->RemoteHost = HostName;
519 HostName = NULL;
520 }
521
522 if (ReConfigure) {
523 //
524 // The request URL is different from previous calls to Request(), close existing TCP instance.
525 //
526 if (!HttpInstance->LocalAddressIsIPv6) {
527 ASSERT (HttpInstance->Tcp4 != NULL);
528 } else {
529 ASSERT (HttpInstance->Tcp6 != NULL);
530 }
531
532 if (HttpInstance->UseHttps && !TlsConfigure) {
533 Status = TlsCloseSession (HttpInstance);
534 if (EFI_ERROR (Status)) {
535 goto Error1;
536 }
537
538 TlsCloseTxRxEvent (HttpInstance);
539 }
540
541 HttpCloseConnection (HttpInstance);
542 EfiHttpCancel (This, NULL);
543 }
544
545 //
546 // Wrap the HTTP token in HTTP_TOKEN_WRAP
547 //
548 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));
549 if (Wrap == NULL) {
550 Status = EFI_OUT_OF_RESOURCES;
551 goto Error1;
552 }
553
554 Wrap->HttpToken = Token;
555 Wrap->HttpInstance = HttpInstance;
556 if (Request != NULL) {
557 Wrap->TcpWrap.Method = Request->Method;
558 }
559
560 Status = HttpInitSession (
561 HttpInstance,
562 Wrap,
563 Configure || ReConfigure,
564 TlsConfigure
565 );
566 if (EFI_ERROR (Status)) {
567 goto Error2;
568 }
569
570 if (!Configure && !ReConfigure && !TlsConfigure) {
571 //
572 // For the new HTTP token, create TX TCP token events.
573 //
574 Status = HttpCreateTcpTxEvent (Wrap);
575 if (EFI_ERROR (Status)) {
576 goto Error1;
577 }
578 }
579
580 //
581 // Create request message.
582 //
583 FileUrl = Url;
584 if (Url != NULL && *FileUrl != '/') {
585 //
586 // Convert the absolute-URI to the absolute-path
587 //
588 while (*FileUrl != ':') {
589 FileUrl++;
590 }
591 if ((*(FileUrl+1) == '/') && (*(FileUrl+2) == '/')) {
592 FileUrl += 3;
593 while (*FileUrl != '/') {
594 FileUrl++;
595 }
596 } else {
597 Status = EFI_INVALID_PARAMETER;
598 goto Error3;
599 }
600 }
601
602 Status = HttpGenRequestMessage (HttpMsg, FileUrl, &RequestMsg, &RequestMsgSize);
603
604 if (EFI_ERROR (Status) || NULL == RequestMsg) {
605 goto Error3;
606 }
607
608 ASSERT (RequestMsg != NULL);
609
610 //
611 // Every request we insert a TxToken and a response call would remove the TxToken.
612 // In cases of PUT/POST, after an initial request-response pair, we would do a
613 // continuous request without a response call. So, in such cases, where Request
614 // structure is NULL, we would not insert a TxToken.
615 //
616 if (Request != NULL) {
617 Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap);
618 if (EFI_ERROR (Status)) {
619 goto Error4;
620 }
621 }
622
623 //
624 // Transmit the request message.
625 //
626 Status = HttpTransmitTcp (
627 HttpInstance,
628 Wrap,
629 (UINT8*) RequestMsg,
630 RequestMsgSize
631 );
632 if (EFI_ERROR (Status)) {
633 goto Error5;
634 }
635
636 DispatchDpc ();
637
638 if (HostName != NULL) {
639 FreePool (HostName);
640 }
641
642 return EFI_SUCCESS;
643
644 Error5:
645 //
646 // We would have inserted a TxToken only if Request structure is not NULL.
647 // Hence check before we do a remove in this error case.
648 //
649 if (Request != NULL) {
650 NetMapRemoveTail (&HttpInstance->TxTokens, NULL);
651 }
652
653 Error4:
654 if (RequestMsg != NULL) {
655 FreePool (RequestMsg);
656 }
657
658 Error3:
659 if (HttpInstance->UseHttps) {
660 TlsCloseSession (HttpInstance);
661 TlsCloseTxRxEvent (HttpInstance);
662 }
663
664 Error2:
665 HttpCloseConnection (HttpInstance);
666
667 HttpCloseTcpConnCloseEvent (HttpInstance);
668 if (NULL != Wrap->TcpWrap.Tx4Token.CompletionToken.Event) {
669 gBS->CloseEvent (Wrap->TcpWrap.Tx4Token.CompletionToken.Event);
670 Wrap->TcpWrap.Tx4Token.CompletionToken.Event = NULL;
671 }
672 if (NULL != Wrap->TcpWrap.Tx6Token.CompletionToken.Event) {
673 gBS->CloseEvent (Wrap->TcpWrap.Tx6Token.CompletionToken.Event);
674 Wrap->TcpWrap.Tx6Token.CompletionToken.Event = NULL;
675 }
676
677 Error1:
678 if (HostName != NULL) {
679 FreePool (HostName);
680 }
681 if (Wrap != NULL) {
682 FreePool (Wrap);
683 }
684 if (UrlParser!= NULL) {
685 HttpUrlFreeParser (UrlParser);
686 }
687
688 return Status;
689
690 }
691
692 /**
693 Cancel a user's Token.
694
695 @param[in] Map The HTTP instance's token queue.
696 @param[in] Item Object container for one HTTP token and token's wrap.
697 @param[in] Context The user's token to cancel.
698
699 @retval EFI_SUCCESS Continue to check the next Item.
700 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
701
702 **/
703 EFI_STATUS
704 EFIAPI
705 HttpCancelTokens (
706 IN NET_MAP *Map,
707 IN NET_MAP_ITEM *Item,
708 IN VOID *Context
709 )
710 {
711 EFI_HTTP_TOKEN *Token;
712 HTTP_TOKEN_WRAP *Wrap;
713 HTTP_PROTOCOL *HttpInstance;
714
715 Token = (EFI_HTTP_TOKEN *) Context;
716
717 //
718 // Return EFI_SUCCESS to check the next item in the map if
719 // this one doesn't match.
720 //
721 if ((Token != NULL) && (Token != Item->Key)) {
722 return EFI_SUCCESS;
723 }
724
725 Wrap = (HTTP_TOKEN_WRAP *) Item->Value;
726 ASSERT (Wrap != NULL);
727 HttpInstance = Wrap->HttpInstance;
728
729 if (!HttpInstance->LocalAddressIsIPv6) {
730 if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) {
731 //
732 // Cancle the Token before close its Event.
733 //
734 HttpInstance->Tcp4->Cancel (HttpInstance->Tcp4, &Wrap->TcpWrap.Rx4Token.CompletionToken);
735
736 //
737 // Dispatch the DPC queued by the NotifyFunction of the canceled token's events.
738 //
739 DispatchDpc ();
740 }
741 } else {
742 if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {
743 //
744 // Cancle the Token before close its Event.
745 //
746 HttpInstance->Tcp6->Cancel (HttpInstance->Tcp6, &Wrap->TcpWrap.Rx6Token.CompletionToken);
747
748 //
749 // Dispatch the DPC queued by the NotifyFunction of the canceled token's events.
750 //
751 DispatchDpc ();
752 }
753 }
754
755 //
756 // If only one item is to be cancel, return EFI_ABORTED to stop
757 // iterating the map any more.
758 //
759 if (Token != NULL) {
760 return EFI_ABORTED;
761 }
762
763 return EFI_SUCCESS;
764 }
765
766 /**
767 Cancel the user's receive/transmit request. It is the worker function of
768 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the
769 token.
770
771 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
772 @param[in] Token The token to cancel. If NULL, all token will be
773 cancelled.
774
775 @retval EFI_SUCCESS The token is cancelled.
776 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.
777 @retval Others Other error as indicated.
778
779 **/
780 EFI_STATUS
781 HttpCancel (
782 IN HTTP_PROTOCOL *HttpInstance,
783 IN EFI_HTTP_TOKEN *Token
784 )
785 {
786 EFI_STATUS Status;
787
788 //
789 // First check the tokens queued by EfiHttpRequest().
790 //
791 Status = NetMapIterate (&HttpInstance->TxTokens, HttpCancelTokens, Token);
792 if (EFI_ERROR (Status)) {
793 if (Token != NULL) {
794 if (Status == EFI_ABORTED) {
795 return EFI_SUCCESS;
796 }
797 } else {
798 return Status;
799 }
800 }
801
802 if (!HttpInstance->UseHttps) {
803 //
804 // Then check the tokens queued by EfiHttpResponse(), except for Https.
805 //
806 Status = NetMapIterate (&HttpInstance->RxTokens, HttpCancelTokens, Token);
807 if (EFI_ERROR (Status)) {
808 if (Token != NULL) {
809 if (Status == EFI_ABORTED) {
810 return EFI_SUCCESS;
811 } else {
812 return EFI_NOT_FOUND;
813 }
814 } else {
815 return Status;
816 }
817 }
818 } else {
819 if (!HttpInstance->LocalAddressIsIPv6) {
820 HttpInstance->Tcp4->Cancel (HttpInstance->Tcp4, &HttpInstance->Tcp4TlsRxToken.CompletionToken);
821 } else {
822 HttpInstance->Tcp6->Cancel (HttpInstance->Tcp6, &HttpInstance->Tcp6TlsRxToken.CompletionToken);
823 }
824 }
825
826 return EFI_SUCCESS;
827 }
828
829
830 /**
831 Abort an asynchronous HTTP request or response token.
832
833 The Cancel() function aborts a pending HTTP request or response transaction. If
834 Token is not NULL and the token is in transmit or receive queues when it is being
835 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
836 be signaled. If the token is not in one of the queues, which usually means that the
837 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
838 all asynchronous tokens issued by Request() or Response() will be aborted.
839
840 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
841 @param[in] Token Point to storage containing HTTP request or response
842 token.
843
844 @retval EFI_SUCCESS Request and Response queues are successfully flushed.
845 @retval EFI_INVALID_PARAMETER This is NULL.
846 @retval EFI_NOT_STARTED This instance hasn't been configured.
847 @retval EFI_NOT_FOUND The asynchronous request or response token is not
848 found.
849 @retval EFI_UNSUPPORTED The implementation does not support this function.
850
851 **/
852 EFI_STATUS
853 EFIAPI
854 EfiHttpCancel (
855 IN EFI_HTTP_PROTOCOL *This,
856 IN EFI_HTTP_TOKEN *Token
857 )
858 {
859 HTTP_PROTOCOL *HttpInstance;
860
861 if (This == NULL) {
862 return EFI_INVALID_PARAMETER;
863 }
864
865 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
866 ASSERT (HttpInstance != NULL);
867
868 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {
869 return EFI_NOT_STARTED;
870 }
871
872 return HttpCancel (HttpInstance, Token);
873
874 }
875
876 /**
877 A callback function to intercept events during message parser.
878
879 This function will be invoked during HttpParseMessageBody() with various events type. An error
880 return status of the callback function will cause the HttpParseMessageBody() aborted.
881
882 @param[in] EventType Event type of this callback call.
883 @param[in] Data A pointer to data buffer.
884 @param[in] Length Length in bytes of the Data.
885 @param[in] Context Callback context set by HttpInitMsgParser().
886
887 @retval EFI_SUCCESS Continue to parser the message body.
888
889 **/
890 EFI_STATUS
891 EFIAPI
892 HttpBodyParserCallback (
893 IN HTTP_BODY_PARSE_EVENT EventType,
894 IN CHAR8 *Data,
895 IN UINTN Length,
896 IN VOID *Context
897 )
898 {
899 HTTP_TOKEN_WRAP *Wrap;
900 UINTN BodyLength;
901 CHAR8 *Body;
902
903 if (EventType != BodyParseEventOnComplete) {
904 return EFI_SUCCESS;
905 }
906
907 if (Data == NULL || Length != 0 || Context == NULL) {
908 return EFI_SUCCESS;
909 }
910
911 Wrap = (HTTP_TOKEN_WRAP *) Context;
912 Body = Wrap->HttpToken->Message->Body;
913 BodyLength = Wrap->HttpToken->Message->BodyLength;
914 if (Data < Body + BodyLength) {
915 Wrap->HttpInstance->NextMsg = Data;
916 } else {
917 Wrap->HttpInstance->NextMsg = NULL;
918 }
919
920
921 //
922 // Free Tx4Token or Tx6Token since already received corrsponding HTTP response.
923 //
924 FreePool (Wrap);
925
926 return EFI_SUCCESS;
927 }
928
929 /**
930 The work function of EfiHttpResponse().
931
932 @param[in] Wrap Pointer to HTTP token's wrap data.
933
934 @retval EFI_SUCCESS Allocation succeeded.
935 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
936 @retval EFI_NOT_READY Can't find a corresponding Tx4Token/Tx6Token or
937 the EFI_HTTP_UTILITIES_PROTOCOL is not available.
938
939 **/
940 EFI_STATUS
941 HttpResponseWorker (
942 IN HTTP_TOKEN_WRAP *Wrap
943 )
944 {
945 EFI_STATUS Status;
946 EFI_HTTP_MESSAGE *HttpMsg;
947 CHAR8 *EndofHeader;
948 CHAR8 *HttpHeaders;
949 UINTN SizeofHeaders;
950 UINTN BufferSize;
951 UINTN StatusCode;
952 CHAR8 *Tmp;
953 CHAR8 *HeaderTmp;
954 CHAR8 *StatusCodeStr;
955 UINTN BodyLen;
956 HTTP_PROTOCOL *HttpInstance;
957 EFI_HTTP_TOKEN *Token;
958 NET_MAP_ITEM *Item;
959 HTTP_TOKEN_WRAP *ValueInItem;
960 UINTN HdrLen;
961 NET_FRAGMENT Fragment;
962
963 if (Wrap == NULL || Wrap->HttpInstance == NULL) {
964 return EFI_INVALID_PARAMETER;
965 }
966
967 HttpInstance = Wrap->HttpInstance;
968 Token = Wrap->HttpToken;
969 HttpMsg = Token->Message;
970
971 HttpInstance->EndofHeader = NULL;
972 HttpInstance->HttpHeaders = NULL;
973 HttpMsg->Headers = NULL;
974 HttpHeaders = NULL;
975 SizeofHeaders = 0;
976 BufferSize = 0;
977 EndofHeader = NULL;
978 ValueInItem = NULL;
979 Fragment.Len = 0;
980 Fragment.Bulk = NULL;
981
982 if (HttpMsg->Data.Response != NULL) {
983 //
984 // Check whether we have cached header from previous call.
985 //
986 if ((HttpInstance->CacheBody != NULL) && (HttpInstance->NextMsg != NULL)) {
987 //
988 // The data is stored at [NextMsg, CacheBody + CacheLen].
989 //
990 HdrLen = HttpInstance->CacheBody + HttpInstance->CacheLen - HttpInstance->NextMsg;
991 HttpHeaders = AllocateZeroPool (HdrLen);
992 if (HttpHeaders == NULL) {
993 Status = EFI_OUT_OF_RESOURCES;
994 goto Error;
995 }
996
997 CopyMem (HttpHeaders, HttpInstance->NextMsg, HdrLen);
998 FreePool (HttpInstance->CacheBody);
999 HttpInstance->CacheBody = NULL;
1000 HttpInstance->NextMsg = NULL;
1001 HttpInstance->CacheOffset = 0;
1002 SizeofHeaders = HdrLen;
1003 BufferSize = HttpInstance->CacheLen;
1004
1005 //
1006 // Check whether we cached the whole HTTP headers.
1007 //
1008 EndofHeader = AsciiStrStr (HttpHeaders, HTTP_END_OF_HDR_STR);
1009 }
1010
1011 HttpInstance->EndofHeader = &EndofHeader;
1012 HttpInstance->HttpHeaders = &HttpHeaders;
1013
1014
1015 if (HttpInstance->TimeoutEvent == NULL) {
1016 //
1017 // Create TimeoutEvent for response
1018 //
1019 Status = gBS->CreateEvent (
1020 EVT_TIMER,
1021 TPL_CALLBACK,
1022 NULL,
1023 NULL,
1024 &HttpInstance->TimeoutEvent
1025 );
1026 if (EFI_ERROR (Status)) {
1027 goto Error;
1028 }
1029 }
1030
1031 //
1032 // Start the timer, and wait Timeout seconds to receive the header packet.
1033 //
1034 Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_RESPONSE_TIMEOUT * TICKS_PER_SECOND);
1035 if (EFI_ERROR (Status)) {
1036 goto Error;
1037 }
1038
1039 Status = HttpTcpReceiveHeader (HttpInstance, &SizeofHeaders, &BufferSize, HttpInstance->TimeoutEvent);
1040
1041 gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0);
1042
1043 if (EFI_ERROR (Status)) {
1044 goto Error;
1045 }
1046
1047 ASSERT (HttpHeaders != NULL);
1048
1049 //
1050 // Cache the part of body.
1051 //
1052 BodyLen = BufferSize - (EndofHeader - HttpHeaders);
1053 if (BodyLen > 0) {
1054 if (HttpInstance->CacheBody != NULL) {
1055 FreePool (HttpInstance->CacheBody);
1056 }
1057
1058 HttpInstance->CacheBody = AllocateZeroPool (BodyLen);
1059 if (HttpInstance->CacheBody == NULL) {
1060 Status = EFI_OUT_OF_RESOURCES;
1061 goto Error;
1062 }
1063
1064 CopyMem (HttpInstance->CacheBody, EndofHeader, BodyLen);
1065 HttpInstance->CacheLen = BodyLen;
1066 }
1067
1068 //
1069 // Search for Status Code.
1070 //
1071 StatusCodeStr = HttpHeaders + AsciiStrLen (HTTP_VERSION_STR) + 1;
1072 if (StatusCodeStr == NULL) {
1073 Status = EFI_NOT_READY;
1074 goto Error;
1075 }
1076
1077 StatusCode = AsciiStrDecimalToUintn (StatusCodeStr);
1078
1079 //
1080 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
1081 //
1082 Tmp = AsciiStrStr (HttpHeaders, HTTP_CRLF_STR);
1083 if (Tmp == NULL) {
1084 Status = EFI_NOT_READY;
1085 goto Error;
1086 }
1087
1088 //
1089 // We could have response with just a HTTP message and no headers. For Example,
1090 // "100 Continue". In such cases, we would not want to unnecessarily call a Parse
1091 // method. A "\r\n" following Tmp string again would indicate an end. Compare and
1092 // set SizeofHeaders to 0.
1093 //
1094 Tmp = Tmp + AsciiStrLen (HTTP_CRLF_STR);
1095 if (CompareMem (Tmp, HTTP_CRLF_STR, AsciiStrLen (HTTP_CRLF_STR)) == 0) {
1096 SizeofHeaders = 0;
1097 } else {
1098 SizeofHeaders = SizeofHeaders - (Tmp - HttpHeaders);
1099 }
1100
1101 HttpMsg->Data.Response->StatusCode = HttpMappingToStatusCode (StatusCode);
1102 HttpInstance->StatusCode = StatusCode;
1103
1104 Status = EFI_NOT_READY;
1105 ValueInItem = NULL;
1106
1107 //
1108 // In cases of PUT/POST, after an initial request-response pair, we would do a
1109 // continuous request without a response call. So, we would not do an insert of
1110 // TxToken. After we have sent the complete file, we will call a response to get
1111 // a final response from server. In such a case, we would not have any TxTokens.
1112 // Hence, check that case before doing a NetMapRemoveHead.
1113 //
1114 if (!NetMapIsEmpty (&HttpInstance->TxTokens)) {
1115 NetMapRemoveHead (&HttpInstance->TxTokens, (VOID**) &ValueInItem);
1116 if (ValueInItem == NULL) {
1117 goto Error;
1118 }
1119
1120 //
1121 // The first Tx Token not transmitted yet, insert back and return error.
1122 //
1123 if (!ValueInItem->TcpWrap.IsTxDone) {
1124 goto Error2;
1125 }
1126 }
1127
1128 if (SizeofHeaders != 0) {
1129 HeaderTmp = AllocateZeroPool (SizeofHeaders);
1130 if (HeaderTmp == NULL) {
1131 Status = EFI_OUT_OF_RESOURCES;
1132 goto Error2;
1133 }
1134
1135 CopyMem (HeaderTmp, Tmp, SizeofHeaders);
1136 FreePool (HttpHeaders);
1137 HttpHeaders = HeaderTmp;
1138
1139 //
1140 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.
1141 //
1142 if (mHttpUtilities == NULL) {
1143 Status = EFI_NOT_READY;
1144 goto Error2;
1145 }
1146
1147 //
1148 // Parse the HTTP header into array of key/value pairs.
1149 //
1150 Status = mHttpUtilities->Parse (
1151 mHttpUtilities,
1152 HttpHeaders,
1153 SizeofHeaders,
1154 &HttpMsg->Headers,
1155 &HttpMsg->HeaderCount
1156 );
1157 if (EFI_ERROR (Status)) {
1158 goto Error2;
1159 }
1160
1161 FreePool (HttpHeaders);
1162 HttpHeaders = NULL;
1163
1164
1165 //
1166 // Init message-body parser by header information.
1167 //
1168 Status = HttpInitMsgParser (
1169 HttpInstance->Method,
1170 HttpMsg->Data.Response->StatusCode,
1171 HttpMsg->HeaderCount,
1172 HttpMsg->Headers,
1173 HttpBodyParserCallback,
1174 (VOID *) ValueInItem,
1175 &HttpInstance->MsgParser
1176 );
1177 if (EFI_ERROR (Status)) {
1178 goto Error2;
1179 }
1180
1181 //
1182 // Check whether we received a complete HTTP message.
1183 //
1184 if (HttpInstance->CacheBody != NULL) {
1185 Status = HttpParseMessageBody (HttpInstance->MsgParser, HttpInstance->CacheLen, HttpInstance->CacheBody);
1186 if (EFI_ERROR (Status)) {
1187 goto Error2;
1188 }
1189
1190 if (HttpIsMessageComplete (HttpInstance->MsgParser)) {
1191 //
1192 // Free the MsgParse since we already have a full HTTP message.
1193 //
1194 HttpFreeMsgParser (HttpInstance->MsgParser);
1195 HttpInstance->MsgParser = NULL;
1196 }
1197 }
1198 }
1199
1200 if ((HttpMsg->Body == NULL) || (HttpMsg->BodyLength == 0)) {
1201 Status = EFI_SUCCESS;
1202 goto Exit;
1203 }
1204 }
1205
1206 //
1207 // Receive the response body.
1208 //
1209 BodyLen = 0;
1210
1211 //
1212 // First check whether we cached some data.
1213 //
1214 if (HttpInstance->CacheBody != NULL) {
1215 //
1216 // Calculate the length of the cached data.
1217 //
1218 if (HttpInstance->NextMsg != NULL) {
1219 //
1220 // We have a cached HTTP message which includes a part of HTTP header of next message.
1221 //
1222 BodyLen = HttpInstance->NextMsg - (HttpInstance->CacheBody + HttpInstance->CacheOffset);
1223 } else {
1224 BodyLen = HttpInstance->CacheLen - HttpInstance->CacheOffset;
1225 }
1226
1227 if (BodyLen > 0) {
1228 //
1229 // We have some cached data. Just copy the data and return.
1230 //
1231 if (HttpMsg->BodyLength < BodyLen) {
1232 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, HttpMsg->BodyLength);
1233 HttpInstance->CacheOffset = HttpInstance->CacheOffset + HttpMsg->BodyLength;
1234 } else {
1235 //
1236 // Copy all cached data out.
1237 //
1238 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, BodyLen);
1239 HttpInstance->CacheOffset = BodyLen + HttpInstance->CacheOffset;
1240 HttpMsg->BodyLength = BodyLen;
1241
1242 if (HttpInstance->NextMsg == NULL) {
1243 //
1244 // There is no HTTP header of next message. Just free the cache buffer.
1245 //
1246 FreePool (HttpInstance->CacheBody);
1247 HttpInstance->CacheBody = NULL;
1248 HttpInstance->NextMsg = NULL;
1249 HttpInstance->CacheOffset = 0;
1250 }
1251 }
1252 //
1253 // Return since we aready received required data.
1254 //
1255 Status = EFI_SUCCESS;
1256 goto Exit;
1257 }
1258
1259 if (BodyLen == 0 && HttpInstance->MsgParser == NULL) {
1260 //
1261 // We received a complete HTTP message, and we don't have more data to return to caller.
1262 //
1263 HttpMsg->BodyLength = 0;
1264 Status = EFI_SUCCESS;
1265 goto Exit;
1266 }
1267 }
1268
1269 ASSERT (HttpInstance->MsgParser != NULL);
1270
1271 //
1272 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1273 //
1274 if (!HttpInstance->UseHttps) {
1275 Status = HttpTcpReceiveBody (Wrap, HttpMsg);
1276
1277 if (EFI_ERROR (Status)) {
1278 goto Error2;
1279 }
1280
1281 } else {
1282 if (HttpInstance->TimeoutEvent == NULL) {
1283 //
1284 // Create TimeoutEvent for response
1285 //
1286 Status = gBS->CreateEvent (
1287 EVT_TIMER,
1288 TPL_CALLBACK,
1289 NULL,
1290 NULL,
1291 &HttpInstance->TimeoutEvent
1292 );
1293 if (EFI_ERROR (Status)) {
1294 goto Error2;
1295 }
1296 }
1297
1298 //
1299 // Start the timer, and wait Timeout seconds to receive the body packet.
1300 //
1301 Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_RESPONSE_TIMEOUT * TICKS_PER_SECOND);
1302 if (EFI_ERROR (Status)) {
1303 goto Error2;
1304 }
1305
1306 Status = HttpsReceive (HttpInstance, &Fragment, HttpInstance->TimeoutEvent);
1307
1308 gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0);
1309
1310 if (EFI_ERROR (Status)) {
1311 goto Error2;
1312 }
1313
1314 //
1315 // Check whether we receive a complete HTTP message.
1316 //
1317 Status = HttpParseMessageBody (
1318 HttpInstance->MsgParser,
1319 (UINTN) Fragment.Len,
1320 (CHAR8 *) Fragment.Bulk
1321 );
1322 if (EFI_ERROR (Status)) {
1323 goto Error2;
1324 }
1325
1326 if (HttpIsMessageComplete (HttpInstance->MsgParser)) {
1327 //
1328 // Free the MsgParse since we already have a full HTTP message.
1329 //
1330 HttpFreeMsgParser (HttpInstance->MsgParser);
1331 HttpInstance->MsgParser = NULL;
1332 }
1333
1334 //
1335 // We receive part of header of next HTTP msg.
1336 //
1337 if (HttpInstance->NextMsg != NULL) {
1338 HttpMsg->BodyLength = MIN ((UINTN) (HttpInstance->NextMsg - (CHAR8 *) Fragment.Bulk), HttpMsg->BodyLength);
1339 CopyMem (HttpMsg->Body, Fragment.Bulk, HttpMsg->BodyLength);
1340
1341 HttpInstance->CacheLen = Fragment.Len - HttpMsg->BodyLength;
1342 if (HttpInstance->CacheLen != 0) {
1343 if (HttpInstance->CacheBody != NULL) {
1344 FreePool (HttpInstance->CacheBody);
1345 }
1346
1347 HttpInstance->CacheBody = AllocateZeroPool (HttpInstance->CacheLen);
1348 if (HttpInstance->CacheBody == NULL) {
1349 Status = EFI_OUT_OF_RESOURCES;
1350 goto Error2;
1351 }
1352
1353 CopyMem (HttpInstance->CacheBody, Fragment.Bulk + HttpMsg->BodyLength, HttpInstance->CacheLen);
1354 HttpInstance->CacheOffset = 0;
1355
1356 HttpInstance->NextMsg = HttpInstance->CacheBody + (UINTN) (HttpInstance->NextMsg - (CHAR8 *) (Fragment.Bulk + HttpMsg->BodyLength));
1357 }
1358 } else {
1359 HttpMsg->BodyLength = MIN (Fragment.Len, (UINT32) HttpMsg->BodyLength);
1360 CopyMem (HttpMsg->Body, Fragment.Bulk, HttpMsg->BodyLength);
1361 HttpInstance->CacheLen = Fragment.Len - HttpMsg->BodyLength;
1362 if (HttpInstance->CacheLen != 0) {
1363 if (HttpInstance->CacheBody != NULL) {
1364 FreePool (HttpInstance->CacheBody);
1365 }
1366
1367 HttpInstance->CacheBody = AllocateZeroPool (HttpInstance->CacheLen);
1368 if (HttpInstance->CacheBody == NULL) {
1369 Status = EFI_OUT_OF_RESOURCES;
1370 goto Error2;
1371 }
1372
1373 CopyMem (HttpInstance->CacheBody, Fragment.Bulk + HttpMsg->BodyLength, HttpInstance->CacheLen);
1374 HttpInstance->CacheOffset = 0;
1375 }
1376 }
1377
1378 if (Fragment.Bulk != NULL) {
1379 FreePool (Fragment.Bulk);
1380 Fragment.Bulk = NULL;
1381 }
1382
1383 goto Exit;
1384 }
1385
1386 return Status;
1387
1388 Exit:
1389 Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);
1390 if (Item != NULL) {
1391 NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);
1392 }
1393
1394 if (HttpInstance->StatusCode >= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE) {
1395 Token->Status = EFI_HTTP_ERROR;
1396 } else {
1397 Token->Status = Status;
1398 }
1399
1400 gBS->SignalEvent (Token->Event);
1401 HttpCloseTcpRxEvent (Wrap);
1402 FreePool (Wrap);
1403 return Status;
1404
1405 Error2:
1406 if (ValueInItem != NULL) {
1407 NetMapInsertHead (&HttpInstance->TxTokens, ValueInItem->HttpToken, ValueInItem);
1408 }
1409
1410 Error:
1411 Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);
1412 if (Item != NULL) {
1413 NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);
1414 }
1415
1416 if (!HttpInstance->UseHttps) {
1417 HttpTcpTokenCleanup (Wrap);
1418 } else {
1419 FreePool (Wrap);
1420 }
1421
1422 if (HttpHeaders != NULL) {
1423 FreePool (HttpHeaders);
1424 HttpHeaders = NULL;
1425 }
1426
1427 if (Fragment.Bulk != NULL) {
1428 FreePool (Fragment.Bulk);
1429 Fragment.Bulk = NULL;
1430 }
1431
1432 if (HttpMsg->Headers != NULL) {
1433 FreePool (HttpMsg->Headers);
1434 HttpMsg->Headers = NULL;
1435 }
1436
1437 if (HttpInstance->CacheBody != NULL) {
1438 FreePool (HttpInstance->CacheBody);
1439 HttpInstance->CacheBody = NULL;
1440 }
1441
1442 if (HttpInstance->StatusCode >= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE) {
1443 Token->Status = EFI_HTTP_ERROR;
1444 } else {
1445 Token->Status = Status;
1446 }
1447
1448 gBS->SignalEvent (Token->Event);
1449
1450 return Status;
1451
1452 }
1453
1454
1455 /**
1456 The Response() function queues an HTTP response to this HTTP instance, similar to
1457 Receive() function in the EFI TCP driver. When the HTTP response is received successfully,
1458 or if there is an error, Status in token will be updated and Event will be signaled.
1459
1460 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1461 is received in the underlying TCP instance, the data will be parsed and Token will
1462 be populated with the response data. If the data received from the remote host
1463 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1464 (asynchronously) for more data to be sent from the remote host before signaling
1465 Event in Token.
1466
1467 It is the responsibility of the caller to allocate a buffer for Body and specify the
1468 size in BodyLength. If the remote host provides a response that contains a content
1469 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1470 BodyLength will be updated with the amount of bytes received and copied to Body. This
1471 allows the client to download a large file in chunks instead of into one contiguous
1472 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1473 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1474 token to underlying TCP instance. If data arrives in the receive buffer, up to
1475 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1476 BodyLength with the amount of bytes received and copied to Body.
1477
1478 If the HTTP driver does not have an open underlying TCP connection with the host
1479 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1480 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1481 an open TCP connection between client and host.
1482
1483 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1484 @param[in] Token Pointer to storage containing HTTP response token.
1485
1486 @retval EFI_SUCCESS Allocation succeeded.
1487 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1488 initialized.
1489 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1490 This is NULL.
1491 Token is NULL.
1492 Token->Message->Headers is NULL.
1493 Token->Message is NULL.
1494 Token->Message->Body is not NULL,
1495 Token->Message->BodyLength is non-zero, and
1496 Token->Message->Data is NULL, but a previous call to
1497 Response() has not been completed successfully.
1498 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1499 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1500 specified by response URL.
1501 **/
1502 EFI_STATUS
1503 EFIAPI
1504 EfiHttpResponse (
1505 IN EFI_HTTP_PROTOCOL *This,
1506 IN EFI_HTTP_TOKEN *Token
1507 )
1508 {
1509 EFI_STATUS Status;
1510 EFI_HTTP_MESSAGE *HttpMsg;
1511 HTTP_PROTOCOL *HttpInstance;
1512 HTTP_TOKEN_WRAP *Wrap;
1513
1514 if ((This == NULL) || (Token == NULL)) {
1515 return EFI_INVALID_PARAMETER;
1516 }
1517
1518 HttpMsg = Token->Message;
1519 if (HttpMsg == NULL) {
1520 return EFI_INVALID_PARAMETER;
1521 }
1522
1523 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
1524 ASSERT (HttpInstance != NULL);
1525
1526 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {
1527 return EFI_NOT_STARTED;
1528 }
1529
1530 //
1531 // Check whether the token already existed.
1532 //
1533 if (EFI_ERROR (NetMapIterate (&HttpInstance->RxTokens, HttpTokenExist, Token))) {
1534 return EFI_ACCESS_DENIED;
1535 }
1536
1537 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));
1538 if (Wrap == NULL) {
1539 return EFI_OUT_OF_RESOURCES;
1540 }
1541
1542 Wrap->HttpInstance = HttpInstance;
1543 Wrap->HttpToken = Token;
1544
1545 //
1546 // Notes: For Https, receive token wrapped in HTTP_TOKEN_WRAP is not used to
1547 // receive the https response. A special TlsRxToken is used for receiving TLS
1548 // related messages. It should be a blocking response.
1549 //
1550 if (!HttpInstance->UseHttps) {
1551 Status = HttpCreateTcpRxEvent (Wrap);
1552 if (EFI_ERROR (Status)) {
1553 goto Error;
1554 }
1555 }
1556
1557 Status = NetMapInsertTail (&HttpInstance->RxTokens, Token, Wrap);
1558 if (EFI_ERROR (Status)) {
1559 goto Error;
1560 }
1561
1562 //
1563 // If already have pending RxTokens, return directly.
1564 //
1565 if (NetMapGetCount (&HttpInstance->RxTokens) > 1) {
1566 return EFI_SUCCESS;
1567 }
1568
1569 return HttpResponseWorker (Wrap);
1570
1571 Error:
1572 if (Wrap != NULL) {
1573 if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) {
1574 gBS->CloseEvent (Wrap->TcpWrap.Rx4Token.CompletionToken.Event);
1575 }
1576
1577 if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {
1578 gBS->CloseEvent (Wrap->TcpWrap.Rx6Token.CompletionToken.Event);
1579 }
1580 FreePool (Wrap);
1581 }
1582
1583 return Status;
1584 }
1585
1586 /**
1587 The Poll() function can be used by network drivers and applications to increase the
1588 rate that data packets are moved between the communication devices and the transmit
1589 and receive queues.
1590
1591 In some systems, the periodic timer event in the managed network driver may not poll
1592 the underlying communications device fast enough to transmit and/or receive all data
1593 packets without missing incoming packets or dropping outgoing packets. Drivers and
1594 applications that are experiencing packet loss should try calling the Poll() function
1595 more often.
1596
1597 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1598
1599 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1600 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1601 @retval EFI_INVALID_PARAMETER This is NULL.
1602 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1603 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1604
1605 **/
1606 EFI_STATUS
1607 EFIAPI
1608 EfiHttpPoll (
1609 IN EFI_HTTP_PROTOCOL *This
1610 )
1611 {
1612 EFI_STATUS Status;
1613 HTTP_PROTOCOL *HttpInstance;
1614
1615 if (This == NULL) {
1616 return EFI_INVALID_PARAMETER;
1617 }
1618
1619 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
1620 ASSERT (HttpInstance != NULL);
1621
1622 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {
1623 return EFI_NOT_STARTED;
1624 }
1625
1626 if (HttpInstance->LocalAddressIsIPv6) {
1627 if (HttpInstance->Tcp6 == NULL) {
1628 return EFI_NOT_STARTED;
1629 }
1630 Status = HttpInstance->Tcp6->Poll (HttpInstance->Tcp6);
1631 } else {
1632 if (HttpInstance->Tcp4 == NULL) {
1633 return EFI_NOT_STARTED;
1634 }
1635 Status = HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);
1636 }
1637
1638 DispatchDpc ();
1639
1640 return Status;
1641 }