]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/HttpDxe/HttpImpl.c
4bd4ac884fdcffdcbe303d998c35178d70202fe9
[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 HttpInstance = Wrap->HttpInstance;
770 Token = Wrap->HttpToken;
771
772 HttpMsg = Token->Message;
773
774 Tcp4 = HttpInstance->Tcp4;
775 ASSERT (Tcp4 != NULL);
776 HttpMsg->Headers = NULL;
777 HttpHeaders = NULL;
778 SizeofHeaders = 0;
779 Buffer = NULL;
780 BufferSize = 0;
781 EndofHeader = NULL;
782
783 if (HttpMsg->Data.Response != NULL) {
784 //
785 // Need receive the HTTP headers, prepare buffer.
786 //
787 Status = HttpCreateTcp4RxEventForHeader (HttpInstance);
788 if (EFI_ERROR (Status)) {
789 goto Error;
790 }
791
792 //
793 // Check whether we have cached header from previous call.
794 //
795 if ((HttpInstance->CacheBody != NULL) && (HttpInstance->NextMsg != NULL)) {
796 //
797 // The data is stored at [NextMsg, CacheBody + CacheLen].
798 //
799 HdrLen = HttpInstance->CacheBody + HttpInstance->CacheLen - HttpInstance->NextMsg;
800 HttpHeaders = AllocateZeroPool (HdrLen);
801 if (HttpHeaders == NULL) {
802 Status = EFI_OUT_OF_RESOURCES;
803 goto Error;
804 }
805
806 CopyMem (HttpHeaders, HttpInstance->NextMsg, HdrLen);
807 FreePool (HttpInstance->CacheBody);
808 HttpInstance->CacheBody = NULL;
809 HttpInstance->NextMsg = NULL;
810 HttpInstance->CacheOffset = 0;
811 SizeofHeaders = HdrLen;
812 BufferSize = HttpInstance->CacheLen;
813
814 //
815 // Check whether we cached the whole HTTP headers.
816 //
817 EndofHeader = AsciiStrStr (HttpHeaders, HTTP_END_OF_HDR_STR);
818 }
819
820 RxToken = &HttpInstance->RxToken;
821 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = AllocateZeroPool (DEF_BUF_LEN);
822 if (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer == NULL) {
823 Status = EFI_OUT_OF_RESOURCES;
824 goto Error;
825 }
826
827 //
828 // Receive the HTTP headers only when EFI_HTTP_RESPONSE_DATA is not NULL.
829 //
830 while (EndofHeader == NULL) {
831 HttpInstance->IsRxDone = FALSE;
832 RxToken->Packet.RxData->DataLength = DEF_BUF_LEN;
833 RxToken->Packet.RxData->FragmentTable[0].FragmentLength = DEF_BUF_LEN;
834 Status = Tcp4->Receive (Tcp4, RxToken);
835 if (EFI_ERROR (Status)) {
836 DEBUG ((EFI_D_ERROR, "Tcp4 receive failed: %r\n", Status));
837 goto Error;
838 }
839
840 while (!HttpInstance->IsRxDone) {
841 Tcp4->Poll (Tcp4);
842 }
843
844 Status = RxToken->CompletionToken.Status;
845 if (EFI_ERROR (Status)) {
846 goto Error;
847 }
848
849 //
850 // Append the response string.
851 //
852 BufferSize = SizeofHeaders + RxToken->Packet.RxData->FragmentTable[0].FragmentLength;
853 Buffer = AllocateZeroPool (BufferSize);
854 if (Buffer == NULL) {
855 Status = EFI_OUT_OF_RESOURCES;
856 goto Error;
857 }
858
859 if (HttpHeaders != NULL) {
860 CopyMem (Buffer, HttpHeaders, SizeofHeaders);
861 FreePool (HttpHeaders);
862 }
863
864 CopyMem (
865 Buffer + SizeofHeaders,
866 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer,
867 RxToken->Packet.RxData->FragmentTable[0].FragmentLength
868 );
869 HttpHeaders = Buffer;
870 SizeofHeaders = BufferSize;
871
872 //
873 // Check whether we received end of HTTP headers.
874 //
875 EndofHeader = AsciiStrStr (HttpHeaders, HTTP_END_OF_HDR_STR);
876 };
877
878 //
879 // Skip the CRLF after the HTTP headers.
880 //
881 EndofHeader = EndofHeader + AsciiStrLen (HTTP_END_OF_HDR_STR);
882
883 //
884 // Cache the part of body.
885 //
886 BodyLen = BufferSize - (EndofHeader - HttpHeaders);
887 if (BodyLen > 0) {
888 if (HttpInstance->CacheBody != NULL) {
889 FreePool (HttpInstance->CacheBody);
890 }
891
892 HttpInstance->CacheBody = AllocateZeroPool (BodyLen);
893 if (HttpInstance->CacheBody == NULL) {
894 Status = EFI_OUT_OF_RESOURCES;
895 goto Error;
896 }
897
898 CopyMem (HttpInstance->CacheBody, EndofHeader, BodyLen);
899 HttpInstance->CacheLen = BodyLen;
900 }
901
902 FreePool (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer);
903 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL;
904
905 //
906 // Search for Status Code.
907 //
908 StatusCodeStr = HttpHeaders + AsciiStrLen (HTTP_VERSION_STR) + 1;
909 if (StatusCodeStr == NULL) {
910 goto Error;
911 }
912
913 StatusCode = AsciiStrDecimalToUintn (StatusCodeStr);
914
915 //
916 // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".
917 //
918 Tmp = AsciiStrStr (HttpHeaders, HTTP_CRLF_STR);
919 if (Tmp == NULL) {
920 goto Error;
921 }
922
923 Tmp = Tmp + AsciiStrLen (HTTP_CRLF_STR);
924 SizeofHeaders = SizeofHeaders - (Tmp - HttpHeaders);
925 HeaderTmp = AllocateZeroPool (SizeofHeaders);
926 if (HeaderTmp == NULL) {
927 goto Error;
928 }
929
930 CopyMem (HeaderTmp, Tmp, SizeofHeaders);
931 FreePool (HttpHeaders);
932 HttpHeaders = HeaderTmp;
933 //
934 // Parse the HTTP header into array of key/value pairs.
935 //
936 Status = HttpUtilitiesParse (HttpHeaders, SizeofHeaders, &HttpMsg->Headers, &HttpMsg->HeaderCount);
937 if (EFI_ERROR (Status)) {
938 goto Error;
939 }
940
941 FreePool (HttpHeaders);
942 HttpHeaders = NULL;
943
944 HttpMsg->Data.Response->StatusCode = HttpMappingToStatusCode (StatusCode);
945
946 //
947 // Init message-body parser by header information.
948 //
949 Status = EFI_NOT_READY;
950 ValueInItem = NULL;
951 NetMapRemoveHead (&HttpInstance->TxTokens, (VOID**) &ValueInItem);
952 if (ValueInItem == NULL) {
953 goto Error;
954 }
955
956 //
957 // The first TxToken not transmitted yet, insert back and return error.
958 //
959 if (!ValueInItem->TcpWrap.IsTxDone) {
960 goto Error2;
961 }
962
963 Status = HttpInitMsgParser (
964 ValueInItem->TcpWrap.Method,
965 HttpMsg->Data.Response->StatusCode,
966 HttpMsg->HeaderCount,
967 HttpMsg->Headers,
968 HttpBodyParserCallback,
969 (VOID *) ValueInItem,
970 &HttpInstance->MsgParser
971 );
972 if (EFI_ERROR (Status)) {
973 goto Error2;
974 }
975
976 //
977 // Check whether we received a complete HTTP message.
978 //
979 if (HttpInstance->CacheBody != NULL) {
980 Status = HttpParseMessageBody (HttpInstance->MsgParser, HttpInstance->CacheLen, HttpInstance->CacheBody);
981 if (EFI_ERROR (Status)) {
982 goto Error2;
983 }
984
985 if (HttpIsMessageComplete (HttpInstance->MsgParser)) {
986 //
987 // Free the MsgParse since we already have a full HTTP message.
988 //
989 HttpFreeMsgParser (HttpInstance->MsgParser);
990 HttpInstance->MsgParser = NULL;
991 }
992 }
993
994 if ((HttpMsg->Body == NULL) || (HttpMsg->BodyLength == 0)) {
995 Status = EFI_SUCCESS;
996 goto Exit;
997 }
998 }
999
1000 //
1001 // Receive the response body.
1002 //
1003 BodyLen = 0;
1004
1005 //
1006 // First check whether we cached some data.
1007 //
1008 if (HttpInstance->CacheBody != NULL) {
1009 //
1010 // Calculate the length of the cached data.
1011 //
1012 if (HttpInstance->NextMsg != NULL) {
1013 //
1014 // We have a cached HTTP message which includes a part of HTTP header of next message.
1015 //
1016 BodyLen = HttpInstance->NextMsg - (HttpInstance->CacheBody + HttpInstance->CacheOffset);
1017 } else {
1018 BodyLen = HttpInstance->CacheLen - HttpInstance->CacheOffset;
1019 }
1020
1021 if (BodyLen > 0) {
1022 //
1023 // We have some cached data. Just copy the data and return.
1024 //
1025 if (HttpMsg->BodyLength < BodyLen) {
1026 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, HttpMsg->BodyLength);
1027 HttpInstance->CacheOffset = HttpInstance->CacheOffset + HttpMsg->BodyLength;
1028 } else {
1029 //
1030 // Copy all cached data out.
1031 //
1032 CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, BodyLen);
1033 HttpInstance->CacheOffset = BodyLen + HttpInstance->CacheOffset;
1034 HttpMsg->BodyLength = BodyLen;
1035
1036 if (HttpInstance->NextMsg == NULL) {
1037 //
1038 // There is no HTTP header of next message. Just free the cache buffer.
1039 //
1040 FreePool (HttpInstance->CacheBody);
1041 HttpInstance->CacheBody = NULL;
1042 HttpInstance->NextMsg = NULL;
1043 HttpInstance->CacheOffset = 0;
1044 }
1045 }
1046 //
1047 // Return since we aready received required data.
1048 //
1049 Status = EFI_SUCCESS;
1050 goto Exit;
1051 }
1052
1053 if (BodyLen == 0 && HttpInstance->MsgParser == NULL) {
1054 //
1055 // We received a complete HTTP message, and we don't have more data to return to caller.
1056 //
1057 HttpMsg->BodyLength = 0;
1058 Status = EFI_SUCCESS;
1059 goto Exit;
1060 }
1061 }
1062
1063 ASSERT (HttpInstance->MsgParser != NULL);
1064
1065 //
1066 // We still need receive more data when there is no cache data and MsgParser is not NULL;
1067 //
1068 RxToken = &Wrap->TcpWrap.RxToken;
1069
1070 RxToken->Packet.RxData->DataLength = (UINT32) HttpMsg->BodyLength;
1071 RxToken->Packet.RxData->FragmentTable[0].FragmentLength = (UINT32) HttpMsg->BodyLength;
1072 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = (VOID *) HttpMsg->Body;
1073
1074 RxToken->CompletionToken.Status = EFI_NOT_READY;
1075 Status = Tcp4->Receive (Tcp4, RxToken);
1076 if (EFI_ERROR (Status)) {
1077 DEBUG ((EFI_D_ERROR, "Tcp4 receive failed: %r\n", Status));
1078 goto Error;
1079 }
1080
1081 return Status;
1082
1083 Exit:
1084 Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);
1085 if (Item != NULL) {
1086 NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);
1087 }
1088 Token->Status = Status;
1089 gBS->SignalEvent (Token->Event);
1090 FreePool (Wrap);
1091 return Status;
1092
1093 Error2:
1094 NetMapInsertHead (&HttpInstance->TxTokens, ValueInItem->HttpToken, ValueInItem);
1095
1096 Error:
1097 if (Wrap != NULL) {
1098 if (Wrap->TcpWrap.RxToken.CompletionToken.Event != NULL) {
1099 gBS->CloseEvent (Wrap->TcpWrap.RxToken.CompletionToken.Event);
1100 }
1101 RxToken = &Wrap->TcpWrap.RxToken;
1102 if (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {
1103 FreePool (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer);
1104 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL;
1105 }
1106 FreePool (Wrap);
1107 }
1108
1109 if (HttpInstance->RxToken.CompletionToken.Event != NULL) {
1110 gBS->CloseEvent (HttpInstance->RxToken.CompletionToken.Event);
1111 HttpInstance->RxToken.CompletionToken.Event = NULL;
1112 }
1113
1114 RxToken = &HttpInstance->RxToken;
1115 if (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {
1116 FreePool (RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer);
1117 RxToken->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL;
1118 }
1119
1120 if (HttpHeaders != NULL) {
1121 FreePool (HttpHeaders);
1122 }
1123
1124 if (HttpMsg->Headers != NULL) {
1125 FreePool (HttpMsg->Headers);
1126 }
1127
1128 if (HttpInstance->CacheBody != NULL) {
1129 FreePool (HttpInstance->CacheBody);
1130 HttpInstance->CacheBody = NULL;
1131 }
1132
1133 Token->Status = Status;
1134 gBS->SignalEvent (Token->Event);
1135
1136 return Status;
1137
1138 }
1139
1140
1141 /**
1142 The Response() function queues an HTTP response to this HTTP instance, similar to
1143 Receive() function in the EFI TCP driver. When the HTTP request is sent successfully,
1144 or if there is an error, Status in token will be updated and Event will be signaled.
1145
1146 The HTTP driver will queue a receive token to the underlying TCP instance. When data
1147 is received in the underlying TCP instance, the data will be parsed and Token will
1148 be populated with the response data. If the data received from the remote host
1149 contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
1150 (asynchronously) for more data to be sent from the remote host before signaling
1151 Event in Token.
1152
1153 It is the responsibility of the caller to allocate a buffer for Body and specify the
1154 size in BodyLength. If the remote host provides a response that contains a content
1155 body, up to BodyLength bytes will be copied from the receive buffer into Body and
1156 BodyLength will be updated with the amount of bytes received and copied to Body. This
1157 allows the client to download a large file in chunks instead of into one contiguous
1158 block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
1159 non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
1160 token to underlying TCP instance. If data arrives in the receive buffer, up to
1161 BodyLength bytes of data will be copied to Body. The HTTP driver will then update
1162 BodyLength with the amount of bytes received and copied to Body.
1163
1164 If the HTTP driver does not have an open underlying TCP connection with the host
1165 specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
1166 consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
1167 an open TCP connection between client and host.
1168
1169 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1170 @param[in] Token Pointer to storage containing HTTP response token.
1171
1172 @retval EFI_SUCCESS Allocation succeeded.
1173 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
1174 initialized.
1175 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1176 This is NULL.
1177 Token is NULL.
1178 Token->Message->Headers is NULL.
1179 Token->Message is NULL.
1180 Token->Message->Body is not NULL,
1181 Token->Message->BodyLength is non-zero, and
1182 Token->Message->Data is NULL, but a previous call to
1183 Response() has not been completed successfully.
1184 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
1185 @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host
1186 specified by response URL.
1187 **/
1188 EFI_STATUS
1189 EFIAPI
1190 EfiHttpResponse (
1191 IN EFI_HTTP_PROTOCOL *This,
1192 IN EFI_HTTP_TOKEN *Token
1193 )
1194 {
1195 EFI_STATUS Status;
1196 EFI_HTTP_MESSAGE *HttpMsg;
1197 HTTP_PROTOCOL *HttpInstance;
1198 HTTP_TOKEN_WRAP *Wrap;
1199
1200 if ((This == NULL) || (Token == NULL)) {
1201 return EFI_INVALID_PARAMETER;
1202 }
1203
1204 HttpMsg = Token->Message;
1205 if (HttpMsg == NULL) {
1206 return EFI_INVALID_PARAMETER;
1207 }
1208
1209 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
1210 ASSERT (HttpInstance != NULL);
1211
1212 if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {
1213 return EFI_NOT_STARTED;
1214 }
1215
1216 if (HttpInstance->LocalAddressIsIPv6) {
1217 return EFI_UNSUPPORTED;
1218 }
1219
1220 //
1221 // Check whether the token already existed.
1222 //
1223 if (EFI_ERROR (NetMapIterate (&HttpInstance->RxTokens, HttpTokenExist, Token))) {
1224 return EFI_ACCESS_DENIED;
1225 }
1226
1227 Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));
1228 if (Wrap == NULL) {
1229 return EFI_OUT_OF_RESOURCES;
1230 }
1231
1232 Wrap->HttpInstance = HttpInstance;
1233 Wrap->HttpToken = Token;
1234
1235 Status = HttpCreateTcp4RxEvent (Wrap);
1236 if (EFI_ERROR (Status)) {
1237 goto Error;
1238 }
1239
1240 Status = NetMapInsertTail (&HttpInstance->RxTokens, Token, Wrap);
1241 if (EFI_ERROR (Status)) {
1242 goto Error;
1243 }
1244
1245 //
1246 // If already have pending RxTokens, return directly.
1247 //
1248 if (NetMapGetCount (&HttpInstance->RxTokens) > 1) {
1249 return EFI_SUCCESS;
1250 }
1251
1252 return HttpResponseWorker (Wrap);
1253
1254 Error:
1255 if (Wrap != NULL) {
1256 if (Wrap->TcpWrap.RxToken.CompletionToken.Event != NULL) {
1257 gBS->CloseEvent (Wrap->TcpWrap.RxToken.CompletionToken.Event);
1258 }
1259 FreePool (Wrap);
1260 }
1261
1262 return Status;
1263 }
1264
1265 /**
1266 The Poll() function can be used by network drivers and applications to increase the
1267 rate that data packets are moved between the communication devices and the transmit
1268 and receive queues.
1269
1270 In some systems, the periodic timer event in the managed network driver may not poll
1271 the underlying communications device fast enough to transmit and/or receive all data
1272 packets without missing incoming packets or dropping outgoing packets. Drivers and
1273 applications that are experiencing packet loss should try calling the Poll() function
1274 more often.
1275
1276 @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.
1277
1278 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1279 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1280 @retval EFI_INVALID_PARAMETER This is NULL.
1281 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1282 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.
1283
1284 **/
1285 EFI_STATUS
1286 EFIAPI
1287 EfiHttpPoll (
1288 IN EFI_HTTP_PROTOCOL *This
1289 )
1290 {
1291 HTTP_PROTOCOL *HttpInstance;
1292
1293 if (This == NULL) {
1294 return EFI_INVALID_PARAMETER;
1295 }
1296
1297 HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);
1298 ASSERT (HttpInstance != NULL);
1299
1300 if (HttpInstance->LocalAddressIsIPv6) {
1301 return EFI_UNSUPPORTED;
1302 }
1303
1304 if (HttpInstance->Tcp4 == NULL || HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {
1305 return EFI_NOT_STARTED;
1306 }
1307
1308 return HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);
1309 }