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