Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
44 changes: 44 additions & 0 deletions .github/workflows/autobahn.yml
Original file line number Diff line number Diff line change
@@ -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@v5

- name: Setup Go
uses: actions/setup-go@v6
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@v5
with:
name: autobahn report ${{ matrix.go }} ${{ matrix.os }}
path: autobahn/report
retention-days: 7
33 changes: 3 additions & 30 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
go: [ '1.15', 'stable', 'oldstable' ]
go: [ 'stable', 'oldstable' ]

runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v4
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
Expand All @@ -42,33 +42,6 @@ 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 ./...

- 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
18 changes: 9 additions & 9 deletions cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
}
}

Expand Down
4 changes: 4 additions & 0 deletions cipher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
9 changes: 8 additions & 1 deletion dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gobwas/ws

go 1.15
go 1.16

require (
github.com/gobwas/httphead v0.1.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
6 changes: 5 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -122,6 +127,7 @@ func BenchmarkHttpWriteUpgradeRequest(b *testing.B) {
test.protocols,
test.extensions,
headers,
test.host,
)
}
})
Expand Down