NetworkPkg: Add HTTP 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
769 HttpInstance = Wrap->HttpInstance;\r
770 Token = Wrap->HttpToken;\r
771\r
772 HttpMsg = Token->Message;\r
773\r
774 Tcp4 = HttpInstance->Tcp4;\r
775 ASSERT (Tcp4 != NULL);\r
776 HttpMsg->Headers = NULL;\r
777 HttpHeaders = NULL;\r
778 SizeofHeaders = 0;\r
779 Buffer = NULL;\r
780 BufferSize = 0;\r
781 EndofHeader = NULL;\r
782 \r
783 if (HttpMsg->Data.Response != NULL) {\r
784 //\r
785 // Need receive the HTTP headers, prepare buffer.\r
786 //\r
787 Status = HttpCreateTcp4RxEventForHeader (HttpInstance);\r
788 if (EFI_ERROR (Status)) {\r
789 goto Error;\r
790 }\r
791\r
792 //\r
793 // Check whether we have cached header from previous call.\r
794 //\r
795 if ((HttpInstance->CacheBody != NULL) && (HttpInstance->NextMsg != NULL)) {\r
796 //\r
797 // The data is stored at [NextMsg, CacheBody + CacheLen].\r
798 //\r
799 HdrLen = HttpInstance->CacheBody + HttpInstance->CacheLen - HttpInstance->NextMsg;\r
800 HttpHeaders = AllocateZeroPool (HdrLen);\r
801 if (HttpHeaders == NULL) {\r
802 Status = EFI_OUT_OF_RESOURCES;\r
803 goto Error;\r
804 }\r
805\r
806 CopyMem (HttpHeaders, HttpInstance->NextMsg, HdrLen);\r
807 FreePool (HttpInstance->CacheBody);\r
808 HttpInstance->CacheBody = NULL;\r
809 HttpInstance->NextMsg = NULL;\r
810 HttpInstance->CacheOffset = 0;\r
811 SizeofHeaders = HdrLen;\r
812 BufferSize = HttpInstance->CacheLen;\r
813\r
814 //\r
815 // Check whether we cached the whole HTTP headers.\r
816 //\r
817 EndofHeader = AsciiStrStr (HttpHeaders, HTTP_END_OF_HDR_STR); \r
818 }\r
819 \r
820 RxToken = &HttpInstance->RxToken;\r
821 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = AllocateZeroPool (DEF_BUF_LEN);\r
822 if (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer == NULL) {\r
823 Status = EFI_OUT_OF_RESOURCES;\r
824 goto Error;\r
825 }\r
826\r
827 //\r
828 // Receive the HTTP headers only when EFI_HTTP_RESPONSE_DATA is not NULL.\r
829 //\r
830 while (EndofHeader == NULL) { \r
831 HttpInstance->IsRxDone = FALSE;\r
832 RxToken->Packet.RxData->DataLength = DEF_BUF_LEN;\r
833 RxToken->Packet.RxData->FragmentTable[0].FragmentLength = DEF_BUF_LEN;\r
834 Status = Tcp4->Receive (Tcp4, RxToken);\r
835 if (EFI_ERROR (Status)) {\r
836 DEBUG ((EFI_D_ERROR, "Tcp4 receive failed: %r\n", Status));\r
837 goto Error;\r
838 }\r
839 \r
840 while (!HttpInstance->IsRxDone) {\r
841 Tcp4->Poll (Tcp4);\r
842 } \r
843\r
844 Status = RxToken->CompletionToken.Status;\r
845 if (EFI_ERROR (Status)) {\r
846 goto Error;\r
847 }\r
848\r
849 //\r
850 // Append the response string.\r
851 //\r
852 BufferSize = SizeofHeaders + RxToken->Packet.RxData->FragmentTable[0].FragmentLength;\r
853 Buffer = AllocateZeroPool (BufferSize);\r
854 if (Buffer == NULL) {\r
855 Status = EFI_OUT_OF_RESOURCES;\r
856 goto Error;\r
857 }\r
858\r
859 if (HttpHeaders != NULL) {\r
860 CopyMem (Buffer, HttpHeaders, SizeofHeaders);\r
861 FreePool (HttpHeaders);\r
862 }\r
863\r
864 CopyMem (\r
865 Buffer + SizeofHeaders,\r
866 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer,\r
867 RxToken->Packet.RxData->FragmentTable[0].FragmentLength\r
868 );\r
869 HttpHeaders = Buffer;\r
870 SizeofHeaders = BufferSize;\r
871\r
872 //\r
873 // Check whether we received end of HTTP headers.\r
874 //\r
875 EndofHeader = AsciiStrStr (HttpHeaders, HTTP_END_OF_HDR_STR); \r
876 };\r
877\r
878 //\r
879 // Skip the CRLF after the HTTP headers.\r
880 //\r
881 EndofHeader = EndofHeader + AsciiStrLen (HTTP_END_OF_HDR_STR);\r
882\r
883 //\r
884 // Cache the part of body.\r
885 //\r
886 BodyLen = BufferSize - (EndofHeader - HttpHeaders);\r
887 if (BodyLen > 0) {\r
888 if (HttpInstance->CacheBody != NULL) {\r
889 FreePool (HttpInstance->CacheBody);\r
890 }\r
891\r
892 HttpInstance->CacheBody = AllocateZeroPool (BodyLen);\r
893 if (HttpInstance->CacheBody == NULL) {\r
894 Status = EFI_OUT_OF_RESOURCES;\r
895 goto Error;\r
896 }\r
897\r
898 CopyMem (HttpInstance->CacheBody, EndofHeader, BodyLen);\r
899 HttpInstance->CacheLen = BodyLen;\r
900 }\r
901\r
902 FreePool (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer);\r
903 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL;\r
904\r
905 //\r
906 // Search for Status Code.\r
907 //\r
908 StatusCodeStr = HttpHeaders + AsciiStrLen (HTTP_VERSION_STR) + 1;\r
909 if (StatusCodeStr == NULL) {\r
910 goto Error;\r
911 }\r
912\r
913 StatusCode = AsciiStrDecimalToUintn (StatusCodeStr);\r
914\r
915 //\r
916 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".\r
917 //\r
918 Tmp = AsciiStrStr (HttpHeaders, HTTP_CRLF_STR);\r
919 if (Tmp == NULL) {\r
920 goto Error;\r
921 }\r
922\r
923 Tmp = Tmp + AsciiStrLen (HTTP_CRLF_STR);\r
924 SizeofHeaders = SizeofHeaders - (Tmp - HttpHeaders);\r
925 HeaderTmp = AllocateZeroPool (SizeofHeaders);\r
926 if (HeaderTmp == NULL) {\r
927 goto Error;\r
928 }\r
929\r
930 CopyMem (HeaderTmp, Tmp, SizeofHeaders);\r
931 FreePool (HttpHeaders);\r
932 HttpHeaders = HeaderTmp;\r
933 //\r
934 // Parse the HTTP header into array of key/value pairs.\r
935 //\r
936 Status = HttpUtilitiesParse (HttpHeaders, SizeofHeaders, &HttpMsg->Headers, &HttpMsg->HeaderCount);\r
937 if (EFI_ERROR (Status)) {\r
938 goto Error;\r
939 }\r
940\r
941 FreePool (HttpHeaders);\r
942 HttpHeaders = NULL;\r
943 \r
944 HttpMsg->Data.Response->StatusCode = HttpMappingToStatusCode (StatusCode);\r
945\r
946 //\r
947 // Init message-body parser by header information. \r
948 //\r
949 Status = EFI_NOT_READY;\r
950 ValueInItem = NULL;\r
951 NetMapRemoveHead (&HttpInstance->TxTokens, (VOID**) &ValueInItem);\r
952 if (ValueInItem == NULL) {\r
953 goto Error;\r
954 }\r
955\r
956 //\r
957 // The first TxToken not transmitted yet, insert back and return error.\r
958 //\r
959 if (!ValueInItem->TcpWrap.IsTxDone) {\r
960 goto Error2;\r
961 }\r
962\r
963 Status = HttpInitMsgParser (\r
964 ValueInItem->TcpWrap.Method,\r
965 HttpMsg->Data.Response->StatusCode,\r
966 HttpMsg->HeaderCount,\r
967 HttpMsg->Headers,\r
968 HttpBodyParserCallback,\r
969 (VOID *) ValueInItem,\r
970 &HttpInstance->MsgParser\r
971 );\r
972 if (EFI_ERROR (Status)) { \r
973 goto Error2;\r
974 }\r
975\r
976 //\r
977 // Check whether we received a complete HTTP message.\r
978 //\r
979 if (HttpInstance->CacheBody != NULL) {\r
980 Status = HttpParseMessageBody (HttpInstance->MsgParser, HttpInstance->CacheLen, HttpInstance->CacheBody);\r
981 if (EFI_ERROR (Status)) {\r
982 goto Error2;\r
983 }\r
984\r
985 if (HttpIsMessageComplete (HttpInstance->MsgParser)) {\r
986 //\r
987 // Free the MsgParse since we already have a full HTTP message.\r
988 //\r
989 HttpFreeMsgParser (HttpInstance->MsgParser);\r
990 HttpInstance->MsgParser = NULL;\r
991 }\r
992 }\r
993\r
994 if ((HttpMsg->Body == NULL) || (HttpMsg->BodyLength == 0)) { \r
995 Status = EFI_SUCCESS;\r
996 goto Exit;\r
997 }\r
998 } \r
999\r
1000 //\r
1001 // Receive the response body.\r
1002 //\r
1003 BodyLen = 0;\r
1004\r
1005 //\r
1006 // First check whether we cached some data.\r
1007 //\r
1008 if (HttpInstance->CacheBody != NULL) {\r
1009 //\r
1010 // Calculate the length of the cached data.\r
1011 //\r
1012 if (HttpInstance->NextMsg != NULL) {\r
1013 //\r
1014 // We have a cached HTTP message which includes a part of HTTP header of next message.\r
1015 //\r
1016 BodyLen = HttpInstance->NextMsg - (HttpInstance->CacheBody + HttpInstance->CacheOffset); \r
1017 } else {\r
1018 BodyLen = HttpInstance->CacheLen - HttpInstance->CacheOffset;\r
1019 }\r
1020\r
1021 if (BodyLen > 0) {\r
1022 //\r
1023 // We have some cached data. Just copy the data and return.\r
1024 //\r
1025 if (HttpMsg->BodyLength < BodyLen) {\r
1026 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, HttpMsg->BodyLength);\r
1027 HttpInstance->CacheOffset = HttpInstance->CacheOffset + HttpMsg->BodyLength;\r
1028 } else {\r
1029 //\r
1030 // Copy all cached data out.\r
1031 //\r
1032 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, BodyLen);\r
1033 HttpInstance->CacheOffset = BodyLen + HttpInstance->CacheOffset;\r
1034 HttpMsg->BodyLength = BodyLen;\r
1035\r
1036 if (HttpInstance->NextMsg == NULL) {\r
1037 //\r
1038 // There is no HTTP header of next message. Just free the cache buffer.\r
1039 //\r
1040 FreePool (HttpInstance->CacheBody);\r
1041 HttpInstance->CacheBody = NULL;\r
1042 HttpInstance->NextMsg = NULL;\r
1043 HttpInstance->CacheOffset = 0;\r
1044 }\r
1045 }\r
1046 //\r
1047 // Return since we aready received required data.\r
1048 //\r
1049 Status = EFI_SUCCESS;\r
1050 goto Exit;\r
1051 } \r
1052\r
1053 if (BodyLen == 0 && HttpInstance->MsgParser == NULL) {\r
1054 //\r
1055 // We received a complete HTTP message, and we don't have more data to return to caller.\r
1056 //\r
1057 HttpMsg->BodyLength = 0;\r
1058 Status = EFI_SUCCESS;\r
1059 goto Exit; \r
1060 } \r
1061 }\r
1062\r
1063 ASSERT (HttpInstance->MsgParser != NULL);\r
1064\r
1065 //\r
1066 // We still need receive more data when there is no cache data and MsgParser is not NULL;\r
1067 //\r
1068 RxToken = &Wrap->TcpWrap.RxToken;\r
1069\r
1070 RxToken->Packet.RxData->DataLength = (UINT32) HttpMsg->BodyLength;\r
1071 RxToken->Packet.RxData->FragmentTable[0].FragmentLength = (UINT32) HttpMsg->BodyLength;\r
1072 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = (VOID *) HttpMsg->Body;\r
1073\r
1074 RxToken->CompletionToken.Status = EFI_NOT_READY;\r
1075 Status = Tcp4->Receive (Tcp4, RxToken);\r
1076 if (EFI_ERROR (Status)) {\r
1077 DEBUG ((EFI_D_ERROR, "Tcp4 receive failed: %r\n", Status));\r
1078 goto Error;\r
1079 }\r
1080\r
1081 return Status;\r
1082\r
1083Exit:\r
1084 Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);\r
1085 if (Item != NULL) {\r
1086 NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);\r
1087 }\r
1088 Token->Status = Status;\r
1089 gBS->SignalEvent (Token->Event);\r
1090 FreePool (Wrap);\r
1091 return Status;\r
1092\r
1093Error2:\r
1094 NetMapInsertHead (&HttpInstance->TxTokens, ValueInItem->HttpToken, ValueInItem);\r
1095\r
1096Error:\r
1097 if (Wrap != NULL) {\r
1098 if (Wrap->TcpWrap.RxToken.CompletionToken.Event != NULL) {\r
1099 gBS->CloseEvent (Wrap->TcpWrap.RxToken.CompletionToken.Event);\r
1100 }\r
1101 RxToken = &Wrap->TcpWrap.RxToken;\r
1102 if (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {\r
1103 FreePool (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer);\r
1104 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL;\r
1105 }\r
1106 FreePool (Wrap);\r
1107 }\r
1108\r
1109 if (HttpInstance->RxToken.CompletionToken.Event != NULL) {\r
1110 gBS->CloseEvent (HttpInstance->RxToken.CompletionToken.Event);\r
1111 HttpInstance->RxToken.CompletionToken.Event = NULL;\r
1112 }\r
1113\r
1114 RxToken = &HttpInstance->RxToken;\r
1115 if (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {\r
1116 FreePool (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer);\r
1117 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL;\r
1118 }\r
1119 \r
1120 if (HttpHeaders != NULL) {\r
1121 FreePool (HttpHeaders);\r
1122 }\r
1123\r
1124 if (HttpMsg->Headers != NULL) {\r
1125 FreePool (HttpMsg->Headers);\r
1126 }\r
1127\r
1128 if (HttpInstance->CacheBody != NULL) {\r
1129 FreePool (HttpInstance->CacheBody);\r
1130 HttpInstance->CacheBody = NULL;\r
1131 }\r
1132\r
1133 Token->Status = Status;\r
1134 gBS->SignalEvent (Token->Event);\r
1135\r
1136 return Status; \r
1137\r
1138}\r
1139\r
1140\r
1141/**\r
1142 The Response() function queues an HTTP response to this HTTP instance, similar to\r
1143 Receive() function in the EFI TCP driver. When the HTTP request is sent successfully,\r
1144 or if there is an error, Status in token will be updated and Event will be signaled.\r
1145\r
1146 The HTTP driver will queue a receive token to the underlying TCP instance. When data\r
1147 is received in the underlying TCP instance, the data will be parsed and Token will\r
1148 be populated with the response data. If the data received from the remote host\r
1149 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting\r
1150 (asynchronously) for more data to be sent from the remote host before signaling\r
1151 Event in Token.\r
1152\r
1153 It is the responsibility of the caller to allocate a buffer for Body and specify the\r
1154 size in BodyLength. If the remote host provides a response that contains a content\r
1155 body, up to BodyLength bytes will be copied from the receive buffer into Body and\r
1156 BodyLength will be updated with the amount of bytes received and copied to Body. This\r
1157 allows the client to download a large file in chunks instead of into one contiguous\r
1158 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is\r
1159 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive\r
1160 token to underlying TCP instance. If data arrives in the receive buffer, up to\r
1161 BodyLength bytes of data will be copied to Body. The HTTP driver will then update\r
1162 BodyLength with the amount of bytes received and copied to Body.\r
1163\r
1164 If the HTTP driver does not have an open underlying TCP connection with the host\r
1165 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is\r
1166 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain\r
1167 an open TCP connection between client and host.\r
1168\r
1169 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
1170 @param[in] Token Pointer to storage containing HTTP response token.\r
1171\r
1172 @retval EFI_SUCCESS Allocation succeeded.\r
1173 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been\r
1174 initialized.\r
1175 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
1176 This is NULL.\r
1177 Token is NULL.\r
1178 Token->Message->Headers is NULL.\r
1179 Token->Message is NULL.\r
1180 Token->Message->Body is not NULL,\r
1181 Token->Message->BodyLength is non-zero, and\r
1182 Token->Message->Data is NULL, but a previous call to\r
1183 Response() has not been completed successfully.\r
1184 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.\r
1185 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host\r
1186 specified by response URL.\r
1187**/\r
1188EFI_STATUS\r
1189EFIAPI\r
1190EfiHttpResponse (\r
1191 IN EFI_HTTP_PROTOCOL *This,\r
1192 IN EFI_HTTP_TOKEN *Token\r
1193 )\r
1194{\r
1195 EFI_STATUS Status;\r
1196 EFI_HTTP_MESSAGE *HttpMsg;\r
1197 HTTP_PROTOCOL *HttpInstance;\r
1198 HTTP_TOKEN_WRAP *Wrap;\r
1199\r
1200 if ((This == NULL) || (Token == NULL)) {\r
1201 return EFI_INVALID_PARAMETER;\r
1202 }\r
1203\r
1204 HttpMsg = Token->Message;\r
1205 if (HttpMsg == NULL) {\r
1206 return EFI_INVALID_PARAMETER;\r
1207 }\r
1208 \r
1209 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
1210 ASSERT (HttpInstance != NULL);\r
1211\r
1212 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r
1213 return EFI_NOT_STARTED;\r
1214 }\r
1215\r
1216 if (HttpInstance->LocalAddressIsIPv6) {\r
1217 return EFI_UNSUPPORTED;\r
1218 } \r
1219\r
1220 //\r
1221 // Check whether the token already existed.\r
1222 //\r
1223 if (EFI_ERROR (NetMapIterate (&HttpInstance->RxTokens, HttpTokenExist, Token))) {\r
1224 return EFI_ACCESS_DENIED; \r
1225 }\r
1226\r
1227 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r
1228 if (Wrap == NULL) {\r
1229 return EFI_OUT_OF_RESOURCES;\r
1230 }\r
1231\r
1232 Wrap->HttpInstance = HttpInstance;\r
1233 Wrap->HttpToken = Token;\r
1234\r
1235 Status = HttpCreateTcp4RxEvent (Wrap);\r
1236 if (EFI_ERROR (Status)) {\r
1237 goto Error;\r
1238 }\r
1239\r
1240 Status = NetMapInsertTail (&HttpInstance->RxTokens, Token, Wrap);\r
1241 if (EFI_ERROR (Status)) {\r
1242 goto Error;\r
1243 }\r
1244\r
1245 //\r
1246 // If already have pending RxTokens, return directly.\r
1247 //\r
1248 if (NetMapGetCount (&HttpInstance->RxTokens) > 1) {\r
1249 return EFI_SUCCESS;\r
1250 }\r
1251\r
1252 return HttpResponseWorker (Wrap);\r
1253\r
1254Error:\r
1255 if (Wrap != NULL) {\r
1256 if (Wrap->TcpWrap.RxToken.CompletionToken.Event != NULL) {\r
1257 gBS->CloseEvent (Wrap->TcpWrap.RxToken.CompletionToken.Event);\r
1258 }\r
1259 FreePool (Wrap);\r
1260 } \r
1261\r
1262 return Status; \r
1263}\r
1264\r
1265/**\r
1266 The Poll() function can be used by network drivers and applications to increase the\r
1267 rate that data packets are moved between the communication devices and the transmit\r
1268 and receive queues.\r
1269\r
1270 In some systems, the periodic timer event in the managed network driver may not poll\r
1271 the underlying communications device fast enough to transmit and/or receive all data\r
1272 packets without missing incoming packets or dropping outgoing packets. Drivers and\r
1273 applications that are experiencing packet loss should try calling the Poll() function\r
1274 more often.\r
1275\r
1276 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
1277\r
1278 @retval EFI_SUCCESS Incoming or outgoing data was processed.\r
1279 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
1280 @retval EFI_INVALID_PARAMETER This is NULL.\r
1281 @retval EFI_NOT_READY No incoming or outgoing data is processed.\r
1282 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.\r
1283\r
1284**/\r
1285EFI_STATUS\r
1286EFIAPI\r
1287EfiHttpPoll (\r
1288 IN EFI_HTTP_PROTOCOL *This\r
1289 )\r
1290{\r
1291 HTTP_PROTOCOL *HttpInstance;\r
1292\r
1293 if (This == NULL) {\r
1294 return EFI_INVALID_PARAMETER;\r
1295 }\r
1296\r
1297 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
1298 ASSERT (HttpInstance != NULL);\r
1299\r
1300 if (HttpInstance->LocalAddressIsIPv6) {\r
1301 return EFI_UNSUPPORTED;\r
1302 }\r
1303\r
1304 if (HttpInstance->Tcp4 == NULL || HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r
1305 return EFI_NOT_STARTED;\r
1306 }\r
1307\r
1308 return HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);\r
1309}\r