VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/NetworkPkg/HttpDxe/HttpImpl.c@ 105668

Last change on this file since 105668 was 99404, checked in by vboxsync, 2 years ago

Devices/EFI/FirmwareNew: Update to edk2-stable202302 and make it build, bugref:4643

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette