Add support for using an URL for establishing HTTP connections

As the URI library is now part of the standalone VM it makes sense to
have the option of using a URL for opening a HTTP connection

R=ager@google.com

BUG=
TEST=

Review URL: https://chromiumcodereview.appspot.com//9625008

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@5153 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
sgjesse@google.com 2012-03-08 08:44:59 +00:00
parent 2284f69669
commit 300f3fe802
3 changed files with 87 additions and 0 deletions

View file

@ -234,16 +234,37 @@ interface HttpClient default _HttpClient {
*/
HttpClientConnection open(String method, String host, int port, String path);
/**
* 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);
/**
* Opens a HTTP connection using the GET method. See [open] for details.
*/
HttpClientConnection get(String host, int port, String path);
/**
* Opens a HTTP connection using the GET method. See [openUrl] for details.
*/
HttpClientConnection getUrl(Uri url);
/**
* Opens a HTTP connection using the POST method. See [open] for details.
*/
HttpClientConnection post(String host, int port, String path);
/**
* Opens a HTTP connection using the POST method. See [openUrl] for details.
*/
HttpClientConnection postUrl(Uri url);
/**
* Shutdown the HTTP client releasing all resources.
*/

View file

@ -1056,14 +1056,39 @@ class _HttpClient implements HttpClient {
return _prepareHttpClientConnection(host, port, method, path);
}
HttpClientConnection openUrl(String method, Uri url) {
if (url.scheme != "http") {
throw new HttpException("Unsupported URL scheme ${url.scheme}");
}
if (url.userInfo != "") {
throw new HttpException("Unsupported user info ${url.userInfo}");
}
int port = url.port == 0 ? HttpClient.DEFAULT_HTTP_PORT : url.port;
String path;
if (url.query != "") {
if (url.fragment != "") {
path = "${url.path}?${url.query}#${url.fragment}";
} else {
path = "${url.path}?${url.query}";
}
} else {
path = url.path;
}
return open(method, url.domain, port, path);
}
HttpClientConnection get(String host, int port, String path) {
return open("GET", host, port, path);
}
HttpClientConnection getUrl(Uri url) => openUrl("GET", url);
HttpClientConnection post(String host, int port, String path) {
return open("POST", host, port, path);
}
HttpClientConnection postUrl(Uri url) => openUrl("POST", url);
void shutdown() {
_openSockets.forEach((String key, Queue<_SocketConnection> connections) {
while (!connections.isEmpty()) {

View file

@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.
#import("dart:io");
#import("dart:uri");
void testGoogle() {
HttpClient client = new HttpClient();
@ -21,8 +22,48 @@ void testGoogle() {
client.shutdown();
};
};
conn.onError = (error) => Expect.fail("Unexpected IO error");
}
void testGoogleUrl() {
HttpClient client = new HttpClient();
void testUrl(String url) {
var conn = client.getUrl(new Uri.fromString(url));
conn.onRequest = (HttpClientRequest request) {
request.keepAlive = false;
request.outputStream.close();
};
conn.onResponse = (HttpClientResponse response) {
Expect.isTrue(response.statusCode < 500);
response.inputStream.onData = () {
response.inputStream.read();
};
response.inputStream.onClosed = () {
client.shutdown();
};
};
conn.onError = (error) => Expect.fail("Unexpected IO error");
}
testUrl('http://www.google.com');
testUrl('http://www.google.com/abc');
testUrl('http://www.google.com/?abc');
testUrl('http://www.google.com/abc?abc');
testUrl('http://www.google.com/abc?abc#abc');
}
void testInvalidUrl() {
HttpClient client = new HttpClient();
Expect.throws(
() => client.getUrl(new Uri.fromString('ftp://www.google.com')));
Expect.throws(
() => client.getUrl(new Uri.fromString('http://usr:pwd@www.google.com')));
}
void main() {
testGoogle();
testGoogleUrl();
testInvalidUrl();
}