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