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