OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 /** | 7 /** |
8 * HTTP status codes. | 8 * HTTP status codes. |
9 */ | 9 */ |
10 abstract class HttpStatus { | 10 abstract class HttpStatus { |
11 static const int CONTINUE = 100; | 11 static const int CONTINUE = 100; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 static const int GATEWAY_TIMEOUT = 504; | 50 static const int GATEWAY_TIMEOUT = 504; |
51 static const int HTTP_VERSION_NOT_SUPPORTED = 505; | 51 static const int HTTP_VERSION_NOT_SUPPORTED = 505; |
52 // Client generated status code. | 52 // Client generated status code. |
53 static const int NETWORK_CONNECT_TIMEOUT_ERROR = 599; | 53 static const int NETWORK_CONNECT_TIMEOUT_ERROR = 599; |
54 } | 54 } |
55 | 55 |
56 | 56 |
57 /** | 57 /** |
58 * HTTP server. | 58 * HTTP server. |
59 */ | 59 */ |
60 abstract class HttpServer { | 60 abstract class HttpServer implements Stream<HttpRequest> { |
61 factory HttpServer() => new _HttpServer(); | 61 // TODO(ajohnsen): Document with example, once the stream API is final. |
62 | 62 // TODO(ajohnsen): Add HttpServer.secure. |
63 /** | 63 /** |
64 * Start listening for HTTP requests on the specified [host] and | 64 * Start listening for HTTP requests on the specified [host] and |
65 * [port]. If a [port] of 0 is specified the server will choose an | 65 * [port]. If a [port] of 0 is specified the server will choose an |
66 * ephemeral port. The optional argument [backlog] can be used to | 66 * ephemeral port. The optional argument [backlog] can be used to |
67 * specify the listen backlog for the underlying OS listen. | 67 * specify the listen backlog for the underlying OS listen |
68 * The optional arguments [certificate_name] and [requestClientCertificate] | 68 * setup. |
69 * are used by the HttpsServer class, which shares the same interface. | |
70 * See [addRequestHandler] and [defaultRequestHandler] for | |
71 * information on how incoming HTTP requests are handled. | |
72 */ | 69 */ |
73 void listen(String host, | 70 static Future<HttpServer> bind([String address = "127.0.0.1", |
74 int port, | 71 int port = 0, |
75 {int backlog: 128, | 72 int backlog = 0]) |
76 String certificate_name, | 73 => _HttpServer.bind(address, port, backlog); |
77 bool requestClientCertificate: false}); | |
78 | 74 |
79 /** | 75 /** |
80 * Attach the HTTP server to an existing [:ServerSocket:]. If the | 76 * Start listening for HTTPS requests on the specified [host] and |
81 * [HttpServer] is closed, the [HttpServer] will just detach itself, | 77 * [port]. If a [port] of 0 is specified the server will choose an |
82 * and not close [serverSocket]. | 78 * ephemeral port. The optional argument [backlog] can be used to |
| 79 * specify the listen backlog for the underlying OS listen |
| 80 * setup. |
| 81 * |
| 82 * The certificate with Distinguished Name [certificate_name] is looked |
| 83 * up in the certificate database, and is used as the server certificate. |
| 84 * if [requestClientCertificate] is true, the server will request clients |
| 85 * to authenticate with a client certificate. |
83 */ | 86 */ |
84 void listenOn(ServerSocket serverSocket); | 87 |
| 88 static Future<HttpServer> bindSecure(String address, |
| 89 int port, |
| 90 {int backlog: 0, |
| 91 String certificateName, |
| 92 bool requestClientCertificate: false}) |
| 93 => _HttpServer.bindSecure(address, |
| 94 port, |
| 95 backlog, |
| 96 certificateName, |
| 97 requestClientCertificate); |
85 | 98 |
86 /** | 99 /** |
87 * Adds a request handler to the list of request handlers. The | 100 * Attach the HTTP server to an existing [:ServerSocket:]. When the |
88 * function [matcher] is called with the request and must return | 101 * [HttpServer] is closed, the [HttpServer] will just detach itself, |
89 * [:true:] if the [handler] should handle the request. The first | 102 * close current connections but not close [serverSocket]. |
90 * handler for which [matcher] returns [:true:] will be handed the | |
91 * request. | |
92 */ | 103 */ |
93 addRequestHandler(bool matcher(HttpRequest request), | 104 factory HttpServer.listenOn(ServerSocket serverSocket) |
94 void handler(HttpRequest request, HttpResponse response)); | 105 => new _HttpServer.listenOn(serverSocket); |
95 | 106 |
96 /** | 107 /** |
97 * Sets the default request handler. This request handler will be | 108 * Stop server listening. This will make the [Stream] close with a done |
98 * called if none of the request handlers registered by | 109 * event. |
99 * [addRequestHandler] matches the current request. If no default | |
100 * request handler is set the server will just respond with status | |
101 * code [:NOT_FOUND:] (404). | |
102 */ | |
103 void set defaultRequestHandler( | |
104 void handler(HttpRequest request, HttpResponse response)); | |
105 | |
106 /** | |
107 * Stop server listening. | |
108 */ | 110 */ |
109 void close(); | 111 void close(); |
110 | 112 |
111 /** | 113 /** |
112 * Returns the port that the server is listening on. This can be | 114 * Returns the port that the server is listening on. This can be |
113 * used to get the actual port used when a value of 0 for [port] is | 115 * used to get the actual port used when a value of 0 for [port] is |
114 * specified in the [listen] call. | 116 * specified in the [listen] call. |
115 */ | 117 */ |
116 int get port; | 118 int get port; |
117 | 119 |
118 /** | 120 /** |
119 * Sets the error handler that is called when a connection error occurs. | |
120 */ | |
121 void set onError(void callback(e)); | |
122 | |
123 /** | |
124 * Set the timeout, in seconds, for sessions of this HTTP server. Default | 121 * Set the timeout, in seconds, for sessions of this HTTP server. Default |
125 * is 20 minutes. | 122 * is 20 minutes. |
126 */ | 123 */ |
127 set sessionTimeout(int timeout); | 124 set sessionTimeout(int timeout); |
128 | 125 |
129 /** | 126 /** |
130 * Returns a [:HttpConnectionsInfo:] object with an overview of the | 127 * Returns a [:HttpConnectionsInfo:] object with an overview of the |
131 * current connection handled by the server. | 128 * current connections handled by the server. |
132 */ | 129 */ |
133 HttpConnectionsInfo connectionsInfo(); | 130 HttpConnectionsInfo connectionsInfo(); |
134 } | 131 } |
135 | 132 |
136 | 133 |
137 /** | 134 /** |
138 * HTTPS server. | |
139 */ | |
140 abstract class HttpsServer implements HttpServer { | |
141 factory HttpsServer() => new _HttpServer.httpsServer(); | |
142 } | |
143 | |
144 | |
145 /** | |
146 * Overview information of the [:HttpServer:] socket connections. | 135 * Overview information of the [:HttpServer:] socket connections. |
147 */ | 136 */ |
148 class HttpConnectionsInfo { | 137 class HttpConnectionsInfo { |
149 /** | 138 /** |
150 * Total number of socket connections. | 139 * Total number of socket connections. |
151 */ | 140 */ |
152 int total = 0; | 141 int total = 0; |
153 | 142 |
154 /** | 143 /** |
155 * Number of active connections where actual request/response | 144 * Number of active connections where actual request/response |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 Map<String, String> get parameters; | 427 Map<String, String> get parameters; |
439 | 428 |
440 /** | 429 /** |
441 * Returns the formatted string representation in the form: | 430 * Returns the formatted string representation in the form: |
442 * | 431 * |
443 * value; parameter1=value1; parameter2=value2 | 432 * value; parameter1=value1; parameter2=value2 |
444 */ | 433 */ |
445 String toString(); | 434 String toString(); |
446 } | 435 } |
447 | 436 |
448 abstract class HttpSession { | 437 abstract class HttpSession implements Map { |
449 /** | 438 /** |
450 * Get the id for the current session. | 439 * Get the id for the current session. |
451 */ | 440 */ |
452 String get id; | 441 String get id; |
453 | 442 |
454 /** | 443 /** |
455 * Access the user-data associated with the session. | |
456 */ | |
457 dynamic data; | |
458 | |
459 /** | |
460 * Destroy the session. This will terminate the session and any further | 444 * Destroy the session. This will terminate the session and any further |
461 * connections with this id will be given a new id and session. | 445 * connections with this id will be given a new id and session. |
462 */ | 446 */ |
463 void destroy(); | 447 void destroy(); |
464 | 448 |
465 /** | 449 /** |
466 * Set a callback that will be called when the session is timed out. | 450 * Set a callback that will be called when the session is timed out. |
467 */ | 451 */ |
468 void set onTimeout(void callback()); | 452 void set onTimeout(void callback()); |
| 453 |
| 454 /** |
| 455 * Is true if the session have not been sent to the client yet. |
| 456 */ |
| 457 bool get isNew; |
469 } | 458 } |
470 | 459 |
471 | 460 |
472 /** | 461 /** |
473 * Representation of a content type. | 462 * Representation of a content type. |
474 */ | 463 */ |
475 abstract class ContentType implements HeaderValue { | 464 abstract class ContentType implements HeaderValue { |
476 /** | 465 /** |
477 * Creates a new content type object setting the primary type and | 466 * Creates a new content type object setting the primary type and |
478 * sub type. | 467 * sub type. |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
583 /** | 572 /** |
584 * Returns the formatted string representation of the cookie. The | 573 * Returns the formatted string representation of the cookie. The |
585 * string representation can be used for for setting the Cookie or | 574 * string representation can be used for for setting the Cookie or |
586 * Set-Cookie headers | 575 * Set-Cookie headers |
587 */ | 576 */ |
588 String toString(); | 577 String toString(); |
589 } | 578 } |
590 | 579 |
591 | 580 |
592 /** | 581 /** |
593 * Http request delivered to the HTTP server callback. | 582 * Http request delivered to the HTTP server callback. The [HttpRequest] is a |
| 583 * [Stream] of the body content of the request. Listen to the body to handle the |
| 584 * data and be notified once the entire body is received. |
594 */ | 585 */ |
595 abstract class HttpRequest { | 586 abstract class HttpRequest implements Stream<List<int>> { |
596 /** | 587 /** |
597 * Returns the content length of the request body. If the size of | 588 * Returns the content length of the request body. If the size of |
598 * the request body is not known in advance this -1. | 589 * the request body is not known in advance this -1. |
599 */ | 590 */ |
600 int get contentLength; | 591 int get contentLength; |
601 | 592 |
602 /** | 593 /** |
603 * Returns the persistent connection state signaled by the client. | |
604 */ | |
605 bool get persistentConnection; | |
606 | |
607 /** | |
608 * Returns the method for the request. | 594 * Returns the method for the request. |
609 */ | 595 */ |
610 String get method; | 596 String get method; |
611 | 597 |
612 /** | 598 /** |
613 * Returns the URI for the request. | 599 * Returns the URI for the request. |
614 */ | 600 */ |
615 String get uri; | 601 Uri get uri; |
616 | |
617 /** | |
618 * Returns the path part of the URI. | |
619 */ | |
620 String get path; | |
621 | |
622 /** | |
623 * Returns the query string. | |
624 */ | |
625 String get queryString; | |
626 | 602 |
627 /** | 603 /** |
628 * Returns the parsed query string. | 604 * Returns the parsed query string. |
629 */ | 605 */ |
630 Map<String, String> get queryParameters; | 606 Map<String, String> get queryParameters; |
631 | 607 |
632 /** | 608 /** |
633 * Returns the request headers. | 609 * Returns the request headers. |
634 */ | 610 */ |
635 HttpHeaders get headers; | 611 HttpHeaders get headers; |
636 | 612 |
637 /** | 613 /** |
638 * Returns the cookies in the request (from the Cookie headers). | 614 * Returns the cookies in the request (from the Cookie headers). |
639 */ | 615 */ |
640 List<Cookie> get cookies; | 616 List<Cookie> get cookies; |
641 | 617 |
642 /** | 618 /** |
| 619 * Returns the persistent connection state signaled by the client. |
| 620 */ |
| 621 bool get persistentConnection; |
| 622 |
| 623 /** |
643 * Returns the client certificate of the client making the request. | 624 * Returns the client certificate of the client making the request. |
644 * Returns null if the connection is not a secure TLS or SSL connection, | 625 * Returns null if the connection is not a secure TLS or SSL connection, |
645 * or if the server does not request a client certificate, or if the client | 626 * or if the server does not request a client certificate, or if the client |
646 * does not provide one. | 627 * does not provide one. |
647 */ | 628 */ |
648 X509Certificate get certificate; | 629 X509Certificate get certificate; |
649 | 630 |
650 /** | 631 /** |
651 * Returns, or initialize, a session for the given request. If the session is | 632 * Get the session for the given request. If the session is |
652 * being initialized by this call, [init] will be called with the | 633 * being initialized by this call, [:isNew:] will be true for the returned |
653 * newly create session. Here the [:HttpSession.data:] field can be set, if | 634 * session. |
654 * needed. | |
655 * See [:HttpServer.sessionTimeout:] on how to change default timeout. | 635 * See [:HttpServer.sessionTimeout:] on how to change default timeout. |
656 */ | 636 */ |
657 HttpSession session([init(HttpSession session)]); | 637 HttpSession get session; |
658 | |
659 /** | |
660 * Returns the input stream for the request. This is used to read | |
661 * the request data. | |
662 */ | |
663 InputStream get inputStream; | |
664 | 638 |
665 /** | 639 /** |
666 * Returns the HTTP protocol version used in the request. This will | 640 * Returns the HTTP protocol version used in the request. This will |
667 * be "1.0" or "1.1". | 641 * be "1.0" or "1.1". |
668 */ | 642 */ |
669 String get protocolVersion; | 643 String get protocolVersion; |
670 | 644 |
671 /** | 645 /** |
672 * Get information about the client connection. Returns [null] if the socket | 646 * Get information about the client connection. Returns [null] if the socket |
673 * isn't available. | 647 * isn't available. |
674 */ | 648 */ |
675 HttpConnectionInfo get connectionInfo; | 649 HttpConnectionInfo get connectionInfo; |
| 650 |
| 651 /** |
| 652 * Get the [HttpResponse] object, used for sending back the response to the |
| 653 * client. |
| 654 */ |
| 655 HttpResponse get response; |
676 } | 656 } |
677 | 657 |
678 | 658 |
679 /** | 659 /** |
680 * HTTP response to be send back to the client. | 660 * HTTP response to be send back to the client. |
681 */ | 661 */ |
682 abstract class HttpResponse { | 662 abstract class HttpResponse implements IOSink<HttpResponse> { |
| 663 // TODO(ajohnsen): Add documentation of how to pipe a file to the response. |
683 /** | 664 /** |
684 * Gets and sets the content length of the response. If the size of | 665 * Gets and sets the content length of the response. If the size of |
685 * the response is not known in advance set the content length to | 666 * the response is not known in advance set the content length to |
686 * -1 - which is also the default if not set. | 667 * -1 - which is also the default if not set. |
687 */ | 668 */ |
688 int contentLength; | 669 int contentLength; |
689 | 670 |
690 /** | 671 /** |
691 * Gets and sets the status code. Any integer value is accepted. For | 672 * Gets and sets the status code. Any integer value is accepted. For |
692 * the official HTTP status codes use the fields from | 673 * the official HTTP status codes use the fields from |
(...skipping 19 matching lines...) Expand all Loading... |
712 * Returns the response headers. | 693 * Returns the response headers. |
713 */ | 694 */ |
714 HttpHeaders get headers; | 695 HttpHeaders get headers; |
715 | 696 |
716 /** | 697 /** |
717 * Cookies to set in the client (in the Set-Cookie header). | 698 * Cookies to set in the client (in the Set-Cookie header). |
718 */ | 699 */ |
719 List<Cookie> get cookies; | 700 List<Cookie> get cookies; |
720 | 701 |
721 /** | 702 /** |
722 * Returns the output stream for the response. This is used to write | |
723 * the response data. When all response data has been written close | |
724 * the stream to indicate the end of the response. | |
725 * | |
726 * When this is accessed for the first time the response header is | |
727 * send. Calling any methods that will change the header after | |
728 * having retrieved the output stream will throw an exception. | |
729 */ | |
730 OutputStream get outputStream; | |
731 | |
732 /** | |
733 * Detach the underlying socket from the HTTP server. When the | 703 * Detach the underlying socket from the HTTP server. When the |
734 * socket is detached the HTTP server will no longer perform any | 704 * socket is detached the HTTP server will no longer perform any |
735 * operations on it. | 705 * operations on it. |
736 * | 706 * |
737 * This is normally used when a HTTP upgrade request is received | 707 * This is normally used when a HTTP upgrade request is received |
738 * and the communication should continue with a different protocol. | 708 * and the communication should continue with a different protocol. |
739 */ | 709 */ |
740 DetachedSocket detachSocket(); | 710 Future<Socket> detachSocket(); |
741 | 711 |
742 /** | 712 /** |
743 * Get information about the client connection. Returns [null] if the socket | 713 * Get information about the client connection. Returns [null] if the socket |
744 * isn't available. | 714 * isn't available. |
745 */ | 715 */ |
746 HttpConnectionInfo get connectionInfo; | 716 HttpConnectionInfo get connectionInfo; |
747 } | 717 } |
748 | 718 |
749 | 719 |
750 /** | 720 /** |
751 * HTTP client factory. The [HttpClient] handles all the sockets associated | 721 * HTTP client factory. The [HttpClient] handles all the sockets associated |
752 * with the [HttpClientConnection]s and when the endpoint supports it, it will | 722 * with the [HttpClientConnection]s and when the endpoint supports it, it will |
753 * try to reuse opened sockets for several requests to support HTTP 1.1 | 723 * try to reuse opened sockets for several requests to support HTTP 1.1 |
754 * persistent connections. This means that sockets will be kept open for some | 724 * persistent connections. This means that sockets will be kept open for some |
755 * time after a requests have completed, unless HTTP procedures indicate that it | 725 * time after a requests have completed, unless HTTP procedures indicate that it |
756 * must be closed as part of completing the request. Use [:HttpClient.shutdown:] | 726 * must be closed as part of completing the request. Use [:HttpClient.close:] |
757 * to force close the idle sockets. | 727 * to force close the idle sockets. |
758 */ | 728 */ |
759 abstract class HttpClient { | 729 abstract class HttpClient { |
760 static const int DEFAULT_HTTP_PORT = 80; | 730 static const int DEFAULT_HTTP_PORT = 80; |
761 static const int DEFAULT_HTTPS_PORT = 443; | 731 static const int DEFAULT_HTTPS_PORT = 443; |
762 | 732 |
763 factory HttpClient() => new _HttpClient(); | 733 factory HttpClient() => new _HttpClient(); |
764 | 734 |
765 /** | 735 /** |
766 * Opens a HTTP connection. The returned [HttpClientConnection] is | 736 * Opens a HTTP connection. The returned [HttpClientRequest] is used to |
767 * used to register callbacks for asynchronous events on the HTTP | 737 * fill in the content of the request before sending it. The "Host" header for |
768 * connection. The "Host" header for the request will be set to the | 738 * the request will be set to the value [host]:[port]. This can be overridden |
769 * value [host]:[port]. This can be overridden through the | 739 * through the [HttpClientRequest] interface before the request is sent. |
770 * HttpClientRequest interface before the request is sent. NOTE if | 740 * NOTE if [host] is an IP address this will still be set in the "Host" |
771 * [host] is an IP address this will still be set in the "Host" | |
772 * header. | 741 * header. |
773 */ | 742 */ |
774 HttpClientConnection open(String method, String host, int port, String path); | 743 Future<HttpClientRequest> open(String method, |
| 744 String host, |
| 745 int port, |
| 746 String path); |
775 | 747 |
776 /** | 748 /** |
777 * Opens a HTTP connection. The returned [HttpClientConnection] is | 749 * Opens a HTTP connection. The returned [HttpClientRequest] is used to |
778 * used to register callbacks for asynchronous events on the HTTP | 750 * fill in the content of the request before sending it. The "Host" header for |
779 * connection. The "Host" header for the request will be set based | 751 * the request will be set to the value [host]:[port]. This can be overridden |
780 * the host and port specified in [url]. This can be overridden | 752 * through the [HttpClientRequest] interface before the request is sent. |
781 * through the HttpClientRequest interface before the request is | 753 * NOTE if [host] is an IP address this will still be set in the "Host" |
782 * sent. NOTE if the host is specified as an IP address this will | 754 * header. |
783 * still be set in the "Host" header. | |
784 */ | 755 */ |
785 HttpClientConnection openUrl(String method, Uri url); | 756 Future<HttpClientRequest> openUrl(String method, Uri url); |
786 | 757 |
787 /** | 758 /** |
788 * Opens a HTTP connection using the GET method. See [open] for | 759 * Opens a HTTP connection using the GET method. See [open] for |
789 * details. Using this method to open a HTTP connection will set the | 760 * details. Using this method to open a HTTP connection will set the |
790 * content length to 0. | 761 * content length to 0. |
791 */ | 762 */ |
792 HttpClientConnection get(String host, int port, String path); | 763 Future<HttpClientRequest> get(String host, int port, String path); |
793 | 764 |
794 /** | 765 /** |
795 * Opens a HTTP connection using the GET method. See [openUrl] for | 766 * Opens a HTTP connection using the GET method. See [openUrl] for |
796 * details. Using this method to open a HTTP connection will set the | 767 * details. Using this method to open a HTTP connection will set the |
797 * content length to 0. | 768 * content length to 0. |
798 */ | 769 */ |
799 HttpClientConnection getUrl(Uri url); | 770 Future<HttpClientRequest> getUrl(Uri url); |
800 | 771 |
801 /** | 772 /** |
802 * Opens a HTTP connection using the POST method. See [open] for details. | 773 * Opens a HTTP connection using the POST method. See [open] for details. |
803 */ | 774 */ |
804 HttpClientConnection post(String host, int port, String path); | 775 Future<HttpClientRequest> post(String host, int port, String path); |
805 | 776 |
806 /** | 777 /** |
807 * Opens a HTTP connection using the POST method. See [openUrl] for details. | 778 * Opens a HTTP connection using the POST method. See [openUrl] for details. |
808 */ | 779 */ |
809 HttpClientConnection postUrl(Uri url); | 780 Future<HttpClientRequest> postUrl(Uri url); |
810 | 781 |
811 /** | 782 /** |
812 * Sets the function to be called when a site is requesting | 783 * Sets the function to be called when a site is requesting |
813 * authentication. The URL requested and the security realm from the | 784 * authentication. The URL requested and the security realm from the |
814 * server are passed in the arguments [url] and [realm]. | 785 * server are passed in the arguments [url] and [realm]. |
815 * | 786 * |
816 * The function returns a [Future] which should complete when the | 787 * The function returns a [Future] which should complete when the |
817 * authentication has been resolved. If credentials cannot be | 788 * authentication has been resolved. If credentials cannot be |
818 * provided the [Future] should complete with [false]. If | 789 * provided the [Future] should complete with [false]. If |
819 * credentials are available the function should add these using | 790 * credentials are available the function should add these using |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
874 /** | 845 /** |
875 * Shutdown the HTTP client. If [force] is [:false:] (the default) | 846 * Shutdown the HTTP client. If [force] is [:false:] (the default) |
876 * the [:HttpClient:] will be kept alive until all active | 847 * the [:HttpClient:] will be kept alive until all active |
877 * connections are done. If [force] is [:true:] any active | 848 * connections are done. If [force] is [:true:] any active |
878 * connections will be closed to immediately release all | 849 * connections will be closed to immediately release all |
879 * resources. These closed connections will receive an [:onError:] | 850 * resources. These closed connections will receive an [:onError:] |
880 * callback to indicate that the client was shutdown. In both cases | 851 * callback to indicate that the client was shutdown. In both cases |
881 * trying to establish a new connection after calling [shutdown] | 852 * trying to establish a new connection after calling [shutdown] |
882 * will throw an exception. | 853 * will throw an exception. |
883 */ | 854 */ |
884 void shutdown({bool force: false}); | 855 void close({bool force: false}); |
885 } | 856 } |
886 | 857 |
887 | 858 |
888 /** | 859 /** |
889 * A [HttpClientConnection] is returned by all [HttpClient] methods | 860 * HTTP request for a client connection. |
890 * that initiate a connection to an HTTP server. The handlers will be | |
891 * called as the connection state progresses. | |
892 * | 861 * |
893 * The setting of all handlers is optional. If [onRequest] is not set | 862 * The request is an [IOSink], used to write the request data. When |
894 * the request will be send without any additional headers and an | 863 * all request data has been written, close the stream to indicate the end of |
895 * empty body. If [onResponse] is not set the response will be read | 864 * the request. |
896 * and discarded. | 865 * |
| 866 * When this is accessed for the first time the request header is |
| 867 * send. Calling any methods that will change the header after |
| 868 * having retrieved the output stream will throw an exception. |
897 */ | 869 */ |
898 abstract class HttpClientConnection { | 870 abstract class HttpClientRequest |
| 871 implements IOSink<HttpClientRequest> { |
899 /** | 872 /** |
900 * Sets the handler that is called when the connection is established. | 873 * Gets and sets the content length of the request. If the size of |
| 874 * the request is not known in advance set content length to -1, |
| 875 * which is also the default. |
901 */ | 876 */ |
902 void set onRequest(void callback(HttpClientRequest request)); | 877 int contentLength; |
903 | 878 |
904 /** | 879 /** |
905 * Sets callback to be called when the request has been send and | 880 * Returns the request headers. |
906 * the response is ready for processing. The callback is called when | |
907 * all headers of the response are received and data is ready to be | |
908 * received. | |
909 */ | 881 */ |
910 void set onResponse(void callback(HttpClientResponse response)); | 882 HttpHeaders get headers; |
911 | 883 |
912 /** | 884 /** |
913 * Sets the handler that gets called if an error occurs while | 885 * Cookies to present to the server (in the Cookie header). |
914 * connecting or processing the HTTP request. | |
915 */ | 886 */ |
916 void set onError(void callback(e)); | 887 List<Cookie> get cookies; |
917 | 888 |
918 /** | 889 /** |
919 * Set this property to [:true:] if this connection should | 890 * Gets and sets the requested persistent connection state. |
920 * automatically follow redirects. The default is | 891 * The default value is [:true:]. |
921 * [:true:]. | 892 */ |
| 893 bool persistentConnection; |
| 894 |
| 895 /** |
| 896 * A [HttpClientResponse] future that will complete once the response is |
| 897 * available. If an error occours before the response is available, this |
| 898 * future will complete with an error. |
| 899 */ |
| 900 Future<HttpClientResponse> get response; |
| 901 |
| 902 /** |
| 903 * Close the request for input. Returns the value of [response]. |
| 904 */ |
| 905 Future<HttpClientResponse> close(); |
| 906 |
| 907 /** |
| 908 * Set this property to [:true:] if this request should |
| 909 * automatically follow redirects. The default is [:true:]. |
922 * | 910 * |
923 * Automatic redirect will only happen for "GET" and "HEAD" requests | 911 * Automatic redirect will only happen for "GET" and "HEAD" requests |
924 * and only for the status codes [:HttpStatus.MOVED_PERMANENTLY:] | 912 * and only for the status codes [:HttpHeaders.MOVED_PERMANENTLY:] |
925 * (301), [:HttpStatus.FOUND:] (302), | 913 * (301), [:HttpStatus.FOUND:] (302), |
926 * [:HttpStatus.MOVED_TEMPORARILY:] (302, alias for | 914 * [:HttpStatus.MOVED_TEMPORARILY:] (302, alias for |
927 * [:HttpStatus.FOUND:]), [:HttpStatus.SEE_OTHER:] (303) and | 915 * [:HttpStatus.FOUND:]), [:HttpStatus.SEE_OTHER:] (303) and |
928 * [:HttpStatus.TEMPORARY_REDIRECT:] (307). For | 916 * [:HttpStatus.TEMPORARY_REDIRECT:] (307). For |
929 * [:HttpStatus.SEE_OTHER:] (303) autmatic redirect will also happen | 917 * [:HttpStatus.SEE_OTHER:] (303) autmatic redirect will also happen |
930 * for "POST" requests with the method changed to "GET" when | 918 * for "POST" requests with the method changed to "GET" when |
931 * following the redirect. | 919 * following the redirect. |
932 * | 920 * |
933 * All headers added to the request will be added to the redirection | 921 * All headers added to the request will be added to the redirection |
934 * request(s). However, any body send with the request will not be | 922 * request(s). However, any body send with the request will not be |
935 * part of the redirection request(s). | 923 * part of the redirection request(s). |
936 */ | 924 */ |
937 bool followRedirects; | 925 bool followRedirects; |
938 | 926 |
939 /** | 927 /** |
940 * Set this property to the maximum number of redirects to follow | 928 * Set this property to the maximum number of redirects to follow |
941 * when [followRedirects] is [:true:]. If this number is exceeded the | 929 * when [followRedirects] is [:true:]. If this number is exceeded the |
942 * [onError] callback will be called with a [RedirectLimitExceeded] | 930 * [onError] callback will be called with a [RedirectLimitExceeded] |
943 * exception. The default value is 5. | 931 * exception. The default value is 5. |
944 */ | 932 */ |
945 int maxRedirects; | 933 int maxRedirects; |
946 | 934 |
947 /** | 935 /** |
948 * Returns the series of redirects this connection has been through. | |
949 */ | |
950 List<RedirectInfo> get redirects; | |
951 | |
952 /** | |
953 * Redirect this connection to a new URL. The default value for | |
954 * [method] is the method for the current request. The default value | |
955 * for [url] is the value of the [:HttpHeaders.LOCATION:] header of | |
956 * the current response. All body data must have been read from the | |
957 * current response before calling [redirect]. | |
958 * | |
959 * All headers added to the request will be added to the redirection | |
960 * request(s). However, any body send with the request will not be | |
961 * part of the redirection request(s). | |
962 */ | |
963 void redirect([String method, Uri url]); | |
964 | |
965 /** | |
966 * Detach the underlying socket from the HTTP client. When the | |
967 * socket is detached the HTTP client will no longer perform any | |
968 * operations on it. | |
969 * | |
970 * This is normally used when a HTTP upgrade is negotiated and the | |
971 * communication should continue with a different protocol. | |
972 */ | |
973 DetachedSocket detachSocket(); | |
974 | |
975 /** | |
976 * Get information about the client connection. Returns [null] if the socket | 936 * Get information about the client connection. Returns [null] if the socket |
977 * isn't available. | 937 * isn't available. |
978 */ | 938 */ |
979 HttpConnectionInfo get connectionInfo; | 939 HttpConnectionInfo get connectionInfo; |
980 } | 940 } |
981 | 941 |
982 | 942 |
983 /** | 943 /** |
984 * HTTP request for a client connection. | 944 * HTTP response for a client connection. The [HttpClientResponse] is a |
| 945 * [Stream] of the body content of the response. Listen to the body to handle |
| 946 * the data and be notified once the entire body is received. |
| 947 |
985 */ | 948 */ |
986 abstract class HttpClientRequest { | 949 abstract class HttpClientResponse implements Stream<List<int>> { |
987 /** | |
988 * Gets and sets the content length of the request. If the size of | |
989 * the request is not known in advance set content length to -1, | |
990 * which is also the default. | |
991 */ | |
992 int contentLength; | |
993 | |
994 /** | |
995 * Returns the request headers. | |
996 */ | |
997 HttpHeaders get headers; | |
998 | |
999 /** | |
1000 * Cookies to present to the server (in the Cookie header). | |
1001 */ | |
1002 List<Cookie> get cookies; | |
1003 | |
1004 /** | |
1005 * Gets and sets the requested persistent connection state. | |
1006 * The default value is [:true:]. | |
1007 */ | |
1008 bool persistentConnection; | |
1009 | |
1010 /** | |
1011 * Returns the output stream for the request. This is used to write | |
1012 * the request data. When all request data has been written close | |
1013 * the stream to indicate the end of the request. | |
1014 * | |
1015 * When this is accessed for the first time the request header is | |
1016 * send. Calling any methods that will change the header after | |
1017 * having retrieved the output stream will throw an exception. | |
1018 */ | |
1019 OutputStream get outputStream; | |
1020 } | |
1021 | |
1022 | |
1023 /** | |
1024 * HTTP response for a client connection. | |
1025 */ | |
1026 abstract class HttpClientResponse { | |
1027 /** | 950 /** |
1028 * Returns the status code. | 951 * Returns the status code. |
1029 */ | 952 */ |
1030 int get statusCode; | 953 int get statusCode; |
1031 | 954 |
1032 /** | 955 /** |
1033 * Returns the reason phrase associated with the status code. | 956 * Returns the reason phrase associated with the status code. |
1034 */ | 957 */ |
1035 String get reasonPhrase; | 958 String get reasonPhrase; |
1036 | 959 |
(...skipping 10 matching lines...) Expand all Loading... |
1047 | 970 |
1048 /** | 971 /** |
1049 * Returns whether the status code is one of the normal redirect | 972 * Returns whether the status code is one of the normal redirect |
1050 * codes [:HttpStatus.MOVED_PERMANENTLY:], [:HttpStatus.FOUND:], | 973 * codes [:HttpStatus.MOVED_PERMANENTLY:], [:HttpStatus.FOUND:], |
1051 * [:HttpStatus.MOVED_TEMPORARILY:], [:HttpStatus.SEE_OTHER:] and | 974 * [:HttpStatus.MOVED_TEMPORARILY:], [:HttpStatus.SEE_OTHER:] and |
1052 * [:HttpStatus.TEMPORARY_REDIRECT:]. | 975 * [:HttpStatus.TEMPORARY_REDIRECT:]. |
1053 */ | 976 */ |
1054 bool get isRedirect; | 977 bool get isRedirect; |
1055 | 978 |
1056 /** | 979 /** |
| 980 * Returns the series of redirects this connection has been through. The |
| 981 * list will be empty if no redirects was followed. [redirects] will be |
| 982 * updated both in the case of an automatic and a manual redirect. |
| 983 */ |
| 984 List<RedirectInfo> get redirects; |
| 985 |
| 986 /** |
| 987 * Redirect this connection to a new URL. The default value for |
| 988 * [method] is the method for the current request. The default value |
| 989 * for [url] is the value of the [:HttpHeaders.LOCATION:] header of |
| 990 * the current response. All body data must have been read from the |
| 991 * current response before calling [redirect]. |
| 992 * |
| 993 * All headers added to the request will be added to the redirection |
| 994 * request(s). However, any body send with the request will not be |
| 995 * part of the redirection request(s). |
| 996 * |
| 997 * If [followLoops] is set to [true], redirect will follow the redirect, |
| 998 * even if was already visited. Default value is [false]. |
| 999 * |
| 1000 * [redirect] will ignore [maxRedirects] and always perform the redirect. |
| 1001 */ |
| 1002 Future<HttpClientResponse> redirect([String method, |
| 1003 Uri url, |
| 1004 bool followLoops]); |
| 1005 |
| 1006 |
| 1007 /** |
1057 * Returns the response headers. | 1008 * Returns the response headers. |
1058 */ | 1009 */ |
1059 HttpHeaders get headers; | 1010 HttpHeaders get headers; |
1060 | 1011 |
1061 /** | 1012 /** |
| 1013 * Detach the underlying socket from the HTTP client. When the |
| 1014 * socket is detached the HTTP client will no longer perform any |
| 1015 * operations on it. |
| 1016 * |
| 1017 * This is normally used when a HTTP upgrade is negotiated and the |
| 1018 * communication should continue with a different protocol. |
| 1019 */ |
| 1020 Future<Socket> detachSocket(); |
| 1021 |
| 1022 /** |
1062 * Cookies set by the server (from the Set-Cookie header). | 1023 * Cookies set by the server (from the Set-Cookie header). |
1063 */ | 1024 */ |
1064 List<Cookie> get cookies; | 1025 List<Cookie> get cookies; |
1065 | 1026 |
1066 /** | 1027 /** |
1067 * Returns the certificate of the HTTPS server providing the response. | 1028 * Returns the certificate of the HTTPS server providing the response. |
1068 * Returns null if the connection is not a secure TLS or SSL connection. | 1029 * Returns null if the connection is not a secure TLS or SSL connection. |
1069 */ | 1030 */ |
1070 X509Certificate get certificate; | 1031 X509Certificate get certificate; |
1071 | 1032 |
1072 /** | 1033 /** |
1073 * Returns the input stream for the response. This is used to read | 1034 * Get information about the client connection. Returns [null] if the socket |
1074 * the response data. | 1035 * isn't available. |
1075 */ | 1036 */ |
1076 InputStream get inputStream; | 1037 HttpConnectionInfo get connectionInfo; |
1077 } | 1038 } |
1078 | 1039 |
1079 | 1040 |
1080 abstract class HttpClientCredentials { } | 1041 abstract class HttpClientCredentials { } |
1081 | 1042 |
1082 | 1043 |
1083 /** | 1044 /** |
1084 * Represent credentials for basic authentication. | 1045 * Represent credentials for basic authentication. |
1085 */ | 1046 */ |
1086 abstract class HttpClientBasicCredentials extends HttpClientCredentials { | 1047 abstract class HttpClientBasicCredentials extends HttpClientCredentials { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1159 class RedirectLimitExceededException extends RedirectException { | 1120 class RedirectLimitExceededException extends RedirectException { |
1160 const RedirectLimitExceededException(List<RedirectInfo> redirects) | 1121 const RedirectLimitExceededException(List<RedirectInfo> redirects) |
1161 : super("Redirect limit exceeded", redirects); | 1122 : super("Redirect limit exceeded", redirects); |
1162 } | 1123 } |
1163 | 1124 |
1164 | 1125 |
1165 class RedirectLoopException extends RedirectException { | 1126 class RedirectLoopException extends RedirectException { |
1166 const RedirectLoopException(List<RedirectInfo> redirects) | 1127 const RedirectLoopException(List<RedirectInfo> redirects) |
1167 : super("Redirect loop detected", redirects); | 1128 : super("Redirect loop detected", redirects); |
1168 } | 1129 } |
OLD | NEW |