1
0
mirror of https://github.com/interviewstreet/go-jira.git synced 2025-02-03 13:11:49 +02:00

Merge pull request #130 from shdunning/issue-129

Some guards around url structures to help avoid bugs
This commit is contained in:
rbriski 2018-05-19 20:24:15 -07:00 committed by GitHub
commit 05574a46ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

15
jira.go
View File

@ -8,6 +8,7 @@ import (
"net/http"
"net/url"
"reflect"
"strings"
"time"
"github.com/google/go-querystring/query"
@ -48,6 +49,11 @@ func NewClient(httpClient *http.Client, baseURL string) (*Client, error) {
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)
if err != nil {
return nil, err
@ -71,13 +77,14 @@ func NewClient(httpClient *http.Client, baseURL string) (*Client, error) {
// 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.
// Relative URLs should always be specified without a preceding slash.
// 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) {
rel, err := url.Parse(urlStr)
if err != nil {
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)
@ -108,13 +115,14 @@ func (c *Client) NewRawRequest(method, urlStr string, body io.Reader) (*http.Req
// 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.
// 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.
func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error) {
rel, err := url.Parse(urlStr)
if err != nil {
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)
@ -176,13 +184,14 @@ func addOptions(s string, opt interface{}) (string, error) {
// 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.
// Relative URLs should always be specified without a preceding slash.
// 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) {
rel, err := url.Parse(urlStr)
if err != nil {
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)