]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/HttpDxe/HttpImpl.c
NetworkPkg: Code logic optimization for DnsDxe and HttpDxe driver
[mirror_edk2.git] / NetworkPkg / HttpDxe / HttpImpl.c
1 /** @file
2 Implementation of EFI_HTTP_PROTOCOL protocol interfaces.
3
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
5
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 EFI_HTTP_PROTOCOL mEfiHttpTemplate = {
19 EfiHttpGetModeData,
20 EfiHttpConfigure,
21 EfiHttpRequest,
22 EfiHttpCancel,
23 EfiHttpResponse,
24 EfiHttpPoll
25 };
26
27 /**
28 Returns the operational parameters for the current HTTP child instance.
29
30 The GetModeData() function is used to read the current mode data (operational
31 parameters) for this HTTP protocol instance.
32
33 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
34 @param[out] HttpConfigData Point to buffer for operational parameters of this
35 HTTP instance.
36
37 @retval EFI_SUCCESS Operation succeeded.
38 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
39 This is NULL.
40 HttpConfigData is NULL.
41 HttpConfigData->AccessPoint is NULL.
42 @retval EFI_NOT_STARTED The HTTP instance is not configured.
43
44 **/
45 EFI_STATUS
46 EFIAPI
47 EfiHttpGetModeData (
48 IN EFI_HTTP_PROTOCOL *This,
49 OUT EFI_HTTP_CONFIG_DATA *HttpConfigData
50 )
51 {
52 HTTP_PROTOCOL *HttpInstance;
53
54 if ((This == NULL) || (HttpConfigData == NULL)) {
55 return EFI_INVALID_PARAMETER;
56 }
57
58 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
59 ASSERT (HttpInstance != NULL);
60
61 if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) {
62 return EFI_NOT_STARTED;
63 }
64
65 if (HttpConfigData->AccessPoint.IPv4Node == NULL) {
66 return EFI_INVALID_PARAMETER;
67 }
68
69 HttpConfigData->HttpVersion = HttpInstance->HttpVersion;
70 HttpConfigData->TimeOutMillisec = HttpInstance->TimeOutMillisec;
71 HttpConfigData->LocalAddressIsIPv6 = HttpInstance->LocalAddressIsIPv6;
72
73 CopyMem (
74 HttpConfigData->AccessPoint.IPv4Node,
75 &HttpInstance->IPv4Node,
76 sizeof (HttpInstance->IPv4Node)
77 );
78
79 return EFI_SUCCESS;
80 }
81
82 /**
83 Initialize or brutally reset the operational parameters for this EFI HTTP instance.
84
85 The Configure() function does the following:
86 When HttpConfigData is not NULL Initialize this EFI HTTP instance by configuring
87 timeout, local address, port, etc.
88 When HttpConfigData is NULL, reset this EFI HTTP instance by closing all active
89 connections with remote hosts, canceling all asynchronous tokens, and flush request
90 and response buffers without informing the appropriate hosts.
91
92 Except for GetModeData() and Configure(), No other EFI HTTP function can be executed
93 by this instance until the Configure() function is executed and returns successfully.
94
95 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
96 @param[in] HttpConfigData Pointer to the configure data to configure the instance.
97
98 @retval EFI_SUCCESS Operation succeeded.
99 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
100 This is NULL.
101 HttpConfigData->LocalAddressIsIPv6 is FALSE and
102 HttpConfigData->IPv4Node is NULL.
103 HttpConfigData->LocalAddressIsIPv6 is TRUE and
104 HttpConfigData->IPv6Node is NULL.
105 @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling
106 Configure() with NULL to reset it.
107 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
108 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when
109 executing Configure().
110 @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported
111 in the implementation.
112 **/
113 EFI_STATUS
114 EFIAPI
115 EfiHttpConfigure (
116 IN EFI_HTTP_PROTOCOL *This,
117 IN EFI_HTTP_CONFIG_DATA *HttpConfigData
118 )
119 {
120 HTTP_PROTOCOL *HttpInstance;
121 EFI_STATUS Status;
122
123 if (This == NULL) {
124 return EFI_INVALID_PARAMETER;
125 }
126
127 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
128 ASSERT (HttpInstance != NULL && HttpInstance->Service != NULL);
129
130 if (HttpConfigData != NULL) {
131 //
132 // Check input parameters.
133 //
134 if (HttpConfigData->LocalAddressIsIPv6) {
135 if (HttpConfigData->AccessPoint.IPv6Node == NULL) {
136 return EFI_INVALID_PARAMETER;
137 }
138 } else {
139 if (HttpConfigData->AccessPoint.IPv4Node == NULL) {
140 return EFI_INVALID_PARAMETER;
141 }
142 }
143 //
144 // Now configure this HTTP instance.
145 //
146 if (HttpInstance->State != HTTP_STATE_UNCONFIGED) {
147 return EFI_ALREADY_STARTED;
148 }
149
150 HttpInstance->HttpVersion = HttpConfigData->HttpVersion;
151 HttpInstance->TimeOutMillisec = HttpConfigData->TimeOutMillisec;
152 HttpInstance->LocalAddressIsIPv6 = HttpConfigData->LocalAddressIsIPv6;
153
154 if (HttpConfigData->LocalAddressIsIPv6) {
155 return EFI_UNSUPPORTED;
156 } else {
157 CopyMem (
158 &HttpInstance->IPv4Node,
159 HttpConfigData->AccessPoint.IPv4Node,
160 sizeof (HttpInstance->IPv4Node)
161 );
162
163 HttpInstance->State = HTTP_STATE_HTTP_CONFIGED;
164 return EFI_SUCCESS;
165 }
166
167 } else {
168 if (HttpInstance->LocalAddressIsIPv6) {
169 return EFI_UNSUPPORTED;
170 } else {
171 HttpCleanProtocol (HttpInstance);
172 Status = HttpInitProtocol (HttpInstance->Service, HttpInstance);
173 if (EFI_ERROR (Status)) {
174 return Status;
175 }
176
177 HttpInstance->State = HTTP_STATE_UNCONFIGED;
178 return EFI_SUCCESS;
179 }
180 }
181 }
182
183
184 /**
185 The Request() function queues an HTTP request to this HTTP instance.
186
187 Similar to Transmit() function in the EFI TCP driver. When the HTTP request is sent
188 successfully, or if there is an error, Status in token will be updated and Event will
189 be signaled.
190
191 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
192 @param[in] Token Pointer to storage containing HTTP request token.
193
194 @retval EFI_SUCCESS Outgoing data was processed.
195 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
196 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
197 @retval EFI_TIMEOUT Data was dropped out of the transmit or receive queue.
198 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
199 @retval EFI_UNSUPPORTED The HTTP method is not supported in current
200 implementation.
201 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
202 This is NULL.
203 Token->Message is NULL.
204 Token->Message->Body is not NULL,
205 Token->Message->BodyLength is non-zero, and
206 Token->Message->Data is NULL, but a previous call to
207 Request()has not been completed successfully.
208 **/
209 EFI_STATUS
210 EFIAPI
211 EfiHttpRequest (
212 IN EFI_HTTP_PROTOCOL *This,
213 IN EFI_HTTP_TOKEN *Token
214 )
215 {
216 EFI_HTTP_MESSAGE *HttpMsg;
217 EFI_HTTP_REQUEST_DATA *Request;
218 VOID *UrlParser;
219 EFI_STATUS Status;
220 CHAR8 *HostName;
221 UINT16 RemotePort;
222 HTTP_PROTOCOL *HttpInstance;
223 BOOLEAN Configure;
224 BOOLEAN ReConfigure;
225 CHAR8 *RequestStr;
226 CHAR8 *Url;
227 CHAR16 *HostNameStr;
228 HTTP_TOKEN_WRAP *Wrap;
229 HTTP_TCP_TOKEN_WRAP *TcpWrap;
230
231 if ((This == NULL) || (Token == NULL)) {
232 return EFI_INVALID_PARAMETER;
233 }
234
235 HttpMsg = Token->Message;
236 if ((HttpMsg == NULL) || (HttpMsg->Headers == NULL)) {
237 return EFI_INVALID_PARAMETER;
238 }
239
240 //
241 // Current implementation does not support POST/PUT method.
242 // If future version supports these two methods, Request could be NULL for a special case that to send large amounts
243 // of data. For this case, the implementation need check whether previous call to Request() has been completed or not.
244 //
245 //
246 Request = HttpMsg->Data.Request;
247 if ((Request == NULL) || (Request->Url == NULL)) {
248 return EFI_INVALID_PARAMETER;
249 }
250
251 //
252 // Only support GET and HEAD method in current implementation.
253 //
254 if ((Request->Method != HttpMethodGet) && (Request->Method != HttpMethodHead)) {
255 return EFI_UNSUPPORTED;
256 }
257
258 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
259 ASSERT (HttpInstance != NULL);
260
261 if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) {
262 return EFI_NOT_STARTED;
263 }
264
265 if (HttpInstance->LocalAddressIsIPv6) {
266 return EFI_UNSUPPORTED;
267 }
268
269 //
270 // Check whether the token already existed.
271 //
272 if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTokenExist, Token))) {
273 return EFI_ACCESS_DENIED;
274 }
275
276 Url = NULL;
277 HostName = NULL;
278 Wrap = NULL;
279 HostNameStr = NULL;
280 TcpWrap = NULL;
281
282 //
283 // Parse the URI of the remote host.
284 //
285 Url = AllocatePool (StrLen (Request->Url) + 1);
286 if (Url == NULL) {
287 return EFI_OUT_OF_RESOURCES;
288 }
289
290 UnicodeStrToAsciiStr (Request->Url, Url);
291 UrlParser = NULL;
292 Status = HttpParseUrl (Url, (UINT32) AsciiStrLen (Url), FALSE, &UrlParser);
293 if (EFI_ERROR (Status)) {
294 goto Error1;
295 }
296
297 RequestStr = NULL;
298 HostName = NULL;
299 Status = HttpUrlGetHostName (Url, UrlParser, &HostName);
300 if (EFI_ERROR (Status)) {
301 goto Error1;
302 }
303
304 Status = HttpUrlGetPort (Url, UrlParser, &RemotePort);
305 if (EFI_ERROR (Status)) {
306 RemotePort = HTTP_DEFAULT_PORT;
307 }
308
309 Configure = TRUE;
310 ReConfigure = TRUE;
311
312 if (HttpInstance->RemoteHost == NULL && HttpInstance->RemotePort == 0) {
313 //
314 // Request() is called the first time.
315 //
316 ReConfigure = FALSE;
317 } else {
318 if ((HttpInstance->RemotePort == RemotePort) &&
319 (AsciiStrCmp (HttpInstance->RemoteHost, HostName) == 0)) {
320 //
321 // Host Name and port number of the request URL are the same with previous call to Request().
322 // Check whether previous TCP packet sent out.
323 //
324 if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTcpNotReady, NULL))) {
325 //
326 // Wrap the HTTP token in HTTP_TOKEN_WRAP
327 //
328 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));
329 if (Wrap == NULL) {
330 Status = EFI_OUT_OF_RESOURCES;
331 goto Error1;
332 }
333
334 Wrap->HttpToken = Token;
335 Wrap->HttpInstance = HttpInstance;
336
337 Status = HttpCreateTcp4TxEvent (Wrap);
338 if (EFI_ERROR (Status)) {
339 goto Error1;
340 }
341
342 Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap);
343 if (EFI_ERROR (Status)) {
344 goto Error1;
345 }
346
347 Wrap->TcpWrap.Method = Request->Method;
348
349 FreePool (Url);
350 FreePool (HostName);
351
352 //
353 // Queue the HTTP token and return.
354 //
355 return EFI_SUCCESS;
356 } else {
357 //
358 // Use existing TCP instance to transmit the packet.
359 //
360 Configure = FALSE;
361 ReConfigure = FALSE;
362 }
363 } else {
364 //
365 // Need close existing TCP instance and create a new TCP instance for data transmit.
366 //
367 if (HttpInstance->RemoteHost != NULL) {
368 FreePool (HttpInstance->RemoteHost);
369 HttpInstance->RemoteHost = NULL;
370 }
371 }
372 }
373
374 if (Configure) {
375 //
376 // Parse Url for IPv4 address, if failed, perform DNS resolution.
377 //
378 Status = NetLibAsciiStrToIp4 (HostName, &HttpInstance->RemoteAddr);
379 if (EFI_ERROR (Status)) {
380 HostNameStr = AllocateZeroPool ((AsciiStrLen (HostName) + 1) * sizeof (UINT16));
381 if (HostNameStr == NULL) {
382 Status = EFI_OUT_OF_RESOURCES;
383 goto Error1;
384 }
385
386 AsciiStrToUnicodeStr (HostName, HostNameStr);
387 Status = HttpDns4 (HttpInstance, HostNameStr, &HttpInstance->RemoteAddr);
388 FreePool (HostNameStr);
389 if (EFI_ERROR (Status)) {
390 goto Error1;
391 }
392 }
393
394 //
395 // Save the RemotePort and RemoteHost.
396 //
397 ASSERT (HttpInstance->RemoteHost == NULL);
398 HttpInstance->RemotePort = RemotePort;
399 HttpInstance->RemoteHost = HostName;
400 HostName = NULL;
401 }
402
403 if (ReConfigure) {
404 //
405 // The request URL is different from previous calls to Request(), close existing TCP instance.
406 //
407 ASSERT (HttpInstance->Tcp4 != NULL);
408 HttpCloseConnection (HttpInstance);
409 EfiHttpCancel (This, NULL);
410 }
411
412 //
413 // Wrap the HTTP token in HTTP_TOKEN_WRAP
414 //
415 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));
416 if (Wrap == NULL) {
417 Status = EFI_OUT_OF_RESOURCES;
418 goto Error1;
419 }
420
421 Wrap->HttpToken = Token;
422 Wrap->HttpInstance = HttpInstance;
423 Wrap->TcpWrap.Method = Request->Method;
424
425 if (Configure) {
426 //
427 // Configure TCP instance.
428 //
429 Status = HttpConfigureTcp4 (HttpInstance, Wrap);
430 if (EFI_ERROR (Status)) {
431 goto Error1;
432 }
433 //
434 // Connect TCP.
435 //
436 Status = HttpConnectTcp4 (HttpInstance);
437 if (EFI_ERROR (Status)) {
438 goto Error2;
439 }
440 } else {
441 //
442 // For the new HTTP token, create TX TCP token events.
443 //
444 Status = HttpCreateTcp4TxEvent (Wrap);
445 if (EFI_ERROR (Status)) {
446 goto Error1;
447 }
448 }
449
450 //
451 // Create request message.
452 //
453 RequestStr = HttpGenRequestString (HttpInstance, HttpMsg, Url);
454 if (RequestStr == NULL) {
455 Status = EFI_OUT_OF_RESOURCES;
456 goto Error3;
457 }
458
459 Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap);
460 if (EFI_ERROR (Status)) {
461 goto Error4;
462 }
463
464 FreePool (Url);
465 if (HostName != NULL) {
466 FreePool (HostName);
467 }
468
469 //
470 // Transmit the request message.
471 //
472 Status = HttpTransmitTcp4 (
473 HttpInstance,
474 Wrap,
475 (UINT8*) RequestStr,
476 AsciiStrLen (RequestStr)
477 );
478 if (EFI_ERROR (Status)) {
479 goto Error5;
480 }
481
482 return EFI_SUCCESS;
483
484 Error5:
485 NetMapRemoveTail (&HttpInstance->TxTokens, NULL);
486
487 Error4:
488 if (RequestStr != NULL) {
489 FreePool (RequestStr);
490 }
491
492 Error3:
493 HttpCloseConnection (HttpInstance);
494
495
496 Error2:
497 HttpCloseTcp4ConnCloseEvent (HttpInstance);
498 if (NULL != Wrap->TcpWrap.TxToken.CompletionToken.Event) {
499 gBS->CloseEvent (Wrap->TcpWrap.TxToken.CompletionToken.Event);
500 }
501
502 Error1:
503 if (Url != NULL) {
504 FreePool (Url);
505 }
506 if (HostName != NULL) {
507 FreePool (HostName);
508 }
509 if (Wrap != NULL) {
510 FreePool (Wrap);
511 }
512 if (UrlParser!= NULL) {
513 HttpUrlFreeParser (UrlParser);
514 }
515
516 return Status;
517
518 }
519
520 /**
521 Cancel a TxToken or RxToken.
522
523 @param[in] Map The HTTP instance's token queue.
524 @param[in] Item Object container for one HTTP token and token's wrap.
525 @param[in] Context The user's token to cancel.
526
527 @retval EFI_SUCCESS Continue to check the next Item.
528 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
529
530 **/
531 EFI_STATUS
532 EFIAPI
533 HttpCancelTokens (
534 IN NET_MAP *Map,
535 IN NET_MAP_ITEM *Item,
536 IN VOID *Context
537 )
538 {
539
540 EFI_HTTP_TOKEN *Token;
541 HTTP_TOKEN_WRAP *Wrap;
542
543 Token = (EFI_HTTP_TOKEN *) Context;
544
545 //
546 // Return EFI_SUCCESS to check the next item in the map if
547 // this one doesn't match.
548 //
549 if ((Token != NULL) && (Token != Item->Key)) {
550 return EFI_SUCCESS;
551 }
552
553 Wrap = (HTTP_TOKEN_WRAP *) Item->Value;
554 ASSERT (Wrap != NULL);
555
556 //
557 // Free resources.
558 //
559 NetMapRemoveItem (Map, Item, NULL);
560
561 if (Wrap->TcpWrap.TxToken.CompletionToken.Event != NULL) {
562 gBS->CloseEvent (Wrap->TcpWrap.TxToken.CompletionToken.Event);
563 }
564
565 if (Wrap->TcpWrap.RxToken.CompletionToken.Event != NULL) {
566 gBS->CloseEvent (Wrap->TcpWrap.RxToken.CompletionToken.Event);
567 }
568
569 if (Wrap->TcpWrap.RxToken.Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {
570 FreePool (Wrap->TcpWrap.RxToken.Packet.RxData->FragmentTable[0].FragmentBuffer);
571 }
572
573 FreePool (Wrap);
574
575 //
576 // If only one item is to be cancel, return EFI_ABORTED to stop
577 // iterating the map any more.
578 //
579 if (Token != NULL) {
580 return EFI_ABORTED;
581 }
582
583 return EFI_SUCCESS;
584 }
585
586 /**
587 Cancel the user's receive/transmit request. It is the worker function of
588 EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the
589 token.
590
591 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
592 @param[in] Token The token to cancel. If NULL, all token will be
593 cancelled.
594
595 @retval EFI_SUCCESS The token is cancelled.
596 @retval EFI_NOT_FOUND The asynchronous request or response token is not found.
597 @retval Others Other error as indicated.
598
599 **/
600 EFI_STATUS
601 HttpCancel (
602 IN HTTP_PROTOCOL *HttpInstance,
603 IN EFI_HTTP_TOKEN *Token
604 )
605 {
606 EFI_STATUS Status;
607
608 //
609 // First check the tokens queued by EfiHttpRequest().
610 //
611 Status = NetMapIterate (&HttpInstance->TxTokens, HttpCancelTokens, Token);
612 if (EFI_ERROR (Status)) {
613 if (Token != NULL) {
614 if (Status == EFI_ABORTED) {
615 return EFI_SUCCESS;
616 }
617 } else {
618 return Status;
619 }
620 }
621
622 //
623 // Then check the tokens queued by EfiHttpResponse().
624 //
625 Status = NetMapIterate (&HttpInstance->RxTokens, HttpCancelTokens, Token);
626 if (EFI_ERROR (Status)) {
627 if (Token != NULL) {
628 if (Status == EFI_ABORTED) {
629 return EFI_SUCCESS;
630 } else {
631 return EFI_NOT_FOUND;
632 }
633 } else {
634 return Status;
635 }
636 }
637
638 return EFI_SUCCESS;
639 }
640
641
642 /**
643 Abort an asynchronous HTTP request or response token.
644
645 The Cancel() function aborts a pending HTTP request or response transaction. If
646 Token is not NULL and the token is in transmit or receive queues when it is being
647 cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
648 be signaled. If the token is not in one of the queues, which usually means that the
649 asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
650 all asynchronous tokens issued by Request() or Response() will be aborted.
651
652 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
653 @param[in] Token Point to storage containing HTTP request or response
654 token.
655
656 @retval EFI_SUCCESS Request and Response queues are successfully flushed.
657 @retval EFI_INVALID_PARAMETER This is NULL.
658 @retval EFI_NOT_STARTED This instance hasn't been configured.
659 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP,
660 BOOTP, RARP, etc.) hasn't finished yet.
661 @retval EFI_NOT_FOUND The asynchronous request or response token is not
662 found.
663 @retval EFI_UNSUPPORTED The implementation does not support this function.
664
665 **/
666 EFI_STATUS
667 EFIAPI
668 EfiHttpCancel (
669 IN EFI_HTTP_PROTOCOL *This,
670 IN EFI_HTTP_TOKEN *Token
671 )
672 {
673 HTTP_PROTOCOL *HttpInstance;
674
675 if (This == NULL) {
676 return EFI_INVALID_PARAMETER;
677 }
678
679 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
680 ASSERT (HttpInstance != NULL);
681
682 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {
683 return EFI_NOT_STARTED;
684 }
685
686 return HttpCancel (HttpInstance, Token);
687
688 }
689
690 /**
691 A callback function to intercept events during message parser.
692
693 This function will be invoked during HttpParseMessageBody() with various events type. An error
694 return status of the callback function will cause the HttpParseMessageBody() aborted.
695
696 @param[in] EventType Event type of this callback call.
697 @param[in] Data A pointer to data buffer.
698 @param[in] Length Length in bytes of the Data.
699 @param[in] Context Callback context set by HttpInitMsgParser().
700
701 @retval EFI_SUCCESS Continue to parser the message body.
702
703 **/
704 EFI_STATUS
705 EFIAPI
706 HttpBodyParserCallback (
707 IN HTTP_BODY_PARSE_EVENT EventType,
708 IN CHAR8 *Data,
709 IN UINTN Length,
710 IN VOID *Context
711 )
712 {
713 HTTP_TOKEN_WRAP *Wrap;
714
715 if (EventType != BodyParseEventOnComplete) {
716 return EFI_SUCCESS;
717 }
718
719 if (Data == NULL || Length != 0 || Context == NULL) {
720 return EFI_SUCCESS;
721 }
722
723 Wrap = (HTTP_TOKEN_WRAP *) Context;
724 Wrap->HttpInstance->NextMsg = Data;
725
726 //
727 // Free TxToken since already received corrsponding HTTP response.
728 //
729 FreePool (Wrap);
730
731 return EFI_SUCCESS;
732 }
733
734 /**
735 The work function of EfiHttpResponse().
736
737 @param[in] Wrap Pointer to HTTP token's wrap data.
738
739 @retval EFI_SUCCESS Allocation succeeded.
740 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
741 @retval EFI_NOT_READY Can't find a corresponding TxToken.
742
743 **/
744 EFI_STATUS
745 HttpResponseWorker (
746 IN HTTP_TOKEN_WRAP *Wrap
747 )
748 {
749 EFI_STATUS Status;
750 EFI_HTTP_MESSAGE *HttpMsg;
751 EFI_TCP4_IO_TOKEN *RxToken;
752 EFI_TCP4_PROTOCOL *Tcp4;
753 CHAR8 *EndofHeader;
754 CHAR8 *HttpHeaders;
755 UINTN SizeofHeaders;
756 CHAR8 *Buffer;
757 UINTN BufferSize;
758 UINTN StatusCode;
759 CHAR8 *Tmp;
760 CHAR8 *HeaderTmp;
761 CHAR8 *StatusCodeStr;
762 UINTN BodyLen;
763 HTTP_PROTOCOL *HttpInstance;
764 EFI_HTTP_TOKEN *Token;
765 NET_MAP_ITEM *Item;
766 HTTP_TOKEN_WRAP *ValueInItem;
767 UINTN HdrLen;
768
769 if (Wrap == NULL || Wrap->HttpInstance == NULL) {
770 return EFI_INVALID_PARAMETER;
771 }
772
773 HttpInstance = Wrap->HttpInstance;
774 Token = Wrap->HttpToken;
775
776 HttpMsg = Token->Message;
777
778 Tcp4 = HttpInstance->Tcp4;
779 ASSERT (Tcp4 != NULL);
780 HttpMsg->Headers = NULL;
781 HttpHeaders = NULL;
782 SizeofHeaders = 0;
783 Buffer = NULL;
784 BufferSize = 0;
785 EndofHeader = NULL;
786
787 if (HttpMsg->Data.Response != NULL) {
788 //
789 // Need receive the HTTP headers, prepare buffer.
790 //
791 Status = HttpCreateTcp4RxEventForHeader (HttpInstance);
792 if (EFI_ERROR (Status)) {
793 goto Error;
794 }
795
796 //
797 // Check whether we have cached header from previous call.
798 //
799 if ((HttpInstance->CacheBody != NULL) && (HttpInstance->NextMsg != NULL)) {
800 //
801 // The data is stored at [NextMsg, CacheBody + CacheLen].
802 //
803 HdrLen = HttpInstance->CacheBody + HttpInstance->CacheLen - HttpInstance->NextMsg;
804 HttpHeaders = AllocateZeroPool (HdrLen);
805 if (HttpHeaders == NULL) {
806 Status = EFI_OUT_OF_RESOURCES;
807 goto Error;
808 }
809
810 CopyMem (HttpHeaders, HttpInstance->NextMsg, HdrLen);
811 FreePool (HttpInstance->CacheBody);
812 HttpInstance->CacheBody = NULL;
813 HttpInstance->NextMsg = NULL;
814 HttpInstance->CacheOffset = 0;
815 SizeofHeaders = HdrLen;
816 BufferSize = HttpInstance->CacheLen;
817
818 //
819 // Check whether we cached the whole HTTP headers.
820 //
821 EndofHeader = AsciiStrStr (HttpHeaders, HTTP_END_OF_HDR_STR);
822 }
823
824 RxToken = &HttpInstance->RxToken;
825 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = AllocateZeroPool (DEF_BUF_LEN);
826 if (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer == NULL) {
827 Status = EFI_OUT_OF_RESOURCES;
828 goto Error;
829 }
830
831 //
832 // Receive the HTTP headers only when EFI_HTTP_RESPONSE_DATA is not NULL.
833 //
834 while (EndofHeader == NULL) {
835 HttpInstance->IsRxDone = FALSE;
836 RxToken->Packet.RxData->DataLength = DEF_BUF_LEN;
837 RxToken->Packet.RxData->FragmentTable[0].FragmentLength = DEF_BUF_LEN;
838 Status = Tcp4->Receive (Tcp4, RxToken);
839 if (EFI_ERROR (Status)) {
840 DEBUG ((EFI_D_ERROR, "Tcp4 receive failed: %r\n", Status));
841 goto Error;
842 }
843
844 while (!HttpInstance->IsRxDone) {
845 Tcp4->Poll (Tcp4);
846 }
847
848 Status = RxToken->CompletionToken.Status;
849 if (EFI_ERROR (Status)) {
850 goto Error;
851 }
852
853 //
854 // Append the response string.
855 //
856 BufferSize = SizeofHeaders + RxToken->Packet.RxData->FragmentTable[0].FragmentLength;
857 Buffer = AllocateZeroPool (BufferSize);
858 if (Buffer == NULL) {
859 Status = EFI_OUT_OF_RESOURCES;
860 goto Error;
861 }
862
863 if (HttpHeaders != NULL) {
864 CopyMem (Buffer, HttpHeaders, SizeofHeaders);
865 FreePool (HttpHeaders);
866 }
867
868 CopyMem (
869 Buffer + SizeofHeaders,
870 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer,
871 RxToken->Packet.RxData->FragmentTable[0].FragmentLength
872 );
873 HttpHeaders = Buffer;
874 SizeofHeaders = BufferSize;
875
876 //
877 // Check whether we received end of HTTP headers.
878 //
879 EndofHeader = AsciiStrStr (HttpHeaders, HTTP_END_OF_HDR_STR);
880 };
881
882 //
883 // Skip the CRLF after the HTTP headers.
884 //
885 EndofHeader = EndofHeader + AsciiStrLen (HTTP_END_OF_HDR_STR);
886
887 //
888 // Cache the part of body.
889 //
890 BodyLen = BufferSize - (EndofHeader - HttpHeaders);
891 if (BodyLen > 0) {
892 if (HttpInstance->CacheBody != NULL) {
893 FreePool (HttpInstance->CacheBody);
894 }
895
896 HttpInstance->CacheBody = AllocateZeroPool (BodyLen);
897 if (HttpInstance->CacheBody == NULL) {
898 Status = EFI_OUT_OF_RESOURCES;
899 goto Error;
900 }
901
902 CopyMem (HttpInstance->CacheBody, EndofHeader, BodyLen);
903 HttpInstance->CacheLen = BodyLen;
904 }
905
906 FreePool (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer);
907 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL;
908
909 //
910 // Search for Status Code.
911 //
912 StatusCodeStr = HttpHeaders + AsciiStrLen (HTTP_VERSION_STR) + 1;
913 if (StatusCodeStr == NULL) {
914 goto Error;
915 }
916
917 StatusCode = AsciiStrDecimalToUintn (StatusCodeStr);
918
919 //
920 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
921 //
922 Tmp = AsciiStrStr (HttpHeaders, HTTP_CRLF_STR);
923 if (Tmp == NULL) {
924 goto Error;
925 }
926
927 Tmp = Tmp + AsciiStrLen (HTTP_CRLF_STR);
928 SizeofHeaders = SizeofHeaders - (Tmp - HttpHeaders);
929 HeaderTmp = AllocateZeroPool (SizeofHeaders);
930 if (HeaderTmp == NULL) {
931 goto Error;
932 }
933
934 CopyMem (HeaderTmp, Tmp, SizeofHeaders);
935 FreePool (HttpHeaders);
936 HttpHeaders = HeaderTmp;
937 //
938 // Parse the HTTP header into array of key/value pairs.
939 //
940 Status = HttpUtilitiesParse (HttpHeaders, SizeofHeaders, &HttpMsg->Headers, &HttpMsg->HeaderCount);
941 if (EFI_ERROR (Status)) {
942 goto Error;
943 }
944
945 FreePool (HttpHeaders);
946 HttpHeaders = NULL;
947
948 HttpMsg->Data.Response->StatusCode = HttpMappingToStatusCode (StatusCode);
949
950 //
951 // Init message-body parser by header information.
952 //
953 Status = EFI_NOT_READY;
954 ValueInItem = NULL;
955 NetMapRemoveHead (&HttpInstance->TxTokens, (VOID**) &ValueInItem);
956 if (ValueInItem == NULL) {
957 goto Error;
958 }
959
960 //
961 // The first TxToken not transmitted yet, insert back and return error.
962 //
963 if (!ValueInItem->TcpWrap.IsTxDone) {
964 goto Error2;
965 }
966
967 Status = HttpInitMsgParser (
968 ValueInItem->TcpWrap.Method,
969 HttpMsg->Data.Response->StatusCode,
970 HttpMsg->HeaderCount,
971 HttpMsg->Headers,
972 HttpBodyParserCallback,
973 (VOID *) ValueInItem,
974 &HttpInstance->MsgParser
975 );
976 if (EFI_ERROR (Status)) {
977 goto Error2;
978 }
979
980 //
981 // Check whether we received a complete HTTP message.
982 //
983 if (HttpInstance->CacheBody != NULL) {
984 Status = HttpParseMessageBody (HttpInstance->MsgParser, HttpInstance->CacheLen, HttpInstance->CacheBody);
985 if (EFI_ERROR (Status)) {
986 goto Error2;
987 }
988
989 if (HttpIsMessageComplete (HttpInstance->MsgParser)) {
990 //
991 // Free the MsgParse since we already have a full HTTP message.
992 //
993 HttpFreeMsgParser (HttpInstance->MsgParser);
994 HttpInstance->MsgParser = NULL;
995 }
996 }
997
998 if ((HttpMsg->Body == NULL) || (HttpMsg->BodyLength == 0)) {
999 Status = EFI_SUCCESS;
1000 goto Exit;
1001 }
1002 }
1003
1004 //
1005 // Receive the response body.
1006 //
1007 BodyLen = 0;
1008
1009 //
1010 // First check whether we cached some data.
1011 //
1012 if (HttpInstance->CacheBody != NULL) {
1013 //
1014 // Calculate the length of the cached data.
1015 //
1016 if (HttpInstance->NextMsg != NULL) {
1017 //
1018 // We have a cached HTTP message which includes a part of HTTP header of next message.
1019 //
1020 BodyLen = HttpInstance->NextMsg - (HttpInstance->CacheBody + HttpInstance->CacheOffset);
1021 } else {
1022 BodyLen = HttpInstance->CacheLen - HttpInstance->CacheOffset;
1023 }
1024
1025 if (BodyLen > 0) {
1026 //
1027 // We have some cached data. Just copy the data and return.
1028 //
1029 if (HttpMsg->BodyLength < BodyLen) {
1030 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, HttpMsg->BodyLength);
1031 HttpInstance->CacheOffset = HttpInstance->CacheOffset + HttpMsg->BodyLength;
1032 } else {
1033 //
1034 // Copy all cached data out.
1035 //
1036 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, BodyLen);
1037 HttpInstance->CacheOffset = BodyLen + HttpInstance->CacheOffset;
1038 HttpMsg->BodyLength = BodyLen;
1039
1040 if (HttpInstance->NextMsg == NULL) {
1041 //
1042 // There is no HTTP header of next message. Just free the cache buffer.
1043 //
1044 FreePool (HttpInstance->CacheBody);
1045 HttpInstance->CacheBody = NULL;
1046 HttpInstance->NextMsg = NULL;
1047 HttpInstance->CacheOffset = 0;
1048 }
1049 }
1050 //
1051 // Return since we aready received required data.
1052 //
1053 Status = EFI_SUCCESS;
1054 goto Exit;
1055 }
1056
1057 if (BodyLen == 0 && HttpInstance->MsgParser == NULL) {
1058 //
1059 // We received a complete HTTP message, and we don't have more data to return to caller.
1060 //
1061 HttpMsg->BodyLength = 0;
1062 Status = EFI_SUCCESS;
1063 goto Exit;
1064 }
1065 }
1066
1067 ASSERT (HttpInstance->MsgParser != NULL);
1068
1069 //
1070 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1071 //
1072 RxToken = &Wrap->TcpWrap.RxToken;
1073
1074 RxToken->Packet.RxData->DataLength = (UINT32) HttpMsg->BodyLength;
1075 RxToken->Packet.RxData->FragmentTable[0].FragmentLength = (UINT32) HttpMsg->BodyLength;
1076 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = (VOID *) HttpMsg->Body;
1077
1078 RxToken->CompletionToken.Status = EFI_NOT_READY;
1079 Status = Tcp4->Receive (Tcp4, RxToken);
1080 if (EFI_ERROR (Status)) {
1081 DEBUG ((EFI_D_ERROR, "Tcp4 receive failed: %r\n", Status));
1082 goto Error;
1083 }
1084
1085 return Status;
1086
1087 Exit:
1088 Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);
1089 if (Item != NULL) {
1090 NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);
1091 }
1092 Token->Status = Status;
1093 gBS->SignalEvent (Token->Event);
1094 FreePool (Wrap);
1095 return Status;
1096
1097 Error2:
1098 NetMapInsertHead (&HttpInstance->TxTokens, ValueInItem->HttpToken, ValueInItem);
1099
1100 Error:
1101 if (Wrap != NULL) {
1102 if (Wrap->TcpWrap.RxToken.CompletionToken.Event != NULL) {
1103 gBS->CloseEvent (Wrap->TcpWrap.RxToken.CompletionToken.Event);
1104 }
1105 RxToken = &Wrap->TcpWrap.RxToken;
1106 if (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {
1107 FreePool (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer);
1108 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL;
1109 }
1110 FreePool (Wrap);
1111 }
1112
1113 if (HttpInstance->RxToken.CompletionToken.Event != NULL) {
1114 gBS->CloseEvent (HttpInstance->RxToken.CompletionToken.Event);
1115 HttpInstance->RxToken.CompletionToken.Event = NULL;
1116 }
1117
1118 RxToken = &HttpInstance->RxToken;
1119 if (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {
1120 FreePool (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer);
1121 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL;
1122 }
1123
1124 if (HttpHeaders != NULL) {
1125 FreePool (HttpHeaders);
1126 }
1127
1128 if (HttpMsg->Headers != NULL) {
1129 FreePool (HttpMsg->Headers);
1130 }
1131
1132 if (HttpInstance->CacheBody != NULL) {
1133 FreePool (HttpInstance->CacheBody);
1134 HttpInstance->CacheBody = NULL;
1135 }
1136
1137 Token->Status = Status;
1138 gBS->SignalEvent (Token->Event);
1139
1140 return Status;
1141
1142 }
1143
1144
1145 /**
1146 The Response() function queues an HTTP response to this HTTP instance, similar to
1147 Receive() function in the EFI TCP driver. When the HTTP request is sent successfully,
1148 or if there is an error, Status in token will be updated and Event will be signaled.
1149
1150 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1151 is received in the underlying TCP instance, the data will be parsed and Token will
1152 be populated with the response data. If the data received from the remote host
1153 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1154 (asynchronously) for more data to be sent from the remote host before signaling
1155 Event in Token.
1156
1157 It is the responsibility of the caller to allocate a buffer for Body and specify the
1158 size in BodyLength. If the remote host provides a response that contains a content
1159 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1160 BodyLength will be updated with the amount of bytes received and copied to Body. This
1161 allows the client to download a large file in chunks instead of into one contiguous
1162 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1163 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1164 token to underlying TCP instance. If data arrives in the receive buffer, up to
1165 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1166 BodyLength with the amount of bytes received and copied to Body.
1167
1168 If the HTTP driver does not have an open underlying TCP connection with the host
1169 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1170 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1171 an open TCP connection between client and host.
1172
1173 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1174 @param[in] Token Pointer to storage containing HTTP response token.
1175
1176 @retval EFI_SUCCESS Allocation succeeded.
1177 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1178 initialized.
1179 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1180 This is NULL.
1181 Token is NULL.
1182 Token->Message->Headers is NULL.
1183 Token->Message is NULL.
1184 Token->Message->Body is not NULL,
1185 Token->Message->BodyLength is non-zero, and
1186 Token->Message->Data is NULL, but a previous call to
1187 Response() has not been completed successfully.
1188 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1189 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1190 specified by response URL.
1191 **/
1192 EFI_STATUS
1193 EFIAPI
1194 EfiHttpResponse (
1195 IN EFI_HTTP_PROTOCOL *This,
1196 IN EFI_HTTP_TOKEN *Token
1197 )
1198 {
1199 EFI_STATUS Status;
1200 EFI_HTTP_MESSAGE *HttpMsg;
1201 HTTP_PROTOCOL *HttpInstance;
1202 HTTP_TOKEN_WRAP *Wrap;
1203
1204 if ((This == NULL) || (Token == NULL)) {
1205 return EFI_INVALID_PARAMETER;
1206 }
1207
1208 HttpMsg = Token->Message;
1209 if (HttpMsg == NULL) {
1210 return EFI_INVALID_PARAMETER;
1211 }
1212
1213 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
1214 ASSERT (HttpInstance != NULL);
1215
1216 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {
1217 return EFI_NOT_STARTED;
1218 }
1219
1220 if (HttpInstance->LocalAddressIsIPv6) {
1221 return EFI_UNSUPPORTED;
1222 }
1223
1224 //
1225 // Check whether the token already existed.
1226 //
1227 if (EFI_ERROR (NetMapIterate (&HttpInstance->RxTokens, HttpTokenExist, Token))) {
1228 return EFI_ACCESS_DENIED;
1229 }
1230
1231 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));
1232 if (Wrap == NULL) {
1233 return EFI_OUT_OF_RESOURCES;
1234 }
1235
1236 Wrap->HttpInstance = HttpInstance;
1237 Wrap->HttpToken = Token;
1238
1239 Status = HttpCreateTcp4RxEvent (Wrap);
1240 if (EFI_ERROR (Status)) {
1241 goto Error;
1242 }
1243
1244 Status = NetMapInsertTail (&HttpInstance->RxTokens, Token, Wrap);
1245 if (EFI_ERROR (Status)) {
1246 goto Error;
1247 }
1248
1249 //
1250 // If already have pending RxTokens, return directly.
1251 //
1252 if (NetMapGetCount (&HttpInstance->RxTokens) > 1) {
1253 return EFI_SUCCESS;
1254 }
1255
1256 return HttpResponseWorker (Wrap);
1257
1258 Error:
1259 if (Wrap != NULL) {
1260 if (Wrap->TcpWrap.RxToken.CompletionToken.Event != NULL) {
1261 gBS->CloseEvent (Wrap->TcpWrap.RxToken.CompletionToken.Event);
1262 }
1263 FreePool (Wrap);
1264 }
1265
1266 return Status;
1267 }
1268
1269 /**
1270 The Poll() function can be used by network drivers and applications to increase the
1271 rate that data packets are moved between the communication devices and the transmit
1272 and receive queues.
1273
1274 In some systems, the periodic timer event in the managed network driver may not poll
1275 the underlying communications device fast enough to transmit and/or receive all data
1276 packets without missing incoming packets or dropping outgoing packets. Drivers and
1277 applications that are experiencing packet loss should try calling the Poll() function
1278 more often.
1279
1280 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1281
1282 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1283 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1284 @retval EFI_INVALID_PARAMETER This is NULL.
1285 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1286 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1287
1288 **/
1289 EFI_STATUS
1290 EFIAPI
1291 EfiHttpPoll (
1292 IN EFI_HTTP_PROTOCOL *This
1293 )
1294 {
1295 HTTP_PROTOCOL *HttpInstance;
1296
1297 if (This == NULL) {
1298 return EFI_INVALID_PARAMETER;
1299 }
1300
1301 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
1302 ASSERT (HttpInstance != NULL);
1303
1304 if (HttpInstance->LocalAddressIsIPv6) {
1305 return EFI_UNSUPPORTED;
1306 }
1307
1308 if (HttpInstance->Tcp4 == NULL || HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {
1309 return EFI_NOT_STARTED;
1310 }
1311
1312 return HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);
1313 }