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

Unified Diff: pkg/shelf_web_socket/lib/shelf_web_socket.dart

Issue 297593003: Add a shelf_web_socket package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fixes Created 6 years, 7 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 | « pkg/shelf_web_socket/README.md ('k') | pkg/shelf_web_socket/lib/src/web_socket_handler.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/shelf_web_socket/lib/shelf_web_socket.dart
diff --git a/pkg/shelf_web_socket/lib/shelf_web_socket.dart b/pkg/shelf_web_socket/lib/shelf_web_socket.dart
new file mode 100644
index 0000000000000000000000000000000000000000..8aac366859ef1ce5eddb9878a557db27729d6774
--- /dev/null
+++ b/pkg/shelf_web_socket/lib/shelf_web_socket.dart
@@ -0,0 +1,61 @@
+// Copyright (c) 2014, 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_web_socket;
+
+import 'package:shelf/shelf.dart';
+
+import 'src/web_socket_handler.dart';
+
+/// A typedef used to determine if a function takes two arguments or not.
+typedef _BinaryFunction(arg1, arg2);
+
+/// Creates a Shelf handler that upgrades HTTP requests to WebSocket
+/// connections.
+///
+/// Only valid WebSocket upgrade requests are upgraded. If a request doesn't
+/// look like a WebSocket upgrade request, a 404 Not Found is returned; if a
+/// request looks like an upgrade request but is invalid, a 400 Bad Request is
+/// returned; and if a request is a valid upgrade request but has an origin that
+/// doesn't match [allowedOrigins] (see below), a 403 Forbidden is returned.
+/// This means that this can be placed first in a [Cascade] and only upgrade
+/// requests will be handled.
+///
+/// The [onConnection] must take a [CompatibleWebSocket] as its first argument.
+/// It may also take a string, the [WebSocket subprotocol][], as its second
+/// argument. The subprotocol is determined by looking at the client's
+/// `Sec-WebSocket-Protocol` header and selecting the first entry that also
+/// appears in [protocols]. If no subprotocols are shared between the client and
+/// the server, `null` will be passed instead. Note that if [onConnection] takes
+/// two arguments, [protocols] must be passed.
+///
+/// [WebSocket subprotocol]: https://tools.ietf.org/html/rfc6455#section-1.9
+///
+/// If [allowedOrigins] is passed, browser connections will only be accepted if
+/// they're made by a script from one of the given origins. This ensures that
+/// malicious scripts running in the browser are unable to fake a WebSocket
+/// handshake. Note that non-browser programs can still make connections freely.
+/// See also the WebSocket spec's discussion of [origin considerations][].
+///
+/// [origin considerations]: https://tools.ietf.org/html/rfc6455#section-10.2
+Handler webSocketHandler(Function onConnection, {Iterable<String> protocols,
+ Iterable<String> allowedOrigins}) {
+ if (protocols != null) protocols = protocols.toSet();
+ if (allowedOrigins != null) {
+ allowedOrigins = allowedOrigins
+ .map((origin) => origin.toLowerCase()).toSet();
+ }
+
+ if (onConnection is! _BinaryFunction) {
+ if (protocols != null) {
+ throw new ArgumentError("If protocols is non-null, onConnection must "
+ "take two arguments, the WebSocket and the protocol.");
+ }
+
+ var innerOnConnection = onConnection;
+ onConnection = (webSocket, _) => innerOnConnection(webSocket);
+ }
+
+ return new WebSocketHandler(onConnection, protocols, allowedOrigins).handle;
+}
« no previous file with comments | « pkg/shelf_web_socket/README.md ('k') | pkg/shelf_web_socket/lib/src/web_socket_handler.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698