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