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