]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - NetworkPkg/HttpDxe/HttpImpl.c
NetworkPkg: Refine type cast for pointer subtraction
[mirror_edk2.git] / NetworkPkg / HttpDxe / HttpImpl.c
... / ...
CommitLineData
1/** @file\r
2 Implementation of EFI_HTTP_PROTOCOL protocol interfaces.\r
3\r
4 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>\r
5 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>\r
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 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
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
58 //\r
59 // Check input parameters.\r
60 //\r
61 if ((This == NULL) || (HttpConfigData == NULL)) {\r
62 return EFI_INVALID_PARAMETER;\r
63 }\r
64\r
65 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
66 ASSERT (HttpInstance != NULL);\r
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
73 if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) {\r
74 return EFI_NOT_STARTED;\r
75 }\r
76\r
77 HttpConfigData->HttpVersion = HttpInstance->HttpVersion;\r
78 HttpConfigData->TimeOutMillisec = HttpInstance->TimeOutMillisec;\r
79 HttpConfigData->LocalAddressIsIPv6 = HttpInstance->LocalAddressIsIPv6;\r
80\r
81 if (HttpInstance->LocalAddressIsIPv6) {\r
82 CopyMem (\r
83 HttpConfigData->AccessPoint.IPv6Node,\r
84 &HttpInstance->Ipv6Node,\r
85 sizeof (HttpInstance->Ipv6Node)\r
86 );\r
87 } else {\r
88 CopyMem (\r
89 HttpConfigData->AccessPoint.IPv4Node,\r
90 &HttpInstance->IPv4Node,\r
91 sizeof (HttpInstance->IPv4Node)\r
92 );\r
93 }\r
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
108 No other EFI HTTP function can be executed by this instance until the Configure()\r
109 function is executed and returns successfully.\r
110\r
111 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
112 @param[in] HttpConfigData Pointer to the configure data to configure the instance.\r
113\r
114 @retval EFI_SUCCESS Operation succeeded.\r
115 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
116 This is NULL.\r
117 HttpConfigData->LocalAddressIsIPv6 is FALSE and\r
118 HttpConfigData->IPv4Node is NULL.\r
119 HttpConfigData->LocalAddressIsIPv6 is TRUE and\r
120 HttpConfigData->IPv6Node is NULL.\r
121 @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling\r
122 Configure() with NULL to reset it.\r
123 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
124 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when\r
125 executing Configure().\r
126 @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported\r
127 in the implementation.\r
128**/\r
129EFI_STATUS\r
130EFIAPI\r
131EfiHttpConfigure (\r
132 IN EFI_HTTP_PROTOCOL *This,\r
133 IN EFI_HTTP_CONFIG_DATA *HttpConfigData\r
134 ) \r
135{\r
136 HTTP_PROTOCOL *HttpInstance;\r
137 EFI_STATUS Status;\r
138 \r
139 //\r
140 // Check input parameters.\r
141 //\r
142 if (This == NULL ||\r
143 (HttpConfigData != NULL && \r
144 ((HttpConfigData->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv6Node == NULL) ||\r
145 (!HttpConfigData->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv4Node == NULL)))) {\r
146 return EFI_INVALID_PARAMETER;\r
147 }\r
148\r
149 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
150 ASSERT (HttpInstance != NULL && HttpInstance->Service != NULL);\r
151\r
152 if (HttpConfigData != NULL) {\r
153\r
154 //\r
155 // Now configure this HTTP instance.\r
156 //\r
157 if (HttpInstance->State != HTTP_STATE_UNCONFIGED) {\r
158 return EFI_ALREADY_STARTED;\r
159 }\r
160\r
161 HttpInstance->HttpVersion = HttpConfigData->HttpVersion;\r
162 HttpInstance->TimeOutMillisec = HttpConfigData->TimeOutMillisec;\r
163 HttpInstance->LocalAddressIsIPv6 = HttpConfigData->LocalAddressIsIPv6;\r
164 \r
165 if (HttpConfigData->LocalAddressIsIPv6) { \r
166 CopyMem (\r
167 &HttpInstance->Ipv6Node,\r
168 HttpConfigData->AccessPoint.IPv6Node,\r
169 sizeof (HttpInstance->Ipv6Node)\r
170 );\r
171 } else {\r
172 CopyMem (\r
173 &HttpInstance->IPv4Node,\r
174 HttpConfigData->AccessPoint.IPv4Node,\r
175 sizeof (HttpInstance->IPv4Node)\r
176 );\r
177 }\r
178 \r
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
189\r
190 } else {\r
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
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 is NULL.\r
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 UINTN HostNameSize;\r
240 UINT16 RemotePort;\r
241 HTTP_PROTOCOL *HttpInstance;\r
242 BOOLEAN Configure;\r
243 BOOLEAN ReConfigure;\r
244 BOOLEAN TlsConfigure;\r
245 CHAR8 *RequestMsg;\r
246 CHAR8 *Url;\r
247 UINTN UrlLen;\r
248 CHAR16 *HostNameStr;\r
249 HTTP_TOKEN_WRAP *Wrap;\r
250 CHAR8 *FileUrl;\r
251 UINTN RequestMsgSize;\r
252 EFI_HANDLE ImageHandle;\r
253\r
254 //\r
255 // Initializations\r
256 //\r
257 Url = NULL;\r
258 UrlParser = NULL;\r
259 RemotePort = 0;\r
260 HostName = NULL;\r
261 RequestMsg = NULL;\r
262 HostNameStr = NULL;\r
263 Wrap = NULL;\r
264 FileUrl = NULL;\r
265 TlsConfigure = FALSE;\r
266\r
267 if ((This == NULL) || (Token == NULL)) {\r
268 return EFI_INVALID_PARAMETER;\r
269 }\r
270\r
271 HttpMsg = Token->Message;\r
272 if (HttpMsg == NULL) {\r
273 return EFI_INVALID_PARAMETER;\r
274 }\r
275\r
276 Request = HttpMsg->Data.Request;\r
277\r
278 //\r
279 // Only support GET, HEAD, PUT and POST method in current implementation.\r
280 //\r
281 if ((Request != NULL) && (Request->Method != HttpMethodGet) &&\r
282 (Request->Method != HttpMethodHead) && (Request->Method != HttpMethodPut) && (Request->Method != HttpMethodPost)) {\r
283 return EFI_UNSUPPORTED;\r
284 }\r
285\r
286 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
287 ASSERT (HttpInstance != NULL);\r
288\r
289 //\r
290 // Capture the method into HttpInstance.\r
291 //\r
292 if (Request != NULL) {\r
293 HttpInstance->Method = Request->Method;\r
294 }\r
295\r
296 if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) {\r
297 return EFI_NOT_STARTED;\r
298 }\r
299\r
300 if (Request == NULL) {\r
301 //\r
302 // Request would be NULL only for PUT/POST operation (in the current implementation)\r
303 //\r
304 if ((HttpInstance->Method != HttpMethodPut) && (HttpInstance->Method != HttpMethodPost)) {\r
305 return EFI_INVALID_PARAMETER;\r
306 }\r
307\r
308 //\r
309 // For PUT/POST, we need to have the TCP already configured. Bail out if it is not!\r
310 //\r
311 if (HttpInstance->State < HTTP_STATE_TCP_CONFIGED) {\r
312 return EFI_INVALID_PARAMETER;\r
313 }\r
314\r
315 //\r
316 // We need to have the Message Body for sending the HTTP message across in these cases.\r
317 //\r
318 if (HttpMsg->Body == NULL || HttpMsg->BodyLength == 0) {\r
319 return EFI_INVALID_PARAMETER;\r
320 }\r
321\r
322 //\r
323 // Use existing TCP instance to transmit the packet.\r
324 //\r
325 Configure = FALSE;\r
326 ReConfigure = FALSE;\r
327 } else {\r
328 //\r
329 // Check whether the token already existed.\r
330 //\r
331 if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTokenExist, Token))) {\r
332 return EFI_ACCESS_DENIED;\r
333 }\r
334\r
335 //\r
336 // Parse the URI of the remote host.\r
337 //\r
338 Url = HttpInstance->Url;\r
339 UrlLen = StrLen (Request->Url) + 1;\r
340 if (UrlLen > HTTP_URL_BUFFER_LEN) {\r
341 Url = AllocateZeroPool (UrlLen);\r
342 if (Url == NULL) {\r
343 return EFI_OUT_OF_RESOURCES;\r
344 }\r
345 FreePool (HttpInstance->Url);\r
346 HttpInstance->Url = Url;\r
347 }\r
348\r
349\r
350 UnicodeStrToAsciiStrS (Request->Url, Url, UrlLen);\r
351\r
352 //\r
353 // From the information in Url, the HTTP instance will \r
354 // be able to determine whether to use http or https.\r
355 //\r
356 HttpInstance->UseHttps = IsHttpsUrl (Url);\r
357\r
358 //\r
359 // HTTP is disabled, return directly if the URI is not HTTPS.\r
360 //\r
361 if (!PcdGetBool (PcdAllowHttpConnections) && !(HttpInstance->UseHttps)) {\r
362 \r
363 DEBUG ((EFI_D_ERROR, "EfiHttpRequest: HTTP is disabled.\n"));\r
364\r
365 return EFI_ACCESS_DENIED;\r
366 }\r
367\r
368 //\r
369 // Check whether we need to create Tls child and open the TLS protocol.\r
370 //\r
371 if (HttpInstance->UseHttps && HttpInstance->TlsChildHandle == NULL) {\r
372 //\r
373 // Use TlsSb to create Tls child and open the TLS protocol.\r
374 //\r
375 if (HttpInstance->LocalAddressIsIPv6) {\r
376 ImageHandle = HttpInstance->Service->Ip6DriverBindingHandle;\r
377 } else {\r
378 ImageHandle = HttpInstance->Service->Ip4DriverBindingHandle;\r
379 }\r
380\r
381 HttpInstance->TlsChildHandle = TlsCreateChild (\r
382 ImageHandle,\r
383 &(HttpInstance->Tls),\r
384 &(HttpInstance->TlsConfiguration)\r
385 );\r
386 if (HttpInstance->TlsChildHandle == NULL) {\r
387 return EFI_DEVICE_ERROR;\r
388 }\r
389\r
390 TlsConfigure = TRUE;\r
391 }\r
392\r
393 UrlParser = NULL;\r
394 Status = HttpParseUrl (Url, (UINT32) AsciiStrLen (Url), FALSE, &UrlParser);\r
395 if (EFI_ERROR (Status)) {\r
396 goto Error1;\r
397 }\r
398\r
399 HostName = NULL;\r
400 Status = HttpUrlGetHostName (Url, UrlParser, &HostName);\r
401 if (EFI_ERROR (Status)) {\r
402 goto Error1;\r
403 }\r
404\r
405 Status = HttpUrlGetPort (Url, UrlParser, &RemotePort);\r
406 if (EFI_ERROR (Status)) {\r
407 if (HttpInstance->UseHttps) {\r
408 RemotePort = HTTPS_DEFAULT_PORT;\r
409 } else {\r
410 RemotePort = HTTP_DEFAULT_PORT;\r
411 }\r
412 }\r
413 //\r
414 // If Configure is TRUE, it indicates the first time to call Request();\r
415 // If ReConfigure is TRUE, it indicates the request URL is not same\r
416 // with the previous call to Request();\r
417 //\r
418 Configure = TRUE;\r
419 ReConfigure = TRUE;\r
420\r
421 if (HttpInstance->RemoteHost == NULL) {\r
422 //\r
423 // Request() is called the first time.\r
424 //\r
425 ReConfigure = FALSE;\r
426 } else {\r
427 if ((HttpInstance->RemotePort == RemotePort) &&\r
428 (AsciiStrCmp (HttpInstance->RemoteHost, HostName) == 0) && \r
429 (!HttpInstance->UseHttps || (HttpInstance->UseHttps && \r
430 !TlsConfigure && \r
431 HttpInstance->TlsSessionState == EfiTlsSessionDataTransferring))) {\r
432 //\r
433 // Host Name and port number of the request URL are the same with previous call to Request().\r
434 // If Https protocol used, the corresponding SessionState is EfiTlsSessionDataTransferring.\r
435 // Check whether previous TCP packet sent out.\r
436 //\r
437\r
438 if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTcpNotReady, NULL))) {\r
439 //\r
440 // Wrap the HTTP token in HTTP_TOKEN_WRAP\r
441 //\r
442 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r
443 if (Wrap == NULL) {\r
444 Status = EFI_OUT_OF_RESOURCES;\r
445 goto Error1;\r
446 }\r
447\r
448 Wrap->HttpToken = Token;\r
449 Wrap->HttpInstance = HttpInstance;\r
450\r
451 Status = HttpCreateTcpTxEvent (Wrap);\r
452 if (EFI_ERROR (Status)) {\r
453 goto Error1;\r
454 }\r
455\r
456 Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap);\r
457 if (EFI_ERROR (Status)) {\r
458 goto Error1;\r
459 }\r
460\r
461 Wrap->TcpWrap.Method = Request->Method;\r
462\r
463 FreePool (HostName);\r
464\r
465 //\r
466 // Queue the HTTP token and return.\r
467 //\r
468 return EFI_SUCCESS;\r
469 } else {\r
470 //\r
471 // Use existing TCP instance to transmit the packet.\r
472 //\r
473 Configure = FALSE;\r
474 ReConfigure = FALSE;\r
475 }\r
476 } else {\r
477 //\r
478 // Need close existing TCP instance and create a new TCP instance for data transmit.\r
479 //\r
480 if (HttpInstance->RemoteHost != NULL) {\r
481 FreePool (HttpInstance->RemoteHost);\r
482 HttpInstance->RemoteHost = NULL;\r
483 HttpInstance->RemotePort = 0;\r
484 }\r
485 }\r
486 }\r
487 } \r
488\r
489 if (Configure) {\r
490 //\r
491 // Parse Url for IPv4 or IPv6 address, if failed, perform DNS resolution.\r
492 //\r
493 if (!HttpInstance->LocalAddressIsIPv6) {\r
494 Status = NetLibAsciiStrToIp4 (HostName, &HttpInstance->RemoteAddr);\r
495 } else {\r
496 Status = HttpUrlGetIp6 (Url, UrlParser, &HttpInstance->RemoteIpv6Addr);\r
497 }\r
498\r
499 if (EFI_ERROR (Status)) {\r
500 HostNameSize = AsciiStrSize (HostName);\r
501 HostNameStr = AllocateZeroPool (HostNameSize * sizeof (CHAR16));\r
502 if (HostNameStr == NULL) {\r
503 Status = EFI_OUT_OF_RESOURCES;\r
504 goto Error1;\r
505 }\r
506 \r
507 AsciiStrToUnicodeStrS (HostName, HostNameStr, HostNameSize);\r
508 if (!HttpInstance->LocalAddressIsIPv6) {\r
509 Status = HttpDns4 (HttpInstance, HostNameStr, &HttpInstance->RemoteAddr);\r
510 } else {\r
511 Status = HttpDns6 (HttpInstance, HostNameStr, &HttpInstance->RemoteIpv6Addr);\r
512 }\r
513 \r
514 FreePool (HostNameStr);\r
515 if (EFI_ERROR (Status)) {\r
516 goto Error1;\r
517 }\r
518 }\r
519\r
520 //\r
521 // Save the RemotePort and RemoteHost.\r
522 //\r
523 ASSERT (HttpInstance->RemoteHost == NULL);\r
524 HttpInstance->RemotePort = RemotePort;\r
525 HttpInstance->RemoteHost = HostName;\r
526 HostName = NULL;\r
527 }\r
528\r
529 if (ReConfigure) {\r
530 //\r
531 // The request URL is different from previous calls to Request(), close existing TCP instance.\r
532 //\r
533 if (!HttpInstance->LocalAddressIsIPv6) {\r
534 ASSERT (HttpInstance->Tcp4 != NULL);\r
535 } else {\r
536 ASSERT (HttpInstance->Tcp6 != NULL);\r
537 }\r
538\r
539 if (HttpInstance->UseHttps && !TlsConfigure) {\r
540 Status = TlsCloseSession (HttpInstance);\r
541 if (EFI_ERROR (Status)) {\r
542 goto Error1;\r
543 }\r
544 \r
545 TlsCloseTxRxEvent (HttpInstance);\r
546 }\r
547 \r
548 HttpCloseConnection (HttpInstance);\r
549 EfiHttpCancel (This, NULL);\r
550 }\r
551\r
552 //\r
553 // Wrap the HTTP token in HTTP_TOKEN_WRAP\r
554 //\r
555 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r
556 if (Wrap == NULL) {\r
557 Status = EFI_OUT_OF_RESOURCES;\r
558 goto Error1;\r
559 }\r
560\r
561 Wrap->HttpToken = Token;\r
562 Wrap->HttpInstance = HttpInstance;\r
563 if (Request != NULL) {\r
564 Wrap->TcpWrap.Method = Request->Method;\r
565 }\r
566 \r
567 Status = HttpInitSession (\r
568 HttpInstance, \r
569 Wrap, \r
570 Configure || ReConfigure, \r
571 TlsConfigure\r
572 );\r
573 if (EFI_ERROR (Status)) {\r
574 goto Error2;\r
575 }\r
576\r
577 if (!Configure && !ReConfigure && !TlsConfigure) {\r
578 //\r
579 // For the new HTTP token, create TX TCP token events. \r
580 //\r
581 Status = HttpCreateTcpTxEvent (Wrap);\r
582 if (EFI_ERROR (Status)) {\r
583 goto Error1;\r
584 }\r
585 }\r
586 \r
587 //\r
588 // Create request message.\r
589 //\r
590 FileUrl = Url;\r
591 if (Url != NULL && *FileUrl != '/') {\r
592 //\r
593 // Convert the absolute-URI to the absolute-path\r
594 //\r
595 while (*FileUrl != ':') {\r
596 FileUrl++;\r
597 }\r
598 if ((*(FileUrl+1) == '/') && (*(FileUrl+2) == '/')) {\r
599 FileUrl += 3;\r
600 while (*FileUrl != '/') {\r
601 FileUrl++;\r
602 }\r
603 } else {\r
604 Status = EFI_INVALID_PARAMETER;\r
605 goto Error3;\r
606 }\r
607 }\r
608\r
609 Status = HttpGenRequestMessage (HttpMsg, FileUrl, &RequestMsg, &RequestMsgSize);\r
610\r
611 if (EFI_ERROR (Status) || NULL == RequestMsg) {\r
612 goto Error3;\r
613 }\r
614\r
615 ASSERT (RequestMsg != NULL);\r
616\r
617 //\r
618 // Every request we insert a TxToken and a response call would remove the TxToken.\r
619 // In cases of PUT/POST, after an initial request-response pair, we would do a\r
620 // continuous request without a response call. So, in such cases, where Request\r
621 // structure is NULL, we would not insert a TxToken.\r
622 //\r
623 if (Request != NULL) {\r
624 Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap);\r
625 if (EFI_ERROR (Status)) {\r
626 goto Error4;\r
627 }\r
628 }\r
629\r
630 //\r
631 // Transmit the request message.\r
632 //\r
633 Status = HttpTransmitTcp (\r
634 HttpInstance,\r
635 Wrap,\r
636 (UINT8*) RequestMsg,\r
637 RequestMsgSize\r
638 );\r
639 if (EFI_ERROR (Status)) {\r
640 goto Error5; \r
641 }\r
642\r
643 DispatchDpc ();\r
644 \r
645 if (HostName != NULL) {\r
646 FreePool (HostName);\r
647 }\r
648 \r
649 return EFI_SUCCESS;\r
650\r
651Error5:\r
652 //\r
653 // We would have inserted a TxToken only if Request structure is not NULL.\r
654 // Hence check before we do a remove in this error case.\r
655 //\r
656 if (Request != NULL) {\r
657 NetMapRemoveTail (&HttpInstance->TxTokens, NULL);\r
658 }\r
659\r
660Error4:\r
661 if (RequestMsg != NULL) {\r
662 FreePool (RequestMsg);\r
663 } \r
664\r
665Error3:\r
666 if (HttpInstance->UseHttps) {\r
667 TlsCloseSession (HttpInstance);\r
668 TlsCloseTxRxEvent (HttpInstance);\r
669 }\r
670\r
671Error2:\r
672 HttpCloseConnection (HttpInstance);\r
673 \r
674 HttpCloseTcpConnCloseEvent (HttpInstance);\r
675 if (NULL != Wrap->TcpWrap.Tx4Token.CompletionToken.Event) {\r
676 gBS->CloseEvent (Wrap->TcpWrap.Tx4Token.CompletionToken.Event);\r
677 Wrap->TcpWrap.Tx4Token.CompletionToken.Event = NULL;\r
678 }\r
679 if (NULL != Wrap->TcpWrap.Tx6Token.CompletionToken.Event) {\r
680 gBS->CloseEvent (Wrap->TcpWrap.Tx6Token.CompletionToken.Event);\r
681 Wrap->TcpWrap.Tx6Token.CompletionToken.Event = NULL;\r
682 }\r
683\r
684Error1:\r
685 if (HostName != NULL) {\r
686 FreePool (HostName);\r
687 }\r
688 if (Wrap != NULL) {\r
689 FreePool (Wrap);\r
690 }\r
691 if (UrlParser!= NULL) {\r
692 HttpUrlFreeParser (UrlParser);\r
693 }\r
694\r
695 return Status;\r
696 \r
697}\r
698\r
699/**\r
700 Cancel a user's Token. \r
701 \r
702 @param[in] Map The HTTP instance's token queue.\r
703 @param[in] Item Object container for one HTTP token and token's wrap.\r
704 @param[in] Context The user's token to cancel.\r
705\r
706 @retval EFI_SUCCESS Continue to check the next Item.\r
707 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.\r
708\r
709**/\r
710EFI_STATUS\r
711EFIAPI\r
712HttpCancelTokens (\r
713 IN NET_MAP *Map,\r
714 IN NET_MAP_ITEM *Item,\r
715 IN VOID *Context\r
716 )\r
717{\r
718 EFI_HTTP_TOKEN *Token;\r
719 HTTP_TOKEN_WRAP *Wrap;\r
720 HTTP_PROTOCOL *HttpInstance;\r
721\r
722 Token = (EFI_HTTP_TOKEN *) Context;\r
723\r
724 //\r
725 // Return EFI_SUCCESS to check the next item in the map if\r
726 // this one doesn't match.\r
727 //\r
728 if ((Token != NULL) && (Token != Item->Key)) {\r
729 return EFI_SUCCESS;\r
730 }\r
731\r
732 Wrap = (HTTP_TOKEN_WRAP *) Item->Value;\r
733 ASSERT (Wrap != NULL);\r
734 HttpInstance = Wrap->HttpInstance;\r
735 \r
736 if (!HttpInstance->LocalAddressIsIPv6) {\r
737 if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) {\r
738 //\r
739 // Cancle the Token before close its Event.\r
740 //\r
741 HttpInstance->Tcp4->Cancel (HttpInstance->Tcp4, &Wrap->TcpWrap.Rx4Token.CompletionToken);\r
742\r
743 //\r
744 // Dispatch the DPC queued by the NotifyFunction of the canceled token's events.\r
745 //\r
746 DispatchDpc ();\r
747 }\r
748 } else {\r
749 if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {\r
750 //\r
751 // Cancle the Token before close its Event.\r
752 //\r
753 HttpInstance->Tcp6->Cancel (HttpInstance->Tcp6, &Wrap->TcpWrap.Rx6Token.CompletionToken);\r
754\r
755 //\r
756 // Dispatch the DPC queued by the NotifyFunction of the canceled token's events.\r
757 //\r
758 DispatchDpc ();\r
759 }\r
760 }\r
761\r
762 //\r
763 // If only one item is to be cancel, return EFI_ABORTED to stop\r
764 // iterating the map any more.\r
765 //\r
766 if (Token != NULL) {\r
767 return EFI_ABORTED;\r
768 }\r
769\r
770 return EFI_SUCCESS; \r
771}\r
772\r
773/**\r
774 Cancel the user's receive/transmit request. It is the worker function of\r
775 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the\r
776 token.\r
777\r
778 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.\r
779 @param[in] Token The token to cancel. If NULL, all token will be\r
780 cancelled.\r
781\r
782 @retval EFI_SUCCESS The token is cancelled.\r
783 @retval EFI_NOT_FOUND The asynchronous request or response token is not found. \r
784 @retval Others Other error as indicated.\r
785\r
786**/\r
787EFI_STATUS\r
788HttpCancel (\r
789 IN HTTP_PROTOCOL *HttpInstance,\r
790 IN EFI_HTTP_TOKEN *Token\r
791 )\r
792{\r
793 EFI_STATUS Status;\r
794\r
795 //\r
796 // First check the tokens queued by EfiHttpRequest().\r
797 //\r
798 Status = NetMapIterate (&HttpInstance->TxTokens, HttpCancelTokens, Token);\r
799 if (EFI_ERROR (Status)) {\r
800 if (Token != NULL) {\r
801 if (Status == EFI_ABORTED) {\r
802 return EFI_SUCCESS;\r
803 } \r
804 } else {\r
805 return Status;\r
806 }\r
807 }\r
808\r
809 if (!HttpInstance->UseHttps) {\r
810 //\r
811 // Then check the tokens queued by EfiHttpResponse(), except for Https.\r
812 //\r
813 Status = NetMapIterate (&HttpInstance->RxTokens, HttpCancelTokens, Token);\r
814 if (EFI_ERROR (Status)) {\r
815 if (Token != NULL) {\r
816 if (Status == EFI_ABORTED) {\r
817 return EFI_SUCCESS;\r
818 } else {\r
819 return EFI_NOT_FOUND;\r
820 }\r
821 } else {\r
822 return Status;\r
823 }\r
824 }\r
825 } else {\r
826 if (!HttpInstance->LocalAddressIsIPv6) {\r
827 HttpInstance->Tcp4->Cancel (HttpInstance->Tcp4, &HttpInstance->Tcp4TlsRxToken.CompletionToken);\r
828 } else {\r
829 HttpInstance->Tcp6->Cancel (HttpInstance->Tcp6, &HttpInstance->Tcp6TlsRxToken.CompletionToken);\r
830 }\r
831 }\r
832 \r
833 return EFI_SUCCESS;\r
834}\r
835\r
836\r
837/**\r
838 Abort an asynchronous HTTP request or response token.\r
839\r
840 The Cancel() function aborts a pending HTTP request or response transaction. If\r
841 Token is not NULL and the token is in transmit or receive queues when it is being\r
842 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will\r
843 be signaled. If the token is not in one of the queues, which usually means that the\r
844 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,\r
845 all asynchronous tokens issued by Request() or Response() will be aborted.\r
846\r
847 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
848 @param[in] Token Point to storage containing HTTP request or response\r
849 token.\r
850\r
851 @retval EFI_SUCCESS Request and Response queues are successfully flushed.\r
852 @retval EFI_INVALID_PARAMETER This is NULL.\r
853 @retval EFI_NOT_STARTED This instance hasn't been configured.\r
854 @retval EFI_NOT_FOUND The asynchronous request or response token is not\r
855 found.\r
856 @retval EFI_UNSUPPORTED The implementation does not support this function.\r
857\r
858**/\r
859EFI_STATUS\r
860EFIAPI\r
861EfiHttpCancel (\r
862 IN EFI_HTTP_PROTOCOL *This,\r
863 IN EFI_HTTP_TOKEN *Token\r
864 )\r
865{\r
866 HTTP_PROTOCOL *HttpInstance;\r
867\r
868 if (This == NULL) {\r
869 return EFI_INVALID_PARAMETER;\r
870 }\r
871\r
872 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
873 ASSERT (HttpInstance != NULL);\r
874\r
875 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r
876 return EFI_NOT_STARTED;\r
877 }\r
878\r
879 return HttpCancel (HttpInstance, Token);\r
880\r
881}\r
882\r
883/**\r
884 A callback function to intercept events during message parser.\r
885\r
886 This function will be invoked during HttpParseMessageBody() with various events type. An error\r
887 return status of the callback function will cause the HttpParseMessageBody() aborted.\r
888\r
889 @param[in] EventType Event type of this callback call.\r
890 @param[in] Data A pointer to data buffer.\r
891 @param[in] Length Length in bytes of the Data.\r
892 @param[in] Context Callback context set by HttpInitMsgParser().\r
893\r
894 @retval EFI_SUCCESS Continue to parser the message body.\r
895\r
896**/\r
897EFI_STATUS\r
898EFIAPI\r
899HttpBodyParserCallback (\r
900 IN HTTP_BODY_PARSE_EVENT EventType,\r
901 IN CHAR8 *Data,\r
902 IN UINTN Length,\r
903 IN VOID *Context\r
904 )\r
905{\r
906 HTTP_TOKEN_WRAP *Wrap;\r
907 UINTN BodyLength;\r
908 CHAR8 *Body;\r
909\r
910 if (EventType != BodyParseEventOnComplete) {\r
911 return EFI_SUCCESS;\r
912 }\r
913\r
914 if (Data == NULL || Length != 0 || Context == NULL) {\r
915 return EFI_SUCCESS;\r
916 }\r
917\r
918 Wrap = (HTTP_TOKEN_WRAP *) Context;\r
919 Body = Wrap->HttpToken->Message->Body;\r
920 BodyLength = Wrap->HttpToken->Message->BodyLength;\r
921 if (Data < Body + BodyLength) {\r
922 Wrap->HttpInstance->NextMsg = Data;\r
923 } else {\r
924 Wrap->HttpInstance->NextMsg = NULL;\r
925 }\r
926 \r
927\r
928 //\r
929 // Free Tx4Token or Tx6Token since already received corrsponding HTTP response.\r
930 //\r
931 FreePool (Wrap);\r
932\r
933 return EFI_SUCCESS;\r
934}\r
935\r
936/**\r
937 The work function of EfiHttpResponse().\r
938\r
939 @param[in] Wrap Pointer to HTTP token's wrap data.\r
940\r
941 @retval EFI_SUCCESS Allocation succeeded.\r
942 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.\r
943 @retval EFI_NOT_READY Can't find a corresponding Tx4Token/Tx6Token or \r
944 the EFI_HTTP_UTILITIES_PROTOCOL is not available.\r
945\r
946**/\r
947EFI_STATUS\r
948HttpResponseWorker (\r
949 IN HTTP_TOKEN_WRAP *Wrap\r
950 )\r
951{\r
952 EFI_STATUS Status;\r
953 EFI_HTTP_MESSAGE *HttpMsg;\r
954 CHAR8 *EndofHeader;\r
955 CHAR8 *HttpHeaders;\r
956 UINTN SizeofHeaders;\r
957 UINTN BufferSize;\r
958 UINTN StatusCode;\r
959 CHAR8 *Tmp;\r
960 CHAR8 *HeaderTmp;\r
961 CHAR8 *StatusCodeStr;\r
962 UINTN BodyLen;\r
963 HTTP_PROTOCOL *HttpInstance;\r
964 EFI_HTTP_TOKEN *Token;\r
965 NET_MAP_ITEM *Item;\r
966 HTTP_TOKEN_WRAP *ValueInItem;\r
967 UINTN HdrLen;\r
968 NET_FRAGMENT Fragment;\r
969\r
970 if (Wrap == NULL || Wrap->HttpInstance == NULL) {\r
971 return EFI_INVALID_PARAMETER;\r
972 }\r
973 \r
974 HttpInstance = Wrap->HttpInstance;\r
975 Token = Wrap->HttpToken;\r
976 HttpMsg = Token->Message;\r
977\r
978 HttpInstance->EndofHeader = NULL;\r
979 HttpInstance->HttpHeaders = NULL;\r
980 HttpMsg->Headers = NULL;\r
981 HttpHeaders = NULL;\r
982 SizeofHeaders = 0;\r
983 BufferSize = 0;\r
984 EndofHeader = NULL;\r
985 ValueInItem = NULL;\r
986 Fragment.Len = 0;\r
987 Fragment.Bulk = NULL;\r
988 \r
989 if (HttpMsg->Data.Response != NULL) {\r
990 //\r
991 // Check whether we have cached header from previous call.\r
992 //\r
993 if ((HttpInstance->CacheBody != NULL) && (HttpInstance->NextMsg != NULL)) {\r
994 //\r
995 // The data is stored at [NextMsg, CacheBody + CacheLen].\r
996 //\r
997 HdrLen = HttpInstance->CacheBody + HttpInstance->CacheLen - HttpInstance->NextMsg;\r
998 HttpHeaders = AllocateZeroPool (HdrLen);\r
999 if (HttpHeaders == NULL) {\r
1000 Status = EFI_OUT_OF_RESOURCES;\r
1001 goto Error;\r
1002 }\r
1003\r
1004 CopyMem (HttpHeaders, HttpInstance->NextMsg, HdrLen);\r
1005 FreePool (HttpInstance->CacheBody);\r
1006 HttpInstance->CacheBody = NULL;\r
1007 HttpInstance->NextMsg = NULL;\r
1008 HttpInstance->CacheOffset = 0;\r
1009 SizeofHeaders = HdrLen;\r
1010 BufferSize = HttpInstance->CacheLen;\r
1011\r
1012 //\r
1013 // Check whether we cached the whole HTTP headers.\r
1014 //\r
1015 EndofHeader = AsciiStrStr (HttpHeaders, HTTP_END_OF_HDR_STR); \r
1016 } \r
1017\r
1018 HttpInstance->EndofHeader = &EndofHeader;\r
1019 HttpInstance->HttpHeaders = &HttpHeaders;\r
1020\r
1021\r
1022 if (HttpInstance->TimeoutEvent == NULL) {\r
1023 //\r
1024 // Create TimeoutEvent for response\r
1025 //\r
1026 Status = gBS->CreateEvent (\r
1027 EVT_TIMER,\r
1028 TPL_CALLBACK,\r
1029 NULL,\r
1030 NULL,\r
1031 &HttpInstance->TimeoutEvent\r
1032 );\r
1033 if (EFI_ERROR (Status)) {\r
1034 goto Error;\r
1035 }\r
1036 }\r
1037\r
1038 //\r
1039 // Start the timer, and wait Timeout seconds to receive the header packet.\r
1040 //\r
1041 Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_RESPONSE_TIMEOUT * TICKS_PER_SECOND);\r
1042 if (EFI_ERROR (Status)) {\r
1043 goto Error;\r
1044 }\r
1045\r
1046 Status = HttpTcpReceiveHeader (HttpInstance, &SizeofHeaders, &BufferSize, HttpInstance->TimeoutEvent);\r
1047\r
1048 gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0);\r
1049\r
1050 if (EFI_ERROR (Status)) {\r
1051 goto Error;\r
1052 }\r
1053\r
1054 ASSERT (HttpHeaders != NULL);\r
1055\r
1056 //\r
1057 // Cache the part of body.\r
1058 //\r
1059 BodyLen = BufferSize - (EndofHeader - HttpHeaders);\r
1060 if (BodyLen > 0) {\r
1061 if (HttpInstance->CacheBody != NULL) {\r
1062 FreePool (HttpInstance->CacheBody);\r
1063 }\r
1064\r
1065 HttpInstance->CacheBody = AllocateZeroPool (BodyLen);\r
1066 if (HttpInstance->CacheBody == NULL) {\r
1067 Status = EFI_OUT_OF_RESOURCES;\r
1068 goto Error;\r
1069 }\r
1070\r
1071 CopyMem (HttpInstance->CacheBody, EndofHeader, BodyLen);\r
1072 HttpInstance->CacheLen = BodyLen;\r
1073 }\r
1074\r
1075 //\r
1076 // Search for Status Code.\r
1077 //\r
1078 StatusCodeStr = HttpHeaders + AsciiStrLen (HTTP_VERSION_STR) + 1;\r
1079 if (StatusCodeStr == NULL) {\r
1080 Status = EFI_NOT_READY;\r
1081 goto Error;\r
1082 }\r
1083\r
1084 StatusCode = AsciiStrDecimalToUintn (StatusCodeStr);\r
1085\r
1086 //\r
1087 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".\r
1088 //\r
1089 Tmp = AsciiStrStr (HttpHeaders, HTTP_CRLF_STR);\r
1090 if (Tmp == NULL) {\r
1091 Status = EFI_NOT_READY;\r
1092 goto Error;\r
1093 }\r
1094\r
1095 //\r
1096 // We could have response with just a HTTP message and no headers. For Example,\r
1097 // "100 Continue". In such cases, we would not want to unnecessarily call a Parse\r
1098 // method. A "\r\n" following Tmp string again would indicate an end. Compare and\r
1099 // set SizeofHeaders to 0.\r
1100 //\r
1101 Tmp = Tmp + AsciiStrLen (HTTP_CRLF_STR);\r
1102 if (CompareMem (Tmp, HTTP_CRLF_STR, AsciiStrLen (HTTP_CRLF_STR)) == 0) {\r
1103 SizeofHeaders = 0;\r
1104 } else {\r
1105 SizeofHeaders = SizeofHeaders - (Tmp - HttpHeaders);\r
1106 }\r
1107\r
1108 HttpMsg->Data.Response->StatusCode = HttpMappingToStatusCode (StatusCode);\r
1109 HttpInstance->StatusCode = StatusCode;\r
1110\r
1111 Status = EFI_NOT_READY;\r
1112 ValueInItem = NULL;\r
1113\r
1114 //\r
1115 // In cases of PUT/POST, after an initial request-response pair, we would do a\r
1116 // continuous request without a response call. So, we would not do an insert of\r
1117 // TxToken. After we have sent the complete file, we will call a response to get\r
1118 // a final response from server. In such a case, we would not have any TxTokens.\r
1119 // Hence, check that case before doing a NetMapRemoveHead.\r
1120 //\r
1121 if (!NetMapIsEmpty (&HttpInstance->TxTokens)) {\r
1122 NetMapRemoveHead (&HttpInstance->TxTokens, (VOID**) &ValueInItem);\r
1123 if (ValueInItem == NULL) {\r
1124 goto Error;\r
1125 }\r
1126\r
1127 //\r
1128 // The first Tx Token not transmitted yet, insert back and return error.\r
1129 //\r
1130 if (!ValueInItem->TcpWrap.IsTxDone) {\r
1131 goto Error2;\r
1132 }\r
1133 }\r
1134\r
1135 if (SizeofHeaders != 0) {\r
1136 HeaderTmp = AllocateZeroPool (SizeofHeaders);\r
1137 if (HeaderTmp == NULL) {\r
1138 Status = EFI_OUT_OF_RESOURCES;\r
1139 goto Error2;\r
1140 }\r
1141\r
1142 CopyMem (HeaderTmp, Tmp, SizeofHeaders);\r
1143 FreePool (HttpHeaders);\r
1144 HttpHeaders = HeaderTmp;\r
1145\r
1146 //\r
1147 // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.\r
1148 //\r
1149 if (mHttpUtilities == NULL) {\r
1150 Status = EFI_NOT_READY;\r
1151 goto Error2;\r
1152 }\r
1153\r
1154 //\r
1155 // Parse the HTTP header into array of key/value pairs.\r
1156 //\r
1157 Status = mHttpUtilities->Parse (\r
1158 mHttpUtilities,\r
1159 HttpHeaders,\r
1160 SizeofHeaders,\r
1161 &HttpMsg->Headers,\r
1162 &HttpMsg->HeaderCount\r
1163 );\r
1164 if (EFI_ERROR (Status)) {\r
1165 goto Error2;\r
1166 }\r
1167\r
1168 FreePool (HttpHeaders);\r
1169 HttpHeaders = NULL;\r
1170\r
1171\r
1172 //\r
1173 // Init message-body parser by header information.\r
1174 //\r
1175 Status = HttpInitMsgParser (\r
1176 HttpInstance->Method,\r
1177 HttpMsg->Data.Response->StatusCode,\r
1178 HttpMsg->HeaderCount,\r
1179 HttpMsg->Headers,\r
1180 HttpBodyParserCallback,\r
1181 (VOID *) ValueInItem,\r
1182 &HttpInstance->MsgParser\r
1183 );\r
1184 if (EFI_ERROR (Status)) {\r
1185 goto Error2;\r
1186 }\r
1187\r
1188 //\r
1189 // Check whether we received a complete HTTP message.\r
1190 //\r
1191 if (HttpInstance->CacheBody != NULL) {\r
1192 Status = HttpParseMessageBody (HttpInstance->MsgParser, HttpInstance->CacheLen, HttpInstance->CacheBody);\r
1193 if (EFI_ERROR (Status)) {\r
1194 goto Error2;\r
1195 }\r
1196\r
1197 if (HttpIsMessageComplete (HttpInstance->MsgParser)) {\r
1198 //\r
1199 // Free the MsgParse since we already have a full HTTP message.\r
1200 //\r
1201 HttpFreeMsgParser (HttpInstance->MsgParser);\r
1202 HttpInstance->MsgParser = NULL;\r
1203 }\r
1204 }\r
1205 }\r
1206\r
1207 if ((HttpMsg->Body == NULL) || (HttpMsg->BodyLength == 0)) {\r
1208 Status = EFI_SUCCESS;\r
1209 goto Exit;\r
1210 }\r
1211 }\r
1212\r
1213 //\r
1214 // Receive the response body.\r
1215 //\r
1216 BodyLen = 0;\r
1217\r
1218 //\r
1219 // First check whether we cached some data.\r
1220 //\r
1221 if (HttpInstance->CacheBody != NULL) {\r
1222 //\r
1223 // Calculate the length of the cached data.\r
1224 //\r
1225 if (HttpInstance->NextMsg != NULL) {\r
1226 //\r
1227 // We have a cached HTTP message which includes a part of HTTP header of next message.\r
1228 //\r
1229 BodyLen = HttpInstance->NextMsg - (HttpInstance->CacheBody + HttpInstance->CacheOffset); \r
1230 } else {\r
1231 BodyLen = HttpInstance->CacheLen - HttpInstance->CacheOffset;\r
1232 }\r
1233\r
1234 if (BodyLen > 0) {\r
1235 //\r
1236 // We have some cached data. Just copy the data and return.\r
1237 //\r
1238 if (HttpMsg->BodyLength < BodyLen) {\r
1239 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, HttpMsg->BodyLength);\r
1240 HttpInstance->CacheOffset = HttpInstance->CacheOffset + HttpMsg->BodyLength;\r
1241 } else {\r
1242 //\r
1243 // Copy all cached data out.\r
1244 //\r
1245 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, BodyLen);\r
1246 HttpInstance->CacheOffset = BodyLen + HttpInstance->CacheOffset;\r
1247 HttpMsg->BodyLength = BodyLen;\r
1248\r
1249 if (HttpInstance->NextMsg == NULL) {\r
1250 //\r
1251 // There is no HTTP header of next message. Just free the cache buffer.\r
1252 //\r
1253 FreePool (HttpInstance->CacheBody);\r
1254 HttpInstance->CacheBody = NULL;\r
1255 HttpInstance->NextMsg = NULL;\r
1256 HttpInstance->CacheOffset = 0;\r
1257 }\r
1258 }\r
1259 //\r
1260 // Return since we aready received required data.\r
1261 //\r
1262 Status = EFI_SUCCESS;\r
1263 goto Exit;\r
1264 } \r
1265\r
1266 if (BodyLen == 0 && HttpInstance->MsgParser == NULL) {\r
1267 //\r
1268 // We received a complete HTTP message, and we don't have more data to return to caller.\r
1269 //\r
1270 HttpMsg->BodyLength = 0;\r
1271 Status = EFI_SUCCESS;\r
1272 goto Exit; \r
1273 } \r
1274 }\r
1275\r
1276 ASSERT (HttpInstance->MsgParser != NULL);\r
1277\r
1278 //\r
1279 // We still need receive more data when there is no cache data and MsgParser is not NULL;\r
1280 //\r
1281 if (!HttpInstance->UseHttps) {\r
1282 Status = HttpTcpReceiveBody (Wrap, HttpMsg);\r
1283\r
1284 if (EFI_ERROR (Status)) {\r
1285 goto Error2;\r
1286 }\r
1287 \r
1288 } else {\r
1289 if (HttpInstance->TimeoutEvent == NULL) {\r
1290 //\r
1291 // Create TimeoutEvent for response\r
1292 //\r
1293 Status = gBS->CreateEvent (\r
1294 EVT_TIMER,\r
1295 TPL_CALLBACK,\r
1296 NULL,\r
1297 NULL,\r
1298 &HttpInstance->TimeoutEvent\r
1299 );\r
1300 if (EFI_ERROR (Status)) {\r
1301 goto Error2;\r
1302 }\r
1303 }\r
1304\r
1305 //\r
1306 // Start the timer, and wait Timeout seconds to receive the body packet.\r
1307 //\r
1308 Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_RESPONSE_TIMEOUT * TICKS_PER_SECOND);\r
1309 if (EFI_ERROR (Status)) {\r
1310 goto Error2;\r
1311 }\r
1312 \r
1313 Status = HttpsReceive (HttpInstance, &Fragment, HttpInstance->TimeoutEvent);\r
1314\r
1315 gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0);\r
1316 \r
1317 if (EFI_ERROR (Status)) {\r
1318 goto Error2;\r
1319 }\r
1320\r
1321 //\r
1322 // Check whether we receive a complete HTTP message.\r
1323 //\r
1324 Status = HttpParseMessageBody (\r
1325 HttpInstance->MsgParser,\r
1326 (UINTN) Fragment.Len,\r
1327 (CHAR8 *) Fragment.Bulk\r
1328 );\r
1329 if (EFI_ERROR (Status)) {\r
1330 goto Error2;\r
1331 }\r
1332\r
1333 if (HttpIsMessageComplete (HttpInstance->MsgParser)) {\r
1334 //\r
1335 // Free the MsgParse since we already have a full HTTP message.\r
1336 //\r
1337 HttpFreeMsgParser (HttpInstance->MsgParser);\r
1338 HttpInstance->MsgParser = NULL;\r
1339 }\r
1340\r
1341 //\r
1342 // We receive part of header of next HTTP msg.\r
1343 //\r
1344 if (HttpInstance->NextMsg != NULL) {\r
1345 HttpMsg->BodyLength = MIN ((UINTN) HttpInstance->NextMsg - (UINTN) Fragment.Bulk, HttpMsg->BodyLength);\r
1346 CopyMem (HttpMsg->Body, Fragment.Bulk, HttpMsg->BodyLength);\r
1347 \r
1348 HttpInstance->CacheLen = Fragment.Len - HttpMsg->BodyLength;\r
1349 if (HttpInstance->CacheLen != 0) {\r
1350 if (HttpInstance->CacheBody != NULL) {\r
1351 FreePool (HttpInstance->CacheBody);\r
1352 }\r
1353 \r
1354 HttpInstance->CacheBody = AllocateZeroPool (HttpInstance->CacheLen);\r
1355 if (HttpInstance->CacheBody == NULL) {\r
1356 Status = EFI_OUT_OF_RESOURCES;\r
1357 goto Error2;\r
1358 }\r
1359 \r
1360 CopyMem (HttpInstance->CacheBody, Fragment.Bulk + HttpMsg->BodyLength, HttpInstance->CacheLen);\r
1361 HttpInstance->CacheOffset = 0;\r
1362\r
1363 HttpInstance->NextMsg = HttpInstance->CacheBody + ((UINTN) HttpInstance->NextMsg - (UINTN) (Fragment.Bulk + HttpMsg->BodyLength));\r
1364 }\r
1365 } else {\r
1366 HttpMsg->BodyLength = MIN (Fragment.Len, (UINT32) HttpMsg->BodyLength);\r
1367 CopyMem (HttpMsg->Body, Fragment.Bulk, HttpMsg->BodyLength);\r
1368 HttpInstance->CacheLen = Fragment.Len - HttpMsg->BodyLength;\r
1369 if (HttpInstance->CacheLen != 0) {\r
1370 if (HttpInstance->CacheBody != NULL) {\r
1371 FreePool (HttpInstance->CacheBody);\r
1372 }\r
1373 \r
1374 HttpInstance->CacheBody = AllocateZeroPool (HttpInstance->CacheLen);\r
1375 if (HttpInstance->CacheBody == NULL) {\r
1376 Status = EFI_OUT_OF_RESOURCES;\r
1377 goto Error2;\r
1378 }\r
1379\r
1380 CopyMem (HttpInstance->CacheBody, Fragment.Bulk + HttpMsg->BodyLength, HttpInstance->CacheLen);\r
1381 HttpInstance->CacheOffset = 0;\r
1382 }\r
1383 }\r
1384\r
1385 if (Fragment.Bulk != NULL) {\r
1386 FreePool (Fragment.Bulk);\r
1387 Fragment.Bulk = NULL;\r
1388 }\r
1389\r
1390 goto Exit;\r
1391 }\r
1392\r
1393 return Status;\r
1394\r
1395Exit:\r
1396 Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);\r
1397 if (Item != NULL) {\r
1398 NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);\r
1399 }\r
1400\r
1401 if (HttpInstance->StatusCode >= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE) {\r
1402 Token->Status = EFI_HTTP_ERROR;\r
1403 } else {\r
1404 Token->Status = Status;\r
1405 }\r
1406\r
1407 gBS->SignalEvent (Token->Event);\r
1408 HttpCloseTcpRxEvent (Wrap);\r
1409 FreePool (Wrap);\r
1410 return Status;\r
1411\r
1412Error2:\r
1413 if (ValueInItem != NULL) {\r
1414 NetMapInsertHead (&HttpInstance->TxTokens, ValueInItem->HttpToken, ValueInItem);\r
1415 }\r
1416\r
1417Error:\r
1418 Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);\r
1419 if (Item != NULL) {\r
1420 NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);\r
1421 }\r
1422\r
1423 if (!HttpInstance->UseHttps) {\r
1424 HttpTcpTokenCleanup (Wrap);\r
1425 } else {\r
1426 FreePool (Wrap);\r
1427 }\r
1428 \r
1429 if (HttpHeaders != NULL) {\r
1430 FreePool (HttpHeaders);\r
1431 HttpHeaders = NULL;\r
1432 }\r
1433\r
1434 if (Fragment.Bulk != NULL) {\r
1435 FreePool (Fragment.Bulk);\r
1436 Fragment.Bulk = NULL;\r
1437 }\r
1438\r
1439 if (HttpMsg->Headers != NULL) {\r
1440 FreePool (HttpMsg->Headers);\r
1441 HttpMsg->Headers = NULL;\r
1442 }\r
1443\r
1444 if (HttpInstance->CacheBody != NULL) {\r
1445 FreePool (HttpInstance->CacheBody);\r
1446 HttpInstance->CacheBody = NULL;\r
1447 }\r
1448\r
1449 if (HttpInstance->StatusCode >= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE) {\r
1450 Token->Status = EFI_HTTP_ERROR;\r
1451 } else {\r
1452 Token->Status = Status;\r
1453 }\r
1454\r
1455 gBS->SignalEvent (Token->Event);\r
1456\r
1457 return Status; \r
1458\r
1459}\r
1460\r
1461\r
1462/**\r
1463 The Response() function queues an HTTP response to this HTTP instance, similar to\r
1464 Receive() function in the EFI TCP driver. When the HTTP response is received successfully,\r
1465 or if there is an error, Status in token will be updated and Event will be signaled.\r
1466\r
1467 The HTTP driver will queue a receive token to the underlying TCP instance. When data\r
1468 is received in the underlying TCP instance, the data will be parsed and Token will\r
1469 be populated with the response data. If the data received from the remote host\r
1470 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting\r
1471 (asynchronously) for more data to be sent from the remote host before signaling\r
1472 Event in Token.\r
1473\r
1474 It is the responsibility of the caller to allocate a buffer for Body and specify the\r
1475 size in BodyLength. If the remote host provides a response that contains a content\r
1476 body, up to BodyLength bytes will be copied from the receive buffer into Body and\r
1477 BodyLength will be updated with the amount of bytes received and copied to Body. This\r
1478 allows the client to download a large file in chunks instead of into one contiguous\r
1479 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is\r
1480 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive\r
1481 token to underlying TCP instance. If data arrives in the receive buffer, up to\r
1482 BodyLength bytes of data will be copied to Body. The HTTP driver will then update\r
1483 BodyLength with the amount of bytes received and copied to Body.\r
1484\r
1485 If the HTTP driver does not have an open underlying TCP connection with the host\r
1486 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is\r
1487 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain\r
1488 an open TCP connection between client and host.\r
1489\r
1490 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
1491 @param[in] Token Pointer to storage containing HTTP response token.\r
1492\r
1493 @retval EFI_SUCCESS Allocation succeeded.\r
1494 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been\r
1495 initialized.\r
1496 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
1497 This is NULL.\r
1498 Token is NULL.\r
1499 Token->Message->Headers is NULL.\r
1500 Token->Message is NULL.\r
1501 Token->Message->Body is not NULL,\r
1502 Token->Message->BodyLength is non-zero, and\r
1503 Token->Message->Data is NULL, but a previous call to\r
1504 Response() has not been completed successfully.\r
1505 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.\r
1506 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host\r
1507 specified by response URL.\r
1508**/\r
1509EFI_STATUS\r
1510EFIAPI\r
1511EfiHttpResponse (\r
1512 IN EFI_HTTP_PROTOCOL *This,\r
1513 IN EFI_HTTP_TOKEN *Token\r
1514 )\r
1515{\r
1516 EFI_STATUS Status;\r
1517 EFI_HTTP_MESSAGE *HttpMsg;\r
1518 HTTP_PROTOCOL *HttpInstance;\r
1519 HTTP_TOKEN_WRAP *Wrap;\r
1520\r
1521 if ((This == NULL) || (Token == NULL)) {\r
1522 return EFI_INVALID_PARAMETER;\r
1523 }\r
1524\r
1525 HttpMsg = Token->Message;\r
1526 if (HttpMsg == NULL) {\r
1527 return EFI_INVALID_PARAMETER;\r
1528 }\r
1529 \r
1530 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
1531 ASSERT (HttpInstance != NULL);\r
1532\r
1533 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r
1534 return EFI_NOT_STARTED;\r
1535 }\r
1536\r
1537 //\r
1538 // Check whether the token already existed.\r
1539 //\r
1540 if (EFI_ERROR (NetMapIterate (&HttpInstance->RxTokens, HttpTokenExist, Token))) {\r
1541 return EFI_ACCESS_DENIED; \r
1542 }\r
1543\r
1544 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r
1545 if (Wrap == NULL) {\r
1546 return EFI_OUT_OF_RESOURCES;\r
1547 }\r
1548\r
1549 Wrap->HttpInstance = HttpInstance;\r
1550 Wrap->HttpToken = Token;\r
1551\r
1552 //\r
1553 // Notes: For Https, receive token wrapped in HTTP_TOKEN_WRAP is not used to \r
1554 // receive the https response. A special TlsRxToken is used for receiving TLS \r
1555 // related messages. It should be a blocking response.\r
1556 //\r
1557 if (!HttpInstance->UseHttps) {\r
1558 Status = HttpCreateTcpRxEvent (Wrap);\r
1559 if (EFI_ERROR (Status)) {\r
1560 goto Error;\r
1561 }\r
1562 }\r
1563\r
1564 Status = NetMapInsertTail (&HttpInstance->RxTokens, Token, Wrap);\r
1565 if (EFI_ERROR (Status)) {\r
1566 goto Error;\r
1567 }\r
1568\r
1569 //\r
1570 // If already have pending RxTokens, return directly.\r
1571 //\r
1572 if (NetMapGetCount (&HttpInstance->RxTokens) > 1) {\r
1573 return EFI_SUCCESS;\r
1574 }\r
1575\r
1576 return HttpResponseWorker (Wrap);\r
1577\r
1578Error:\r
1579 if (Wrap != NULL) {\r
1580 if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) {\r
1581 gBS->CloseEvent (Wrap->TcpWrap.Rx4Token.CompletionToken.Event);\r
1582 }\r
1583\r
1584 if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {\r
1585 gBS->CloseEvent (Wrap->TcpWrap.Rx6Token.CompletionToken.Event);\r
1586 }\r
1587 FreePool (Wrap);\r
1588 } \r
1589\r
1590 return Status; \r
1591}\r
1592\r
1593/**\r
1594 The Poll() function can be used by network drivers and applications to increase the\r
1595 rate that data packets are moved between the communication devices and the transmit\r
1596 and receive queues.\r
1597\r
1598 In some systems, the periodic timer event in the managed network driver may not poll\r
1599 the underlying communications device fast enough to transmit and/or receive all data\r
1600 packets without missing incoming packets or dropping outgoing packets. Drivers and\r
1601 applications that are experiencing packet loss should try calling the Poll() function\r
1602 more often.\r
1603\r
1604 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r
1605\r
1606 @retval EFI_SUCCESS Incoming or outgoing data was processed.\r
1607 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
1608 @retval EFI_INVALID_PARAMETER This is NULL.\r
1609 @retval EFI_NOT_READY No incoming or outgoing data is processed.\r
1610 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.\r
1611\r
1612**/\r
1613EFI_STATUS\r
1614EFIAPI\r
1615EfiHttpPoll (\r
1616 IN EFI_HTTP_PROTOCOL *This\r
1617 )\r
1618{\r
1619 EFI_STATUS Status;\r
1620 HTTP_PROTOCOL *HttpInstance;\r
1621\r
1622 if (This == NULL) {\r
1623 return EFI_INVALID_PARAMETER;\r
1624 }\r
1625\r
1626 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r
1627 ASSERT (HttpInstance != NULL);\r
1628\r
1629 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r
1630 return EFI_NOT_STARTED;\r
1631 }\r
1632 \r
1633 if (HttpInstance->LocalAddressIsIPv6) {\r
1634 if (HttpInstance->Tcp6 == NULL) {\r
1635 return EFI_NOT_STARTED;\r
1636 }\r
1637 Status = HttpInstance->Tcp6->Poll (HttpInstance->Tcp6);\r
1638 } else {\r
1639 if (HttpInstance->Tcp4 == NULL) {\r
1640 return EFI_NOT_STARTED;\r
1641 }\r
1642 Status = HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);\r
1643 }\r
1644 \r
1645 DispatchDpc ();\r
1646 \r
1647 return Status;\r
1648}\r