Skip to content

Commit

Permalink
Only copy String bytes if masked (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Forgy authored and hustf committed Feb 21, 2018
1 parent 03380cd commit bafb175
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/WebSockets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ function write_fragment(io::IO, islast::Bool, opcode, hasmask::Bool, data::Vecto
else
error("Attempted to send too much data for one websocket fragment\n")
end
hasmask && write(io,mask!(data))
if hasmask
if opcode == OPCODE_TEXT
data = copy(data) # Avoid masking Strings bytes in place
end
write(io,mask!(data))
end
write(io, data)
end

Expand All @@ -173,7 +178,7 @@ end

""" Write text data; will be sent as one frame."""
function Base.write(ws::WebSocket,data::String)
locked_write(ws.socket, true, OPCODE_TEXT, !ws.server, copy(Vector{UInt8}(data))) # Remove this `copy` after v0.7!
locked_write(ws.socket, true, OPCODE_TEXT, !ws.server, Vector{UInt8}(data)) # Vector{UInt8}(String) will give a warning in v0.7.
end

""" Write binary data; will be sent as one frame."""
Expand Down

0 comments on commit bafb175

Please sign in to comment.