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