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