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