mirror of
https://github.com/interviewstreet/go-jira.git
synced 2025-07-17 01:12:24 +02:00
Some guards around url structures to help avoid bugs
This commit is contained in:
21
jira.go
21
jira.go
@ -12,6 +12,7 @@ import (
|
|||||||
|
|
||||||
"github.com/google/go-querystring/query"
|
"github.com/google/go-querystring/query"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Client manages communication with the JIRA API.
|
// A Client manages communication with the JIRA API.
|
||||||
@ -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,16 @@ 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
|
||||||
|
if !rel.IsAbs() {
|
||||||
|
rel.Path = strings.TrimLeft(rel.Path, "/")
|
||||||
|
}
|
||||||
|
|
||||||
u := c.baseURL.ResolveReference(rel)
|
u := c.baseURL.ResolveReference(rel)
|
||||||
|
|
||||||
@ -108,13 +117,16 @@ 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
|
||||||
|
if !rel.IsAbs() {
|
||||||
|
rel.Path = strings.TrimLeft(rel.Path, "/")
|
||||||
|
}
|
||||||
|
|
||||||
u := c.baseURL.ResolveReference(rel)
|
u := c.baseURL.ResolveReference(rel)
|
||||||
|
|
||||||
@ -176,13 +188,16 @@ 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
|
||||||
|
if !rel.IsAbs() {
|
||||||
|
rel.Path = strings.TrimLeft(rel.Path, "/")
|
||||||
|
}
|
||||||
|
|
||||||
u := c.baseURL.ResolveReference(rel)
|
u := c.baseURL.ResolveReference(rel)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user