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