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