OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 import 'package:http_parser/http_parser.dart'; |
5 import 'package:shelf/shelf.dart'; | 6 import 'package:shelf/shelf.dart'; |
6 | 7 |
7 import 'src/web_socket_handler.dart'; | 8 import 'src/web_socket_handler.dart'; |
8 | 9 |
9 /// A typedef used to determine if a function takes two arguments or not. | 10 /// A typedef used to determine if a function takes two arguments or not. |
10 typedef _BinaryFunction(arg1, arg2); | 11 typedef _BinaryFunction(arg1, arg2); |
11 | 12 |
12 /// Creates a Shelf handler that upgrades HTTP requests to WebSocket | 13 /// Creates a Shelf handler that upgrades HTTP requests to WebSocket |
13 /// connections. | 14 /// connections. |
14 /// | 15 /// |
15 /// Only valid WebSocket upgrade requests are upgraded. If a request doesn't | 16 /// Only valid WebSocket upgrade requests are upgraded. If a request doesn't |
16 /// look like a WebSocket upgrade request, a 404 Not Found is returned; if a | 17 /// look like a WebSocket upgrade request, a 404 Not Found is returned; if a |
17 /// request looks like an upgrade request but is invalid, a 400 Bad Request is | 18 /// request looks like an upgrade request but is invalid, a 400 Bad Request is |
18 /// returned; and if a request is a valid upgrade request but has an origin that | 19 /// returned; and if a request is a valid upgrade request but has an origin that |
19 /// doesn't match [allowedOrigins] (see below), a 403 Forbidden is returned. | 20 /// doesn't match [allowedOrigins] (see below), a 403 Forbidden is returned. |
20 /// This means that this can be placed first in a [Cascade] and only upgrade | 21 /// This means that this can be placed first in a [Cascade] and only upgrade |
21 /// requests will be handled. | 22 /// requests will be handled. |
22 /// | 23 /// |
23 /// The [onConnection] must take a [CompatibleWebSocket] as its first argument. | 24 /// The [onConnection] must take a [WebSocketChannel] as its first argument. It |
24 /// It may also take a string, the [WebSocket subprotocol][], as its second | 25 /// may also take a string, the [WebSocket subprotocol][], as its second |
25 /// argument. The subprotocol is determined by looking at the client's | 26 /// argument. The subprotocol is determined by looking at the client's |
26 /// `Sec-WebSocket-Protocol` header and selecting the first entry that also | 27 /// `Sec-WebSocket-Protocol` header and selecting the first entry that also |
27 /// appears in [protocols]. If no subprotocols are shared between the client and | 28 /// appears in [protocols]. If no subprotocols are shared between the client and |
28 /// the server, `null` will be passed instead. Note that if [onConnection] takes | 29 /// the server, `null` will be passed instead. Note that if [onConnection] takes |
29 /// two arguments, [protocols] must be passed. | 30 /// two arguments, [protocols] must be passed. |
30 /// | 31 /// |
31 /// [WebSocket subprotocol]: https://tools.ietf.org/html/rfc6455#section-1.9 | 32 /// [WebSocket subprotocol]: https://tools.ietf.org/html/rfc6455#section-1.9 |
32 /// | 33 /// |
33 /// If [allowedOrigins] is passed, browser connections will only be accepted if | 34 /// If [allowedOrigins] is passed, browser connections will only be accepted if |
34 /// they're made by a script from one of the given origins. This ensures that | 35 /// they're made by a script from one of the given origins. This ensures that |
(...skipping 15 matching lines...) Expand all Loading... |
50 throw new ArgumentError("If protocols is non-null, onConnection must " | 51 throw new ArgumentError("If protocols is non-null, onConnection must " |
51 "take two arguments, the WebSocket and the protocol."); | 52 "take two arguments, the WebSocket and the protocol."); |
52 } | 53 } |
53 | 54 |
54 var innerOnConnection = onConnection; | 55 var innerOnConnection = onConnection; |
55 onConnection = (webSocket, _) => innerOnConnection(webSocket); | 56 onConnection = (webSocket, _) => innerOnConnection(webSocket); |
56 } | 57 } |
57 | 58 |
58 return new WebSocketHandler(onConnection, protocols, allowedOrigins).handle; | 59 return new WebSocketHandler(onConnection, protocols, allowedOrigins).handle; |
59 } | 60 } |
OLD | NEW |