This repository has been archived by the owner on Jun 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to fetch from URLs that contain allowed reserved delimiters
Go has a bug where it does not include all allowed reserved sub-delimiters As a workaround, a http.Get is extended to keep the paths un-escaped. It is expected that URL are previously escaped, so there should be no negative side effect For more information on bug: golang/go#5684
- Loading branch information
1 parent
4e8182b
commit 10134fc
Showing
2 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
// A Client extends http.Client and patches incorrect URL path escaping | ||
// for more information on bug: https://github.com/golang/go/issues/5684 | ||
type Client struct { | ||
http.Client | ||
} | ||
|
||
// Get issues a GET to the specified URL. If the response is one of the | ||
// following redirect codes, Get follows the redirect after calling the | ||
// Client's CheckRedirect function. | ||
// | ||
// 301 (Moved Permanently) | ||
// 302 (Found) | ||
// 303 (See Other) | ||
// 307 (Temporary Redirect) | ||
// | ||
// An error is returned if the Client's CheckRedirect function fails | ||
// or if there was an HTTP protocol error. A non-2xx response doesn't | ||
// cause an error. | ||
// | ||
// When err is nil, resp always contains a non-nil resp.Body. | ||
// Caller should close resp.Body when done reading from it. | ||
// | ||
func (c *Client) Get(urlStr string) (resp *http.Response, err error) { | ||
fu, err := url.Parse(urlStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
u := &url.URL{ | ||
Scheme: fu.Scheme, | ||
Host: fu.Host, | ||
Opaque: fu.Path, | ||
} | ||
|
||
req := &http.Request{ | ||
Method: "GET", | ||
URL: u, | ||
Proto: "HTTP/1.1", | ||
ProtoMajor: 1, | ||
ProtoMinor: 1, | ||
Header: make(http.Header), | ||
Host: u.Host, | ||
} | ||
|
||
return c.Do(req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters