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