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