Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Rewrite with compression support#163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nhooyr merged 56 commits intomasterfromcompress
Feb 16, 2020
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
56 commits
Select commitHold shift + click to select a range
8604dee
Increase TestWASM timeout
nhooyrOct 15, 2019
e55ac18
Document compression API
nhooyrOct 14, 2019
e142e08
Improve compression docs
nhooyrNov 12, 2019
53c1aea
Implement compression extension negotiation
nhooyrNov 12, 2019
2cf6c28
Implement compression writer and reader pooling
nhooyrNov 12, 2019
a01afea
Support x-webkit-deflate-frame extension for Safari
nhooyrNov 12, 2019
531d4fa
Improve general compression API and write docs
nhooyrNov 12, 2019
d0a8049
Rewrite core
nhooyrNov 19, 2019
dd107dd
Update CI
nhooyrNov 28, 2019
6c6b8e9
Cleanup wspb and wsjson
nhooyrNov 28, 2019
6b782a3
Run make fmt
nhooyrNov 28, 2019
989ba2f
Change websocket to WebSocket in docs/errors
nhooyrNov 28, 2019
9f15963
Simplify dial.go
nhooyrNov 28, 2019
120911b
Remove use of math/rand.Init
nhooyrNov 28, 2019
7ad1514
Update README.md comparison
nhooyrNov 29, 2019
746140b
Further improve README
nhooyrNov 29, 2019
43cb01e
Refactor read.go/write.go
nhooyrNov 29, 2019
e8dfe27
Make CI pass
nhooyrNov 29, 2019
f6137f3
Add minor improvements
nhooyrDec 6, 2019
6f6fa43
Refactor autobahn
nhooyrDec 31, 2019
8c87970
Add slidingWindowReader
nhooyrJan 4, 2020
aaf4b45
Up test coverage of accept.go to 100%
nhooyrJan 26, 2020
6b76536
Up dial coverage to 100%
nhooyrJan 30, 2020
0f115ed
Add Go 1.12 support
nhooyrJan 31, 2020
b6b56b7
Both modes seem to work :)
nhooyrFeb 5, 2020
9e32354
Fix randString method in tests
nhooyrFeb 6, 2020
78da35e
Get test with multiple messages working
nhooyrFeb 7, 2020
d092686
Autobahn tests fully pass :)
nhooyrFeb 8, 2020
6975801
Fix race in tests
nhooyrFeb 9, 2020
bbaf469
Fix test step
nhooyrFeb 9, 2020
faadcc9
Simplify tests
nhooyrFeb 9, 2020
3f2589f
Remove quite a bit of slog
nhooyrFeb 9, 2020
b53f306
Get Wasm tests working
nhooyrFeb 9, 2020
69ff675
More tests and fixes
nhooyrFeb 9, 2020
085e671
Get coverage to 85%
nhooyrFeb 9, 2020
51769b3
Add wspb test
nhooyrFeb 9, 2020
670be05
Merge in handshake improvements from master
nhooyrFeb 9, 2020
988b8f2
Merge remote-tracking branch 'origin/master' into compress
nhooyrFeb 9, 2020
3a526d8
Fix bug in closeHandshake
nhooyrFeb 9, 2020
999b812
Fix race in msgReader
nhooyrFeb 9, 2020
4b84d25
Fix a race with c.closed
nhooyrFeb 9, 2020
85f249d
Up timeouts
nhooyrFeb 9, 2020
6b38ebb
Test fixes
nhooyrFeb 9, 2020
6770421
Fix goroutine leak from deadlock when closing
nhooyrFeb 12, 2020
c752365
Make flateThreshold work
nhooyrFeb 12, 2020
0ea9466
Cleanup writeMu and flateThreshold
nhooyrFeb 12, 2020
b33d48c
Minor cleanup
nhooyrFeb 12, 2020
9c5bfab
Simplifications of conn_test.go
nhooyrFeb 13, 2020
3673c2c
Use basic test assertions
nhooyrFeb 13, 2020
c5b0a00
Fix badPing test duration
nhooyrFeb 13, 2020
1c7c14e
Pool sliding windows
nhooyrFeb 13, 2020
503b469
Simplify sliding window API
nhooyrFeb 13, 2020
dff4af3
Add conn benchmark
nhooyrFeb 13, 2020
2377cca
Switch to klauspost/compress
nhooyrFeb 15, 2020
d57b253
Report how efficient compression is in BenchmarkConn
nhooyrFeb 16, 2020
1bc100d
Update docs and random little issues
nhooyrFeb 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Rewrite core
Too many improvements and changes to list.Will include a detailed changelog for release.
  • Loading branch information
@nhooyr
nhooyr committedNov 29, 2019
commitd0a80496108cf7cdd4e20c24e4689cd5934b5b89
63 changes: 36 additions & 27 deletionsaccept.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -60,10 +60,15 @@ func Accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (*Conn,
return c, nil
}

funcaccept(w http.ResponseWriter, r *http.Request,opts *AcceptOptions)(*Conn, error) {
func(opts *AcceptOptions)ensure() *AcceptOptions {
if opts == nil {
opts = &AcceptOptions{}
return &AcceptOptions{}
}
return opts
}

func accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (*Conn, error) {
opts = opts.ensure()

err := verifyClientRequest(w, r)
if err != nil {
Expand DownExpand Up@@ -114,31 +119,14 @@ func accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (*Conn,
b, _ := brw.Reader.Peek(brw.Reader.Buffered())
brw.Reader.Reset(io.MultiReader(bytes.NewReader(b), netConn))

c := &Conn{
return newConn(connConfig{
subprotocol: w.Header().Get("Sec-WebSocket-Protocol"),
rwc: netConn,
client: false,
copts: copts,
br: brw.Reader,
bw: brw.Writer,
closer: netConn,
copts: copts,
}
c.init()

return c, nil
}

func authenticateOrigin(r *http.Request) error {
origin := r.Header.Get("Origin")
if origin == "" {
return nil
}
u, err := url.Parse(origin)
if err != nil {
return fmt.Errorf("failed to parse Origin header %q: %w", origin, err)
}
if !strings.EqualFold(u.Host, r.Host) {
return fmt.Errorf("request Origin %q is not authorized for Host %q", origin, r.Host)
}
return nil
}), nil
}

func verifyClientRequest(w http.ResponseWriter, r *http.Request) error {
Expand DownExpand Up@@ -181,15 +169,37 @@ func verifyClientRequest(w http.ResponseWriter, r *http.Request) error {
return nil
}

func authenticateOrigin(r *http.Request) error {
origin := r.Header.Get("Origin")
if origin == "" {
return nil
}
u, err := url.Parse(origin)
if err != nil {
return fmt.Errorf("failed to parse Origin header %q: %w", origin, err)
}
if !strings.EqualFold(u.Host, r.Host) {
return fmt.Errorf("request Origin %q is not authorized for Host %q", origin, r.Host)
}
return nil
}

func handleSecWebSocketKey(w http.ResponseWriter, r *http.Request) {
key := r.Header.Get("Sec-WebSocket-Key")
w.Header().Set("Sec-WebSocket-Accept", secWebSocketAccept(key))
}

func selectSubprotocol(r *http.Request, subprotocols []string) string {
cps := headerTokens(r.Header, "Sec-WebSocket-Protocol")
if len(cps) == 0 {
return ""
}

for _, sp := range subprotocols {
if headerContainsToken(r.Header, "Sec-WebSocket-Protocol", sp) {
return sp
for _, cp := range cps {
if strings.EqualFold(sp, cp) {
return cp
}
}
}
return ""
Expand DownExpand Up@@ -266,7 +276,6 @@ func acceptWebkitDeflate(w http.ResponseWriter, ext websocketExtension, mode Com
return copts, nil
}


func headerContainsToken(h http.Header, key, token string) bool {
token = strings.ToLower(token)

Expand Down
14 changes: 14 additions & 0 deletionsassert_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,6 +23,8 @@ func randBytes(n int) []byte {
}

func assertJSONEcho(t *testing.T, ctx context.Context, c *websocket.Conn, n int) {
t.Helper()

exp := randString(n)
err := wsjson.Write(ctx, c, exp)
assert.Success(t, err)
Expand All@@ -35,6 +37,8 @@ func assertJSONEcho(t *testing.T, ctx context.Context, c *websocket.Conn, n int)
}

func assertJSONRead(t *testing.T, ctx context.Context, c *websocket.Conn, exp interface{}) {
t.Helper()

var act interface{}
err := wsjson.Read(ctx, c, &act)
assert.Success(t, err)
Expand All@@ -56,6 +60,8 @@ func randString(n int) string {
}

func assertEcho(t *testing.T, ctx context.Context, c *websocket.Conn, typ websocket.MessageType, n int) {
t.Helper()

p := randBytes(n)
err := c.Write(ctx, typ, p)
assert.Success(t, err)
Expand All@@ -68,5 +74,13 @@ func assertEcho(t *testing.T, ctx context.Context, c *websocket.Conn, typ websoc
}

func assertSubprotocol(t *testing.T, c *websocket.Conn, exp string) {
t.Helper()

assert.Equalf(t, exp, c.Subprotocol(), "unexpected subprotocol")
}

func assertCloseStatus(t *testing.T, exp websocket.StatusCode, err error) {
t.Helper()

assert.Equalf(t, exp, websocket.CloseStatus(err), "unexpected status code")
}
252 changes: 252 additions & 0 deletionsautobahn_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
package websocket_test

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"nhooyr.io/websocket"
"os"
"os/exec"
"strconv"
"strings"
"testing"
"time"
)

func TestAutobahn(t *testing.T) {
// This test contains the old autobahn test suite tests that use the
// python binary. The approach is clunky and slow so new tests
// have been written in pure Go in websocket_test.go.
// These have been kept for correctness purposes and are occasionally ran.
if os.Getenv("AUTOBAHN") == "" {
t.Skip("Set $AUTOBAHN to run tests against the autobahn test suite")
}

t.Run("server", testServerAutobahnPython)
t.Run("client", testClientAutobahnPython)
}

// https://github.com/crossbario/autobahn-python/tree/master/wstest
func testServerAutobahnPython(t *testing.T) {
t.Parallel()

s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
Subprotocols: []string{"echo"},
})
if err != nil {
t.Logf("server handshake failed: %+v", err)
return
}
echoLoop(r.Context(), c)
}))
defer s.Close()

spec := map[string]interface{}{
"outdir": "ci/out/wstestServerReports",
"servers": []interface{}{
map[string]interface{}{
"agent": "main",
"url": strings.Replace(s.URL, "http", "ws", 1),
},
},
"cases": []string{"*"},
// We skip the UTF-8 handling tests as there isn't any reason to reject invalid UTF-8, just
// more performance overhead. 7.5.1 is the same.
"exclude-cases": []string{"6.*", "7.5.1"},
}
specFile, err := ioutil.TempFile("", "websocketFuzzingClient.json")
if err != nil {
t.Fatalf("failed to create temp file for fuzzingclient.json: %v", err)
}
defer specFile.Close()

e := json.NewEncoder(specFile)
e.SetIndent("", "\t")
err = e.Encode(spec)
if err != nil {
t.Fatalf("failed to write spec: %v", err)
}

err = specFile.Close()
if err != nil {
t.Fatalf("failed to close file: %v", err)
}

ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Minute*10)
defer cancel()

args := []string{"--mode", "fuzzingclient", "--spec", specFile.Name()}
wstest := exec.CommandContext(ctx, "wstest", args...)
out, err := wstest.CombinedOutput()
if err != nil {
t.Fatalf("failed to run wstest: %v\nout:\n%s", err, out)
}

checkWSTestIndex(t, "./ci/out/wstestServerReports/index.json")
}

func unusedListenAddr() (string, error) {
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
return "", err
}
l.Close()
return l.Addr().String(), nil
}

// https://github.com/crossbario/autobahn-python/blob/master/wstest/testee_client_aio.py
func testClientAutobahnPython(t *testing.T) {
t.Parallel()

if os.Getenv("AUTOBAHN_PYTHON") == "" {
t.Skip("Set $AUTOBAHN_PYTHON to test against the python autobahn test suite")
}

serverAddr, err := unusedListenAddr()
if err != nil {
t.Fatalf("failed to get unused listen addr for wstest: %v", err)
}

wsServerURL := "ws://" + serverAddr

spec := map[string]interface{}{
"url": wsServerURL,
"outdir": "ci/out/wstestClientReports",
"cases": []string{"*"},
// See TestAutobahnServer for the reasons why we exclude these.
"exclude-cases": []string{"6.*", "7.5.1"},
}
specFile, err := ioutil.TempFile("", "websocketFuzzingServer.json")
if err != nil {
t.Fatalf("failed to create temp file for fuzzingserver.json: %v", err)
}
defer specFile.Close()

e := json.NewEncoder(specFile)
e.SetIndent("", "\t")
err = e.Encode(spec)
if err != nil {
t.Fatalf("failed to write spec: %v", err)
}

err = specFile.Close()
if err != nil {
t.Fatalf("failed to close file: %v", err)
}

ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Minute*10)
defer cancel()

args := []string{"--mode", "fuzzingserver", "--spec", specFile.Name(),
// Disables some server that runs as part of fuzzingserver mode.
// See https://github.com/crossbario/autobahn-testsuite/blob/058db3a36b7c3a1edf68c282307c6b899ca4857f/autobahntestsuite/autobahntestsuite/wstest.py#L124
"--webport=0",
}
wstest := exec.CommandContext(ctx, "wstest", args...)
err = wstest.Start()
if err != nil {
t.Fatal(err)
}
defer func() {
err := wstest.Process.Kill()
if err != nil {
t.Error(err)
}
}()

// Let it come up.
time.Sleep(time.Second * 5)

var cases int
func() {
c, _, err := websocket.Dial(ctx, wsServerURL+"/getCaseCount", nil)
if err != nil {
t.Fatal(err)
}
defer c.Close(websocket.StatusInternalError, "")

_, r, err := c.Reader(ctx)
if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal(err)
}
cases, err = strconv.Atoi(string(b))
if err != nil {
t.Fatal(err)
}

c.Close(websocket.StatusNormalClosure, "")
}()

for i := 1; i <= cases; i++ {
func() {
ctx, cancel := context.WithTimeout(ctx, time.Second*45)
defer cancel()

c, _, err := websocket.Dial(ctx, fmt.Sprintf(wsServerURL+"/runCase?case=%v&agent=main", i), nil)
if err != nil {
t.Fatal(err)
}
echoLoop(ctx, c)
}()
}

c, _, err := websocket.Dial(ctx, fmt.Sprintf(wsServerURL+"/updateReports?agent=main"), nil)
if err != nil {
t.Fatal(err)
}
c.Close(websocket.StatusNormalClosure, "")

checkWSTestIndex(t, "./ci/out/wstestClientReports/index.json")
}

func checkWSTestIndex(t *testing.T, path string) {
wstestOut, err := ioutil.ReadFile(path)
if err != nil {
t.Fatalf("failed to read index.json: %v", err)
}

var indexJSON map[string]map[string]struct {
Behavior string `json:"behavior"`
BehaviorClose string `json:"behaviorClose"`
}
err = json.Unmarshal(wstestOut, &indexJSON)
if err != nil {
t.Fatalf("failed to unmarshal index.json: %v", err)
}

var failed bool
for _, tests := range indexJSON {
for test, result := range tests {
switch result.Behavior {
case "OK", "NON-STRICT", "INFORMATIONAL":
default:
failed = true
t.Errorf("test %v failed", test)
}
switch result.BehaviorClose {
case "OK", "INFORMATIONAL":
default:
failed = true
t.Errorf("bad close behaviour for test %v", test)
}
}
}

if failed {
path = strings.Replace(path, ".json", ".html", 1)
if os.Getenv("CI") == "" {
t.Errorf("wstest found failure, see %q (output as an artifact in CI)", path)
}
}
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp