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