]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/HttpDxe/HttpImpl.c
NetworkPkg: Add HTTP Additional Event Notifications
[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 return EFI_INVALID_PARAMETER;
66 }
67
68 if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) {
69 return EFI_NOT_STARTED;
70 }
71
72 HttpConfigData->HttpVersion = HttpInstance->HttpVersion;
73 HttpConfigData->TimeOutMillisec = HttpInstance->TimeOutMillisec;
74 HttpConfigData->LocalAddressIsIPv6 = HttpInstance->LocalAddressIsIPv6;
75
76 if (HttpInstance->LocalAddressIsIPv6) {
77 CopyMem (
78 HttpConfigData->AccessPoint.IPv6Node,
79 &HttpInstance->Ipv6Node,
80 sizeof (HttpInstance->Ipv6Node)
81 );
82 } else {
83 CopyMem (
84 HttpConfigData->AccessPoint.IPv4Node,
85 &HttpInstance->IPv4Node,
86 sizeof (HttpInstance->IPv4Node)
87 );
88 }
89
90 return EFI_SUCCESS;
91 }
92
93 /**
94 Initialize or brutally reset the operational parameters for this EFI HTTP instance.
95
96 The Configure() function does the following:
97 When HttpConfigData is not NULL Initialize this EFI HTTP instance by configuring
98 timeout, local address, port, etc.
99 When HttpConfigData is NULL, reset this EFI HTTP instance by closing all active
100 connections with remote hosts, canceling all asynchronous tokens, and flush request
101 and response buffers without informing the appropriate hosts.
102
103 No other EFI HTTP function can be executed by this instance until the Configure()
104 function is executed and returns successfully.
105
106 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
107 @param[in] HttpConfigData Pointer to the configure data to configure the instance.
108
109 @retval EFI_SUCCESS Operation succeeded.
110 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
111 This is NULL.
112 HttpConfigData->LocalAddressIsIPv6 is FALSE and
113 HttpConfigData->AccessPoint.IPv4Node is NULL.
114 HttpConfigData->LocalAddressIsIPv6 is TRUE and
115 HttpConfigData->AccessPoint.IPv6Node is NULL.
116 @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling
117 Configure() with NULL to reset it.
118 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
119 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when
120 executing Configure().
121 @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported
122 in the implementation.
123 **/
124 EFI_STATUS
125 EFIAPI
126 EfiHttpConfigure (
127 IN EFI_HTTP_PROTOCOL *This,
128 IN EFI_HTTP_CONFIG_DATA *HttpConfigData OPTIONAL
129 )
130 {
131 HTTP_PROTOCOL *HttpInstance;
132 EFI_STATUS Status;
133
134 //
135 // Check input parameters.
136 //
137 if (This == NULL ||
138 (HttpConfigData != NULL &&
139 ((HttpConfigData->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv6Node == NULL) ||
140 (!HttpConfigData->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv4Node == NULL)))) {
141 return EFI_INVALID_PARAMETER;
142 }
143
144 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
145 ASSERT (HttpInstance->Service != NULL);
146
147 if (HttpConfigData != NULL) {
148
149 if (HttpConfigData->HttpVersion >= HttpVersionUnsupported) {
150 return EFI_UNSUPPORTED;
151 }
152
153 //
154 // Now configure this HTTP instance.
155 //
156 if (HttpInstance->State != HTTP_STATE_UNCONFIGED) {
157 return EFI_ALREADY_STARTED;
158 }
159
160 HttpInstance->HttpVersion = HttpConfigData->HttpVersion;
161 HttpInstance->TimeOutMillisec = HttpConfigData->TimeOutMillisec;
162 HttpInstance->LocalAddressIsIPv6 = HttpConfigData->LocalAddressIsIPv6;
163
164 if (HttpConfigData->LocalAddressIsIPv6) {
165 CopyMem (
166 &HttpInstance->Ipv6Node,
167 HttpConfigData->AccessPoint.IPv6Node,
168 sizeof (HttpInstance->Ipv6Node)
169 );
170 } else {
171 CopyMem (
172 &HttpInstance->IPv4Node,
173 HttpConfigData->AccessPoint.IPv4Node,
174 sizeof (HttpInstance->IPv4Node)
175 );
176 }
177
178 //
179 // Creat Tcp child
180 //
181 Status = HttpInitProtocol (HttpInstance, HttpInstance->LocalAddressIsIPv6);
182 if (EFI_ERROR (Status)) {
183 return Status;
184 }
185
186 HttpInstance->State = HTTP_STATE_HTTP_CONFIGED;
187 return EFI_SUCCESS;
188
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 /**
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 **/
226 EFI_STATUS
227 EFIAPI
228 EfiHttpRequest (
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 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 return EFI_INVALID_PARAMETER;
308 }
309
310 //
311 // For PUT/POST/PATCH, we need to have the TCP already configured. Bail out if it is not!
312 //
313 if (HttpInstance->State < HTTP_STATE_TCP_CONFIGED) {
314 return EFI_INVALID_PARAMETER;
315 }
316
317 //
318 // We need to have the Message Body for sending the HTTP message across in these cases.
319 //
320 if (HttpMsg->Body == NULL || HttpMsg->BodyLength == 0) {
321 return EFI_INVALID_PARAMETER;
322 }
323
324 //
325 // Use existing TCP instance to transmit the packet.
326 //
327 Configure = FALSE;
328 ReConfigure = FALSE;
329 } else {
330 //
331 // Check whether the token already existed.
332 //
333 if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTokenExist, Token))) {
334 return EFI_ACCESS_DENIED;
335 }
336
337 //
338 // Parse the URI of the remote host.
339 //
340 Url = HttpInstance->Url;
341 UrlLen = StrLen (Request->Url) + 1;
342 if (UrlLen > HTTP_URL_BUFFER_LEN) {
343 Url = AllocateZeroPool (UrlLen);
344 if (Url == NULL) {
345 return EFI_OUT_OF_RESOURCES;
346 }
347 FreePool (HttpInstance->Url);
348 HttpInstance->Url = Url;
349 }
350
351
352 UnicodeStrToAsciiStrS (Request->Url, Url, UrlLen);
353
354 //
355 // From the information in Url, the HTTP instance will
356 // be able to determine whether to use http or https.
357 //
358 HttpInstance->UseHttps = IsHttpsUrl (Url);
359
360 //
361 // HTTP is disabled, return directly if the URI is not HTTPS.
362 //
363 if (!PcdGetBool (PcdAllowHttpConnections) && !(HttpInstance->UseHttps)) {
364
365 DEBUG ((EFI_D_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 // If Configure is TRUE, it indicates the first time to call Request();
430 // If ReConfigure is TRUE, it indicates the request URL is not same
431 // with the previous call to Request();
432 //
433 Configure = TRUE;
434 ReConfigure = TRUE;
435
436 if (HttpInstance->RemoteHost == NULL) {
437 //
438 // Request() is called the first time.
439 //
440 ReConfigure = FALSE;
441 } else {
442 if ((HttpInstance->RemotePort == RemotePort) &&
443 (AsciiStrCmp (HttpInstance->RemoteHost, HostName) == 0) &&
444 (!HttpInstance->UseHttps || (HttpInstance->UseHttps &&
445 !TlsConfigure &&
446 HttpInstance->TlsSessionState == EfiTlsSessionDataTransferring))) {
447 //
448 // Host Name and port number of the request URL are the same with previous call to Request().
449 // If Https protocol used, the corresponding SessionState is EfiTlsSessionDataTransferring.
450 // Check whether previous TCP packet sent out.
451 //
452
453 if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTcpNotReady, NULL))) {
454 //
455 // Wrap the HTTP token in HTTP_TOKEN_WRAP
456 //
457 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));
458 if (Wrap == NULL) {
459 Status = EFI_OUT_OF_RESOURCES;
460 goto Error1;
461 }
462
463 Wrap->HttpToken = Token;
464 Wrap->HttpInstance = HttpInstance;
465
466 Status = HttpCreateTcpTxEvent (Wrap);
467 if (EFI_ERROR (Status)) {
468 goto Error1;
469 }
470
471 Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap);
472 if (EFI_ERROR (Status)) {
473 goto Error1;
474 }
475
476 Wrap->TcpWrap.Method = Request->Method;
477
478 FreePool (HostName);
479
480 HttpUrlFreeParser (UrlParser);
481
482 //
483 // Queue the HTTP token and return.
484 //
485 return EFI_SUCCESS;
486 } else {
487 //
488 // Use existing TCP instance to transmit the packet.
489 //
490 Configure = FALSE;
491 ReConfigure = FALSE;
492 }
493 } else {
494 //
495 // Need close existing TCP instance and create a new TCP instance for data transmit.
496 //
497 if (HttpInstance->RemoteHost != NULL) {
498 FreePool (HttpInstance->RemoteHost);
499 HttpInstance->RemoteHost = NULL;
500 HttpInstance->RemotePort = 0;
501 }
502 }
503 }
504 }
505
506 if (Configure) {
507 //
508 // Parse Url for IPv4 or IPv6 address, if failed, perform DNS resolution.
509 //
510 if (!HttpInstance->LocalAddressIsIPv6) {
511 Status = NetLibAsciiStrToIp4 (HostName, &HttpInstance->RemoteAddr);
512 } else {
513 Status = HttpUrlGetIp6 (Url, UrlParser, &HttpInstance->RemoteIpv6Addr);
514 }
515
516 if (EFI_ERROR (Status)) {
517 HostNameSize = AsciiStrSize (HostName);
518 HostNameStr = AllocateZeroPool (HostNameSize * sizeof (CHAR16));
519 if (HostNameStr == NULL) {
520 Status = EFI_OUT_OF_RESOURCES;
521 goto Error1;
522 }
523
524 AsciiStrToUnicodeStrS (HostName, HostNameStr, HostNameSize);
525 if (!HttpInstance->LocalAddressIsIPv6) {
526 Status = HttpDns4 (HttpInstance, HostNameStr, &HttpInstance->RemoteAddr);
527 } else {
528 Status = HttpDns6 (HttpInstance, HostNameStr, &HttpInstance->RemoteIpv6Addr);
529 }
530 HttpNotify (HttpEventDns, Status);
531
532 FreePool (HostNameStr);
533 if (EFI_ERROR (Status)) {
534 DEBUG ((EFI_D_ERROR, "Error: Could not retrieve the host address from DNS server.\n"));
535 goto Error1;
536 }
537 }
538
539 //
540 // Save the RemotePort and RemoteHost.
541 //
542 ASSERT (HttpInstance->RemoteHost == NULL);
543 HttpInstance->RemotePort = RemotePort;
544 HttpInstance->RemoteHost = HostName;
545 HostName = NULL;
546 }
547
548 if (ReConfigure) {
549 //
550 // The request URL is different from previous calls to Request(), close existing TCP instance.
551 //
552 if (!HttpInstance->LocalAddressIsIPv6) {
553 ASSERT (HttpInstance->Tcp4 != NULL);
554 } else {
555 ASSERT (HttpInstance->Tcp6 != NULL);
556 }
557
558 if (HttpInstance->UseHttps && !TlsConfigure) {
559 Status = TlsCloseSession (HttpInstance);
560 if (EFI_ERROR (Status)) {
561 goto Error1;
562 }
563
564 TlsCloseTxRxEvent (HttpInstance);
565 }
566
567 HttpCloseConnection (HttpInstance);
568 EfiHttpCancel (This, NULL);
569 }
570
571 //
572 // Wrap the HTTP token in HTTP_TOKEN_WRAP
573 //
574 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));
575 if (Wrap == NULL) {
576 Status = EFI_OUT_OF_RESOURCES;
577 goto Error1;
578 }
579
580 Wrap->HttpToken = Token;
581 Wrap->HttpInstance = HttpInstance;
582 if (Request != NULL) {
583 Wrap->TcpWrap.Method = Request->Method;
584 }
585
586 Status = HttpInitSession (
587 HttpInstance,
588 Wrap,
589 Configure || ReConfigure,
590 TlsConfigure
591 );
592 HttpNotify (HttpEventInitSession, Status);
593 if (EFI_ERROR (Status)) {
594 goto Error2;
595 }
596
597 if (!Configure && !ReConfigure && !TlsConfigure) {
598 //
599 // For the new HTTP token, create TX TCP token events.
600 //
601 Status = HttpCreateTcpTxEvent (Wrap);
602 if (EFI_ERROR (Status)) {
603 goto Error1;
604 }
605 }
606
607 //
608 // Create request message.
609 //
610 FileUrl = Url;
611 if (Url != NULL && *FileUrl != '/') {
612 //
613 // Convert the absolute-URI to the absolute-path
614 //
615 while (*FileUrl != ':') {
616 FileUrl++;
617 }
618 if ((*(FileUrl+1) == '/') && (*(FileUrl+2) == '/')) {
619 FileUrl += 3;
620 while (*FileUrl != '/') {
621 FileUrl++;
622 }
623 } else {
624 Status = EFI_INVALID_PARAMETER;
625 goto Error3;
626 }
627 }
628
629 Status = HttpGenRequestMessage (HttpMsg, FileUrl, &RequestMsg, &RequestMsgSize);
630
631 if (EFI_ERROR (Status) || NULL == RequestMsg) {
632 goto Error3;
633 }
634
635 //
636 // Every request we insert a TxToken and a response call would remove the TxToken.
637 // In cases of PUT/POST/PATCH, after an initial request-response pair, we would do a
638 // continuous request without a response call. So, in such cases, where Request
639 // structure is NULL, we would not insert a TxToken.
640 //
641 if (Request != NULL) {
642 Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap);
643 if (EFI_ERROR (Status)) {
644 goto Error4;
645 }
646 }
647
648 //
649 // Transmit the request message.
650 //
651 Status = HttpTransmitTcp (
652 HttpInstance,
653 Wrap,
654 (UINT8*) RequestMsg,
655 RequestMsgSize
656 );
657 if (EFI_ERROR (Status)) {
658 goto Error5;
659 }
660
661 DispatchDpc ();
662
663 if (HostName != NULL) {
664 FreePool (HostName);
665 }
666
667 if (UrlParser != NULL) {
668 HttpUrlFreeParser (UrlParser);
669 }
670
671 return EFI_SUCCESS;
672
673 Error5:
674 //
675 // We would have inserted a TxToken only if Request structure is not NULL.
676 // Hence check before we do a remove in this error case.
677 //
678 if (Request != NULL) {
679 NetMapRemoveTail (&HttpInstance->TxTokens, NULL);
680 }
681
682 Error4:
683 if (RequestMsg != NULL) {
684 FreePool (RequestMsg);
685 }
686
687 Error3:
688 if (HttpInstance->UseHttps) {
689 TlsCloseSession (HttpInstance);
690 TlsCloseTxRxEvent (HttpInstance);
691 }
692
693 Error2:
694 HttpCloseConnection (HttpInstance);
695
696 HttpCloseTcpConnCloseEvent (HttpInstance);
697 if (NULL != Wrap->TcpWrap.Tx4Token.CompletionToken.Event) {
698 gBS->CloseEvent (Wrap->TcpWrap.Tx4Token.CompletionToken.Event);
699 Wrap->TcpWrap.Tx4Token.CompletionToken.Event = NULL;
700 }
701 if (NULL != Wrap->TcpWrap.Tx6Token.CompletionToken.Event) {
702 gBS->CloseEvent (Wrap->TcpWrap.Tx6Token.CompletionToken.Event);
703 Wrap->TcpWrap.Tx6Token.CompletionToken.Event = NULL;
704 }
705
706 Error1:
707 if (HostName != NULL) {
708 FreePool (HostName);
709 }
710 if (Wrap != NULL) {
711 FreePool (Wrap);
712 }
713 if (UrlParser != NULL) {
714 HttpUrlFreeParser (UrlParser);
715 }
716
717 return Status;
718
719 }
720
721 /**
722 Cancel a user's Token.
723
724 @param[in] Map The HTTP instance's token queue.
725 @param[in] Item Object container for one HTTP token and token's wrap.
726 @param[in] Context The user's token to cancel.
727
728 @retval EFI_SUCCESS Continue to check the next Item.
729 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
730
731 **/
732 EFI_STATUS
733 EFIAPI
734 HttpCancelTokens (
735 IN NET_MAP *Map,
736 IN NET_MAP_ITEM *Item,
737 IN VOID *Context
738 )
739 {
740 EFI_HTTP_TOKEN *Token;
741 HTTP_TOKEN_WRAP *Wrap;
742 HTTP_PROTOCOL *HttpInstance;
743
744 Token = (EFI_HTTP_TOKEN *) Context;
745
746 //
747 // Return EFI_SUCCESS to check the next item in the map if
748 // this one doesn't match.
749 //
750 if ((Token != NULL) && (Token != Item->Key)) {
751 return EFI_SUCCESS;
752 }
753
754 Wrap = (HTTP_TOKEN_WRAP *) Item->Value;
755 ASSERT (Wrap != NULL);
756 HttpInstance = Wrap->HttpInstance;
757
758 if (!HttpInstance->LocalAddressIsIPv6) {
759 if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) {
760 //
761 // Cancel the Token before close its Event.
762 //
763 HttpInstance->Tcp4->Cancel (HttpInstance->Tcp4, &Wrap->TcpWrap.Rx4Token.CompletionToken);
764
765 //
766 // Dispatch the DPC queued by the NotifyFunction of the canceled token's events.
767 //
768 DispatchDpc ();
769 }
770 } else {
771 if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {
772 //
773 // Cancel the Token before close its Event.
774 //
775 HttpInstance->Tcp6->Cancel (HttpInstance->Tcp6, &Wrap->TcpWrap.Rx6Token.CompletionToken);
776
777 //
778 // Dispatch the DPC queued by the NotifyFunction of the canceled token's events.
779 //
780 DispatchDpc ();
781 }
782 }
783
784 //
785 // If only one item is to be cancel, return EFI_ABORTED to stop
786 // iterating the map any more.
787 //
788 if (Token != NULL) {
789 return EFI_ABORTED;
790 }
791
792 return EFI_SUCCESS;
793 }
794
795 /**
796 Cancel the user's receive/transmit request. It is the worker function of
797 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the
798 token.
799
800 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
801 @param[in] Token The token to cancel. If NULL, all token will be
802 cancelled.
803
804 @retval EFI_SUCCESS The token is cancelled.
805 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.
806 @retval Others Other error as indicated.
807
808 **/
809 EFI_STATUS
810 HttpCancel (
811 IN HTTP_PROTOCOL *HttpInstance,
812 IN EFI_HTTP_TOKEN *Token
813 )
814 {
815 EFI_STATUS Status;
816
817 //
818 // First check the tokens queued by EfiHttpRequest().
819 //
820 Status = NetMapIterate (&HttpInstance->TxTokens, HttpCancelTokens, Token);
821 if (EFI_ERROR (Status)) {
822 if (Token != NULL) {
823 if (Status == EFI_ABORTED) {
824 return EFI_SUCCESS;
825 }
826 } else {
827 return Status;
828 }
829 }
830
831 if (!HttpInstance->UseHttps) {
832 //
833 // Then check the tokens queued by EfiHttpResponse(), except for Https.
834 //
835 Status = NetMapIterate (&HttpInstance->RxTokens, HttpCancelTokens, Token);
836 if (EFI_ERROR (Status)) {
837 if (Token != NULL) {
838 if (Status == EFI_ABORTED) {
839 return EFI_SUCCESS;
840 } else {
841 return EFI_NOT_FOUND;
842 }
843 } else {
844 return Status;
845 }
846 }
847 } else {
848 if (!HttpInstance->LocalAddressIsIPv6) {
849 HttpInstance->Tcp4->Cancel (HttpInstance->Tcp4, &HttpInstance->Tcp4TlsRxToken.CompletionToken);
850 } else {
851 HttpInstance->Tcp6->Cancel (HttpInstance->Tcp6, &HttpInstance->Tcp6TlsRxToken.CompletionToken);
852 }
853 }
854
855 return EFI_SUCCESS;
856 }
857
858
859 /**
860 Abort an asynchronous HTTP request or response token.
861
862 The Cancel() function aborts a pending HTTP request or response transaction. If
863 Token is not NULL and the token is in transmit or receive queues when it is being
864 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
865 be signaled. If the token is not in one of the queues, which usually means that the
866 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
867 all asynchronous tokens issued by Request() or Response() will be aborted.
868
869 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
870 @param[in] Token Point to storage containing HTTP request or response
871 token.
872
873 @retval EFI_SUCCESS Request and Response queues are successfully flushed.
874 @retval EFI_INVALID_PARAMETER This is NULL.
875 @retval EFI_NOT_STARTED This instance hasn't been configured.
876 @retval EFI_NOT_FOUND The asynchronous request or response token is not
877 found.
878 @retval EFI_UNSUPPORTED The implementation does not support this function.
879
880 **/
881 EFI_STATUS
882 EFIAPI
883 EfiHttpCancel (
884 IN EFI_HTTP_PROTOCOL *This,
885 IN EFI_HTTP_TOKEN *Token
886 )
887 {
888 HTTP_PROTOCOL *HttpInstance;
889
890 if (This == NULL) {
891 return EFI_INVALID_PARAMETER;
892 }
893
894 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
895
896 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {
897 return EFI_NOT_STARTED;
898 }
899
900 return HttpCancel (HttpInstance, Token);
901
902 }
903
904 /**
905 A callback function to intercept events during message parser.
906
907 This function will be invoked during HttpParseMessageBody() with various events type. An error
908 return status of the callback function will cause the HttpParseMessageBody() aborted.
909
910 @param[in] EventType Event type of this callback call.
911 @param[in] Data A pointer to data buffer.
912 @param[in] Length Length in bytes of the Data.
913 @param[in] Context Callback context set by HttpInitMsgParser().
914
915 @retval EFI_SUCCESS Continue to parser the message body.
916
917 **/
918 EFI_STATUS
919 EFIAPI
920 HttpBodyParserCallback (
921 IN HTTP_BODY_PARSE_EVENT EventType,
922 IN CHAR8 *Data,
923 IN UINTN Length,
924 IN VOID *Context
925 )
926 {
927 HTTP_CALLBACK_DATA *CallbackData;
928 HTTP_TOKEN_WRAP *Wrap;
929 UINTN BodyLength;
930 CHAR8 *Body;
931
932 if (EventType != BodyParseEventOnComplete) {
933 return EFI_SUCCESS;
934 }
935
936 if (Data == NULL || Length != 0 || Context == NULL) {
937 return EFI_SUCCESS;
938 }
939
940 CallbackData = (HTTP_CALLBACK_DATA *) Context;
941
942 Wrap = (HTTP_TOKEN_WRAP *) (CallbackData->Wrap);
943 Body = CallbackData->ParseData;
944 BodyLength = CallbackData->ParseDataLength;
945
946 if (Data < Body + BodyLength) {
947 Wrap->HttpInstance->NextMsg = Data;
948 } else {
949 Wrap->HttpInstance->NextMsg = NULL;
950 }
951
952 return EFI_SUCCESS;
953 }
954
955 /**
956 The work function of EfiHttpResponse().
957
958 @param[in] Wrap Pointer to HTTP token's wrap data.
959
960 @retval EFI_SUCCESS Allocation succeeded.
961 @retval EFI_OUT_OF_RESOURCES Failed to complete the operation due to lack of resources.
962 @retval EFI_NOT_READY Can't find a corresponding Tx4Token/Tx6Token or
963 the EFI_HTTP_UTILITIES_PROTOCOL is not available.
964
965 **/
966 EFI_STATUS
967 HttpResponseWorker (
968 IN HTTP_TOKEN_WRAP *Wrap
969 )
970 {
971 EFI_STATUS Status;
972 EFI_HTTP_MESSAGE *HttpMsg;
973 CHAR8 *EndofHeader;
974 CHAR8 *HttpHeaders;
975 UINTN SizeofHeaders;
976 UINTN BufferSize;
977 UINTN StatusCode;
978 CHAR8 *Tmp;
979 CHAR8 *HeaderTmp;
980 CHAR8 *StatusCodeStr;
981 UINTN BodyLen;
982 HTTP_PROTOCOL *HttpInstance;
983 EFI_HTTP_TOKEN *Token;
984 NET_MAP_ITEM *Item;
985 HTTP_TOKEN_WRAP *ValueInItem;
986 UINTN HdrLen;
987 NET_FRAGMENT Fragment;
988 UINT32 TimeoutValue;
989
990 if (Wrap == NULL || Wrap->HttpInstance == NULL) {
991 return EFI_INVALID_PARAMETER;
992 }
993
994 HttpInstance = Wrap->HttpInstance;
995 Token = Wrap->HttpToken;
996 HttpMsg = Token->Message;
997
998 HttpInstance->EndofHeader = NULL;
999 HttpInstance->HttpHeaders = NULL;
1000 HttpMsg->Headers = NULL;
1001 HttpHeaders = NULL;
1002 SizeofHeaders = 0;
1003 BufferSize = 0;
1004 EndofHeader = NULL;
1005 ValueInItem = NULL;
1006 Fragment.Len = 0;
1007 Fragment.Bulk = NULL;
1008
1009 if (HttpMsg->Data.Response != NULL) {
1010 //
1011 // Check whether we have cached header from previous call.
1012 //
1013 if ((HttpInstance->CacheBody != NULL) && (HttpInstance->NextMsg != NULL)) {
1014 //
1015 // The data is stored at [NextMsg, CacheBody + CacheLen].
1016 //
1017 HdrLen = HttpInstance->CacheBody + HttpInstance->CacheLen - HttpInstance->NextMsg;
1018 HttpHeaders = AllocateZeroPool (HdrLen);
1019 if (HttpHeaders == NULL) {
1020 Status = EFI_OUT_OF_RESOURCES;
1021 goto Error;
1022 }
1023
1024 CopyMem (HttpHeaders, HttpInstance->NextMsg, HdrLen);
1025 FreePool (HttpInstance->CacheBody);
1026 HttpInstance->CacheBody = NULL;
1027 HttpInstance->NextMsg = NULL;
1028 HttpInstance->CacheOffset = 0;
1029 SizeofHeaders = HdrLen;
1030 BufferSize = HttpInstance->CacheLen;
1031
1032 //
1033 // Check whether we cached the whole HTTP headers.
1034 //
1035 EndofHeader = AsciiStrStr (HttpHeaders, HTTP_END_OF_HDR_STR);
1036 }
1037
1038 HttpInstance->EndofHeader = &EndofHeader;
1039 HttpInstance->HttpHeaders = &HttpHeaders;
1040
1041
1042 if (HttpInstance->TimeoutEvent == NULL) {
1043 //
1044 // Create TimeoutEvent for response
1045 //
1046 Status = gBS->CreateEvent (
1047 EVT_TIMER,
1048 TPL_CALLBACK,
1049 NULL,
1050 NULL,
1051 &HttpInstance->TimeoutEvent
1052 );
1053 if (EFI_ERROR (Status)) {
1054 goto Error;
1055 }
1056 }
1057
1058 //
1059 // Get HTTP timeout value
1060 //
1061 TimeoutValue = PcdGet32 (PcdHttpIoTimeout);
1062
1063 //
1064 // Start the timer, and wait Timeout seconds to receive the header packet.
1065 //
1066 Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, TimeoutValue * TICKS_PER_MS);
1067 if (EFI_ERROR (Status)) {
1068 goto Error;
1069 }
1070
1071 Status = HttpTcpReceiveHeader (HttpInstance, &SizeofHeaders, &BufferSize, HttpInstance->TimeoutEvent);
1072
1073 gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0);
1074
1075 if (EFI_ERROR (Status)) {
1076 goto Error;
1077 }
1078
1079 ASSERT (HttpHeaders != NULL);
1080
1081 //
1082 // Cache the part of body.
1083 //
1084 BodyLen = BufferSize - (EndofHeader - HttpHeaders);
1085 if (BodyLen > 0) {
1086 if (HttpInstance->CacheBody != NULL) {
1087 FreePool (HttpInstance->CacheBody);
1088 }
1089
1090 HttpInstance->CacheBody = AllocateZeroPool (BodyLen);
1091 if (HttpInstance->CacheBody == NULL) {
1092 Status = EFI_OUT_OF_RESOURCES;
1093 goto Error;
1094 }
1095
1096 CopyMem (HttpInstance->CacheBody, EndofHeader, BodyLen);
1097 HttpInstance->CacheLen = BodyLen;
1098 }
1099
1100 //
1101 // Search for Status Code.
1102 //
1103 StatusCodeStr = HttpHeaders + AsciiStrLen (HTTP_VERSION_STR) + 1;
1104 if (StatusCodeStr == NULL) {
1105 Status = EFI_NOT_READY;
1106 goto Error;
1107 }
1108
1109 StatusCode = AsciiStrDecimalToUintn (StatusCodeStr);
1110
1111 //
1112 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
1113 //
1114 Tmp = AsciiStrStr (HttpHeaders, HTTP_CRLF_STR);
1115 if (Tmp == NULL) {
1116 Status = EFI_NOT_READY;
1117 goto Error;
1118 }
1119
1120 //
1121 // We could have response with just a HTTP message and no headers. For Example,
1122 // "100 Continue". In such cases, we would not want to unnecessarily call a Parse
1123 // method. A "\r\n" following Tmp string again would indicate an end. Compare and
1124 // set SizeofHeaders to 0.
1125 //
1126 Tmp = Tmp + AsciiStrLen (HTTP_CRLF_STR);
1127 if (CompareMem (Tmp, HTTP_CRLF_STR, AsciiStrLen (HTTP_CRLF_STR)) == 0) {
1128 SizeofHeaders = 0;
1129 } else {
1130 SizeofHeaders = SizeofHeaders - (Tmp - HttpHeaders);
1131 }
1132
1133 HttpMsg->Data.Response->StatusCode = HttpMappingToStatusCode (StatusCode);
1134 HttpInstance->StatusCode = StatusCode;
1135
1136 Status = EFI_NOT_READY;
1137 ValueInItem = NULL;
1138
1139 //
1140 // In cases of PUT/POST/PATCH, after an initial request-response pair, we would do a
1141 // continuous request without a response call. So, we would not do an insert of
1142 // TxToken. After we have sent the complete file, we will call a response to get
1143 // a final response from server. In such a case, we would not have any TxTokens.
1144 // Hence, check that case before doing a NetMapRemoveHead.
1145 //
1146 if (!NetMapIsEmpty (&HttpInstance->TxTokens)) {
1147 NetMapRemoveHead (&HttpInstance->TxTokens, (VOID**) &ValueInItem);
1148 if (ValueInItem == NULL) {
1149 goto Error;
1150 }
1151
1152 //
1153 // The first Tx Token not transmitted yet, insert back and return error.
1154 //
1155 if (!ValueInItem->TcpWrap.IsTxDone) {
1156 goto Error2;
1157 }
1158 }
1159
1160 if (SizeofHeaders != 0) {
1161 HeaderTmp = AllocateZeroPool (SizeofHeaders);
1162 if (HeaderTmp == NULL) {
1163 Status = EFI_OUT_OF_RESOURCES;
1164 goto Error2;
1165 }
1166
1167 CopyMem (HeaderTmp, Tmp, SizeofHeaders);
1168 FreePool (HttpHeaders);
1169 HttpHeaders = HeaderTmp;
1170
1171 //
1172 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.
1173 //
1174 if (mHttpUtilities == NULL) {
1175 Status = EFI_NOT_READY;
1176 goto Error2;
1177 }
1178
1179 //
1180 // Parse the HTTP header into array of key/value pairs.
1181 //
1182 Status = mHttpUtilities->Parse (
1183 mHttpUtilities,
1184 HttpHeaders,
1185 SizeofHeaders,
1186 &HttpMsg->Headers,
1187 &HttpMsg->HeaderCount
1188 );
1189 if (EFI_ERROR (Status)) {
1190 goto Error2;
1191 }
1192
1193 FreePool (HttpHeaders);
1194 HttpHeaders = NULL;
1195
1196
1197 //
1198 // Init message-body parser by header information.
1199 //
1200 Status = HttpInitMsgParser (
1201 HttpInstance->Method,
1202 HttpMsg->Data.Response->StatusCode,
1203 HttpMsg->HeaderCount,
1204 HttpMsg->Headers,
1205 HttpBodyParserCallback,
1206 (VOID *) (&HttpInstance->CallbackData),
1207 &HttpInstance->MsgParser
1208 );
1209 if (EFI_ERROR (Status)) {
1210 goto Error2;
1211 }
1212
1213 //
1214 // Check whether we received a complete HTTP message.
1215 //
1216 if (HttpInstance->CacheBody != NULL) {
1217 //
1218 // Record the CallbackData data.
1219 //
1220 HttpInstance->CallbackData.Wrap = (VOID *) Wrap;
1221 HttpInstance->CallbackData.ParseData = (VOID *) HttpInstance->CacheBody;
1222 HttpInstance->CallbackData.ParseDataLength = HttpInstance->CacheLen;
1223
1224 //
1225 // Parse message with CallbackData data.
1226 //
1227 Status = HttpParseMessageBody (HttpInstance->MsgParser, HttpInstance->CacheLen, HttpInstance->CacheBody);
1228 if (EFI_ERROR (Status)) {
1229 goto Error2;
1230 }
1231 }
1232
1233 if (HttpIsMessageComplete (HttpInstance->MsgParser)) {
1234 //
1235 // Free the MsgParse since we already have a full HTTP message.
1236 //
1237 HttpFreeMsgParser (HttpInstance->MsgParser);
1238 HttpInstance->MsgParser = NULL;
1239 }
1240 }
1241
1242 if ((HttpMsg->Body == NULL) || (HttpMsg->BodyLength == 0)) {
1243 Status = EFI_SUCCESS;
1244 goto Exit;
1245 }
1246 }
1247
1248 //
1249 // Receive the response body.
1250 //
1251 BodyLen = 0;
1252
1253 //
1254 // First check whether we cached some data.
1255 //
1256 if (HttpInstance->CacheBody != NULL) {
1257 //
1258 // Calculate the length of the cached data.
1259 //
1260 if (HttpInstance->NextMsg != NULL) {
1261 //
1262 // We have a cached HTTP message which includes a part of HTTP header of next message.
1263 //
1264 BodyLen = HttpInstance->NextMsg - (HttpInstance->CacheBody + HttpInstance->CacheOffset);
1265 } else {
1266 BodyLen = HttpInstance->CacheLen - HttpInstance->CacheOffset;
1267 }
1268
1269 if (BodyLen > 0) {
1270 //
1271 // We have some cached data. Just copy the data and return.
1272 //
1273 if (HttpMsg->BodyLength < BodyLen) {
1274 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, HttpMsg->BodyLength);
1275 HttpInstance->CacheOffset = HttpInstance->CacheOffset + HttpMsg->BodyLength;
1276 } else {
1277 //
1278 // Copy all cached data out.
1279 //
1280 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, BodyLen);
1281 HttpInstance->CacheOffset = BodyLen + HttpInstance->CacheOffset;
1282 HttpMsg->BodyLength = BodyLen;
1283
1284 if (HttpInstance->NextMsg == NULL) {
1285 //
1286 // There is no HTTP header of next message. Just free the cache buffer.
1287 //
1288 FreePool (HttpInstance->CacheBody);
1289 HttpInstance->CacheBody = NULL;
1290 HttpInstance->NextMsg = NULL;
1291 HttpInstance->CacheOffset = 0;
1292 }
1293 }
1294 //
1295 // Return since we already received required data.
1296 //
1297 Status = EFI_SUCCESS;
1298 goto Exit;
1299 }
1300
1301 if (BodyLen == 0 && HttpInstance->MsgParser == NULL) {
1302 //
1303 // We received a complete HTTP message, and we don't have more data to return to caller.
1304 //
1305 HttpMsg->BodyLength = 0;
1306 Status = EFI_SUCCESS;
1307 goto Exit;
1308 }
1309 }
1310
1311 ASSERT (HttpInstance->MsgParser != NULL);
1312
1313 //
1314 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1315 //
1316 if (!HttpInstance->UseHttps) {
1317 Status = HttpTcpReceiveBody (Wrap, HttpMsg);
1318
1319 if (EFI_ERROR (Status)) {
1320 goto Error2;
1321 }
1322
1323 } else {
1324 if (HttpInstance->TimeoutEvent == NULL) {
1325 //
1326 // Create TimeoutEvent for response
1327 //
1328 Status = gBS->CreateEvent (
1329 EVT_TIMER,
1330 TPL_CALLBACK,
1331 NULL,
1332 NULL,
1333 &HttpInstance->TimeoutEvent
1334 );
1335 if (EFI_ERROR (Status)) {
1336 goto Error2;
1337 }
1338 }
1339
1340 //
1341 // Get HTTP timeout value
1342 //
1343 TimeoutValue = PcdGet32 (PcdHttpIoTimeout);
1344
1345 //
1346 // Start the timer, and wait Timeout seconds to receive the body packet.
1347 //
1348 Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, TimeoutValue * TICKS_PER_MS);
1349 if (EFI_ERROR (Status)) {
1350 goto Error2;
1351 }
1352
1353 Status = HttpsReceive (HttpInstance, &Fragment, HttpInstance->TimeoutEvent);
1354
1355 gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0);
1356
1357 if (EFI_ERROR (Status)) {
1358 goto Error2;
1359 }
1360
1361 //
1362 // Process the received the body packet.
1363 //
1364 HttpMsg->BodyLength = MIN ((UINTN) Fragment.Len, HttpMsg->BodyLength);
1365
1366 CopyMem (HttpMsg->Body, Fragment.Bulk, HttpMsg->BodyLength);
1367
1368 //
1369 // Record the CallbackData data.
1370 //
1371 HttpInstance->CallbackData.Wrap = (VOID *) Wrap;
1372 HttpInstance->CallbackData.ParseData = HttpMsg->Body;
1373 HttpInstance->CallbackData.ParseDataLength = HttpMsg->BodyLength;
1374
1375 //
1376 // Parse Body with CallbackData data.
1377 //
1378 Status = HttpParseMessageBody (
1379 HttpInstance->MsgParser,
1380 HttpMsg->BodyLength,
1381 HttpMsg->Body
1382 );
1383 if (EFI_ERROR (Status)) {
1384 goto Error2;
1385 }
1386
1387 if (HttpIsMessageComplete (HttpInstance->MsgParser)) {
1388 //
1389 // Free the MsgParse since we already have a full HTTP message.
1390 //
1391 HttpFreeMsgParser (HttpInstance->MsgParser);
1392 HttpInstance->MsgParser = NULL;
1393 }
1394
1395 //
1396 // Check whether there is the next message header in the HttpMsg->Body.
1397 //
1398 if (HttpInstance->NextMsg != NULL) {
1399 HttpMsg->BodyLength = HttpInstance->NextMsg - (CHAR8 *) HttpMsg->Body;
1400 }
1401
1402 HttpInstance->CacheLen = Fragment.Len - HttpMsg->BodyLength;
1403 if (HttpInstance->CacheLen != 0) {
1404 if (HttpInstance->CacheBody != NULL) {
1405 FreePool (HttpInstance->CacheBody);
1406 }
1407
1408 HttpInstance->CacheBody = AllocateZeroPool (HttpInstance->CacheLen);
1409 if (HttpInstance->CacheBody == NULL) {
1410 Status = EFI_OUT_OF_RESOURCES;
1411 goto Error2;
1412 }
1413
1414 CopyMem (HttpInstance->CacheBody, Fragment.Bulk + HttpMsg->BodyLength, HttpInstance->CacheLen);
1415 HttpInstance->CacheOffset = 0;
1416 if (HttpInstance->NextMsg != NULL) {
1417 HttpInstance->NextMsg = HttpInstance->CacheBody;
1418 }
1419 }
1420
1421 if (Fragment.Bulk != NULL) {
1422 FreePool (Fragment.Bulk);
1423 Fragment.Bulk = NULL;
1424 }
1425
1426 goto Exit;
1427 }
1428
1429 return Status;
1430
1431 Exit:
1432 Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);
1433 if (Item != NULL) {
1434 NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);
1435 }
1436
1437 if (HttpInstance->StatusCode >= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE) {
1438 Token->Status = EFI_HTTP_ERROR;
1439 } else {
1440 Token->Status = Status;
1441 }
1442
1443 gBS->SignalEvent (Token->Event);
1444 HttpCloseTcpRxEvent (Wrap);
1445 FreePool (Wrap);
1446 return Status;
1447
1448 Error2:
1449 if (ValueInItem != NULL) {
1450 NetMapInsertHead (&HttpInstance->TxTokens, ValueInItem->HttpToken, ValueInItem);
1451 }
1452
1453 Error:
1454 Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);
1455 if (Item != NULL) {
1456 NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);
1457 }
1458
1459 if (!HttpInstance->UseHttps) {
1460 HttpTcpTokenCleanup (Wrap);
1461 } else {
1462 FreePool (Wrap);
1463 }
1464
1465 if (HttpHeaders != NULL) {
1466 FreePool (HttpHeaders);
1467 HttpHeaders = NULL;
1468 }
1469
1470 if (Fragment.Bulk != NULL) {
1471 FreePool (Fragment.Bulk);
1472 Fragment.Bulk = NULL;
1473 }
1474
1475 if (HttpMsg->Headers != NULL) {
1476 FreePool (HttpMsg->Headers);
1477 HttpMsg->Headers = NULL;
1478 }
1479
1480 if (HttpInstance->CacheBody != NULL) {
1481 FreePool (HttpInstance->CacheBody);
1482 HttpInstance->CacheBody = NULL;
1483 }
1484
1485 if (HttpInstance->StatusCode >= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE) {
1486 Token->Status = EFI_HTTP_ERROR;
1487 } else {
1488 Token->Status = Status;
1489 }
1490
1491 gBS->SignalEvent (Token->Event);
1492
1493 return Status;
1494
1495 }
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 FreePool (Wrap);
1623 }
1624
1625 return Status;
1626 }
1627
1628 /**
1629 The Poll() function can be used by network drivers and applications to increase the
1630 rate that data packets are moved between the communication devices and the transmit
1631 and receive queues.
1632
1633 In some systems, the periodic timer event in the managed network driver may not poll
1634 the underlying communications device fast enough to transmit and/or receive all data
1635 packets without missing incoming packets or dropping outgoing packets. Drivers and
1636 applications that are experiencing packet loss should try calling the Poll() function
1637 more often.
1638
1639 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1640
1641 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1642 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1643 @retval EFI_INVALID_PARAMETER This is NULL.
1644 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1645 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1646
1647 **/
1648 EFI_STATUS
1649 EFIAPI
1650 EfiHttpPoll (
1651 IN EFI_HTTP_PROTOCOL *This
1652 )
1653 {
1654 EFI_STATUS Status;
1655 HTTP_PROTOCOL *HttpInstance;
1656
1657 if (This == NULL) {
1658 return EFI_INVALID_PARAMETER;
1659 }
1660
1661 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
1662
1663 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {
1664 return EFI_NOT_STARTED;
1665 }
1666
1667 if (HttpInstance->LocalAddressIsIPv6) {
1668 if (HttpInstance->Tcp6 == NULL) {
1669 return EFI_NOT_STARTED;
1670 }
1671 Status = HttpInstance->Tcp6->Poll (HttpInstance->Tcp6);
1672 } else {
1673 if (HttpInstance->Tcp4 == NULL) {
1674 return EFI_NOT_STARTED;
1675 }
1676 Status = HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);
1677 }
1678
1679 DispatchDpc ();
1680
1681 return Status;
1682 }