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