mirror of
https://github.com/interviewstreet/go-jira.git
synced 2025-04-19 12:02:20 +02:00
Merge pull request #130 from shdunning/issue-129
Some guards around url structures to help avoid bugs
This commit is contained in:
commit
05574a46ff
15
jira.go
15
jira.go
@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/go-querystring/query"
|
"github.com/google/go-querystring/query"
|
||||||
@ -48,6 +49,11 @@ func NewClient(httpClient *http.Client, baseURL string) (*Client, error) {
|
|||||||
httpClient = http.DefaultClient
|
httpClient = http.DefaultClient
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ensure the baseURL contains a trailing slash so that all paths are preserved in later calls
|
||||||
|
if !strings.HasSuffix(baseURL, "/") {
|
||||||
|
baseURL += "/"
|
||||||
|
}
|
||||||
|
|
||||||
parsedBaseURL, err := url.Parse(baseURL)
|
parsedBaseURL, err := url.Parse(baseURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -71,13 +77,14 @@ func NewClient(httpClient *http.Client, baseURL string) (*Client, error) {
|
|||||||
|
|
||||||
// NewRawRequest creates an API request.
|
// NewRawRequest creates an API request.
|
||||||
// A relative URL can be provided in urlStr, in which case it is resolved relative to the baseURL of the Client.
|
// A relative URL can be provided in urlStr, in which case it is resolved relative to the baseURL of the Client.
|
||||||
// Relative URLs should always be specified without a preceding slash.
|
|
||||||
// Allows using an optional native io.Reader for sourcing the request body.
|
// Allows using an optional native io.Reader for sourcing the request body.
|
||||||
func (c *Client) NewRawRequest(method, urlStr string, body io.Reader) (*http.Request, error) {
|
func (c *Client) NewRawRequest(method, urlStr string, body io.Reader) (*http.Request, error) {
|
||||||
rel, err := url.Parse(urlStr)
|
rel, err := url.Parse(urlStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
// Relative URLs should be specified without a preceding slash since baseURL will have the trailing slash
|
||||||
|
rel.Path = strings.TrimLeft(rel.Path, "/")
|
||||||
|
|
||||||
u := c.baseURL.ResolveReference(rel)
|
u := c.baseURL.ResolveReference(rel)
|
||||||
|
|
||||||
@ -108,13 +115,14 @@ func (c *Client) NewRawRequest(method, urlStr string, body io.Reader) (*http.Req
|
|||||||
|
|
||||||
// NewRequest creates an API request.
|
// NewRequest creates an API request.
|
||||||
// A relative URL can be provided in urlStr, in which case it is resolved relative to the baseURL of the Client.
|
// A relative URL can be provided in urlStr, in which case it is resolved relative to the baseURL of the Client.
|
||||||
// Relative URLs should always be specified without a preceding slash.
|
|
||||||
// If specified, the value pointed to by body is JSON encoded and included as the request body.
|
// If specified, the value pointed to by body is JSON encoded and included as the request body.
|
||||||
func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error) {
|
func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error) {
|
||||||
rel, err := url.Parse(urlStr)
|
rel, err := url.Parse(urlStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
// Relative URLs should be specified without a preceding slash since baseURL will have the trailing slash
|
||||||
|
rel.Path = strings.TrimLeft(rel.Path, "/")
|
||||||
|
|
||||||
u := c.baseURL.ResolveReference(rel)
|
u := c.baseURL.ResolveReference(rel)
|
||||||
|
|
||||||
@ -176,13 +184,14 @@ func addOptions(s string, opt interface{}) (string, error) {
|
|||||||
|
|
||||||
// NewMultiPartRequest creates an API request including a multi-part file.
|
// NewMultiPartRequest creates an API request including a multi-part file.
|
||||||
// A relative URL can be provided in urlStr, in which case it is resolved relative to the baseURL of the Client.
|
// A relative URL can be provided in urlStr, in which case it is resolved relative to the baseURL of the Client.
|
||||||
// Relative URLs should always be specified without a preceding slash.
|
|
||||||
// If specified, the value pointed to by buf is a multipart form.
|
// If specified, the value pointed to by buf is a multipart form.
|
||||||
func (c *Client) NewMultiPartRequest(method, urlStr string, buf *bytes.Buffer) (*http.Request, error) {
|
func (c *Client) NewMultiPartRequest(method, urlStr string, buf *bytes.Buffer) (*http.Request, error) {
|
||||||
rel, err := url.Parse(urlStr)
|
rel, err := url.Parse(urlStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
// Relative URLs should be specified without a preceding slash since baseURL will have the trailing slash
|
||||||
|
rel.Path = strings.TrimLeft(rel.Path, "/")
|
||||||
|
|
||||||
u := c.baseURL.ResolveReference(rel)
|
u := c.baseURL.ResolveReference(rel)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user