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