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