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