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