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