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