Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added option for dialer to follow redirects #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
48 changes: 36 additions & 12 deletions dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ type Dialer struct {
// Note that for debugging purposes of an http handshake (e.g. sent request
// and received response), there is an wsutil.DebugDialer struct.
WrapConn func(conn net.Conn) net.Conn

// FollowRedirects is a boolean specifying if to follow redirects
// Redirects are any 3xx responses coming from the server
FollowRedirects bool
}

// Dial connects to the url host and upgrades connection to WebSocket.
Expand Down Expand Up @@ -188,7 +192,10 @@ func (d Dialer) Dial(ctx context.Context, urlstr string) (conn net.Conn, br *buf
}

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


if e, ok := err.(RedirectError); ok {
return d.Dial(ctx, string(e))
}
return
}

Expand Down Expand Up @@ -332,19 +339,24 @@ func (d Dialer) Upgrade(conn io.ReadWriter, u *url.URL) (br *bufio.Reader, hs Ha
err = ErrHandshakeBadProtocol
return
}
var isRedirect bool
if resp.status != 101 {
err = StatusError(resp.status)
if onStatusError := d.OnStatusError; onStatusError != nil {
// Invoke callback with multireader of status-line bytes br.
onStatusError(resp.status, resp.reason,
io.MultiReader(
bytes.NewReader(sl),
strings.NewReader(crlf),
br,
),
)
if resp.status >= 300 && resp.status < 400 && d.FollowRedirects {
isRedirect = true // Cant return, need to process Location header
} else {
err = StatusError(resp.status)
if onStatusError := d.OnStatusError; onStatusError != nil {
// Invoke callback with multireader of status-line bytes br.
onStatusError(resp.status, resp.reason,
io.MultiReader(
bytes.NewReader(sl),
strings.NewReader(crlf),
br,
),
)
}
return
}
return
}
// 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 @@ -369,6 +381,12 @@ func (d Dialer) Upgrade(conn io.ReadWriter, u *url.URL) (br *bufio.Reader, hs Ha
}

switch btsToString(k) {
case headerLocationCanonical:
if d.FollowRedirects && isRedirect {
err = RedirectError(string(v))
return
}

case headerUpgradeCanonical:
headerSeen |= headerSeenUpgrade
if !bytes.Equal(v, specHeaderValueUpgrade) && !bytes.EqualFold(v, specHeaderValueUpgrade) {
Expand Down Expand Up @@ -457,6 +475,12 @@ func (s StatusError) Error() string {
return "unexpected HTTP response status: " + strconv.Itoa(int(s))
}

type RedirectError string

func (r RedirectError) Error() string {
return "received redirect from server to: " + string(r)
}

func isTimeoutError(err error) bool {
t, ok := err.(net.Error)
return ok && t.Timeout()
Expand Down
2 changes: 2 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
headerHost = "Host"
headerUpgrade = "Upgrade"
headerConnection = "Connection"
headerLocation = "Location"
headerSecVersion = "Sec-WebSocket-Version"
headerSecProtocol = "Sec-WebSocket-Protocol"
headerSecExtensions = "Sec-WebSocket-Extensions"
Expand All @@ -51,6 +52,7 @@ var (
headerHostCanonical = textproto.CanonicalMIMEHeaderKey(headerHost)
headerUpgradeCanonical = textproto.CanonicalMIMEHeaderKey(headerUpgrade)
headerConnectionCanonical = textproto.CanonicalMIMEHeaderKey(headerConnection)
headerLocationCanonical = textproto.CanonicalMIMEHeaderKey(headerLocation)
headerSecVersionCanonical = textproto.CanonicalMIMEHeaderKey(headerSecVersion)
headerSecProtocolCanonical = textproto.CanonicalMIMEHeaderKey(headerSecProtocol)
headerSecExtensionsCanonical = textproto.CanonicalMIMEHeaderKey(headerSecExtensions)
Expand Down