2012-02-28 15:11:34 +00:00
|
|
|
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
|
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP status codes.
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
abstract class HttpStatus {
|
2012-08-27 23:51:37 +00:00
|
|
|
static const int CONTINUE = 100;
|
|
|
|
static const int SWITCHING_PROTOCOLS = 101;
|
|
|
|
static const int OK = 200;
|
|
|
|
static const int CREATED = 201;
|
|
|
|
static const int ACCEPTED = 202;
|
|
|
|
static const int NON_AUTHORITATIVE_INFORMATION = 203;
|
|
|
|
static const int NO_CONTENT = 204;
|
|
|
|
static const int RESET_CONTENT = 205;
|
|
|
|
static const int PARTIAL_CONTENT = 206;
|
|
|
|
static const int MULTIPLE_CHOICES = 300;
|
|
|
|
static const int MOVED_PERMANENTLY = 301;
|
|
|
|
static const int FOUND = 302;
|
|
|
|
static const int MOVED_TEMPORARILY = 302; // Common alias for FOUND.
|
|
|
|
static const int SEE_OTHER = 303;
|
|
|
|
static const int NOT_MODIFIED = 304;
|
|
|
|
static const int USE_PROXY = 305;
|
|
|
|
static const int TEMPORARY_REDIRECT = 307;
|
|
|
|
static const int BAD_REQUEST = 400;
|
|
|
|
static const int UNAUTHORIZED = 401;
|
|
|
|
static const int PAYMENT_REQUIRED = 402;
|
|
|
|
static const int FORBIDDEN = 403;
|
|
|
|
static const int NOT_FOUND = 404;
|
|
|
|
static const int METHOD_NOT_ALLOWED = 405;
|
|
|
|
static const int NOT_ACCEPTABLE = 406;
|
|
|
|
static const int PROXY_AUTHENTICATION_REQUIRED = 407;
|
|
|
|
static const int REQUEST_TIMEOUT = 408;
|
|
|
|
static const int CONFLICT = 409;
|
|
|
|
static const int GONE = 410;
|
|
|
|
static const int LENGTH_REQUIRED = 411;
|
|
|
|
static const int PRECONDITION_FAILED = 412;
|
|
|
|
static const int REQUEST_ENTITY_TOO_LARGE = 413;
|
|
|
|
static const int REQUEST_URI_TOO_LONG = 414;
|
|
|
|
static const int UNSUPPORTED_MEDIA_TYPE = 415;
|
|
|
|
static const int REQUESTED_RANGE_NOT_SATISFIABLE = 416;
|
|
|
|
static const int EXPECTATION_FAILED = 417;
|
|
|
|
static const int INTERNAL_SERVER_ERROR = 500;
|
|
|
|
static const int NOT_IMPLEMENTED = 501;
|
|
|
|
static const int BAD_GATEWAY = 502;
|
|
|
|
static const int SERVICE_UNAVAILABLE = 503;
|
|
|
|
static const int GATEWAY_TIMEOUT = 504;
|
|
|
|
static const int HTTP_VERSION_NOT_SUPPORTED = 505;
|
2012-02-28 15:11:34 +00:00
|
|
|
// Client generated status code.
|
2012-08-27 23:51:37 +00:00
|
|
|
static const int NETWORK_CONNECT_TIMEOUT_ERROR = 599;
|
2012-02-28 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP server.
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
abstract class HttpServer {
|
|
|
|
factory HttpServer() => new _HttpServer();
|
2012-02-28 15:11:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start listening for HTTP requests on the specified [host] and
|
2012-05-08 07:04:06 +00:00
|
|
|
* [port]. If a [port] of 0 is specified the server will choose an
|
|
|
|
* ephemeral port. The optional argument [backlog] can be used to
|
2012-11-28 15:36:10 +00:00
|
|
|
* specify the listen backlog for the underlying OS listen.
|
|
|
|
* The optional argument [certificate_name] is used by the HttpsServer
|
|
|
|
* class, which shares the same interface.
|
|
|
|
* See [addRequestHandler] and [defaultRequestHandler] for
|
2012-05-08 07:04:06 +00:00
|
|
|
* information on how incoming HTTP requests are handled.
|
2012-02-28 15:11:34 +00:00
|
|
|
*/
|
2012-11-28 15:36:10 +00:00
|
|
|
void listen(String host,
|
|
|
|
int port,
|
|
|
|
{int backlog: 128,
|
|
|
|
String certificate_name});
|
2012-02-28 15:11:34 +00:00
|
|
|
|
2012-04-02 08:24:37 +00:00
|
|
|
/**
|
2012-05-08 07:04:06 +00:00
|
|
|
* Attach the HTTP server to an existing [:ServerSocket:]. If the
|
|
|
|
* [HttpServer] is closed, the [HttpServer] will just detach itself,
|
|
|
|
* and not close [serverSocket].
|
2012-04-02 08:24:37 +00:00
|
|
|
*/
|
|
|
|
void listenOn(ServerSocket serverSocket);
|
|
|
|
|
2012-04-18 13:03:43 +00:00
|
|
|
/**
|
|
|
|
* Adds a request handler to the list of request handlers. The
|
|
|
|
* function [matcher] is called with the request and must return
|
|
|
|
* [:true:] if the [handler] should handle the request. The first
|
|
|
|
* handler for which [matcher] returns [:true:] will be handed the
|
2012-04-25 13:27:14 +00:00
|
|
|
* request.
|
2012-04-18 13:03:43 +00:00
|
|
|
*/
|
2012-04-25 13:27:14 +00:00
|
|
|
addRequestHandler(bool matcher(HttpRequest request),
|
|
|
|
void handler(HttpRequest request, HttpResponse response));
|
2012-04-18 13:03:43 +00:00
|
|
|
|
|
|
|
/**
|
2012-04-25 13:27:14 +00:00
|
|
|
* Sets the default request handler. This request handler will be
|
|
|
|
* called if none of the request handlers registered by
|
|
|
|
* [addRequestHandler] matches the current request. If no default
|
|
|
|
* request handler is set the server will just respond with status
|
|
|
|
* code [:NOT_FOUND:] (404).
|
2012-04-18 13:03:43 +00:00
|
|
|
*/
|
2012-04-25 13:27:14 +00:00
|
|
|
void set defaultRequestHandler(
|
|
|
|
void handler(HttpRequest request, HttpResponse response));
|
2012-04-18 13:03:43 +00:00
|
|
|
|
2012-02-28 15:11:34 +00:00
|
|
|
/**
|
|
|
|
* Stop server listening.
|
|
|
|
*/
|
|
|
|
void close();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the port that the server is listening on. This can be
|
|
|
|
* used to get the actual port used when a value of 0 for [port] is
|
|
|
|
* specified in the [listen] call.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
int get port;
|
2012-02-28 15:11:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the error handler that is called when a connection error occurs.
|
|
|
|
*/
|
2012-04-26 08:12:17 +00:00
|
|
|
void set onError(void callback(e));
|
2012-10-22 13:08:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the timeout, in seconds, for sessions of this HTTP server. Default
|
|
|
|
* is 20 minutes.
|
|
|
|
*/
|
2012-10-30 10:32:30 +00:00
|
|
|
set sessionTimeout(int timeout);
|
2012-11-19 11:11:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a [:HttpConnectionsInfo:] object with an overview of the
|
|
|
|
* current connection handled by the server.
|
|
|
|
*/
|
|
|
|
HttpConnectionsInfo connectionsInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-28 15:36:10 +00:00
|
|
|
/**
|
|
|
|
* HTTPS server.
|
|
|
|
*/
|
|
|
|
abstract class HttpsServer implements HttpServer {
|
|
|
|
factory HttpsServer() => new _HttpServer.httpsServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-19 11:11:00 +00:00
|
|
|
/**
|
|
|
|
* Overview information of the [:HttpServer:] socket connections.
|
|
|
|
*/
|
|
|
|
class HttpConnectionsInfo {
|
|
|
|
/**
|
|
|
|
* Total number of socket connections.
|
|
|
|
*/
|
|
|
|
int total = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of active connections where actual request/response
|
|
|
|
* processing is active.
|
|
|
|
*/
|
|
|
|
int active = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of idle connections held by clients as persistent connections.
|
|
|
|
*/
|
|
|
|
int idle = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of connections which are preparing to close. Note: These
|
|
|
|
* connections are also part of the [:active:] count as they might
|
|
|
|
* still be sending data to the client before finally closing.
|
|
|
|
*/
|
|
|
|
int closing = 0;
|
2012-02-28 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-16 12:18:45 +00:00
|
|
|
/**
|
|
|
|
* Access to the HTTP headers for requests and responses. In some
|
|
|
|
* situations the headers will be imutable and the mutating methods
|
|
|
|
* will then throw exceptions.
|
|
|
|
*
|
|
|
|
* For all operation on HTTP headers the header name is
|
|
|
|
* case-insensitive.
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
abstract class HttpHeaders {
|
2012-08-27 23:51:37 +00:00
|
|
|
static const ACCEPT = "Accept";
|
|
|
|
static const ACCEPT_CHARSET = "Accept-Charset";
|
|
|
|
static const ACCEPT_ENCODING = "Accept-Encoding";
|
|
|
|
static const ACCEPT_LANGUAGE = "Accept-Language";
|
|
|
|
static const ACCEPT_RANGES = "Accept-Ranges";
|
|
|
|
static const AGE = "Age";
|
|
|
|
static const ALLOW = "Allow";
|
|
|
|
static const AUTHORIZATION = "Authorization";
|
|
|
|
static const CACHE_CONTROL = "Cache-Control";
|
|
|
|
static const CONNECTION = "Connection";
|
|
|
|
static const CONTENT_ENCODING = "Content-Encoding";
|
|
|
|
static const CONTENT_LANGUAGE = "Content-Language";
|
|
|
|
static const CONTENT_LENGTH = "Content-Length";
|
|
|
|
static const CONTENT_LOCATION = "Content-Location";
|
|
|
|
static const CONTENT_MD5 = "Content-MD5";
|
|
|
|
static const CONTENT_RANGE = "Content-Range";
|
|
|
|
static const CONTENT_TYPE = "Content-Type";
|
|
|
|
static const DATE = "Date";
|
|
|
|
static const ETAG = "ETag";
|
|
|
|
static const EXPECT = "Expect";
|
|
|
|
static const EXPIRES = "Expires";
|
|
|
|
static const FROM = "From";
|
|
|
|
static const HOST = "Host";
|
|
|
|
static const IF_MATCH = "If-Match";
|
|
|
|
static const IF_MODIFIED_SINCE = "If-Modified-Since";
|
|
|
|
static const IF_NONE_MATCH = "If-None-Match";
|
|
|
|
static const IF_RANGE = "If-Range";
|
|
|
|
static const IF_UNMODIFIED_SINCE = "If-Unmodified-Since";
|
|
|
|
static const LAST_MODIFIED = "Last-Modified";
|
|
|
|
static const LOCATION = "Location";
|
|
|
|
static const MAX_FORWARDS = "Max-Forwards";
|
|
|
|
static const PRAGMA = "Pragma";
|
|
|
|
static const PROXY_AUTHENTICATE = "Proxy-Authenticate";
|
|
|
|
static const PROXY_AUTHORIZATION = "Proxy-Authorization";
|
|
|
|
static const RANGE = "Range";
|
|
|
|
static const REFERER = "Referer";
|
|
|
|
static const RETRY_AFTER = "Retry-After";
|
|
|
|
static const SERVER = "Server";
|
|
|
|
static const TE = "TE";
|
|
|
|
static const TRAILER = "Trailer";
|
|
|
|
static const TRANSFER_ENCODING = "Transfer-Encoding";
|
|
|
|
static const UPGRADE = "Upgrade";
|
|
|
|
static const USER_AGENT = "User-Agent";
|
|
|
|
static const VARY = "Vary";
|
|
|
|
static const VIA = "Via";
|
|
|
|
static const WARNING = "Warning";
|
|
|
|
static const WWW_AUTHENTICATE = "WWW-Authenticate";
|
|
|
|
|
|
|
|
static const GENERAL_HEADERS = const [CACHE_CONTROL,
|
2012-04-16 12:18:45 +00:00
|
|
|
CONNECTION,
|
|
|
|
DATE,
|
|
|
|
PRAGMA,
|
|
|
|
TRAILER,
|
|
|
|
TRANSFER_ENCODING,
|
|
|
|
UPGRADE,
|
|
|
|
VIA,
|
|
|
|
WARNING];
|
|
|
|
|
2012-08-27 23:51:37 +00:00
|
|
|
static const ENTITY_HEADERS = const [ALLOW,
|
2012-09-06 07:31:46 +00:00
|
|
|
CONTENT_ENCODING,
|
|
|
|
CONTENT_LANGUAGE,
|
|
|
|
CONTENT_LENGTH,
|
|
|
|
CONTENT_LOCATION,
|
|
|
|
CONTENT_MD5,
|
|
|
|
CONTENT_RANGE,
|
|
|
|
CONTENT_TYPE,
|
2012-04-16 12:18:45 +00:00
|
|
|
EXPIRES,
|
2012-09-06 07:31:46 +00:00
|
|
|
LAST_MODIFIED];
|
2012-04-16 12:18:45 +00:00
|
|
|
|
|
|
|
|
2012-09-06 07:31:46 +00:00
|
|
|
static const RESPONSE_HEADERS = const [ACCEPT_RANGES,
|
2012-04-16 12:18:45 +00:00
|
|
|
AGE,
|
|
|
|
ETAG,
|
|
|
|
LOCATION,
|
2012-09-06 07:31:46 +00:00
|
|
|
PROXY_AUTHENTICATE,
|
|
|
|
RETRY_AFTER,
|
2012-04-16 12:18:45 +00:00
|
|
|
SERVER,
|
|
|
|
VARY,
|
2012-09-06 07:31:46 +00:00
|
|
|
WWW_AUTHENTICATE];
|
2012-04-16 12:18:45 +00:00
|
|
|
|
2012-08-27 23:51:37 +00:00
|
|
|
static const REQUEST_HEADERS = const [ACCEPT,
|
2012-09-06 07:31:46 +00:00
|
|
|
ACCEPT_CHARSET,
|
|
|
|
ACCEPT_ENCODING,
|
|
|
|
ACCEPT_LANGUAGE,
|
2012-04-16 12:18:45 +00:00
|
|
|
AUTHORIZATION,
|
|
|
|
EXPECT,
|
|
|
|
FROM,
|
|
|
|
HOST,
|
2012-09-06 07:31:46 +00:00
|
|
|
IF_MATCH,
|
|
|
|
IF_MODIFIED_SINCE,
|
|
|
|
IF_NONE_MATCH,
|
|
|
|
IF_RANGE,
|
|
|
|
IF_UNMODIFIED_SINCE,
|
|
|
|
MAX_FORWARDS,
|
|
|
|
PROXY_AUTHORIZATION,
|
2012-04-16 12:18:45 +00:00
|
|
|
RANGE,
|
|
|
|
REFERER,
|
|
|
|
TE,
|
2012-09-06 07:31:46 +00:00
|
|
|
USER_AGENT];
|
2012-04-16 12:18:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the list of values for the header named [name]. If there
|
2012-05-22 12:53:35 +00:00
|
|
|
* is no headers with the provided name [:null:] will be returned.
|
2012-04-16 12:18:45 +00:00
|
|
|
*/
|
|
|
|
List<String> operator[](String name);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience method for the value for a single values header. If
|
2012-05-22 12:53:35 +00:00
|
|
|
* there is no header with the provided name [:null:] will be
|
2012-04-16 12:18:45 +00:00
|
|
|
* returned. If the header has more than one value an exception is
|
|
|
|
* thrown.
|
|
|
|
*/
|
|
|
|
String value(String name);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a header value. The header named [name] will have the value
|
|
|
|
* [value] added to its list of values. Some headers are single
|
|
|
|
* values and for these adding a value will replace the previous
|
|
|
|
* value. If the value is of type Date a HTTP date format will be
|
|
|
|
* applied. If the value is a [:List:] each element of the list will
|
|
|
|
* be added separately. For all other types the default [:toString:]
|
|
|
|
* method will be used.
|
|
|
|
*/
|
|
|
|
void add(String name, Object value);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a header. The header named [name] will have all its values
|
|
|
|
* cleared before the value [value] is added as its value.
|
|
|
|
*/
|
|
|
|
void set(String name, Object value);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a specific value for a header name. Some headers have
|
|
|
|
* system supplied values and for these the system supplied values
|
|
|
|
* will still be added to the collection of values for the header.
|
|
|
|
*/
|
|
|
|
void remove(String name, Object value);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all values for the specified header name. Some headers
|
|
|
|
* have system supplied values and for these the system supplied
|
|
|
|
* values will still be added to the collection of values for the
|
|
|
|
* header.
|
|
|
|
*/
|
|
|
|
void removeAll(String name);
|
2012-04-17 07:37:47 +00:00
|
|
|
|
2012-05-07 13:52:14 +00:00
|
|
|
/**
|
|
|
|
* Enumerate the headers applying the function [f] to each
|
2012-05-08 07:04:06 +00:00
|
|
|
* header. The header name passed in [name] will be all lower
|
2012-05-07 13:52:14 +00:00
|
|
|
* case.
|
|
|
|
*/
|
|
|
|
void forEach(void f(String name, List<String> values));
|
|
|
|
|
2012-05-24 13:03:31 +00:00
|
|
|
/**
|
|
|
|
* Disable folding for the header named [name] when sending the HTTP
|
|
|
|
* header. By default, multiple header values are folded into a
|
|
|
|
* single header line by separating the values with commas. The
|
|
|
|
* Set-Cookie header has folding disabled by default.
|
|
|
|
*/
|
|
|
|
void noFolding(String name);
|
|
|
|
|
2012-04-25 09:16:37 +00:00
|
|
|
/**
|
|
|
|
* Gets and sets the date. The value of this property will
|
2012-08-16 06:50:54 +00:00
|
|
|
* reflect the "Date" header.
|
2012-04-25 09:16:37 +00:00
|
|
|
*/
|
|
|
|
Date date;
|
|
|
|
|
2012-04-17 07:37:47 +00:00
|
|
|
/**
|
|
|
|
* Gets and sets the expiry date. The value of this property will
|
2012-08-16 06:50:54 +00:00
|
|
|
* reflect the "Expires" header.
|
2012-04-17 07:37:47 +00:00
|
|
|
*/
|
|
|
|
Date expires;
|
|
|
|
|
2012-08-16 06:50:54 +00:00
|
|
|
/**
|
|
|
|
* Gets and sets the 'if-modified-since' date. The value of this property will
|
|
|
|
* reflect the "if-modified-since" header.
|
|
|
|
*/
|
|
|
|
Date ifModifiedSince;
|
|
|
|
|
2012-04-17 07:37:47 +00:00
|
|
|
/**
|
|
|
|
* Gets and sets the host part of the "Host" header for the
|
|
|
|
* connection.
|
|
|
|
*/
|
|
|
|
String host;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets and sets the port part of the "Host" header for the
|
|
|
|
* connection.
|
|
|
|
*/
|
|
|
|
int port;
|
2012-05-22 12:53:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets and sets the content type. Note that the content type in the
|
|
|
|
* header will only be updated if this field is set
|
|
|
|
* directly. Mutating the returned current value will have no
|
|
|
|
* effect.
|
|
|
|
*/
|
|
|
|
ContentType contentType;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Representation of a header value in the form:
|
|
|
|
*
|
|
|
|
* [:value; parameter1=value1; parameter2=value2:]
|
|
|
|
*
|
|
|
|
* [HeaderValue] can be used to conveniently build and parse header
|
|
|
|
* values on this form.
|
|
|
|
*
|
|
|
|
* To build an [:Accepts:] header with the value
|
|
|
|
*
|
|
|
|
* text/plain; q=0.3, text/html
|
|
|
|
*
|
|
|
|
* use code like this:
|
|
|
|
*
|
|
|
|
* HttpClientRequest request = ...;
|
|
|
|
* var v = new HeaderValue();
|
|
|
|
* v.value = "text/plain";
|
|
|
|
* v.parameters["q"] = "0.3"
|
|
|
|
* request.headers.add(HttpHeaders.ACCEPT, v);
|
|
|
|
* request.headers.add(HttpHeaders.ACCEPT, "text/html");
|
|
|
|
*
|
|
|
|
* To parse the header values use the [:fromString:] constructor.
|
|
|
|
*
|
|
|
|
* HttpRequest request = ...;
|
|
|
|
* List<String> values = request.headers[HttpHeaders.ACCEPT];
|
|
|
|
* values.forEach((value) {
|
|
|
|
* HeaderValue v = new HeaderValue.fromString(value);
|
|
|
|
* // Use v.value and v.parameters
|
|
|
|
* });
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
abstract class HeaderValue {
|
2012-05-22 12:53:35 +00:00
|
|
|
/**
|
|
|
|
* Creates a new header value object setting the value part.
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
factory HeaderValue([String value = ""]) => new _HeaderValue(value);
|
2012-05-22 12:53:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new header value object from parsing a header value
|
|
|
|
* string with both value and optional parameters.
|
|
|
|
*/
|
2012-10-29 12:13:07 +00:00
|
|
|
factory HeaderValue.fromString(String value,
|
|
|
|
{String parameterSeparator: ";"}) {
|
|
|
|
return new _HeaderValue.fromString(
|
|
|
|
value, parameterSeparator: parameterSeparator);
|
2012-09-18 10:54:37 +00:00
|
|
|
}
|
2012-05-22 12:53:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets and sets the header value.
|
|
|
|
*/
|
|
|
|
String value;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the map of parameters.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
Map<String, String> get parameters;
|
2012-05-22 12:53:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the formatted string representation in the form:
|
|
|
|
*
|
|
|
|
* value; parameter1=value1; parameter2=value2
|
|
|
|
*/
|
|
|
|
String toString();
|
|
|
|
}
|
|
|
|
|
2012-10-22 13:08:42 +00:00
|
|
|
abstract class HttpSession {
|
|
|
|
/**
|
|
|
|
* Get the id for the current session.
|
|
|
|
*/
|
|
|
|
String get id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access the user-data associated with the session.
|
|
|
|
*/
|
2012-10-30 10:32:30 +00:00
|
|
|
dynamic data;
|
2012-10-22 13:08:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy the session. This will terminate the session and any further
|
|
|
|
* connections with this id will be given a new id and session.
|
|
|
|
*/
|
|
|
|
void destroy();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a callback that will be called when the session is timed out.
|
|
|
|
*/
|
|
|
|
void set onTimeout(void callback());
|
|
|
|
}
|
|
|
|
|
2012-05-22 12:53:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Representation of a content type.
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
abstract class ContentType implements HeaderValue {
|
2012-05-22 12:53:35 +00:00
|
|
|
/**
|
|
|
|
* Creates a new content type object setting the primary type and
|
2012-09-18 10:54:37 +00:00
|
|
|
* sub type.
|
2012-05-22 12:53:35 +00:00
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
factory ContentType([String primaryType = "", String subType = ""]) {
|
|
|
|
return new _ContentType(primaryType, subType);
|
|
|
|
}
|
2012-05-22 12:53:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new content type object from parsing a Content-Type
|
|
|
|
* header value. As primary type, sub type and parameter names and
|
|
|
|
* values are not case sensitive all these values will be converted
|
|
|
|
* to lower case. Parsing this string
|
|
|
|
*
|
|
|
|
* text/html; charset=utf-8
|
|
|
|
*
|
|
|
|
* will create a content type object with primary type [:text:], sub
|
|
|
|
* type [:html:] and parameter [:charset:] with value [:utf-8:].
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
factory ContentType.fromString(String value) {
|
|
|
|
return new _ContentType.fromString(value);
|
|
|
|
}
|
2012-05-22 12:53:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets and sets the content type in the form "primaryType/subType".
|
|
|
|
*/
|
|
|
|
String value;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets and sets the primary type.
|
|
|
|
*/
|
|
|
|
String primaryType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets and sets the sub type.
|
|
|
|
*/
|
|
|
|
String subType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets and sets the character set.
|
|
|
|
*/
|
|
|
|
String charset;
|
2012-04-16 12:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-24 13:03:31 +00:00
|
|
|
/**
|
|
|
|
* Representation of a cookie. For cookies received by the server as
|
|
|
|
* Cookie header values only [:name:] and [:value:] fields will be
|
|
|
|
* set. When building a cookie for the Set-Cookie header in the server
|
|
|
|
* and when receiving cookies in the client as Set-Cookie headers all
|
|
|
|
* fields can be used.
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
abstract class Cookie {
|
2012-05-24 13:03:31 +00:00
|
|
|
/**
|
|
|
|
* Creates a new cookie optionally setting the name and value.
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
factory Cookie([String name, String value]) => new _Cookie(name, value);
|
2012-05-24 13:03:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new cookie by parsing a header value from a Set-Cookie
|
|
|
|
* header.
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
factory Cookie.fromSetCookieValue(String value) {
|
|
|
|
return new _Cookie.fromSetCookieValue(value);
|
|
|
|
}
|
2012-05-24 13:03:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets and sets the name.
|
|
|
|
*/
|
|
|
|
String name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets and sets the value.
|
|
|
|
*/
|
|
|
|
String value;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets and sets the expiry date.
|
|
|
|
*/
|
|
|
|
Date expires;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets and sets the max age. A value of [:0:] means delete cookie
|
|
|
|
* now.
|
|
|
|
*/
|
|
|
|
int maxAge;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets and sets the domain.
|
|
|
|
*/
|
|
|
|
String domain;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets and sets the path.
|
|
|
|
*/
|
|
|
|
String path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets and sets whether this cookie is secure.
|
|
|
|
*/
|
|
|
|
bool secure;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets and sets whether this cookie is HTTP only.
|
|
|
|
*/
|
|
|
|
bool httpOnly;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the formatted string representation of the cookie. The
|
|
|
|
* string representation can be used for for setting the Cookie or
|
|
|
|
* Set-Cookie headers
|
|
|
|
*/
|
|
|
|
String toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-28 15:11:34 +00:00
|
|
|
/**
|
|
|
|
* Http request delivered to the HTTP server callback.
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
abstract class HttpRequest {
|
2012-02-28 15:11:34 +00:00
|
|
|
/**
|
|
|
|
* Returns the content length of the request body. If the size of
|
|
|
|
* the request body is not known in advance this -1.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
int get contentLength;
|
2012-02-28 15:11:34 +00:00
|
|
|
|
2012-05-03 07:08:32 +00:00
|
|
|
/**
|
|
|
|
* Returns the persistent connection state signaled by the client.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
bool get persistentConnection;
|
2012-05-03 07:08:32 +00:00
|
|
|
|
2012-02-28 15:11:34 +00:00
|
|
|
/**
|
|
|
|
* Returns the method for the request.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
String get method;
|
2012-02-28 15:11:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the URI for the request.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
String get uri;
|
2012-02-28 15:11:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the path part of the URI.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
String get path;
|
2012-02-28 15:11:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the query string.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
String get queryString;
|
2012-02-28 15:11:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the parsed query string.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
Map<String, String> get queryParameters;
|
2012-02-28 15:11:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the request headers.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
HttpHeaders get headers;
|
2012-02-28 15:11:34 +00:00
|
|
|
|
2012-05-24 13:03:31 +00:00
|
|
|
/**
|
2012-10-22 13:08:42 +00:00
|
|
|
* Returns the cookies in the request (from the Cookie headers).
|
2012-05-24 13:03:31 +00:00
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
List<Cookie> get cookies;
|
2012-05-24 13:03:31 +00:00
|
|
|
|
2012-10-22 13:08:42 +00:00
|
|
|
/**
|
|
|
|
* Returns, or initialize, a session for the given request. If the session is
|
|
|
|
* being initialized by this call, [init] will be called with the
|
|
|
|
* newly create session. Here the [:HttpSession.data:] field can be set, if
|
|
|
|
* needed.
|
|
|
|
* See [:HttpServer.sessionTimeout:] on how to change default timeout.
|
|
|
|
*/
|
|
|
|
HttpSession session([init(HttpSession session)]);
|
|
|
|
|
2012-02-28 15:11:34 +00:00
|
|
|
/**
|
|
|
|
* Returns the input stream for the request. This is used to read
|
|
|
|
* the request data.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
InputStream get inputStream;
|
2012-05-09 09:06:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the HTTP protocol version used in the request. This will
|
|
|
|
* be "1.0" or "1.1".
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
String get protocolVersion;
|
2012-07-19 15:18:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get information about the client connection. Returns [null] if the socket
|
|
|
|
* isn't available.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
HttpConnectionInfo get connectionInfo;
|
2012-02-28 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP response to be send back to the client.
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
abstract class HttpResponse {
|
2012-02-28 15:11:34 +00:00
|
|
|
/**
|
|
|
|
* Gets and sets the content length of the response. If the size of
|
|
|
|
* the response is not known in advance set the content length to
|
|
|
|
* -1 - which is also the default if not set.
|
|
|
|
*/
|
2012-02-29 12:59:44 +00:00
|
|
|
int contentLength;
|
2012-02-28 15:11:34 +00:00
|
|
|
|
2012-02-29 12:59:44 +00:00
|
|
|
/**
|
2012-09-21 09:06:50 +00:00
|
|
|
* Gets and sets the status code. Any integer value is accepted. For
|
|
|
|
* the official HTTP status codes use the fields from
|
|
|
|
* [HttpStatus]. If no status code is explicitly set the default
|
|
|
|
* value [HttpStatus.OK] is used.
|
2012-02-29 12:59:44 +00:00
|
|
|
*/
|
|
|
|
int statusCode;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets and sets the reason phrase. If no reason phrase is explicitly
|
|
|
|
* set a default reason phrase is provided.
|
|
|
|
*/
|
|
|
|
String reasonPhrase;
|
2012-02-28 15:11:34 +00:00
|
|
|
|
2012-05-03 07:08:32 +00:00
|
|
|
/**
|
|
|
|
* Gets and sets the persistent connection state. The initial value
|
|
|
|
* of this property is the persistent connection state from the
|
|
|
|
* request.
|
|
|
|
*/
|
|
|
|
bool persistentConnection;
|
|
|
|
|
2012-03-06 10:59:28 +00:00
|
|
|
/**
|
|
|
|
* Returns the response headers.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
HttpHeaders get headers;
|
2012-03-06 10:59:28 +00:00
|
|
|
|
2012-05-24 13:03:31 +00:00
|
|
|
/**
|
|
|
|
* Cookies to set in the client (in the Set-Cookie header).
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
List<Cookie> get cookies;
|
2012-05-24 13:03:31 +00:00
|
|
|
|
2012-02-28 15:11:34 +00:00
|
|
|
/**
|
|
|
|
* Returns the output stream for the response. This is used to write
|
|
|
|
* the response data. When all response data has been written close
|
|
|
|
* the stream to indicate the end of the response.
|
|
|
|
*
|
|
|
|
* When this is accessed for the first time the response header is
|
|
|
|
* send. Calling any methods that will change the header after
|
|
|
|
* having retrieved the output stream will throw an exception.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
OutputStream get outputStream;
|
2012-04-26 10:15:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Detach the underlying socket from the HTTP server. When the
|
|
|
|
* socket is detached the HTTP server will no longer perform any
|
|
|
|
* operations on it.
|
|
|
|
*
|
2012-05-02 10:30:38 +00:00
|
|
|
* This is normally used when a HTTP upgrade request is received
|
2012-04-26 10:15:30 +00:00
|
|
|
* and the communication should continue with a different protocol.
|
|
|
|
*/
|
2012-05-02 10:30:38 +00:00
|
|
|
DetachedSocket detachSocket();
|
2012-07-19 15:18:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get information about the client connection. Returns [null] if the socket
|
|
|
|
* isn't available.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
HttpConnectionInfo get connectionInfo;
|
2012-02-28 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-06-22 09:13:18 +00:00
|
|
|
* HTTP client factory. The [HttpClient] handles all the sockets associated
|
|
|
|
* with the [HttpClientConnection]s and when the endpoint supports it, it will
|
|
|
|
* try to reuse opened sockets for several requests to support HTTP 1.1
|
|
|
|
* persistent connections. This means that sockets will be kept open for some
|
|
|
|
* time after a requests have completed, unless HTTP procedures indicate that it
|
|
|
|
* must be closed as part of completing the request. Use [:HttpClient.shutdown:]
|
|
|
|
* to force close the idle sockets.
|
2012-02-28 15:11:34 +00:00
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
abstract class HttpClient {
|
2012-08-27 23:51:37 +00:00
|
|
|
static const int DEFAULT_HTTP_PORT = 80;
|
2012-11-28 13:47:17 +00:00
|
|
|
static const int DEFAULT_HTTPS_PORT = 443;
|
2012-03-06 10:59:28 +00:00
|
|
|
|
2012-09-18 10:54:37 +00:00
|
|
|
factory HttpClient() => new _HttpClient();
|
2012-02-28 15:11:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a HTTP connection. The returned [HttpClientConnection] is
|
2012-03-06 10:59:28 +00:00
|
|
|
* used to register callbacks for asynchronous events on the HTTP
|
|
|
|
* connection. The "Host" header for the request will be set to the
|
|
|
|
* value [host]:[port]. This can be overridden through the
|
|
|
|
* HttpClientRequest interface before the request is sent. NOTE if
|
|
|
|
* [host] is an IP address this will still be set in the "Host"
|
|
|
|
* header.
|
2012-02-28 15:11:34 +00:00
|
|
|
*/
|
|
|
|
HttpClientConnection open(String method, String host, int port, String path);
|
|
|
|
|
2012-03-08 08:44:59 +00:00
|
|
|
/**
|
|
|
|
* Opens a HTTP connection. The returned [HttpClientConnection] is
|
|
|
|
* used to register callbacks for asynchronous events on the HTTP
|
|
|
|
* connection. The "Host" header for the request will be set based
|
|
|
|
* the host and port specified in [url]. This can be overridden
|
|
|
|
* through the HttpClientRequest interface before the request is
|
|
|
|
* sent. NOTE if the host is specified as an IP address this will
|
|
|
|
* still be set in the "Host" header.
|
|
|
|
*/
|
|
|
|
HttpClientConnection openUrl(String method, Uri url);
|
|
|
|
|
2012-02-28 15:11:34 +00:00
|
|
|
/**
|
2012-04-26 11:43:32 +00:00
|
|
|
* Opens a HTTP connection using the GET method. See [open] for
|
|
|
|
* details. Using this method to open a HTTP connection will set the
|
|
|
|
* content length to 0.
|
2012-02-28 15:11:34 +00:00
|
|
|
*/
|
|
|
|
HttpClientConnection get(String host, int port, String path);
|
|
|
|
|
2012-03-08 08:44:59 +00:00
|
|
|
/**
|
2012-04-26 11:43:32 +00:00
|
|
|
* Opens a HTTP connection using the GET method. See [openUrl] for
|
|
|
|
* details. Using this method to open a HTTP connection will set the
|
|
|
|
* content length to 0.
|
2012-03-08 08:44:59 +00:00
|
|
|
*/
|
|
|
|
HttpClientConnection getUrl(Uri url);
|
|
|
|
|
2012-02-28 15:11:34 +00:00
|
|
|
/**
|
2012-03-06 10:59:28 +00:00
|
|
|
* Opens a HTTP connection using the POST method. See [open] for details.
|
2012-02-28 15:11:34 +00:00
|
|
|
*/
|
|
|
|
HttpClientConnection post(String host, int port, String path);
|
|
|
|
|
2012-03-08 08:44:59 +00:00
|
|
|
/**
|
|
|
|
* Opens a HTTP connection using the POST method. See [openUrl] for details.
|
|
|
|
*/
|
|
|
|
HttpClientConnection postUrl(Uri url);
|
|
|
|
|
2012-10-29 12:13:07 +00:00
|
|
|
/**
|
|
|
|
* Sets the function to be called when a site is requesting
|
|
|
|
* authentication. The URL requested and the security realm from the
|
|
|
|
* server are passed in the arguments [url] and [realm].
|
|
|
|
*
|
|
|
|
* The function returns a [Future] which should complete when the
|
2012-10-29 12:23:38 +00:00
|
|
|
* authentication has been resolved. If credentials cannot be
|
2012-10-29 12:13:07 +00:00
|
|
|
* provided the [Future] should complete with [false]. If
|
|
|
|
* credentials are available the function should add these using
|
|
|
|
* [addCredentials] before completing the [Future] with the value
|
|
|
|
* [true].
|
|
|
|
*
|
|
|
|
* If the [Future] completes with true the request will be retried
|
|
|
|
* using the updated credentials. Otherwise response processing will
|
|
|
|
* continue normally.
|
|
|
|
*/
|
|
|
|
set authenticate(Future<bool> f(Uri url, String scheme, String realm));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add credentials to be used for authorizing HTTP requests.
|
|
|
|
*/
|
|
|
|
void addCredentials(Uri url, String realm, HttpClientCredentials credentials);
|
|
|
|
|
2012-10-08 12:19:14 +00:00
|
|
|
/**
|
|
|
|
* Sets the function used to resolve the proxy server to be used for
|
|
|
|
* opening a HTTP connection to the specified [url]. If this
|
|
|
|
* function is not set, direct connections will always be used.
|
|
|
|
*
|
|
|
|
* The string returned by [f] must be in the format used by browser
|
|
|
|
* PAC (proxy auto-config) scripts. That is either
|
|
|
|
*
|
|
|
|
* "DIRECT"
|
|
|
|
*
|
|
|
|
* for using a direct connection or
|
|
|
|
*
|
|
|
|
* "PROXY host:port"
|
|
|
|
*
|
|
|
|
* for using the proxy server [:host:] on port [:port:].
|
|
|
|
*
|
|
|
|
* A configuration can contain several configuration elements
|
|
|
|
* separated by semicolons, e.g.
|
|
|
|
*
|
|
|
|
* "PROXY host:port; PROXY host2:port2; DIRECT"
|
|
|
|
*/
|
|
|
|
set findProxy(String f(Uri url));
|
|
|
|
|
2012-02-28 15:11:34 +00:00
|
|
|
/**
|
2012-11-23 09:28:34 +00:00
|
|
|
* Shutdown the HTTP client. If [force] is [:false:] (the default)
|
|
|
|
* the [:HttpClient:] will be kept alive until all active
|
|
|
|
* connections are done. If [force] is [:true:] any active
|
|
|
|
* connections will be closed to immediately release all
|
|
|
|
* resources. These closed connections will receive an [:onError:]
|
|
|
|
* callback to indicate that the client was shutdown. In both cases
|
|
|
|
* trying to establish a new connection after calling [shutdown]
|
|
|
|
* will throw an exception.
|
|
|
|
*/
|
|
|
|
void shutdown({bool force: false});
|
2012-02-28 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A [HttpClientConnection] is returned by all [HttpClient] methods
|
|
|
|
* that initiate a connection to an HTTP server. The handlers will be
|
|
|
|
* called as the connection state progresses.
|
|
|
|
*
|
2012-03-01 10:07:35 +00:00
|
|
|
* The setting of all handlers is optional. If [onRequest] is not set
|
|
|
|
* the request will be send without any additional headers and an
|
|
|
|
* empty body. If [onResponse] is not set the response will be read
|
|
|
|
* and discarded.
|
2012-02-28 15:11:34 +00:00
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
abstract class HttpClientConnection {
|
2012-02-28 15:11:34 +00:00
|
|
|
/**
|
|
|
|
* Sets the handler that is called when the connection is established.
|
|
|
|
*/
|
2012-03-23 08:00:27 +00:00
|
|
|
void set onRequest(void callback(HttpClientRequest request));
|
2012-02-28 15:11:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets callback to be called when the request has been send and
|
|
|
|
* the response is ready for processing. The callback is called when
|
|
|
|
* all headers of the response are received and data is ready to be
|
|
|
|
* received.
|
|
|
|
*/
|
2012-03-23 08:00:27 +00:00
|
|
|
void set onResponse(void callback(HttpClientResponse response));
|
2012-02-28 15:11:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the handler that gets called if an error occurs while
|
2012-03-23 08:00:27 +00:00
|
|
|
* connecting or processing the HTTP request.
|
2012-02-28 15:11:34 +00:00
|
|
|
*/
|
2012-04-26 08:12:17 +00:00
|
|
|
void set onError(void callback(e));
|
2012-05-02 10:30:38 +00:00
|
|
|
|
2012-05-08 13:32:45 +00:00
|
|
|
/**
|
|
|
|
* Set this property to [:true:] if this connection should
|
|
|
|
* automatically follow redirects. The default is [:true:].
|
|
|
|
*/
|
2012-06-11 15:09:54 +00:00
|
|
|
bool followRedirects;
|
2012-05-08 13:32:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set this property to the maximum number of redirects to follow
|
2012-06-11 15:09:54 +00:00
|
|
|
* when [followRedirects] is [:true:]. If this number is exceeded the
|
2012-05-08 13:32:45 +00:00
|
|
|
* [onError] callback will be called with a [RedirectLimitExceeded]
|
|
|
|
* exception. The default value is 5.
|
|
|
|
*/
|
|
|
|
int maxRedirects;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the series of redirects this connection has been through.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
List<RedirectInfo> get redirects;
|
2012-05-08 13:32:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirect this connection to a new URL. The default value for
|
|
|
|
* [method] is the method for the current request. The default value
|
|
|
|
* for [url] is the value of the [:HttpStatus.LOCATION:] header of
|
|
|
|
* the current response. All body data must have been read from the
|
|
|
|
* current response before calling [redirect].
|
|
|
|
*/
|
|
|
|
void redirect([String method, Uri url]);
|
|
|
|
|
2012-05-02 10:30:38 +00:00
|
|
|
/**
|
|
|
|
* Detach the underlying socket from the HTTP client. When the
|
|
|
|
* socket is detached the HTTP client will no longer perform any
|
|
|
|
* operations on it.
|
|
|
|
*
|
|
|
|
* This is normally used when a HTTP upgrade is negotiated and the
|
|
|
|
* communication should continue with a different protocol.
|
|
|
|
*/
|
|
|
|
DetachedSocket detachSocket();
|
2012-07-19 15:18:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get information about the client connection. Returns [null] if the socket
|
|
|
|
* isn't available.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
HttpConnectionInfo get connectionInfo;
|
2012-02-28 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP request for a client connection.
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
abstract class HttpClientRequest {
|
2012-02-28 15:11:34 +00:00
|
|
|
/**
|
|
|
|
* Gets and sets the content length of the request. If the size of
|
|
|
|
* the request is not known in advance set content length to -1,
|
|
|
|
* which is also the default.
|
|
|
|
*/
|
2012-02-29 12:59:44 +00:00
|
|
|
int contentLength;
|
2012-02-28 15:11:34 +00:00
|
|
|
|
2012-03-06 10:59:28 +00:00
|
|
|
/**
|
|
|
|
* Returns the request headers.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
HttpHeaders get headers;
|
2012-03-06 10:59:28 +00:00
|
|
|
|
2012-05-24 13:03:31 +00:00
|
|
|
/**
|
|
|
|
* Cookies to present to the server (in the Cookie header).
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
List<Cookie> get cookies;
|
2012-05-24 13:03:31 +00:00
|
|
|
|
2012-06-11 15:09:54 +00:00
|
|
|
/**
|
|
|
|
* Gets and sets the requested persistent connection state.
|
|
|
|
* The default value is [:true:].
|
|
|
|
*/
|
|
|
|
bool persistentConnection;
|
|
|
|
|
2012-02-28 15:11:34 +00:00
|
|
|
/**
|
|
|
|
* Returns the output stream for the request. This is used to write
|
|
|
|
* the request data. When all request data has been written close
|
|
|
|
* the stream to indicate the end of the request.
|
|
|
|
*
|
|
|
|
* When this is accessed for the first time the request header is
|
|
|
|
* send. Calling any methods that will change the header after
|
|
|
|
* having retrieved the output stream will throw an exception.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
OutputStream get outputStream;
|
2012-02-28 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP response for a client connection.
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
abstract class HttpClientResponse {
|
2012-02-28 15:11:34 +00:00
|
|
|
/**
|
|
|
|
* Returns the status code.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
int get statusCode;
|
2012-02-28 15:11:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the reason phrase associated with the status code.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
String get reasonPhrase;
|
2012-02-28 15:11:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the content length of the request body. If the size of
|
|
|
|
* the request body is not known in advance this -1.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
int get contentLength;
|
2012-02-28 15:11:34 +00:00
|
|
|
|
2012-06-11 15:09:54 +00:00
|
|
|
/**
|
|
|
|
* Gets the persistent connection state returned by the server.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
bool get persistentConnection;
|
2012-06-11 15:09:54 +00:00
|
|
|
|
2012-05-08 13:32:45 +00:00
|
|
|
/**
|
|
|
|
* Returns whether the status code is one of the normal redirect
|
|
|
|
* codes [:HttpStatus.MOVED_PERMANENTLY:], [:HttpStatus.FOUND:],
|
|
|
|
* [:HttpStatus.MOVED_TEMPORARILY:], [:HttpStatus.SEE_OTHER:] and
|
|
|
|
* [:HttpStatus.TEMPORARY_REDIRECT:].
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
bool get isRedirect;
|
2012-05-08 13:32:45 +00:00
|
|
|
|
2012-02-28 15:11:34 +00:00
|
|
|
/**
|
|
|
|
* Returns the response headers.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
HttpHeaders get headers;
|
2012-02-28 15:11:34 +00:00
|
|
|
|
2012-05-24 13:03:31 +00:00
|
|
|
/**
|
|
|
|
* Cookies set by the server (from the Set-Cookie header).
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
List<Cookie> get cookies;
|
2012-05-24 13:03:31 +00:00
|
|
|
|
2012-02-28 15:11:34 +00:00
|
|
|
/**
|
|
|
|
* Returns the input stream for the response. This is used to read
|
|
|
|
* the response data.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
InputStream get inputStream;
|
2012-02-28 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 12:13:07 +00:00
|
|
|
|
|
|
|
abstract class HttpClientCredentials { }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represent credentials for basic authentication.
|
|
|
|
*/
|
|
|
|
abstract class HttpClientBasicCredentials extends HttpClientCredentials {
|
|
|
|
factory HttpClientBasicCredentials(String username, String password) =>
|
|
|
|
new _HttpClientBasicCredentials(username, password);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represent credentials for digest authentication.
|
|
|
|
*/
|
|
|
|
abstract class HttpClientDigestCredentials extends HttpClientCredentials {
|
|
|
|
factory HttpClientDigestCredentials(String username, String password) =>
|
|
|
|
new _HttpClientDigestCredentials(username, password);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-19 15:18:50 +00:00
|
|
|
/**
|
|
|
|
* Connection information.
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
abstract class HttpConnectionInfo {
|
2012-08-24 10:11:05 +00:00
|
|
|
String get remoteHost;
|
|
|
|
int get remotePort;
|
|
|
|
int get localPort;
|
2012-07-19 15:18:50 +00:00
|
|
|
}
|
|
|
|
|
2012-02-28 15:11:34 +00:00
|
|
|
|
2012-05-08 13:32:45 +00:00
|
|
|
/**
|
|
|
|
* Redirect information.
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
abstract class RedirectInfo {
|
2012-05-08 13:32:45 +00:00
|
|
|
/**
|
|
|
|
* Returns the status code used for the redirect.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
int get statusCode;
|
2012-05-08 13:32:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the method used for the redirect.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
String get method;
|
2012-05-08 13:32:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the location for the redirect.
|
|
|
|
*/
|
2012-08-24 10:11:05 +00:00
|
|
|
Uri get location;
|
2012-05-08 13:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-02 10:30:38 +00:00
|
|
|
/**
|
|
|
|
* When detaching a socket from either the [:HttpServer:] or the
|
|
|
|
* [:HttpClient:] due to a HTTP connection upgrade there might be
|
|
|
|
* unparsed data already read from the socket. This unparsed data
|
|
|
|
* together with the detached socket is returned in an instance of
|
|
|
|
* this class.
|
|
|
|
*/
|
2012-09-18 10:54:37 +00:00
|
|
|
abstract class DetachedSocket {
|
2012-08-24 10:11:05 +00:00
|
|
|
Socket get socket;
|
|
|
|
List<int> get unparsedData;
|
2012-05-02 10:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-28 15:11:34 +00:00
|
|
|
class HttpException implements Exception {
|
|
|
|
const HttpException([String this.message = ""]);
|
|
|
|
String toString() => "HttpException: $message";
|
|
|
|
final String message;
|
|
|
|
}
|
2012-05-08 13:32:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RedirectException extends HttpException {
|
|
|
|
const RedirectException(String message,
|
|
|
|
List<RedirectInfo> this.redirects) : super(message);
|
|
|
|
final List<RedirectInfo> redirects;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-09 10:18:24 +00:00
|
|
|
class RedirectLimitExceededException extends RedirectException {
|
|
|
|
const RedirectLimitExceededException(List<RedirectInfo> redirects)
|
2012-05-08 13:32:45 +00:00
|
|
|
: super("Redirect limit exceeded", redirects);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-09 10:18:24 +00:00
|
|
|
class RedirectLoopException extends RedirectException {
|
|
|
|
const RedirectLoopException(List<RedirectInfo> redirects)
|
2012-05-08 13:32:45 +00:00
|
|
|
: super("Redirect loop detected", redirects);
|
|
|
|
}
|