diff options
| author | Shulhan <m.shulhan@gmail.com> | 2020-08-11 16:36:45 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2020-08-11 16:36:45 +0700 |
| commit | 07e8370f158addf23591caa4e268e0fdd0992b44 (patch) | |
| tree | fad1cca37969f6c0aa01811d8fdf4c4a5b96e349 /lib/websocket | |
| parent | 7a6afab5cbd0a02402309fed0ff021d6f9143427 (diff) | |
| download | pakakeh.go-07e8370f158addf23591caa4e268e0fdd0992b44.tar.xz | |
websocket: reformat some files
Diffstat (limited to 'lib/websocket')
| -rw-r--r-- | lib/websocket/client.go | 23 | ||||
| -rw-r--r-- | lib/websocket/examples/cmd/server/main.go | 7 | ||||
| -rw-r--r-- | lib/websocket/frame.go | 21 | ||||
| -rw-r--r-- | lib/websocket/frame_test.go | 24 | ||||
| -rw-r--r-- | lib/websocket/frames.go | 6 | ||||
| -rw-r--r-- | lib/websocket/frames_test.go | 18 | ||||
| -rw-r--r-- | lib/websocket/funcs.go | 3 | ||||
| -rw-r--r-- | lib/websocket/server.go | 32 | ||||
| -rw-r--r-- | lib/websocket/websocket_test.go | 3 |
9 files changed, 92 insertions, 45 deletions
diff --git a/lib/websocket/client.go b/lib/websocket/client.go index 9aeae12b..bb76b3c0 100644 --- a/lib/websocket/client.go +++ b/lib/websocket/client.go @@ -348,7 +348,8 @@ func (cl *Client) open() (err error) { } if debug.Value >= 3 { - fmt.Printf("websocket: Client.open: remoteAddr: %s\n", cl.remoteAddr) + fmt.Printf("websocket: Client.open: remoteAddr: %s\n", + cl.remoteAddr) } if cl.TLSConfig != nil { @@ -409,7 +410,9 @@ func (cl *Client) handshake() (rest []byte, err error) { return rest, nil } -func (cl *Client) doHandshake(keyAccept string, req []byte) (rest []byte, err error) { +func (cl *Client) doHandshake(keyAccept string, req []byte) ( + rest []byte, err error, +) { err = cl.send(req) if err != nil { return nil, err @@ -587,7 +590,8 @@ func (cl *Client) handleFrame(frame *Frame) (isClosing bool) { if isInvalid { isClosing = true } - case OpcodeDataRsv3, OpcodeDataRsv4, OpcodeDataRsv5, OpcodeDataRsv6, OpcodeDataRsv7: + case OpcodeDataRsv3, OpcodeDataRsv4, OpcodeDataRsv5, OpcodeDataRsv6, + OpcodeDataRsv7: cl.handleBadRequest() return true case OpcodeClose: @@ -604,7 +608,8 @@ func (cl *Client) handleFrame(frame *Frame) (isClosing bool) { if cl.handlePong != nil { _ = cl.handlePong(cl, frame) } - case OpcodeControlRsvB, OpcodeControlRsvC, OpcodeControlRsvD, OpcodeControlRsvE, OpcodeControlRsvF: + case OpcodeControlRsvB, OpcodeControlRsvC, OpcodeControlRsvD, + OpcodeControlRsvE, OpcodeControlRsvF: if cl.HandleRsvControl != nil { _ = cl.HandleRsvControl(cl, frame) } else { @@ -616,13 +621,16 @@ func (cl *Client) handleFrame(frame *Frame) (isClosing bool) { return isClosing } -func (cl *Client) handleHandshake(keyAccept string, resp []byte) (rest []byte, err error) { +func (cl *Client) handleHandshake(keyAccept string, resp []byte) ( + rest []byte, err error, +) { if debug.Value >= 3 { max := 512 if len(resp) < 512 { max = len(resp) } - fmt.Printf("websocket: Client.handleHandshake:\n%s\n--\n", resp[:max]) + fmt.Printf("websocket: Client.handleHandshake:\n%s\n--\n", + resp[:max]) } var httpRes *http.Response @@ -840,7 +848,8 @@ func (cl *Client) recv() (packet []byte, err error) { if max > 16 { max = 16 } - fmt.Printf("websocket: Client.recv: packet: len:%d % x\n", len(packet), packet[:max]) + fmt.Printf("websocket: Client.recv: packet: len:%d % x\n", + len(packet), packet[:max]) } return packet, err diff --git a/lib/websocket/examples/cmd/server/main.go b/lib/websocket/examples/cmd/server/main.go index 2258718f..4bf771fc 100644 --- a/lib/websocket/examples/cmd/server/main.go +++ b/lib/websocket/examples/cmd/server/main.go @@ -34,7 +34,8 @@ func main() { server = websocket.NewServer(opts) // Register the message handler - err := server.RegisterTextHandler(http.MethodPost, "/message", handlePostMessage) + err := server.RegisterTextHandler(http.MethodPost, "/message", + handlePostMessage) if err != nil { log.Fatal(err) } @@ -120,7 +121,9 @@ func handleClientRemove(ctx context.Context, conn int) { // // handlePostMessage handle message that is send to server by client. // -func handlePostMessage(ctx context.Context, req *websocket.Request) (res websocket.Response) { +func handlePostMessage(ctx context.Context, req *websocket.Request) ( + res websocket.Response, +) { uid := ctx.Value(websocket.CtxKeyUID).(int64) user := examples.Users[uid] diff --git a/lib/websocket/frame.go b/lib/websocket/frame.go index 574966a9..6d0e89e6 100644 --- a/lib/websocket/frame.go +++ b/lib/websocket/frame.go @@ -379,24 +379,30 @@ func (f *Frame) unpack(packet []byte) []byte { if len(f.chopped) < 10 { exp := 10 - len(f.chopped) if len(packet) < exp { - f.chopped = append(f.chopped, packet...) + f.chopped = append(f.chopped, + packet...) return nil } // chopped: 81 FF 0 0 0 1 0 0 = 10 - 8) = 2 // exp: 0 0 - f.chopped = append(f.chopped, packet[:exp]...) - f.len = binary.BigEndian.Uint64(f.chopped[2:10]) + f.chopped = append(f.chopped, + packet[:exp]...) + f.len = binary.BigEndian.Uint64( + f.chopped[2:10]) packet = packet[exp:] } case frameMediumPayload: if len(f.chopped) < 4 { exp := 4 - len(f.chopped) if len(packet) < exp { - f.chopped = append(f.chopped, packet...) + f.chopped = append(f.chopped, + packet...) return nil } - f.chopped = append(f.chopped, packet[:exp]...) - f.len = uint64(binary.BigEndian.Uint16(f.chopped[2:4])) + f.chopped = append(f.chopped, + packet[:exp]...) + f.len = uint64(binary.BigEndian.Uint16( + f.chopped[2:4])) packet = packet[exp:] } } @@ -454,7 +460,8 @@ func (f *Frame) unpack(packet []byte) []byte { case 1: f.closeCode = StatusBadRequest default: - f.closeCode = CloseCode(binary.BigEndian.Uint16(f.payload[:2])) + f.closeCode = CloseCode(binary.BigEndian.Uint16( + f.payload[:2])) } } f.isComplete = true diff --git a/lib/websocket/frame_test.go b/lib/websocket/frame_test.go index 306dd3f9..1775f44c 100644 --- a/lib/websocket/frame_test.go +++ b/lib/websocket/frame_test.go @@ -64,7 +64,8 @@ func TestNewFrameClose(t *testing.T) { opcode: OpcodeClose, closeCode: StatusBadRequest, masked: frameIsMasked, - payload: libbytes.Concat([]byte{0x03, 0xEA}, []byte("Hello!")), + payload: libbytes.Concat([]byte{0x03, 0xEA}, + []byte("Hello!")), }, }, { desc: "With overflow payload", @@ -74,7 +75,8 @@ func TestNewFrameClose(t *testing.T) { opcode: OpcodeClose, closeCode: StatusBadRequest, masked: frameIsMasked, - payload: libbytes.Concat([]byte{0x03, 0xEA}, _dummyPayload256[:123]), + payload: libbytes.Concat([]byte{0x03, 0xEA}, + _dummyPayload256[:123]), }, }} @@ -87,7 +89,8 @@ func TestNewFrameClose(t *testing.T) { test.Assert(t, "Frame.fin", c.exp.fin, frame.fin, true) test.Assert(t, "Frame.opcode", c.exp.opcode, frame.opcode, true) - test.Assert(t, "Frame.closeCode", c.exp.closeCode, frame.closeCode, true) + test.Assert(t, "Frame.closeCode", c.exp.closeCode, + frame.closeCode, true) test.Assert(t, "Frame.masked", c.exp.masked, frame.masked, true) test.Assert(t, "Frame.payload", c.exp.payload, frame.payload, true) } @@ -236,7 +239,8 @@ func TestFramePack(t *testing.T) { }, exp: []byte{ 0x81, 0x85, - _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], _testMaskKey[3], + _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], + _testMaskKey[3], 0x7f, 0x9f, 0x4d, 0x51, 0x58, }, }, { @@ -277,7 +281,8 @@ func TestFramePack(t *testing.T) { }, exp: []byte{ 0x8a, 0x85, - _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], _testMaskKey[3], + _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], + _testMaskKey[3], 0x7f, 0x9f, 0x4d, 0x51, 0x58, }, }, { @@ -288,7 +293,8 @@ func TestFramePack(t *testing.T) { masked: 0, payload: _dummyPayload256, }, - exp: libbytes.Concat([]byte{0x82, 0x7E, 0x01, 0x00}, _dummyPayload256), + exp: libbytes.Concat([]byte{0x82, 0x7E, 0x01, 0x00}, + _dummyPayload256), }, { desc: `256 bytes binary message in a single masked frame`, f: Frame{ @@ -301,7 +307,8 @@ func TestFramePack(t *testing.T) { exp: libbytes.Concat([]byte{ 0x82, 0xFE, 0x01, 0x00, - _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], _testMaskKey[3], + _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], + _testMaskKey[3], }, _dummyPayload256Masked), }, { desc: `65536 binary message in a single unmasked frame`, @@ -328,7 +335,8 @@ func TestFramePack(t *testing.T) { exp: libbytes.Concat([]byte{ 0x82, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, - _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], _testMaskKey[3], + _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], + _testMaskKey[3], }, _dummyPayload65536Masked), }} diff --git a/lib/websocket/frames.go b/lib/websocket/frames.go index 6240776c..0cb6a182 100644 --- a/lib/websocket/frames.go +++ b/lib/websocket/frames.go @@ -68,7 +68,8 @@ func (frames *Frames) fin(last *Frame) (frame *Frame) { } // Ignore control PING or PONG frame. - if frames.v[x].opcode == OpcodePing || frames.v[x].opcode == OpcodePong { + if frames.v[x].opcode == OpcodePing || + frames.v[x].opcode == OpcodePong { continue } @@ -129,7 +130,8 @@ func (frames *Frames) payload() (payload []byte) { } // Ignore control PING or PONG frame. - if frames.v[x].opcode == OpcodePing || frames.v[x].opcode == OpcodePong { + if frames.v[x].opcode == OpcodePing || + frames.v[x].opcode == OpcodePong { continue } diff --git a/lib/websocket/frames_test.go b/lib/websocket/frames_test.go index 6669b848..22e6379b 100644 --- a/lib/websocket/frames_test.go +++ b/lib/websocket/frames_test.go @@ -33,7 +33,8 @@ func TestFrameUnpack(t *testing.T) { desc: "A single-frame masked text message", in: []byte{ 0x81, 0x85, - _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], _testMaskKey[3], + _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], + _testMaskKey[3], 0x7f, 0x9f, 0x4d, 0x51, 0x58, }, exp: &Frame{ @@ -82,7 +83,8 @@ func TestFrameUnpack(t *testing.T) { desc: `Pong without payload`, in: []byte{ 0x8A, 0x80, - _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], _testMaskKey[3], + _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], + _testMaskKey[3], }, exp: &Frame{ fin: frameIsFinished, @@ -95,7 +97,8 @@ func TestFrameUnpack(t *testing.T) { desc: `Pong with payload`, in: []byte{ 0x8a, 0x85, - _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], _testMaskKey[3], + _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], + _testMaskKey[3], 0x7f, 0x9f, 0x4d, 0x51, 0x58, }, exp: &Frame{ @@ -109,7 +112,8 @@ func TestFrameUnpack(t *testing.T) { }, }, { desc: `256 bytes binary message in a single unmasked frame`, - in: libbytes.Concat([]byte{0x82, 0x7E, 0x01, 0x00}, _dummyPayload256), + in: libbytes.Concat([]byte{0x82, 0x7E, 0x01, 0x00}, + _dummyPayload256), exp: &Frame{ fin: frameIsFinished, opcode: OpcodeBin, @@ -122,7 +126,8 @@ func TestFrameUnpack(t *testing.T) { desc: `256 bytes binary message in a single masked frame`, in: libbytes.Concat([]byte{ 0x82, 0xFE, 0x01, 0x00, - _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], _testMaskKey[3], + _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], + _testMaskKey[3], }, _dummyPayload256Masked), exp: &Frame{ fin: frameIsFinished, @@ -152,7 +157,8 @@ func TestFrameUnpack(t *testing.T) { in: libbytes.Concat([]byte{ 0x82, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, - _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], _testMaskKey[3], + _testMaskKey[0], _testMaskKey[1], _testMaskKey[2], + _testMaskKey[3], }, _dummyPayload65536Masked), exp: &Frame{ fin: frameIsFinished, diff --git a/lib/websocket/funcs.go b/lib/websocket/funcs.go index 80d6cc18..656be8ba 100644 --- a/lib/websocket/funcs.go +++ b/lib/websocket/funcs.go @@ -62,7 +62,8 @@ func Send(fd int, packet []byte) (err error) { if err != nil { errno, ok := err.(unix.Errno) if ok { - log.Printf("websocket: Send: errno: %d %d\n", errno, unix.EAGAIN) + log.Printf("websocket: Send: errno: %d %d\n", + errno, unix.EAGAIN) if errno == unix.EAGAIN { continue } diff --git a/lib/websocket/server.go b/lib/websocket/server.go index 327df6a0..3f907f06 100644 --- a/lib/websocket/server.go +++ b/lib/websocket/server.go @@ -144,7 +144,9 @@ func (serv *Server) createSockServer() (err error) { // RegisterTextHandler register specific function to be called by server when // request opcode is text, and method and target matched with Request. // -func (serv *Server) RegisterTextHandler(method, target string, handler RouteHandler) (err error) { +func (serv *Server) RegisterTextHandler( + method, target string, handler RouteHandler, +) (err error) { if len(method) == 0 || len(target) == 0 || handler == nil { return } @@ -257,7 +259,8 @@ func (serv *Server) upgrader() { continue } if hs.URL.Path != serv.opts.ConnectPath { - serv.handleError(conn, http.StatusNotFound, "unknown path") + serv.handleError(conn, http.StatusNotFound, + "unknown path") continue } @@ -273,7 +276,8 @@ func (serv *Server) upgrader() { err = Send(conn, []byte(httpRes)) if err != nil { - log.Println("websocket: server.upgrader: Send: " + err.Error()) + log.Println("websocket: server.upgrader: Send: ", + err.Error()) unix.Close(conn) continue } @@ -284,7 +288,8 @@ func (serv *Server) upgrader() { err = serv.clientAdd(ctx, conn) if err != nil { - log.Println("websocket: server.upgrader: clientAdd: " + err.Error()) + log.Println("websocket: server.upgrader: clientAdd: ", + err.Error()) unix.Close(conn) } } @@ -321,7 +326,8 @@ func (serv *Server) handleFragment(conn int, req *Frame) (isInvalid bool) { if debug.Value >= 3 { fmt.Printf("websocket: Server.handleFragment: frame: {fin:%d opcode:%d masked:%d len:%d, payload.len:%d}\n", - req.fin, req.opcode, req.masked, req.len, len(req.payload)) + req.fin, req.opcode, req.masked, req.len, + len(req.payload)) } if frames == nil { @@ -389,7 +395,8 @@ func (serv *Server) handleFrame(conn int, frame *Frame) (isClosing bool) { if isInvalid { isClosing = true } - case OpcodeDataRsv3, OpcodeDataRsv4, OpcodeDataRsv5, OpcodeDataRsv6, OpcodeDataRsv7: + case OpcodeDataRsv3, OpcodeDataRsv4, OpcodeDataRsv5, OpcodeDataRsv6, + OpcodeDataRsv7: serv.handleBadRequest(conn) isClosing = true case OpcodeClose: @@ -401,7 +408,8 @@ func (serv *Server) handleFrame(conn int, frame *Frame) (isClosing bool) { if serv.handlePong != nil { go serv.handlePong(conn, frame) } - case OpcodeControlRsvB, OpcodeControlRsvC, OpcodeControlRsvD, OpcodeControlRsvE, OpcodeControlRsvF: + case OpcodeControlRsvB, OpcodeControlRsvC, OpcodeControlRsvD, + OpcodeControlRsvE, OpcodeControlRsvF: if serv.opts.HandleRsvControl != nil { serv.opts.HandleRsvControl(conn, frame) } else { @@ -616,7 +624,8 @@ func (serv *Server) handleInvalidData(conn int) { // func (serv *Server) handlePing(conn int, req *Frame) { if debug.Value >= 3 { - fmt.Printf("websocket: Server.handlePing: conn:%d frame:%+v\n", conn, req) + fmt.Printf("websocket: Server.handlePing: conn:%d frame:%+v\n", + conn, req) } req.opcode = OpcodePong @@ -666,7 +675,8 @@ func (serv *Server) reader() { if max > 16 { max = 16 } - fmt.Printf("websocket: Server.reader: packet {len:%d value:% x ...}\n", len(packet), packet[:max]) + fmt.Printf("websocket: Server.reader: packet {len:%d value:% x ...}\n", + len(packet), packet[:max]) } // Handle chopped, unfinished packet or payload. @@ -731,8 +741,8 @@ func (serv *Server) pinger() { for _, conn := range all { err := Send(conn, framePing) if err != nil { - // Error on sending PING will be assumed as - // bad connection. + // Error on sending PING will be + // assumed as bad connection. serv.ClientRemove(conn) } } diff --git a/lib/websocket/websocket_test.go b/lib/websocket/websocket_test.go index a2dc2bb5..fb7bb0bb 100644 --- a/lib/websocket/websocket_test.go +++ b/lib/websocket/websocket_test.go @@ -95,7 +95,8 @@ func runTestServer() { _testAddr = "127.0.0.1:" + strconv.Itoa(_testPort) _testWSAddr = "ws://" + _testAddr + "/" - _testEndpointAuth = _testWSAddr + "?" + _qKeyTicket + "=" + _testExternalJWT + _testEndpointAuth = _testWSAddr + "?" + _qKeyTicket + "=" + + _testExternalJWT opts := &ServerOptions{ Address: _testAddr, |
