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