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