refactor: move to go modules and lint
This commit is contained in:
parent
7fb4241187
commit
db52cfd28f
33
client.go
33
client.go
|
@ -4,10 +4,11 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.xdrm.io/go/websocket/internal/http/upgrade/request"
|
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.xdrm.io/go/ws/internal/http/upgrade/request"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Represents a client socket utility (reader, writer, ..)
|
// 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) {
|
func clientReader(c *client) {
|
||||||
var frag *Message
|
var frag *Message
|
||||||
|
|
||||||
closeStatus := NORMAL
|
closeStatus := Normal
|
||||||
clientAck := true
|
clientAck := true
|
||||||
|
|
||||||
c.io.reading.Add(1)
|
c.io.reading.Add(1)
|
||||||
|
@ -136,7 +137,7 @@ func clientReader(c *client) {
|
||||||
msg, err := readMessage(c.io.reader)
|
msg, err := readMessage(c.io.reader)
|
||||||
|
|
||||||
if err == ErrUnmaskedFrame || err == ErrReservedBits {
|
if err == ErrUnmaskedFrame || err == ErrReservedBits {
|
||||||
closeStatus = PROTOCOL_ERR
|
closeStatus = ProtocolError
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
|
@ -152,25 +153,25 @@ func clientReader(c *client) {
|
||||||
|
|
||||||
// Fail
|
// Fail
|
||||||
case ErrUnexpectedContinuation:
|
case ErrUnexpectedContinuation:
|
||||||
closeStatus = NONE
|
closeStatus = None
|
||||||
clientAck = false
|
clientAck = false
|
||||||
mustClose = true
|
mustClose = true
|
||||||
|
|
||||||
// proper close
|
// proper close
|
||||||
case CloseFrame:
|
case ErrCloseFrame:
|
||||||
closeStatus = NORMAL
|
closeStatus = Normal
|
||||||
clientAck = true
|
clientAck = true
|
||||||
mustClose = true
|
mustClose = true
|
||||||
|
|
||||||
// invalid payload proper close
|
// invalid payload proper close
|
||||||
case ErrInvalidPayload:
|
case ErrInvalidPayload:
|
||||||
closeStatus = INVALID_PAYLOAD
|
closeStatus = InvalidPayload
|
||||||
clientAck = true
|
clientAck = true
|
||||||
mustClose = true
|
mustClose = true
|
||||||
|
|
||||||
// any other error -> protocol error
|
// any other error -> protocol error
|
||||||
default:
|
default:
|
||||||
closeStatus = PROTOCOL_ERR
|
closeStatus = ProtocolError
|
||||||
clientAck = true
|
clientAck = true
|
||||||
mustClose = true
|
mustClose = true
|
||||||
}
|
}
|
||||||
|
@ -182,9 +183,9 @@ func clientReader(c *client) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (4) Ping <-> Pong */
|
/* (4) Ping <-> Pong */
|
||||||
if msg.Type == PING && c.io.writing {
|
if msg.Type == Ping && c.io.writing {
|
||||||
msg.Final = true
|
msg.Final = true
|
||||||
msg.Type = PONG
|
msg.Type = Pong
|
||||||
c.ch.send <- *msg
|
c.ch.send <- *msg
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -213,10 +214,10 @@ func clientReader(c *client) {
|
||||||
// check message errors
|
// check message errors
|
||||||
fragErr := frag.check(false)
|
fragErr := frag.check(false)
|
||||||
if fragErr == ErrInvalidPayload {
|
if fragErr == ErrInvalidPayload {
|
||||||
closeStatus = INVALID_PAYLOAD
|
closeStatus = InvalidPayload
|
||||||
break
|
break
|
||||||
} else if fragErr != nil {
|
} else if fragErr != nil {
|
||||||
closeStatus = PROTOCOL_ERR
|
closeStatus = ProtocolError
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -226,7 +227,7 @@ func clientReader(c *client) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (7) Dispatch to receiver */
|
/* (7) Dispatch to receiver */
|
||||||
if msg.Type == TEXT || msg.Type == BINARY {
|
if msg.Type == Text || msg.Type == Binary {
|
||||||
c.ch.receive <- *msg
|
c.ch.receive <- *msg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,7 +266,7 @@ func clientWriter(c *client) {
|
||||||
|
|
||||||
/* (4) close channel (if not already done) */
|
/* (4) close channel (if not already done) */
|
||||||
// fmt.Printf("[writer] end\n")
|
// 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.sock.SetReadDeadline(time.Now().Add(time.Second * -1))
|
||||||
c.io.reading.Wait()
|
c.io.reading.Wait()
|
||||||
|
|
||||||
if status != NONE {
|
if status != None {
|
||||||
|
|
||||||
/* (3) Build message */
|
/* (3) Build message */
|
||||||
msg := &Message{
|
msg := &Message{
|
||||||
Final: true,
|
Final: true,
|
||||||
Type: CLOSE,
|
Type: Close,
|
||||||
Size: 2,
|
Size: 2,
|
||||||
Data: make([]byte, 2),
|
Data: make([]byte, 2),
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,9 @@ package iface
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
ws "git.xdrm.io/go/websocket"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
ws "git.xdrm.io/go/ws"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -1,29 +1,29 @@
|
||||||
package websocket
|
package websocket
|
||||||
|
|
||||||
import (
|
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 {
|
type Client struct {
|
||||||
Protocol string // choosen protocol (Sec-WebSocket-Protocol)
|
Protocol string // choosen protocol (Sec-WebSocket-Protocol)
|
||||||
Arguments [][]string // URI parameters, index 0 is full URI, then matching groups
|
Arguments [][]string // URI parameters, index 0 is full URI, then matching groups
|
||||||
Store interface{} // store (for client implementation-specific data)
|
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)
|
type ControllerFunc func(*Client, <-chan Message, chan<- Message, chan<- Message)
|
||||||
|
|
||||||
// Represents a websocket controller
|
// Controller is a websocket controller
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
URI *parser.Scheme // uri scheme
|
URI *parser.Scheme // uri scheme
|
||||||
Fun ControllerFunc // controller function
|
Fun ControllerFunc // controller function
|
||||||
}
|
}
|
||||||
|
|
||||||
// Represents a controller set
|
// ControllerSet is set of controllers
|
||||||
type ControllerSet struct {
|
type ControllerSet struct {
|
||||||
Def *Controller // default controller
|
Def *Controller // default controller
|
||||||
Uri []*Controller // uri controllers
|
URI []*Controller // uri controllers
|
||||||
}
|
}
|
||||||
|
|
||||||
// Match finds a controller for a given URI
|
// Match finds a controller for a given URI
|
||||||
|
@ -31,10 +31,10 @@ type ControllerSet struct {
|
||||||
func (s *ControllerSet) Match(uri string) (*Controller, [][]string) {
|
func (s *ControllerSet) Match(uri string) (*Controller, [][]string) {
|
||||||
|
|
||||||
/* (1) Initialise argument list */
|
/* (1) Initialise argument list */
|
||||||
arguments := [][]string{[]string{uri}}
|
arguments := [][]string{{uri}}
|
||||||
|
|
||||||
/* (2) Try each controller */
|
/* (2) Try each controller */
|
||||||
for _, c := range s.Uri {
|
for _, c := range s.URI {
|
||||||
|
|
||||||
/* 1. If matches */
|
/* 1. If matches */
|
||||||
if c.URI.Match(uri) {
|
if c.URI.Match(uri) {
|
||||||
|
|
|
@ -6,37 +6,34 @@ package reader
|
||||||
// the golang standard library
|
// the golang standard library
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"bufio"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Maximum line length
|
// Maximum line length
|
||||||
var maxLineLength = 4096
|
var maxLineLength = 4096
|
||||||
|
|
||||||
// Chunk reader
|
// ChunkReader struct
|
||||||
type chunkReader struct {
|
type ChunkReader struct {
|
||||||
reader *bufio.Reader // the reader
|
reader *bufio.Reader // the reader
|
||||||
isEnded bool // If we are done (2 consecutive CRLF)
|
isEnded bool // If we are done (2 consecutive CRLF)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewReader creates a new reader
|
||||||
// New creates a new reader
|
func NewReader(r io.Reader) *ChunkReader {
|
||||||
func NewReader(r io.Reader) (reader *chunkReader) {
|
|
||||||
|
|
||||||
br, ok := r.(*bufio.Reader)
|
br, ok := r.(*bufio.Reader)
|
||||||
if !ok {
|
if !ok {
|
||||||
br = bufio.NewReader(r)
|
br = bufio.NewReader(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &chunkReader{reader: br}
|
return &ChunkReader{reader: br}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Read reads a chunk, err is io.EOF when done
|
// 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 */
|
/* (1) If already ended */
|
||||||
if r.isEnded {
|
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]) {
|
for len(b) > 0 && isASCIISpace(b[len(b)-1]) {
|
||||||
b = b[:len(b)-1]
|
b = b[:len(b)-1]
|
||||||
}
|
}
|
||||||
|
@ -83,5 +78,5 @@ func removeTrailingSpace(b []byte) []byte{
|
||||||
}
|
}
|
||||||
|
|
||||||
func isASCIISpace(b byte) bool {
|
func isASCIISpace(b byte) bool {
|
||||||
return b == ' ' || b == '\t' || b == '\r' || b =='\n'
|
return b == ' ' || b == '\t' || b == '\r' || b == '\n'
|
||||||
}
|
}
|
|
@ -2,10 +2,11 @@ package request
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.xdrm.io/go/websocket/internal/http/upgrade/request/parser/header"
|
|
||||||
"git.xdrm.io/go/websocket/internal/http/upgrade/response"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"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
|
// checkHost checks and extracts the Host header
|
||||||
|
@ -31,7 +32,7 @@ func (r *T) extractHostPort(bb header.HeaderValue) error {
|
||||||
// extract port
|
// extract port
|
||||||
readPort, err := strconv.ParseUint(split[1], 10, 16)
|
readPort, err := strconv.ParseUint(split[1], 10, 16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.code = response.BAD_REQUEST
|
r.code = response.BadRequest
|
||||||
return &InvalidRequest{"Host", "cannot read port"}
|
return &InvalidRequest{"Host", "cannot read port"}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,7 +42,7 @@ func (r *T) extractHostPort(bb header.HeaderValue) error {
|
||||||
if len(r.origin) > 0 {
|
if len(r.origin) > 0 {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = r.checkOriginPolicy()
|
err = r.checkOriginPolicy()
|
||||||
r.code = response.FORBIDDEN
|
r.code = response.Forbidden
|
||||||
return &InvalidOriginPolicy{r.host, r.origin, err}
|
return &InvalidOriginPolicy{r.host, r.origin, err}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,7 +60,7 @@ func (r *T) extractOrigin(bb header.HeaderValue) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(bb) != 1 {
|
if len(bb) != 1 {
|
||||||
r.code = response.FORBIDDEN
|
r.code = response.Forbidden
|
||||||
return &InvalidRequest{"Origin", fmt.Sprintf("expected single value, got %d", len(bb))}
|
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 {
|
if len(r.host) > 0 {
|
||||||
err := r.checkOriginPolicy()
|
err := r.checkOriginPolicy()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.code = response.FORBIDDEN
|
r.code = response.Forbidden
|
||||||
return &InvalidOriginPolicy{r.host, r.origin, err}
|
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)"}
|
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 {
|
func (r *T) checkUpgrade(bb header.HeaderValue) error {
|
||||||
|
|
||||||
if len(bb) != 1 {
|
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))}
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
r.code = response.BAD_REQUEST
|
r.code = response.BadRequest
|
||||||
return &InvalidRequest{"Upgrade", fmt.Sprintf("expected 'websocket' (case insensitive), got '%s'", bb[0])}
|
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 {
|
func (r *T) checkVersion(bb header.HeaderValue) error {
|
||||||
|
|
||||||
if len(bb) != 1 || string(bb[0]) != "13" {
|
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])}
|
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 {
|
func (r *T) extractKey(bb header.HeaderValue) error {
|
||||||
|
|
||||||
if len(bb) != 1 || len(bb[0]) != 24 {
|
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]))}
|
return &InvalidRequest{"Sec-WebSocket-Key", fmt.Sprintf("expected 24 bytes base64, got %d bytes", len(bb[0]))}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,9 @@ package request
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"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
|
// parseHeader parses any http request line
|
||||||
|
@ -17,7 +18,7 @@ func (r *T) parseHeader(b []byte) error {
|
||||||
err := r.request.Parse(b)
|
err := r.request.Parse(b)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.code = response.BAD_REQUEST
|
r.code = response.BadRequest
|
||||||
return &InvalidRequest{"Request-Line", err.Error()}
|
return &InvalidRequest{"Request-Line", err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +32,7 @@ func (r *T) parseHeader(b []byte) error {
|
||||||
/* (1) Try to parse header */
|
/* (1) Try to parse header */
|
||||||
head, err := header.Parse(b)
|
head, err := header.Parse(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.code = response.BAD_REQUEST
|
r.code = response.BadRequest
|
||||||
return fmt.Errorf("Error parsing header: %s", err)
|
return fmt.Errorf("Error parsing header: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,10 @@ package request
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.xdrm.io/go/websocket/internal/http/reader"
|
|
||||||
"git.xdrm.io/go/websocket/internal/http/upgrade/response"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"git.xdrm.io/go/ws/internal/http/reader"
|
||||||
|
"git.xdrm.io/go/ws/internal/http/upgrade/response"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Parse builds an upgrade HTTP request
|
// Parse builds an upgrade HTTP request
|
||||||
|
@ -45,11 +46,11 @@ func Parse(r io.Reader) (request *T, err error) {
|
||||||
/* (3) Check completion */
|
/* (3) Check completion */
|
||||||
err = req.isComplete()
|
err = req.isComplete()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
req.code = response.BAD_REQUEST
|
req.code = response.BadRequest
|
||||||
return req, err
|
return req, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.code = response.SWITCHING_PROTOCOLS
|
req.code = response.SwitchingProtocols
|
||||||
return req, nil
|
return req, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package request
|
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
|
// If origin is required
|
||||||
const bypassOriginPolicy = true
|
const bypassOriginPolicy = true
|
||||||
|
|
|
@ -12,7 +12,7 @@ func (r *T) SetStatusCode(sc StatusCode) {
|
||||||
r.code = sc
|
r.code = sc
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetProtocols sets the protocols
|
// SetProtocol sets the protocols
|
||||||
func (r *T) SetProtocol(p []byte) {
|
func (r *T) SetProtocol(p []byte) {
|
||||||
r.protocol = p
|
r.protocol = p
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ func (r *T) ProcessKey(k []byte) {
|
||||||
func (r T) Send(w io.Writer) (int, error) {
|
func (r T) Send(w io.Writer) (int, error) {
|
||||||
|
|
||||||
/* (1) Build response line */
|
/* (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 */
|
/* (2) Build headers */
|
||||||
optionalProtocol := ""
|
optionalProtocol := ""
|
||||||
|
|
|
@ -3,27 +3,35 @@ package response
|
||||||
// StatusCode maps the status codes (and description)
|
// StatusCode maps the status codes (and description)
|
||||||
type StatusCode uint16
|
type StatusCode uint16
|
||||||
|
|
||||||
var SWITCHING_PROTOCOLS StatusCode = 101 // handshake success
|
var (
|
||||||
var BAD_REQUEST StatusCode = 400 // missing/malformed headers
|
// SwitchingProtocols - handshake success
|
||||||
var FORBIDDEN StatusCode = 403 // invalid origin policy, TLS required
|
SwitchingProtocols StatusCode = 101
|
||||||
var UPGRADE_REQUIRED StatusCode = 426 // invalid WS version
|
// BadRequest - missing/malformed headers
|
||||||
var NOT_FOUND StatusCode = 404 // unserved or invalid URI
|
BadRequest StatusCode = 400
|
||||||
var INTERNAL StatusCode = 500 // custom error
|
// Forbidden - invalid origin policy, TLS required
|
||||||
|
Forbidden StatusCode = 403
|
||||||
func (sc StatusCode) Message() string {
|
// 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 {
|
switch sc {
|
||||||
case SWITCHING_PROTOCOLS:
|
case SwitchingProtocols:
|
||||||
return "Switching Protocols"
|
return "Switching Protocols"
|
||||||
case BAD_REQUEST:
|
case BadRequest:
|
||||||
return "Bad Request"
|
return "Bad Request"
|
||||||
case FORBIDDEN:
|
case Forbidden:
|
||||||
return "Forbidden"
|
return "Forbidden"
|
||||||
case UPGRADE_REQUIRED:
|
case UpgradeRequired:
|
||||||
return "Upgrade Required"
|
return "Upgrade Required"
|
||||||
case NOT_FOUND:
|
case NotFound:
|
||||||
return "Not Found"
|
return "Not Found"
|
||||||
case INTERNAL:
|
case Internal:
|
||||||
return "Internal Server Error"
|
return "Internal Server Error"
|
||||||
default:
|
default:
|
||||||
return "Unknown Status Code"
|
return "Unknown Status Code"
|
||||||
|
|
97
message.go
97
message.go
|
@ -7,47 +7,72 @@ import (
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrUnmaskedFrame = fmt.Errorf("Received unmasked frame")
|
var (
|
||||||
var ErrTooLongControlFrame = fmt.Errorf("Received a control frame that is fragmented or too long")
|
// ErrUnmaskedFrame error
|
||||||
var ErrInvalidFragment = fmt.Errorf("Received invalid fragmentation")
|
ErrUnmaskedFrame = fmt.Errorf("Received unmasked frame")
|
||||||
var ErrUnexpectedContinuation = fmt.Errorf("Received unexpected continuation frame")
|
// ErrTooLongControlFrame error
|
||||||
var ErrInvalidSize = fmt.Errorf("Received invalid payload size")
|
ErrTooLongControlFrame = fmt.Errorf("Received a control frame that is fragmented or too long")
|
||||||
var ErrInvalidPayload = fmt.Errorf("Received invalid utf8 payload")
|
// ErrInvalidFragment error
|
||||||
var ErrInvalidCloseStatus = fmt.Errorf("Received invalid close status")
|
ErrInvalidFragment = fmt.Errorf("Received invalid fragmentation")
|
||||||
var ErrInvalidOpCode = fmt.Errorf("Received invalid OpCode")
|
// ErrUnexpectedContinuation error
|
||||||
var ErrReservedBits = fmt.Errorf("Received reserved bits")
|
ErrUnexpectedContinuation = fmt.Errorf("Received unexpected continuation frame")
|
||||||
var CloseFrame = fmt.Errorf("Received close 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
|
// Maximum Header Size = Final/OpCode + isMask/Length + Length + Mask
|
||||||
const maximumHeaderSize = 1 + 1 + 8 + 4
|
const maximumHeaderSize = 1 + 1 + 8 + 4
|
||||||
const maxWriteChunk = 0x7fff
|
const maxWriteChunk = 0x7fff
|
||||||
|
|
||||||
// Lists websocket close status
|
// MessageError lists websocket close statuses
|
||||||
type MessageError uint16
|
type MessageError uint16
|
||||||
|
|
||||||
const (
|
const (
|
||||||
NONE MessageError = 0
|
// None used when there is no error
|
||||||
NORMAL MessageError = 1000
|
None MessageError = 0
|
||||||
GOING_AWAY MessageError = 1001
|
// Normal error
|
||||||
PROTOCOL_ERR MessageError = 1002
|
Normal MessageError = 1000
|
||||||
UNACCEPTABLE_OPCODE MessageError = 1003
|
// GoingAway error
|
||||||
INVALID_PAYLOAD MessageError = 1007 // utf8
|
GoingAway MessageError = 1001
|
||||||
MESSAGE_TOO_LARGE MessageError = 1009
|
// 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
|
type MessageType byte
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CONTINUATION MessageType = 0x00
|
// Continuation message type
|
||||||
TEXT MessageType = 0x01
|
Continuation MessageType = 0x00
|
||||||
BINARY MessageType = 0x02
|
// Text message type
|
||||||
CLOSE MessageType = 0x08
|
Text MessageType = 0x01
|
||||||
PING MessageType = 0x09
|
// Binary message type
|
||||||
PONG MessageType = 0x0a
|
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 {
|
type Message struct {
|
||||||
Final bool
|
Final bool
|
||||||
Type MessageType
|
Type MessageType
|
||||||
|
@ -248,38 +273,38 @@ func (m Message) Send(writer io.Writer) error {
|
||||||
func (m *Message) check(fragment bool) error {
|
func (m *Message) check(fragment bool) error {
|
||||||
|
|
||||||
/* (1) Invalid first fragment (not TEXT nor BINARY) */
|
/* (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
|
return ErrInvalidFragment
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (2) Waiting fragment but received standalone frame */
|
/* (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
|
return ErrInvalidFragment
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (3) Control frame too long */
|
/* (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
|
return ErrTooLongControlFrame
|
||||||
}
|
}
|
||||||
|
|
||||||
switch m.Type {
|
switch m.Type {
|
||||||
case CONTINUATION:
|
case Continuation:
|
||||||
// unexpected continuation
|
// unexpected continuation
|
||||||
if !fragment {
|
if !fragment {
|
||||||
return ErrUnexpectedContinuation
|
return ErrUnexpectedContinuation
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case TEXT:
|
case Text:
|
||||||
if m.Final && !utf8.Valid(m.Data) {
|
if m.Final && !utf8.Valid(m.Data) {
|
||||||
return ErrInvalidPayload
|
return ErrInvalidPayload
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case BINARY:
|
case Binary:
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case CLOSE:
|
case Close:
|
||||||
// incomplete code
|
// incomplete code
|
||||||
if m.Size == 1 {
|
if m.Size == 1 {
|
||||||
return ErrInvalidCloseStatus
|
return ErrInvalidCloseStatus
|
||||||
|
@ -298,12 +323,12 @@ func (m *Message) check(fragment bool) error {
|
||||||
return ErrInvalidCloseStatus
|
return ErrInvalidCloseStatus
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return CloseFrame
|
return ErrCloseFrame
|
||||||
|
|
||||||
case PING:
|
case Ping:
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case PONG:
|
case Pong:
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
140
message_test.go
140
message_test.go
|
@ -41,25 +41,25 @@ func TestSimpleMessageReading(t *testing.T) {
|
||||||
{ // FIN ; TEXT ; hello
|
{ // FIN ; TEXT ; hello
|
||||||
"simple hello text message",
|
"simple hello text message",
|
||||||
[]byte{0x81, 0x85, 0x00, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
[]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,
|
nil,
|
||||||
},
|
},
|
||||||
{ // FIN ; BINARY ; hello
|
{ // FIN ; BINARY ; hello
|
||||||
"simple hello binary message",
|
"simple hello binary message",
|
||||||
[]byte{0x82, 0x85, 0x00, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
[]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,
|
nil,
|
||||||
},
|
},
|
||||||
{ // FIN ; BINARY ; test unmasking
|
{ // FIN ; BINARY ; test unmasking
|
||||||
"unmasking test",
|
"unmasking test",
|
||||||
[]byte{0x82, 0x88, 0x01, 0x02, 0x03, 0x04, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80},
|
[]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,
|
nil,
|
||||||
},
|
},
|
||||||
{ // FIN=0 ; TEXT ;
|
{ // FIN=0 ; TEXT ;
|
||||||
"non final frame",
|
"non final frame",
|
||||||
[]byte{0x01, 0x82, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02},
|
[]byte{0x01, 0x82, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02},
|
||||||
Message{false, TEXT, 2, []byte{0x01, 0x02}},
|
Message{false, Text, 2, []byte{0x01, 0x02}},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -222,43 +222,43 @@ func TestSimpleMessageSending(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"simple hello text message",
|
"simple hello text message",
|
||||||
Message{true, TEXT, 5, []byte("hello")},
|
Message{true, Text, 5, []byte("hello")},
|
||||||
[]byte{0x81, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
[]byte{0x81, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
||||||
}, {
|
}, {
|
||||||
"simple hello binary message",
|
"simple hello binary message",
|
||||||
Message{true, BINARY, 5, []byte("hello")},
|
Message{true, Binary, 5, []byte("hello")},
|
||||||
[]byte{0x82, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
[]byte{0x82, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
||||||
}, {
|
}, {
|
||||||
"other simple binary message",
|
"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},
|
[]byte{0x82, 0x08, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80},
|
||||||
}, {
|
}, {
|
||||||
"non final frame",
|
"non final frame",
|
||||||
Message{false, TEXT, 2, []byte{0x01, 0x02}},
|
Message{false, Text, 2, []byte{0x01, 0x02}},
|
||||||
[]byte{0x01, 0x02, 0x01, 0x02},
|
[]byte{0x01, 0x02, 0x01, 0x02},
|
||||||
}, {
|
}, {
|
||||||
"125 > normal length",
|
"125 > normal length",
|
||||||
Message{true, TEXT, uint(len(m4b1)), m4b1},
|
Message{true, Text, uint(len(m4b1)), m4b1},
|
||||||
append([]byte{0x81, 0x7e - 1}, m4b1...),
|
append([]byte{0x81, 0x7e - 1}, m4b1...),
|
||||||
}, {
|
}, {
|
||||||
"126 > extended 16 bits length",
|
"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...),
|
append([]byte{0x81, 126, 0x00, 0x7e}, m4b2...),
|
||||||
}, {
|
}, {
|
||||||
"127 > extended 16 bits length",
|
"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...),
|
append([]byte{0x81, 126, 0x00, 0x7e + 1}, m4b3...),
|
||||||
}, {
|
}, {
|
||||||
"fffe > extended 16 bits length",
|
"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...),
|
append([]byte{0x81, 126, 0xff, 0xfe}, m16b1...),
|
||||||
}, {
|
}, {
|
||||||
"ffff > extended 16 bits length",
|
"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...),
|
append([]byte{0x81, 126, 0xff, 0xff}, m16b2...),
|
||||||
}, {
|
}, {
|
||||||
"10000 > extended 64 bits length",
|
"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...),
|
append([]byte{0x81, 127, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00}, m16b3...),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -305,22 +305,22 @@ func TestMessageCheck(t *testing.T) {
|
||||||
Cases: []Case{
|
Cases: []Case{
|
||||||
{
|
{
|
||||||
"CONTINUATION must fail",
|
"CONTINUATION must fail",
|
||||||
Message{false, CONTINUATION, 0, []byte{}}, false, ErrInvalidFragment,
|
Message{false, Continuation, 0, []byte{}}, false, ErrInvalidFragment,
|
||||||
}, {
|
}, {
|
||||||
"TEXT must not fail",
|
"TEXT must not fail",
|
||||||
Message{false, TEXT, 0, []byte{}}, false, nil,
|
Message{false, Text, 0, []byte{}}, false, nil,
|
||||||
}, {
|
}, {
|
||||||
"BINARY must not fail",
|
"BINARY must not fail",
|
||||||
Message{false, BINARY, 0, []byte{}}, false, nil,
|
Message{false, Binary, 0, []byte{}}, false, nil,
|
||||||
}, {
|
}, {
|
||||||
"CLOSE must fail",
|
"CLOSE must fail",
|
||||||
Message{false, CLOSE, 0, []byte{}}, false, ErrInvalidFragment,
|
Message{false, Close, 0, []byte{}}, false, ErrInvalidFragment,
|
||||||
}, {
|
}, {
|
||||||
"PING must fail",
|
"PING must fail",
|
||||||
Message{false, PING, 0, []byte{}}, false, ErrInvalidFragment,
|
Message{false, Ping, 0, []byte{}}, false, ErrInvalidFragment,
|
||||||
}, {
|
}, {
|
||||||
"PONG must fail",
|
"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{
|
Cases: []Case{
|
||||||
{
|
{
|
||||||
"CONTINUATION must not fail",
|
"CONTINUATION must not fail",
|
||||||
Message{true, CONTINUATION, 0, []byte{}}, true, nil,
|
Message{true, Continuation, 0, []byte{}}, true, nil,
|
||||||
}, {
|
}, {
|
||||||
"TEXT must fail",
|
"TEXT must fail",
|
||||||
Message{true, TEXT, 0, []byte{}}, true, ErrInvalidFragment,
|
Message{true, Text, 0, []byte{}}, true, ErrInvalidFragment,
|
||||||
}, {
|
}, {
|
||||||
"BINARY must fail",
|
"BINARY must fail",
|
||||||
Message{true, BINARY, 0, []byte{}}, true, ErrInvalidFragment,
|
Message{true, Binary, 0, []byte{}}, true, ErrInvalidFragment,
|
||||||
}, {
|
}, {
|
||||||
"CLOSE must not fail",
|
"CLOSE must not fail",
|
||||||
Message{true, CLOSE, 0, []byte{}}, true, CloseFrame,
|
Message{true, Close, 0, []byte{}}, true, ErrCloseFrame,
|
||||||
}, {
|
}, {
|
||||||
"PING must not fail",
|
"PING must not fail",
|
||||||
Message{true, PING, 0, []byte{}}, true, nil,
|
Message{true, Ping, 0, []byte{}}, true, nil,
|
||||||
}, {
|
}, {
|
||||||
"PONG must not fail",
|
"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{
|
Cases: []Case{
|
||||||
{
|
{
|
||||||
"CLOSE must not fail",
|
"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",
|
"PING must not fail",
|
||||||
Message{true, PING, 125, []byte{}}, false, nil,
|
Message{true, Ping, 125, []byte{}}, false, nil,
|
||||||
}, {
|
}, {
|
||||||
"PONG must not fail",
|
"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{
|
Cases: []Case{
|
||||||
{
|
{
|
||||||
"CLOSE must fail",
|
"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",
|
"PING must fail",
|
||||||
Message{true, PING, 126, []byte{}}, false, ErrTooLongControlFrame,
|
Message{true, Ping, 126, []byte{}}, false, ErrTooLongControlFrame,
|
||||||
}, {
|
}, {
|
||||||
"PONG must fail",
|
"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{
|
Cases: []Case{
|
||||||
{
|
{
|
||||||
"CLOSE must fail",
|
"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",
|
"PING must fail",
|
||||||
Message{false, PING, 126, []byte{}}, false, ErrInvalidFragment,
|
Message{false, Ping, 126, []byte{}}, false, ErrInvalidFragment,
|
||||||
}, {
|
}, {
|
||||||
"PONG must fail",
|
"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{
|
Cases: []Case{
|
||||||
{
|
{
|
||||||
"no waiting fragment final",
|
"no waiting fragment final",
|
||||||
Message{false, CONTINUATION, 126, nil}, false, ErrInvalidFragment,
|
Message{false, Continuation, 126, nil}, false, ErrInvalidFragment,
|
||||||
}, {
|
}, {
|
||||||
"no waiting fragment non-final",
|
"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{
|
Cases: []Case{
|
||||||
{
|
{
|
||||||
"CLOSE valid reason",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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{
|
Cases: []Case{
|
||||||
{
|
{
|
||||||
"CLOSE only 1 byte",
|
"CLOSE only 1 byte",
|
||||||
Message{true, CLOSE, 1, []byte{0x03}}, false, ErrInvalidCloseStatus,
|
Message{true, Close, 1, []byte{0x03}}, false, ErrInvalidCloseStatus,
|
||||||
}, {
|
}, {
|
||||||
"valid CLOSE status 1000",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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",
|
"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},
|
{"5", Message{true, 5, 0, []byte{}}, false, ErrInvalidOpCode},
|
||||||
{"6", Message{true, 6, 0, []byte{}}, false, ErrInvalidOpCode},
|
{"6", Message{true, 6, 0, []byte{}}, false, ErrInvalidOpCode},
|
||||||
{"7", Message{true, 7, 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},
|
{"9", Message{true, 9, 0, []byte{}}, false, nil},
|
||||||
{"10", Message{true, 10, 0, []byte{}}, false, nil},
|
{"10", Message{true, 10, 0, []byte{}}, false, nil},
|
||||||
{"11", Message{true, 11, 0, []byte{}}, false, ErrInvalidOpCode},
|
{"11", Message{true, 11, 0, []byte{}}, false, ErrInvalidOpCode},
|
||||||
|
|
30
server.go
30
server.go
|
@ -2,18 +2,19 @@ package websocket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.xdrm.io/go/websocket/internal/uri/parser"
|
|
||||||
"net"
|
"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 {
|
type serverChannelSet struct {
|
||||||
register chan *client
|
register chan *client
|
||||||
unregister chan *client
|
unregister chan *client
|
||||||
broadcast chan Message
|
broadcast chan Message
|
||||||
}
|
}
|
||||||
|
|
||||||
// Represents a websocket server
|
// Server is a websocket server
|
||||||
type Server struct {
|
type Server struct {
|
||||||
sock net.Listener // listen socket
|
sock net.Listener // listen socket
|
||||||
addr []byte // server listening ip/host
|
addr []byte // server listening ip/host
|
||||||
|
@ -26,7 +27,7 @@ type Server struct {
|
||||||
ch serverChannelSet
|
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 {
|
func CreateServer(host string, port uint16) *Server {
|
||||||
|
|
||||||
return &Server{
|
return &Server{
|
||||||
|
@ -37,7 +38,7 @@ func CreateServer(host string, port uint16) *Server {
|
||||||
|
|
||||||
ctl: ControllerSet{
|
ctl: ControllerSet{
|
||||||
Def: nil,
|
Def: nil,
|
||||||
Uri: make([]*Controller, 0),
|
URI: make([]*Controller, 0),
|
||||||
},
|
},
|
||||||
|
|
||||||
ch: serverChannelSet{
|
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 {
|
func (s *Server) Bind(uri string, f ControllerFunc) error {
|
||||||
|
|
||||||
/* (1) Build URI parser */
|
/* (1) Build URI parser */
|
||||||
|
@ -71,7 +72,7 @@ func (s *Server) Bind(uri string, f ControllerFunc) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (2) Create controller */
|
/* (2) Create controller */
|
||||||
s.ctl.Uri = append(s.ctl.Uri, &Controller{
|
s.ctl.URI = append(s.ctl.URI, &Controller{
|
||||||
URI: uriScheme,
|
URI: uriScheme,
|
||||||
Fun: f,
|
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 {
|
func (s *Server) Launch() error {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
@ -140,23 +141,16 @@ func (s *Server) scheduler() {
|
||||||
|
|
||||||
select {
|
select {
|
||||||
|
|
||||||
/* (1) New client */
|
/* (1) Create client */
|
||||||
case client := <-s.ch.register:
|
case client := <-s.ch.register:
|
||||||
|
|
||||||
// fmt.Printf(" + client\n")
|
|
||||||
s.clients[client.io.sock] = client
|
s.clients[client.io.sock] = client
|
||||||
|
|
||||||
/* (2) New client */
|
/* (2) Remove client */
|
||||||
case client := <-s.ch.unregister:
|
case client := <-s.ch.unregister:
|
||||||
|
|
||||||
// fmt.Printf(" - client\n")
|
|
||||||
delete(s.clients, client.io.sock)
|
delete(s.clients, client.io.sock)
|
||||||
|
|
||||||
/* (3) Broadcast */
|
/* (3) Broadcast */
|
||||||
case msg := <-s.ch.broadcast:
|
case msg := <-s.ch.broadcast:
|
||||||
|
|
||||||
fmt.Printf(" + broadcast\n")
|
|
||||||
|
|
||||||
for _, c := range s.clients {
|
for _, c := range s.clients {
|
||||||
c.ch.send <- msg
|
c.ch.send <- msg
|
||||||
}
|
}
|
||||||
|
@ -165,6 +159,4 @@ func (s *Server) scheduler() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("+ server stopped\n")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue