refactor: move to go modules and lint

This commit is contained in:
Adrien Marquès 2021-05-14 16:47:02 +02:00
parent 7fb4241187
commit db52cfd28f
14 changed files with 229 additions and 201 deletions

View File

@ -4,10 +4,11 @@ import (
"bufio"
"encoding/binary"
"fmt"
"git.xdrm.io/go/websocket/internal/http/upgrade/request"
"net"
"sync"
"time"
"git.xdrm.io/go/ws/internal/http/upgrade/request"
)
// Represents a client socket utility (reader, writer, ..)
@ -119,7 +120,7 @@ func buildClient(s net.Conn, ctl ControllerSet, serverCh serverChannelSet) (*cli
func clientReader(c *client) {
var frag *Message
closeStatus := NORMAL
closeStatus := Normal
clientAck := true
c.io.reading.Add(1)
@ -136,7 +137,7 @@ func clientReader(c *client) {
msg, err := readMessage(c.io.reader)
if err == ErrUnmaskedFrame || err == ErrReservedBits {
closeStatus = PROTOCOL_ERR
closeStatus = ProtocolError
}
if err != nil {
break
@ -152,25 +153,25 @@ func clientReader(c *client) {
// Fail
case ErrUnexpectedContinuation:
closeStatus = NONE
closeStatus = None
clientAck = false
mustClose = true
// proper close
case CloseFrame:
closeStatus = NORMAL
case ErrCloseFrame:
closeStatus = Normal
clientAck = true
mustClose = true
// invalid payload proper close
case ErrInvalidPayload:
closeStatus = INVALID_PAYLOAD
closeStatus = InvalidPayload
clientAck = true
mustClose = true
// any other error -> protocol error
default:
closeStatus = PROTOCOL_ERR
closeStatus = ProtocolError
clientAck = true
mustClose = true
}
@ -182,9 +183,9 @@ func clientReader(c *client) {
}
/* (4) Ping <-> Pong */
if msg.Type == PING && c.io.writing {
if msg.Type == Ping && c.io.writing {
msg.Final = true
msg.Type = PONG
msg.Type = Pong
c.ch.send <- *msg
continue
}
@ -213,10 +214,10 @@ func clientReader(c *client) {
// check message errors
fragErr := frag.check(false)
if fragErr == ErrInvalidPayload {
closeStatus = INVALID_PAYLOAD
closeStatus = InvalidPayload
break
} else if fragErr != nil {
closeStatus = PROTOCOL_ERR
closeStatus = ProtocolError
break
}
@ -226,7 +227,7 @@ func clientReader(c *client) {
}
/* (7) Dispatch to receiver */
if msg.Type == TEXT || msg.Type == BINARY {
if msg.Type == Text || msg.Type == Binary {
c.ch.receive <- *msg
}
@ -265,7 +266,7 @@ func clientWriter(c *client) {
/* (4) close channel (if not already done) */
// fmt.Printf("[writer] end\n")
c.close(NORMAL, true)
c.close(Normal, true)
}
@ -295,12 +296,12 @@ func (c *client) close(status MessageError, clientACK bool) {
c.io.sock.SetReadDeadline(time.Now().Add(time.Second * -1))
c.io.reading.Wait()
if status != NONE {
if status != None {
/* (3) Build message */
msg := &Message{
Final: true,
Type: CLOSE,
Type: Close,
Size: 2,
Data: make([]byte, 2),
}

View File

@ -2,8 +2,9 @@ package iface
import (
"fmt"
ws "git.xdrm.io/go/websocket"
"time"
ws "git.xdrm.io/go/ws"
)
func main() {

View File

@ -1,29 +1,29 @@
package websocket
import (
"git.xdrm.io/go/websocket/internal/uri/parser"
"git.xdrm.io/go/ws/internal/uri/parser"
)
// Represents available information about a client
// Client contains available information about a client
type Client struct {
Protocol string // choosen protocol (Sec-WebSocket-Protocol)
Arguments [][]string // URI parameters, index 0 is full URI, then matching groups
Store interface{} // store (for client implementation-specific data)
}
// Represents a websocket controller callback function
// ControllerFunc is a websocket controller callback function
type ControllerFunc func(*Client, <-chan Message, chan<- Message, chan<- Message)
// Represents a websocket controller
// Controller is a websocket controller
type Controller struct {
URI *parser.Scheme // uri scheme
Fun ControllerFunc // controller function
}
// Represents a controller set
// ControllerSet is set of controllers
type ControllerSet struct {
Def *Controller // default controller
Uri []*Controller // uri controllers
URI []*Controller // uri controllers
}
// Match finds a controller for a given URI
@ -31,10 +31,10 @@ type ControllerSet struct {
func (s *ControllerSet) Match(uri string) (*Controller, [][]string) {
/* (1) Initialise argument list */
arguments := [][]string{[]string{uri}}
arguments := [][]string{{uri}}
/* (2) Try each controller */
for _, c := range s.Uri {
for _, c := range s.URI {
/* 1. If matches */
if c.URI.Match(uri) {

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.xdrm.io/go/ws
go 1.16

View File

@ -6,37 +6,34 @@ package reader
// the golang standard library
import (
"bufio"
"fmt"
"io"
"bufio"
)
// Maximum line length
var maxLineLength = 4096
// Chunk reader
type chunkReader struct {
// ChunkReader struct
type ChunkReader struct {
reader *bufio.Reader // the reader
isEnded bool // If we are done (2 consecutive CRLF)
}
// New creates a new reader
func NewReader(r io.Reader) (reader *chunkReader) {
// NewReader creates a new reader
func NewReader(r io.Reader) *ChunkReader {
br, ok := r.(*bufio.Reader)
if !ok {
br = bufio.NewReader(r)
}
return &chunkReader{reader: br}
return &ChunkReader{reader: br}
}
// Read reads a chunk, err is io.EOF when done
func (r *chunkReader) Read() ([]byte, error){
func (r *ChunkReader) Read() ([]byte, error) {
/* (1) If already ended */
if r.isEnded {
@ -73,9 +70,7 @@ func (r *chunkReader) Read() ([]byte, error){
}
func removeTrailingSpace(b []byte) []byte{
func removeTrailingSpace(b []byte) []byte {
for len(b) > 0 && isASCIISpace(b[len(b)-1]) {
b = b[:len(b)-1]
}
@ -83,5 +78,5 @@ func removeTrailingSpace(b []byte) []byte{
}
func isASCIISpace(b byte) bool {
return b == ' ' || b == '\t' || b == '\r' || b =='\n'
return b == ' ' || b == '\t' || b == '\r' || b == '\n'
}

View File

@ -2,10 +2,11 @@ package request
import (
"fmt"
"git.xdrm.io/go/websocket/internal/http/upgrade/request/parser/header"
"git.xdrm.io/go/websocket/internal/http/upgrade/response"
"strconv"
"strings"
"git.xdrm.io/go/ws/internal/http/upgrade/request/parser/header"
"git.xdrm.io/go/ws/internal/http/upgrade/response"
)
// checkHost checks and extracts the Host header
@ -31,7 +32,7 @@ func (r *T) extractHostPort(bb header.HeaderValue) error {
// extract port
readPort, err := strconv.ParseUint(split[1], 10, 16)
if err != nil {
r.code = response.BAD_REQUEST
r.code = response.BadRequest
return &InvalidRequest{"Host", "cannot read port"}
}
@ -41,7 +42,7 @@ func (r *T) extractHostPort(bb header.HeaderValue) error {
if len(r.origin) > 0 {
if err != nil {
err = r.checkOriginPolicy()
r.code = response.FORBIDDEN
r.code = response.Forbidden
return &InvalidOriginPolicy{r.host, r.origin, err}
}
}
@ -59,7 +60,7 @@ func (r *T) extractOrigin(bb header.HeaderValue) error {
}
if len(bb) != 1 {
r.code = response.FORBIDDEN
r.code = response.Forbidden
return &InvalidRequest{"Origin", fmt.Sprintf("expected single value, got %d", len(bb))}
}
@ -69,7 +70,7 @@ func (r *T) extractOrigin(bb header.HeaderValue) error {
if len(r.host) > 0 {
err := r.checkOriginPolicy()
if err != nil {
r.code = response.FORBIDDEN
r.code = response.Forbidden
return &InvalidOriginPolicy{r.host, r.origin, err}
}
}
@ -98,7 +99,7 @@ func (r *T) checkConnection(bb header.HeaderValue) error {
}
r.code = response.BAD_REQUEST
r.code = response.BadRequest
return &InvalidRequest{"Upgrade", "expected 'Upgrade' (case insensitive)"}
}
@ -108,7 +109,7 @@ func (r *T) checkConnection(bb header.HeaderValue) error {
func (r *T) checkUpgrade(bb header.HeaderValue) error {
if len(bb) != 1 {
r.code = response.BAD_REQUEST
r.code = response.BadRequest
return &InvalidRequest{"Upgrade", fmt.Sprintf("expected single value, got %d", len(bb))}
}
@ -117,7 +118,7 @@ func (r *T) checkUpgrade(bb header.HeaderValue) error {
return nil
}
r.code = response.BAD_REQUEST
r.code = response.BadRequest
return &InvalidRequest{"Upgrade", fmt.Sprintf("expected 'websocket' (case insensitive), got '%s'", bb[0])}
}
@ -127,7 +128,7 @@ func (r *T) checkUpgrade(bb header.HeaderValue) error {
func (r *T) checkVersion(bb header.HeaderValue) error {
if len(bb) != 1 || string(bb[0]) != "13" {
r.code = response.UPGRADE_REQUIRED
r.code = response.UpgradeRequired
return &InvalidRequest{"Sec-WebSocket-Version", fmt.Sprintf("expected '13', got '%s'", bb[0])}
}
@ -141,7 +142,7 @@ func (r *T) checkVersion(bb header.HeaderValue) error {
func (r *T) extractKey(bb header.HeaderValue) error {
if len(bb) != 1 || len(bb[0]) != 24 {
r.code = response.BAD_REQUEST
r.code = response.BadRequest
return &InvalidRequest{"Sec-WebSocket-Key", fmt.Sprintf("expected 24 bytes base64, got %d bytes", len(bb[0]))}
}

View File

@ -2,8 +2,9 @@ package request
import (
"fmt"
"git.xdrm.io/go/websocket/internal/http/upgrade/request/parser/header"
"git.xdrm.io/go/websocket/internal/http/upgrade/response"
"git.xdrm.io/go/ws/internal/http/upgrade/request/parser/header"
"git.xdrm.io/go/ws/internal/http/upgrade/response"
)
// parseHeader parses any http request line
@ -17,7 +18,7 @@ func (r *T) parseHeader(b []byte) error {
err := r.request.Parse(b)
if err != nil {
r.code = response.BAD_REQUEST
r.code = response.BadRequest
return &InvalidRequest{"Request-Line", err.Error()}
}
@ -31,7 +32,7 @@ func (r *T) parseHeader(b []byte) error {
/* (1) Try to parse header */
head, err := header.Parse(b)
if err != nil {
r.code = response.BAD_REQUEST
r.code = response.BadRequest
return fmt.Errorf("Error parsing header: %s", err)
}

View File

@ -2,9 +2,10 @@ package request
import (
"fmt"
"git.xdrm.io/go/websocket/internal/http/reader"
"git.xdrm.io/go/websocket/internal/http/upgrade/response"
"io"
"git.xdrm.io/go/ws/internal/http/reader"
"git.xdrm.io/go/ws/internal/http/upgrade/response"
)
// Parse builds an upgrade HTTP request
@ -45,11 +46,11 @@ func Parse(r io.Reader) (request *T, err error) {
/* (3) Check completion */
err = req.isComplete()
if err != nil {
req.code = response.BAD_REQUEST
req.code = response.BadRequest
return req, err
}
req.code = response.SWITCHING_PROTOCOLS
req.code = response.SwitchingProtocols
return req, nil
}

View File

@ -1,6 +1,6 @@
package request
import "git.xdrm.io/go/websocket/internal/http/upgrade/response"
import "git.xdrm.io/go/ws/internal/http/upgrade/response"
// If origin is required
const bypassOriginPolicy = true

View File

@ -12,7 +12,7 @@ func (r *T) SetStatusCode(sc StatusCode) {
r.code = sc
}
// SetProtocols sets the protocols
// SetProtocol sets the protocols
func (r *T) SetProtocol(p []byte) {
r.protocol = p
}
@ -43,7 +43,7 @@ func (r *T) ProcessKey(k []byte) {
func (r T) Send(w io.Writer) (int, error) {
/* (1) Build response line */
responseLine := fmt.Sprintf("HTTP/%s %d %s\r\n", HttpVersion, r.code, r.code.Message())
responseLine := fmt.Sprintf("HTTP/%s %d %s\r\n", HttpVersion, r.code, r.code.String())
/* (2) Build headers */
optionalProtocol := ""

View File

@ -3,27 +3,35 @@ package response
// StatusCode maps the status codes (and description)
type StatusCode uint16
var SWITCHING_PROTOCOLS StatusCode = 101 // handshake success
var BAD_REQUEST StatusCode = 400 // missing/malformed headers
var FORBIDDEN StatusCode = 403 // invalid origin policy, TLS required
var UPGRADE_REQUIRED StatusCode = 426 // invalid WS version
var NOT_FOUND StatusCode = 404 // unserved or invalid URI
var INTERNAL StatusCode = 500 // custom error
func (sc StatusCode) Message() string {
var (
// SwitchingProtocols - handshake success
SwitchingProtocols StatusCode = 101
// BadRequest - missing/malformed headers
BadRequest StatusCode = 400
// Forbidden - invalid origin policy, TLS required
Forbidden StatusCode = 403
// UpgradeRequired - invalid WS version
UpgradeRequired StatusCode = 426
// NotFound - unserved or invalid URI
NotFound StatusCode = 404
// Internal - custom error
Internal StatusCode = 500
)
// String implements the Stringer interface
func (sc StatusCode) String() string {
switch sc {
case SWITCHING_PROTOCOLS:
case SwitchingProtocols:
return "Switching Protocols"
case BAD_REQUEST:
case BadRequest:
return "Bad Request"
case FORBIDDEN:
case Forbidden:
return "Forbidden"
case UPGRADE_REQUIRED:
case UpgradeRequired:
return "Upgrade Required"
case NOT_FOUND:
case NotFound:
return "Not Found"
case INTERNAL:
case Internal:
return "Internal Server Error"
default:
return "Unknown Status Code"

View File

@ -7,47 +7,72 @@ import (
"unicode/utf8"
)
var ErrUnmaskedFrame = fmt.Errorf("Received unmasked frame")
var ErrTooLongControlFrame = fmt.Errorf("Received a control frame that is fragmented or too long")
var ErrInvalidFragment = fmt.Errorf("Received invalid fragmentation")
var ErrUnexpectedContinuation = fmt.Errorf("Received unexpected continuation frame")
var ErrInvalidSize = fmt.Errorf("Received invalid payload size")
var ErrInvalidPayload = fmt.Errorf("Received invalid utf8 payload")
var ErrInvalidCloseStatus = fmt.Errorf("Received invalid close status")
var ErrInvalidOpCode = fmt.Errorf("Received invalid OpCode")
var ErrReservedBits = fmt.Errorf("Received reserved bits")
var CloseFrame = fmt.Errorf("Received close Frame")
var (
// ErrUnmaskedFrame error
ErrUnmaskedFrame = fmt.Errorf("Received unmasked frame")
// ErrTooLongControlFrame error
ErrTooLongControlFrame = fmt.Errorf("Received a control frame that is fragmented or too long")
// ErrInvalidFragment error
ErrInvalidFragment = fmt.Errorf("Received invalid fragmentation")
// ErrUnexpectedContinuation error
ErrUnexpectedContinuation = fmt.Errorf("Received unexpected continuation frame")
// ErrInvalidSize error
ErrInvalidSize = fmt.Errorf("Received invalid payload size")
// ErrInvalidPayload error
ErrInvalidPayload = fmt.Errorf("Received invalid utf8 payload")
// ErrInvalidCloseStatus error
ErrInvalidCloseStatus = fmt.Errorf("Received invalid close status")
// ErrInvalidOpCode error
ErrInvalidOpCode = fmt.Errorf("Received invalid OpCode")
// ErrReservedBits error
ErrReservedBits = fmt.Errorf("Received reserved bits")
// ErrCloseFrame error
ErrCloseFrame = fmt.Errorf("Received close Frame")
)
// Maximum Header Size = Final/OpCode + isMask/Length + Length + Mask
const maximumHeaderSize = 1 + 1 + 8 + 4
const maxWriteChunk = 0x7fff
// Lists websocket close status
// MessageError lists websocket close statuses
type MessageError uint16
const (
NONE MessageError = 0
NORMAL MessageError = 1000
GOING_AWAY MessageError = 1001
PROTOCOL_ERR MessageError = 1002
UNACCEPTABLE_OPCODE MessageError = 1003
INVALID_PAYLOAD MessageError = 1007 // utf8
MESSAGE_TOO_LARGE MessageError = 1009
// None used when there is no error
None MessageError = 0
// Normal error
Normal MessageError = 1000
// GoingAway error
GoingAway MessageError = 1001
// ProtocolError error
ProtocolError MessageError = 1002
// UnacceptableOpCode error
UnacceptableOpCode MessageError = 1003
// InvalidPayload error
InvalidPayload MessageError = 1007 // utf8
// MessageTooLarge error
MessageTooLarge MessageError = 1009
)
// Lists websocket message types
// MessageType lists websocket message types
type MessageType byte
const (
CONTINUATION MessageType = 0x00
TEXT MessageType = 0x01
BINARY MessageType = 0x02
CLOSE MessageType = 0x08
PING MessageType = 0x09
PONG MessageType = 0x0a
// Continuation message type
Continuation MessageType = 0x00
// Text message type
Text MessageType = 0x01
// Binary message type
Binary MessageType = 0x02
// Close message type
Close MessageType = 0x08
// Ping message type
Ping MessageType = 0x09
// Pong message type
Pong MessageType = 0x0a
)
// Represents a websocket message
// Message is a websocket message
type Message struct {
Final bool
Type MessageType
@ -248,38 +273,38 @@ func (m Message) Send(writer io.Writer) error {
func (m *Message) check(fragment bool) error {
/* (1) Invalid first fragment (not TEXT nor BINARY) */
if !m.Final && !fragment && m.Type != TEXT && m.Type != BINARY {
if !m.Final && !fragment && m.Type != Text && m.Type != Binary {
return ErrInvalidFragment
}
/* (2) Waiting fragment but received standalone frame */
if fragment && m.Type != CONTINUATION && m.Type != CLOSE && m.Type != PING && m.Type != PONG {
if fragment && m.Type != Continuation && m.Type != Close && m.Type != Ping && m.Type != Pong {
return ErrInvalidFragment
}
/* (3) Control frame too long */
if (m.Type == CLOSE || m.Type == PING || m.Type == PONG) && (m.Size > 125 || !m.Final) {
if (m.Type == Close || m.Type == Ping || m.Type == Pong) && (m.Size > 125 || !m.Final) {
return ErrTooLongControlFrame
}
switch m.Type {
case CONTINUATION:
case Continuation:
// unexpected continuation
if !fragment {
return ErrUnexpectedContinuation
}
return nil
case TEXT:
case Text:
if m.Final && !utf8.Valid(m.Data) {
return ErrInvalidPayload
}
return nil
case BINARY:
case Binary:
return nil
case CLOSE:
case Close:
// incomplete code
if m.Size == 1 {
return ErrInvalidCloseStatus
@ -298,12 +323,12 @@ func (m *Message) check(fragment bool) error {
return ErrInvalidCloseStatus
}
}
return CloseFrame
return ErrCloseFrame
case PING:
case Ping:
return nil
case PONG:
case Pong:
return nil
default:

View File

@ -41,25 +41,25 @@ func TestSimpleMessageReading(t *testing.T) {
{ // FIN ; TEXT ; hello
"simple hello text message",
[]byte{0x81, 0x85, 0x00, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
Message{true, TEXT, 5, []byte("hello")},
Message{true, Text, 5, []byte("hello")},
nil,
},
{ // FIN ; BINARY ; hello
"simple hello binary message",
[]byte{0x82, 0x85, 0x00, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
Message{true, BINARY, 5, []byte("hello")},
Message{true, Binary, 5, []byte("hello")},
nil,
},
{ // FIN ; BINARY ; test unmasking
"unmasking test",
[]byte{0x82, 0x88, 0x01, 0x02, 0x03, 0x04, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80},
Message{true, BINARY, 8, []byte{0x11, 0x22, 0x33, 0x44, 0x51, 0x62, 0x73, 0x84}},
Message{true, Binary, 8, []byte{0x11, 0x22, 0x33, 0x44, 0x51, 0x62, 0x73, 0x84}},
nil,
},
{ // FIN=0 ; TEXT ;
"non final frame",
[]byte{0x01, 0x82, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02},
Message{false, TEXT, 2, []byte{0x01, 0x02}},
Message{false, Text, 2, []byte{0x01, 0x02}},
nil,
},
}
@ -222,43 +222,43 @@ func TestSimpleMessageSending(t *testing.T) {
}{
{
"simple hello text message",
Message{true, TEXT, 5, []byte("hello")},
Message{true, Text, 5, []byte("hello")},
[]byte{0x81, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
}, {
"simple hello binary message",
Message{true, BINARY, 5, []byte("hello")},
Message{true, Binary, 5, []byte("hello")},
[]byte{0x82, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
}, {
"other simple binary message",
Message{true, BINARY, 8, []byte{0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}},
Message{true, Binary, 8, []byte{0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}},
[]byte{0x82, 0x08, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80},
}, {
"non final frame",
Message{false, TEXT, 2, []byte{0x01, 0x02}},
Message{false, Text, 2, []byte{0x01, 0x02}},
[]byte{0x01, 0x02, 0x01, 0x02},
}, {
"125 > normal length",
Message{true, TEXT, uint(len(m4b1)), m4b1},
Message{true, Text, uint(len(m4b1)), m4b1},
append([]byte{0x81, 0x7e - 1}, m4b1...),
}, {
"126 > extended 16 bits length",
Message{true, TEXT, uint(len(m4b2)), m4b2},
Message{true, Text, uint(len(m4b2)), m4b2},
append([]byte{0x81, 126, 0x00, 0x7e}, m4b2...),
}, {
"127 > extended 16 bits length",
Message{true, TEXT, uint(len(m4b3)), m4b3},
Message{true, Text, uint(len(m4b3)), m4b3},
append([]byte{0x81, 126, 0x00, 0x7e + 1}, m4b3...),
}, {
"fffe > extended 16 bits length",
Message{true, TEXT, uint(len(m16b1)), m16b1},
Message{true, Text, uint(len(m16b1)), m16b1},
append([]byte{0x81, 126, 0xff, 0xfe}, m16b1...),
}, {
"ffff > extended 16 bits length",
Message{true, TEXT, uint(len(m16b2)), m16b2},
Message{true, Text, uint(len(m16b2)), m16b2},
append([]byte{0x81, 126, 0xff, 0xff}, m16b2...),
}, {
"10000 > extended 64 bits length",
Message{true, TEXT, uint(len(m16b3)), m16b3},
Message{true, Text, uint(len(m16b3)), m16b3},
append([]byte{0x81, 127, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00}, m16b3...),
},
}
@ -305,22 +305,22 @@ func TestMessageCheck(t *testing.T) {
Cases: []Case{
{
"CONTINUATION must fail",
Message{false, CONTINUATION, 0, []byte{}}, false, ErrInvalidFragment,
Message{false, Continuation, 0, []byte{}}, false, ErrInvalidFragment,
}, {
"TEXT must not fail",
Message{false, TEXT, 0, []byte{}}, false, nil,
Message{false, Text, 0, []byte{}}, false, nil,
}, {
"BINARY must not fail",
Message{false, BINARY, 0, []byte{}}, false, nil,
Message{false, Binary, 0, []byte{}}, false, nil,
}, {
"CLOSE must fail",
Message{false, CLOSE, 0, []byte{}}, false, ErrInvalidFragment,
Message{false, Close, 0, []byte{}}, false, ErrInvalidFragment,
}, {
"PING must fail",
Message{false, PING, 0, []byte{}}, false, ErrInvalidFragment,
Message{false, Ping, 0, []byte{}}, false, ErrInvalidFragment,
}, {
"PONG must fail",
Message{false, PONG, 0, []byte{}}, false, ErrInvalidFragment,
Message{false, Pong, 0, []byte{}}, false, ErrInvalidFragment,
},
},
}, {
@ -328,22 +328,22 @@ func TestMessageCheck(t *testing.T) {
Cases: []Case{
{
"CONTINUATION must not fail",
Message{true, CONTINUATION, 0, []byte{}}, true, nil,
Message{true, Continuation, 0, []byte{}}, true, nil,
}, {
"TEXT must fail",
Message{true, TEXT, 0, []byte{}}, true, ErrInvalidFragment,
Message{true, Text, 0, []byte{}}, true, ErrInvalidFragment,
}, {
"BINARY must fail",
Message{true, BINARY, 0, []byte{}}, true, ErrInvalidFragment,
Message{true, Binary, 0, []byte{}}, true, ErrInvalidFragment,
}, {
"CLOSE must not fail",
Message{true, CLOSE, 0, []byte{}}, true, CloseFrame,
Message{true, Close, 0, []byte{}}, true, ErrCloseFrame,
}, {
"PING must not fail",
Message{true, PING, 0, []byte{}}, true, nil,
Message{true, Ping, 0, []byte{}}, true, nil,
}, {
"PONG must not fail",
Message{true, PONG, 0, []byte{}}, true, nil,
Message{true, Pong, 0, []byte{}}, true, nil,
},
},
}, {
@ -351,13 +351,13 @@ func TestMessageCheck(t *testing.T) {
Cases: []Case{
{
"CLOSE must not fail",
Message{true, CLOSE, 125, []byte{0x03, 0xe8, 0}}, false, CloseFrame,
Message{true, Close, 125, []byte{0x03, 0xe8, 0}}, false, ErrCloseFrame,
}, {
"PING must not fail",
Message{true, PING, 125, []byte{}}, false, nil,
Message{true, Ping, 125, []byte{}}, false, nil,
}, {
"PONG must not fail",
Message{true, PONG, 125, []byte{}}, false, nil,
Message{true, Pong, 125, []byte{}}, false, nil,
},
},
}, {
@ -365,13 +365,13 @@ func TestMessageCheck(t *testing.T) {
Cases: []Case{
{
"CLOSE must fail",
Message{true, CLOSE, 126, []byte{0x03, 0xe8, 0}}, false, ErrTooLongControlFrame,
Message{true, Close, 126, []byte{0x03, 0xe8, 0}}, false, ErrTooLongControlFrame,
}, {
"PING must fail",
Message{true, PING, 126, []byte{}}, false, ErrTooLongControlFrame,
Message{true, Ping, 126, []byte{}}, false, ErrTooLongControlFrame,
}, {
"PONG must fail",
Message{true, PONG, 126, []byte{}}, false, ErrTooLongControlFrame,
Message{true, Pong, 126, []byte{}}, false, ErrTooLongControlFrame,
},
},
}, {
@ -379,13 +379,13 @@ func TestMessageCheck(t *testing.T) {
Cases: []Case{
{
"CLOSE must fail",
Message{false, CLOSE, 126, []byte{0x03, 0xe8, 0}}, false, ErrInvalidFragment,
Message{false, Close, 126, []byte{0x03, 0xe8, 0}}, false, ErrInvalidFragment,
}, {
"PING must fail",
Message{false, PING, 126, []byte{}}, false, ErrInvalidFragment,
Message{false, Ping, 126, []byte{}}, false, ErrInvalidFragment,
}, {
"PONG must fail",
Message{false, PONG, 126, []byte{}}, false, ErrInvalidFragment,
Message{false, Pong, 126, []byte{}}, false, ErrInvalidFragment,
},
},
}, {
@ -393,10 +393,10 @@ func TestMessageCheck(t *testing.T) {
Cases: []Case{
{
"no waiting fragment final",
Message{false, CONTINUATION, 126, nil}, false, ErrInvalidFragment,
Message{false, Continuation, 126, nil}, false, ErrInvalidFragment,
}, {
"no waiting fragment non-final",
Message{true, CONTINUATION, 126, nil}, false, ErrUnexpectedContinuation,
Message{true, Continuation, 126, nil}, false, ErrUnexpectedContinuation,
},
},
}, {
@ -404,23 +404,23 @@ func TestMessageCheck(t *testing.T) {
Cases: []Case{
{
"CLOSE valid reason",
Message{true, CLOSE, 5, []byte{0x03, 0xe8, 0xe2, 0x82, 0xa1}}, false, CloseFrame,
Message{true, Close, 5, []byte{0x03, 0xe8, 0xe2, 0x82, 0xa1}}, false, ErrCloseFrame,
}, {
"CLOSE invalid reason byte 2",
Message{true, CLOSE, 5, []byte{0x03, 0xe8, 0xe2, 0x28, 0xa1}}, false, ErrInvalidPayload,
Message{true, Close, 5, []byte{0x03, 0xe8, 0xe2, 0x28, 0xa1}}, false, ErrInvalidPayload,
}, {
"CLOSE invalid reason byte 3",
Message{true, CLOSE, 5, []byte{0x03, 0xe8, 0xe2, 0x82, 0x28}}, false, ErrInvalidPayload,
Message{true, Close, 5, []byte{0x03, 0xe8, 0xe2, 0x82, 0x28}}, false, ErrInvalidPayload,
},
{
"TEXT valid reason",
Message{true, TEXT, 3, []byte{0xe2, 0x82, 0xa1}}, false, nil,
Message{true, Text, 3, []byte{0xe2, 0x82, 0xa1}}, false, nil,
}, {
"TEXT invalid reason byte 2",
Message{true, TEXT, 3, []byte{0xe2, 0x28, 0xa1}}, false, ErrInvalidPayload,
Message{true, Text, 3, []byte{0xe2, 0x28, 0xa1}}, false, ErrInvalidPayload,
}, {
"TEXT invalid reason byte 3",
Message{true, TEXT, 3, []byte{0xe2, 0x82, 0x28}}, false, ErrInvalidPayload,
Message{true, Text, 3, []byte{0xe2, 0x82, 0x28}}, false, ErrInvalidPayload,
},
},
}, {
@ -428,82 +428,82 @@ func TestMessageCheck(t *testing.T) {
Cases: []Case{
{
"CLOSE only 1 byte",
Message{true, CLOSE, 1, []byte{0x03}}, false, ErrInvalidCloseStatus,
Message{true, Close, 1, []byte{0x03}}, false, ErrInvalidCloseStatus,
}, {
"valid CLOSE status 1000",
Message{true, CLOSE, 2, []byte{0x03, 0xe8}}, false, CloseFrame,
Message{true, Close, 2, []byte{0x03, 0xe8}}, false, ErrCloseFrame,
}, {
"invalid CLOSE status 999 under 1000",
Message{true, CLOSE, 2, []byte{0x03, 0xe7}}, false, ErrInvalidCloseStatus,
Message{true, Close, 2, []byte{0x03, 0xe7}}, false, ErrInvalidCloseStatus,
}, {
"valid CLOSE status 1001",
Message{true, CLOSE, 2, []byte{0x03, 0xe9}}, false, CloseFrame,
Message{true, Close, 2, []byte{0x03, 0xe9}}, false, ErrCloseFrame,
}, {
"valid CLOSE status 1002",
Message{true, CLOSE, 2, []byte{0x03, 0xea}}, false, CloseFrame,
Message{true, Close, 2, []byte{0x03, 0xea}}, false, ErrCloseFrame,
}, {
"valid CLOSE status 1003",
Message{true, CLOSE, 2, []byte{0x03, 0xeb}}, false, CloseFrame,
Message{true, Close, 2, []byte{0x03, 0xeb}}, false, ErrCloseFrame,
}, {
"invalid CLOSE status 1004",
Message{true, CLOSE, 2, []byte{0x03, 0xec}}, false, ErrInvalidCloseStatus,
Message{true, Close, 2, []byte{0x03, 0xec}}, false, ErrInvalidCloseStatus,
}, {
"invalid CLOSE status 1005",
Message{true, CLOSE, 2, []byte{0x03, 0xed}}, false, ErrInvalidCloseStatus,
Message{true, Close, 2, []byte{0x03, 0xed}}, false, ErrInvalidCloseStatus,
}, {
"invalid CLOSE status 1006",
Message{true, CLOSE, 2, []byte{0x03, 0xee}}, false, ErrInvalidCloseStatus,
Message{true, Close, 2, []byte{0x03, 0xee}}, false, ErrInvalidCloseStatus,
}, {
"valid CLOSE status 1007",
Message{true, CLOSE, 2, []byte{0x03, 0xef}}, false, CloseFrame,
Message{true, Close, 2, []byte{0x03, 0xef}}, false, ErrCloseFrame,
}, {
"valid CLOSE status 1011",
Message{true, CLOSE, 2, []byte{0x03, 0xf3}}, false, CloseFrame,
Message{true, Close, 2, []byte{0x03, 0xf3}}, false, ErrCloseFrame,
}, {
"invalid CLOSE status 1012",
Message{true, CLOSE, 2, []byte{0x03, 0xf4}}, false, ErrInvalidCloseStatus,
Message{true, Close, 2, []byte{0x03, 0xf4}}, false, ErrInvalidCloseStatus,
}, {
"invalid CLOSE status 1013",
Message{true, CLOSE, 2, []byte{0x03, 0xf5}}, false, ErrInvalidCloseStatus,
Message{true, Close, 2, []byte{0x03, 0xf5}}, false, ErrInvalidCloseStatus,
}, {
"invalid CLOSE status 1014",
Message{true, CLOSE, 2, []byte{0x03, 0xf6}}, false, ErrInvalidCloseStatus,
Message{true, Close, 2, []byte{0x03, 0xf6}}, false, ErrInvalidCloseStatus,
}, {
"invalid CLOSE status 1015",
Message{true, CLOSE, 2, []byte{0x03, 0xf7}}, false, ErrInvalidCloseStatus,
Message{true, Close, 2, []byte{0x03, 0xf7}}, false, ErrInvalidCloseStatus,
}, {
"invalid CLOSE status 1016",
Message{true, CLOSE, 2, []byte{0x03, 0xf8}}, false, ErrInvalidCloseStatus,
Message{true, Close, 2, []byte{0x03, 0xf8}}, false, ErrInvalidCloseStatus,
}, {
"valid CLOSE status 1017",
Message{true, CLOSE, 2, []byte{0x03, 0xf9}}, false, CloseFrame,
Message{true, Close, 2, []byte{0x03, 0xf9}}, false, ErrCloseFrame,
}, {
"valid CLOSE status 1099",
Message{true, CLOSE, 2, []byte{0x04, 0x4b}}, false, CloseFrame,
Message{true, Close, 2, []byte{0x04, 0x4b}}, false, ErrCloseFrame,
}, {
"invalid CLOSE status 1100",
Message{true, CLOSE, 2, []byte{0x04, 0x4c}}, false, ErrInvalidCloseStatus,
Message{true, Close, 2, []byte{0x04, 0x4c}}, false, ErrInvalidCloseStatus,
}, {
"valid CLOSE status 1101",
Message{true, CLOSE, 2, []byte{0x04, 0x4d}}, false, CloseFrame,
Message{true, Close, 2, []byte{0x04, 0x4d}}, false, ErrCloseFrame,
}, {
"valid CLOSE status 1999",
Message{true, CLOSE, 2, []byte{0x07, 0xcf}}, false, CloseFrame,
Message{true, Close, 2, []byte{0x07, 0xcf}}, false, ErrCloseFrame,
}, {
"invalid CLOSE status 2000",
Message{true, CLOSE, 2, []byte{0x07, 0xd0}}, false, ErrInvalidCloseStatus,
Message{true, Close, 2, []byte{0x07, 0xd0}}, false, ErrInvalidCloseStatus,
}, {
"valid CLOSE status 2001",
Message{true, CLOSE, 2, []byte{0x07, 0xd1}}, false, CloseFrame,
Message{true, Close, 2, []byte{0x07, 0xd1}}, false, ErrCloseFrame,
}, {
"valid CLOSE status 2998",
Message{true, CLOSE, 2, []byte{0x0b, 0xb6}}, false, CloseFrame,
Message{true, Close, 2, []byte{0x0b, 0xb6}}, false, ErrCloseFrame,
}, {
"invalid CLOSE status 2999",
Message{true, CLOSE, 2, []byte{0x0b, 0xb7}}, false, ErrInvalidCloseStatus,
Message{true, Close, 2, []byte{0x0b, 0xb7}}, false, ErrInvalidCloseStatus,
}, {
"valid CLOSE status 3000",
Message{true, CLOSE, 2, []byte{0x0b, 0xb8}}, false, CloseFrame,
Message{true, Close, 2, []byte{0x0b, 0xb8}}, false, ErrCloseFrame,
},
},
}, {
@ -517,7 +517,7 @@ func TestMessageCheck(t *testing.T) {
{"5", Message{true, 5, 0, []byte{}}, false, ErrInvalidOpCode},
{"6", Message{true, 6, 0, []byte{}}, false, ErrInvalidOpCode},
{"7", Message{true, 7, 0, []byte{}}, false, ErrInvalidOpCode},
{"8", Message{true, 8, 0, []byte{}}, false, CloseFrame},
{"8", Message{true, 8, 0, []byte{}}, false, ErrCloseFrame},
{"9", Message{true, 9, 0, []byte{}}, false, nil},
{"10", Message{true, 10, 0, []byte{}}, false, nil},
{"11", Message{true, 11, 0, []byte{}}, false, ErrInvalidOpCode},

View File

@ -2,18 +2,19 @@ package websocket
import (
"fmt"
"git.xdrm.io/go/websocket/internal/uri/parser"
"net"
"git.xdrm.io/go/ws/internal/uri/parser"
)
// Represents all channels that need a server
// All channels that a server features
type serverChannelSet struct {
register chan *client
unregister chan *client
broadcast chan Message
}
// Represents a websocket server
// Server is a websocket server
type Server struct {
sock net.Listener // listen socket
addr []byte // server listening ip/host
@ -26,7 +27,7 @@ type Server struct {
ch serverChannelSet
}
// CreateServer creates a server for a specific HOST and PORT
// CreateServer for a specific HOST and PORT
func CreateServer(host string, port uint16) *Server {
return &Server{
@ -37,7 +38,7 @@ func CreateServer(host string, port uint16) *Server {
ctl: ControllerSet{
Def: nil,
Uri: make([]*Controller, 0),
URI: make([]*Controller, 0),
},
ch: serverChannelSet{
@ -61,7 +62,7 @@ func (s *Server) BindDefault(f ControllerFunc) {
}
// Bind binds a controller to an URI scheme
// Bind a controller to an URI scheme
func (s *Server) Bind(uri string, f ControllerFunc) error {
/* (1) Build URI parser */
@ -71,7 +72,7 @@ func (s *Server) Bind(uri string, f ControllerFunc) error {
}
/* (2) Create controller */
s.ctl.Uri = append(s.ctl.Uri, &Controller{
s.ctl.URI = append(s.ctl.URI, &Controller{
URI: uriScheme,
Fun: f,
})
@ -80,7 +81,7 @@ func (s *Server) Bind(uri string, f ControllerFunc) error {
}
// Launch launches the websocket server
// Launch the websocket server
func (s *Server) Launch() error {
var err error
@ -140,23 +141,16 @@ func (s *Server) scheduler() {
select {
/* (1) New client */
/* (1) Create client */
case client := <-s.ch.register:
// fmt.Printf(" + client\n")
s.clients[client.io.sock] = client
/* (2) New client */
/* (2) Remove client */
case client := <-s.ch.unregister:
// fmt.Printf(" - client\n")
delete(s.clients, client.io.sock)
/* (3) Broadcast */
case msg := <-s.ch.broadcast:
fmt.Printf(" + broadcast\n")
for _, c := range s.clients {
c.ch.send <- msg
}
@ -165,6 +159,4 @@ func (s *Server) scheduler() {
}
fmt.Printf("+ server stopped\n")
}