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