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