From 08b8f7adec2e29f781444632c1643d14a96ab7e0 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Sat, 8 Sep 2018 22:14:31 -0400 Subject: [PATCH 1/5] Add stubbed API --- go.mod | 1 + go.sum | 0 internal/wscore/wscore.go | 57 +++++++++++++++++++++++++++++++++++++++ ws.go | 29 ++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/wscore/wscore.go create mode 100644 ws.go diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..44649774 --- /dev/null +++ b/go.mod @@ -0,0 +1 @@ +module nhooyr.io/ws diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..e69de29b diff --git a/internal/wscore/wscore.go b/internal/wscore/wscore.go new file mode 100644 index 00000000..7630beef --- /dev/null +++ b/internal/wscore/wscore.go @@ -0,0 +1,57 @@ +package wscore + +import ( + "io" +) + +// This is how it is capitalized in the RFC. +type Opcode int + +const ( + Continuation Opcode = iota + Text + Binary + // 3 - 7 are reserved for further non-control frames. + Close = 8 + Ping + Pong + // 11-16 are reserved for further control frames. +) + +type Header struct { + FIN bool + + RSV1 bool + RSV2 bool + RSV3 bool + + Opcode Opcode + + // Length is an integer because the RFC mandates the MSB bit cannot be set. + // We cannot send or receive a frame with negative length. + Length int64 + + Mask []byte +} + +func (f *Header) Bytes() []byte { + panic("TODO") +} + +func (f *Header) MaskPayload(payload []byte) { + panic("TODO") +} + +func Read(w io.Writer) (*Header, error) { + panic("TODO") +} + +type StatusCode uint16 + +const ( + NormalClosure StatusCode = 1000 + iota + GoingAway + ProtocolError + UnsupportedData + // TODO +) diff --git a/ws.go b/ws.go new file mode 100644 index 00000000..46b968c9 --- /dev/null +++ b/ws.go @@ -0,0 +1,29 @@ +package ws + +import ( + "io" + "nhooyr.io/ws/internal/wscore" +) + +type FrameType = wscore.Opcode + +const ( + continuation FrameType = iota + Text + Binary + Close = 8 + Ping + Pong +) + +func Write(w io.Writer, typ FrameType, p []byte) (n int, err error) { + panic("TODO") +} + +func Read(r io.Reader) (typ FrameType, payload io.Reader, err error) { + panic("TODO") +} + +func SecWebsocketAccept(secWebsocketKey string) string { + panic("TODO") +} From b753edbc9b3d509653f62b6063e04ce973fbae20 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Sun, 9 Sep 2018 14:16:14 -0400 Subject: [PATCH 2/5] Update stubbed API --- ws.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ws.go b/ws.go index 46b968c9..dc1a4b4f 100644 --- a/ws.go +++ b/ws.go @@ -1,7 +1,9 @@ package ws import ( + "bufio" "io" + "nhooyr.io/ws/internal/wscore" ) @@ -16,7 +18,7 @@ const ( Pong ) -func Write(w io.Writer, typ FrameType, p []byte) (n int, err error) { +func Writer(w bufio.Writer, typ FrameType) io.WriteCloser { panic("TODO") } From 25a3fb62721bf8b80bca2e77681bf6ebe103b9ed Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Sun, 9 Sep 2018 16:09:05 -0400 Subject: [PATCH 3/5] Update stubbed API --- internal/wscore/wscore.go | 6 +++++- ws.go | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/wscore/wscore.go b/internal/wscore/wscore.go index 7630beef..8bfff2c6 100644 --- a/internal/wscore/wscore.go +++ b/internal/wscore/wscore.go @@ -42,7 +42,7 @@ func (f *Header) MaskPayload(payload []byte) { panic("TODO") } -func Read(w io.Writer) (*Header, error) { +func ReadHeader(w io.Writer) (*Header, error) { panic("TODO") } @@ -55,3 +55,7 @@ const ( UnsupportedData // TODO ) + +func SecWebsocketAccept(secWebsocketKey string) string { + panic("TODO") +} diff --git a/ws.go b/ws.go index dc1a4b4f..871f3e94 100644 --- a/ws.go +++ b/ws.go @@ -3,6 +3,8 @@ package ws import ( "bufio" "io" + "net" + "net/http" "nhooyr.io/ws/internal/wscore" ) @@ -22,10 +24,14 @@ func Writer(w bufio.Writer, typ FrameType) io.WriteCloser { panic("TODO") } +func Write(w io.Writer, typ FrameType, p []byte) error { + panic("TODO") +} + func Read(r io.Reader) (typ FrameType, payload io.Reader, err error) { panic("TODO") } -func SecWebsocketAccept(secWebsocketKey string) string { +func Upgrade(w http.ResponseWriter, r *http.Request) (net.Conn, bufio.ReadWriter, error) { panic("TODO") } From 4bfcd7cbf534e6139d6a4bacd3c81593d0c15e22 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Sun, 9 Sep 2018 19:45:33 -0400 Subject: [PATCH 4/5] Update stubbed API --- internal/wscore/wscore.go => core.go | 12 ++++-------- ws.go | 21 ++++----------------- 2 files changed, 8 insertions(+), 25 deletions(-) rename internal/wscore/wscore.go => core.go (77%) diff --git a/internal/wscore/wscore.go b/core.go similarity index 77% rename from internal/wscore/wscore.go rename to core.go index 8bfff2c6..995feb20 100644 --- a/internal/wscore/wscore.go +++ b/core.go @@ -1,4 +1,4 @@ -package wscore +package ws import ( "io" @@ -28,7 +28,7 @@ type Header struct { Opcode Opcode // Length is an integer because the RFC mandates the MSB bit cannot be set. - // We cannot send or receive a frame with negative length. + // So we cannot send or receive a frame with negative length. Length int64 Mask []byte @@ -42,7 +42,7 @@ func (f *Header) MaskPayload(payload []byte) { panic("TODO") } -func ReadHeader(w io.Writer) (*Header, error) { +func ReadHeader(w io.Writer) (Header, error) { panic("TODO") } @@ -53,9 +53,5 @@ const ( GoingAway ProtocolError UnsupportedData - // TODO + // ... ) - -func SecWebsocketAccept(secWebsocketKey string) string { - panic("TODO") -} diff --git a/ws.go b/ws.go index 871f3e94..ee3e9da1 100644 --- a/ws.go +++ b/ws.go @@ -5,33 +5,20 @@ import ( "io" "net" "net/http" - - "nhooyr.io/ws/internal/wscore" -) - -type FrameType = wscore.Opcode - -const ( - continuation FrameType = iota - Text - Binary - Close = 8 - Ping - Pong ) -func Writer(w bufio.Writer, typ FrameType) io.WriteCloser { +func Writer(w bufio.Writer, typ Opcode) io.WriteCloser { panic("TODO") } -func Write(w io.Writer, typ FrameType, p []byte) error { +func WriteFrame(w io.Writer, typ Opcode, p []byte) error { panic("TODO") } -func Read(r io.Reader) (typ FrameType, payload io.Reader, err error) { +func ReadFrame(r io.Reader) (typ Opcode, payload io.Reader, err error) { panic("TODO") } -func Upgrade(w http.ResponseWriter, r *http.Request) (net.Conn, bufio.ReadWriter, error) { +func Upgrade(w http.ResponseWriter, r *http.Request) (net.Conn, *bufio.ReadWriter, error) { panic("TODO") } From b0d1603b2497f8af1e7715419489723d48d3c12e Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Sun, 9 Sep 2018 19:50:29 -0400 Subject: [PATCH 5/5] Update stubbed API --- core.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core.go b/core.go index 995feb20..fd2e443d 100644 --- a/core.go +++ b/core.go @@ -31,7 +31,8 @@ type Header struct { // So we cannot send or receive a frame with negative length. Length int64 - Mask []byte + Masked bool + Mask [4]byte } func (f *Header) Bytes() []byte {