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