Skip to content

Commit

Permalink
modified: src/WebSockets.jl Now exits without calling handler when un…
Browse files Browse the repository at this point in the history
…accepted handshakes.
  • Loading branch information
hustf committed Aug 4, 2017
1 parent e3c4acb commit 8d23b9d
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/WebSockets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -428,25 +428,25 @@ end

"""
Responds to a WebSocket handshake request.
Checks for required headers; sends Response(400) if they're missing or bad.
Otherwise, transforms client key into accept value, and sends Reponse(101).
Checks for required headers and subprotocols; sends Response(400) if they're missing or bad. Otherwise, transforms client key into accept value, and sends Reponse(101).
Function returns true for accepted handshakes.
"""
function websocket_handshake(request,client)
if !haskey(request.headers, "Sec-WebSocket-Key")
Base.write(client.sock, Response(400))
return
return false
end
if get(request.headers, "Sec-WebSocket-Version", "13") != "13"
response = Response(400)
response.headers["Sec-WebSocket-Version"] = "13"
Base.write(client.sock, response)
return
return false
end

key = request.headers["Sec-WebSocket-Key"]
if length(decode(Base64,key)) != 16 # Key must be 16 bytes
Base.write(client.sock, Response(400))
return
return false
end
resp_key = generate_websocket_key(key)

Expand All @@ -460,11 +460,12 @@ function websocket_handshake(request,client)
response.headers["Sec-WebSocket-Protocol"] = request.headers["Sec-WebSocket-Protocol"]
else
Base.write(client.sock, Response(400))
return
return false
end
end

Base.write(client.sock, response)
return true
end

""" Implement the WebSocketInterface, for compatilibility with HttpServer."""
Expand All @@ -473,11 +474,19 @@ immutable WebSocketHandler <: HttpServer.WebSocketInterface
end

import HttpServer: handle, is_websocket_handshake
"""
Performs handshake. If successfull, establishes WebSocket type and calls
handler with the WebSocket and the original request. On exit from handler, closes websocket. No return value.
"""
function handle(handler::WebSocketHandler, req::Request, client::HttpServer.Client)
websocket_handshake(req, client)
websocket_handshake(req, client) || return
sock = WebSocket(client.id, client.sock)
handler.handle(req, sock)
isopen(sock) && close(sock)
if isopen(sock)
try
close(sock)
end
end
end
function is_websocket_handshake(handler::WebSocketHandler, req::Request)
is_get = req.method == "GET"
Expand Down

0 comments on commit 8d23b9d

Please sign in to comment.