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