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