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