From 4600238e942e9bfef7f519fe34b5752f8d83bf4b Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Mon, 8 Jan 2024 09:29:08 +0100 Subject: [PATCH 1/7] Add Dialer.Host field (#196) --- dialer.go | 9 ++++++++- http.go | 6 +++++- http_test.go | 6 ++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/dialer.go b/dialer.go index 64d4681..e66678e 100644 --- a/dialer.go +++ b/dialer.go @@ -87,6 +87,13 @@ type Dialer struct { // land. Header HandshakeHeader + // Host is an optional string that could be used to specify the host during + // HTTP upgrade request by setting 'Host' header. + // + // Default value is an empty string, which results in setting 'Host' header + // equal to the URL hostname given to Dialer.Dial(). + Host string + // OnStatusError is the callback that will be called after receiving non // "101 Continue" HTTP response status. It receives an io.Reader object // representing server response bytes. That is, it gives ability to parse @@ -310,7 +317,7 @@ func (d Dialer) Upgrade(conn io.ReadWriter, u *url.URL) (br *bufio.Reader, hs Ha nonce := make([]byte, nonceSize) initNonce(nonce) - httpWriteUpgradeRequest(bw, u, nonce, d.Protocols, d.Extensions, d.Header) + httpWriteUpgradeRequest(bw, u, nonce, d.Protocols, d.Extensions, d.Header, d.Host) if err := bw.Flush(); err != nil { return br, hs, err } diff --git a/http.go b/http.go index 129e77e..a3a682d 100644 --- a/http.go +++ b/http.go @@ -286,12 +286,16 @@ func httpWriteUpgradeRequest( protocols []string, extensions []httphead.Option, header HandshakeHeader, + host string, ) { bw.WriteString("GET ") bw.WriteString(u.RequestURI()) bw.WriteString(" HTTP/1.1\r\n") - httpWriteHeader(bw, headerHost, u.Host) + if host == "" { + host = u.Host + } + httpWriteHeader(bw, headerHost, host) httpWriteHeaderBts(bw, headerUpgrade, specHeaderValueUpgrade) httpWriteHeaderBts(bw, headerConnection, specHeaderValueConnection) diff --git a/http_test.go b/http_test.go index 802e937..7e2ff1e 100644 --- a/http_test.go +++ b/http_test.go @@ -99,10 +99,15 @@ func BenchmarkHttpWriteUpgradeRequest(b *testing.B) { protocols []string extensions []httphead.Option headers HandshakeHeaderFunc + host string }{ { url: makeURL("ws://example.org"), }, + { + url: makeURL("ws://example.org"), + host: "test-host", + }, } { bw := bufio.NewWriter(ioutil.Discard) nonce := make([]byte, nonceSize) @@ -122,6 +127,7 @@ func BenchmarkHttpWriteUpgradeRequest(b *testing.B) { test.protocols, test.extensions, headers, + test.host, ) } }) From 02bca952972ad741960dac53f5b0d03cf256d122 Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Fri, 3 May 2024 09:49:55 +0200 Subject: [PATCH 2/7] Drop Go 1.15 support (#191) --- .github/workflows/main.yml | 12 +----------- go.mod | 2 +- go.sum | 2 -- 3 files changed, 2 insertions(+), 14 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e6ddeb7..5baa09b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,7 +17,7 @@ jobs: strategy: matrix: os: [ ubuntu-latest, macos-latest, windows-latest ] - go: [ '1.15', 'stable', 'oldstable' ] + go: [ 'stable', 'oldstable' ] runs-on: ${{ matrix.os }} steps: @@ -42,17 +42,7 @@ jobs: run: | go mod verify - - name: Test Go 1.15 - # This step if needed because -shuffle not available on Go 1.15. - # Tests are failing on MacOS. So, we just disable it. - if: >- - matrix.go == '1.15' && matrix.os != 'macos-latest' - run: | - go test -v -race -cover ./... - - name: Test - if: >- - matrix.go != '1.15' run: | go test -v -race -shuffle=on -cover ./... diff --git a/go.mod b/go.mod index 146c70d..1ff8aa5 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/gobwas/ws -go 1.15 +go 1.16 require ( github.com/gobwas/httphead v0.1.0 diff --git a/go.sum b/go.sum index 744c946..4d8ed8f 100644 --- a/go.sum +++ b/go.sum @@ -2,7 +2,5 @@ github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM= github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= -golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d h1:MiWWjyhUzZ+jvhZvloX6ZrUsdEghn8a64Upd8EMHglE= -golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= From af7917fc1a681fd10b565a9c13b97d750b667bc0 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Fri, 3 May 2024 01:33:17 -0700 Subject: [PATCH 3/7] Cipher 1.9x speedup (#198) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Cipher 1.9x speedup Simplify cipher loop and unroll once. ``` λ benchcmp before.txt after.txt | grep -v "naive" benchmark old ns/op new ns/op delta BenchmarkCipher/bytes=7;offset=1-32 7.17 7.79 +8.65% BenchmarkCipher/bytes=125;offset=0-32 24.5 23.1 -6.07% BenchmarkCipher/bytes=1024;offset=0-32 126 63.4 -49.87% BenchmarkCipher/bytes=4096;offset=0-32 462 244 -47.26% BenchmarkCipher/bytes=4100;offset=4-32 460 249 -45.93% BenchmarkCipher/bytes=4099;offset=3-32 463 250 -45.93% BenchmarkCipher/bytes=32775;offset=49-32 3619 1936 -46.50% benchmark old MB/s new MB/s speedup BenchmarkCipher/bytes=7;offset=1-32 976.74 898.93 0.92x BenchmarkCipher/bytes=125;offset=0-32 5092.84 5423.57 1.06x BenchmarkCipher/bytes=1024;offset=0-32 8103.00 16159.29 1.99x BenchmarkCipher/bytes=4096;offset=0-32 8870.86 16818.08 1.90x BenchmarkCipher/bytes=4100;offset=4-32 8917.63 16491.32 1.85x BenchmarkCipher/bytes=4099;offset=3-32 8854.38 16379.58 1.85x BenchmarkCipher/bytes=32775;offset=49-32 9056.37 16926.83 1.87x ``` I tried a few variations, but this seemed fine without too many changes. * Upgrade CI to more modern Go versions. * Fix doc * Revert "Upgrade CI to more modern Go versions." This reverts commit ed2945571fe7cbfead692fd2ba1cbb6a60aeefb5. --- cipher.go | 18 +++++++++--------- cipher_test.go | 4 ++++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/cipher.go b/cipher.go index 3c35e6b..ffe4161 100644 --- a/cipher.go +++ b/cipher.go @@ -25,8 +25,8 @@ func Cipher(payload []byte, mask [4]byte, offset int) { // Count number of bytes will processed one by one from the beginning of payload. ln := remain[mpos] // Count number of bytes will processed one by one from the end of payload. - // This is done to process payload by 8 bytes in each iteration of main loop. - rn := (n - ln) % 8 + // This is done to process payload by 16 bytes in each iteration of main loop. + rn := (n - ln) % 16 for i := 0; i < ln; i++ { payload[i] ^= mask[(mpos+i)%4] @@ -44,15 +44,15 @@ func Cipher(payload []byte, mask [4]byte, offset int) { ) // Skip already processed right part. // Get number of uint64 parts remaining to process. - n = (n - ln - rn) >> 3 + n = (n - ln - rn) >> 4 + j := ln for i := 0; i < n; i++ { - var ( - j = ln + (i << 3) - chunk = payload[j : j+8] - ) - p := binary.LittleEndian.Uint64(chunk) - p = p ^ m2 + chunk := payload[j : j+16] + p := binary.LittleEndian.Uint64(chunk) ^ m2 + p2 := binary.LittleEndian.Uint64(chunk[8:]) ^ m2 binary.LittleEndian.PutUint64(chunk, p) + binary.LittleEndian.PutUint64(chunk[8:], p2) + j += 16 } } diff --git a/cipher_test.go b/cipher_test.go index 2b62913..af6a092 100644 --- a/cipher_test.go +++ b/cipher_test.go @@ -169,6 +169,8 @@ func BenchmarkCipher(b *testing.B) { b.Run(fmt.Sprintf("naive_bytes=%d;offset=%d", bench.size, bench.offset), func(b *testing.B) { var sink int64 + b.SetBytes(int64(bench.size)) + b.ResetTimer() for i := 0; i < b.N; i++ { r := cipherNaiveNoCp(bts, mask, bench.offset) sink += int64(len(r)) @@ -177,6 +179,8 @@ func BenchmarkCipher(b *testing.B) { }) b.Run(fmt.Sprintf("bytes=%d;offset=%d", bench.size, bench.offset), func(b *testing.B) { var sink int64 + b.SetBytes(int64(bench.size)) + b.ResetTimer() for i := 0; i < b.N; i++ { Cipher(bts, mask, bench.offset) sink += int64(len(bts)) From 4f030072d3475c0a99f5908553997de71bb70d06 Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Fri, 3 May 2024 11:12:44 +0200 Subject: [PATCH 4/7] Extract Autobahn test suite to a separate CI step (#199) --- .github/workflows/autobahn.yml | 44 ++++++++++++++++++++++++++++++++++ .github/workflows/main.yml | 17 ------------- 2 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/autobahn.yml diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml new file mode 100644 index 0000000..d98c735 --- /dev/null +++ b/.github/workflows/autobahn.yml @@ -0,0 +1,44 @@ +name: Autobahn + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + schedule: + - cron: '0 10 * * 1' # run "At 10:00 on Monday" + +concurrency: + group: autobahn-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + test: + strategy: + matrix: + os: [ ubuntu-latest ] + go: [ 'stable', 'oldstable' ] + + runs-on: ${{ matrix.os }} + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go }} + check-latest: true + + - name: Autobahn + env: + CRYPTOGRAPHY_ALLOW_OPENSSL_102: yes + run: | + make test autobahn + + - name: Autobahn Report Artifact + uses: actions/upload-artifact@v4 + with: + name: autobahn report ${{ matrix.go }} ${{ matrix.os }} + path: autobahn/report + retention-days: 7 diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5baa09b..4c55391 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -45,20 +45,3 @@ jobs: - name: Test run: | go test -v -race -shuffle=on -cover ./... - - - name: Autobahn - if: >- - startsWith(matrix.os, 'ubuntu') - env: - CRYPTOGRAPHY_ALLOW_OPENSSL_102: yes - run: | - make test autobahn - - - name: Autobahn Report Artifact - if: >- - startsWith(matrix.os, 'ubuntu') - uses: actions/upload-artifact@v4 - with: - name: autobahn report ${{ matrix.go }} ${{ matrix.os }} - path: autobahn/report - retention-days: 7 From 0e2f65f991577ffef263dbe440a6b3113911f8f5 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 17 Aug 2025 12:22:30 +0200 Subject: [PATCH 5/7] ci: bump actions/checkout from 4 to 5 (#211) Bumps [actions/checkout](https://github.com/actions/checkout) from 4 to 5. - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/checkout/compare/v4...v5) --- updated-dependencies: - dependency-name: actions/checkout dependency-version: '5' dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/autobahn.yml | 2 +- .github/workflows/main.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml index d98c735..81527d4 100644 --- a/.github/workflows/autobahn.yml +++ b/.github/workflows/autobahn.yml @@ -22,7 +22,7 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v5 - name: Setup Go uses: actions/setup-go@v5 diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4c55391..100f0ce 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,7 +22,7 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v5 - name: Setup Go uses: actions/setup-go@v5 From da6b0937e8a7ccf9e5deac55f8b6c45fa13e4c8e Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 7 Sep 2025 18:51:13 +0200 Subject: [PATCH 6/7] ci: bump actions/setup-go from 5 to 6 (#212) Bumps [actions/setup-go](https://github.com/actions/setup-go) from 5 to 6. - [Release notes](https://github.com/actions/setup-go/releases) - [Commits](https://github.com/actions/setup-go/compare/v5...v6) --- updated-dependencies: - dependency-name: actions/setup-go dependency-version: '6' dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/autobahn.yml | 2 +- .github/workflows/main.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml index 81527d4..bed8636 100644 --- a/.github/workflows/autobahn.yml +++ b/.github/workflows/autobahn.yml @@ -25,7 +25,7 @@ jobs: uses: actions/checkout@v5 - name: Setup Go - uses: actions/setup-go@v5 + uses: actions/setup-go@v6 with: go-version: ${{ matrix.go }} check-latest: true diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 100f0ce..61ca209 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -25,7 +25,7 @@ jobs: uses: actions/checkout@v5 - name: Setup Go - uses: actions/setup-go@v5 + uses: actions/setup-go@v6 with: go-version: ${{ matrix.go }} check-latest: true From 36a216a86cc6331538c2b9ca81303f7b656333f6 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 26 Oct 2025 15:25:27 +0100 Subject: [PATCH 7/7] ci: bump actions/upload-artifact from 4 to 5 (#213) Bumps [actions/upload-artifact](https://github.com/actions/upload-artifact) from 4 to 5. - [Release notes](https://github.com/actions/upload-artifact/releases) - [Commits](https://github.com/actions/upload-artifact/compare/v4...v5) --- updated-dependencies: - dependency-name: actions/upload-artifact dependency-version: '5' dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/autobahn.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/autobahn.yml b/.github/workflows/autobahn.yml index bed8636..7abd566 100644 --- a/.github/workflows/autobahn.yml +++ b/.github/workflows/autobahn.yml @@ -37,7 +37,7 @@ jobs: make test autobahn - name: Autobahn Report Artifact - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v5 with: name: autobahn report ${{ matrix.go }} ${{ matrix.os }} path: autobahn/report