Skip to content

Commit

Permalink
Fix linter suggestions (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Apr 6, 2023
1 parent 0898358 commit 7c372c0
Show file tree
Hide file tree
Showing 31 changed files with 228 additions and 266 deletions.
10 changes: 2 additions & 8 deletions autobahn/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,13 @@ func main() {
log.Fatal(err)
}

var servers []string
servers := make([]string, 0, len(report))
for s := range report {
servers = append(servers, s)
}
sort.Strings(servers)

var (
failed bool
)
var failed bool
tw := tabwriter.NewWriter(os.Stderr, 0, 4, 1, ' ', 0)
for _, server := range servers {
var (
Expand Down Expand Up @@ -176,10 +174,6 @@ func main() {
os.Exit(rc)
}

type spec struct {
OutDir string `json:"outdir"`
}

type report map[string]server

type server map[string]entry
Expand Down
1 change: 1 addition & 0 deletions autobahn/main_go18.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.8
// +build go1.8

package main
Expand Down
22 changes: 17 additions & 5 deletions cipher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,27 @@ func BenchmarkCipher(b *testing.B) {
b.Fatal(err)
}

//b.Run(fmt.Sprintf("naive_bytes=%d;offset=%d", bench.size, bench.offset), func(b *testing.B) {
// for i := 0; i < b.N; i++ {
// cipherNaiveNoCp(bts, mask, bench.offset)
// }
//})
b.Run(fmt.Sprintf("naive_bytes=%d;offset=%d", bench.size, bench.offset), func(b *testing.B) {
var sink int64
for i := 0; i < b.N; i++ {
r := cipherNaiveNoCp(bts, mask, bench.offset)
sink += int64(len(r))
}
sinkValue(sink)
})
b.Run(fmt.Sprintf("bytes=%d;offset=%d", bench.size, bench.offset), func(b *testing.B) {
var sink int64
for i := 0; i < b.N; i++ {
Cipher(bts, mask, bench.offset)
sink += int64(len(bts))
}
sinkValue(sink)
})
}
}

func sinkValue(v int64) {
if r := rand.Float32(); r > 2 {
panic(fmt.Sprintf("impossible %g: %v", r, v))
}
}
41 changes: 21 additions & 20 deletions dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net"
"net/http"
"net/url"
"strconv"
"strings"
Expand Down Expand Up @@ -163,7 +164,7 @@ func (d Dialer) Dial(ctx context.Context, urlstr string) (conn net.Conn, br *buf
}
}
if conn, err = d.dial(dialctx, u); err != nil {
return
return conn, br, hs, err
}
defer func() {
if err != nil {
Expand All @@ -189,7 +190,7 @@ func (d Dialer) Dial(ctx context.Context, urlstr string) (conn net.Conn, br *buf

br, hs, err = d.Upgrade(conn, u)

return
return conn, br, hs, err
}

var (
Expand All @@ -204,7 +205,7 @@ func tlsDefaultConfig() *tls.Config {
return &tlsEmptyConfig
}

func hostport(host string, defaultPort string) (hostname, addr string) {
func hostport(host, defaultPort string) (hostname, addr string) {
var (
colon = strings.LastIndexByte(host, ':')
bracket = strings.IndexByte(host, ']')
Expand Down Expand Up @@ -241,7 +242,7 @@ func (d Dialer) dial(ctx context.Context, u *url.URL) (conn net.Conn, err error)
if wrap := d.WrapConn; wrap != nil {
conn = wrap(conn)
}
return
return conn, err
}

func (d Dialer) tlsClient(conn net.Conn, hostname string) net.Conn {
Expand Down Expand Up @@ -310,29 +311,29 @@ func (d Dialer) Upgrade(conn io.ReadWriter, u *url.URL) (br *bufio.Reader, hs Ha
initNonce(nonce)

httpWriteUpgradeRequest(bw, u, nonce, d.Protocols, d.Extensions, d.Header)
if err = bw.Flush(); err != nil {
return
if err := bw.Flush(); err != nil {
return br, hs, err
}

// Read HTTP status line like "HTTP/1.1 101 Switching Protocols".
sl, err := readLine(br)
if err != nil {
return
return br, hs, err
}
// Begin validation of the response.
// See https://tools.ietf.org/html/rfc6455#section-4.2.2
// Parse request line data like HTTP version, uri and method.
resp, err := httpParseResponseLine(sl)
if err != nil {
return
return br, hs, err
}
// Even if RFC says "1.1 or higher" without mentioning the part of the
// version, we apply it only to minor part.
if resp.major != 1 || resp.minor < 1 {
err = ErrHandshakeBadProtocol
return
return br, hs, err
}
if resp.status != 101 {
if resp.status != http.StatusSwitchingProtocols {
err = StatusError(resp.status)
if onStatusError := d.OnStatusError; onStatusError != nil {
// Invoke callback with multireader of status-line bytes br.
Expand All @@ -344,7 +345,7 @@ func (d Dialer) Upgrade(conn io.ReadWriter, u *url.URL) (br *bufio.Reader, hs Ha
),
)
}
return
return br, hs, err
}
// If response status is 101 then we expect all technical headers to be
// valid. If not, then we stop processing response without giving user
Expand All @@ -355,7 +356,7 @@ func (d Dialer) Upgrade(conn io.ReadWriter, u *url.URL) (br *bufio.Reader, hs Ha
line, e := readLine(br)
if e != nil {
err = e
return
return br, hs, err
}
if len(line) == 0 {
// Blank line, no more lines to read.
Expand All @@ -365,15 +366,15 @@ func (d Dialer) Upgrade(conn io.ReadWriter, u *url.URL) (br *bufio.Reader, hs Ha
k, v, ok := httpParseHeaderLine(line)
if !ok {
err = ErrMalformedResponse
return
return br, hs, err
}

switch btsToString(k) {
case headerUpgradeCanonical:
headerSeen |= headerSeenUpgrade
if !bytes.Equal(v, specHeaderValueUpgrade) && !bytes.EqualFold(v, specHeaderValueUpgrade) {
err = ErrHandshakeBadUpgrade
return
return br, hs, err
}

case headerConnectionCanonical:
Expand All @@ -384,14 +385,14 @@ func (d Dialer) Upgrade(conn io.ReadWriter, u *url.URL) (br *bufio.Reader, hs Ha
// multiple token. But in response it must contains exactly one.
if !bytes.Equal(v, specHeaderValueConnection) && !bytes.EqualFold(v, specHeaderValueConnection) {
err = ErrHandshakeBadConnection
return
return br, hs, err
}

case headerSecAcceptCanonical:
headerSeen |= headerSeenSecAccept
if !checkAcceptFromNonce(v, nonce) {
err = ErrHandshakeBadSecAccept
return
return br, hs, err
}

case headerSecProtocolCanonical:
Expand All @@ -409,20 +410,20 @@ func (d Dialer) Upgrade(conn io.ReadWriter, u *url.URL) (br *bufio.Reader, hs Ha
// Server echoed subprotocol that is not present in client
// requested protocols.
err = ErrHandshakeBadSubProtocol
return
return br, hs, err
}

case headerSecExtensionsCanonical:
hs.Extensions, err = matchSelectedExtensions(v, d.Extensions, hs.Extensions)
if err != nil {
return
return br, hs, err
}

default:
if onHeader := d.OnHeader; onHeader != nil {
if e := onHeader(k, v); e != nil {
err = e
return
return br, hs, err
}
}
}
Expand All @@ -439,7 +440,7 @@ func (d Dialer) Upgrade(conn io.ReadWriter, u *url.URL) (br *bufio.Reader, hs Ha
panic("unknown headers state")
}
}
return
return br, hs, err
}

// PutReader returns bufio.Reader instance to the inner reuse pool.
Expand Down
Loading

0 comments on commit 7c372c0

Please sign in to comment.