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