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