]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/HttpDxe/HttpImpl.c
NetworkPkg/HttpDxe: Detect 'Connection: close' header
[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
47f51a06
YT
1108 //\r
1109 // Search for Status Code.\r
1110 //\r
1111 StatusCodeStr = HttpHeaders + AsciiStrLen (HTTP_VERSION_STR) + 1;\r
1112 if (StatusCodeStr == NULL) {\r
30526a51 1113 Status = EFI_NOT_READY;\r
47f51a06
YT
1114 goto Error;\r
1115 }\r
1116\r
1117 StatusCode = AsciiStrDecimalToUintn (StatusCodeStr);\r
1118\r
1119 //\r
1120 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".\r
1121 //\r
1122 Tmp = AsciiStrStr (HttpHeaders, HTTP_CRLF_STR);\r
1123 if (Tmp == NULL) {\r
30526a51 1124 Status = EFI_NOT_READY;\r
47f51a06
YT
1125 goto Error;\r
1126 }\r
1127\r
47f51a06 1128 //\r
d8293d31
NH
1129 // We could have response with just a HTTP message and no headers. For Example,\r
1130 // "100 Continue". In such cases, we would not want to unnecessarily call a Parse\r
1131 // method. A "\r\n" following Tmp string again would indicate an end. Compare and\r
1132 // set SizeofHeaders to 0.\r
47f51a06 1133 //\r
d8293d31
NH
1134 Tmp = Tmp + AsciiStrLen (HTTP_CRLF_STR);\r
1135 if (CompareMem (Tmp, HTTP_CRLF_STR, AsciiStrLen (HTTP_CRLF_STR)) == 0) {\r
1136 SizeofHeaders = 0;\r
1137 } else {\r
1138 SizeofHeaders = SizeofHeaders - (Tmp - HttpHeaders);\r
47f51a06
YT
1139 }\r
1140\r
47f51a06 1141 HttpMsg->Data.Response->StatusCode = HttpMappingToStatusCode (StatusCode);\r
d1050b9d 1142 HttpInstance->StatusCode = StatusCode;\r
d8293d31 1143\r
d1050b9d 1144 Status = EFI_NOT_READY;\r
47f51a06 1145 ValueInItem = NULL;\r
47f51a06
YT
1146\r
1147 //\r
97c567ef 1148 // In cases of PUT/POST/PATCH, after an initial request-response pair, we would do a\r
d8293d31
NH
1149 // continuous request without a response call. So, we would not do an insert of\r
1150 // TxToken. After we have sent the complete file, we will call a response to get\r
1151 // a final response from server. In such a case, we would not have any TxTokens.\r
1152 // Hence, check that case before doing a NetMapRemoveHead.\r
47f51a06 1153 //\r
d8293d31 1154 if (!NetMapIsEmpty (&HttpInstance->TxTokens)) {\r
d1050b9d
MK
1155 NetMapRemoveHead (&HttpInstance->TxTokens, (VOID **)&ValueInItem);\r
1156 if (ValueInItem == NULL) {\r
d8293d31
NH
1157 goto Error;\r
1158 }\r
47f51a06 1159\r
d8293d31
NH
1160 //\r
1161 // The first Tx Token not transmitted yet, insert back and return error.\r
1162 //\r
1163 if (!ValueInItem->TcpWrap.IsTxDone) {\r
1164 goto Error2;\r
1165 }\r
47f51a06
YT
1166 }\r
1167\r
d8293d31
NH
1168 if (SizeofHeaders != 0) {\r
1169 HeaderTmp = AllocateZeroPool (SizeofHeaders);\r
1170 if (HeaderTmp == NULL) {\r
30526a51 1171 Status = EFI_OUT_OF_RESOURCES;\r
5646819f 1172 goto Error2;\r
d8293d31
NH
1173 }\r
1174\r
1175 CopyMem (HeaderTmp, Tmp, SizeofHeaders);\r
1176 FreePool (HttpHeaders);\r
1177 HttpHeaders = HeaderTmp;\r
1178\r
1179 //\r
1180 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.\r
1181 //\r
1182 if (mHttpUtilities == NULL) {\r
1183 Status = EFI_NOT_READY;\r
5646819f 1184 goto Error2;\r
d8293d31
NH
1185 }\r
1186\r
1187 //\r
1188 // Parse the HTTP header into array of key/value pairs.\r
1189 //\r
1190 Status = mHttpUtilities->Parse (\r
1191 mHttpUtilities,\r
1192 HttpHeaders,\r
1193 SizeofHeaders,\r
1194 &HttpMsg->Headers,\r
1195 &HttpMsg->HeaderCount\r
1196 );\r
1197 if (EFI_ERROR (Status)) {\r
5646819f 1198 goto Error2;\r
d8293d31
NH
1199 }\r
1200\r
1201 FreePool (HttpHeaders);\r
1202 HttpHeaders = NULL;\r
1203\r
753fd319
OS
1204 for (Index = 0; Index < HttpMsg->HeaderCount; ++Index) {\r
1205 if ((AsciiStriCmp ("Connection", HttpMsg->Headers[Index].FieldName) == 0) &&\r
1206 (AsciiStriCmp ("close", HttpMsg->Headers[Index].FieldValue) == 0))\r
1207 {\r
1208 DEBUG ((DEBUG_VERBOSE, "Http: 'Connection: close' header received.\n"));\r
1209 HttpInstance->ConnectionClose = TRUE;\r
1210 break;\r
1211 }\r
1212 }\r
1213\r
d8293d31
NH
1214 //\r
1215 // Init message-body parser by header information.\r
1216 //\r
1217 Status = HttpInitMsgParser (\r
1218 HttpInstance->Method,\r
1219 HttpMsg->Data.Response->StatusCode,\r
1220 HttpMsg->HeaderCount,\r
1221 HttpMsg->Headers,\r
1222 HttpBodyParserCallback,\r
d1050b9d 1223 (VOID *)(&HttpInstance->CallbackData),\r
d8293d31
NH
1224 &HttpInstance->MsgParser\r
1225 );\r
47f51a06
YT
1226 if (EFI_ERROR (Status)) {\r
1227 goto Error2;\r
1228 }\r
1229\r
d8293d31
NH
1230 //\r
1231 // Check whether we received a complete HTTP message.\r
1232 //\r
1233 if (HttpInstance->CacheBody != NULL) {\r
895b87e3
JW
1234 //\r
1235 // Record the CallbackData data.\r
1236 //\r
d1050b9d
MK
1237 HttpInstance->CallbackData.Wrap = (VOID *)Wrap;\r
1238 HttpInstance->CallbackData.ParseData = (VOID *)HttpInstance->CacheBody;\r
895b87e3
JW
1239 HttpInstance->CallbackData.ParseDataLength = HttpInstance->CacheLen;\r
1240\r
1241 //\r
1242 // Parse message with CallbackData data.\r
1243 //\r
d8293d31
NH
1244 Status = HttpParseMessageBody (HttpInstance->MsgParser, HttpInstance->CacheLen, HttpInstance->CacheBody);\r
1245 if (EFI_ERROR (Status)) {\r
1246 goto Error2;\r
1247 }\r
895b87e3 1248 }\r
d8293d31 1249\r
895b87e3
JW
1250 if (HttpIsMessageComplete (HttpInstance->MsgParser)) {\r
1251 //\r
1252 // Free the MsgParse since we already have a full HTTP message.\r
1253 //\r
1254 HttpFreeMsgParser (HttpInstance->MsgParser);\r
1255 HttpInstance->MsgParser = NULL;\r
47f51a06
YT
1256 }\r
1257 }\r
1258\r
d8293d31 1259 if ((HttpMsg->Body == NULL) || (HttpMsg->BodyLength == 0)) {\r
47f51a06
YT
1260 Status = EFI_SUCCESS;\r
1261 goto Exit;\r
1262 }\r
d8293d31 1263 }\r
47f51a06
YT
1264\r
1265 //\r
1266 // Receive the response body.\r
1267 //\r
1268 BodyLen = 0;\r
1269\r
1270 //\r
1271 // First check whether we cached some data.\r
1272 //\r
1273 if (HttpInstance->CacheBody != NULL) {\r
1274 //\r
1275 // Calculate the length of the cached data.\r
1276 //\r
1277 if (HttpInstance->NextMsg != NULL) {\r
1278 //\r
1279 // We have a cached HTTP message which includes a part of HTTP header of next message.\r
1280 //\r
f75a7f56 1281 BodyLen = HttpInstance->NextMsg - (HttpInstance->CacheBody + HttpInstance->CacheOffset);\r
47f51a06
YT
1282 } else {\r
1283 BodyLen = HttpInstance->CacheLen - HttpInstance->CacheOffset;\r
1284 }\r
1285\r
1286 if (BodyLen > 0) {\r
1287 //\r
1288 // We have some cached data. Just copy the data and return.\r
1289 //\r
1290 if (HttpMsg->BodyLength < BodyLen) {\r
1291 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, HttpMsg->BodyLength);\r
1292 HttpInstance->CacheOffset = HttpInstance->CacheOffset + HttpMsg->BodyLength;\r
1293 } else {\r
1294 //\r
1295 // Copy all cached data out.\r
1296 //\r
1297 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, BodyLen);\r
1298 HttpInstance->CacheOffset = BodyLen + HttpInstance->CacheOffset;\r
d1050b9d 1299 HttpMsg->BodyLength = BodyLen;\r
47f51a06
YT
1300\r
1301 if (HttpInstance->NextMsg == NULL) {\r
1302 //\r
1303 // There is no HTTP header of next message. Just free the cache buffer.\r
1304 //\r
1305 FreePool (HttpInstance->CacheBody);\r
1306 HttpInstance->CacheBody = NULL;\r
1307 HttpInstance->NextMsg = NULL;\r
1308 HttpInstance->CacheOffset = 0;\r
1309 }\r
1310 }\r
d1050b9d 1311\r
47f51a06 1312 //\r
ba3b642d 1313 // Return since we already received required data.\r
47f51a06
YT
1314 //\r
1315 Status = EFI_SUCCESS;\r
1316 goto Exit;\r
f75a7f56 1317 }\r
47f51a06 1318\r
d1050b9d 1319 if ((BodyLen == 0) && (HttpInstance->MsgParser == NULL)) {\r
47f51a06
YT
1320 //\r
1321 // We received a complete HTTP message, and we don't have more data to return to caller.\r
1322 //\r
1323 HttpMsg->BodyLength = 0;\r
d1050b9d 1324 Status = EFI_SUCCESS;\r
f75a7f56
LG
1325 goto Exit;\r
1326 }\r
47f51a06
YT
1327 }\r
1328\r
1329 ASSERT (HttpInstance->MsgParser != NULL);\r
1330\r
1331 //\r
1332 // We still need receive more data when there is no cache data and MsgParser is not NULL;\r
1333 //\r
dac45de3
JW
1334 if (!HttpInstance->UseHttps) {\r
1335 Status = HttpTcpReceiveBody (Wrap, HttpMsg);\r
1336\r
1337 if (EFI_ERROR (Status)) {\r
1338 goto Error2;\r
1339 }\r
dac45de3
JW
1340 } else {\r
1341 if (HttpInstance->TimeoutEvent == NULL) {\r
1342 //\r
1343 // Create TimeoutEvent for response\r
1344 //\r
1345 Status = gBS->CreateEvent (\r
1346 EVT_TIMER,\r
1347 TPL_CALLBACK,\r
1348 NULL,\r
1349 NULL,\r
1350 &HttpInstance->TimeoutEvent\r
1351 );\r
1352 if (EFI_ERROR (Status)) {\r
1353 goto Error2;\r
1354 }\r
1355 }\r
1356\r
ac70e71b
ZCW
1357 //\r
1358 // Get HTTP timeout value\r
1359 //\r
1360 TimeoutValue = PcdGet32 (PcdHttpIoTimeout);\r
1361\r
dac45de3
JW
1362 //\r
1363 // Start the timer, and wait Timeout seconds to receive the body packet.\r
1364 //\r
ac70e71b 1365 Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, TimeoutValue * TICKS_PER_MS);\r
dac45de3
JW
1366 if (EFI_ERROR (Status)) {\r
1367 goto Error2;\r
1368 }\r
f75a7f56 1369\r
dac45de3
JW
1370 Status = HttpsReceive (HttpInstance, &Fragment, HttpInstance->TimeoutEvent);\r
1371\r
1372 gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0);\r
f75a7f56 1373\r
dac45de3
JW
1374 if (EFI_ERROR (Status)) {\r
1375 goto Error2;\r
1376 }\r
1377\r
1378 //\r
895b87e3
JW
1379 // Process the received the body packet.\r
1380 //\r
d1050b9d 1381 HttpMsg->BodyLength = MIN ((UINTN)Fragment.Len, HttpMsg->BodyLength);\r
895b87e3
JW
1382\r
1383 CopyMem (HttpMsg->Body, Fragment.Bulk, HttpMsg->BodyLength);\r
1384\r
1385 //\r
1386 // Record the CallbackData data.\r
1387 //\r
d1050b9d
MK
1388 HttpInstance->CallbackData.Wrap = (VOID *)Wrap;\r
1389 HttpInstance->CallbackData.ParseData = HttpMsg->Body;\r
895b87e3
JW
1390 HttpInstance->CallbackData.ParseDataLength = HttpMsg->BodyLength;\r
1391\r
1392 //\r
1393 // Parse Body with CallbackData data.\r
dac45de3
JW
1394 //\r
1395 Status = HttpParseMessageBody (\r
1396 HttpInstance->MsgParser,\r
895b87e3
JW
1397 HttpMsg->BodyLength,\r
1398 HttpMsg->Body\r
dac45de3
JW
1399 );\r
1400 if (EFI_ERROR (Status)) {\r
1401 goto Error2;\r
1402 }\r
1403\r
1404 if (HttpIsMessageComplete (HttpInstance->MsgParser)) {\r
1405 //\r
1406 // Free the MsgParse since we already have a full HTTP message.\r
1407 //\r
1408 HttpFreeMsgParser (HttpInstance->MsgParser);\r
1409 HttpInstance->MsgParser = NULL;\r
1410 }\r
1411\r
1412 //\r
895b87e3 1413 // Check whether there is the next message header in the HttpMsg->Body.\r
dac45de3
JW
1414 //\r
1415 if (HttpInstance->NextMsg != NULL) {\r
d1050b9d 1416 HttpMsg->BodyLength = HttpInstance->NextMsg - (CHAR8 *)HttpMsg->Body;\r
895b87e3 1417 }\r
dac45de3 1418\r
895b87e3
JW
1419 HttpInstance->CacheLen = Fragment.Len - HttpMsg->BodyLength;\r
1420 if (HttpInstance->CacheLen != 0) {\r
1421 if (HttpInstance->CacheBody != NULL) {\r
1422 FreePool (HttpInstance->CacheBody);\r
dac45de3 1423 }\r
f75a7f56 1424\r
895b87e3
JW
1425 HttpInstance->CacheBody = AllocateZeroPool (HttpInstance->CacheLen);\r
1426 if (HttpInstance->CacheBody == NULL) {\r
1427 Status = EFI_OUT_OF_RESOURCES;\r
1428 goto Error2;\r
1429 }\r
dac45de3 1430\r
895b87e3
JW
1431 CopyMem (HttpInstance->CacheBody, Fragment.Bulk + HttpMsg->BodyLength, HttpInstance->CacheLen);\r
1432 HttpInstance->CacheOffset = 0;\r
1433 if (HttpInstance->NextMsg != NULL) {\r
1434 HttpInstance->NextMsg = HttpInstance->CacheBody;\r
dac45de3
JW
1435 }\r
1436 }\r
1437\r
1438 if (Fragment.Bulk != NULL) {\r
1439 FreePool (Fragment.Bulk);\r
1440 Fragment.Bulk = NULL;\r
1441 }\r
1442\r
1443 goto Exit;\r
47f51a06
YT
1444 }\r
1445\r
1446 return Status;\r
1447\r
1448Exit:\r
1449 Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);\r
1450 if (Item != NULL) {\r
1451 NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);\r
1452 }\r
072289f4
ZL
1453\r
1454 if (HttpInstance->StatusCode >= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE) {\r
1455 Token->Status = EFI_HTTP_ERROR;\r
1456 } else {\r
1457 Token->Status = Status;\r
1458 }\r
1459\r
47f51a06 1460 gBS->SignalEvent (Token->Event);\r
b659408b 1461 HttpCloseTcpRxEvent (Wrap);\r
47f51a06
YT
1462 FreePool (Wrap);\r
1463 return Status;\r
1464\r
1465Error2:\r
5646819f
FS
1466 if (ValueInItem != NULL) {\r
1467 NetMapInsertHead (&HttpInstance->TxTokens, ValueInItem->HttpToken, ValueInItem);\r
1468 }\r
47f51a06
YT
1469\r
1470Error:\r
30526a51
JW
1471 Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);\r
1472 if (Item != NULL) {\r
1473 NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);\r
1474 }\r
dac45de3
JW
1475\r
1476 if (!HttpInstance->UseHttps) {\r
1477 HttpTcpTokenCleanup (Wrap);\r
1478 } else {\r
1479 FreePool (Wrap);\r
1480 }\r
f75a7f56 1481\r
47f51a06
YT
1482 if (HttpHeaders != NULL) {\r
1483 FreePool (HttpHeaders);\r
dac45de3
JW
1484 HttpHeaders = NULL;\r
1485 }\r
1486\r
1487 if (Fragment.Bulk != NULL) {\r
1488 FreePool (Fragment.Bulk);\r
1489 Fragment.Bulk = NULL;\r
47f51a06
YT
1490 }\r
1491\r
1492 if (HttpMsg->Headers != NULL) {\r
1493 FreePool (HttpMsg->Headers);\r
dac45de3 1494 HttpMsg->Headers = NULL;\r
47f51a06
YT
1495 }\r
1496\r
1497 if (HttpInstance->CacheBody != NULL) {\r
1498 FreePool (HttpInstance->CacheBody);\r
1499 HttpInstance->CacheBody = NULL;\r
1500 }\r
1501\r
072289f4
ZL
1502 if (HttpInstance->StatusCode >= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE) {\r
1503 Token->Status = EFI_HTTP_ERROR;\r
1504 } else {\r
1505 Token->Status = Status;\r
1506 }\r
1507\r
47f51a06
YT
1508 gBS->SignalEvent (Token->Event);\r
1509\r
f75a7f56 1510 return Status;\r
47f51a06
YT
1511}\r
1512\r
47f51a06
YT
1513/**\r
1514 The Response() function queues an HTTP response to this HTTP instance, similar to\r
f0ab5a81 1515 Receive() function in the EFI TCP driver. When the HTTP response is received successfully,\r
47f51a06
YT
1516 or if there is an error, Status in token will be updated and Event will be signaled.\r
1517\r
1518 The HTTP driver will queue a receive token to the underlying TCP instance. When data\r
1519 is received in the underlying TCP instance, the data will be parsed and Token will\r
1520 be populated with the response data. If the data received from the remote host\r
1521 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting\r
1522 (asynchronously) for more data to be sent from the remote host before signaling\r
1523 Event in Token.\r
1524\r
1525 It is the responsibility of the caller to allocate a buffer for Body and specify the\r
1526 size in BodyLength. If the remote host provides a response that contains a content\r
1527 body, up to BodyLength bytes will be copied from the receive buffer into Body and\r
1528 BodyLength will be updated with the amount of bytes received and copied to Body. This\r
1529 allows the client to download a large file in chunks instead of into one contiguous\r
1530 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is\r
1531 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive\r
1532 token to underlying TCP instance. If data arrives in the receive buffer, up to\r
1533 BodyLength bytes of data will be copied to Body. The HTTP driver will then update\r
1534 BodyLength with the amount of bytes received and copied to Body.\r
1535\r
1536 If the HTTP driver does not have an open underlying TCP connection with the host\r
1537 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is\r
1538 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain\r
1539 an open TCP connection between client and host.\r
1540\r
1541 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
1542 @param[in] Token Pointer to storage containing HTTP response token.\r
1543\r
1544 @retval EFI_SUCCESS Allocation succeeded.\r
1545 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been\r
1546 initialized.\r
1547 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
1548 This is NULL.\r
1549 Token is NULL.\r
1550 Token->Message->Headers is NULL.\r
1551 Token->Message is NULL.\r
1552 Token->Message->Body is not NULL,\r
1553 Token->Message->BodyLength is non-zero, and\r
1554 Token->Message->Data is NULL, but a previous call to\r
1555 Response() has not been completed successfully.\r
1556 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.\r
1557 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host\r
1558 specified by response URL.\r
1559**/\r
1560EFI_STATUS\r
1561EFIAPI\r
1562EfiHttpResponse (\r
d1050b9d
MK
1563 IN EFI_HTTP_PROTOCOL *This,\r
1564 IN EFI_HTTP_TOKEN *Token\r
47f51a06
YT
1565 )\r
1566{\r
d1050b9d
MK
1567 EFI_STATUS Status;\r
1568 EFI_HTTP_MESSAGE *HttpMsg;\r
1569 HTTP_PROTOCOL *HttpInstance;\r
1570 HTTP_TOKEN_WRAP *Wrap;\r
47f51a06
YT
1571\r
1572 if ((This == NULL) || (Token == NULL)) {\r
1573 return EFI_INVALID_PARAMETER;\r
1574 }\r
1575\r
1576 HttpMsg = Token->Message;\r
1577 if (HttpMsg == NULL) {\r
1578 return EFI_INVALID_PARAMETER;\r
1579 }\r
f75a7f56 1580\r
47f51a06 1581 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
47f51a06
YT
1582\r
1583 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r
1584 return EFI_NOT_STARTED;\r
1585 }\r
1586\r
47f51a06
YT
1587 //\r
1588 // Check whether the token already existed.\r
1589 //\r
1590 if (EFI_ERROR (NetMapIterate (&HttpInstance->RxTokens, HttpTokenExist, Token))) {\r
f75a7f56 1591 return EFI_ACCESS_DENIED;\r
47f51a06
YT
1592 }\r
1593\r
1594 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r
1595 if (Wrap == NULL) {\r
1596 return EFI_OUT_OF_RESOURCES;\r
1597 }\r
1598\r
1599 Wrap->HttpInstance = HttpInstance;\r
1600 Wrap->HttpToken = Token;\r
1601\r
dac45de3 1602 //\r
f75a7f56
LG
1603 // Notes: For Https, receive token wrapped in HTTP_TOKEN_WRAP is not used to\r
1604 // receive the https response. A special TlsRxToken is used for receiving TLS\r
dac45de3
JW
1605 // related messages. It should be a blocking response.\r
1606 //\r
1607 if (!HttpInstance->UseHttps) {\r
1608 Status = HttpCreateTcpRxEvent (Wrap);\r
1609 if (EFI_ERROR (Status)) {\r
1610 goto Error;\r
1611 }\r
47f51a06
YT
1612 }\r
1613\r
1614 Status = NetMapInsertTail (&HttpInstance->RxTokens, Token, Wrap);\r
1615 if (EFI_ERROR (Status)) {\r
1616 goto Error;\r
1617 }\r
1618\r
1619 //\r
1620 // If already have pending RxTokens, return directly.\r
1621 //\r
1622 if (NetMapGetCount (&HttpInstance->RxTokens) > 1) {\r
1623 return EFI_SUCCESS;\r
1624 }\r
1625\r
1626 return HttpResponseWorker (Wrap);\r
1627\r
1628Error:\r
1629 if (Wrap != NULL) {\r
b659408b
ZL
1630 if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) {\r
1631 gBS->CloseEvent (Wrap->TcpWrap.Rx4Token.CompletionToken.Event);\r
1632 }\r
1633\r
1634 if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {\r
1635 gBS->CloseEvent (Wrap->TcpWrap.Rx6Token.CompletionToken.Event);\r
47f51a06 1636 }\r
d1050b9d 1637\r
47f51a06 1638 FreePool (Wrap);\r
f75a7f56 1639 }\r
47f51a06 1640\r
f75a7f56 1641 return Status;\r
47f51a06
YT
1642}\r
1643\r
1644/**\r
1645 The Poll() function can be used by network drivers and applications to increase the\r
1646 rate that data packets are moved between the communication devices and the transmit\r
1647 and receive queues.\r
1648\r
1649 In some systems, the periodic timer event in the managed network driver may not poll\r
1650 the underlying communications device fast enough to transmit and/or receive all data\r
1651 packets without missing incoming packets or dropping outgoing packets. Drivers and\r
1652 applications that are experiencing packet loss should try calling the Poll() function\r
1653 more often.\r
1654\r
1655 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
1656\r
1657 @retval EFI_SUCCESS Incoming or outgoing data was processed.\r
1658 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
1659 @retval EFI_INVALID_PARAMETER This is NULL.\r
1660 @retval EFI_NOT_READY No incoming or outgoing data is processed.\r
1661 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.\r
1662\r
1663**/\r
1664EFI_STATUS\r
1665EFIAPI\r
1666EfiHttpPoll (\r
d1050b9d 1667 IN EFI_HTTP_PROTOCOL *This\r
47f51a06
YT
1668 )\r
1669{\r
d1050b9d
MK
1670 EFI_STATUS Status;\r
1671 HTTP_PROTOCOL *HttpInstance;\r
47f51a06
YT
1672\r
1673 if (This == NULL) {\r
1674 return EFI_INVALID_PARAMETER;\r
1675 }\r
1676\r
1677 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
47f51a06 1678\r
1b96428d 1679 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r
47f51a06
YT
1680 return EFI_NOT_STARTED;\r
1681 }\r
f75a7f56 1682\r
b659408b 1683 if (HttpInstance->LocalAddressIsIPv6) {\r
1b96428d
ZL
1684 if (HttpInstance->Tcp6 == NULL) {\r
1685 return EFI_NOT_STARTED;\r
1686 }\r
d1050b9d 1687\r
b659408b
ZL
1688 Status = HttpInstance->Tcp6->Poll (HttpInstance->Tcp6);\r
1689 } else {\r
1b96428d
ZL
1690 if (HttpInstance->Tcp4 == NULL) {\r
1691 return EFI_NOT_STARTED;\r
1692 }\r
d1050b9d 1693\r
b659408b
ZL
1694 Status = HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);\r
1695 }\r
f75a7f56 1696\r
49c9f74c 1697 DispatchDpc ();\r
f75a7f56 1698\r
49c9f74c 1699 return Status;\r
47f51a06 1700}\r