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