ShellPkg: Don't strip positional parameters of quotation marks.
[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
c5a693ce 5 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
47f51a06
YT
6\r
7 This program and the accompanying materials\r
8 are licensed and made available under the terms and conditions of the BSD License\r
9 which accompanies this distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php.\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include "HttpDriver.h"\r
18\r
19EFI_HTTP_PROTOCOL mEfiHttpTemplate = {\r
20 EfiHttpGetModeData,\r
21 EfiHttpConfigure,\r
22 EfiHttpRequest,\r
23 EfiHttpCancel,\r
24 EfiHttpResponse,\r
25 EfiHttpPoll\r
26};\r
27\r
28/**\r
29 Returns the operational parameters for the current HTTP child instance.\r
30\r
31 The GetModeData() function is used to read the current mode data (operational\r
32 parameters) for this HTTP protocol instance.\r
33\r
34 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
35 @param[out] HttpConfigData Point to buffer for operational parameters of this\r
36 HTTP instance.\r
37\r
38 @retval EFI_SUCCESS Operation succeeded.\r
39 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
40 This is NULL.\r
41 HttpConfigData is NULL.\r
42 HttpConfigData->AccessPoint is NULL.\r
43 @retval EFI_NOT_STARTED The HTTP instance is not configured.\r
44\r
45**/\r
46EFI_STATUS\r
47EFIAPI\r
48EfiHttpGetModeData (\r
49 IN EFI_HTTP_PROTOCOL *This,\r
50 OUT EFI_HTTP_CONFIG_DATA *HttpConfigData\r
51 )\r
52{\r
53 HTTP_PROTOCOL *HttpInstance;\r
54\r
55 if ((This == NULL) || (HttpConfigData == NULL)) {\r
56 return EFI_INVALID_PARAMETER;\r
57 }\r
58 \r
59 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
60 ASSERT (HttpInstance != NULL);\r
61\r
62 if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) {\r
63 return EFI_NOT_STARTED;\r
64 }\r
65\r
66 if (HttpConfigData->AccessPoint.IPv4Node == NULL) {\r
67 return EFI_INVALID_PARAMETER;\r
68 }\r
69\r
70 HttpConfigData->HttpVersion = HttpInstance->HttpVersion;\r
71 HttpConfigData->TimeOutMillisec = HttpInstance->TimeOutMillisec;\r
72 HttpConfigData->LocalAddressIsIPv6 = HttpInstance->LocalAddressIsIPv6;\r
73\r
74 CopyMem (\r
75 HttpConfigData->AccessPoint.IPv4Node,\r
76 &HttpInstance->IPv4Node,\r
77 sizeof (HttpInstance->IPv4Node)\r
78 );\r
79\r
80 return EFI_SUCCESS;\r
81}\r
82\r
83/**\r
84 Initialize or brutally reset the operational parameters for this EFI HTTP instance.\r
85\r
86 The Configure() function does the following:\r
87 When HttpConfigData is not NULL Initialize this EFI HTTP instance by configuring\r
88 timeout, local address, port, etc.\r
89 When HttpConfigData is NULL, reset this EFI HTTP instance by closing all active\r
90 connections with remote hosts, canceling all asynchronous tokens, and flush request\r
91 and response buffers without informing the appropriate hosts.\r
92\r
93 Except for GetModeData() and Configure(), No other EFI HTTP function can be executed\r
94 by this instance until the Configure() function is executed and returns successfully.\r
95\r
96 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
97 @param[in] HttpConfigData Pointer to the configure data to configure the instance.\r
98\r
99 @retval EFI_SUCCESS Operation succeeded.\r
100 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
101 This is NULL.\r
102 HttpConfigData->LocalAddressIsIPv6 is FALSE and\r
103 HttpConfigData->IPv4Node is NULL.\r
104 HttpConfigData->LocalAddressIsIPv6 is TRUE and\r
105 HttpConfigData->IPv6Node is NULL.\r
106 @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling\r
107 Configure() with NULL to reset it.\r
108 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
109 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when\r
110 executing Configure().\r
111 @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported\r
112 in the implementation.\r
113**/\r
114EFI_STATUS\r
115EFIAPI\r
116EfiHttpConfigure (\r
117 IN EFI_HTTP_PROTOCOL *This,\r
118 IN EFI_HTTP_CONFIG_DATA *HttpConfigData\r
119 ) \r
120{\r
121 HTTP_PROTOCOL *HttpInstance;\r
122 EFI_STATUS Status;\r
123\r
124 if (This == NULL) {\r
125 return EFI_INVALID_PARAMETER;\r
126 }\r
127\r
128 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
129 ASSERT (HttpInstance != NULL && HttpInstance->Service != NULL);\r
130\r
131 if (HttpConfigData != NULL) {\r
132 //\r
133 // Check input parameters.\r
134 //\r
135 if (HttpConfigData->LocalAddressIsIPv6) {\r
136 if (HttpConfigData->AccessPoint.IPv6Node == NULL) {\r
137 return EFI_INVALID_PARAMETER;\r
138 }\r
139 } else {\r
140 if (HttpConfigData->AccessPoint.IPv4Node == NULL) {\r
141 return EFI_INVALID_PARAMETER;\r
142 }\r
143 }\r
144 //\r
145 // Now configure this HTTP instance.\r
146 //\r
147 if (HttpInstance->State != HTTP_STATE_UNCONFIGED) {\r
148 return EFI_ALREADY_STARTED;\r
149 }\r
150\r
151 HttpInstance->HttpVersion = HttpConfigData->HttpVersion;\r
152 HttpInstance->TimeOutMillisec = HttpConfigData->TimeOutMillisec;\r
153 HttpInstance->LocalAddressIsIPv6 = HttpConfigData->LocalAddressIsIPv6;\r
154\r
155 if (HttpConfigData->LocalAddressIsIPv6) {\r
156 return EFI_UNSUPPORTED;\r
157 } else {\r
158 CopyMem (\r
159 &HttpInstance->IPv4Node,\r
160 HttpConfigData->AccessPoint.IPv4Node,\r
161 sizeof (HttpInstance->IPv4Node)\r
162 );\r
163\r
164 HttpInstance->State = HTTP_STATE_HTTP_CONFIGED;\r
165 return EFI_SUCCESS;\r
166 }\r
167\r
168 } else {\r
169 if (HttpInstance->LocalAddressIsIPv6) {\r
170 return EFI_UNSUPPORTED;\r
171 } else {\r
172 HttpCleanProtocol (HttpInstance);\r
173 Status = HttpInitProtocol (HttpInstance->Service, HttpInstance);\r
174 if (EFI_ERROR (Status)) {\r
175 return Status;\r
176 }\r
177\r
178 HttpInstance->State = HTTP_STATE_UNCONFIGED;\r
179 return EFI_SUCCESS;\r
180 }\r
181 }\r
182}\r
183 \r
184\r
185/**\r
186 The Request() function queues an HTTP request to this HTTP instance.\r
187\r
188 Similar to Transmit() function in the EFI TCP driver. When the HTTP request is sent\r
189 successfully, or if there is an error, Status in token will be updated and Event will\r
190 be signaled.\r
191\r
192 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
193 @param[in] Token Pointer to storage containing HTTP request token.\r
194\r
195 @retval EFI_SUCCESS Outgoing data was processed.\r
196 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.\r
197 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
198 @retval EFI_TIMEOUT Data was dropped out of the transmit or receive queue.\r
199 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.\r
200 @retval EFI_UNSUPPORTED The HTTP method is not supported in current\r
201 implementation.\r
202 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
203 This is NULL.\r
204 Token->Message is NULL.\r
205 Token->Message->Body is not NULL,\r
206 Token->Message->BodyLength is non-zero, and\r
207 Token->Message->Data is NULL, but a previous call to\r
208 Request()has not been completed successfully.\r
209**/\r
210EFI_STATUS\r
211EFIAPI\r
212EfiHttpRequest (\r
213 IN EFI_HTTP_PROTOCOL *This,\r
214 IN EFI_HTTP_TOKEN *Token\r
215 )\r
216{\r
217 EFI_HTTP_MESSAGE *HttpMsg;\r
218 EFI_HTTP_REQUEST_DATA *Request;\r
219 VOID *UrlParser;\r
220 EFI_STATUS Status;\r
221 CHAR8 *HostName;\r
222 UINT16 RemotePort;\r
223 HTTP_PROTOCOL *HttpInstance;\r
224 BOOLEAN Configure;\r
225 BOOLEAN ReConfigure;\r
226 CHAR8 *RequestStr;\r
227 CHAR8 *Url;\r
51b0450e 228 UINTN UrlLen;\r
47f51a06
YT
229 CHAR16 *HostNameStr;\r
230 HTTP_TOKEN_WRAP *Wrap;\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
47f51a06
YT
281\r
282 //\r
283 // Parse the URI of the remote host.\r
284 //\r
7ef0690f 285 Url = HttpInstance->Url;\r
51b0450e
FS
286 UrlLen = StrLen (Request->Url) + 1;\r
287 if (UrlLen > HTTP_URL_BUFFER_LEN) {\r
288 Url = AllocateZeroPool (UrlLen);\r
289 if (Url == NULL) {\r
290 return EFI_OUT_OF_RESOURCES;\r
291 }\r
292 FreePool (HttpInstance->Url);\r
293 HttpInstance->Url = Url; \r
294 } \r
47f51a06
YT
295\r
296 UnicodeStrToAsciiStr (Request->Url, Url);\r
297 UrlParser = NULL;\r
298 Status = HttpParseUrl (Url, (UINT32) AsciiStrLen (Url), FALSE, &UrlParser);\r
299 if (EFI_ERROR (Status)) {\r
300 goto Error1;\r
301 }\r
302\r
303 RequestStr = NULL;\r
304 HostName = NULL;\r
305 Status = HttpUrlGetHostName (Url, UrlParser, &HostName);\r
306 if (EFI_ERROR (Status)) {\r
307 goto Error1;\r
308 }\r
309\r
310 Status = HttpUrlGetPort (Url, UrlParser, &RemotePort);\r
311 if (EFI_ERROR (Status)) {\r
312 RemotePort = HTTP_DEFAULT_PORT;\r
313 }\r
314\r
315 Configure = TRUE;\r
316 ReConfigure = TRUE; \r
317\r
a8706acb 318 if (HttpInstance->RemoteHost == NULL) {\r
47f51a06
YT
319 //\r
320 // Request() is called the first time. \r
321 //\r
322 ReConfigure = FALSE;\r
323 } else {\r
324 if ((HttpInstance->RemotePort == RemotePort) &&\r
325 (AsciiStrCmp (HttpInstance->RemoteHost, HostName) == 0)) {\r
326 //\r
327 // Host Name and port number of the request URL are the same with previous call to Request().\r
328 // Check whether previous TCP packet sent out.\r
329 //\r
330 if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTcpNotReady, NULL))) {\r
331 //\r
332 // Wrap the HTTP token in HTTP_TOKEN_WRAP\r
333 //\r
334 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r
335 if (Wrap == NULL) {\r
336 Status = EFI_OUT_OF_RESOURCES;\r
337 goto Error1;\r
338 }\r
339\r
340 Wrap->HttpToken = Token;\r
341 Wrap->HttpInstance = HttpInstance;\r
342\r
343 Status = HttpCreateTcp4TxEvent (Wrap);\r
344 if (EFI_ERROR (Status)) {\r
345 goto Error1;\r
346 }\r
347\r
348 Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap);\r
349 if (EFI_ERROR (Status)) {\r
350 goto Error1;\r
351 }\r
352\r
353 Wrap->TcpWrap.Method = Request->Method;\r
354\r
47f51a06
YT
355 FreePool (HostName);\r
356 \r
357 //\r
358 // Queue the HTTP token and return.\r
359 //\r
360 return EFI_SUCCESS;\r
361 } else {\r
362 //\r
363 // Use existing TCP instance to transmit the packet.\r
364 //\r
365 Configure = FALSE;\r
366 ReConfigure = FALSE;\r
367 }\r
368 } else {\r
369 //\r
370 // Need close existing TCP instance and create a new TCP instance for data transmit.\r
371 //\r
372 if (HttpInstance->RemoteHost != NULL) {\r
373 FreePool (HttpInstance->RemoteHost);\r
374 HttpInstance->RemoteHost = NULL;\r
a8706acb 375 HttpInstance->RemotePort = 0;\r
47f51a06
YT
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 //\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
49c9f74c
FS
501 DispatchDpc ();\r
502\r
cdf8c32e
NH
503 if (HostName != NULL) {\r
504 FreePool (HostName);\r
505 }\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
3666990b
NH
1133\r
1134 if (Wrap != NULL) {\r
1135 if (Wrap->TcpWrap.RxToken.CompletionToken.Event != NULL) {\r
1136 gBS->CloseEvent (Wrap->TcpWrap.RxToken.CompletionToken.Event);\r
1137 }\r
1138 }\r
1139 \r
1140 if (HttpInstance->RxToken.CompletionToken.Event != NULL) {\r
1141 gBS->CloseEvent (HttpInstance->RxToken.CompletionToken.Event);\r
1142 HttpInstance->RxToken.CompletionToken.Event = NULL;\r
1143 } \r
1144 \r
47f51a06
YT
1145 FreePool (Wrap);\r
1146 return Status;\r
1147\r
1148Error2:\r
1149 NetMapInsertHead (&HttpInstance->TxTokens, ValueInItem->HttpToken, ValueInItem);\r
1150\r
1151Error:\r
1152 if (Wrap != NULL) {\r
1153 if (Wrap->TcpWrap.RxToken.CompletionToken.Event != NULL) {\r
1154 gBS->CloseEvent (Wrap->TcpWrap.RxToken.CompletionToken.Event);\r
1155 }\r
1156 RxToken = &Wrap->TcpWrap.RxToken;\r
1157 if (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {\r
1158 FreePool (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer);\r
1159 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL;\r
1160 }\r
1161 FreePool (Wrap);\r
1162 }\r
1163\r
1164 if (HttpInstance->RxToken.CompletionToken.Event != NULL) {\r
1165 gBS->CloseEvent (HttpInstance->RxToken.CompletionToken.Event);\r
1166 HttpInstance->RxToken.CompletionToken.Event = NULL;\r
1167 }\r
1168\r
1169 RxToken = &HttpInstance->RxToken;\r
1170 if (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {\r
1171 FreePool (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer);\r
1172 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL;\r
1173 }\r
1174 \r
1175 if (HttpHeaders != NULL) {\r
1176 FreePool (HttpHeaders);\r
1177 }\r
1178\r
1179 if (HttpMsg->Headers != NULL) {\r
1180 FreePool (HttpMsg->Headers);\r
1181 }\r
1182\r
1183 if (HttpInstance->CacheBody != NULL) {\r
1184 FreePool (HttpInstance->CacheBody);\r
1185 HttpInstance->CacheBody = NULL;\r
1186 }\r
1187\r
1188 Token->Status = Status;\r
1189 gBS->SignalEvent (Token->Event);\r
1190\r
1191 return Status; \r
1192\r
1193}\r
1194\r
1195\r
1196/**\r
1197 The Response() function queues an HTTP response to this HTTP instance, similar to\r
1198 Receive() function in the EFI TCP driver. When the HTTP request is sent successfully,\r
1199 or if there is an error, Status in token will be updated and Event will be signaled.\r
1200\r
1201 The HTTP driver will queue a receive token to the underlying TCP instance. When data\r
1202 is received in the underlying TCP instance, the data will be parsed and Token will\r
1203 be populated with the response data. If the data received from the remote host\r
1204 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting\r
1205 (asynchronously) for more data to be sent from the remote host before signaling\r
1206 Event in Token.\r
1207\r
1208 It is the responsibility of the caller to allocate a buffer for Body and specify the\r
1209 size in BodyLength. If the remote host provides a response that contains a content\r
1210 body, up to BodyLength bytes will be copied from the receive buffer into Body and\r
1211 BodyLength will be updated with the amount of bytes received and copied to Body. This\r
1212 allows the client to download a large file in chunks instead of into one contiguous\r
1213 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is\r
1214 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive\r
1215 token to underlying TCP instance. If data arrives in the receive buffer, up to\r
1216 BodyLength bytes of data will be copied to Body. The HTTP driver will then update\r
1217 BodyLength with the amount of bytes received and copied to Body.\r
1218\r
1219 If the HTTP driver does not have an open underlying TCP connection with the host\r
1220 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is\r
1221 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain\r
1222 an open TCP connection between client and host.\r
1223\r
1224 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
1225 @param[in] Token Pointer to storage containing HTTP response token.\r
1226\r
1227 @retval EFI_SUCCESS Allocation succeeded.\r
1228 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been\r
1229 initialized.\r
1230 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
1231 This is NULL.\r
1232 Token is NULL.\r
1233 Token->Message->Headers is NULL.\r
1234 Token->Message is NULL.\r
1235 Token->Message->Body is not NULL,\r
1236 Token->Message->BodyLength is non-zero, and\r
1237 Token->Message->Data is NULL, but a previous call to\r
1238 Response() has not been completed successfully.\r
1239 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.\r
1240 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host\r
1241 specified by response URL.\r
1242**/\r
1243EFI_STATUS\r
1244EFIAPI\r
1245EfiHttpResponse (\r
1246 IN EFI_HTTP_PROTOCOL *This,\r
1247 IN EFI_HTTP_TOKEN *Token\r
1248 )\r
1249{\r
1250 EFI_STATUS Status;\r
1251 EFI_HTTP_MESSAGE *HttpMsg;\r
1252 HTTP_PROTOCOL *HttpInstance;\r
1253 HTTP_TOKEN_WRAP *Wrap;\r
1254\r
1255 if ((This == NULL) || (Token == NULL)) {\r
1256 return EFI_INVALID_PARAMETER;\r
1257 }\r
1258\r
1259 HttpMsg = Token->Message;\r
1260 if (HttpMsg == NULL) {\r
1261 return EFI_INVALID_PARAMETER;\r
1262 }\r
1263 \r
1264 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
1265 ASSERT (HttpInstance != NULL);\r
1266\r
1267 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r
1268 return EFI_NOT_STARTED;\r
1269 }\r
1270\r
1271 if (HttpInstance->LocalAddressIsIPv6) {\r
1272 return EFI_UNSUPPORTED;\r
1273 } \r
1274\r
1275 //\r
1276 // Check whether the token already existed.\r
1277 //\r
1278 if (EFI_ERROR (NetMapIterate (&HttpInstance->RxTokens, HttpTokenExist, Token))) {\r
1279 return EFI_ACCESS_DENIED; \r
1280 }\r
1281\r
1282 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r
1283 if (Wrap == NULL) {\r
1284 return EFI_OUT_OF_RESOURCES;\r
1285 }\r
1286\r
1287 Wrap->HttpInstance = HttpInstance;\r
1288 Wrap->HttpToken = Token;\r
1289\r
1290 Status = HttpCreateTcp4RxEvent (Wrap);\r
1291 if (EFI_ERROR (Status)) {\r
1292 goto Error;\r
1293 }\r
1294\r
1295 Status = NetMapInsertTail (&HttpInstance->RxTokens, Token, Wrap);\r
1296 if (EFI_ERROR (Status)) {\r
1297 goto Error;\r
1298 }\r
1299\r
1300 //\r
1301 // If already have pending RxTokens, return directly.\r
1302 //\r
1303 if (NetMapGetCount (&HttpInstance->RxTokens) > 1) {\r
1304 return EFI_SUCCESS;\r
1305 }\r
1306\r
1307 return HttpResponseWorker (Wrap);\r
1308\r
1309Error:\r
1310 if (Wrap != NULL) {\r
1311 if (Wrap->TcpWrap.RxToken.CompletionToken.Event != NULL) {\r
1312 gBS->CloseEvent (Wrap->TcpWrap.RxToken.CompletionToken.Event);\r
1313 }\r
1314 FreePool (Wrap);\r
1315 } \r
1316\r
1317 return Status; \r
1318}\r
1319\r
1320/**\r
1321 The Poll() function can be used by network drivers and applications to increase the\r
1322 rate that data packets are moved between the communication devices and the transmit\r
1323 and receive queues.\r
1324\r
1325 In some systems, the periodic timer event in the managed network driver may not poll\r
1326 the underlying communications device fast enough to transmit and/or receive all data\r
1327 packets without missing incoming packets or dropping outgoing packets. Drivers and\r
1328 applications that are experiencing packet loss should try calling the Poll() function\r
1329 more often.\r
1330\r
1331 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
1332\r
1333 @retval EFI_SUCCESS Incoming or outgoing data was processed.\r
1334 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
1335 @retval EFI_INVALID_PARAMETER This is NULL.\r
1336 @retval EFI_NOT_READY No incoming or outgoing data is processed.\r
1337 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.\r
1338\r
1339**/\r
1340EFI_STATUS\r
1341EFIAPI\r
1342EfiHttpPoll (\r
1343 IN EFI_HTTP_PROTOCOL *This\r
1344 )\r
1345{\r
1346 HTTP_PROTOCOL *HttpInstance;\r
49c9f74c 1347 EFI_STATUS Status;\r
47f51a06
YT
1348\r
1349 if (This == NULL) {\r
1350 return EFI_INVALID_PARAMETER;\r
1351 }\r
1352\r
1353 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
1354 ASSERT (HttpInstance != NULL);\r
1355\r
1356 if (HttpInstance->LocalAddressIsIPv6) {\r
1357 return EFI_UNSUPPORTED;\r
1358 }\r
1359\r
1360 if (HttpInstance->Tcp4 == NULL || HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r
1361 return EFI_NOT_STARTED;\r
1362 }\r
1363\r
49c9f74c
FS
1364 Status = HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);\r
1365\r
1366 DispatchDpc ();\r
1367\r
1368 return Status;\r
47f51a06 1369}\r