Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, 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 const int _OUTGOING_BUFFER_SIZE = 8 * 1024; | 7 const int _OUTGOING_BUFFER_SIZE = 8 * 1024; |
| 8 | 8 |
| 9 class _HttpIncoming extends Stream<List<int>> { | 9 class _HttpIncoming extends Stream<List<int>> { |
| 10 final int _transferLength; | 10 final int _transferLength; |
| (...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 496 int _statusCode = 200; | 496 int _statusCode = 200; |
| 497 String _reasonPhrase; | 497 String _reasonPhrase; |
| 498 List<Cookie> _cookies; | 498 List<Cookie> _cookies; |
| 499 _HttpRequest _httpRequest; | 499 _HttpRequest _httpRequest; |
| 500 Duration _deadline; | 500 Duration _deadline; |
| 501 Timer _deadlineTimer; | 501 Timer _deadlineTimer; |
| 502 | 502 |
| 503 _HttpResponse(Uri uri, | 503 _HttpResponse(Uri uri, |
| 504 String protocolVersion, | 504 String protocolVersion, |
| 505 _HttpOutgoing outgoing, | 505 _HttpOutgoing outgoing, |
| 506 HttpHeaders defaultHeaders, | |
| 506 String serverHeader) | 507 String serverHeader) |
| 507 : super(uri, protocolVersion, outgoing) { | 508 : super(uri, protocolVersion, outgoing) { |
| 508 if (serverHeader != null) headers._add('server', serverHeader); | 509 defaultHeaders.forEach((name, value) => headers.add(name, value)); |
|
Anders Johnsen
2014/08/08 06:15:45
I'm curious to the overhead of this. Maybe we shou
Søren Gjesse
2014/08/11 14:10:09
Don't know. Pass the default header values to the
| |
| 510 if (serverHeader != null) headers.set('server', serverHeader); | |
| 509 } | 511 } |
| 510 | 512 |
| 511 bool get _isConnectionClosed => _httpRequest._httpConnection._isClosing; | 513 bool get _isConnectionClosed => _httpRequest._httpConnection._isClosing; |
| 512 | 514 |
| 513 List<Cookie> get cookies { | 515 List<Cookie> get cookies { |
| 514 if (_cookies == null) _cookies = new List<Cookie>(); | 516 if (_cookies == null) _cookies = new List<Cookie>(); |
| 515 return _cookies; | 517 return _cookies; |
| 516 } | 518 } |
| 517 | 519 |
| 518 int get statusCode => _statusCode; | 520 int get statusCode => _statusCode; |
| (...skipping 1511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2030 if (closing) destroy(); | 2032 if (closing) destroy(); |
| 2031 }); | 2033 }); |
| 2032 // Only handle one incoming request at the time. Keep the | 2034 // Only handle one incoming request at the time. Keep the |
| 2033 // stream paused until the request has been send. | 2035 // stream paused until the request has been send. |
| 2034 _subscription.pause(); | 2036 _subscription.pause(); |
| 2035 _state = _ACTIVE; | 2037 _state = _ACTIVE; |
| 2036 var outgoing = new _HttpOutgoing(_socket); | 2038 var outgoing = new _HttpOutgoing(_socket); |
| 2037 var response = new _HttpResponse(incoming.uri, | 2039 var response = new _HttpResponse(incoming.uri, |
| 2038 incoming.headers.protocolVersion, | 2040 incoming.headers.protocolVersion, |
| 2039 outgoing, | 2041 outgoing, |
| 2042 _httpServer.defaultResponseHeaders, | |
| 2040 _httpServer.serverHeader); | 2043 _httpServer.serverHeader); |
| 2041 var request = new _HttpRequest(response, incoming, _httpServer, this); | 2044 var request = new _HttpRequest(response, incoming, _httpServer, this); |
| 2042 _streamFuture = outgoing.done | 2045 _streamFuture = outgoing.done |
| 2043 .then((_) { | 2046 .then((_) { |
| 2044 response.deadline = null; | 2047 response.deadline = null; |
| 2045 if (_state == _DETACHED) return; | 2048 if (_state == _DETACHED) return; |
| 2046 if (response.persistentConnection && | 2049 if (response.persistentConnection && |
| 2047 request.persistentConnection && | 2050 request.persistentConnection && |
| 2048 incoming.fullBodyRead && | 2051 incoming.fullBodyRead && |
| 2049 !_httpParser.upgrade && | 2052 !_httpParser.upgrade && |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2148 | 2151 |
| 2149 | 2152 |
| 2150 // HTTP server waiting for socket connections. | 2153 // HTTP server waiting for socket connections. |
| 2151 class _HttpServer | 2154 class _HttpServer |
| 2152 extends Stream<HttpRequest> with _ServiceObject | 2155 extends Stream<HttpRequest> with _ServiceObject |
| 2153 implements HttpServer { | 2156 implements HttpServer { |
| 2154 // Use default Map so we keep order. | 2157 // Use default Map so we keep order. |
| 2155 static Map<int, _HttpServer> _servers = new Map<int, _HttpServer>(); | 2158 static Map<int, _HttpServer> _servers = new Map<int, _HttpServer>(); |
| 2156 | 2159 |
| 2157 String serverHeader; | 2160 String serverHeader; |
| 2161 HttpHeaders defaultResponseHeaders; | |
| 2158 | 2162 |
| 2159 Duration _idleTimeout; | 2163 Duration _idleTimeout; |
| 2160 Timer _idleTimer; | 2164 Timer _idleTimer; |
| 2161 | 2165 |
| 2162 static Future<HttpServer> bind(address, int port, int backlog) { | 2166 static Future<HttpServer> bind(address, int port, int backlog) { |
| 2163 return ServerSocket.bind(address, port, backlog: backlog).then((socket) { | 2167 return ServerSocket.bind(address, port, backlog: backlog).then((socket) { |
| 2164 return new _HttpServer._(socket, true); | 2168 return new _HttpServer._(socket, true); |
| 2165 }); | 2169 }); |
| 2166 } | 2170 } |
| 2167 | 2171 |
| 2168 static Future<HttpServer> bindSecure(address, | 2172 static Future<HttpServer> bindSecure(address, |
| 2169 int port, | 2173 int port, |
| 2170 int backlog, | 2174 int backlog, |
| 2171 String certificate_name, | 2175 String certificate_name, |
| 2172 bool requestClientCertificate) { | 2176 bool requestClientCertificate) { |
| 2173 return SecureServerSocket.bind( | 2177 return SecureServerSocket.bind( |
| 2174 address, | 2178 address, |
| 2175 port, | 2179 port, |
| 2176 certificate_name, | 2180 certificate_name, |
| 2177 backlog: backlog, | 2181 backlog: backlog, |
| 2178 requestClientCertificate: requestClientCertificate) | 2182 requestClientCertificate: requestClientCertificate) |
| 2179 .then((socket) { | 2183 .then((socket) { |
| 2180 return new _HttpServer._(socket, true); | 2184 return new _HttpServer._(socket, true); |
| 2181 }); | 2185 }); |
| 2182 } | 2186 } |
| 2183 | 2187 |
| 2184 _HttpServer._(this._serverSocket, this._closeServer) { | 2188 _HttpServer._(this._serverSocket, this._closeServer) { |
| 2189 _initDefaultResponseHeaders(); | |
| 2185 _controller = new StreamController<HttpRequest>(sync: true, | 2190 _controller = new StreamController<HttpRequest>(sync: true, |
| 2186 onCancel: close); | 2191 onCancel: close); |
| 2187 idleTimeout = const Duration(seconds: 120); | 2192 idleTimeout = const Duration(seconds: 120); |
| 2188 _servers[_serviceId] = this; | 2193 _servers[_serviceId] = this; |
| 2189 _serverSocket._owner = this; | 2194 _serverSocket._owner = this; |
| 2190 } | 2195 } |
| 2191 | 2196 |
| 2192 _HttpServer.listenOn(this._serverSocket) : _closeServer = false { | 2197 _HttpServer.listenOn(this._serverSocket) : _closeServer = false { |
| 2198 _initDefaultResponseHeaders(); | |
| 2193 _controller = new StreamController<HttpRequest>(sync: true, | 2199 _controller = new StreamController<HttpRequest>(sync: true, |
| 2194 onCancel: close); | 2200 onCancel: close); |
| 2195 idleTimeout = const Duration(seconds: 120); | 2201 idleTimeout = const Duration(seconds: 120); |
| 2196 _servers[_serviceId] = this; | 2202 _servers[_serviceId] = this; |
| 2197 try { _serverSocket._owner = this; } catch (_) {} | 2203 try { _serverSocket._owner = this; } catch (_) {} |
| 2198 } | 2204 } |
| 2199 | 2205 |
| 2206 _initDefaultResponseHeaders() { | |
| 2207 defaultResponseHeaders = new _HttpHeaders('1.1'); | |
| 2208 defaultResponseHeaders.contentType = ContentType.TEXT; | |
| 2209 defaultResponseHeaders.set('X-Frame-Options', 'SAMEORIGIN'); | |
| 2210 defaultResponseHeaders.set('X-Content-Type-Options', 'nosniff'); | |
| 2211 defaultResponseHeaders.set('X-XSS-Protection', '1; mode=block'); | |
| 2212 } | |
| 2213 | |
| 2200 Duration get idleTimeout => _idleTimeout; | 2214 Duration get idleTimeout => _idleTimeout; |
| 2201 | 2215 |
| 2202 void set idleTimeout(Duration duration) { | 2216 void set idleTimeout(Duration duration) { |
| 2203 if (_idleTimer != null) { | 2217 if (_idleTimer != null) { |
| 2204 _idleTimer.cancel(); | 2218 _idleTimer.cancel(); |
| 2205 _idleTimer = null; | 2219 _idleTimer = null; |
| 2206 } | 2220 } |
| 2207 _idleTimeout = duration; | 2221 _idleTimeout = duration; |
| 2208 if (_idleTimeout != null) { | 2222 if (_idleTimeout != null) { |
| 2209 _idleTimer = new Timer.periodic(_idleTimeout, (_) { | 2223 _idleTimer = new Timer.periodic(_idleTimeout, (_) { |
| (...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2803 const _RedirectInfo(this.statusCode, this.method, this.location); | 2817 const _RedirectInfo(this.statusCode, this.method, this.location); |
| 2804 } | 2818 } |
| 2805 | 2819 |
| 2806 String _getHttpVersion() { | 2820 String _getHttpVersion() { |
| 2807 var version = Platform.version; | 2821 var version = Platform.version; |
| 2808 // Only include major and minor version numbers. | 2822 // Only include major and minor version numbers. |
| 2809 int index = version.indexOf('.', version.indexOf('.') + 1); | 2823 int index = version.indexOf('.', version.indexOf('.') + 1); |
| 2810 version = version.substring(0, index); | 2824 version = version.substring(0, index); |
| 2811 return 'Dart/$version (dart:io)'; | 2825 return 'Dart/$version (dart:io)'; |
| 2812 } | 2826 } |
| OLD | NEW |