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