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