mirror of
https://github.com/dart-lang/sdk
synced 2024-09-23 00:13:55 +00:00
Add 'Server' and 'User-Agent' default header fields.
They include the major and minor version of Dart. BUG= R=sgjesse@google.com Review URL: https://codereview.chromium.org//16758008 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@23844 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
parent
5460153587
commit
2149b3a859
|
@ -21,6 +21,7 @@ void main() {
|
|||
var request = new http.StreamedRequest("POST", serverUrl);
|
||||
request.headers[HttpHeaders.CONTENT_TYPE] =
|
||||
'application/json; charset=utf-8';
|
||||
request.headers[HttpHeaders.USER_AGENT] = 'Dart';
|
||||
|
||||
expect(client.send(request).then((response) {
|
||||
expect(response.request, equals(request));
|
||||
|
@ -37,6 +38,7 @@ void main() {
|
|||
'headers': {
|
||||
'content-type': ['application/json; charset=utf-8'],
|
||||
'accept-encoding': ['gzip'],
|
||||
'user-agent': ['Dart'],
|
||||
'transfer-encoding': ['chunked']
|
||||
},
|
||||
'body': '{"hello": "world"}'
|
||||
|
|
|
@ -28,7 +28,8 @@ main() {
|
|||
expect(startServer().then((_) {
|
||||
expect(http.get(serverUrl, headers: {
|
||||
'X-Random-Header': 'Value',
|
||||
'X-Other-Header': 'Other Value'
|
||||
'X-Other-Header': 'Other Value',
|
||||
'User-Agent': 'Dart'
|
||||
}).then((response) {
|
||||
expect(response.statusCode, equals(200));
|
||||
expect(response.body, parse(equals({
|
||||
|
@ -37,6 +38,7 @@ main() {
|
|||
'headers': {
|
||||
'content-length': ['0'],
|
||||
'accept-encoding': ['gzip'],
|
||||
'user-agent': ['Dart'],
|
||||
'x-random-header': ['Value'],
|
||||
'x-other-header': ['Other Value']
|
||||
},
|
||||
|
@ -49,7 +51,8 @@ main() {
|
|||
expect(startServer().then((_) {
|
||||
expect(http.post(serverUrl, headers: {
|
||||
'X-Random-Header': 'Value',
|
||||
'X-Other-Header': 'Other Value'
|
||||
'X-Other-Header': 'Other Value',
|
||||
'User-Agent': 'Dart'
|
||||
}, fields: {
|
||||
'some-field': 'value',
|
||||
'other-field': 'other value'
|
||||
|
@ -64,6 +67,7 @@ main() {
|
|||
],
|
||||
'content-length': ['40'],
|
||||
'accept-encoding': ['gzip'],
|
||||
'user-agent': ['Dart'],
|
||||
'x-random-header': ['Value'],
|
||||
'x-other-header': ['Other Value']
|
||||
},
|
||||
|
@ -78,7 +82,8 @@ main() {
|
|||
expect(http.post(serverUrl, headers: {
|
||||
'X-Random-Header': 'Value',
|
||||
'X-Other-Header': 'Other Value',
|
||||
'Content-Type': 'text/plain'
|
||||
'Content-Type': 'text/plain',
|
||||
'User-Agent': 'Dart'
|
||||
}).then((response) {
|
||||
expect(response.statusCode, equals(200));
|
||||
expect(response.body, parse(equals({
|
||||
|
@ -88,6 +93,7 @@ main() {
|
|||
'accept-encoding': ['gzip'],
|
||||
'content-length': ['0'],
|
||||
'content-type': ['text/plain'],
|
||||
'user-agent': ['Dart'],
|
||||
'x-random-header': ['Value'],
|
||||
'x-other-header': ['Other Value']
|
||||
}
|
||||
|
@ -100,7 +106,8 @@ main() {
|
|||
expect(startServer().then((_) {
|
||||
expect(http.put(serverUrl, headers: {
|
||||
'X-Random-Header': 'Value',
|
||||
'X-Other-Header': 'Other Value'
|
||||
'X-Other-Header': 'Other Value',
|
||||
'User-Agent': 'Dart'
|
||||
}, fields: {
|
||||
'some-field': 'value',
|
||||
'other-field': 'other value'
|
||||
|
@ -115,6 +122,7 @@ main() {
|
|||
],
|
||||
'accept-encoding': ['gzip'],
|
||||
'content-length': ['40'],
|
||||
'user-agent': ['Dart'],
|
||||
'x-random-header': ['Value'],
|
||||
'x-other-header': ['Other Value']
|
||||
},
|
||||
|
@ -129,7 +137,8 @@ main() {
|
|||
expect(http.put(serverUrl, headers: {
|
||||
'X-Random-Header': 'Value',
|
||||
'X-Other-Header': 'Other Value',
|
||||
'Content-Type': 'text/plain'
|
||||
'Content-Type': 'text/plain',
|
||||
'User-Agent': 'Dart'
|
||||
}).then((response) {
|
||||
expect(response.statusCode, equals(200));
|
||||
expect(response.body, parse(equals({
|
||||
|
@ -139,6 +148,7 @@ main() {
|
|||
'content-length': ['0'],
|
||||
'accept-encoding': ['gzip'],
|
||||
'content-type': ['text/plain'],
|
||||
'user-agent': ['Dart'],
|
||||
'x-random-header': ['Value'],
|
||||
'x-other-header': ['Other Value']
|
||||
}
|
||||
|
@ -151,7 +161,8 @@ main() {
|
|||
expect(startServer().then((_) {
|
||||
expect(http.delete(serverUrl, headers: {
|
||||
'X-Random-Header': 'Value',
|
||||
'X-Other-Header': 'Other Value'
|
||||
'X-Other-Header': 'Other Value',
|
||||
'User-Agent': 'Dart'
|
||||
}).then((response) {
|
||||
expect(response.statusCode, equals(200));
|
||||
expect(response.body, parse(equals({
|
||||
|
@ -160,6 +171,7 @@ main() {
|
|||
'headers': {
|
||||
'content-length': ['0'],
|
||||
'accept-encoding': ['gzip'],
|
||||
'user-agent': ['Dart'],
|
||||
'x-random-header': ['Value'],
|
||||
'x-other-header': ['Other Value']
|
||||
}
|
||||
|
@ -172,13 +184,15 @@ main() {
|
|||
expect(startServer().then((_) {
|
||||
expect(http.read(serverUrl, headers: {
|
||||
'X-Random-Header': 'Value',
|
||||
'X-Other-Header': 'Other Value'
|
||||
'X-Other-Header': 'Other Value',
|
||||
'User-Agent': 'Dart'
|
||||
}).then((val) => val), completion(parse(equals({
|
||||
'method': 'GET',
|
||||
'path': '/',
|
||||
'headers': {
|
||||
'content-length': ['0'],
|
||||
'accept-encoding': ['gzip'],
|
||||
'user-agent': ['Dart'],
|
||||
'x-random-header': ['Value'],
|
||||
'x-other-header': ['Other Value']
|
||||
},
|
||||
|
@ -196,7 +210,8 @@ main() {
|
|||
expect(startServer().then((_) {
|
||||
var future = http.readBytes(serverUrl, headers: {
|
||||
'X-Random-Header': 'Value',
|
||||
'X-Other-Header': 'Other Value'
|
||||
'X-Other-Header': 'Other Value',
|
||||
'User-Agent': 'Dart'
|
||||
}).then((bytes) => new String.fromCharCodes(bytes));
|
||||
|
||||
expect(future, completion(parse(equals({
|
||||
|
@ -205,6 +220,7 @@ main() {
|
|||
'headers': {
|
||||
'content-length': ['0'],
|
||||
'accept-encoding': ['gzip'],
|
||||
'user-agent': ['Dart'],
|
||||
'x-random-header': ['Value'],
|
||||
'x-other-header': ['Other Value']
|
||||
},
|
||||
|
|
|
@ -18,6 +18,7 @@ void main() {
|
|||
|
||||
var request = new http.Request('POST', serverUrl);
|
||||
request.body = "hello";
|
||||
request.headers['User-Agent'] = 'Dart';
|
||||
|
||||
expect(request.send().then((response) {
|
||||
expect(response.statusCode, equals(200));
|
||||
|
@ -28,6 +29,7 @@ void main() {
|
|||
'headers': {
|
||||
'content-type': ['text/plain; charset=utf-8'],
|
||||
'accept-encoding': ['gzip'],
|
||||
'user-agent': ['Dart'],
|
||||
'content-length': ['5']
|
||||
},
|
||||
'body': 'hello'
|
||||
|
|
|
@ -58,5 +58,12 @@ void FUNCTION_NAME(Common_IsBuiltinList)(Dart_NativeArguments args) {
|
|||
Dart_ExitScope();
|
||||
}
|
||||
|
||||
|
||||
void FUNCTION_NAME(Common_GetVersion)(Dart_NativeArguments args) {
|
||||
Dart_EnterScope();
|
||||
Dart_SetReturnValue(args, Dart_NewStringFromCString(Dart_VersionString()));
|
||||
Dart_ExitScope();
|
||||
}
|
||||
|
||||
} // namespace bin
|
||||
} // namespace dart
|
||||
|
|
|
@ -11,3 +11,7 @@ patch class _IOCrypto {
|
|||
/* patch */ static Uint8List getRandomBytes(int count)
|
||||
native "Crypto_GetRandomBytes";
|
||||
}
|
||||
|
||||
patch class _OptionsImpl {
|
||||
/* patch */ String get version native "Common_GetVersion";
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ namespace bin {
|
|||
// builtin_natives.cc instead.
|
||||
#define IO_NATIVE_LIST(V) \
|
||||
V(Common_IsBuiltinList, 1) \
|
||||
V(Common_GetVersion, 1) \
|
||||
V(Crypto_GetRandomBytes, 1) \
|
||||
V(EventHandler_Start, 1) \
|
||||
V(EventHandler_SendData, 4) \
|
||||
|
|
|
@ -303,3 +303,9 @@ patch class _Filter {
|
|||
throw new UnsupportedError("newZLibInflateFilter");
|
||||
}
|
||||
}
|
||||
|
||||
patch class _OptionsImpl {
|
||||
patch String get version {
|
||||
throw new UnsupportedError("_OptionsImpl.version");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -171,6 +171,16 @@ abstract class HttpServer implements Stream<HttpRequest> {
|
|||
* current connections handled by the server.
|
||||
*/
|
||||
HttpConnectionsInfo connectionsInfo();
|
||||
|
||||
/**
|
||||
* Set and get the default value of the `Server` header for all responses
|
||||
* generated by this [HttpServer]. The default value is
|
||||
* `Dart/<version> (dart:io)`.
|
||||
*
|
||||
* If the serverHeader is set to `null`, no default `Server` header will be
|
||||
* added to each response.
|
||||
*/
|
||||
String serverHeader;
|
||||
}
|
||||
|
||||
|
||||
|
@ -879,6 +889,16 @@ abstract class HttpClient {
|
|||
*/
|
||||
Duration idleTimeout;
|
||||
|
||||
/**
|
||||
* Set and get the default value of the `User-Agent` header for all requests
|
||||
* generated by this [HttpClient]. The default value is
|
||||
* `Dart/<version> (dart:io)`.
|
||||
*
|
||||
* If the userAgent is set to `null`, no default `User-Agent` header will be
|
||||
* added to each request.
|
||||
*/
|
||||
String userAgent;
|
||||
|
||||
factory HttpClient() => new _HttpClient();
|
||||
|
||||
/**
|
||||
|
|
|
@ -676,8 +676,11 @@ class _HttpResponse extends _HttpOutboundMessage<HttpResponse>
|
|||
_HttpRequest _httpRequest;
|
||||
|
||||
_HttpResponse(String protocolVersion,
|
||||
_HttpOutgoing _outgoing)
|
||||
: super(protocolVersion, _outgoing);
|
||||
_HttpOutgoing _outgoing,
|
||||
String serverHeader)
|
||||
: super(protocolVersion, _outgoing) {
|
||||
if (serverHeader != null) headers.set('Server', serverHeader);
|
||||
}
|
||||
|
||||
List<Cookie> get cookies {
|
||||
if (_cookies == null) _cookies = new List<Cookie>();
|
||||
|
@ -1149,6 +1152,9 @@ class _HttpClientConnection {
|
|||
request.headers.host = uri.host;
|
||||
request.headers.port = port;
|
||||
request.headers.set(HttpHeaders.ACCEPT_ENCODING, "gzip");
|
||||
if (_httpClient.userAgent != null) {
|
||||
request.headers.set('User-Agent', _httpClient.userAgent);
|
||||
}
|
||||
if (proxy.isAuthenticated) {
|
||||
// If the proxy configuration contains user information use that
|
||||
// for proxy basic authorization.
|
||||
|
@ -1346,6 +1352,8 @@ class _HttpClient implements HttpClient {
|
|||
|
||||
Duration get idleTimeout => _idleTimeout;
|
||||
|
||||
String userAgent = _getHttpVersion();
|
||||
|
||||
void set idleTimeout(Duration timeout) {
|
||||
_idleTimeout = timeout;
|
||||
_idleConnections.values.forEach(
|
||||
|
@ -1356,6 +1364,7 @@ class _HttpClient implements HttpClient {
|
|||
}));
|
||||
}
|
||||
|
||||
|
||||
Future<HttpClientRequest> open(String method,
|
||||
String host,
|
||||
int port,
|
||||
|
@ -1755,7 +1764,8 @@ class _HttpConnection {
|
|||
_state = _ACTIVE;
|
||||
var outgoing = new _HttpOutgoing(_socket);
|
||||
var response = new _HttpResponse(incoming.headers.protocolVersion,
|
||||
outgoing);
|
||||
outgoing,
|
||||
_httpServer.serverHeader);
|
||||
var request = new _HttpRequest(response, incoming, _httpServer, this);
|
||||
_streamFuture = outgoing.done
|
||||
.then((_) {
|
||||
|
@ -1819,6 +1829,7 @@ class _HttpConnection {
|
|||
|
||||
// HTTP server waiting for socket connections.
|
||||
class _HttpServer extends Stream<HttpRequest> implements HttpServer {
|
||||
String serverHeader = _getHttpVersion();
|
||||
|
||||
static Future<HttpServer> bind(address, int port, int backlog) {
|
||||
return ServerSocket.bind(address, port, backlog: backlog).then((socket) {
|
||||
|
@ -2349,3 +2360,11 @@ class _RedirectInfo implements RedirectInfo {
|
|||
final String method;
|
||||
final Uri location;
|
||||
}
|
||||
|
||||
String _getHttpVersion() {
|
||||
var version = new Options().version;
|
||||
// Only include major and minor version numbers.
|
||||
int index = version.indexOf('.', version.indexOf('.') + 1);
|
||||
version = version.substring(0, index);
|
||||
return 'Dart/$version (dart:io)';
|
||||
}
|
||||
|
|
|
@ -41,6 +41,12 @@ abstract class Options {
|
|||
* string is returned.
|
||||
*/
|
||||
String get script;
|
||||
|
||||
|
||||
/**
|
||||
* Returns the version of the current dart runtime.
|
||||
*/
|
||||
String get version;
|
||||
}
|
||||
|
||||
class _OptionsImpl implements Options {
|
||||
|
@ -60,6 +66,8 @@ class _OptionsImpl implements Options {
|
|||
return _nativeScript;
|
||||
}
|
||||
|
||||
String get version;
|
||||
|
||||
List<String> _arguments = null;
|
||||
|
||||
// This arguments singleton is written to by the embedder if applicable.
|
||||
|
|
|
@ -13,6 +13,7 @@ import "dart:isolate";
|
|||
|
||||
void testServerDetachSocket() {
|
||||
HttpServer.bind("127.0.0.1", 0).then((server) {
|
||||
server.serverHeader = null;
|
||||
server.listen((request) {
|
||||
var response = request.response;
|
||||
response.contentLength = 0;
|
||||
|
@ -89,6 +90,7 @@ void testClientDetachSocket() {
|
|||
});
|
||||
|
||||
var client = new HttpClient();
|
||||
client.userAgent = null;
|
||||
client.get("127.0.0.1", server.port, "/")
|
||||
.then((request) => request.close())
|
||||
.then((response) {
|
||||
|
|
Loading…
Reference in a new issue