]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/HttpDxe/HttpImpl.c
NetworkPkg: Clean up source files
[mirror_edk2.git] / NetworkPkg / HttpDxe / HttpImpl.c
CommitLineData
47f51a06
YT
1/** @file\r
2 Implementation of EFI_HTTP_PROTOCOL protocol interfaces.\r
3\r
79f84eb6 4 Copyright (c) 2015 - 2018, 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
f75a7f56
LG
36 HTTP instance. It is the responsibility of the caller\r
37 to allocate the memory for HttpConfigData and\r
38 HttpConfigData->AccessPoint.IPv6Node/IPv4Node. In fact,\r
39 it is recommended to allocate sufficient memory to record\r
40 IPv6Node since it is big enough for all possibilities.\r
47f51a06
YT
41\r
42 @retval EFI_SUCCESS Operation succeeded.\r
43 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
44 This is NULL.\r
45 HttpConfigData is NULL.\r
f75a7f56 46 HttpConfigData->AccessPoint.IPv4Node or\r
de15f8b6 47 HttpConfigData->AccessPoint.IPv6Node is NULL.\r
f0ab5a81 48 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.\r
47f51a06
YT
49\r
50**/\r
51EFI_STATUS\r
52EFIAPI\r
53EfiHttpGetModeData (\r
54 IN EFI_HTTP_PROTOCOL *This,\r
55 OUT EFI_HTTP_CONFIG_DATA *HttpConfigData\r
56 )\r
57{\r
58 HTTP_PROTOCOL *HttpInstance;\r
59\r
f0ab5a81
ZL
60 //\r
61 // Check input parameters.\r
62 //\r
47f51a06
YT
63 if ((This == NULL) || (HttpConfigData == NULL)) {\r
64 return EFI_INVALID_PARAMETER;\r
65 }\r
f0ab5a81 66\r
47f51a06 67 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
f0ab5a81 68\r
de15f8b6
JW
69 if ((HttpConfigData->AccessPoint.IPv6Node == NULL) ||\r
70 (HttpConfigData->AccessPoint.IPv4Node == NULL)) {\r
f0ab5a81
ZL
71 return EFI_INVALID_PARAMETER;\r
72 }\r
73\r
47f51a06
YT
74 if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) {\r
75 return EFI_NOT_STARTED;\r
76 }\r
77\r
47f51a06
YT
78 HttpConfigData->HttpVersion = HttpInstance->HttpVersion;\r
79 HttpConfigData->TimeOutMillisec = HttpInstance->TimeOutMillisec;\r
80 HttpConfigData->LocalAddressIsIPv6 = HttpInstance->LocalAddressIsIPv6;\r
81\r
b659408b 82 if (HttpInstance->LocalAddressIsIPv6) {\r
b659408b 83 CopyMem (\r
f0ab5a81 84 HttpConfigData->AccessPoint.IPv6Node,\r
b659408b
ZL
85 &HttpInstance->Ipv6Node,\r
86 sizeof (HttpInstance->Ipv6Node)\r
47f51a06 87 );\r
b659408b 88 } else {\r
b659408b 89 CopyMem (\r
f0ab5a81 90 HttpConfigData->AccessPoint.IPv4Node,\r
b659408b
ZL
91 &HttpInstance->IPv4Node,\r
92 sizeof (HttpInstance->IPv4Node)\r
93 );\r
b659408b 94 }\r
47f51a06
YT
95\r
96 return EFI_SUCCESS;\r
97}\r
98\r
99/**\r
100 Initialize or brutally reset the operational parameters for this EFI HTTP instance.\r
101\r
102 The Configure() function does the following:\r
103 When HttpConfigData is not NULL Initialize this EFI HTTP instance by configuring\r
104 timeout, local address, port, etc.\r
105 When HttpConfigData is NULL, reset this EFI HTTP instance by closing all active\r
106 connections with remote hosts, canceling all asynchronous tokens, and flush request\r
107 and response buffers without informing the appropriate hosts.\r
108\r
f0ab5a81
ZL
109 No other EFI HTTP function can be executed by this instance until the Configure()\r
110 function is executed and returns successfully.\r
47f51a06
YT
111\r
112 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
113 @param[in] HttpConfigData Pointer to the configure data to configure the instance.\r
114\r
115 @retval EFI_SUCCESS Operation succeeded.\r
116 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
117 This is NULL.\r
118 HttpConfigData->LocalAddressIsIPv6 is FALSE and\r
de15f8b6 119 HttpConfigData->AccessPoint.IPv4Node is NULL.\r
47f51a06 120 HttpConfigData->LocalAddressIsIPv6 is TRUE and\r
de15f8b6 121 HttpConfigData->AccessPoint.IPv6Node is NULL.\r
47f51a06
YT
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
30c9eaea 134 IN EFI_HTTP_CONFIG_DATA *HttpConfigData OPTIONAL\r
f75a7f56 135 )\r
47f51a06
YT
136{\r
137 HTTP_PROTOCOL *HttpInstance;\r
138 EFI_STATUS Status;\r
f75a7f56 139\r
b659408b
ZL
140 //\r
141 // Check input parameters.\r
142 //\r
f0ab5a81 143 if (This == NULL ||\r
f75a7f56 144 (HttpConfigData != NULL &&\r
6be1193f
JW
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
2af10590 151 ASSERT (HttpInstance->Service != NULL);\r
47f51a06
YT
152\r
153 if (HttpConfigData != NULL) {\r
b659408b 154\r
79de8c79
JW
155 if (HttpConfigData->HttpVersion >= HttpVersionUnsupported) {\r
156 return EFI_UNSUPPORTED;\r
157 }\r
158\r
47f51a06
YT
159 //\r
160 // Now configure this HTTP instance.\r
161 //\r
162 if (HttpInstance->State != HTTP_STATE_UNCONFIGED) {\r
163 return EFI_ALREADY_STARTED;\r
164 }\r
165\r
166 HttpInstance->HttpVersion = HttpConfigData->HttpVersion;\r
167 HttpInstance->TimeOutMillisec = HttpConfigData->TimeOutMillisec;\r
168 HttpInstance->LocalAddressIsIPv6 = HttpConfigData->LocalAddressIsIPv6;\r
f75a7f56
LG
169\r
170 if (HttpConfigData->LocalAddressIsIPv6) {\r
b659408b
ZL
171 CopyMem (\r
172 &HttpInstance->Ipv6Node,\r
173 HttpConfigData->AccessPoint.IPv6Node,\r
174 sizeof (HttpInstance->Ipv6Node)\r
175 );\r
47f51a06
YT
176 } else {\r
177 CopyMem (\r
178 &HttpInstance->IPv4Node,\r
179 HttpConfigData->AccessPoint.IPv4Node,\r
180 sizeof (HttpInstance->IPv4Node)\r
181 );\r
47f51a06 182 }\r
f75a7f56 183\r
b659408b
ZL
184 //\r
185 // Creat Tcp child\r
186 //\r
187 Status = HttpInitProtocol (HttpInstance, HttpInstance->LocalAddressIsIPv6);\r
188 if (EFI_ERROR (Status)) {\r
189 return Status;\r
190 }\r
f75a7f56 191\r
b659408b
ZL
192 HttpInstance->State = HTTP_STATE_HTTP_CONFIGED;\r
193 return EFI_SUCCESS;\r
47f51a06
YT
194\r
195 } else {\r
b659408b
ZL
196 //\r
197 // Reset all the resources related to HttpInsance.\r
198 //\r
199 HttpCleanProtocol (HttpInstance);\r
200 HttpInstance->State = HTTP_STATE_UNCONFIGED;\r
201 return EFI_SUCCESS;\r
47f51a06
YT
202 }\r
203}\r
f75a7f56 204\r
47f51a06
YT
205\r
206/**\r
207 The Request() function queues an HTTP request to this HTTP instance.\r
208\r
209 Similar to Transmit() function in the EFI TCP driver. When the HTTP request is sent\r
210 successfully, or if there is an error, Status in token will be updated and Event will\r
211 be signaled.\r
212\r
213 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
214 @param[in] Token Pointer to storage containing HTTP request token.\r
215\r
216 @retval EFI_SUCCESS Outgoing data was processed.\r
217 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.\r
218 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
219 @retval EFI_TIMEOUT Data was dropped out of the transmit or receive queue.\r
220 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.\r
221 @retval EFI_UNSUPPORTED The HTTP method is not supported in current\r
222 implementation.\r
223 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
224 This is NULL.\r
f0ab5a81 225 Token is NULL.\r
47f51a06
YT
226 Token->Message is NULL.\r
227 Token->Message->Body is not NULL,\r
228 Token->Message->BodyLength is non-zero, and\r
229 Token->Message->Data is NULL, but a previous call to\r
230 Request()has not been completed successfully.\r
231**/\r
232EFI_STATUS\r
233EFIAPI\r
234EfiHttpRequest (\r
235 IN EFI_HTTP_PROTOCOL *This,\r
236 IN EFI_HTTP_TOKEN *Token\r
237 )\r
238{\r
239 EFI_HTTP_MESSAGE *HttpMsg;\r
240 EFI_HTTP_REQUEST_DATA *Request;\r
241 VOID *UrlParser;\r
242 EFI_STATUS Status;\r
243 CHAR8 *HostName;\r
b9679cd7 244 UINTN HostNameSize;\r
47f51a06
YT
245 UINT16 RemotePort;\r
246 HTTP_PROTOCOL *HttpInstance;\r
247 BOOLEAN Configure;\r
248 BOOLEAN ReConfigure;\r
dac45de3 249 BOOLEAN TlsConfigure;\r
19c25725 250 CHAR8 *RequestMsg;\r
47f51a06 251 CHAR8 *Url;\r
51b0450e 252 UINTN UrlLen;\r
47f51a06
YT
253 CHAR16 *HostNameStr;\r
254 HTTP_TOKEN_WRAP *Wrap;\r
b199d941 255 CHAR8 *FileUrl;\r
19c25725 256 UINTN RequestMsgSize;\r
7cf59c85 257 EFI_HANDLE ImageHandle;\r
d8293d31
NH
258\r
259 //\r
260 // Initializations\r
261 //\r
262 Url = NULL;\r
5646819f 263 UrlParser = NULL;\r
6be1193f 264 RemotePort = 0;\r
d8293d31
NH
265 HostName = NULL;\r
266 RequestMsg = NULL;\r
267 HostNameStr = NULL;\r
268 Wrap = NULL;\r
269 FileUrl = NULL;\r
dac45de3 270 TlsConfigure = FALSE;\r
d8293d31 271\r
47f51a06
YT
272 if ((This == NULL) || (Token == NULL)) {\r
273 return EFI_INVALID_PARAMETER;\r
274 }\r
275\r
276 HttpMsg = Token->Message;\r
d8293d31 277 if (HttpMsg == NULL) {\r
47f51a06
YT
278 return EFI_INVALID_PARAMETER;\r
279 }\r
280\r
47f51a06 281 Request = HttpMsg->Data.Request;\r
47f51a06
YT
282\r
283 //\r
79f84eb6 284 // Only support GET, HEAD, DELETE, PATCH, PUT and POST method in current implementation.\r
47f51a06 285 //\r
d8293d31 286 if ((Request != NULL) && (Request->Method != HttpMethodGet) &&\r
f75a7f56
LG
287 (Request->Method != HttpMethodHead) && (Request->Method != HttpMethodDelete) &&\r
288 (Request->Method != HttpMethodPut) && (Request->Method != HttpMethodPost) &&\r
79f84eb6 289 (Request->Method != HttpMethodPatch)) {\r
47f51a06
YT
290 return EFI_UNSUPPORTED;\r
291 }\r
292\r
293 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
47f51a06 294\r
d8293d31
NH
295 //\r
296 // Capture the method into HttpInstance.\r
297 //\r
298 if (Request != NULL) {\r
299 HttpInstance->Method = Request->Method;\r
300 }\r
301\r
47f51a06
YT
302 if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) {\r
303 return EFI_NOT_STARTED;\r
304 }\r
305\r
d8293d31
NH
306 if (Request == NULL) {\r
307 //\r
97c567ef 308 // Request would be NULL only for PUT/POST/PATCH operation (in the current implementation)\r
d8293d31 309 //\r
f75a7f56
LG
310 if ((HttpInstance->Method != HttpMethodPut) &&\r
311 (HttpInstance->Method != HttpMethodPost) &&\r
97c567ef 312 (HttpInstance->Method != HttpMethodPatch)) {\r
d8293d31
NH
313 return EFI_INVALID_PARAMETER;\r
314 }\r
47f51a06 315\r
d8293d31 316 //\r
97c567ef 317 // For PUT/POST/PATCH, we need to have the TCP already configured. Bail out if it is not!\r
d8293d31
NH
318 //\r
319 if (HttpInstance->State < HTTP_STATE_TCP_CONFIGED) {\r
320 return EFI_INVALID_PARAMETER;\r
321 }\r
47f51a06 322\r
d8293d31
NH
323 //\r
324 // We need to have the Message Body for sending the HTTP message across in these cases.\r
325 //\r
326 if (HttpMsg->Body == NULL || HttpMsg->BodyLength == 0) {\r
327 return EFI_INVALID_PARAMETER;\r
51b0450e 328 }\r
b659408b 329\r
d8293d31
NH
330 //\r
331 // Use existing TCP instance to transmit the packet.\r
332 //\r
333 Configure = FALSE;\r
334 ReConfigure = FALSE;\r
335 } else {\r
336 //\r
337 // Check whether the token already existed.\r
338 //\r
339 if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTokenExist, Token))) {\r
340 return EFI_ACCESS_DENIED;\r
341 }\r
47f51a06 342\r
d8293d31
NH
343 //\r
344 // Parse the URI of the remote host.\r
345 //\r
346 Url = HttpInstance->Url;\r
347 UrlLen = StrLen (Request->Url) + 1;\r
348 if (UrlLen > HTTP_URL_BUFFER_LEN) {\r
349 Url = AllocateZeroPool (UrlLen);\r
350 if (Url == NULL) {\r
351 return EFI_OUT_OF_RESOURCES;\r
352 }\r
353 FreePool (HttpInstance->Url);\r
354 HttpInstance->Url = Url;\r
355 }\r
47f51a06 356\r
47f51a06 357\r
b9679cd7 358 UnicodeStrToAsciiStrS (Request->Url, Url, UrlLen);\r
dac45de3
JW
359\r
360 //\r
f75a7f56 361 // From the information in Url, the HTTP instance will\r
dac45de3
JW
362 // be able to determine whether to use http or https.\r
363 //\r
364 HttpInstance->UseHttps = IsHttpsUrl (Url);\r
365\r
221463c2
JW
366 //\r
367 // HTTP is disabled, return directly if the URI is not HTTPS.\r
368 //\r
369 if (!PcdGetBool (PcdAllowHttpConnections) && !(HttpInstance->UseHttps)) {\r
f75a7f56 370\r
221463c2
JW
371 DEBUG ((EFI_D_ERROR, "EfiHttpRequest: HTTP is disabled.\n"));\r
372\r
373 return EFI_ACCESS_DENIED;\r
374 }\r
375\r
dac45de3
JW
376 //\r
377 // Check whether we need to create Tls child and open the TLS protocol.\r
378 //\r
379 if (HttpInstance->UseHttps && HttpInstance->TlsChildHandle == NULL) {\r
380 //\r
381 // Use TlsSb to create Tls child and open the TLS protocol.\r
382 //\r
7cf59c85
ZL
383 if (HttpInstance->LocalAddressIsIPv6) {\r
384 ImageHandle = HttpInstance->Service->Ip6DriverBindingHandle;\r
385 } else {\r
386 ImageHandle = HttpInstance->Service->Ip4DriverBindingHandle;\r
387 }\r
388\r
dac45de3 389 HttpInstance->TlsChildHandle = TlsCreateChild (\r
7cf59c85 390 ImageHandle,\r
45ea8a0c 391 &(HttpInstance->TlsSb),\r
dac45de3
JW
392 &(HttpInstance->Tls),\r
393 &(HttpInstance->TlsConfiguration)\r
394 );\r
395 if (HttpInstance->TlsChildHandle == NULL) {\r
396 return EFI_DEVICE_ERROR;\r
397 }\r
398\r
399 TlsConfigure = TRUE;\r
400 }\r
401\r
d8293d31
NH
402 UrlParser = NULL;\r
403 Status = HttpParseUrl (Url, (UINT32) AsciiStrLen (Url), FALSE, &UrlParser);\r
404 if (EFI_ERROR (Status)) {\r
405 goto Error1;\r
406 }\r
47f51a06 407\r
d8293d31
NH
408 HostName = NULL;\r
409 Status = HttpUrlGetHostName (Url, UrlParser, &HostName);\r
410 if (EFI_ERROR (Status)) {\r
411 goto Error1;\r
412 }\r
413\r
414 Status = HttpUrlGetPort (Url, UrlParser, &RemotePort);\r
415 if (EFI_ERROR (Status)) {\r
dac45de3
JW
416 if (HttpInstance->UseHttps) {\r
417 RemotePort = HTTPS_DEFAULT_PORT;\r
418 } else {\r
419 RemotePort = HTTP_DEFAULT_PORT;\r
420 }\r
d8293d31 421 }\r
47f51a06 422 //\r
d8293d31
NH
423 // If Configure is TRUE, it indicates the first time to call Request();\r
424 // If ReConfigure is TRUE, it indicates the request URL is not same\r
425 // with the previous call to Request();\r
47f51a06 426 //\r
d8293d31
NH
427 Configure = TRUE;\r
428 ReConfigure = TRUE;\r
429\r
430 if (HttpInstance->RemoteHost == NULL) {\r
47f51a06 431 //\r
d8293d31 432 // Request() is called the first time.\r
47f51a06 433 //\r
d8293d31
NH
434 ReConfigure = FALSE;\r
435 } else {\r
436 if ((HttpInstance->RemotePort == RemotePort) &&\r
f75a7f56
LG
437 (AsciiStrCmp (HttpInstance->RemoteHost, HostName) == 0) &&\r
438 (!HttpInstance->UseHttps || (HttpInstance->UseHttps &&\r
439 !TlsConfigure &&\r
dac45de3 440 HttpInstance->TlsSessionState == EfiTlsSessionDataTransferring))) {\r
47f51a06 441 //\r
d8293d31 442 // Host Name and port number of the request URL are the same with previous call to Request().\r
dac45de3 443 // If Https protocol used, the corresponding SessionState is EfiTlsSessionDataTransferring.\r
d8293d31 444 // Check whether previous TCP packet sent out.\r
47f51a06 445 //\r
47f51a06 446\r
d8293d31
NH
447 if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTcpNotReady, NULL))) {\r
448 //\r
449 // Wrap the HTTP token in HTTP_TOKEN_WRAP\r
450 //\r
451 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r
452 if (Wrap == NULL) {\r
453 Status = EFI_OUT_OF_RESOURCES;\r
454 goto Error1;\r
455 }\r
456\r
457 Wrap->HttpToken = Token;\r
458 Wrap->HttpInstance = HttpInstance;\r
459\r
460 Status = HttpCreateTcpTxEvent (Wrap);\r
461 if (EFI_ERROR (Status)) {\r
462 goto Error1;\r
463 }\r
464\r
465 Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap);\r
466 if (EFI_ERROR (Status)) {\r
467 goto Error1;\r
468 }\r
469\r
470 Wrap->TcpWrap.Method = Request->Method;\r
471\r
472 FreePool (HostName);\r
473\r
c0a0a5a5
JW
474 HttpUrlFreeParser (UrlParser);\r
475\r
d8293d31
NH
476 //\r
477 // Queue the HTTP token and return.\r
478 //\r
479 return EFI_SUCCESS;\r
480 } else {\r
481 //\r
482 // Use existing TCP instance to transmit the packet.\r
483 //\r
484 Configure = FALSE;\r
485 ReConfigure = FALSE;\r
47f51a06 486 }\r
47f51a06
YT
487 } else {\r
488 //\r
d8293d31 489 // Need close existing TCP instance and create a new TCP instance for data transmit.\r
47f51a06 490 //\r
d8293d31
NH
491 if (HttpInstance->RemoteHost != NULL) {\r
492 FreePool (HttpInstance->RemoteHost);\r
493 HttpInstance->RemoteHost = NULL;\r
494 HttpInstance->RemotePort = 0;\r
495 }\r
47f51a06
YT
496 }\r
497 }\r
f75a7f56 498 }\r
47f51a06
YT
499\r
500 if (Configure) {\r
501 //\r
b659408b 502 // Parse Url for IPv4 or IPv6 address, if failed, perform DNS resolution.\r
47f51a06 503 //\r
b659408b
ZL
504 if (!HttpInstance->LocalAddressIsIPv6) {\r
505 Status = NetLibAsciiStrToIp4 (HostName, &HttpInstance->RemoteAddr);\r
506 } else {\r
ac458853 507 Status = HttpUrlGetIp6 (Url, UrlParser, &HttpInstance->RemoteIpv6Addr);\r
b659408b
ZL
508 }\r
509\r
47f51a06 510 if (EFI_ERROR (Status)) {\r
b9679cd7
SZ
511 HostNameSize = AsciiStrSize (HostName);\r
512 HostNameStr = AllocateZeroPool (HostNameSize * sizeof (CHAR16));\r
47f51a06
YT
513 if (HostNameStr == NULL) {\r
514 Status = EFI_OUT_OF_RESOURCES;\r
515 goto Error1;\r
516 }\r
f75a7f56 517\r
b9679cd7 518 AsciiStrToUnicodeStrS (HostName, HostNameStr, HostNameSize);\r
b659408b
ZL
519 if (!HttpInstance->LocalAddressIsIPv6) {\r
520 Status = HttpDns4 (HttpInstance, HostNameStr, &HttpInstance->RemoteAddr);\r
521 } else {\r
522 Status = HttpDns6 (HttpInstance, HostNameStr, &HttpInstance->RemoteIpv6Addr);\r
523 }\r
f75a7f56 524\r
47f51a06
YT
525 FreePool (HostNameStr);\r
526 if (EFI_ERROR (Status)) {\r
fca04738 527 DEBUG ((EFI_D_ERROR, "Error: Could not retrieve the host address from DNS server.\n"));\r
47f51a06
YT
528 goto Error1;\r
529 }\r
530 }\r
531\r
532 //\r
533 // Save the RemotePort and RemoteHost.\r
534 //\r
535 ASSERT (HttpInstance->RemoteHost == NULL);\r
536 HttpInstance->RemotePort = RemotePort;\r
537 HttpInstance->RemoteHost = HostName;\r
538 HostName = NULL;\r
539 }\r
540\r
541 if (ReConfigure) {\r
542 //\r
543 // The request URL is different from previous calls to Request(), close existing TCP instance.\r
544 //\r
a2e61982
ZL
545 if (!HttpInstance->LocalAddressIsIPv6) {\r
546 ASSERT (HttpInstance->Tcp4 != NULL);\r
547 } else {\r
548 ASSERT (HttpInstance->Tcp6 != NULL);\r
549 }\r
dac45de3
JW
550\r
551 if (HttpInstance->UseHttps && !TlsConfigure) {\r
552 Status = TlsCloseSession (HttpInstance);\r
553 if (EFI_ERROR (Status)) {\r
554 goto Error1;\r
555 }\r
f75a7f56 556\r
dac45de3
JW
557 TlsCloseTxRxEvent (HttpInstance);\r
558 }\r
f75a7f56 559\r
47f51a06
YT
560 HttpCloseConnection (HttpInstance);\r
561 EfiHttpCancel (This, NULL);\r
562 }\r
563\r
564 //\r
565 // Wrap the HTTP token in HTTP_TOKEN_WRAP\r
566 //\r
567 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r
568 if (Wrap == NULL) {\r
569 Status = EFI_OUT_OF_RESOURCES;\r
570 goto Error1;\r
571 }\r
572\r
573 Wrap->HttpToken = Token;\r
574 Wrap->HttpInstance = HttpInstance;\r
d8293d31
NH
575 if (Request != NULL) {\r
576 Wrap->TcpWrap.Method = Request->Method;\r
577 }\r
f75a7f56 578\r
dac45de3 579 Status = HttpInitSession (\r
f75a7f56
LG
580 HttpInstance,\r
581 Wrap,\r
582 Configure || ReConfigure,\r
dac45de3
JW
583 TlsConfigure\r
584 );\r
a2e61982
ZL
585 if (EFI_ERROR (Status)) {\r
586 goto Error2;\r
dac45de3 587 }\r
b659408b 588\r
dac45de3 589 if (!Configure && !ReConfigure && !TlsConfigure) {\r
47f51a06 590 //\r
f75a7f56 591 // For the new HTTP token, create TX TCP token events.\r
47f51a06 592 //\r
b659408b 593 Status = HttpCreateTcpTxEvent (Wrap);\r
47f51a06
YT
594 if (EFI_ERROR (Status)) {\r
595 goto Error1;\r
596 }\r
597 }\r
f75a7f56 598\r
47f51a06
YT
599 //\r
600 // Create request message.\r
601 //\r
b199d941 602 FileUrl = Url;\r
d8293d31 603 if (Url != NULL && *FileUrl != '/') {\r
b199d941
GCPL
604 //\r
605 // Convert the absolute-URI to the absolute-path\r
606 //\r
607 while (*FileUrl != ':') {\r
608 FileUrl++;\r
609 }\r
610 if ((*(FileUrl+1) == '/') && (*(FileUrl+2) == '/')) {\r
611 FileUrl += 3;\r
612 while (*FileUrl != '/') {\r
613 FileUrl++;\r
614 }\r
615 } else {\r
616 Status = EFI_INVALID_PARAMETER;\r
617 goto Error3;\r
618 }\r
619 }\r
f58554fc 620\r
19c25725 621 Status = HttpGenRequestMessage (HttpMsg, FileUrl, &RequestMsg, &RequestMsgSize);\r
f58554fc 622\r
63f1d6a4 623 if (EFI_ERROR (Status) || NULL == RequestMsg) {\r
47f51a06
YT
624 goto Error3;\r
625 }\r
626\r
d8293d31
NH
627 //\r
628 // Every request we insert a TxToken and a response call would remove the TxToken.\r
97c567ef 629 // In cases of PUT/POST/PATCH, after an initial request-response pair, we would do a\r
d8293d31
NH
630 // continuous request without a response call. So, in such cases, where Request\r
631 // structure is NULL, we would not insert a TxToken.\r
632 //\r
633 if (Request != NULL) {\r
634 Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap);\r
635 if (EFI_ERROR (Status)) {\r
636 goto Error4;\r
637 }\r
47f51a06
YT
638 }\r
639\r
47f51a06
YT
640 //\r
641 // Transmit the request message.\r
642 //\r
b659408b 643 Status = HttpTransmitTcp (\r
47f51a06
YT
644 HttpInstance,\r
645 Wrap,\r
19c25725
NH
646 (UINT8*) RequestMsg,\r
647 RequestMsgSize\r
47f51a06
YT
648 );\r
649 if (EFI_ERROR (Status)) {\r
f75a7f56 650 goto Error5;\r
47f51a06
YT
651 }\r
652\r
49c9f74c 653 DispatchDpc ();\r
f75a7f56 654\r
cdf8c32e
NH
655 if (HostName != NULL) {\r
656 FreePool (HostName);\r
657 }\r
c0a0a5a5
JW
658\r
659 if (UrlParser != NULL) {\r
660 HttpUrlFreeParser (UrlParser);\r
661 }\r
f75a7f56 662\r
47f51a06
YT
663 return EFI_SUCCESS;\r
664\r
665Error5:\r
30526a51
JW
666 //\r
667 // We would have inserted a TxToken only if Request structure is not NULL.\r
668 // Hence check before we do a remove in this error case.\r
669 //\r
670 if (Request != NULL) {\r
671 NetMapRemoveTail (&HttpInstance->TxTokens, NULL);\r
672 }\r
47f51a06
YT
673\r
674Error4:\r
19c25725
NH
675 if (RequestMsg != NULL) {\r
676 FreePool (RequestMsg);\r
f75a7f56 677 }\r
47f51a06
YT
678\r
679Error3:\r
dac45de3
JW
680 if (HttpInstance->UseHttps) {\r
681 TlsCloseSession (HttpInstance);\r
682 TlsCloseTxRxEvent (HttpInstance);\r
683 }\r
47f51a06 684\r
47f51a06 685Error2:\r
dac45de3 686 HttpCloseConnection (HttpInstance);\r
f75a7f56 687\r
b659408b
ZL
688 HttpCloseTcpConnCloseEvent (HttpInstance);\r
689 if (NULL != Wrap->TcpWrap.Tx4Token.CompletionToken.Event) {\r
690 gBS->CloseEvent (Wrap->TcpWrap.Tx4Token.CompletionToken.Event);\r
691 Wrap->TcpWrap.Tx4Token.CompletionToken.Event = NULL;\r
692 }\r
693 if (NULL != Wrap->TcpWrap.Tx6Token.CompletionToken.Event) {\r
694 gBS->CloseEvent (Wrap->TcpWrap.Tx6Token.CompletionToken.Event);\r
695 Wrap->TcpWrap.Tx6Token.CompletionToken.Event = NULL;\r
47f51a06
YT
696 }\r
697\r
698Error1:\r
47f51a06
YT
699 if (HostName != NULL) {\r
700 FreePool (HostName);\r
701 }\r
702 if (Wrap != NULL) {\r
703 FreePool (Wrap);\r
704 }\r
c0a0a5a5 705 if (UrlParser != NULL) {\r
47f51a06
YT
706 HttpUrlFreeParser (UrlParser);\r
707 }\r
708\r
709 return Status;\r
f75a7f56 710\r
47f51a06
YT
711}\r
712\r
713/**\r
f75a7f56
LG
714 Cancel a user's Token.\r
715\r
47f51a06
YT
716 @param[in] Map The HTTP instance's token queue.\r
717 @param[in] Item Object container for one HTTP token and token's wrap.\r
718 @param[in] Context The user's token to cancel.\r
719\r
720 @retval EFI_SUCCESS Continue to check the next Item.\r
721 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.\r
722\r
723**/\r
724EFI_STATUS\r
725EFIAPI\r
726HttpCancelTokens (\r
727 IN NET_MAP *Map,\r
728 IN NET_MAP_ITEM *Item,\r
729 IN VOID *Context\r
730 )\r
731{\r
47f51a06
YT
732 EFI_HTTP_TOKEN *Token;\r
733 HTTP_TOKEN_WRAP *Wrap;\r
b659408b 734 HTTP_PROTOCOL *HttpInstance;\r
47f51a06
YT
735\r
736 Token = (EFI_HTTP_TOKEN *) Context;\r
737\r
738 //\r
739 // Return EFI_SUCCESS to check the next item in the map if\r
740 // this one doesn't match.\r
741 //\r
742 if ((Token != NULL) && (Token != Item->Key)) {\r
743 return EFI_SUCCESS;\r
744 }\r
745\r
746 Wrap = (HTTP_TOKEN_WRAP *) Item->Value;\r
747 ASSERT (Wrap != NULL);\r
b659408b 748 HttpInstance = Wrap->HttpInstance;\r
f75a7f56 749\r
b659408b 750 if (!HttpInstance->LocalAddressIsIPv6) {\r
b659408b 751 if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) {\r
30526a51
JW
752 //\r
753 // Cancle the Token before close its Event.\r
754 //\r
755 HttpInstance->Tcp4->Cancel (HttpInstance->Tcp4, &Wrap->TcpWrap.Rx4Token.CompletionToken);\r
47f51a06 756\r
30526a51
JW
757 //\r
758 // Dispatch the DPC queued by the NotifyFunction of the canceled token's events.\r
759 //\r
760 DispatchDpc ();\r
b659408b 761 }\r
30526a51 762 } else {\r
b659408b 763 if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {\r
30526a51
JW
764 //\r
765 // Cancle the Token before close its Event.\r
766 //\r
767 HttpInstance->Tcp6->Cancel (HttpInstance->Tcp6, &Wrap->TcpWrap.Rx6Token.CompletionToken);\r
47f51a06 768\r
30526a51
JW
769 //\r
770 // Dispatch the DPC queued by the NotifyFunction of the canceled token's events.\r
771 //\r
772 DispatchDpc ();\r
b659408b 773 }\r
47f51a06
YT
774 }\r
775\r
47f51a06
YT
776 //\r
777 // If only one item is to be cancel, return EFI_ABORTED to stop\r
778 // iterating the map any more.\r
779 //\r
780 if (Token != NULL) {\r
781 return EFI_ABORTED;\r
782 }\r
783\r
f75a7f56 784 return EFI_SUCCESS;\r
47f51a06
YT
785}\r
786\r
787/**\r
788 Cancel the user's receive/transmit request. It is the worker function of\r
789 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the\r
790 token.\r
791\r
792 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.\r
793 @param[in] Token The token to cancel. If NULL, all token will be\r
794 cancelled.\r
795\r
796 @retval EFI_SUCCESS The token is cancelled.\r
f75a7f56 797 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.\r
47f51a06
YT
798 @retval Others Other error as indicated.\r
799\r
800**/\r
801EFI_STATUS\r
802HttpCancel (\r
803 IN HTTP_PROTOCOL *HttpInstance,\r
804 IN EFI_HTTP_TOKEN *Token\r
805 )\r
806{\r
807 EFI_STATUS Status;\r
808\r
809 //\r
810 // First check the tokens queued by EfiHttpRequest().\r
811 //\r
812 Status = NetMapIterate (&HttpInstance->TxTokens, HttpCancelTokens, Token);\r
813 if (EFI_ERROR (Status)) {\r
814 if (Token != NULL) {\r
815 if (Status == EFI_ABORTED) {\r
816 return EFI_SUCCESS;\r
f75a7f56 817 }\r
47f51a06
YT
818 } else {\r
819 return Status;\r
820 }\r
821 }\r
822\r
dac45de3
JW
823 if (!HttpInstance->UseHttps) {\r
824 //\r
825 // Then check the tokens queued by EfiHttpResponse(), except for Https.\r
826 //\r
827 Status = NetMapIterate (&HttpInstance->RxTokens, HttpCancelTokens, Token);\r
828 if (EFI_ERROR (Status)) {\r
829 if (Token != NULL) {\r
830 if (Status == EFI_ABORTED) {\r
831 return EFI_SUCCESS;\r
832 } else {\r
833 return EFI_NOT_FOUND;\r
834 }\r
47f51a06 835 } else {\r
dac45de3 836 return Status;\r
47f51a06 837 }\r
dac45de3
JW
838 }\r
839 } else {\r
840 if (!HttpInstance->LocalAddressIsIPv6) {\r
841 HttpInstance->Tcp4->Cancel (HttpInstance->Tcp4, &HttpInstance->Tcp4TlsRxToken.CompletionToken);\r
47f51a06 842 } else {\r
dac45de3 843 HttpInstance->Tcp6->Cancel (HttpInstance->Tcp6, &HttpInstance->Tcp6TlsRxToken.CompletionToken);\r
47f51a06
YT
844 }\r
845 }\r
f75a7f56 846\r
47f51a06
YT
847 return EFI_SUCCESS;\r
848}\r
849\r
850\r
851/**\r
852 Abort an asynchronous HTTP request or response token.\r
853\r
854 The Cancel() function aborts a pending HTTP request or response transaction. If\r
855 Token is not NULL and the token is in transmit or receive queues when it is being\r
856 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will\r
857 be signaled. If the token is not in one of the queues, which usually means that the\r
858 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,\r
859 all asynchronous tokens issued by Request() or Response() will be aborted.\r
860\r
861 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
862 @param[in] Token Point to storage containing HTTP request or response\r
863 token.\r
864\r
865 @retval EFI_SUCCESS Request and Response queues are successfully flushed.\r
866 @retval EFI_INVALID_PARAMETER This is NULL.\r
867 @retval EFI_NOT_STARTED This instance hasn't been configured.\r
47f51a06
YT
868 @retval EFI_NOT_FOUND The asynchronous request or response token is not\r
869 found.\r
870 @retval EFI_UNSUPPORTED The implementation does not support this function.\r
871\r
872**/\r
873EFI_STATUS\r
874EFIAPI\r
875EfiHttpCancel (\r
876 IN EFI_HTTP_PROTOCOL *This,\r
877 IN EFI_HTTP_TOKEN *Token\r
878 )\r
879{\r
880 HTTP_PROTOCOL *HttpInstance;\r
881\r
882 if (This == NULL) {\r
883 return EFI_INVALID_PARAMETER;\r
884 }\r
885\r
886 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
47f51a06
YT
887\r
888 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r
889 return EFI_NOT_STARTED;\r
890 }\r
891\r
892 return HttpCancel (HttpInstance, Token);\r
893\r
894}\r
895\r
896/**\r
897 A callback function to intercept events during message parser.\r
898\r
899 This function will be invoked during HttpParseMessageBody() with various events type. An error\r
900 return status of the callback function will cause the HttpParseMessageBody() aborted.\r
901\r
902 @param[in] EventType Event type of this callback call.\r
903 @param[in] Data A pointer to data buffer.\r
904 @param[in] Length Length in bytes of the Data.\r
905 @param[in] Context Callback context set by HttpInitMsgParser().\r
906\r
907 @retval EFI_SUCCESS Continue to parser the message body.\r
908\r
909**/\r
910EFI_STATUS\r
911EFIAPI\r
912HttpBodyParserCallback (\r
913 IN HTTP_BODY_PARSE_EVENT EventType,\r
914 IN CHAR8 *Data,\r
915 IN UINTN Length,\r
916 IN VOID *Context\r
917 )\r
918{\r
919 HTTP_TOKEN_WRAP *Wrap;\r
5ba9f065
ZL
920 UINTN BodyLength;\r
921 CHAR8 *Body;\r
47f51a06
YT
922\r
923 if (EventType != BodyParseEventOnComplete) {\r
924 return EFI_SUCCESS;\r
925 }\r
926\r
927 if (Data == NULL || Length != 0 || Context == NULL) {\r
928 return EFI_SUCCESS;\r
929 }\r
930\r
931 Wrap = (HTTP_TOKEN_WRAP *) Context;\r
5ba9f065
ZL
932 Body = Wrap->HttpToken->Message->Body;\r
933 BodyLength = Wrap->HttpToken->Message->BodyLength;\r
934 if (Data < Body + BodyLength) {\r
935 Wrap->HttpInstance->NextMsg = Data;\r
936 } else {\r
937 Wrap->HttpInstance->NextMsg = NULL;\r
938 }\r
f75a7f56 939\r
47f51a06
YT
940\r
941 //\r
b659408b 942 // Free Tx4Token or Tx6Token since already received corrsponding HTTP response.\r
47f51a06
YT
943 //\r
944 FreePool (Wrap);\r
945\r
946 return EFI_SUCCESS;\r
947}\r
948\r
949/**\r
950 The work function of EfiHttpResponse().\r
951\r
952 @param[in] Wrap Pointer to HTTP token's wrap data.\r
953\r
954 @retval EFI_SUCCESS Allocation succeeded.\r
955 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.\r
f75a7f56 956 @retval EFI_NOT_READY Can't find a corresponding Tx4Token/Tx6Token or\r
5ca29abe 957 the EFI_HTTP_UTILITIES_PROTOCOL is not available.\r
47f51a06
YT
958\r
959**/\r
960EFI_STATUS\r
961HttpResponseWorker (\r
962 IN HTTP_TOKEN_WRAP *Wrap\r
963 )\r
964{\r
965 EFI_STATUS Status;\r
966 EFI_HTTP_MESSAGE *HttpMsg;\r
47f51a06
YT
967 CHAR8 *EndofHeader;\r
968 CHAR8 *HttpHeaders;\r
969 UINTN SizeofHeaders;\r
47f51a06
YT
970 UINTN BufferSize;\r
971 UINTN StatusCode;\r
972 CHAR8 *Tmp;\r
973 CHAR8 *HeaderTmp;\r
974 CHAR8 *StatusCodeStr;\r
975 UINTN BodyLen;\r
976 HTTP_PROTOCOL *HttpInstance;\r
977 EFI_HTTP_TOKEN *Token;\r
978 NET_MAP_ITEM *Item;\r
979 HTTP_TOKEN_WRAP *ValueInItem;\r
980 UINTN HdrLen;\r
dac45de3 981 NET_FRAGMENT Fragment;\r
47f51a06 982\r
3fd7bd08 983 if (Wrap == NULL || Wrap->HttpInstance == NULL) {\r
984 return EFI_INVALID_PARAMETER;\r
985 }\r
f75a7f56 986\r
47f51a06
YT
987 HttpInstance = Wrap->HttpInstance;\r
988 Token = Wrap->HttpToken;\r
47f51a06
YT
989 HttpMsg = Token->Message;\r
990\r
b659408b
ZL
991 HttpInstance->EndofHeader = NULL;\r
992 HttpInstance->HttpHeaders = NULL;\r
993 HttpMsg->Headers = NULL;\r
994 HttpHeaders = NULL;\r
995 SizeofHeaders = 0;\r
996 BufferSize = 0;\r
997 EndofHeader = NULL;\r
6be1193f 998 ValueInItem = NULL;\r
dac45de3
JW
999 Fragment.Len = 0;\r
1000 Fragment.Bulk = NULL;\r
f75a7f56 1001\r
47f51a06 1002 if (HttpMsg->Data.Response != NULL) {\r
47f51a06
YT
1003 //\r
1004 // Check whether we have cached header from previous call.\r
1005 //\r
1006 if ((HttpInstance->CacheBody != NULL) && (HttpInstance->NextMsg != NULL)) {\r
1007 //\r
1008 // The data is stored at [NextMsg, CacheBody + CacheLen].\r
1009 //\r
1010 HdrLen = HttpInstance->CacheBody + HttpInstance->CacheLen - HttpInstance->NextMsg;\r
1011 HttpHeaders = AllocateZeroPool (HdrLen);\r
1012 if (HttpHeaders == NULL) {\r
1013 Status = EFI_OUT_OF_RESOURCES;\r
1014 goto Error;\r
1015 }\r
1016\r
1017 CopyMem (HttpHeaders, HttpInstance->NextMsg, HdrLen);\r
1018 FreePool (HttpInstance->CacheBody);\r
1019 HttpInstance->CacheBody = NULL;\r
1020 HttpInstance->NextMsg = NULL;\r
1021 HttpInstance->CacheOffset = 0;\r
1022 SizeofHeaders = HdrLen;\r
1023 BufferSize = HttpInstance->CacheLen;\r
1024\r
1025 //\r
1026 // Check whether we cached the whole HTTP headers.\r
1027 //\r
f75a7f56
LG
1028 EndofHeader = AsciiStrStr (HttpHeaders, HTTP_END_OF_HDR_STR);\r
1029 }\r
47f51a06 1030\r
b659408b
ZL
1031 HttpInstance->EndofHeader = &EndofHeader;\r
1032 HttpInstance->HttpHeaders = &HttpHeaders;\r
47f51a06 1033\r
b347a22a
JW
1034\r
1035 if (HttpInstance->TimeoutEvent == NULL) {\r
1036 //\r
1037 // Create TimeoutEvent for response\r
1038 //\r
1039 Status = gBS->CreateEvent (\r
1040 EVT_TIMER,\r
1041 TPL_CALLBACK,\r
1042 NULL,\r
1043 NULL,\r
1044 &HttpInstance->TimeoutEvent\r
1045 );\r
1046 if (EFI_ERROR (Status)) {\r
1047 goto Error;\r
1048 }\r
1049 }\r
1050\r
1051 //\r
1052 // Start the timer, and wait Timeout seconds to receive the header packet.\r
1053 //\r
1054 Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_RESPONSE_TIMEOUT * TICKS_PER_SECOND);\r
1055 if (EFI_ERROR (Status)) {\r
1056 goto Error;\r
1057 }\r
1058\r
1059 Status = HttpTcpReceiveHeader (HttpInstance, &SizeofHeaders, &BufferSize, HttpInstance->TimeoutEvent);\r
1060\r
1061 gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0);\r
1062\r
b659408b
ZL
1063 if (EFI_ERROR (Status)) {\r
1064 goto Error;\r
1065 }\r
47f51a06 1066\r
1b96428d
ZL
1067 ASSERT (HttpHeaders != NULL);\r
1068\r
47f51a06
YT
1069 //\r
1070 // Cache the part of body.\r
1071 //\r
1072 BodyLen = BufferSize - (EndofHeader - HttpHeaders);\r
1073 if (BodyLen > 0) {\r
1074 if (HttpInstance->CacheBody != NULL) {\r
1075 FreePool (HttpInstance->CacheBody);\r
1076 }\r
1077\r
1078 HttpInstance->CacheBody = AllocateZeroPool (BodyLen);\r
1079 if (HttpInstance->CacheBody == NULL) {\r
1080 Status = EFI_OUT_OF_RESOURCES;\r
1081 goto Error;\r
1082 }\r
1083\r
1084 CopyMem (HttpInstance->CacheBody, EndofHeader, BodyLen);\r
1085 HttpInstance->CacheLen = BodyLen;\r
1086 }\r
1087\r
47f51a06
YT
1088 //\r
1089 // Search for Status Code.\r
1090 //\r
1091 StatusCodeStr = HttpHeaders + AsciiStrLen (HTTP_VERSION_STR) + 1;\r
1092 if (StatusCodeStr == NULL) {\r
30526a51 1093 Status = EFI_NOT_READY;\r
47f51a06
YT
1094 goto Error;\r
1095 }\r
1096\r
1097 StatusCode = AsciiStrDecimalToUintn (StatusCodeStr);\r
1098\r
1099 //\r
1100 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".\r
1101 //\r
1102 Tmp = AsciiStrStr (HttpHeaders, HTTP_CRLF_STR);\r
1103 if (Tmp == NULL) {\r
30526a51 1104 Status = EFI_NOT_READY;\r
47f51a06
YT
1105 goto Error;\r
1106 }\r
1107\r
47f51a06 1108 //\r
d8293d31
NH
1109 // We could have response with just a HTTP message and no headers. For Example,\r
1110 // "100 Continue". In such cases, we would not want to unnecessarily call a Parse\r
1111 // method. A "\r\n" following Tmp string again would indicate an end. Compare and\r
1112 // set SizeofHeaders to 0.\r
47f51a06 1113 //\r
d8293d31
NH
1114 Tmp = Tmp + AsciiStrLen (HTTP_CRLF_STR);\r
1115 if (CompareMem (Tmp, HTTP_CRLF_STR, AsciiStrLen (HTTP_CRLF_STR)) == 0) {\r
1116 SizeofHeaders = 0;\r
1117 } else {\r
1118 SizeofHeaders = SizeofHeaders - (Tmp - HttpHeaders);\r
47f51a06
YT
1119 }\r
1120\r
47f51a06 1121 HttpMsg->Data.Response->StatusCode = HttpMappingToStatusCode (StatusCode);\r
072289f4 1122 HttpInstance->StatusCode = StatusCode;\r
d8293d31 1123\r
47f51a06
YT
1124 Status = EFI_NOT_READY;\r
1125 ValueInItem = NULL;\r
47f51a06
YT
1126\r
1127 //\r
97c567ef 1128 // In cases of PUT/POST/PATCH, after an initial request-response pair, we would do a\r
d8293d31
NH
1129 // continuous request without a response call. So, we would not do an insert of\r
1130 // TxToken. After we have sent the complete file, we will call a response to get\r
1131 // a final response from server. In such a case, we would not have any TxTokens.\r
1132 // Hence, check that case before doing a NetMapRemoveHead.\r
47f51a06 1133 //\r
d8293d31
NH
1134 if (!NetMapIsEmpty (&HttpInstance->TxTokens)) {\r
1135 NetMapRemoveHead (&HttpInstance->TxTokens, (VOID**) &ValueInItem);\r
1136 if (ValueInItem == NULL) {\r
1137 goto Error;\r
1138 }\r
47f51a06 1139\r
d8293d31
NH
1140 //\r
1141 // The first Tx Token not transmitted yet, insert back and return error.\r
1142 //\r
1143 if (!ValueInItem->TcpWrap.IsTxDone) {\r
1144 goto Error2;\r
1145 }\r
47f51a06
YT
1146 }\r
1147\r
d8293d31
NH
1148 if (SizeofHeaders != 0) {\r
1149 HeaderTmp = AllocateZeroPool (SizeofHeaders);\r
1150 if (HeaderTmp == NULL) {\r
30526a51 1151 Status = EFI_OUT_OF_RESOURCES;\r
5646819f 1152 goto Error2;\r
d8293d31
NH
1153 }\r
1154\r
1155 CopyMem (HeaderTmp, Tmp, SizeofHeaders);\r
1156 FreePool (HttpHeaders);\r
1157 HttpHeaders = HeaderTmp;\r
1158\r
1159 //\r
1160 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.\r
1161 //\r
1162 if (mHttpUtilities == NULL) {\r
1163 Status = EFI_NOT_READY;\r
5646819f 1164 goto Error2;\r
d8293d31
NH
1165 }\r
1166\r
1167 //\r
1168 // Parse the HTTP header into array of key/value pairs.\r
1169 //\r
1170 Status = mHttpUtilities->Parse (\r
1171 mHttpUtilities,\r
1172 HttpHeaders,\r
1173 SizeofHeaders,\r
1174 &HttpMsg->Headers,\r
1175 &HttpMsg->HeaderCount\r
1176 );\r
1177 if (EFI_ERROR (Status)) {\r
5646819f 1178 goto Error2;\r
d8293d31
NH
1179 }\r
1180\r
1181 FreePool (HttpHeaders);\r
1182 HttpHeaders = NULL;\r
1183\r
1184\r
1185 //\r
1186 // Init message-body parser by header information.\r
1187 //\r
1188 Status = HttpInitMsgParser (\r
1189 HttpInstance->Method,\r
1190 HttpMsg->Data.Response->StatusCode,\r
1191 HttpMsg->HeaderCount,\r
1192 HttpMsg->Headers,\r
1193 HttpBodyParserCallback,\r
1194 (VOID *) ValueInItem,\r
1195 &HttpInstance->MsgParser\r
1196 );\r
47f51a06
YT
1197 if (EFI_ERROR (Status)) {\r
1198 goto Error2;\r
1199 }\r
1200\r
d8293d31
NH
1201 //\r
1202 // Check whether we received a complete HTTP message.\r
1203 //\r
1204 if (HttpInstance->CacheBody != NULL) {\r
1205 Status = HttpParseMessageBody (HttpInstance->MsgParser, HttpInstance->CacheLen, HttpInstance->CacheBody);\r
1206 if (EFI_ERROR (Status)) {\r
1207 goto Error2;\r
1208 }\r
1209\r
1210 if (HttpIsMessageComplete (HttpInstance->MsgParser)) {\r
1211 //\r
1212 // Free the MsgParse since we already have a full HTTP message.\r
1213 //\r
1214 HttpFreeMsgParser (HttpInstance->MsgParser);\r
1215 HttpInstance->MsgParser = NULL;\r
1216 }\r
47f51a06
YT
1217 }\r
1218 }\r
1219\r
d8293d31 1220 if ((HttpMsg->Body == NULL) || (HttpMsg->BodyLength == 0)) {\r
47f51a06
YT
1221 Status = EFI_SUCCESS;\r
1222 goto Exit;\r
1223 }\r
d8293d31 1224 }\r
47f51a06
YT
1225\r
1226 //\r
1227 // Receive the response body.\r
1228 //\r
1229 BodyLen = 0;\r
1230\r
1231 //\r
1232 // First check whether we cached some data.\r
1233 //\r
1234 if (HttpInstance->CacheBody != NULL) {\r
1235 //\r
1236 // Calculate the length of the cached data.\r
1237 //\r
1238 if (HttpInstance->NextMsg != NULL) {\r
1239 //\r
1240 // We have a cached HTTP message which includes a part of HTTP header of next message.\r
1241 //\r
f75a7f56 1242 BodyLen = HttpInstance->NextMsg - (HttpInstance->CacheBody + HttpInstance->CacheOffset);\r
47f51a06
YT
1243 } else {\r
1244 BodyLen = HttpInstance->CacheLen - HttpInstance->CacheOffset;\r
1245 }\r
1246\r
1247 if (BodyLen > 0) {\r
1248 //\r
1249 // We have some cached data. Just copy the data and return.\r
1250 //\r
1251 if (HttpMsg->BodyLength < BodyLen) {\r
1252 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, HttpMsg->BodyLength);\r
1253 HttpInstance->CacheOffset = HttpInstance->CacheOffset + HttpMsg->BodyLength;\r
1254 } else {\r
1255 //\r
1256 // Copy all cached data out.\r
1257 //\r
1258 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, BodyLen);\r
1259 HttpInstance->CacheOffset = BodyLen + HttpInstance->CacheOffset;\r
1260 HttpMsg->BodyLength = BodyLen;\r
1261\r
1262 if (HttpInstance->NextMsg == NULL) {\r
1263 //\r
1264 // There is no HTTP header of next message. Just free the cache buffer.\r
1265 //\r
1266 FreePool (HttpInstance->CacheBody);\r
1267 HttpInstance->CacheBody = NULL;\r
1268 HttpInstance->NextMsg = NULL;\r
1269 HttpInstance->CacheOffset = 0;\r
1270 }\r
1271 }\r
1272 //\r
1273 // Return since we aready received required data.\r
1274 //\r
1275 Status = EFI_SUCCESS;\r
1276 goto Exit;\r
f75a7f56 1277 }\r
47f51a06
YT
1278\r
1279 if (BodyLen == 0 && HttpInstance->MsgParser == NULL) {\r
1280 //\r
1281 // We received a complete HTTP message, and we don't have more data to return to caller.\r
1282 //\r
1283 HttpMsg->BodyLength = 0;\r
1284 Status = EFI_SUCCESS;\r
f75a7f56
LG
1285 goto Exit;\r
1286 }\r
47f51a06
YT
1287 }\r
1288\r
1289 ASSERT (HttpInstance->MsgParser != NULL);\r
1290\r
1291 //\r
1292 // We still need receive more data when there is no cache data and MsgParser is not NULL;\r
1293 //\r
dac45de3
JW
1294 if (!HttpInstance->UseHttps) {\r
1295 Status = HttpTcpReceiveBody (Wrap, HttpMsg);\r
1296\r
1297 if (EFI_ERROR (Status)) {\r
1298 goto Error2;\r
1299 }\r
f75a7f56 1300\r
dac45de3
JW
1301 } else {\r
1302 if (HttpInstance->TimeoutEvent == NULL) {\r
1303 //\r
1304 // Create TimeoutEvent for response\r
1305 //\r
1306 Status = gBS->CreateEvent (\r
1307 EVT_TIMER,\r
1308 TPL_CALLBACK,\r
1309 NULL,\r
1310 NULL,\r
1311 &HttpInstance->TimeoutEvent\r
1312 );\r
1313 if (EFI_ERROR (Status)) {\r
1314 goto Error2;\r
1315 }\r
1316 }\r
1317\r
1318 //\r
1319 // Start the timer, and wait Timeout seconds to receive the body packet.\r
1320 //\r
1321 Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_RESPONSE_TIMEOUT * TICKS_PER_SECOND);\r
1322 if (EFI_ERROR (Status)) {\r
1323 goto Error2;\r
1324 }\r
f75a7f56 1325\r
dac45de3
JW
1326 Status = HttpsReceive (HttpInstance, &Fragment, HttpInstance->TimeoutEvent);\r
1327\r
1328 gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0);\r
f75a7f56 1329\r
dac45de3
JW
1330 if (EFI_ERROR (Status)) {\r
1331 goto Error2;\r
1332 }\r
1333\r
1334 //\r
1335 // Check whether we receive a complete HTTP message.\r
1336 //\r
1337 Status = HttpParseMessageBody (\r
1338 HttpInstance->MsgParser,\r
1339 (UINTN) Fragment.Len,\r
1340 (CHAR8 *) Fragment.Bulk\r
1341 );\r
1342 if (EFI_ERROR (Status)) {\r
1343 goto Error2;\r
1344 }\r
1345\r
1346 if (HttpIsMessageComplete (HttpInstance->MsgParser)) {\r
1347 //\r
1348 // Free the MsgParse since we already have a full HTTP message.\r
1349 //\r
1350 HttpFreeMsgParser (HttpInstance->MsgParser);\r
1351 HttpInstance->MsgParser = NULL;\r
1352 }\r
1353\r
1354 //\r
1355 // We receive part of header of next HTTP msg.\r
1356 //\r
1357 if (HttpInstance->NextMsg != NULL) {\r
cb7dbc13 1358 HttpMsg->BodyLength = MIN ((UINTN) HttpInstance->NextMsg - (UINTN) Fragment.Bulk, HttpMsg->BodyLength);\r
dac45de3 1359 CopyMem (HttpMsg->Body, Fragment.Bulk, HttpMsg->BodyLength);\r
f75a7f56 1360\r
dac45de3
JW
1361 HttpInstance->CacheLen = Fragment.Len - HttpMsg->BodyLength;\r
1362 if (HttpInstance->CacheLen != 0) {\r
1363 if (HttpInstance->CacheBody != NULL) {\r
1364 FreePool (HttpInstance->CacheBody);\r
1365 }\r
f75a7f56 1366\r
dac45de3
JW
1367 HttpInstance->CacheBody = AllocateZeroPool (HttpInstance->CacheLen);\r
1368 if (HttpInstance->CacheBody == NULL) {\r
1369 Status = EFI_OUT_OF_RESOURCES;\r
1370 goto Error2;\r
1371 }\r
f75a7f56 1372\r
dac45de3
JW
1373 CopyMem (HttpInstance->CacheBody, Fragment.Bulk + HttpMsg->BodyLength, HttpInstance->CacheLen);\r
1374 HttpInstance->CacheOffset = 0;\r
1375\r
cb7dbc13 1376 HttpInstance->NextMsg = HttpInstance->CacheBody + ((UINTN) HttpInstance->NextMsg - (UINTN) (Fragment.Bulk + HttpMsg->BodyLength));\r
dac45de3
JW
1377 }\r
1378 } else {\r
1379 HttpMsg->BodyLength = MIN (Fragment.Len, (UINT32) HttpMsg->BodyLength);\r
1380 CopyMem (HttpMsg->Body, Fragment.Bulk, HttpMsg->BodyLength);\r
1381 HttpInstance->CacheLen = Fragment.Len - HttpMsg->BodyLength;\r
1382 if (HttpInstance->CacheLen != 0) {\r
1383 if (HttpInstance->CacheBody != NULL) {\r
1384 FreePool (HttpInstance->CacheBody);\r
1385 }\r
f75a7f56 1386\r
dac45de3
JW
1387 HttpInstance->CacheBody = AllocateZeroPool (HttpInstance->CacheLen);\r
1388 if (HttpInstance->CacheBody == NULL) {\r
1389 Status = EFI_OUT_OF_RESOURCES;\r
1390 goto Error2;\r
1391 }\r
1392\r
1393 CopyMem (HttpInstance->CacheBody, Fragment.Bulk + HttpMsg->BodyLength, HttpInstance->CacheLen);\r
1394 HttpInstance->CacheOffset = 0;\r
1395 }\r
1396 }\r
1397\r
1398 if (Fragment.Bulk != NULL) {\r
1399 FreePool (Fragment.Bulk);\r
1400 Fragment.Bulk = NULL;\r
1401 }\r
1402\r
1403 goto Exit;\r
47f51a06
YT
1404 }\r
1405\r
1406 return Status;\r
1407\r
1408Exit:\r
1409 Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);\r
1410 if (Item != NULL) {\r
1411 NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);\r
1412 }\r
072289f4
ZL
1413\r
1414 if (HttpInstance->StatusCode >= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE) {\r
1415 Token->Status = EFI_HTTP_ERROR;\r
1416 } else {\r
1417 Token->Status = Status;\r
1418 }\r
1419\r
47f51a06 1420 gBS->SignalEvent (Token->Event);\r
b659408b 1421 HttpCloseTcpRxEvent (Wrap);\r
47f51a06
YT
1422 FreePool (Wrap);\r
1423 return Status;\r
1424\r
1425Error2:\r
5646819f
FS
1426 if (ValueInItem != NULL) {\r
1427 NetMapInsertHead (&HttpInstance->TxTokens, ValueInItem->HttpToken, ValueInItem);\r
1428 }\r
47f51a06
YT
1429\r
1430Error:\r
30526a51
JW
1431 Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);\r
1432 if (Item != NULL) {\r
1433 NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);\r
1434 }\r
dac45de3
JW
1435\r
1436 if (!HttpInstance->UseHttps) {\r
1437 HttpTcpTokenCleanup (Wrap);\r
1438 } else {\r
1439 FreePool (Wrap);\r
1440 }\r
f75a7f56 1441\r
47f51a06
YT
1442 if (HttpHeaders != NULL) {\r
1443 FreePool (HttpHeaders);\r
dac45de3
JW
1444 HttpHeaders = NULL;\r
1445 }\r
1446\r
1447 if (Fragment.Bulk != NULL) {\r
1448 FreePool (Fragment.Bulk);\r
1449 Fragment.Bulk = NULL;\r
47f51a06
YT
1450 }\r
1451\r
1452 if (HttpMsg->Headers != NULL) {\r
1453 FreePool (HttpMsg->Headers);\r
dac45de3 1454 HttpMsg->Headers = NULL;\r
47f51a06
YT
1455 }\r
1456\r
1457 if (HttpInstance->CacheBody != NULL) {\r
1458 FreePool (HttpInstance->CacheBody);\r
1459 HttpInstance->CacheBody = NULL;\r
1460 }\r
1461\r
072289f4
ZL
1462 if (HttpInstance->StatusCode >= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE) {\r
1463 Token->Status = EFI_HTTP_ERROR;\r
1464 } else {\r
1465 Token->Status = Status;\r
1466 }\r
1467\r
47f51a06
YT
1468 gBS->SignalEvent (Token->Event);\r
1469\r
f75a7f56 1470 return Status;\r
47f51a06
YT
1471\r
1472}\r
1473\r
1474\r
1475/**\r
1476 The Response() function queues an HTTP response to this HTTP instance, similar to\r
f0ab5a81 1477 Receive() function in the EFI TCP driver. When the HTTP response is received successfully,\r
47f51a06
YT
1478 or if there is an error, Status in token will be updated and Event will be signaled.\r
1479\r
1480 The HTTP driver will queue a receive token to the underlying TCP instance. When data\r
1481 is received in the underlying TCP instance, the data will be parsed and Token will\r
1482 be populated with the response data. If the data received from the remote host\r
1483 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting\r
1484 (asynchronously) for more data to be sent from the remote host before signaling\r
1485 Event in Token.\r
1486\r
1487 It is the responsibility of the caller to allocate a buffer for Body and specify the\r
1488 size in BodyLength. If the remote host provides a response that contains a content\r
1489 body, up to BodyLength bytes will be copied from the receive buffer into Body and\r
1490 BodyLength will be updated with the amount of bytes received and copied to Body. This\r
1491 allows the client to download a large file in chunks instead of into one contiguous\r
1492 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is\r
1493 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive\r
1494 token to underlying TCP instance. If data arrives in the receive buffer, up to\r
1495 BodyLength bytes of data will be copied to Body. The HTTP driver will then update\r
1496 BodyLength with the amount of bytes received and copied to Body.\r
1497\r
1498 If the HTTP driver does not have an open underlying TCP connection with the host\r
1499 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is\r
1500 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain\r
1501 an open TCP connection between client and host.\r
1502\r
1503 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
1504 @param[in] Token Pointer to storage containing HTTP response token.\r
1505\r
1506 @retval EFI_SUCCESS Allocation succeeded.\r
1507 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been\r
1508 initialized.\r
1509 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
1510 This is NULL.\r
1511 Token is NULL.\r
1512 Token->Message->Headers is NULL.\r
1513 Token->Message is NULL.\r
1514 Token->Message->Body is not NULL,\r
1515 Token->Message->BodyLength is non-zero, and\r
1516 Token->Message->Data is NULL, but a previous call to\r
1517 Response() has not been completed successfully.\r
1518 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.\r
1519 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host\r
1520 specified by response URL.\r
1521**/\r
1522EFI_STATUS\r
1523EFIAPI\r
1524EfiHttpResponse (\r
1525 IN EFI_HTTP_PROTOCOL *This,\r
1526 IN EFI_HTTP_TOKEN *Token\r
1527 )\r
1528{\r
1529 EFI_STATUS Status;\r
1530 EFI_HTTP_MESSAGE *HttpMsg;\r
1531 HTTP_PROTOCOL *HttpInstance;\r
1532 HTTP_TOKEN_WRAP *Wrap;\r
1533\r
1534 if ((This == NULL) || (Token == NULL)) {\r
1535 return EFI_INVALID_PARAMETER;\r
1536 }\r
1537\r
1538 HttpMsg = Token->Message;\r
1539 if (HttpMsg == NULL) {\r
1540 return EFI_INVALID_PARAMETER;\r
1541 }\r
f75a7f56 1542\r
47f51a06 1543 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
47f51a06
YT
1544\r
1545 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r
1546 return EFI_NOT_STARTED;\r
1547 }\r
1548\r
47f51a06
YT
1549 //\r
1550 // Check whether the token already existed.\r
1551 //\r
1552 if (EFI_ERROR (NetMapIterate (&HttpInstance->RxTokens, HttpTokenExist, Token))) {\r
f75a7f56 1553 return EFI_ACCESS_DENIED;\r
47f51a06
YT
1554 }\r
1555\r
1556 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r
1557 if (Wrap == NULL) {\r
1558 return EFI_OUT_OF_RESOURCES;\r
1559 }\r
1560\r
1561 Wrap->HttpInstance = HttpInstance;\r
1562 Wrap->HttpToken = Token;\r
1563\r
dac45de3 1564 //\r
f75a7f56
LG
1565 // Notes: For Https, receive token wrapped in HTTP_TOKEN_WRAP is not used to\r
1566 // receive the https response. A special TlsRxToken is used for receiving TLS\r
dac45de3
JW
1567 // related messages. It should be a blocking response.\r
1568 //\r
1569 if (!HttpInstance->UseHttps) {\r
1570 Status = HttpCreateTcpRxEvent (Wrap);\r
1571 if (EFI_ERROR (Status)) {\r
1572 goto Error;\r
1573 }\r
47f51a06
YT
1574 }\r
1575\r
1576 Status = NetMapInsertTail (&HttpInstance->RxTokens, Token, Wrap);\r
1577 if (EFI_ERROR (Status)) {\r
1578 goto Error;\r
1579 }\r
1580\r
1581 //\r
1582 // If already have pending RxTokens, return directly.\r
1583 //\r
1584 if (NetMapGetCount (&HttpInstance->RxTokens) > 1) {\r
1585 return EFI_SUCCESS;\r
1586 }\r
1587\r
1588 return HttpResponseWorker (Wrap);\r
1589\r
1590Error:\r
1591 if (Wrap != NULL) {\r
b659408b
ZL
1592 if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) {\r
1593 gBS->CloseEvent (Wrap->TcpWrap.Rx4Token.CompletionToken.Event);\r
1594 }\r
1595\r
1596 if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {\r
1597 gBS->CloseEvent (Wrap->TcpWrap.Rx6Token.CompletionToken.Event);\r
47f51a06
YT
1598 }\r
1599 FreePool (Wrap);\r
f75a7f56 1600 }\r
47f51a06 1601\r
f75a7f56 1602 return Status;\r
47f51a06
YT
1603}\r
1604\r
1605/**\r
1606 The Poll() function can be used by network drivers and applications to increase the\r
1607 rate that data packets are moved between the communication devices and the transmit\r
1608 and receive queues.\r
1609\r
1610 In some systems, the periodic timer event in the managed network driver may not poll\r
1611 the underlying communications device fast enough to transmit and/or receive all data\r
1612 packets without missing incoming packets or dropping outgoing packets. Drivers and\r
1613 applications that are experiencing packet loss should try calling the Poll() function\r
1614 more often.\r
1615\r
1616 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
1617\r
1618 @retval EFI_SUCCESS Incoming or outgoing data was processed.\r
1619 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
1620 @retval EFI_INVALID_PARAMETER This is NULL.\r
1621 @retval EFI_NOT_READY No incoming or outgoing data is processed.\r
1622 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.\r
1623\r
1624**/\r
1625EFI_STATUS\r
1626EFIAPI\r
1627EfiHttpPoll (\r
1628 IN EFI_HTTP_PROTOCOL *This\r
1629 )\r
1630{\r
49c9f74c 1631 EFI_STATUS Status;\r
b659408b 1632 HTTP_PROTOCOL *HttpInstance;\r
47f51a06
YT
1633\r
1634 if (This == NULL) {\r
1635 return EFI_INVALID_PARAMETER;\r
1636 }\r
1637\r
1638 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
47f51a06 1639\r
1b96428d 1640 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r
47f51a06
YT
1641 return EFI_NOT_STARTED;\r
1642 }\r
f75a7f56 1643\r
b659408b 1644 if (HttpInstance->LocalAddressIsIPv6) {\r
1b96428d
ZL
1645 if (HttpInstance->Tcp6 == NULL) {\r
1646 return EFI_NOT_STARTED;\r
1647 }\r
b659408b
ZL
1648 Status = HttpInstance->Tcp6->Poll (HttpInstance->Tcp6);\r
1649 } else {\r
1b96428d
ZL
1650 if (HttpInstance->Tcp4 == NULL) {\r
1651 return EFI_NOT_STARTED;\r
1652 }\r
b659408b
ZL
1653 Status = HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);\r
1654 }\r
f75a7f56 1655\r
49c9f74c 1656 DispatchDpc ();\r
f75a7f56 1657\r
49c9f74c 1658 return Status;\r
47f51a06 1659}\r