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