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