test-cleancode: add the rest of vlib/x/websocket/
parent
b65353794c
commit
b7a5dbf7b4
|
@ -69,8 +69,7 @@ const (
|
||||||
'vlib/strings/',
|
'vlib/strings/',
|
||||||
'vlib/time/',
|
'vlib/time/',
|
||||||
'vlib/vweb/',
|
'vlib/vweb/',
|
||||||
'vlib/x/websocket/websocket_server.v',
|
'vlib/x/websocket/',
|
||||||
'vlib/x/websocket/websocket_client.v',
|
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -30,14 +30,14 @@ fn (mut ws Client) handshake() ? {
|
||||||
ws.debug_log('sending handshake: $handshake')
|
ws.debug_log('sending handshake: $handshake')
|
||||||
ws.socket_write(handshake_bytes) ?
|
ws.socket_write(handshake_bytes) ?
|
||||||
ws.read_handshake(seckey) ?
|
ws.read_handshake(seckey) ?
|
||||||
unsafe {handshake_bytes.free()}
|
unsafe { handshake_bytes.free() }
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle_server_handshake manages websocket server handshake process
|
// handle_server_handshake manages websocket server handshake process
|
||||||
fn (mut s Server) handle_server_handshake(mut c Client) ?(string, &ServerClient) {
|
fn (mut s Server) handle_server_handshake(mut c Client) ?(string, &ServerClient) {
|
||||||
msg := c.read_handshake_str() ?
|
msg := c.read_handshake_str() ?
|
||||||
handshake_response, client := s.parse_client_handshake(msg, mut c) ?
|
handshake_response, client := s.parse_client_handshake(msg, mut c) ?
|
||||||
unsafe {msg.free()}
|
unsafe { msg.free() }
|
||||||
return handshake_response, client
|
return handshake_response, client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,7 +83,7 @@ fn (mut s Server) parse_client_handshake(client_handshake string, mut c Client)
|
||||||
// we ignore other headers like protocol for now
|
// we ignore other headers like protocol for now
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsafe {keys.free()}
|
unsafe { keys.free() }
|
||||||
}
|
}
|
||||||
if flags.len < 3 {
|
if flags.len < 3 {
|
||||||
return error_with_code('invalid client handshake, $client_handshake', 4)
|
return error_with_code('invalid client handshake, $client_handshake', 4)
|
||||||
|
@ -130,7 +130,7 @@ fn (mut ws Client) read_handshake_str() ?string {
|
||||||
fn (mut ws Client) read_handshake(seckey string) ? {
|
fn (mut ws Client) read_handshake(seckey string) ? {
|
||||||
mut msg := ws.read_handshake_str() ?
|
mut msg := ws.read_handshake_str() ?
|
||||||
ws.check_handshake_response(msg, seckey) ?
|
ws.check_handshake_response(msg, seckey) ?
|
||||||
unsafe {msg.free()}
|
unsafe { msg.free() }
|
||||||
}
|
}
|
||||||
|
|
||||||
// check_handshake_response checks the response from handshake and returns
|
// check_handshake_response checks the response from handshake and returns
|
||||||
|
@ -164,13 +164,13 @@ fn (mut ws Client) check_handshake_response(handshake_response string, seckey st
|
||||||
7)
|
7)
|
||||||
}
|
}
|
||||||
ws.flags << .has_accept
|
ws.flags << .has_accept
|
||||||
unsafe {challenge.free()}
|
unsafe { challenge.free() }
|
||||||
}
|
}
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
unsafe {keys.free()}
|
unsafe { keys.free() }
|
||||||
}
|
}
|
||||||
unsafe {lines.free()}
|
unsafe { lines.free() }
|
||||||
if ws.flags.len < 3 {
|
if ws.flags.len < 3 {
|
||||||
ws.close(1002, 'invalid websocket HTTP headers') ?
|
ws.close(1002, 'invalid websocket HTTP headers') ?
|
||||||
return error_with_code('invalid websocket HTTP headers', 8)
|
return error_with_code('invalid websocket HTTP headers', 8)
|
||||||
|
|
|
@ -18,8 +18,10 @@ struct Fragment {
|
||||||
// Frame represents a data frame header
|
// Frame represents a data frame header
|
||||||
struct Frame {
|
struct Frame {
|
||||||
mut:
|
mut:
|
||||||
header_len int = 2 // length of the websocket header part
|
// length of the websocket header part
|
||||||
frame_size int = 2 // size of total frame
|
header_len int = 2
|
||||||
|
// size of total frame
|
||||||
|
frame_size int = 2
|
||||||
fin bool // true if final fragment of message
|
fin bool // true if final fragment of message
|
||||||
rsv1 bool // reserved for future use in websocket RFC
|
rsv1 bool // reserved for future use in websocket RFC
|
||||||
rsv2 bool // reserved for future use in websocket RFC
|
rsv2 bool // reserved for future use in websocket RFC
|
||||||
|
@ -129,7 +131,7 @@ pub fn (mut ws Client) read_next_message() ?Message {
|
||||||
opcode: OPCode(frame.opcode)
|
opcode: OPCode(frame.opcode)
|
||||||
payload: frame_payload.clone()
|
payload: frame_payload.clone()
|
||||||
}
|
}
|
||||||
unsafe {frame_payload.free()}
|
unsafe { frame_payload.free() }
|
||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
// if the message is fragmented we just put it on fragments
|
// if the message is fragmented we just put it on fragments
|
||||||
|
@ -139,7 +141,7 @@ pub fn (mut ws Client) read_next_message() ?Message {
|
||||||
data: frame_payload.clone()
|
data: frame_payload.clone()
|
||||||
opcode: frame.opcode
|
opcode: frame.opcode
|
||||||
}
|
}
|
||||||
unsafe {frame_payload.free()}
|
unsafe { frame_payload.free() }
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if ws.fragments.len == 0 {
|
if ws.fragments.len == 0 {
|
||||||
|
@ -152,7 +154,7 @@ pub fn (mut ws Client) read_next_message() ?Message {
|
||||||
opcode: OPCode(frame.opcode)
|
opcode: OPCode(frame.opcode)
|
||||||
payload: frame_payload.clone()
|
payload: frame_payload.clone()
|
||||||
}
|
}
|
||||||
unsafe {frame_payload.free()}
|
unsafe { frame_payload.free() }
|
||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
defer {
|
defer {
|
||||||
|
|
|
@ -1,38 +1,33 @@
|
||||||
// use this test to test the websocket client in the autobahn test
|
// use this test to test the websocket client in the autobahn test
|
||||||
|
|
||||||
module main
|
module main
|
||||||
|
|
||||||
import x.websocket
|
import x.websocket
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
for i in 1 ..304 {
|
for i in 1 .. 304 {
|
||||||
println('\ncase: $i')
|
println('\ncase: $i')
|
||||||
handle_case(i) or {
|
handle_case(i) or { println('error should be ok: $err') }
|
||||||
println('error should be ok: $err')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// update the reports
|
// update the reports
|
||||||
uri := 'ws://autobahn_server:9001/updateReports?agent=v-client'
|
uri := 'ws://autobahn_server:9001/updateReports?agent=v-client'
|
||||||
mut ws := websocket.new_client(uri)?
|
mut ws := websocket.new_client(uri) ?
|
||||||
ws.connect()?
|
ws.connect() ?
|
||||||
ws.listen()?
|
ws.listen() ?
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_case(case_nr int) ? {
|
fn handle_case(case_nr int) ? {
|
||||||
uri := 'ws://autobahn_server:9001/runCase?case=$case_nr&agent=v-client'
|
uri := 'ws://autobahn_server:9001/runCase?case=$case_nr&agent=v-client'
|
||||||
mut ws := websocket.new_client(uri)?
|
mut ws := websocket.new_client(uri) ?
|
||||||
ws.on_message(on_message)
|
ws.on_message(on_message)
|
||||||
ws.connect()?
|
ws.connect() ?
|
||||||
ws.listen()?
|
ws.listen() ?
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_message(mut ws websocket.Client, msg &websocket.Message)? {
|
fn on_message(mut ws websocket.Client, msg &websocket.Message) ? {
|
||||||
// autobahn tests expects to send same message back
|
// autobahn tests expects to send same message back
|
||||||
if msg.opcode == .pong {
|
if msg.opcode == .pong {
|
||||||
// We just wanna pass text and binary message back to autobahn
|
// We just wanna pass text and binary message back to autobahn
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ws.write(msg.payload, msg.opcode) or {
|
ws.write(msg.payload, msg.opcode) or { panic(err) }
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,40 +1,35 @@
|
||||||
// use this test to test the websocket client in the autobahn test
|
// use this test to test the websocket client in the autobahn test
|
||||||
|
|
||||||
module main
|
module main
|
||||||
|
|
||||||
import x.websocket
|
import x.websocket
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
for i in 1 ..304 {
|
for i in 1 .. 304 {
|
||||||
println('\ncase: $i')
|
println('\ncase: $i')
|
||||||
handle_case(i) or {
|
handle_case(i) or { println('error should be ok: $err') }
|
||||||
println('error should be ok: $err')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// update the reports
|
// update the reports
|
||||||
// uri := 'wss://localhost:9002/updateReports?agent=v-client'
|
// uri := 'wss://localhost:9002/updateReports?agent=v-client'
|
||||||
uri := 'wss://autobahn_server_wss:9002/updateReports?agent=v-client'
|
uri := 'wss://autobahn_server_wss:9002/updateReports?agent=v-client'
|
||||||
mut ws := websocket.new_client(uri)?
|
mut ws := websocket.new_client(uri) ?
|
||||||
ws.connect()?
|
ws.connect() ?
|
||||||
ws.listen()?
|
ws.listen() ?
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_case(case_nr int) ? {
|
fn handle_case(case_nr int) ? {
|
||||||
uri := 'wss://autobahn_server_wss:9002/runCase?case=$case_nr&agent=v-client'
|
uri := 'wss://autobahn_server_wss:9002/runCase?case=$case_nr&agent=v-client'
|
||||||
// uri := 'wss://localhost:9002/runCase?case=$case_nr&agent=v-client'
|
// uri := 'wss://localhost:9002/runCase?case=$case_nr&agent=v-client'
|
||||||
mut ws := websocket.new_client(uri)?
|
mut ws := websocket.new_client(uri) ?
|
||||||
ws.on_message(on_message)
|
ws.on_message(on_message)
|
||||||
ws.connect()?
|
ws.connect() ?
|
||||||
ws.listen()?
|
ws.listen() ?
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_message(mut ws websocket.Client, msg &websocket.Message)? {
|
fn on_message(mut ws websocket.Client, msg &websocket.Message) ? {
|
||||||
// autobahn tests expects to send same message back
|
// autobahn tests expects to send same message back
|
||||||
if msg.opcode == .pong {
|
if msg.opcode == .pong {
|
||||||
// We just wanna pass text and binary message back to autobahn
|
// We just wanna pass text and binary message back to autobahn
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ws.write(msg.payload, msg.opcode) or {
|
ws.write(msg.payload, msg.opcode) or { panic(err) }
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
// use this to test websocket server to the autobahn test
|
// use this to test websocket server to the autobahn test
|
||||||
|
|
||||||
module main
|
module main
|
||||||
|
|
||||||
import x.websocket
|
import x.websocket
|
||||||
|
@ -12,19 +11,17 @@ fn main() {
|
||||||
|
|
||||||
fn handle_case(case_nr int) ? {
|
fn handle_case(case_nr int) ? {
|
||||||
uri := 'ws://localhost:9002/runCase?case=$case_nr&agent=v-client'
|
uri := 'ws://localhost:9002/runCase?case=$case_nr&agent=v-client'
|
||||||
mut ws := websocket.new_client(uri)?
|
mut ws := websocket.new_client(uri) ?
|
||||||
ws.on_message(on_message)
|
ws.on_message(on_message)
|
||||||
ws.connect()?
|
ws.connect() ?
|
||||||
ws.listen()?
|
ws.listen() ?
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_message(mut ws websocket.Client, msg &websocket.Message)? {
|
fn on_message(mut ws websocket.Client, msg &websocket.Message) ? {
|
||||||
// autobahn tests expects to send same message back
|
// autobahn tests expects to send same message back
|
||||||
if msg.opcode == .pong {
|
if msg.opcode == .pong {
|
||||||
// We just wanna pass text and binary message back to autobahn
|
// We just wanna pass text and binary message back to autobahn
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ws.write(msg.payload, msg.opcode) or {
|
ws.write(msg.payload, msg.opcode) or { panic(err) }
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,38 +1,33 @@
|
||||||
// use this test to test the websocket client in the autobahn test
|
// use this test to test the websocket client in the autobahn test
|
||||||
|
|
||||||
module main
|
module main
|
||||||
|
|
||||||
import x.websocket
|
import x.websocket
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
for i in 1 ..304 {
|
for i in 1 .. 304 {
|
||||||
println('\ncase: $i')
|
println('\ncase: $i')
|
||||||
handle_case(i) or {
|
handle_case(i) or { println('error should be ok: $err') }
|
||||||
println('error should be ok: $err')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// update the reports
|
// update the reports
|
||||||
uri := 'ws://localhost:9001/updateReports?agent=v-client'
|
uri := 'ws://localhost:9001/updateReports?agent=v-client'
|
||||||
mut ws := websocket.new_client(uri)?
|
mut ws := websocket.new_client(uri) ?
|
||||||
ws.connect()?
|
ws.connect() ?
|
||||||
ws.listen()?
|
ws.listen() ?
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_case(case_nr int) ? {
|
fn handle_case(case_nr int) ? {
|
||||||
uri := 'ws://localhost:9001/runCase?case=$case_nr&agent=v-client'
|
uri := 'ws://localhost:9001/runCase?case=$case_nr&agent=v-client'
|
||||||
mut ws := websocket.new_client(uri)?
|
mut ws := websocket.new_client(uri) ?
|
||||||
ws.on_message(on_message)
|
ws.on_message(on_message)
|
||||||
ws.connect()?
|
ws.connect() ?
|
||||||
ws.listen()?
|
ws.listen() ?
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_message(mut ws websocket.Client, msg &websocket.Message)? {
|
fn on_message(mut ws websocket.Client, msg &websocket.Message) ? {
|
||||||
// autobahn tests expects to send same message back
|
// autobahn tests expects to send same message back
|
||||||
if msg.opcode == .pong {
|
if msg.opcode == .pong {
|
||||||
// We just wanna pass text and binary message back to autobahn
|
// We just wanna pass text and binary message back to autobahn
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ws.write(msg.payload, msg.opcode) or {
|
ws.write(msg.payload, msg.opcode) or { panic(err) }
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,40 +1,35 @@
|
||||||
// use this test to test the websocket client in the autobahn test
|
// use this test to test the websocket client in the autobahn test
|
||||||
|
|
||||||
module main
|
module main
|
||||||
|
|
||||||
import x.websocket
|
import x.websocket
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
for i in 1 ..304 {
|
for i in 1 .. 304 {
|
||||||
println('\ncase: $i')
|
println('\ncase: $i')
|
||||||
handle_case(i) or {
|
handle_case(i) or { println('error should be ok: $err') }
|
||||||
println('error should be ok: $err')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// update the reports
|
// update the reports
|
||||||
// uri := 'wss://localhost:9002/updateReports?agent=v-client'
|
// uri := 'wss://localhost:9002/updateReports?agent=v-client'
|
||||||
uri := 'wss://autobahn_server_wss:9002/updateReports?agent=v-client'
|
uri := 'wss://autobahn_server_wss:9002/updateReports?agent=v-client'
|
||||||
mut ws := websocket.new_client(uri)?
|
mut ws := websocket.new_client(uri) ?
|
||||||
ws.connect()?
|
ws.connect() ?
|
||||||
ws.listen()?
|
ws.listen() ?
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_case(case_nr int) ? {
|
fn handle_case(case_nr int) ? {
|
||||||
uri := 'wss://autobahn_server_wss:9002/runCase?case=$case_nr&agent=v-client'
|
uri := 'wss://autobahn_server_wss:9002/runCase?case=$case_nr&agent=v-client'
|
||||||
// uri := 'wss://localhost:9002/runCase?case=$case_nr&agent=v-client'
|
// uri := 'wss://localhost:9002/runCase?case=$case_nr&agent=v-client'
|
||||||
mut ws := websocket.new_client(uri)?
|
mut ws := websocket.new_client(uri) ?
|
||||||
ws.on_message(on_message)
|
ws.on_message(on_message)
|
||||||
ws.connect()?
|
ws.connect() ?
|
||||||
ws.listen()?
|
ws.listen() ?
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_message(mut ws websocket.Client, msg &websocket.Message)? {
|
fn on_message(mut ws websocket.Client, msg &websocket.Message) ? {
|
||||||
// autobahn tests expects to send same message back
|
// autobahn tests expects to send same message back
|
||||||
if msg.opcode == .pong {
|
if msg.opcode == .pong {
|
||||||
// We just wanna pass text and binary message back to autobahn
|
// We just wanna pass text and binary message back to autobahn
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ws.write(msg.payload, msg.opcode) or {
|
ws.write(msg.payload, msg.opcode) or { panic(err) }
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ fn htonl64(payload_len u64) []byte {
|
||||||
fn create_masking_key() []byte {
|
fn create_masking_key() []byte {
|
||||||
mask_bit := byte(rand.intn(255))
|
mask_bit := byte(rand.intn(255))
|
||||||
buf := []byte{len: 4, init: `0`}
|
buf := []byte{len: 4, init: `0`}
|
||||||
unsafe {C.memcpy(buf.data, &mask_bit, 4)}
|
unsafe { C.memcpy(buf.data, &mask_bit, 4) }
|
||||||
return buf
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,7 @@ import time
|
||||||
fn test_ws() {
|
fn test_ws() {
|
||||||
go start_server()
|
go start_server()
|
||||||
time.sleep_ms(100)
|
time.sleep_ms(100)
|
||||||
ws_test('ws://localhost:30000') or {
|
ws_test('ws://localhost:30000') or { assert false }
|
||||||
assert false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn start_server() ? {
|
fn start_server() ? {
|
||||||
|
@ -24,14 +22,12 @@ fn start_server() ? {
|
||||||
return true
|
return true
|
||||||
}) ?
|
}) ?
|
||||||
s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
|
s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
|
||||||
ws.write(msg.payload, msg.opcode) or {
|
ws.write(msg.payload, msg.opcode) or { panic(err) }
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
s.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
|
s.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
|
||||||
// not used
|
// not used
|
||||||
})
|
})
|
||||||
s.listen() or {}
|
s.listen() or { }
|
||||||
}
|
}
|
||||||
|
|
||||||
// ws_test tests connect to the websocket server from websocket client
|
// ws_test tests connect to the websocket server from websocket client
|
||||||
|
@ -61,15 +57,11 @@ fn ws_test(uri string) ? {
|
||||||
println('Binary message: $msg')
|
println('Binary message: $msg')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
ws.connect() or {
|
ws.connect() or { panic('fail to connect') }
|
||||||
panic('fail to connect')
|
|
||||||
}
|
|
||||||
go ws.listen()
|
go ws.listen()
|
||||||
text := ['a'].repeat(2)
|
text := ['a'].repeat(2)
|
||||||
for msg in text {
|
for msg in text {
|
||||||
ws.write(msg.bytes(), .text_frame) or {
|
ws.write(msg.bytes(), .text_frame) or { panic('fail to write to websocket') }
|
||||||
panic('fail to write to websocket')
|
|
||||||
}
|
|
||||||
// sleep to give time to recieve response before send a new one
|
// sleep to give time to recieve response before send a new one
|
||||||
time.sleep_ms(100)
|
time.sleep_ms(100)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue