Skip to content

Commit

Permalink
Support SSLContext
Browse files Browse the repository at this point in the history
  • Loading branch information
EricForgy committed Feb 13, 2018
1 parent cfab8e5 commit 0e7ecc8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
29 changes: 7 additions & 22 deletions src/WebSockets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,18 @@ end

""" Read a frame: turn bytes from the websocket into a WebSocketFragment."""
function read_frame(io::IO)
a = read(io,UInt8)
ab = read(io,2)
a = ab[1]
fin = a & 0b1000_0000 >>> 7 # If fin, then is final fragment
rsv1 = a & 0b0100_0000 # If not 0, fail.
rsv2 = a & 0b0010_0000 # If not 0, fail.
rsv3 = a & 0b0001_0000 # If not 0, fail.
opcode = a & 0b0000_1111 # If not known code, fail.
# TODO: add validation somewhere to ensure rsv, opcode, mask, etc are valid.

b = read(io,UInt8)
b = ab[2]
mask = b & 0b1000_0000 >>> 7
has_mask = mask != 0
hasmask = mask != 0

# if mask != 1
# error("WebSocket reader cannot handle incoming messages without mask. " *
Expand All @@ -356,26 +357,10 @@ function read_frame(io::IO)
payload_len = ntoh(read(io,UInt64)) # 8 bytes
end

if has_mask
maskkey = Array{UInt8,1}(4)
for i in 1:4
maskkey[i] = read(io,UInt8)
end
else
maskkey = UInt8[]
end
maskkey = hasmask ? read(io,4) : UInt8[]

data = Array{UInt8,1}(payload_len)
if has_mask
for i in 1:payload_len
d = read(io, UInt8)
data[i] = xor(d , maskkey[mod(i - 1, 4) + 1])
end
else
for i in 1:payload_len
data[i] = read(io, UInt8)
end
end
data = read(io,Int(payload_len))
hasmask && mask!(data,maskkey)

return WebSocketFragment(fin,rsv1,rsv2,rsv3,opcode,mask,payload_len,maskkey,data)
end
Expand Down
21 changes: 14 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@ import WebSockets: CONNECTED, CLOSING, CLOSED

@testset "WebSockets" begin

for s in ["ws"] # ["ws", "wss"]
info("Testing $(s)...")
WebSockets.open("$s://echo.websocket.org") do ws
write(ws, "Foo")
@test String(read(ws)) == "Foo"
info("Testing ws...")
WebSockets.open("ws://echo.websocket.org") do ws
write(ws, "Foo")
@test String(read(ws)) == "Foo"

close(ws)
end
close(ws)
end
sleep(1)

info("Testing wss...")
WebSockets.open("wss://echo.websocket.org") do ws
write(ws, "Foo")
@test String(read(ws)) == "Foo"

close(ws)
end
sleep(1)

p = UInt16(8000)
@async HTTP.listen("127.0.0.1",p) do http
Expand Down

0 comments on commit 0e7ecc8

Please sign in to comment.