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