0001    // Server.swift
0002    //
0003    // The MIT License (MIT)
0004    //
0005    // Copyright (c) 2015 Zewo
0006    //
0007    // Permission is hereby granted, free of charge, to any person obtaining a copy
0008    // of this software and associated documentation files (the "Software"), to deal
0009    // in the Software without restriction, including without limitation the rights
0010    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0011    // copies of the Software, and to permit persons to whom the Software is
0012    // furnished to do so, subject to the following conditions:
0013    //
0014    // The above copyright notice and this permission notice shall be included in all
0015    // copies or substantial portions of the Software.
0016    //
0017    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0018    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0019    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0020    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0021    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0022    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0023    // SOFTWARE.
0024    
0025    @_exported import HTTP
0026    
0027    public struct Server: ResponderType, MiddlewareType, ChainType {
0028    	
0029    	public enum Error: ErrorType {
0030    		case NoResponse
0031    	}
0032    	
0033    	private let onConnect
Server.swift:36
		self.onConnect =  onConnect
: Socket throws -> Void 0034 0035 public init(onConnect: Socket throws -> Void) { 0036 self.onConnect = onConnect 0037 } 0038 0039 // MARK: - MiddlewareType 0040 0041 public func respond(request: Request, chain: ChainType) throws -> Response { 0042 guard request.isWebSocket && request.webSocketVersion == "13", let key = request.webSocketKey else { 0043 return try chain.proceed(request) 0044 } 0045 0046 guard let accept = Socket.accept(key) else { 0047 return Response(status: .InternalServerError) 0048 } 0049 0050 let headers: Headers = [ 0051 "Connection": "Upgrade", 0052 "Upgrade": "websocket", 0053 "Sec-WebSocket-Accept": accept 0054 ] 0055 0056 var _response: Response? 0057 let response = Response(status: .SwitchingProtocols, headers: headers) { _, stream in 0058 guard let response = _response else { 0059 throw Error.NoResponse 0060 } 0061 0062 let webSocket = Socket(stream: stream, mode: .Server, request: request, response: response) 0063 try self.onConnect(webSocket) 0064 try webSocket.loop() 0065 } 0066 _response = response 0067 0068 return response 0069 } 0070 0071 // MARK: - ResponderType 0072 0073 public func respond
Server.swift:41
	public func respond(request: Request, chain: ChainType) throws -> Response {
(request: Request) throws -> Response { 0074 return try respond(request, chain: self) 0075 } 0076 0077 // MARK: - ChainType 0078 0079 public func proceed(request: Request) throws -> Response { 0080 return Response(status: .BadRequest) 0081 } 0082 0083 } 0084 0085 public extension MessageType { 0086 0087 public var webSocketVersion: String? { 0088 return headers["Sec-Websocket-Version"] 0089 } 0090 0091 public var webSocketKey: String? { 0092 return headers["Sec-Websocket-Key"] 0093 } 0094 0095 public var webSocketAccept: String? { 0096 return headers["Sec-WebSocket-Accept"] 0097 } 0098 0099 public var isWebSocket: Bool { 0100 return connection?.lowercaseString == "upgrade" && upgrade?.lowercaseString == "websocket" 0101 } 0102 0103 } 0104