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