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