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