132 lines
3.6 KiB
Plaintext
132 lines
3.6 KiB
Plaintext
import "pkg:/components/WebServer/WebSocket/WebSocketOpCode.bs"
|
|
|
|
namespace Web
|
|
|
|
class WebSocketFrame
|
|
public fin
|
|
public opcode
|
|
public is_masked
|
|
public payload_size
|
|
public mask
|
|
public payload
|
|
|
|
private index
|
|
private length
|
|
|
|
function new()
|
|
m.payload = CreateObject("roByteArray")
|
|
end function
|
|
|
|
function ReadFromBuffer(buffer as object, index as integer, length as integer) as boolean
|
|
m.buffer = buffer
|
|
m.index = index
|
|
m.length = length
|
|
|
|
firstByte = m.ReadByte()
|
|
m.fin = (firstByte and &h80) <> 0
|
|
m.opcode = firstByte and &hf
|
|
|
|
if not m.fin
|
|
return false
|
|
end if
|
|
|
|
if m.opcode <> Web.WebSocketOpCode.TEXT
|
|
return false
|
|
end if
|
|
|
|
secondByte = m.ReadByte()
|
|
m.is_masked = (secondByte and &h80) <> 0
|
|
if not m.is_masked
|
|
return false
|
|
end if
|
|
|
|
if not m.ReadPayloadSize(secondByte)
|
|
return false
|
|
end if
|
|
|
|
m.mask = [m.ReadByte(), m.ReadByte(), m.ReadByte(), m.ReadByte()]
|
|
|
|
m.payload.Clear()
|
|
for i = 0 to m.payload_size - 1
|
|
m.payload[i] = m.Xor(m.ReadByte(), m.mask[i mod 4])
|
|
end for
|
|
|
|
return true
|
|
end function
|
|
|
|
function ReadPayloadSize(secondByte as integer) as boolean
|
|
payloadSize = secondByte and &h7F
|
|
if payloadSize < 126
|
|
m.payload_size = payloadSize
|
|
return true
|
|
else if payloadSize = 126
|
|
payloadSize = (m.ReadByte() << 8) or m.ReadByte()
|
|
m.payload_size = payloadSize
|
|
return true
|
|
else
|
|
'TODO: Read size in 8 bytes
|
|
return false
|
|
end if
|
|
end function
|
|
|
|
function WritePayloadSize(payloadSize as integer)
|
|
if payloadSize < 126
|
|
m.WriteByte(payloadSize)
|
|
else if payloadSize < 65536
|
|
m.WriteByte(126)
|
|
m.WriteByte(payloadSize >> 8)
|
|
m.WriteByte(payloadSize and &hFF)
|
|
else
|
|
m.WriteByte(127)
|
|
'TODO: Write size in 8 bytes
|
|
end if
|
|
end function
|
|
|
|
function WriteToBuffer(buffer as object, index as integer, length as integer) as integer
|
|
m.buffer = buffer
|
|
m.index = index
|
|
m.length = length
|
|
|
|
m.WriteByte(m.fin or m.opcode)
|
|
m.WritePayloadSize(m.payload_size)
|
|
|
|
for i = 0 to m.payload_size - 1
|
|
m.buffer[m.index] = m.payload[i]
|
|
m.index++
|
|
end for
|
|
|
|
return m.index - index
|
|
end function
|
|
|
|
function GetPayloadText() as string
|
|
return m.payload.ToAsciiString()
|
|
end function
|
|
|
|
function SetPayloadText(text as string)
|
|
m.fin = &h80
|
|
m.opcode = Web.WebSocketOpCode.TEXT
|
|
m.is_masked = false
|
|
m.mask = invalid
|
|
m.payload.FromAsciiString(text)
|
|
m.payload_size = m.payload.Count()
|
|
end function
|
|
|
|
function ReadByte() as integer
|
|
byte = m.buffer[m.index]
|
|
m.index++
|
|
return byte
|
|
end function
|
|
|
|
function WriteByte(byte as integer) as integer
|
|
m.buffer[m.index] = byte
|
|
m.index++
|
|
return byte
|
|
end function
|
|
|
|
function Xor(a as integer, b as integer) as integer
|
|
return (a or b) and not (a and b)
|
|
end function
|
|
end class
|
|
|
|
end namespace
|