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