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