Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Unified Diff: lib/src/io_server.dart

Issue 1411553006: Add a Server interface. (Closed) Base URL: git@github.com:dart-lang/shelf@master
Patch Set: Code review changes Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/shelf_io.dart ('k') | lib/src/server.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/io_server.dart
diff --git a/lib/src/io_server.dart b/lib/src/io_server.dart
new file mode 100644
index 0000000000000000000000000000000000000000..854e3417bfaa4594dfc14960b3ee098b4acd5512
--- /dev/null
+++ b/lib/src/io_server.dart
@@ -0,0 +1,60 @@
+// Copyright (c) 2015, 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.
+
+library shelf.io_server;
+
+import 'dart:async';
+
+import 'dart:io';
+
+import '../shelf_io.dart';
+import 'handler.dart';
+import 'server.dart';
+
+/// A [Server] backed by a `dart:io` [HttpServer].
+class IOServer implements Server {
+ /// The underlying [HttpServer].
+ final HttpServer server;
+
+ /// Whether [mount] has been called.
+ bool _mounted = false;
+
+ Uri get url {
+ if (server.address.isLoopback) {
+ return new Uri(scheme: "http", host: "localhost", port: server.port);
+ }
+
+ // IPv6 addresses in URLs need to be enclosed in square brackets to avoid
+ // URL ambiguity with the ":" in the address.
+ if (server.address.type == InternetAddressType.IP_V6) {
+ return new Uri(
+ scheme: "http",
+ host: "[${server.address.address}]",
+ port: server.port);
+ }
+
+ return new Uri(
+ scheme: "http", host: server.address.address, port: server.port);
+ }
+
+ /// Calls [HttpServer.bind] and wraps the result in an [IOServer].
+ static Future<IOServer> bind(address, int port, {int backlog}) async {
+ backlog ??= 0;
+ var server = await HttpServer.bind(address, port, backlog: backlog);
+ return new IOServer(server);
+ }
+
+ IOServer(this.server);
+
+ void mount(Handler handler) {
+ if (_mounted) {
+ throw new StateError("Can't mount two handlers for the same server.");
+ }
+ _mounted = true;
+
+ serveRequests(server, handler);
+ }
+
+ Future close() => server.close();
+}
« no previous file with comments | « lib/shelf_io.dart ('k') | lib/src/server.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698