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