VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/NetworkPkg/HttpDxe/HttpProto.c@ 80721

Last change on this file since 80721 was 80721, checked in by vboxsync, 5 years ago

Devices/EFI/FirmwareNew: Start upgrade process to edk2-stable201908 (compiles on Windows and works to some extent), bugref:4643

  • Property svn:eol-style set to native
File size: 61.4 KB
Line 
1/** @file
2 Miscellaneous routines for HttpDxe driver.
3
4Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
6SPDX-License-Identifier: BSD-2-Clause-Patent
7
8**/
9
10#include "HttpDriver.h"
11
12/**
13 The common notify function used in HTTP driver.
14
15 @param[in] Event The event signaled.
16 @param[in] Context The context.
17
18**/
19VOID
20EFIAPI
21HttpCommonNotify (
22 IN EFI_EVENT Event,
23 IN VOID *Context
24 )
25{
26 if ((Event == NULL) || (Context == NULL)) {
27 return ;
28 }
29
30 *((BOOLEAN *) Context) = TRUE;
31}
32
33/**
34 The notify function associated with Tx4Token for Tcp4->Transmit() or Tx6Token for Tcp6->Transmit().
35
36 @param[in] Context The context.
37
38**/
39VOID
40EFIAPI
41HttpTcpTransmitNotifyDpc (
42 IN VOID *Context
43 )
44{
45 HTTP_TOKEN_WRAP *Wrap;
46 HTTP_PROTOCOL *HttpInstance;
47
48 if (Context == NULL) {
49 return ;
50 }
51
52 Wrap = (HTTP_TOKEN_WRAP *) Context;
53 HttpInstance = Wrap->HttpInstance;
54
55 if (!HttpInstance->LocalAddressIsIPv6) {
56 Wrap->HttpToken->Status = Wrap->TcpWrap.Tx4Token.CompletionToken.Status;
57 gBS->SignalEvent (Wrap->HttpToken->Event);
58
59 //
60 // Free resources.
61 //
62 if (Wrap->TcpWrap.Tx4Token.Packet.TxData->FragmentTable[0].FragmentBuffer != NULL) {
63 FreePool (Wrap->TcpWrap.Tx4Token.Packet.TxData->FragmentTable[0].FragmentBuffer);
64 }
65
66 if (Wrap->TcpWrap.Tx4Token.CompletionToken.Event != NULL) {
67 gBS->CloseEvent (Wrap->TcpWrap.Tx4Token.CompletionToken.Event);
68 }
69
70 } else {
71 Wrap->HttpToken->Status = Wrap->TcpWrap.Tx6Token.CompletionToken.Status;
72 gBS->SignalEvent (Wrap->HttpToken->Event);
73
74 //
75 // Free resources.
76 //
77 if (Wrap->TcpWrap.Tx6Token.Packet.TxData->FragmentTable[0].FragmentBuffer != NULL) {
78 FreePool (Wrap->TcpWrap.Tx6Token.Packet.TxData->FragmentTable[0].FragmentBuffer);
79 }
80
81 if (Wrap->TcpWrap.Tx6Token.CompletionToken.Event != NULL) {
82 gBS->CloseEvent (Wrap->TcpWrap.Tx6Token.CompletionToken.Event);
83 }
84 }
85
86
87 Wrap->TcpWrap.IsTxDone = TRUE;
88
89 //
90 // Check pending TxTokens and sent out.
91 //
92 NetMapIterate (&Wrap->HttpInstance->TxTokens, HttpTcpTransmit, NULL);
93
94}
95
96/**
97 Request HttpTcpTransmitNotifyDpc as a DPC at TPL_CALLBACK.
98
99 @param Event The receive event delivered to TCP for transmit.
100 @param Context Context for the callback.
101
102**/
103VOID
104EFIAPI
105HttpTcpTransmitNotify (
106 IN EFI_EVENT Event,
107 IN VOID *Context
108 )
109{
110 //
111 // Request HttpTcpTransmitNotifyDpc as a DPC at TPL_CALLBACK
112 //
113 QueueDpc (TPL_CALLBACK, HttpTcpTransmitNotifyDpc, Context);
114}
115
116/**
117 The notify function associated with Rx4Token for Tcp4->Receive () or Rx6Token for Tcp6->Receive().
118
119 @param[in] Context The context.
120
121**/
122VOID
123EFIAPI
124HttpTcpReceiveNotifyDpc (
125 IN VOID *Context
126 )
127{
128 HTTP_TOKEN_WRAP *Wrap;
129 NET_MAP_ITEM *Item;
130 UINTN Length;
131 EFI_STATUS Status;
132 HTTP_PROTOCOL *HttpInstance;
133 BOOLEAN UsingIpv6;
134
135 if (Context == NULL) {
136 return ;
137 }
138
139 Wrap = (HTTP_TOKEN_WRAP *) Context;
140 HttpInstance = Wrap->HttpInstance;
141 UsingIpv6 = HttpInstance->LocalAddressIsIPv6;
142
143 if (UsingIpv6) {
144 gBS->CloseEvent (Wrap->TcpWrap.Rx6Token.CompletionToken.Event);
145 Wrap->TcpWrap.Rx6Token.CompletionToken.Event = NULL;
146
147 if (EFI_ERROR (Wrap->TcpWrap.Rx6Token.CompletionToken.Status)) {
148 DEBUG ((EFI_D_ERROR, "HttpTcpReceiveNotifyDpc: %r!\n", Wrap->TcpWrap.Rx6Token.CompletionToken.Status));
149 Wrap->HttpToken->Status = Wrap->TcpWrap.Rx6Token.CompletionToken.Status;
150 gBS->SignalEvent (Wrap->HttpToken->Event);
151
152 Item = NetMapFindKey (&HttpInstance->RxTokens, Wrap->HttpToken);
153 if (Item != NULL) {
154 NetMapRemoveItem (&HttpInstance->RxTokens, Item, NULL);
155 }
156
157 FreePool (Wrap);
158 Wrap = NULL;
159
160 return ;
161 }
162
163 } else {
164 gBS->CloseEvent (Wrap->TcpWrap.Rx4Token.CompletionToken.Event);
165 Wrap->TcpWrap.Rx4Token.CompletionToken.Event = NULL;
166
167 if (EFI_ERROR (Wrap->TcpWrap.Rx4Token.CompletionToken.Status)) {
168 DEBUG ((EFI_D_ERROR, "HttpTcpReceiveNotifyDpc: %r!\n", Wrap->TcpWrap.Rx4Token.CompletionToken.Status));
169 Wrap->HttpToken->Status = Wrap->TcpWrap.Rx4Token.CompletionToken.Status;
170 gBS->SignalEvent (Wrap->HttpToken->Event);
171
172 Item = NetMapFindKey (&HttpInstance->RxTokens, Wrap->HttpToken);
173 if (Item != NULL) {
174 NetMapRemoveItem (&HttpInstance->RxTokens, Item, NULL);
175 }
176
177 FreePool (Wrap);
178 Wrap = NULL;
179
180 return ;
181 }
182 }
183
184 //
185 // Check whether we receive a complete HTTP message.
186 //
187 ASSERT (HttpInstance->MsgParser != NULL);
188 if (UsingIpv6) {
189 Length = (UINTN) Wrap->TcpWrap.Rx6Data.FragmentTable[0].FragmentLength;
190 } else {
191 Length = (UINTN) Wrap->TcpWrap.Rx4Data.FragmentTable[0].FragmentLength;
192 }
193
194 //
195 // Record the CallbackData data.
196 //
197 HttpInstance->CallbackData.Wrap = (VOID *) Wrap;
198 HttpInstance->CallbackData.ParseData = Wrap->HttpToken->Message->Body;
199 HttpInstance->CallbackData.ParseDataLength = Length;
200
201 //
202 // Parse Body with CallbackData data.
203 //
204 Status = HttpParseMessageBody (
205 HttpInstance->MsgParser,
206 Length,
207 Wrap->HttpToken->Message->Body
208 );
209 if (EFI_ERROR (Status)) {
210 return ;
211 }
212
213 if (HttpIsMessageComplete (HttpInstance->MsgParser)) {
214 //
215 // Free the MsgParse since we already have a full HTTP message.
216 //
217 HttpFreeMsgParser (HttpInstance->MsgParser);
218 HttpInstance->MsgParser = NULL;
219 }
220
221 Wrap->HttpToken->Message->BodyLength = Length;
222 ASSERT (HttpInstance->CacheBody == NULL);
223 //
224 // We receive part of header of next HTTP msg.
225 //
226 if (HttpInstance->NextMsg != NULL) {
227 Wrap->HttpToken->Message->BodyLength = HttpInstance->NextMsg -
228 (CHAR8 *) Wrap->HttpToken->Message->Body;
229 HttpInstance->CacheLen = Length - Wrap->HttpToken->Message->BodyLength;
230 if (HttpInstance->CacheLen != 0) {
231 HttpInstance->CacheBody = AllocateZeroPool (HttpInstance->CacheLen);
232 if (HttpInstance->CacheBody == NULL) {
233 return ;
234 }
235 CopyMem (HttpInstance->CacheBody, HttpInstance->NextMsg, HttpInstance->CacheLen);
236 HttpInstance->NextMsg = HttpInstance->CacheBody;
237 HttpInstance->CacheOffset = 0;
238 }
239 }
240
241 Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);
242 if (Item != NULL) {
243 NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);
244 }
245
246
247 Wrap->TcpWrap.IsRxDone = TRUE;
248 if (UsingIpv6) {
249 Wrap->HttpToken->Status = Wrap->TcpWrap.Rx6Token.CompletionToken.Status;
250 } else {
251 Wrap->HttpToken->Status = Wrap->TcpWrap.Rx4Token.CompletionToken.Status;
252 }
253
254
255 gBS->SignalEvent (Wrap->HttpToken->Event);
256
257 //
258 // Check pending RxTokens and receive the HTTP message.
259 //
260 NetMapIterate (&Wrap->HttpInstance->RxTokens, HttpTcpReceive, NULL);
261
262 FreePool (Wrap);
263 Wrap = NULL;
264}
265
266/**
267 Request HttpTcpReceiveNotifyDpc as a DPC at TPL_CALLBACK.
268
269 @param Event The receive event delivered to TCP for receive.
270 @param Context Context for the callback.
271
272**/
273VOID
274EFIAPI
275HttpTcpReceiveNotify (
276 IN EFI_EVENT Event,
277 IN VOID *Context
278 )
279{
280 //
281 // Request HttpTcpTransmitNotifyDpc as a DPC at TPL_CALLBACK
282 //
283 QueueDpc (TPL_CALLBACK, HttpTcpReceiveNotifyDpc, Context);
284}
285
286/**
287 Create events for the TCP connection token and TCP close token.
288
289 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
290
291 @retval EFI_SUCCESS The events are created successfully.
292 @retval others Other error as indicated.
293
294**/
295EFI_STATUS
296HttpCreateTcpConnCloseEvent (
297 IN HTTP_PROTOCOL *HttpInstance
298 )
299{
300 EFI_STATUS Status;
301
302 if (!HttpInstance->LocalAddressIsIPv6) {
303 //
304 // Create events for variuos asynchronous operations.
305 //
306 Status = gBS->CreateEvent (
307 EVT_NOTIFY_SIGNAL,
308 TPL_NOTIFY,
309 HttpCommonNotify,
310 &HttpInstance->IsTcp4ConnDone,
311 &HttpInstance->Tcp4ConnToken.CompletionToken.Event
312 );
313 if (EFI_ERROR (Status)) {
314 goto ERROR;
315 }
316
317 //
318 // Initialize Tcp4CloseToken
319 //
320 Status = gBS->CreateEvent (
321 EVT_NOTIFY_SIGNAL,
322 TPL_NOTIFY,
323 HttpCommonNotify,
324 &HttpInstance->IsTcp4CloseDone,
325 &HttpInstance->Tcp4CloseToken.CompletionToken.Event
326 );
327 if (EFI_ERROR (Status)) {
328 goto ERROR;
329 }
330
331 } else {
332 //
333 // Create events for variuos asynchronous operations.
334 //
335 Status = gBS->CreateEvent (
336 EVT_NOTIFY_SIGNAL,
337 TPL_NOTIFY,
338 HttpCommonNotify,
339 &HttpInstance->IsTcp6ConnDone,
340 &HttpInstance->Tcp6ConnToken.CompletionToken.Event
341 );
342 if (EFI_ERROR (Status)) {
343 goto ERROR;
344 }
345
346 //
347 // Initialize Tcp6CloseToken
348 //
349 Status = gBS->CreateEvent (
350 EVT_NOTIFY_SIGNAL,
351 TPL_NOTIFY,
352 HttpCommonNotify,
353 &HttpInstance->IsTcp6CloseDone,
354 &HttpInstance->Tcp6CloseToken.CompletionToken.Event
355 );
356 if (EFI_ERROR (Status)) {
357 goto ERROR;
358 }
359 }
360
361 return EFI_SUCCESS;
362
363ERROR:
364 //
365 // Error handling
366 //
367 HttpCloseTcpConnCloseEvent (HttpInstance);
368
369 return Status;
370}
371
372
373/**
374 Close events in the TCP connection token and TCP close token.
375
376 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
377
378**/
379VOID
380HttpCloseTcpConnCloseEvent (
381 IN HTTP_PROTOCOL *HttpInstance
382 )
383{
384 ASSERT (HttpInstance != NULL);
385
386 if (HttpInstance->LocalAddressIsIPv6) {
387 if (NULL != HttpInstance->Tcp6ConnToken.CompletionToken.Event) {
388 gBS->CloseEvent (HttpInstance->Tcp6ConnToken.CompletionToken.Event);
389 HttpInstance->Tcp6ConnToken.CompletionToken.Event = NULL;
390 }
391
392 if (NULL != HttpInstance->Tcp6CloseToken.CompletionToken.Event) {
393 gBS->CloseEvent(HttpInstance->Tcp6CloseToken.CompletionToken.Event);
394 HttpInstance->Tcp6CloseToken.CompletionToken.Event = NULL;
395 }
396
397 } else {
398 if (NULL != HttpInstance->Tcp4ConnToken.CompletionToken.Event) {
399 gBS->CloseEvent (HttpInstance->Tcp4ConnToken.CompletionToken.Event);
400 HttpInstance->Tcp4ConnToken.CompletionToken.Event = NULL;
401 }
402
403 if (NULL != HttpInstance->Tcp4CloseToken.CompletionToken.Event) {
404 gBS->CloseEvent(HttpInstance->Tcp4CloseToken.CompletionToken.Event);
405 HttpInstance->Tcp4CloseToken.CompletionToken.Event = NULL;
406 }
407 }
408
409}
410
411/**
412 Create event for the TCP transmit token.
413
414 @param[in] Wrap Point to HTTP token's wrap data.
415
416 @retval EFI_SUCCESS The events is created successfully.
417 @retval others Other error as indicated.
418
419**/
420EFI_STATUS
421HttpCreateTcpTxEvent (
422 IN HTTP_TOKEN_WRAP *Wrap
423 )
424{
425 EFI_STATUS Status;
426 HTTP_PROTOCOL *HttpInstance;
427 HTTP_TCP_TOKEN_WRAP *TcpWrap;
428
429 HttpInstance = Wrap->HttpInstance;
430 TcpWrap = &Wrap->TcpWrap;
431
432 if (!HttpInstance->LocalAddressIsIPv6) {
433 Status = gBS->CreateEvent (
434 EVT_NOTIFY_SIGNAL,
435 TPL_NOTIFY,
436 HttpTcpTransmitNotify,
437 Wrap,
438 &TcpWrap->Tx4Token.CompletionToken.Event
439 );
440 if (EFI_ERROR (Status)) {
441 return Status;
442 }
443
444 TcpWrap->Tx4Data.Push = TRUE;
445 TcpWrap->Tx4Data.Urgent = FALSE;
446 TcpWrap->Tx4Data.FragmentCount = 1;
447 TcpWrap->Tx4Token.Packet.TxData = &Wrap->TcpWrap.Tx4Data;
448 TcpWrap->Tx4Token.CompletionToken.Status = EFI_NOT_READY;
449
450 } else {
451 Status = gBS->CreateEvent (
452 EVT_NOTIFY_SIGNAL,
453 TPL_NOTIFY,
454 HttpTcpTransmitNotify,
455 Wrap,
456 &TcpWrap->Tx6Token.CompletionToken.Event
457 );
458 if (EFI_ERROR (Status)) {
459 return Status;
460 }
461
462 TcpWrap->Tx6Data.Push = TRUE;
463 TcpWrap->Tx6Data.Urgent = FALSE;
464 TcpWrap->Tx6Data.FragmentCount = 1;
465 TcpWrap->Tx6Token.Packet.TxData = &Wrap->TcpWrap.Tx6Data;
466 TcpWrap->Tx6Token.CompletionToken.Status =EFI_NOT_READY;
467
468 }
469
470 return EFI_SUCCESS;
471}
472
473/**
474 Create event for the TCP receive token which is used to receive HTTP header.
475
476 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
477
478 @retval EFI_SUCCESS The events is created successfully.
479 @retval others Other error as indicated.
480
481**/
482EFI_STATUS
483HttpCreateTcpRxEventForHeader (
484 IN HTTP_PROTOCOL *HttpInstance
485 )
486{
487 EFI_STATUS Status;
488
489 if (!HttpInstance->LocalAddressIsIPv6) {
490 Status = gBS->CreateEvent (
491 EVT_NOTIFY_SIGNAL,
492 TPL_NOTIFY,
493 HttpCommonNotify,
494 &HttpInstance->IsRxDone,
495 &HttpInstance->Rx4Token.CompletionToken.Event
496 );
497 if (EFI_ERROR (Status)) {
498 return Status;
499 }
500
501 HttpInstance->Rx4Data.FragmentCount = 1;
502 HttpInstance->Rx4Token.Packet.RxData = &HttpInstance->Rx4Data;
503 HttpInstance->Rx4Token.CompletionToken.Status = EFI_NOT_READY;
504
505 } else {
506 Status = gBS->CreateEvent (
507 EVT_NOTIFY_SIGNAL,
508 TPL_NOTIFY,
509 HttpCommonNotify,
510 &HttpInstance->IsRxDone,
511 &HttpInstance->Rx6Token.CompletionToken.Event
512 );
513 if (EFI_ERROR (Status)) {
514 return Status;
515 }
516
517 HttpInstance->Rx6Data.FragmentCount =1;
518 HttpInstance->Rx6Token.Packet.RxData = &HttpInstance->Rx6Data;
519 HttpInstance->Rx6Token.CompletionToken.Status = EFI_NOT_READY;
520
521 }
522
523
524 return EFI_SUCCESS;
525}
526
527/**
528 Create event for the TCP receive token which is used to receive HTTP body.
529
530 @param[in] Wrap Point to HTTP token's wrap data.
531
532 @retval EFI_SUCCESS The events is created successfully.
533 @retval others Other error as indicated.
534
535**/
536EFI_STATUS
537HttpCreateTcpRxEvent (
538 IN HTTP_TOKEN_WRAP *Wrap
539 )
540{
541 EFI_STATUS Status;
542 HTTP_PROTOCOL *HttpInstance;
543 HTTP_TCP_TOKEN_WRAP *TcpWrap;
544
545 HttpInstance = Wrap->HttpInstance;
546 TcpWrap = &Wrap->TcpWrap;
547 if (!HttpInstance->LocalAddressIsIPv6) {
548 Status = gBS->CreateEvent (
549 EVT_NOTIFY_SIGNAL,
550 TPL_NOTIFY,
551 HttpTcpReceiveNotify,
552 Wrap,
553 &TcpWrap->Rx4Token.CompletionToken.Event
554 );
555 if (EFI_ERROR (Status)) {
556 return Status;
557 }
558
559 TcpWrap->Rx4Data.FragmentCount = 1;
560 TcpWrap->Rx4Token.Packet.RxData = &Wrap->TcpWrap.Rx4Data;
561 TcpWrap->Rx4Token.CompletionToken.Status = EFI_NOT_READY;
562
563 } else {
564 Status = gBS->CreateEvent (
565 EVT_NOTIFY_SIGNAL,
566 TPL_NOTIFY,
567 HttpTcpReceiveNotify,
568 Wrap,
569 &TcpWrap->Rx6Token.CompletionToken.Event
570 );
571 if (EFI_ERROR (Status)) {
572 return Status;
573 }
574
575 TcpWrap->Rx6Data.FragmentCount = 1;
576 TcpWrap->Rx6Token.Packet.RxData = &Wrap->TcpWrap.Rx6Data;
577 TcpWrap->Rx6Token.CompletionToken.Status = EFI_NOT_READY;
578 }
579
580 return EFI_SUCCESS;
581}
582
583/**
584 Close Events for Tcp Receive Tokens for HTTP body and HTTP header.
585
586 @param[in] Wrap Pointer to HTTP token's wrap data.
587
588**/
589VOID
590HttpCloseTcpRxEvent (
591 IN HTTP_TOKEN_WRAP *Wrap
592 )
593{
594 HTTP_PROTOCOL *HttpInstance;
595
596 ASSERT (Wrap != NULL);
597 HttpInstance = Wrap->HttpInstance;
598
599 if (HttpInstance->LocalAddressIsIPv6) {
600 if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {
601 gBS->CloseEvent (Wrap->TcpWrap.Rx6Token.CompletionToken.Event);
602 }
603
604 if (HttpInstance->Rx6Token.CompletionToken.Event != NULL) {
605 gBS->CloseEvent (HttpInstance->Rx6Token.CompletionToken.Event);
606 HttpInstance->Rx6Token.CompletionToken.Event = NULL;
607 }
608 } else {
609 if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) {
610 gBS->CloseEvent (Wrap->TcpWrap.Rx4Token.CompletionToken.Event);
611 }
612
613 if (HttpInstance->Rx4Token.CompletionToken.Event != NULL) {
614 gBS->CloseEvent (HttpInstance->Rx4Token.CompletionToken.Event);
615 HttpInstance->Rx4Token.CompletionToken.Event = NULL;
616 }
617 }
618}
619
620/**
621 Intiialize the HTTP_PROTOCOL structure to the unconfigured state.
622
623 @param[in, out] HttpInstance Pointer to HTTP_PROTOCOL structure.
624 @param[in] IpVersion Indicate us TCP4 protocol or TCP6 protocol.
625
626 @retval EFI_SUCCESS HTTP_PROTOCOL structure is initialized successfully.
627 @retval Others Other error as indicated.
628
629**/
630EFI_STATUS
631HttpInitProtocol (
632 IN OUT HTTP_PROTOCOL *HttpInstance,
633 IN BOOLEAN IpVersion
634 )
635{
636 EFI_STATUS Status;
637 VOID *Interface;
638 BOOLEAN UsingIpv6;
639
640 ASSERT (HttpInstance != NULL);
641 UsingIpv6 = IpVersion;
642
643 if (!UsingIpv6) {
644 //
645 // Create TCP4 child.
646 //
647 Status = NetLibCreateServiceChild (
648 HttpInstance->Service->ControllerHandle,
649 HttpInstance->Service->Ip4DriverBindingHandle,
650 &gEfiTcp4ServiceBindingProtocolGuid,
651 &HttpInstance->Tcp4ChildHandle
652 );
653
654 if (EFI_ERROR (Status)) {
655 goto ON_ERROR;
656 }
657
658 Status = gBS->OpenProtocol (
659 HttpInstance->Tcp4ChildHandle,
660 &gEfiTcp4ProtocolGuid,
661 (VOID **) &Interface,
662 HttpInstance->Service->Ip4DriverBindingHandle,
663 HttpInstance->Service->ControllerHandle,
664 EFI_OPEN_PROTOCOL_BY_DRIVER
665 );
666
667 if (EFI_ERROR (Status)) {
668 goto ON_ERROR;
669 }
670
671 Status = gBS->OpenProtocol (
672 HttpInstance->Tcp4ChildHandle,
673 &gEfiTcp4ProtocolGuid,
674 (VOID **) &HttpInstance->Tcp4,
675 HttpInstance->Service->Ip4DriverBindingHandle,
676 HttpInstance->Handle,
677 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
678 );
679 if (EFI_ERROR(Status)) {
680 goto ON_ERROR;
681 }
682
683 Status = gBS->OpenProtocol (
684 HttpInstance->Service->Tcp4ChildHandle,
685 &gEfiTcp4ProtocolGuid,
686 (VOID **) &Interface,
687 HttpInstance->Service->Ip4DriverBindingHandle,
688 HttpInstance->Handle,
689 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
690 );
691 if (EFI_ERROR(Status)) {
692 goto ON_ERROR;
693 }
694 } else {
695 //
696 // Create TCP6 Child.
697 //
698 Status = NetLibCreateServiceChild (
699 HttpInstance->Service->ControllerHandle,
700 HttpInstance->Service->Ip6DriverBindingHandle,
701 &gEfiTcp6ServiceBindingProtocolGuid,
702 &HttpInstance->Tcp6ChildHandle
703 );
704
705 if (EFI_ERROR (Status)) {
706 goto ON_ERROR;
707 }
708
709 Status = gBS->OpenProtocol (
710 HttpInstance->Tcp6ChildHandle,
711 &gEfiTcp6ProtocolGuid,
712 (VOID **) &Interface,
713 HttpInstance->Service->Ip6DriverBindingHandle,
714 HttpInstance->Service->ControllerHandle,
715 EFI_OPEN_PROTOCOL_BY_DRIVER
716 );
717
718 if (EFI_ERROR (Status)) {
719 goto ON_ERROR;
720 }
721
722 Status = gBS->OpenProtocol (
723 HttpInstance->Tcp6ChildHandle,
724 &gEfiTcp6ProtocolGuid,
725 (VOID **) &HttpInstance->Tcp6,
726 HttpInstance->Service->Ip6DriverBindingHandle,
727 HttpInstance->Handle,
728 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
729 );
730
731 if (EFI_ERROR(Status)) {
732 goto ON_ERROR;
733 }
734
735 Status = gBS->OpenProtocol (
736 HttpInstance->Service->Tcp6ChildHandle,
737 &gEfiTcp6ProtocolGuid,
738 (VOID **) &Interface,
739 HttpInstance->Service->Ip6DriverBindingHandle,
740 HttpInstance->Handle,
741 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
742 );
743
744 if (EFI_ERROR(Status)) {
745 goto ON_ERROR;
746 }
747 }
748
749 HttpInstance->Url = AllocateZeroPool (HTTP_URL_BUFFER_LEN);
750 if (HttpInstance->Url == NULL) {
751 Status = EFI_OUT_OF_RESOURCES;
752 goto ON_ERROR;
753 }
754
755 return EFI_SUCCESS;
756
757ON_ERROR:
758
759 if (HttpInstance->Tcp4ChildHandle != NULL) {
760 gBS->CloseProtocol (
761 HttpInstance->Tcp4ChildHandle,
762 &gEfiTcp4ProtocolGuid,
763 HttpInstance->Service->Ip4DriverBindingHandle,
764 HttpInstance->Service->ControllerHandle
765 );
766
767 gBS->CloseProtocol (
768 HttpInstance->Tcp4ChildHandle,
769 &gEfiTcp4ProtocolGuid,
770 HttpInstance->Service->Ip4DriverBindingHandle,
771 HttpInstance->Handle
772 );
773
774 NetLibDestroyServiceChild (
775 HttpInstance->Service->ControllerHandle,
776 HttpInstance->Service->Ip4DriverBindingHandle,
777 &gEfiTcp4ServiceBindingProtocolGuid,
778 HttpInstance->Tcp4ChildHandle
779 );
780 }
781
782 if (HttpInstance->Service->Tcp4ChildHandle != NULL) {
783 gBS->CloseProtocol (
784 HttpInstance->Service->Tcp4ChildHandle,
785 &gEfiTcp4ProtocolGuid,
786 HttpInstance->Service->Ip4DriverBindingHandle,
787 HttpInstance->Handle
788 );
789 }
790
791 if (HttpInstance->Tcp6ChildHandle != NULL) {
792 gBS->CloseProtocol (
793 HttpInstance->Tcp6ChildHandle,
794 &gEfiTcp6ProtocolGuid,
795 HttpInstance->Service->Ip6DriverBindingHandle,
796 HttpInstance->Service->ControllerHandle
797 );
798
799 gBS->CloseProtocol (
800 HttpInstance->Tcp6ChildHandle,
801 &gEfiTcp6ProtocolGuid,
802 HttpInstance->Service->Ip6DriverBindingHandle,
803 HttpInstance->Handle
804 );
805
806 NetLibDestroyServiceChild (
807 HttpInstance->Service->ControllerHandle,
808 HttpInstance->Service->Ip6DriverBindingHandle,
809 &gEfiTcp6ServiceBindingProtocolGuid,
810 HttpInstance->Tcp6ChildHandle
811 );
812 }
813
814 if (HttpInstance->Service->Tcp6ChildHandle != NULL) {
815 gBS->CloseProtocol (
816 HttpInstance->Service->Tcp6ChildHandle,
817 &gEfiTcp6ProtocolGuid,
818 HttpInstance->Service->Ip6DriverBindingHandle,
819 HttpInstance->Handle
820 );
821 }
822
823 return EFI_UNSUPPORTED;
824
825}
826
827/**
828 Clean up the HTTP child, release all the resources used by it.
829
830 @param[in] HttpInstance The HTTP child to clean up.
831
832**/
833VOID
834HttpCleanProtocol (
835 IN HTTP_PROTOCOL *HttpInstance
836 )
837{
838 HttpCloseConnection (HttpInstance);
839
840 HttpCloseTcpConnCloseEvent (HttpInstance);
841
842 if (HttpInstance->TimeoutEvent != NULL) {
843 gBS->CloseEvent (HttpInstance->TimeoutEvent);
844 HttpInstance->TimeoutEvent = NULL;
845 }
846
847 if (HttpInstance->CacheBody != NULL) {
848 FreePool (HttpInstance->CacheBody);
849 HttpInstance->CacheBody = NULL;
850 HttpInstance->NextMsg = NULL;
851 }
852
853 if (HttpInstance->RemoteHost != NULL) {
854 FreePool (HttpInstance->RemoteHost);
855 HttpInstance->RemoteHost = NULL;
856 }
857
858 if (HttpInstance->MsgParser != NULL) {
859 HttpFreeMsgParser (HttpInstance->MsgParser);
860 HttpInstance->MsgParser = NULL;
861 }
862
863 if (HttpInstance->Url != NULL) {
864 FreePool (HttpInstance->Url);
865 HttpInstance->Url = NULL;
866 }
867
868 NetMapClean (&HttpInstance->TxTokens);
869 NetMapClean (&HttpInstance->RxTokens);
870
871 if (HttpInstance->TlsSb != NULL && HttpInstance->TlsChildHandle != NULL) {
872 //
873 // Destroy the TLS instance.
874 //
875 HttpInstance->TlsSb->DestroyChild (HttpInstance->TlsSb, HttpInstance->TlsChildHandle);
876 }
877
878 if (HttpInstance->Tcp4ChildHandle != NULL) {
879 gBS->CloseProtocol (
880 HttpInstance->Tcp4ChildHandle,
881 &gEfiTcp4ProtocolGuid,
882 HttpInstance->Service->Ip4DriverBindingHandle,
883 HttpInstance->Service->ControllerHandle
884 );
885
886 gBS->CloseProtocol (
887 HttpInstance->Tcp4ChildHandle,
888 &gEfiTcp4ProtocolGuid,
889 HttpInstance->Service->Ip4DriverBindingHandle,
890 HttpInstance->Handle
891 );
892
893 NetLibDestroyServiceChild (
894 HttpInstance->Service->ControllerHandle,
895 HttpInstance->Service->Ip4DriverBindingHandle,
896 &gEfiTcp4ServiceBindingProtocolGuid,
897 HttpInstance->Tcp4ChildHandle
898 );
899 }
900
901 if (HttpInstance->Service->Tcp4ChildHandle != NULL) {
902 gBS->CloseProtocol (
903 HttpInstance->Service->Tcp4ChildHandle,
904 &gEfiTcp4ProtocolGuid,
905 HttpInstance->Service->Ip4DriverBindingHandle,
906 HttpInstance->Handle
907 );
908 }
909
910 if (HttpInstance->Tcp6ChildHandle != NULL) {
911 gBS->CloseProtocol (
912 HttpInstance->Tcp6ChildHandle,
913 &gEfiTcp6ProtocolGuid,
914 HttpInstance->Service->Ip6DriverBindingHandle,
915 HttpInstance->Service->ControllerHandle
916 );
917
918 gBS->CloseProtocol (
919 HttpInstance->Tcp6ChildHandle,
920 &gEfiTcp6ProtocolGuid,
921 HttpInstance->Service->Ip6DriverBindingHandle,
922 HttpInstance->Handle
923 );
924
925 NetLibDestroyServiceChild (
926 HttpInstance->Service->ControllerHandle,
927 HttpInstance->Service->Ip6DriverBindingHandle,
928 &gEfiTcp6ServiceBindingProtocolGuid,
929 HttpInstance->Tcp6ChildHandle
930 );
931 }
932
933 if (HttpInstance->Service->Tcp6ChildHandle != NULL) {
934 gBS->CloseProtocol (
935 HttpInstance->Service->Tcp6ChildHandle,
936 &gEfiTcp6ProtocolGuid,
937 HttpInstance->Service->Ip6DriverBindingHandle,
938 HttpInstance->Handle
939 );
940 }
941
942 TlsCloseTxRxEvent (HttpInstance);
943}
944
945/**
946 Establish TCP connection with HTTP server.
947
948 @param[in] HttpInstance The HTTP instance private data.
949
950 @retval EFI_SUCCESS The TCP connection is established.
951 @retval Others Other error as indicated.
952
953**/
954EFI_STATUS
955HttpCreateConnection (
956 IN HTTP_PROTOCOL *HttpInstance
957 )
958{
959 EFI_STATUS Status;
960
961 //
962 // Connect to Http server
963 //
964 if (!HttpInstance->LocalAddressIsIPv6) {
965 HttpInstance->IsTcp4ConnDone = FALSE;
966 HttpInstance->Tcp4ConnToken.CompletionToken.Status = EFI_NOT_READY;
967 Status = HttpInstance->Tcp4->Connect (HttpInstance->Tcp4, &HttpInstance->Tcp4ConnToken);
968 if (EFI_ERROR (Status)) {
969 DEBUG ((EFI_D_ERROR, "HttpCreateConnection: Tcp4->Connect() = %r\n", Status));
970 return Status;
971 }
972
973 while (!HttpInstance->IsTcp4ConnDone) {
974 HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);
975 }
976
977 Status = HttpInstance->Tcp4ConnToken.CompletionToken.Status;
978
979 } else {
980 HttpInstance->IsTcp6ConnDone = FALSE;
981 HttpInstance->Tcp6ConnToken.CompletionToken.Status = EFI_NOT_READY;
982 Status = HttpInstance->Tcp6->Connect (HttpInstance->Tcp6, &HttpInstance->Tcp6ConnToken);
983 if (EFI_ERROR (Status)) {
984 DEBUG ((EFI_D_ERROR, "HttpCreateConnection: Tcp6->Connect() = %r\n", Status));
985 return Status;
986 }
987
988 while(!HttpInstance->IsTcp6ConnDone) {
989 HttpInstance->Tcp6->Poll (HttpInstance->Tcp6);
990 }
991
992 Status = HttpInstance->Tcp6ConnToken.CompletionToken.Status;
993 }
994
995 if (!EFI_ERROR (Status)) {
996 HttpInstance->State = HTTP_STATE_TCP_CONNECTED;
997 }
998
999 return Status;
1000}
1001
1002/**
1003 Close existing TCP connection.
1004
1005 @param[in] HttpInstance The HTTP instance private data.
1006
1007 @retval EFI_SUCCESS The TCP connection is closed.
1008 @retval Others Other error as indicated.
1009
1010**/
1011EFI_STATUS
1012HttpCloseConnection (
1013 IN HTTP_PROTOCOL *HttpInstance
1014 )
1015{
1016 EFI_STATUS Status;
1017
1018 if (HttpInstance->State == HTTP_STATE_TCP_CONNECTED) {
1019
1020 if (HttpInstance->LocalAddressIsIPv6) {
1021 HttpInstance->Tcp6CloseToken.AbortOnClose = TRUE;
1022 HttpInstance->IsTcp6CloseDone = FALSE;
1023 Status = HttpInstance->Tcp6->Close (HttpInstance->Tcp6, &HttpInstance->Tcp6CloseToken);
1024 if (EFI_ERROR (Status)) {
1025 return Status;
1026 }
1027
1028 while (!HttpInstance->IsTcp6CloseDone) {
1029 HttpInstance->Tcp6->Poll (HttpInstance->Tcp6);
1030 }
1031
1032 } else {
1033 HttpInstance->Tcp4CloseToken.AbortOnClose = TRUE;
1034 HttpInstance->IsTcp4CloseDone = FALSE;
1035 Status = HttpInstance->Tcp4->Close (HttpInstance->Tcp4, &HttpInstance->Tcp4CloseToken);
1036 if (EFI_ERROR (Status)) {
1037 return Status;
1038 }
1039
1040 while (!HttpInstance->IsTcp4CloseDone) {
1041 HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);
1042 }
1043 }
1044
1045 }
1046
1047 HttpInstance->State = HTTP_STATE_TCP_CLOSED;
1048 return EFI_SUCCESS;
1049}
1050
1051/**
1052 Configure TCP4 protocol child.
1053
1054 @param[in] HttpInstance The HTTP instance private data.
1055 @param[in] Wrap The HTTP token's wrap data.
1056
1057 @retval EFI_SUCCESS The TCP4 protocol child is configured.
1058 @retval Others Other error as indicated.
1059
1060**/
1061EFI_STATUS
1062HttpConfigureTcp4 (
1063 IN HTTP_PROTOCOL *HttpInstance,
1064 IN HTTP_TOKEN_WRAP *Wrap
1065 )
1066{
1067 EFI_STATUS Status;
1068 EFI_TCP4_CONFIG_DATA *Tcp4CfgData;
1069 EFI_TCP4_ACCESS_POINT *Tcp4AP;
1070 EFI_TCP4_OPTION *Tcp4Option;
1071
1072 ASSERT (HttpInstance != NULL);
1073
1074
1075 Tcp4CfgData = &HttpInstance->Tcp4CfgData;
1076 ZeroMem (Tcp4CfgData, sizeof (EFI_TCP4_CONFIG_DATA));
1077
1078 Tcp4CfgData->TypeOfService = HTTP_TOS_DEAULT;
1079 Tcp4CfgData->TimeToLive = HTTP_TTL_DEAULT;
1080 Tcp4CfgData->ControlOption = &HttpInstance->Tcp4Option;
1081
1082 Tcp4AP = &Tcp4CfgData->AccessPoint;
1083 Tcp4AP->UseDefaultAddress = HttpInstance->IPv4Node.UseDefaultAddress;
1084 if (!Tcp4AP->UseDefaultAddress) {
1085 IP4_COPY_ADDRESS (&Tcp4AP->StationAddress, &HttpInstance->IPv4Node.LocalAddress);
1086 IP4_COPY_ADDRESS (&Tcp4AP->SubnetMask, &HttpInstance->IPv4Node.LocalSubnet);
1087 }
1088
1089 Tcp4AP->StationPort = HttpInstance->IPv4Node.LocalPort;
1090 Tcp4AP->RemotePort = HttpInstance->RemotePort;
1091 Tcp4AP->ActiveFlag = TRUE;
1092 IP4_COPY_ADDRESS (&Tcp4AP->RemoteAddress, &HttpInstance->RemoteAddr);
1093
1094 Tcp4Option = Tcp4CfgData->ControlOption;
1095 Tcp4Option->ReceiveBufferSize = HTTP_BUFFER_SIZE_DEAULT;
1096 Tcp4Option->SendBufferSize = HTTP_BUFFER_SIZE_DEAULT;
1097 Tcp4Option->MaxSynBackLog = HTTP_MAX_SYN_BACK_LOG;
1098 Tcp4Option->ConnectionTimeout = HTTP_CONNECTION_TIMEOUT;
1099 Tcp4Option->DataRetries = HTTP_DATA_RETRIES;
1100 Tcp4Option->FinTimeout = HTTP_FIN_TIMEOUT;
1101 Tcp4Option->KeepAliveProbes = HTTP_KEEP_ALIVE_PROBES;
1102 Tcp4Option->KeepAliveTime = HTTP_KEEP_ALIVE_TIME;
1103 Tcp4Option->KeepAliveInterval = HTTP_KEEP_ALIVE_INTERVAL;
1104 Tcp4Option->EnableNagle = TRUE;
1105 Tcp4CfgData->ControlOption = Tcp4Option;
1106
1107 Status = HttpInstance->Tcp4->Configure (HttpInstance->Tcp4, Tcp4CfgData);
1108 if (EFI_ERROR (Status)) {
1109 DEBUG ((EFI_D_ERROR, "HttpConfigureTcp4 - %r\n", Status));
1110 return Status;
1111 }
1112
1113 Status = HttpCreateTcpConnCloseEvent (HttpInstance);
1114 if (EFI_ERROR (Status)) {
1115 return Status;
1116 }
1117
1118 Status = HttpCreateTcpTxEvent (Wrap);
1119 if (EFI_ERROR (Status)) {
1120 return Status;
1121 }
1122
1123 HttpInstance->State = HTTP_STATE_TCP_CONFIGED;
1124
1125 return EFI_SUCCESS;
1126}
1127
1128/**
1129 Configure TCP6 protocol child.
1130
1131 @param[in] HttpInstance The HTTP instance private data.
1132 @param[in] Wrap The HTTP token's wrap data.
1133
1134 @retval EFI_SUCCESS The TCP6 protocol child is configured.
1135 @retval Others Other error as indicated.
1136
1137**/
1138EFI_STATUS
1139HttpConfigureTcp6 (
1140 IN HTTP_PROTOCOL *HttpInstance,
1141 IN HTTP_TOKEN_WRAP *Wrap
1142 )
1143{
1144 EFI_STATUS Status;
1145 EFI_TCP6_CONFIG_DATA *Tcp6CfgData;
1146 EFI_TCP6_ACCESS_POINT *Tcp6Ap;
1147 EFI_TCP6_OPTION *Tcp6Option;
1148
1149 ASSERT (HttpInstance != NULL);
1150
1151 Tcp6CfgData = &HttpInstance->Tcp6CfgData;
1152 ZeroMem (Tcp6CfgData, sizeof (EFI_TCP6_CONFIG_DATA));
1153
1154 Tcp6CfgData->TrafficClass = 0;
1155 Tcp6CfgData->HopLimit = 255;
1156 Tcp6CfgData->ControlOption = &HttpInstance->Tcp6Option;
1157
1158 Tcp6Ap = &Tcp6CfgData->AccessPoint;
1159 Tcp6Ap->ActiveFlag = TRUE;
1160 Tcp6Ap->StationPort = HttpInstance->Ipv6Node.LocalPort;
1161 Tcp6Ap->RemotePort = HttpInstance->RemotePort;
1162 IP6_COPY_ADDRESS (&Tcp6Ap->StationAddress, &HttpInstance->Ipv6Node.LocalAddress);
1163 IP6_COPY_ADDRESS (&Tcp6Ap->RemoteAddress , &HttpInstance->RemoteIpv6Addr);
1164
1165 Tcp6Option = Tcp6CfgData->ControlOption;
1166 Tcp6Option->ReceiveBufferSize = HTTP_BUFFER_SIZE_DEAULT;
1167 Tcp6Option->SendBufferSize = HTTP_BUFFER_SIZE_DEAULT;
1168 Tcp6Option->MaxSynBackLog = HTTP_MAX_SYN_BACK_LOG;
1169 Tcp6Option->ConnectionTimeout = HTTP_CONNECTION_TIMEOUT;
1170 Tcp6Option->DataRetries = HTTP_DATA_RETRIES;
1171 Tcp6Option->FinTimeout = HTTP_FIN_TIMEOUT;
1172 Tcp6Option->KeepAliveProbes = HTTP_KEEP_ALIVE_PROBES;
1173 Tcp6Option->KeepAliveTime = HTTP_KEEP_ALIVE_TIME;
1174 Tcp6Option->KeepAliveInterval = HTTP_KEEP_ALIVE_INTERVAL;
1175 Tcp6Option->EnableNagle = TRUE;
1176
1177 Status = HttpInstance->Tcp6->Configure (HttpInstance->Tcp6, Tcp6CfgData);
1178 if (EFI_ERROR (Status)) {
1179 DEBUG ((EFI_D_ERROR, "HttpConfigureTcp6 - %r\n", Status));
1180 return Status;
1181 }
1182
1183 Status = HttpCreateTcpConnCloseEvent (HttpInstance);
1184 if (EFI_ERROR (Status)) {
1185 return Status;
1186 }
1187
1188 Status = HttpCreateTcpTxEvent (Wrap);
1189 if (EFI_ERROR (Status)) {
1190 return Status;
1191 }
1192
1193 HttpInstance->State = HTTP_STATE_TCP_CONFIGED;
1194
1195 return EFI_SUCCESS;
1196
1197}
1198
1199/**
1200 Check existing TCP connection, if in error state, recover TCP4 connection. Then,
1201 connect one TLS session if required.
1202
1203 @param[in] HttpInstance The HTTP instance private data.
1204
1205 @retval EFI_SUCCESS The TCP connection is established.
1206 @retval EFI_NOT_READY TCP4 protocol child is not created or configured.
1207 @retval Others Other error as indicated.
1208
1209**/
1210EFI_STATUS
1211HttpConnectTcp4 (
1212 IN HTTP_PROTOCOL *HttpInstance
1213 )
1214{
1215 EFI_STATUS Status;
1216 EFI_TCP4_CONNECTION_STATE Tcp4State;
1217
1218
1219 if (HttpInstance->State < HTTP_STATE_TCP_CONFIGED || HttpInstance->Tcp4 == NULL) {
1220 return EFI_NOT_READY;
1221 }
1222
1223 Status = HttpInstance->Tcp4->GetModeData(
1224 HttpInstance->Tcp4,
1225 &Tcp4State,
1226 NULL,
1227 NULL,
1228 NULL,
1229 NULL
1230 );
1231 if (EFI_ERROR(Status)){
1232 DEBUG ((EFI_D_ERROR, "Tcp4 GetModeData fail - %x\n", Status));
1233 return Status;
1234 }
1235
1236 if (Tcp4State == Tcp4StateEstablished) {
1237 return EFI_SUCCESS;
1238 } else if (Tcp4State > Tcp4StateEstablished ) {
1239 HttpCloseConnection(HttpInstance);
1240 }
1241
1242 Status = HttpCreateConnection (HttpInstance);
1243 if (EFI_ERROR(Status)){
1244 DEBUG ((EFI_D_ERROR, "Tcp4 Connection fail - %x\n", Status));
1245 return Status;
1246 }
1247
1248 //
1249 // Tls session connection.
1250 //
1251 if (HttpInstance->UseHttps) {
1252 if (HttpInstance->TimeoutEvent == NULL) {
1253 //
1254 // Create TimeoutEvent for TLS connection.
1255 //
1256 Status = gBS->CreateEvent (
1257 EVT_TIMER,
1258 TPL_CALLBACK,
1259 NULL,
1260 NULL,
1261 &HttpInstance->TimeoutEvent
1262 );
1263 if (EFI_ERROR (Status)) {
1264 TlsCloseTxRxEvent (HttpInstance);
1265 return Status;
1266 }
1267 }
1268
1269 //
1270 // Start the timer, and wait Timeout seconds for connection.
1271 //
1272 Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_CONNECTION_TIMEOUT * TICKS_PER_SECOND);
1273 if (EFI_ERROR (Status)) {
1274 TlsCloseTxRxEvent (HttpInstance);
1275 return Status;
1276 }
1277
1278 Status = TlsConnectSession (HttpInstance, HttpInstance->TimeoutEvent);
1279
1280 gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0);
1281
1282 if (EFI_ERROR (Status)) {
1283 TlsCloseTxRxEvent (HttpInstance);
1284 return Status;
1285 }
1286 }
1287
1288 return Status;
1289}
1290
1291/**
1292 Check existing TCP connection, if in error state, recover TCP6 connection. Then,
1293 connect one TLS session if required.
1294
1295 @param[in] HttpInstance The HTTP instance private data.
1296
1297 @retval EFI_SUCCESS The TCP connection is established.
1298 @retval EFI_NOT_READY TCP6 protocol child is not created or configured.
1299 @retval Others Other error as indicated.
1300
1301**/
1302EFI_STATUS
1303HttpConnectTcp6 (
1304 IN HTTP_PROTOCOL *HttpInstance
1305 )
1306{
1307 EFI_STATUS Status;
1308 EFI_TCP6_CONNECTION_STATE Tcp6State;
1309
1310 if (HttpInstance->State < HTTP_STATE_TCP_CONFIGED || HttpInstance->Tcp6 == NULL) {
1311 return EFI_NOT_READY;
1312 }
1313
1314 Status = HttpInstance->Tcp6->GetModeData (
1315 HttpInstance->Tcp6,
1316 &Tcp6State,
1317 NULL,
1318 NULL,
1319 NULL,
1320 NULL
1321 );
1322
1323 if (EFI_ERROR(Status)){
1324 DEBUG ((EFI_D_ERROR, "Tcp6 GetModeData fail - %x\n", Status));
1325 return Status;
1326 }
1327
1328 if (Tcp6State == Tcp6StateEstablished) {
1329 return EFI_SUCCESS;
1330 } else if (Tcp6State > Tcp6StateEstablished ) {
1331 HttpCloseConnection(HttpInstance);
1332 }
1333
1334 Status = HttpCreateConnection (HttpInstance);
1335 if (EFI_ERROR(Status)){
1336 DEBUG ((EFI_D_ERROR, "Tcp6 Connection fail - %x\n", Status));
1337 return Status;
1338 }
1339
1340 //
1341 // Tls session connection.
1342 //
1343 if (HttpInstance->UseHttps) {
1344 if (HttpInstance->TimeoutEvent == NULL) {
1345 //
1346 // Create TimeoutEvent for TLS connection.
1347 //
1348 Status = gBS->CreateEvent (
1349 EVT_TIMER,
1350 TPL_CALLBACK,
1351 NULL,
1352 NULL,
1353 &HttpInstance->TimeoutEvent
1354 );
1355 if (EFI_ERROR (Status)) {
1356 TlsCloseTxRxEvent (HttpInstance);
1357 return Status;
1358 }
1359 }
1360
1361 //
1362 // Start the timer, and wait Timeout seconds for connection.
1363 //
1364 Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_CONNECTION_TIMEOUT * TICKS_PER_SECOND);
1365 if (EFI_ERROR (Status)) {
1366 TlsCloseTxRxEvent (HttpInstance);
1367 return Status;
1368 }
1369
1370 Status = TlsConnectSession (HttpInstance, HttpInstance->TimeoutEvent);
1371
1372 gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0);
1373
1374 if (EFI_ERROR (Status)) {
1375 TlsCloseTxRxEvent (HttpInstance);
1376 return Status;
1377 }
1378 }
1379
1380 return Status;
1381}
1382
1383/**
1384 Initialize Http session.
1385
1386 @param[in] HttpInstance The HTTP instance private data.
1387 @param[in] Wrap The HTTP token's wrap data.
1388 @param[in] Configure The Flag indicates whether need to initialize session.
1389 @param[in] TlsConfigure The Flag indicates whether it's the new Tls session.
1390
1391 @retval EFI_SUCCESS The initialization of session is done.
1392 @retval Others Other error as indicated.
1393
1394**/
1395EFI_STATUS
1396HttpInitSession (
1397 IN HTTP_PROTOCOL *HttpInstance,
1398 IN HTTP_TOKEN_WRAP *Wrap,
1399 IN BOOLEAN Configure,
1400 IN BOOLEAN TlsConfigure
1401 )
1402{
1403 EFI_STATUS Status;
1404 ASSERT (HttpInstance != NULL);
1405
1406 //
1407 // Configure Tls session.
1408 //
1409 if (TlsConfigure) {
1410 Status = TlsConfigureSession (HttpInstance);
1411 if (EFI_ERROR (Status)) {
1412 return Status;
1413 }
1414 }
1415
1416 if (!HttpInstance->LocalAddressIsIPv6) {
1417 //
1418 // Configure TCP instance.
1419 //
1420 if (Configure) {
1421 Status = HttpConfigureTcp4 (HttpInstance, Wrap);
1422 if (EFI_ERROR (Status)) {
1423 return Status;
1424 }
1425 }
1426
1427 //
1428 // Connect TCP.
1429 //
1430 Status = HttpConnectTcp4 (HttpInstance);
1431 if (EFI_ERROR (Status)) {
1432 return Status;
1433 }
1434 } else {
1435 //
1436 // Configure TCP instance.
1437 //
1438 if (Configure) {
1439 Status = HttpConfigureTcp6 (HttpInstance, Wrap);
1440 if (EFI_ERROR (Status)) {
1441 return Status;
1442 }
1443 }
1444
1445 //
1446 // Connect TCP.
1447 //
1448 Status = HttpConnectTcp6 (HttpInstance);
1449 if (EFI_ERROR (Status)) {
1450 return Status;
1451 }
1452 }
1453
1454 return EFI_SUCCESS;
1455
1456}
1457
1458/**
1459 Send the HTTP or HTTPS message through TCP4 or TCP6.
1460
1461 @param[in] HttpInstance The HTTP instance private data.
1462 @param[in] Wrap The HTTP token's wrap data.
1463 @param[in] TxString Buffer containing the HTTP message string.
1464 @param[in] TxStringLen Length of the HTTP message string in bytes.
1465
1466 @retval EFI_SUCCESS The HTTP message is queued into TCP transmit queue.
1467 @retval Others Other error as indicated.
1468
1469**/
1470EFI_STATUS
1471HttpTransmitTcp (
1472 IN HTTP_PROTOCOL *HttpInstance,
1473 IN HTTP_TOKEN_WRAP *Wrap,
1474 IN UINT8 *TxString,
1475 IN UINTN TxStringLen
1476 )
1477{
1478 EFI_STATUS Status;
1479 EFI_TCP4_IO_TOKEN *Tx4Token;
1480 EFI_TCP4_PROTOCOL *Tcp4;
1481 EFI_TCP6_IO_TOKEN *Tx6Token;
1482 EFI_TCP6_PROTOCOL *Tcp6;
1483 UINT8 *TlsRecord;
1484 UINT16 PayloadSize;
1485 NET_FRAGMENT TempFragment;
1486 NET_FRAGMENT Fragment;
1487 UINTN RecordCount;
1488 UINTN RemainingLen;
1489
1490 Status = EFI_SUCCESS;
1491 TlsRecord = NULL;
1492 PayloadSize = 0;
1493 TempFragment.Len = 0;
1494 TempFragment.Bulk = NULL;
1495 Fragment.Len = 0;
1496 Fragment.Bulk = NULL;
1497 RecordCount = 0;
1498 RemainingLen = 0;
1499
1500 //
1501 // Need to encrypt data.
1502 //
1503 if (HttpInstance->UseHttps) {
1504 //
1505 // Allocate enough buffer for each TLS plaintext records.
1506 //
1507 TlsRecord = AllocateZeroPool (TLS_RECORD_HEADER_LENGTH + TLS_PLAINTEXT_RECORD_MAX_PAYLOAD_LENGTH);
1508 if (TlsRecord == NULL) {
1509 Status = EFI_OUT_OF_RESOURCES;
1510 return Status;
1511 }
1512
1513 //
1514 // Allocate enough buffer for all TLS ciphertext records.
1515 //
1516 RecordCount = TxStringLen / TLS_PLAINTEXT_RECORD_MAX_PAYLOAD_LENGTH + 1;
1517 Fragment.Bulk = AllocateZeroPool (RecordCount * (TLS_RECORD_HEADER_LENGTH + TLS_CIPHERTEXT_RECORD_MAX_PAYLOAD_LENGTH));
1518 if (Fragment.Bulk == NULL) {
1519 Status = EFI_OUT_OF_RESOURCES;
1520 goto ON_ERROR;
1521 }
1522
1523 //
1524 // Encrypt each TLS plaintext records.
1525 //
1526 RemainingLen = TxStringLen;
1527 while (RemainingLen != 0) {
1528 PayloadSize = (UINT16) MIN (TLS_PLAINTEXT_RECORD_MAX_PAYLOAD_LENGTH, RemainingLen);
1529
1530 ((TLS_RECORD_HEADER *) TlsRecord)->ContentType = TlsContentTypeApplicationData;
1531 ((TLS_RECORD_HEADER *) TlsRecord)->Version.Major = HttpInstance->TlsConfigData.Version.Major;
1532 ((TLS_RECORD_HEADER *) TlsRecord)->Version.Minor = HttpInstance->TlsConfigData.Version.Minor;
1533 ((TLS_RECORD_HEADER *) TlsRecord)->Length = PayloadSize;
1534
1535 CopyMem (TlsRecord + TLS_RECORD_HEADER_LENGTH, TxString + (TxStringLen - RemainingLen), PayloadSize);
1536
1537 Status = TlsProcessMessage (
1538 HttpInstance,
1539 TlsRecord,
1540 TLS_RECORD_HEADER_LENGTH + PayloadSize,
1541 EfiTlsEncrypt,
1542 &TempFragment
1543 );
1544 if (EFI_ERROR (Status)) {
1545 goto ON_ERROR;
1546 }
1547
1548 //
1549 // Record the processed/encrypted Packet.
1550 //
1551 CopyMem (Fragment.Bulk + Fragment.Len, TempFragment.Bulk, TempFragment.Len);
1552 Fragment.Len += TempFragment.Len;
1553
1554 FreePool (TempFragment.Bulk);
1555 TempFragment.Len = 0;
1556 TempFragment.Bulk = NULL;
1557
1558 RemainingLen -= (UINTN) PayloadSize;
1559 ZeroMem (TlsRecord, TLS_RECORD_HEADER_LENGTH + TLS_PLAINTEXT_RECORD_MAX_PAYLOAD_LENGTH);
1560 }
1561
1562 FreePool (TlsRecord);
1563 TlsRecord = NULL;
1564 }
1565
1566 if (!HttpInstance->LocalAddressIsIPv6) {
1567 Tcp4 = HttpInstance->Tcp4;
1568 Tx4Token = &Wrap->TcpWrap.Tx4Token;
1569
1570 if (HttpInstance->UseHttps) {
1571 Tx4Token->Packet.TxData->DataLength = Fragment.Len;
1572 Tx4Token->Packet.TxData->FragmentTable[0].FragmentLength = Fragment.Len;
1573 Tx4Token->Packet.TxData->FragmentTable[0].FragmentBuffer = (VOID *) Fragment.Bulk;
1574 } else {
1575 Tx4Token->Packet.TxData->DataLength = (UINT32) TxStringLen;
1576 Tx4Token->Packet.TxData->FragmentTable[0].FragmentLength = (UINT32) TxStringLen;
1577 Tx4Token->Packet.TxData->FragmentTable[0].FragmentBuffer = (VOID *) TxString;
1578 }
1579
1580 Tx4Token->CompletionToken.Status = EFI_NOT_READY;
1581
1582 Wrap->TcpWrap.IsTxDone = FALSE;
1583 Status = Tcp4->Transmit (Tcp4, Tx4Token);
1584 if (EFI_ERROR (Status)) {
1585 DEBUG ((EFI_D_ERROR, "Transmit failed: %r\n", Status));
1586 goto ON_ERROR;
1587 }
1588
1589 } else {
1590 Tcp6 = HttpInstance->Tcp6;
1591 Tx6Token = &Wrap->TcpWrap.Tx6Token;
1592
1593 if (HttpInstance->UseHttps) {
1594 Tx6Token->Packet.TxData->DataLength = Fragment.Len;
1595 Tx6Token->Packet.TxData->FragmentTable[0].FragmentLength = Fragment.Len;
1596 Tx6Token->Packet.TxData->FragmentTable[0].FragmentBuffer = (VOID *) Fragment.Bulk;
1597 } else {
1598 Tx6Token->Packet.TxData->DataLength = (UINT32) TxStringLen;
1599 Tx6Token->Packet.TxData->FragmentTable[0].FragmentLength = (UINT32) TxStringLen;
1600 Tx6Token->Packet.TxData->FragmentTable[0].FragmentBuffer = (VOID *) TxString;
1601 }
1602
1603 Tx6Token->CompletionToken.Status = EFI_NOT_READY;
1604
1605 Wrap->TcpWrap.IsTxDone = FALSE;
1606 Status = Tcp6->Transmit (Tcp6, Tx6Token);
1607 if (EFI_ERROR (Status)) {
1608 DEBUG ((EFI_D_ERROR, "Transmit failed: %r\n", Status));
1609 goto ON_ERROR;
1610 }
1611 }
1612
1613 return Status;
1614
1615ON_ERROR:
1616
1617 if (HttpInstance->UseHttps) {
1618 if (TlsRecord != NULL) {
1619 FreePool (TlsRecord);
1620 TlsRecord = NULL;
1621 }
1622
1623 if (Fragment.Bulk != NULL) {
1624 FreePool (Fragment.Bulk);
1625 Fragment.Bulk = NULL;
1626 }
1627 }
1628
1629 return Status;
1630}
1631
1632/**
1633 Check whether the user's token or event has already
1634 been enqueue on HTTP Tx or Rx Token list.
1635
1636 @param[in] Map The container of either user's transmit or receive
1637 token.
1638 @param[in] Item Current item to check against.
1639 @param[in] Context The Token to check againist.
1640
1641 @retval EFI_ACCESS_DENIED The token or event has already been enqueued in IP
1642 @retval EFI_SUCCESS The current item isn't the same token/event as the
1643 context.
1644
1645**/
1646EFI_STATUS
1647EFIAPI
1648HttpTokenExist (
1649 IN NET_MAP *Map,
1650 IN NET_MAP_ITEM *Item,
1651 IN VOID *Context
1652 )
1653{
1654 EFI_HTTP_TOKEN *Token;
1655 EFI_HTTP_TOKEN *TokenInItem;
1656
1657 Token = (EFI_HTTP_TOKEN *) Context;
1658 TokenInItem = (EFI_HTTP_TOKEN *) Item->Key;
1659
1660 if (Token == TokenInItem || Token->Event == TokenInItem->Event) {
1661 return EFI_ACCESS_DENIED;
1662 }
1663
1664 return EFI_SUCCESS;
1665}
1666
1667/**
1668 Check whether the HTTP message associated with Tx4Token or Tx6Token is already sent out.
1669
1670 @param[in] Map The container of Tx4Token or Tx6Token.
1671 @param[in] Item Current item to check against.
1672 @param[in] Context The Token to check againist.
1673
1674 @retval EFI_NOT_READY The HTTP message is still queued in the list.
1675 @retval EFI_SUCCESS The HTTP message has been sent out.
1676
1677**/
1678EFI_STATUS
1679EFIAPI
1680HttpTcpNotReady (
1681 IN NET_MAP *Map,
1682 IN NET_MAP_ITEM *Item,
1683 IN VOID *Context
1684 )
1685{
1686 HTTP_TOKEN_WRAP *ValueInItem;
1687
1688 ValueInItem = (HTTP_TOKEN_WRAP *) Item->Value;
1689
1690 if (!ValueInItem->TcpWrap.IsTxDone) {
1691 return EFI_NOT_READY;
1692 }
1693
1694 return EFI_SUCCESS;
1695}
1696
1697/**
1698 Transmit the HTTP or HTTPS mssage by processing the associated HTTP token.
1699
1700 @param[in] Map The container of Tx4Token or Tx6Token.
1701 @param[in] Item Current item to check against.
1702 @param[in] Context The Token to check againist.
1703
1704 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
1705 @retval EFI_SUCCESS The HTTP message is queued into TCP transmit
1706 queue.
1707
1708**/
1709EFI_STATUS
1710EFIAPI
1711HttpTcpTransmit (
1712 IN NET_MAP *Map,
1713 IN NET_MAP_ITEM *Item,
1714 IN VOID *Context
1715 )
1716{
1717 HTTP_TOKEN_WRAP *ValueInItem;
1718 EFI_STATUS Status;
1719 CHAR8 *RequestMsg;
1720 CHAR8 *Url;
1721 UINTN UrlSize;
1722 UINTN RequestMsgSize;
1723
1724 RequestMsg = NULL;
1725
1726 ValueInItem = (HTTP_TOKEN_WRAP *) Item->Value;
1727 if (ValueInItem->TcpWrap.IsTxDone) {
1728 return EFI_SUCCESS;
1729 }
1730
1731 //
1732 // Parse the URI of the remote host.
1733 //
1734 UrlSize = StrLen (ValueInItem->HttpToken->Message->Data.Request->Url) + 1;
1735 Url = AllocatePool (UrlSize);
1736 if (Url == NULL) {
1737 return EFI_OUT_OF_RESOURCES;
1738 }
1739
1740 UnicodeStrToAsciiStrS (ValueInItem->HttpToken->Message->Data.Request->Url, Url, UrlSize);
1741
1742 //
1743 // Create request message.
1744 //
1745 Status = HttpGenRequestMessage (
1746 ValueInItem->HttpToken->Message,
1747 Url,
1748 &RequestMsg,
1749 &RequestMsgSize
1750 );
1751 FreePool (Url);
1752
1753 if (EFI_ERROR (Status) || NULL == RequestMsg){
1754 return Status;
1755 }
1756
1757 ASSERT (RequestMsg != NULL);
1758
1759 //
1760 // Transmit the request message.
1761 //
1762 Status = HttpTransmitTcp (
1763 ValueInItem->HttpInstance,
1764 ValueInItem,
1765 (UINT8*) RequestMsg,
1766 RequestMsgSize
1767 );
1768 FreePool (RequestMsg);
1769 return Status;
1770}
1771
1772/**
1773 Receive the HTTP response by processing the associated HTTP token.
1774
1775 @param[in] Map The container of Rx4Token or Rx6Token.
1776 @param[in] Item Current item to check against.
1777 @param[in] Context The Token to check againist.
1778
1779 @retval EFI_SUCCESS The HTTP response is queued into TCP receive
1780 queue.
1781 @retval Others Other error as indicated.
1782
1783**/
1784EFI_STATUS
1785EFIAPI
1786HttpTcpReceive (
1787 IN NET_MAP *Map,
1788 IN NET_MAP_ITEM *Item,
1789 IN VOID *Context
1790 )
1791{
1792 //
1793 // Process the queued HTTP response.
1794 //
1795 return HttpResponseWorker ((HTTP_TOKEN_WRAP *) Item->Value);
1796}
1797
1798/**
1799 Receive the HTTP header by processing the associated HTTP token.
1800
1801 @param[in] HttpInstance The HTTP instance private data.
1802 @param[in, out] SizeofHeaders The HTTP header length.
1803 @param[in, out] BufferSize The size of buffer to cacahe the header message.
1804 @param[in] Timeout The time to wait for receiving the header packet.
1805
1806 @retval EFI_SUCCESS The HTTP header is received.
1807 @retval Others Other errors as indicated.
1808
1809**/
1810EFI_STATUS
1811HttpTcpReceiveHeader (
1812 IN HTTP_PROTOCOL *HttpInstance,
1813 IN OUT UINTN *SizeofHeaders,
1814 IN OUT UINTN *BufferSize,
1815 IN EFI_EVENT Timeout
1816 )
1817{
1818 EFI_STATUS Status;
1819 EFI_TCP4_IO_TOKEN *Rx4Token;
1820 EFI_TCP4_PROTOCOL *Tcp4;
1821 EFI_TCP6_IO_TOKEN *Rx6Token;
1822 EFI_TCP6_PROTOCOL *Tcp6;
1823 CHAR8 **EndofHeader;
1824 CHAR8 **HttpHeaders;
1825 CHAR8 *Buffer;
1826 NET_FRAGMENT Fragment;
1827
1828 ASSERT (HttpInstance != NULL);
1829
1830 EndofHeader = HttpInstance->EndofHeader;
1831 HttpHeaders = HttpInstance->HttpHeaders;
1832 Tcp4 = HttpInstance->Tcp4;
1833 Tcp6 = HttpInstance->Tcp6;
1834 Buffer = NULL;
1835 Rx4Token = NULL;
1836 Rx6Token = NULL;
1837 Fragment.Len = 0;
1838 Fragment.Bulk = NULL;
1839
1840 if (HttpInstance->LocalAddressIsIPv6) {
1841 ASSERT (Tcp6 != NULL);
1842 } else {
1843 ASSERT (Tcp4 != NULL);
1844 }
1845
1846 if (!HttpInstance->UseHttps) {
1847 Status = HttpCreateTcpRxEventForHeader (HttpInstance);
1848 if (EFI_ERROR (Status)) {
1849 return Status;
1850 }
1851 }
1852
1853 if (!HttpInstance->LocalAddressIsIPv6) {
1854 if (!HttpInstance->UseHttps) {
1855 Rx4Token = &HttpInstance->Rx4Token;
1856 Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer = AllocateZeroPool (DEF_BUF_LEN);
1857 if (Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer == NULL) {
1858 Status = EFI_OUT_OF_RESOURCES;
1859 return Status;
1860 }
1861 }
1862
1863 //
1864 // Receive the HTTP headers only when EFI_HTTP_RESPONSE_DATA is not NULL.
1865 //
1866 while (*EndofHeader == NULL) {
1867 if (!HttpInstance->UseHttps) {
1868 HttpInstance->IsRxDone = FALSE;
1869 Rx4Token->Packet.RxData->DataLength = DEF_BUF_LEN;
1870 Rx4Token->Packet.RxData->FragmentTable[0].FragmentLength = DEF_BUF_LEN;
1871 Status = Tcp4->Receive (Tcp4, Rx4Token);
1872 if (EFI_ERROR (Status)) {
1873 DEBUG ((EFI_D_ERROR, "Tcp4 receive failed: %r\n", Status));
1874 return Status;
1875 }
1876
1877 while (!HttpInstance->IsRxDone && ((Timeout == NULL) || EFI_ERROR (gBS->CheckEvent (Timeout)))) {
1878 Tcp4->Poll (Tcp4);
1879 }
1880
1881 if (!HttpInstance->IsRxDone) {
1882 //
1883 // Cancle the Token before close its Event.
1884 //
1885 Tcp4->Cancel (HttpInstance->Tcp4, &Rx4Token->CompletionToken);
1886 gBS->CloseEvent (Rx4Token->CompletionToken.Event);
1887 Rx4Token->CompletionToken.Status = EFI_TIMEOUT;
1888 }
1889
1890 Status = Rx4Token->CompletionToken.Status;
1891 if (EFI_ERROR (Status)) {
1892 return Status;
1893 }
1894
1895 Fragment.Len = Rx4Token->Packet.RxData->FragmentTable[0].FragmentLength;
1896 Fragment.Bulk = (UINT8 *) Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer;
1897 } else {
1898 if (Fragment.Bulk != NULL) {
1899 FreePool (Fragment.Bulk);
1900 Fragment.Bulk = NULL;
1901 }
1902
1903 Status = HttpsReceive (HttpInstance, &Fragment, Timeout);
1904 if (EFI_ERROR (Status)) {
1905 DEBUG ((EFI_D_ERROR, "Tcp4 receive failed: %r\n", Status));
1906 return Status;
1907 }
1908 }
1909
1910 //
1911 // Append the response string along with a Null-terminator.
1912 //
1913 *BufferSize = *SizeofHeaders + Fragment.Len;
1914 Buffer = AllocatePool (*BufferSize + 1);
1915 if (Buffer == NULL) {
1916 Status = EFI_OUT_OF_RESOURCES;
1917 return Status;
1918 }
1919
1920 if (*HttpHeaders != NULL) {
1921 CopyMem (Buffer, *HttpHeaders, *SizeofHeaders);
1922 FreePool (*HttpHeaders);
1923 }
1924
1925 CopyMem (
1926 Buffer + *SizeofHeaders,
1927 Fragment.Bulk,
1928 Fragment.Len
1929 );
1930 *(Buffer + *BufferSize) = '\0';
1931 *HttpHeaders = Buffer;
1932 *SizeofHeaders = *BufferSize;
1933
1934 //
1935 // Check whether we received end of HTTP headers.
1936 //
1937 *EndofHeader = AsciiStrStr (*HttpHeaders, HTTP_END_OF_HDR_STR);
1938 };
1939
1940 //
1941 // Free the buffer.
1942 //
1943 if (Rx4Token != NULL && Rx4Token->Packet.RxData != NULL && Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {
1944 FreePool (Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer);
1945 Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL;
1946 Fragment.Bulk = NULL;
1947 }
1948
1949 if (Fragment.Bulk != NULL) {
1950 FreePool (Fragment.Bulk);
1951 Fragment.Bulk = NULL;
1952 }
1953 } else {
1954 if (!HttpInstance->UseHttps) {
1955 Rx6Token = &HttpInstance->Rx6Token;
1956 Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer = AllocateZeroPool (DEF_BUF_LEN);
1957 if (Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer == NULL) {
1958 Status = EFI_OUT_OF_RESOURCES;
1959 return Status;
1960 }
1961 }
1962
1963 //
1964 // Receive the HTTP headers only when EFI_HTTP_RESPONSE_DATA is not NULL.
1965 //
1966 while (*EndofHeader == NULL) {
1967 if (!HttpInstance->UseHttps) {
1968 HttpInstance->IsRxDone = FALSE;
1969 Rx6Token->Packet.RxData->DataLength = DEF_BUF_LEN;
1970 Rx6Token->Packet.RxData->FragmentTable[0].FragmentLength = DEF_BUF_LEN;
1971 Status = Tcp6->Receive (Tcp6, Rx6Token);
1972 if (EFI_ERROR (Status)) {
1973 DEBUG ((EFI_D_ERROR, "Tcp6 receive failed: %r\n", Status));
1974 return Status;
1975 }
1976
1977 while (!HttpInstance->IsRxDone && ((Timeout == NULL) || EFI_ERROR (gBS->CheckEvent (Timeout)))) {
1978 Tcp6->Poll (Tcp6);
1979 }
1980
1981 if (!HttpInstance->IsRxDone) {
1982 //
1983 // Cancle the Token before close its Event.
1984 //
1985 Tcp6->Cancel (HttpInstance->Tcp6, &Rx6Token->CompletionToken);
1986 gBS->CloseEvent (Rx6Token->CompletionToken.Event);
1987 Rx6Token->CompletionToken.Status = EFI_TIMEOUT;
1988 }
1989
1990 Status = Rx6Token->CompletionToken.Status;
1991 if (EFI_ERROR (Status)) {
1992 return Status;
1993 }
1994
1995 Fragment.Len = Rx6Token->Packet.RxData->FragmentTable[0].FragmentLength;
1996 Fragment.Bulk = (UINT8 *) Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer;
1997 } else {
1998 if (Fragment.Bulk != NULL) {
1999 FreePool (Fragment.Bulk);
2000 Fragment.Bulk = NULL;
2001 }
2002
2003 Status = HttpsReceive (HttpInstance, &Fragment, Timeout);
2004 if (EFI_ERROR (Status)) {
2005 DEBUG ((EFI_D_ERROR, "Tcp6 receive failed: %r\n", Status));
2006 return Status;
2007 }
2008 }
2009
2010 //
2011 // Append the response string along with a Null-terminator.
2012 //
2013 *BufferSize = *SizeofHeaders + Fragment.Len;
2014 Buffer = AllocatePool (*BufferSize + 1);
2015 if (Buffer == NULL) {
2016 Status = EFI_OUT_OF_RESOURCES;
2017 return Status;
2018 }
2019
2020 if (*HttpHeaders != NULL) {
2021 CopyMem (Buffer, *HttpHeaders, *SizeofHeaders);
2022 FreePool (*HttpHeaders);
2023 }
2024
2025 CopyMem (
2026 Buffer + *SizeofHeaders,
2027 Fragment.Bulk,
2028 Fragment.Len
2029 );
2030 *(Buffer + *BufferSize) = '\0';
2031 *HttpHeaders = Buffer;
2032 *SizeofHeaders = *BufferSize;
2033
2034 //
2035 // Check whether we received end of HTTP headers.
2036 //
2037 *EndofHeader = AsciiStrStr (*HttpHeaders, HTTP_END_OF_HDR_STR);
2038 };
2039
2040 //
2041 // Free the buffer.
2042 //
2043 if (Rx6Token != NULL && Rx6Token->Packet.RxData != NULL && Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {
2044 FreePool (Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer);
2045 Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL;
2046 Fragment.Bulk = NULL;
2047 }
2048
2049 if (Fragment.Bulk != NULL) {
2050 FreePool (Fragment.Bulk);
2051 Fragment.Bulk = NULL;
2052 }
2053 }
2054
2055 //
2056 // Skip the CRLF after the HTTP headers.
2057 //
2058 *EndofHeader = *EndofHeader + AsciiStrLen (HTTP_END_OF_HDR_STR);
2059
2060 *SizeofHeaders = *EndofHeader - *HttpHeaders;
2061
2062 return EFI_SUCCESS;
2063}
2064
2065/**
2066 Receive the HTTP body by processing the associated HTTP token.
2067
2068 @param[in] Wrap The HTTP token's wrap data.
2069 @param[in] HttpMsg The HTTP message data.
2070
2071 @retval EFI_SUCCESS The HTTP body is received.
2072 @retval Others Other error as indicated.
2073
2074**/
2075EFI_STATUS
2076HttpTcpReceiveBody (
2077 IN HTTP_TOKEN_WRAP *Wrap,
2078 IN EFI_HTTP_MESSAGE *HttpMsg
2079 )
2080{
2081 EFI_STATUS Status;
2082 HTTP_PROTOCOL *HttpInstance;
2083 EFI_TCP6_PROTOCOL *Tcp6;
2084 EFI_TCP6_IO_TOKEN *Rx6Token;
2085 EFI_TCP4_PROTOCOL *Tcp4;
2086 EFI_TCP4_IO_TOKEN *Rx4Token;
2087
2088 HttpInstance = Wrap->HttpInstance;
2089 Tcp4 = HttpInstance->Tcp4;
2090 Tcp6 = HttpInstance->Tcp6;
2091 Rx4Token = NULL;
2092 Rx6Token = NULL;
2093
2094 if (HttpInstance->LocalAddressIsIPv6) {
2095 ASSERT (Tcp6 != NULL);
2096 } else {
2097 ASSERT (Tcp4 != NULL);
2098 }
2099
2100 if (HttpInstance->LocalAddressIsIPv6) {
2101 Rx6Token = &Wrap->TcpWrap.Rx6Token;
2102 Rx6Token ->Packet.RxData->DataLength = (UINT32) MIN (MAX_UINT32, HttpMsg->BodyLength);
2103 Rx6Token ->Packet.RxData->FragmentTable[0].FragmentLength = (UINT32) MIN (MAX_UINT32, HttpMsg->BodyLength);
2104 Rx6Token ->Packet.RxData->FragmentTable[0].FragmentBuffer = (VOID *) HttpMsg->Body;
2105 Rx6Token->CompletionToken.Status = EFI_NOT_READY;
2106
2107 Status = Tcp6->Receive (Tcp6, Rx6Token);
2108 if (EFI_ERROR (Status)) {
2109 DEBUG ((EFI_D_ERROR, "Tcp6 receive failed: %r\n", Status));
2110 return Status;
2111 }
2112 } else {
2113 Rx4Token = &Wrap->TcpWrap.Rx4Token;
2114 Rx4Token->Packet.RxData->DataLength = (UINT32) MIN (MAX_UINT32, HttpMsg->BodyLength);
2115 Rx4Token->Packet.RxData->FragmentTable[0].FragmentLength = (UINT32) MIN (MAX_UINT32, HttpMsg->BodyLength);
2116 Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer = (VOID *) HttpMsg->Body;
2117
2118 Rx4Token->CompletionToken.Status = EFI_NOT_READY;
2119 Status = Tcp4->Receive (Tcp4, Rx4Token);
2120 if (EFI_ERROR (Status)) {
2121 DEBUG ((EFI_D_ERROR, "Tcp4 receive failed: %r\n", Status));
2122 return Status;
2123 }
2124 }
2125
2126 return EFI_SUCCESS;
2127
2128}
2129
2130/**
2131 Clean up Tcp Tokens while the Tcp transmission error occurs.
2132
2133 @param[in] Wrap Pointer to HTTP token's wrap data.
2134
2135**/
2136VOID
2137HttpTcpTokenCleanup (
2138 IN HTTP_TOKEN_WRAP *Wrap
2139 )
2140{
2141 HTTP_PROTOCOL *HttpInstance;
2142 EFI_TCP4_IO_TOKEN *Rx4Token;
2143 EFI_TCP6_IO_TOKEN *Rx6Token;
2144
2145 ASSERT (Wrap != NULL);
2146 HttpInstance = Wrap->HttpInstance;
2147 Rx4Token = NULL;
2148 Rx6Token = NULL;
2149
2150 if (HttpInstance->LocalAddressIsIPv6) {
2151 Rx6Token = &Wrap->TcpWrap.Rx6Token;
2152
2153 if (Rx6Token->CompletionToken.Event != NULL) {
2154 gBS->CloseEvent (Rx6Token->CompletionToken.Event);
2155 Rx6Token->CompletionToken.Event = NULL;
2156 }
2157
2158 FreePool (Wrap);
2159
2160 Rx6Token = &HttpInstance->Rx6Token;
2161
2162 if (Rx6Token->CompletionToken.Event != NULL) {
2163 gBS->CloseEvent (Rx6Token->CompletionToken.Event);
2164 Rx6Token->CompletionToken.Event = NULL;
2165 }
2166
2167 if (Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {
2168 FreePool (Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer);
2169 Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL;
2170 }
2171
2172 } else {
2173 Rx4Token = &Wrap->TcpWrap.Rx4Token;
2174
2175 if (Rx4Token->CompletionToken.Event != NULL) {
2176 gBS->CloseEvent (Rx4Token->CompletionToken.Event);
2177 Rx4Token->CompletionToken.Event = NULL;
2178 }
2179
2180 FreePool (Wrap);
2181
2182 Rx4Token = &HttpInstance->Rx4Token;
2183
2184 if (Rx4Token->CompletionToken.Event != NULL) {
2185 gBS->CloseEvent (Rx4Token->CompletionToken.Event);
2186 Rx4Token->CompletionToken.Event = NULL;
2187 }
2188
2189
2190 if (Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {
2191 FreePool (Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer);
2192 Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL;
2193 }
2194 }
2195
2196}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette