]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/HttpDxe/HttpImpl.c
NetworkPkg:HttpDxe: Code changes to support HTTP PUT/POST operations
[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 is NULL.
118 HttpConfigData->LocalAddressIsIPv6 is FALSE and
119 HttpConfigData->IPv4Node is NULL.
120 HttpConfigData->LocalAddressIsIPv6 is TRUE and
121 HttpConfigData->IPv6Node is NULL.
122 @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling
123 Configure() with NULL to reset it.
124 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
125 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when
126 executing Configure().
127 @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported
128 in the implementation.
129 **/
130 EFI_STATUS
131 EFIAPI
132 EfiHttpConfigure (
133 IN EFI_HTTP_PROTOCOL *This,
134 IN EFI_HTTP_CONFIG_DATA *HttpConfigData
135 )
136 {
137 HTTP_PROTOCOL *HttpInstance;
138 EFI_STATUS Status;
139
140 //
141 // Check input parameters.
142 //
143 if (This == NULL ||
144 HttpConfigData == NULL ||
145 ((HttpConfigData->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv6Node == NULL) ||
146 (!HttpConfigData->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv4Node == NULL))) {
147 return EFI_INVALID_PARAMETER;
148 }
149
150 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
151 ASSERT (HttpInstance != NULL && HttpInstance->Service != NULL);
152
153 if (HttpConfigData != NULL) {
154
155 //
156 // Now configure this HTTP instance.
157 //
158 if (HttpInstance->State != HTTP_STATE_UNCONFIGED) {
159 return EFI_ALREADY_STARTED;
160 }
161
162 HttpInstance->HttpVersion = HttpConfigData->HttpVersion;
163 HttpInstance->TimeOutMillisec = HttpConfigData->TimeOutMillisec;
164 HttpInstance->LocalAddressIsIPv6 = HttpConfigData->LocalAddressIsIPv6;
165
166 if (HttpConfigData->LocalAddressIsIPv6) {
167 CopyMem (
168 &HttpInstance->Ipv6Node,
169 HttpConfigData->AccessPoint.IPv6Node,
170 sizeof (HttpInstance->Ipv6Node)
171 );
172 } else {
173 CopyMem (
174 &HttpInstance->IPv4Node,
175 HttpConfigData->AccessPoint.IPv4Node,
176 sizeof (HttpInstance->IPv4Node)
177 );
178 }
179
180 //
181 // Creat Tcp child
182 //
183 Status = HttpInitProtocol (HttpInstance, HttpInstance->LocalAddressIsIPv6);
184 if (EFI_ERROR (Status)) {
185 return Status;
186 }
187
188 HttpInstance->State = HTTP_STATE_HTTP_CONFIGED;
189 return EFI_SUCCESS;
190
191 } else {
192 //
193 // Reset all the resources related to HttpInsance.
194 //
195 HttpCleanProtocol (HttpInstance);
196 HttpInstance->State = HTTP_STATE_UNCONFIGED;
197 return EFI_SUCCESS;
198 }
199 }
200
201
202 /**
203 The Request() function queues an HTTP request to this HTTP instance.
204
205 Similar to Transmit() function in the EFI TCP driver. When the HTTP request is sent
206 successfully, or if there is an error, Status in token will be updated and Event will
207 be signaled.
208
209 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
210 @param[in] Token Pointer to storage containing HTTP request token.
211
212 @retval EFI_SUCCESS Outgoing data was processed.
213 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
214 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
215 @retval EFI_TIMEOUT Data was dropped out of the transmit or receive queue.
216 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
217 @retval EFI_UNSUPPORTED The HTTP method is not supported in current
218 implementation.
219 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
220 This is NULL.
221 Token is NULL.
222 Token->Message is NULL.
223 Token->Message->Body is not NULL,
224 Token->Message->BodyLength is non-zero, and
225 Token->Message->Data is NULL, but a previous call to
226 Request()has not been completed successfully.
227 **/
228 EFI_STATUS
229 EFIAPI
230 EfiHttpRequest (
231 IN EFI_HTTP_PROTOCOL *This,
232 IN EFI_HTTP_TOKEN *Token
233 )
234 {
235 EFI_HTTP_MESSAGE *HttpMsg;
236 EFI_HTTP_REQUEST_DATA *Request;
237 VOID *UrlParser;
238 EFI_STATUS Status;
239 CHAR8 *HostName;
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 HostName = NULL;
257 RequestMsg = NULL;
258 HostNameStr = NULL;
259 Wrap = NULL;
260 FileUrl = NULL;
261
262 if ((This == NULL) || (Token == NULL)) {
263 return EFI_INVALID_PARAMETER;
264 }
265
266 HttpMsg = Token->Message;
267 if (HttpMsg == NULL) {
268 return EFI_INVALID_PARAMETER;
269 }
270
271 Request = HttpMsg->Data.Request;
272
273 //
274 // Only support GET, HEAD, PUT and POST method in current implementation.
275 //
276 if ((Request != NULL) && (Request->Method != HttpMethodGet) &&
277 (Request->Method != HttpMethodHead) && (Request->Method != HttpMethodPut) && (Request->Method != HttpMethodPost)) {
278 return EFI_UNSUPPORTED;
279 }
280
281 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
282 ASSERT (HttpInstance != NULL);
283
284 //
285 // Capture the method into HttpInstance.
286 //
287 if (Request != NULL) {
288 HttpInstance->Method = Request->Method;
289 }
290
291 if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) {
292 return EFI_NOT_STARTED;
293 }
294
295 if (Request == NULL) {
296 //
297 // Request would be NULL only for PUT/POST operation (in the current implementation)
298 //
299 if ((HttpInstance->Method != HttpMethodPut) && (HttpInstance->Method != HttpMethodPost)) {
300 return EFI_INVALID_PARAMETER;
301 }
302
303 //
304 // For PUT/POST, we need to have the TCP already configured. Bail out if it is not!
305 //
306 if (HttpInstance->State < HTTP_STATE_TCP_CONFIGED) {
307 return EFI_INVALID_PARAMETER;
308 }
309
310 //
311 // We need to have the Message Body for sending the HTTP message across in these cases.
312 //
313 if (HttpMsg->Body == NULL || HttpMsg->BodyLength == 0) {
314 return EFI_INVALID_PARAMETER;
315 }
316
317 //
318 // Use existing TCP instance to transmit the packet.
319 //
320 Configure = FALSE;
321 ReConfigure = FALSE;
322 } else {
323 //
324 // Check whether the token already existed.
325 //
326 if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTokenExist, Token))) {
327 return EFI_ACCESS_DENIED;
328 }
329
330 //
331 // Parse the URI of the remote host.
332 //
333 Url = HttpInstance->Url;
334 UrlLen = StrLen (Request->Url) + 1;
335 if (UrlLen > HTTP_URL_BUFFER_LEN) {
336 Url = AllocateZeroPool (UrlLen);
337 if (Url == NULL) {
338 return EFI_OUT_OF_RESOURCES;
339 }
340 FreePool (HttpInstance->Url);
341 HttpInstance->Url = Url;
342 }
343
344
345 UnicodeStrToAsciiStr (Request->Url, Url);
346 UrlParser = NULL;
347 Status = HttpParseUrl (Url, (UINT32) AsciiStrLen (Url), FALSE, &UrlParser);
348 if (EFI_ERROR (Status)) {
349 goto Error1;
350 }
351
352 HostName = NULL;
353 Status = HttpUrlGetHostName (Url, UrlParser, &HostName);
354 if (EFI_ERROR (Status)) {
355 goto Error1;
356 }
357
358 Status = HttpUrlGetPort (Url, UrlParser, &RemotePort);
359 if (EFI_ERROR (Status)) {
360 RemotePort = HTTP_DEFAULT_PORT;
361 }
362 //
363 // If Configure is TRUE, it indicates the first time to call Request();
364 // If ReConfigure is TRUE, it indicates the request URL is not same
365 // with the previous call to Request();
366 //
367 Configure = TRUE;
368 ReConfigure = TRUE;
369
370 if (HttpInstance->RemoteHost == NULL) {
371 //
372 // Request() is called the first time.
373 //
374 ReConfigure = FALSE;
375 } else {
376 if ((HttpInstance->RemotePort == RemotePort) &&
377 (AsciiStrCmp (HttpInstance->RemoteHost, HostName) == 0)) {
378 //
379 // Host Name and port number of the request URL are the same with previous call to Request().
380 // Check whether previous TCP packet sent out.
381 //
382
383 if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTcpNotReady, NULL))) {
384 //
385 // Wrap the HTTP token in HTTP_TOKEN_WRAP
386 //
387 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));
388 if (Wrap == NULL) {
389 Status = EFI_OUT_OF_RESOURCES;
390 goto Error1;
391 }
392
393 Wrap->HttpToken = Token;
394 Wrap->HttpInstance = HttpInstance;
395
396 Status = HttpCreateTcpTxEvent (Wrap);
397 if (EFI_ERROR (Status)) {
398 goto Error1;
399 }
400
401 Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap);
402 if (EFI_ERROR (Status)) {
403 goto Error1;
404 }
405
406 Wrap->TcpWrap.Method = Request->Method;
407
408 FreePool (HostName);
409
410 //
411 // Queue the HTTP token and return.
412 //
413 return EFI_SUCCESS;
414 } else {
415 //
416 // Use existing TCP instance to transmit the packet.
417 //
418 Configure = FALSE;
419 ReConfigure = FALSE;
420 }
421 } else {
422 //
423 // Need close existing TCP instance and create a new TCP instance for data transmit.
424 //
425 if (HttpInstance->RemoteHost != NULL) {
426 FreePool (HttpInstance->RemoteHost);
427 HttpInstance->RemoteHost = NULL;
428 HttpInstance->RemotePort = 0;
429 }
430 }
431 }
432 }
433
434 if (Configure) {
435 //
436 // Parse Url for IPv4 or IPv6 address, if failed, perform DNS resolution.
437 //
438 if (!HttpInstance->LocalAddressIsIPv6) {
439 Status = NetLibAsciiStrToIp4 (HostName, &HttpInstance->RemoteAddr);
440 } else {
441 Status = HttpUrlGetIp6 (Url, UrlParser, &HttpInstance->RemoteIpv6Addr);
442 }
443
444 if (EFI_ERROR (Status)) {
445 HostNameStr = AllocateZeroPool ((AsciiStrLen (HostName) + 1) * sizeof (CHAR16));
446 if (HostNameStr == NULL) {
447 Status = EFI_OUT_OF_RESOURCES;
448 goto Error1;
449 }
450
451 AsciiStrToUnicodeStr (HostName, HostNameStr);
452 if (!HttpInstance->LocalAddressIsIPv6) {
453 Status = HttpDns4 (HttpInstance, HostNameStr, &HttpInstance->RemoteAddr);
454 } else {
455 Status = HttpDns6 (HttpInstance, HostNameStr, &HttpInstance->RemoteIpv6Addr);
456 }
457
458 FreePool (HostNameStr);
459 if (EFI_ERROR (Status)) {
460 goto Error1;
461 }
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
609 if (HostName != NULL) {
610 FreePool (HostName);
611 }
612 if (Wrap != NULL) {
613 FreePool (Wrap);
614 }
615 if (UrlParser!= NULL) {
616 HttpUrlFreeParser (UrlParser);
617 }
618
619 return Status;
620
621 }
622
623 /**
624 Cancel a user's Token.
625
626 @param[in] Map The HTTP instance's token queue.
627 @param[in] Item Object container for one HTTP token and token's wrap.
628 @param[in] Context The user's token to cancel.
629
630 @retval EFI_SUCCESS Continue to check the next Item.
631 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
632
633 **/
634 EFI_STATUS
635 EFIAPI
636 HttpCancelTokens (
637 IN NET_MAP *Map,
638 IN NET_MAP_ITEM *Item,
639 IN VOID *Context
640 )
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 //
662 // Free resources.
663 //
664 NetMapRemoveItem (Map, Item, NULL);
665
666 if (!HttpInstance->LocalAddressIsIPv6) {
667 if (Wrap->TcpWrap.Tx4Token.CompletionToken.Event != NULL) {
668 gBS->CloseEvent (Wrap->TcpWrap.Tx4Token.CompletionToken.Event);
669 }
670
671 if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) {
672 gBS->CloseEvent (Wrap->TcpWrap.Rx4Token.CompletionToken.Event);
673 }
674
675 if (Wrap->TcpWrap.Rx4Token.Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {
676 FreePool (Wrap->TcpWrap.Rx4Token.Packet.RxData->FragmentTable[0].FragmentBuffer);
677 }
678
679 } else {
680 if (Wrap->TcpWrap.Tx6Token.CompletionToken.Event != NULL) {
681 gBS->CloseEvent (Wrap->TcpWrap.Tx6Token.CompletionToken.Event);
682 }
683
684 if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {
685 gBS->CloseEvent (Wrap->TcpWrap.Rx6Token.CompletionToken.Event);
686 }
687
688 if (Wrap->TcpWrap.Rx6Token.Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {
689 FreePool (Wrap->TcpWrap.Rx6Token.Packet.RxData->FragmentTable[0].FragmentBuffer);
690 }
691 }
692
693
694 FreePool (Wrap);
695
696 //
697 // If only one item is to be cancel, return EFI_ABORTED to stop
698 // iterating the map any more.
699 //
700 if (Token != NULL) {
701 return EFI_ABORTED;
702 }
703
704 return EFI_SUCCESS;
705 }
706
707 /**
708 Cancel the user's receive/transmit request. It is the worker function of
709 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the
710 token.
711
712 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
713 @param[in] Token The token to cancel. If NULL, all token will be
714 cancelled.
715
716 @retval EFI_SUCCESS The token is cancelled.
717 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.
718 @retval Others Other error as indicated.
719
720 **/
721 EFI_STATUS
722 HttpCancel (
723 IN HTTP_PROTOCOL *HttpInstance,
724 IN EFI_HTTP_TOKEN *Token
725 )
726 {
727 EFI_STATUS Status;
728
729 //
730 // First check the tokens queued by EfiHttpRequest().
731 //
732 Status = NetMapIterate (&HttpInstance->TxTokens, HttpCancelTokens, Token);
733 if (EFI_ERROR (Status)) {
734 if (Token != NULL) {
735 if (Status == EFI_ABORTED) {
736 return EFI_SUCCESS;
737 }
738 } else {
739 return Status;
740 }
741 }
742
743 //
744 // Then check the tokens queued by EfiHttpResponse().
745 //
746 Status = NetMapIterate (&HttpInstance->RxTokens, HttpCancelTokens, Token);
747 if (EFI_ERROR (Status)) {
748 if (Token != NULL) {
749 if (Status == EFI_ABORTED) {
750 return EFI_SUCCESS;
751 } else {
752 return EFI_NOT_FOUND;
753 }
754 } else {
755 return Status;
756 }
757 }
758
759 return EFI_SUCCESS;
760 }
761
762
763 /**
764 Abort an asynchronous HTTP request or response token.
765
766 The Cancel() function aborts a pending HTTP request or response transaction. If
767 Token is not NULL and the token is in transmit or receive queues when it is being
768 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
769 be signaled. If the token is not in one of the queues, which usually means that the
770 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
771 all asynchronous tokens issued by Request() or Response() will be aborted.
772
773 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
774 @param[in] Token Point to storage containing HTTP request or response
775 token.
776
777 @retval EFI_SUCCESS Request and Response queues are successfully flushed.
778 @retval EFI_INVALID_PARAMETER This is NULL.
779 @retval EFI_NOT_STARTED This instance hasn't been configured.
780 @retval EFI_NOT_FOUND The asynchronous request or response token is not
781 found.
782 @retval EFI_UNSUPPORTED The implementation does not support this function.
783
784 **/
785 EFI_STATUS
786 EFIAPI
787 EfiHttpCancel (
788 IN EFI_HTTP_PROTOCOL *This,
789 IN EFI_HTTP_TOKEN *Token
790 )
791 {
792 HTTP_PROTOCOL *HttpInstance;
793
794 if (This == NULL) {
795 return EFI_INVALID_PARAMETER;
796 }
797
798 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
799 ASSERT (HttpInstance != NULL);
800
801 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {
802 return EFI_NOT_STARTED;
803 }
804
805 return HttpCancel (HttpInstance, Token);
806
807 }
808
809 /**
810 A callback function to intercept events during message parser.
811
812 This function will be invoked during HttpParseMessageBody() with various events type. An error
813 return status of the callback function will cause the HttpParseMessageBody() aborted.
814
815 @param[in] EventType Event type of this callback call.
816 @param[in] Data A pointer to data buffer.
817 @param[in] Length Length in bytes of the Data.
818 @param[in] Context Callback context set by HttpInitMsgParser().
819
820 @retval EFI_SUCCESS Continue to parser the message body.
821
822 **/
823 EFI_STATUS
824 EFIAPI
825 HttpBodyParserCallback (
826 IN HTTP_BODY_PARSE_EVENT EventType,
827 IN CHAR8 *Data,
828 IN UINTN Length,
829 IN VOID *Context
830 )
831 {
832 HTTP_TOKEN_WRAP *Wrap;
833 UINTN BodyLength;
834 CHAR8 *Body;
835
836 if (EventType != BodyParseEventOnComplete) {
837 return EFI_SUCCESS;
838 }
839
840 if (Data == NULL || Length != 0 || Context == NULL) {
841 return EFI_SUCCESS;
842 }
843
844 Wrap = (HTTP_TOKEN_WRAP *) Context;
845 Body = Wrap->HttpToken->Message->Body;
846 BodyLength = Wrap->HttpToken->Message->BodyLength;
847 if (Data < Body + BodyLength) {
848 Wrap->HttpInstance->NextMsg = Data;
849 } else {
850 Wrap->HttpInstance->NextMsg = NULL;
851 }
852
853
854 //
855 // Free Tx4Token or Tx6Token since already received corrsponding HTTP response.
856 //
857 FreePool (Wrap);
858
859 return EFI_SUCCESS;
860 }
861
862 /**
863 The work function of EfiHttpResponse().
864
865 @param[in] Wrap Pointer to HTTP token's wrap data.
866
867 @retval EFI_SUCCESS Allocation succeeded.
868 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
869 @retval EFI_NOT_READY Can't find a corresponding Tx4Token/Tx6Token or
870 the EFI_HTTP_UTILITIES_PROTOCOL is not available.
871
872 **/
873 EFI_STATUS
874 HttpResponseWorker (
875 IN HTTP_TOKEN_WRAP *Wrap
876 )
877 {
878 EFI_STATUS Status;
879 EFI_HTTP_MESSAGE *HttpMsg;
880 CHAR8 *EndofHeader;
881 CHAR8 *HttpHeaders;
882 UINTN SizeofHeaders;
883 UINTN BufferSize;
884 UINTN StatusCode;
885 CHAR8 *Tmp;
886 CHAR8 *HeaderTmp;
887 CHAR8 *StatusCodeStr;
888 UINTN BodyLen;
889 HTTP_PROTOCOL *HttpInstance;
890 EFI_HTTP_TOKEN *Token;
891 NET_MAP_ITEM *Item;
892 HTTP_TOKEN_WRAP *ValueInItem;
893 UINTN HdrLen;
894
895 if (Wrap == NULL || Wrap->HttpInstance == NULL) {
896 return EFI_INVALID_PARAMETER;
897 }
898
899 HttpInstance = Wrap->HttpInstance;
900 Token = Wrap->HttpToken;
901 HttpMsg = Token->Message;
902
903 HttpInstance->EndofHeader = NULL;
904 HttpInstance->HttpHeaders = NULL;
905 HttpMsg->Headers = NULL;
906 HttpHeaders = NULL;
907 SizeofHeaders = 0;
908 BufferSize = 0;
909 EndofHeader = NULL;
910
911 if (HttpMsg->Data.Response != NULL) {
912 //
913 // Need receive the HTTP headers, prepare buffer.
914 //
915 Status = HttpCreateTcpRxEventForHeader (HttpInstance);
916 if (EFI_ERROR (Status)) {
917 goto Error;
918 }
919
920 //
921 // Check whether we have cached header from previous call.
922 //
923 if ((HttpInstance->CacheBody != NULL) && (HttpInstance->NextMsg != NULL)) {
924 //
925 // The data is stored at [NextMsg, CacheBody + CacheLen].
926 //
927 HdrLen = HttpInstance->CacheBody + HttpInstance->CacheLen - HttpInstance->NextMsg;
928 HttpHeaders = AllocateZeroPool (HdrLen);
929 if (HttpHeaders == NULL) {
930 Status = EFI_OUT_OF_RESOURCES;
931 goto Error;
932 }
933
934 CopyMem (HttpHeaders, HttpInstance->NextMsg, HdrLen);
935 FreePool (HttpInstance->CacheBody);
936 HttpInstance->CacheBody = NULL;
937 HttpInstance->NextMsg = NULL;
938 HttpInstance->CacheOffset = 0;
939 SizeofHeaders = HdrLen;
940 BufferSize = HttpInstance->CacheLen;
941
942 //
943 // Check whether we cached the whole HTTP headers.
944 //
945 EndofHeader = AsciiStrStr (HttpHeaders, HTTP_END_OF_HDR_STR);
946 }
947
948 HttpInstance->EndofHeader = &EndofHeader;
949 HttpInstance->HttpHeaders = &HttpHeaders;
950
951
952 if (HttpInstance->TimeoutEvent == NULL) {
953 //
954 // Create TimeoutEvent for response
955 //
956 Status = gBS->CreateEvent (
957 EVT_TIMER,
958 TPL_CALLBACK,
959 NULL,
960 NULL,
961 &HttpInstance->TimeoutEvent
962 );
963 if (EFI_ERROR (Status)) {
964 goto Error;
965 }
966 }
967
968 //
969 // Start the timer, and wait Timeout seconds to receive the header packet.
970 //
971 Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_RESPONSE_TIMEOUT * TICKS_PER_SECOND);
972 if (EFI_ERROR (Status)) {
973 goto Error;
974 }
975
976 Status = HttpTcpReceiveHeader (HttpInstance, &SizeofHeaders, &BufferSize, HttpInstance->TimeoutEvent);
977
978 gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0);
979
980 if (EFI_ERROR (Status)) {
981 goto Error;
982 }
983
984 ASSERT (HttpHeaders != NULL);
985
986 //
987 // Cache the part of body.
988 //
989 BodyLen = BufferSize - (EndofHeader - HttpHeaders);
990 if (BodyLen > 0) {
991 if (HttpInstance->CacheBody != NULL) {
992 FreePool (HttpInstance->CacheBody);
993 }
994
995 HttpInstance->CacheBody = AllocateZeroPool (BodyLen);
996 if (HttpInstance->CacheBody == NULL) {
997 Status = EFI_OUT_OF_RESOURCES;
998 goto Error;
999 }
1000
1001 CopyMem (HttpInstance->CacheBody, EndofHeader, BodyLen);
1002 HttpInstance->CacheLen = BodyLen;
1003 }
1004
1005 //
1006 // Search for Status Code.
1007 //
1008 StatusCodeStr = HttpHeaders + AsciiStrLen (HTTP_VERSION_STR) + 1;
1009 if (StatusCodeStr == NULL) {
1010 goto Error;
1011 }
1012
1013 StatusCode = AsciiStrDecimalToUintn (StatusCodeStr);
1014
1015 //
1016 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
1017 //
1018 Tmp = AsciiStrStr (HttpHeaders, HTTP_CRLF_STR);
1019 if (Tmp == NULL) {
1020 goto Error;
1021 }
1022
1023 //
1024 // We could have response with just a HTTP message and no headers. For Example,
1025 // "100 Continue". In such cases, we would not want to unnecessarily call a Parse
1026 // method. A "\r\n" following Tmp string again would indicate an end. Compare and
1027 // set SizeofHeaders to 0.
1028 //
1029 Tmp = Tmp + AsciiStrLen (HTTP_CRLF_STR);
1030 if (CompareMem (Tmp, HTTP_CRLF_STR, AsciiStrLen (HTTP_CRLF_STR)) == 0) {
1031 SizeofHeaders = 0;
1032 } else {
1033 SizeofHeaders = SizeofHeaders - (Tmp - HttpHeaders);
1034 }
1035
1036 HttpMsg->Data.Response->StatusCode = HttpMappingToStatusCode (StatusCode);
1037 HttpInstance->StatusCode = StatusCode;
1038
1039 Status = EFI_NOT_READY;
1040 ValueInItem = NULL;
1041
1042 //
1043 // In cases of PUT/POST, after an initial request-response pair, we would do a
1044 // continuous request without a response call. So, we would not do an insert of
1045 // TxToken. After we have sent the complete file, we will call a response to get
1046 // a final response from server. In such a case, we would not have any TxTokens.
1047 // Hence, check that case before doing a NetMapRemoveHead.
1048 //
1049 if (!NetMapIsEmpty (&HttpInstance->TxTokens)) {
1050 NetMapRemoveHead (&HttpInstance->TxTokens, (VOID**) &ValueInItem);
1051 if (ValueInItem == NULL) {
1052 goto Error;
1053 }
1054
1055 //
1056 // The first Tx Token not transmitted yet, insert back and return error.
1057 //
1058 if (!ValueInItem->TcpWrap.IsTxDone) {
1059 goto Error2;
1060 }
1061 }
1062
1063 if (SizeofHeaders != 0) {
1064 HeaderTmp = AllocateZeroPool (SizeofHeaders);
1065 if (HeaderTmp == NULL) {
1066 goto Error;
1067 }
1068
1069 CopyMem (HeaderTmp, Tmp, SizeofHeaders);
1070 FreePool (HttpHeaders);
1071 HttpHeaders = HeaderTmp;
1072
1073 //
1074 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.
1075 //
1076 if (mHttpUtilities == NULL) {
1077 Status = EFI_NOT_READY;
1078 goto Error;
1079 }
1080
1081 //
1082 // Parse the HTTP header into array of key/value pairs.
1083 //
1084 Status = mHttpUtilities->Parse (
1085 mHttpUtilities,
1086 HttpHeaders,
1087 SizeofHeaders,
1088 &HttpMsg->Headers,
1089 &HttpMsg->HeaderCount
1090 );
1091 if (EFI_ERROR (Status)) {
1092 goto Error;
1093 }
1094
1095 FreePool (HttpHeaders);
1096 HttpHeaders = NULL;
1097
1098
1099 //
1100 // Init message-body parser by header information.
1101 //
1102 Status = HttpInitMsgParser (
1103 HttpInstance->Method,
1104 HttpMsg->Data.Response->StatusCode,
1105 HttpMsg->HeaderCount,
1106 HttpMsg->Headers,
1107 HttpBodyParserCallback,
1108 (VOID *) ValueInItem,
1109 &HttpInstance->MsgParser
1110 );
1111 if (EFI_ERROR (Status)) {
1112 goto Error2;
1113 }
1114
1115 //
1116 // Check whether we received a complete HTTP message.
1117 //
1118 if (HttpInstance->CacheBody != NULL) {
1119 Status = HttpParseMessageBody (HttpInstance->MsgParser, HttpInstance->CacheLen, HttpInstance->CacheBody);
1120 if (EFI_ERROR (Status)) {
1121 goto Error2;
1122 }
1123
1124 if (HttpIsMessageComplete (HttpInstance->MsgParser)) {
1125 //
1126 // Free the MsgParse since we already have a full HTTP message.
1127 //
1128 HttpFreeMsgParser (HttpInstance->MsgParser);
1129 HttpInstance->MsgParser = NULL;
1130 }
1131 }
1132 }
1133
1134 if ((HttpMsg->Body == NULL) || (HttpMsg->BodyLength == 0)) {
1135 Status = EFI_SUCCESS;
1136 goto Exit;
1137 }
1138 }
1139
1140 //
1141 // Receive the response body.
1142 //
1143 BodyLen = 0;
1144
1145 //
1146 // First check whether we cached some data.
1147 //
1148 if (HttpInstance->CacheBody != NULL) {
1149 //
1150 // Calculate the length of the cached data.
1151 //
1152 if (HttpInstance->NextMsg != NULL) {
1153 //
1154 // We have a cached HTTP message which includes a part of HTTP header of next message.
1155 //
1156 BodyLen = HttpInstance->NextMsg - (HttpInstance->CacheBody + HttpInstance->CacheOffset);
1157 } else {
1158 BodyLen = HttpInstance->CacheLen - HttpInstance->CacheOffset;
1159 }
1160
1161 if (BodyLen > 0) {
1162 //
1163 // We have some cached data. Just copy the data and return.
1164 //
1165 if (HttpMsg->BodyLength < BodyLen) {
1166 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, HttpMsg->BodyLength);
1167 HttpInstance->CacheOffset = HttpInstance->CacheOffset + HttpMsg->BodyLength;
1168 } else {
1169 //
1170 // Copy all cached data out.
1171 //
1172 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, BodyLen);
1173 HttpInstance->CacheOffset = BodyLen + HttpInstance->CacheOffset;
1174 HttpMsg->BodyLength = BodyLen;
1175
1176 if (HttpInstance->NextMsg == NULL) {
1177 //
1178 // There is no HTTP header of next message. Just free the cache buffer.
1179 //
1180 FreePool (HttpInstance->CacheBody);
1181 HttpInstance->CacheBody = NULL;
1182 HttpInstance->NextMsg = NULL;
1183 HttpInstance->CacheOffset = 0;
1184 }
1185 }
1186 //
1187 // Return since we aready received required data.
1188 //
1189 Status = EFI_SUCCESS;
1190 goto Exit;
1191 }
1192
1193 if (BodyLen == 0 && HttpInstance->MsgParser == NULL) {
1194 //
1195 // We received a complete HTTP message, and we don't have more data to return to caller.
1196 //
1197 HttpMsg->BodyLength = 0;
1198 Status = EFI_SUCCESS;
1199 goto Exit;
1200 }
1201 }
1202
1203 ASSERT (HttpInstance->MsgParser != NULL);
1204
1205 if (HttpInstance->TimeoutEvent == NULL) {
1206 //
1207 // Create TimeoutEvent for response
1208 //
1209 Status = gBS->CreateEvent (
1210 EVT_TIMER,
1211 TPL_CALLBACK,
1212 NULL,
1213 NULL,
1214 &HttpInstance->TimeoutEvent
1215 );
1216 if (EFI_ERROR (Status)) {
1217 goto Error;
1218 }
1219 }
1220
1221 //
1222 // Start the timer, and wait Timeout seconds to receive the body packet.
1223 //
1224 Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_RESPONSE_TIMEOUT * TICKS_PER_SECOND);
1225 if (EFI_ERROR (Status)) {
1226 goto Error;
1227 }
1228
1229 //
1230 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1231 //
1232 Status = HttpTcpReceiveBody (Wrap, HttpMsg, HttpInstance->TimeoutEvent);
1233
1234 gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0);
1235
1236 if (EFI_ERROR (Status)) {
1237 goto Error;
1238 }
1239
1240 return Status;
1241
1242 Exit:
1243 Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);
1244 if (Item != NULL) {
1245 NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);
1246 }
1247
1248 if (HttpInstance->StatusCode >= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE) {
1249 Token->Status = EFI_HTTP_ERROR;
1250 } else {
1251 Token->Status = Status;
1252 }
1253
1254 gBS->SignalEvent (Token->Event);
1255 HttpCloseTcpRxEvent (Wrap);
1256 FreePool (Wrap);
1257 return Status;
1258
1259 Error2:
1260 NetMapInsertHead (&HttpInstance->TxTokens, ValueInItem->HttpToken, ValueInItem);
1261
1262 Error:
1263 HttpTcpTokenCleanup (Wrap);
1264
1265 if (HttpHeaders != NULL) {
1266 FreePool (HttpHeaders);
1267 }
1268
1269 if (HttpMsg->Headers != NULL) {
1270 FreePool (HttpMsg->Headers);
1271 }
1272
1273 if (HttpInstance->CacheBody != NULL) {
1274 FreePool (HttpInstance->CacheBody);
1275 HttpInstance->CacheBody = NULL;
1276 }
1277
1278 if (HttpInstance->StatusCode >= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE) {
1279 Token->Status = EFI_HTTP_ERROR;
1280 } else {
1281 Token->Status = Status;
1282 }
1283
1284 gBS->SignalEvent (Token->Event);
1285
1286 return Status;
1287
1288 }
1289
1290
1291 /**
1292 The Response() function queues an HTTP response to this HTTP instance, similar to
1293 Receive() function in the EFI TCP driver. When the HTTP response is received successfully,
1294 or if there is an error, Status in token will be updated and Event will be signaled.
1295
1296 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1297 is received in the underlying TCP instance, the data will be parsed and Token will
1298 be populated with the response data. If the data received from the remote host
1299 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1300 (asynchronously) for more data to be sent from the remote host before signaling
1301 Event in Token.
1302
1303 It is the responsibility of the caller to allocate a buffer for Body and specify the
1304 size in BodyLength. If the remote host provides a response that contains a content
1305 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1306 BodyLength will be updated with the amount of bytes received and copied to Body. This
1307 allows the client to download a large file in chunks instead of into one contiguous
1308 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1309 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1310 token to underlying TCP instance. If data arrives in the receive buffer, up to
1311 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1312 BodyLength with the amount of bytes received and copied to Body.
1313
1314 If the HTTP driver does not have an open underlying TCP connection with the host
1315 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1316 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1317 an open TCP connection between client and host.
1318
1319 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1320 @param[in] Token Pointer to storage containing HTTP response token.
1321
1322 @retval EFI_SUCCESS Allocation succeeded.
1323 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1324 initialized.
1325 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1326 This is NULL.
1327 Token is NULL.
1328 Token->Message->Headers is NULL.
1329 Token->Message is NULL.
1330 Token->Message->Body is not NULL,
1331 Token->Message->BodyLength is non-zero, and
1332 Token->Message->Data is NULL, but a previous call to
1333 Response() has not been completed successfully.
1334 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1335 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1336 specified by response URL.
1337 **/
1338 EFI_STATUS
1339 EFIAPI
1340 EfiHttpResponse (
1341 IN EFI_HTTP_PROTOCOL *This,
1342 IN EFI_HTTP_TOKEN *Token
1343 )
1344 {
1345 EFI_STATUS Status;
1346 EFI_HTTP_MESSAGE *HttpMsg;
1347 HTTP_PROTOCOL *HttpInstance;
1348 HTTP_TOKEN_WRAP *Wrap;
1349
1350 if ((This == NULL) || (Token == NULL)) {
1351 return EFI_INVALID_PARAMETER;
1352 }
1353
1354 HttpMsg = Token->Message;
1355 if (HttpMsg == NULL) {
1356 return EFI_INVALID_PARAMETER;
1357 }
1358
1359 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
1360 ASSERT (HttpInstance != NULL);
1361
1362 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {
1363 return EFI_NOT_STARTED;
1364 }
1365
1366 //
1367 // Check whether the token already existed.
1368 //
1369 if (EFI_ERROR (NetMapIterate (&HttpInstance->RxTokens, HttpTokenExist, Token))) {
1370 return EFI_ACCESS_DENIED;
1371 }
1372
1373 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));
1374 if (Wrap == NULL) {
1375 return EFI_OUT_OF_RESOURCES;
1376 }
1377
1378 Wrap->HttpInstance = HttpInstance;
1379 Wrap->HttpToken = Token;
1380
1381 Status = HttpCreateTcpRxEvent (Wrap);
1382 if (EFI_ERROR (Status)) {
1383 goto Error;
1384 }
1385
1386 Status = NetMapInsertTail (&HttpInstance->RxTokens, Token, Wrap);
1387 if (EFI_ERROR (Status)) {
1388 goto Error;
1389 }
1390
1391 //
1392 // If already have pending RxTokens, return directly.
1393 //
1394 if (NetMapGetCount (&HttpInstance->RxTokens) > 1) {
1395 return EFI_SUCCESS;
1396 }
1397
1398 return HttpResponseWorker (Wrap);
1399
1400 Error:
1401 if (Wrap != NULL) {
1402 if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) {
1403 gBS->CloseEvent (Wrap->TcpWrap.Rx4Token.CompletionToken.Event);
1404 }
1405
1406 if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {
1407 gBS->CloseEvent (Wrap->TcpWrap.Rx6Token.CompletionToken.Event);
1408 }
1409 FreePool (Wrap);
1410 }
1411
1412 return Status;
1413 }
1414
1415 /**
1416 The Poll() function can be used by network drivers and applications to increase the
1417 rate that data packets are moved between the communication devices and the transmit
1418 and receive queues.
1419
1420 In some systems, the periodic timer event in the managed network driver may not poll
1421 the underlying communications device fast enough to transmit and/or receive all data
1422 packets without missing incoming packets or dropping outgoing packets. Drivers and
1423 applications that are experiencing packet loss should try calling the Poll() function
1424 more often.
1425
1426 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1427
1428 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1429 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1430 @retval EFI_INVALID_PARAMETER This is NULL.
1431 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1432 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1433
1434 **/
1435 EFI_STATUS
1436 EFIAPI
1437 EfiHttpPoll (
1438 IN EFI_HTTP_PROTOCOL *This
1439 )
1440 {
1441 EFI_STATUS Status;
1442 HTTP_PROTOCOL *HttpInstance;
1443
1444 if (This == NULL) {
1445 return EFI_INVALID_PARAMETER;
1446 }
1447
1448 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
1449 ASSERT (HttpInstance != NULL);
1450
1451 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {
1452 return EFI_NOT_STARTED;
1453 }
1454
1455 if (HttpInstance->LocalAddressIsIPv6) {
1456 if (HttpInstance->Tcp6 == NULL) {
1457 return EFI_NOT_STARTED;
1458 }
1459 Status = HttpInstance->Tcp6->Poll (HttpInstance->Tcp6);
1460 } else {
1461 if (HttpInstance->Tcp4 == NULL) {
1462 return EFI_NOT_STARTED;
1463 }
1464 Status = HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);
1465 }
1466
1467 DispatchDpc ();
1468
1469 return Status;
1470 }