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