Skip to content

Instantly share code, notes, and snippets.

@MagnificentPako
Created March 31, 2017 18:05
Show Gist options
  • Save MagnificentPako/665a6a7c1a647d8f53977be433b291ce to your computer and use it in GitHub Desktop.
Save MagnificentPako/665a6a7c1a647d8f53977be433b291ce to your computer and use it in GitHub Desktop.
fun ref received(conn: TCPConnection ref, data: Array[U8] iso): Bool =>
if(not _connected) then
_connected = true //"swallows" the first response, which is supposed to be
//a HTTP Upgrade response
else
_writing = false
let rb: Reader = Reader
rb.append(consume data)
var final: Bool = true
var opcode: Opcode = OPCLOSE
var datt: String = ""
try
var fin_op: U8 = rb.u8()
var mask_payloadlen: U8 = rb.u8()
final = if (((fin_op >> 7) and 0b00000001) == 1) then true else false end
opcode = OPTEXT
var use_mask: Bool = if (((mask_payloadlen >> 7) and 0b00000001) == 1) then true else false end
var payloadlen: U8 = (mask_payloadlen and 0b01111111)
var payload_type: U8 = if payloadlen == 0b01111111 then
1 else if payloadlen == 0b01111110 then
2 else
0 end end
var payload_size: U64 = match payload_type
| 0 => payloadlen.u64()
| 1 => rb.u16_be().u64()
| 2 => rb.u64_be().u64()
else 0 end
Debug("Payload size: " + payload_size.string())
//Debug(fin_op.string() + " " + mask_payloadlen.string())
var mask_key = if use_mask then rb.u32_be() else None end
datt = String.from_array(rb.block(payload_size.usize()))
Debug("...\n")
if(not final) then
_current_content = _current_content + datt
else
_current_content = _current_content + datt
_notify.received(conn, _current_content)
_current_content = ""
end
end
end
true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment