mirror of
https://github.com/Threnklyn/jira.git
synced 2026-06-07 13:33:32 +02:00
tweak cookies to store hostname
dump all http request/response with --verbose
This commit is contained in:
@@ -10,11 +10,11 @@ import (
|
|||||||
"gopkg.in/op/go-logging.v1"
|
"gopkg.in/op/go-logging.v1"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -66,7 +66,22 @@ func New(opts map[string]interface{}) *Cli {
|
|||||||
return cli
|
return cli
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cli) saveCookies(cookies []*http.Cookie) {
|
func (c *Cli) saveCookies(resp *http.Response) {
|
||||||
|
if _, ok := resp.Header["Set-Cookie"]; !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cookies := resp.Cookies()
|
||||||
|
for _, cookie := range cookies {
|
||||||
|
if cookie.Domain == "" {
|
||||||
|
// if it is host:port then we need to split off port
|
||||||
|
parts := strings.Split(resp.Request.URL.Host, ":")
|
||||||
|
host := parts[0]
|
||||||
|
log.Debugf("Setting DOMAIN to %s for Cookie: %s", host, cookie)
|
||||||
|
cookie.Domain = host
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// expiry in one week from now
|
// expiry in one week from now
|
||||||
expiry := time.Now().Add(24 * 7 * time.Hour)
|
expiry := time.Now().Add(24 * 7 * time.Hour)
|
||||||
for _, cookie := range cookies {
|
for _, cookie := range cookies {
|
||||||
@@ -76,11 +91,11 @@ func (c *Cli) saveCookies(cookies []*http.Cookie) {
|
|||||||
if currentCookies := c.loadCookies(); currentCookies != nil {
|
if currentCookies := c.loadCookies(); currentCookies != nil {
|
||||||
currentCookiesByName := make(map[string]*http.Cookie)
|
currentCookiesByName := make(map[string]*http.Cookie)
|
||||||
for _, cookie := range currentCookies {
|
for _, cookie := range currentCookies {
|
||||||
currentCookiesByName[cookie.Name] = cookie
|
currentCookiesByName[cookie.Name+cookie.Domain] = cookie
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, cookie := range cookies {
|
for _, cookie := range cookies {
|
||||||
currentCookiesByName[cookie.Name] = cookie
|
currentCookiesByName[cookie.Name+cookie.Domain] = cookie
|
||||||
}
|
}
|
||||||
|
|
||||||
mergedCookies := make([]*http.Cookie, 0, len(currentCookiesByName))
|
mergedCookies := make([]*http.Cookie, 0, len(currentCookiesByName))
|
||||||
@@ -102,14 +117,17 @@ func (c *Cli) loadCookies() []*http.Cookie {
|
|||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to open %s: %s", c.cookieFile, err)
|
log.Errorf("Failed to open %s: %s", c.cookieFile, err)
|
||||||
os.Exit(1)
|
panic(err)
|
||||||
}
|
}
|
||||||
cookies := make([]*http.Cookie, 0)
|
cookies := make([]*http.Cookie, 0)
|
||||||
err = json.Unmarshal(bytes, &cookies)
|
err = json.Unmarshal(bytes, &cookies)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to parse json from file %s: %s", c.cookieFile, err)
|
log.Errorf("Failed to parse json from file %s: %s", c.cookieFile, err)
|
||||||
}
|
}
|
||||||
log.Debugf("Loading Cookies: %s", cookies)
|
|
||||||
|
if os.Getenv("LOG_TRACE") != "" && log.IsEnabledFor(logging.DEBUG) {
|
||||||
|
log.Debugf("Loading Cookies: %s", cookies)
|
||||||
|
}
|
||||||
return cookies
|
return cookies
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,15 +162,6 @@ func (c *Cli) makeRequestWithContent(method string, uri string, content string)
|
|||||||
req, _ := http.NewRequest(method, uri, buffer)
|
req, _ := http.NewRequest(method, uri, buffer)
|
||||||
|
|
||||||
log.Infof("%s %s", req.Method, req.URL.String())
|
log.Infof("%s %s", req.Method, req.URL.String())
|
||||||
if log.IsEnabledFor(logging.DEBUG) {
|
|
||||||
logBuffer := bytes.NewBuffer(make([]byte, 0, len(content)))
|
|
||||||
req.Write(logBuffer)
|
|
||||||
log.Debugf("%s", logBuffer)
|
|
||||||
// need to recreate the buffer since the offset is now at the end
|
|
||||||
// need to be able to rewind the buffer offset, dont know how yet
|
|
||||||
req, _ = http.NewRequest(method, uri, bytes.NewBufferString(content))
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp, err := c.makeRequest(req); err != nil {
|
if resp, err := c.makeRequest(req); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
@@ -190,7 +199,20 @@ func (c *Cli) get(uri string) (*http.Response, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cli) makeRequest(req *http.Request) (resp *http.Response, err error) {
|
func (c *Cli) makeRequest(req *http.Request) (resp *http.Response, err error) {
|
||||||
|
req.Header.Set("Accept", "application/json")
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
// this is actually done in http.send but doing it
|
||||||
|
// here so we can log it in DumpRequest for debugging
|
||||||
|
for _, cookie := range c.ua.Jar.Cookies(req.URL) {
|
||||||
|
req.AddCookie(cookie)
|
||||||
|
}
|
||||||
|
|
||||||
|
if log.IsEnabledFor(logging.DEBUG) {
|
||||||
|
out, _ := httputil.DumpRequest(req,true);
|
||||||
|
log.Debugf("Request: %s", out)
|
||||||
|
}
|
||||||
|
|
||||||
if resp, err = c.ua.Do(req); err != nil {
|
if resp, err = c.ua.Do(req); err != nil {
|
||||||
log.Errorf("Failed to %s %s: %s", req.Method, req.URL.String(), err)
|
log.Errorf("Failed to %s %s: %s", req.Method, req.URL.String(), err)
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -204,9 +226,13 @@ func (c *Cli) makeRequest(req *http.Request) (resp *http.Response, err error) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if _, ok := resp.Header["Set-Cookie"]; ok {
|
if _, ok := resp.Header["Set-Cookie"]; ok {
|
||||||
c.saveCookies(resp.Cookies())
|
c.saveCookies(resp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if log.IsEnabledFor(logging.DEBUG) {
|
||||||
|
out, _ := httputil.DumpResponse(resp,true);
|
||||||
|
log.Debugf("Response: %s", out)
|
||||||
|
}
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,8 +28,6 @@ func (c *Cli) CmdLogin() error {
|
|||||||
if resp, err := c.makeRequest(req); err != nil {
|
if resp, err := c.makeRequest(req); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
out, _ := httputil.DumpResponse(resp, true)
|
|
||||||
log.Debugf("%s", out)
|
|
||||||
if resp.StatusCode == 403 {
|
if resp.StatusCode == 403 {
|
||||||
// probably got this, need to redirect the user to login manually
|
// probably got this, need to redirect the user to login manually
|
||||||
// X-Authentication-Denied-Reason: CAPTCHA_CHALLENGE; login-url=https://jira/login.jsp
|
// X-Authentication-Denied-Reason: CAPTCHA_CHALLENGE; login-url=https://jira/login.jsp
|
||||||
@@ -260,7 +258,6 @@ func (c *Cli) CmdCreate() error {
|
|||||||
fmt.Sprintf("create-%s-", sanitizedType),
|
fmt.Sprintf("create-%s-", sanitizedType),
|
||||||
issueData,
|
issueData,
|
||||||
func(json string) error {
|
func(json string) error {
|
||||||
log.Debugf("JSON: %s", json)
|
|
||||||
uri := fmt.Sprintf("%s/rest/api/2/issue", c.endpoint)
|
uri := fmt.Sprintf("%s/rest/api/2/issue", c.endpoint)
|
||||||
if c.getOptBool("dryrun", false) {
|
if c.getOptBool("dryrun", false) {
|
||||||
log.Debugf("POST: %s", json)
|
log.Debugf("POST: %s", json)
|
||||||
@@ -519,8 +516,6 @@ func (c *Cli) CmdTransition(issue string, trans string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handlePost := func(json string) error {
|
handlePost := func(json string) error {
|
||||||
log.Debugf("POST: %s", json)
|
|
||||||
// os.Exit(0)
|
|
||||||
uri = fmt.Sprintf("%s/rest/api/2/issue/%s/transitions", c.endpoint, issue)
|
uri = fmt.Sprintf("%s/rest/api/2/issue/%s/transitions", c.endpoint, issue)
|
||||||
if c.getOptBool("dryrun", false) {
|
if c.getOptBool("dryrun", false) {
|
||||||
log.Debugf("POST: %s", json)
|
log.Debugf("POST: %s", json)
|
||||||
@@ -572,7 +567,6 @@ func (c *Cli) CmdComment(issue string) error {
|
|||||||
log.Debugf("comment called")
|
log.Debugf("comment called")
|
||||||
|
|
||||||
handlePost := func(json string) error {
|
handlePost := func(json string) error {
|
||||||
log.Debugf("JSON: %s", json)
|
|
||||||
uri := fmt.Sprintf("%s/rest/api/2/issue/%s/comment", c.endpoint, issue)
|
uri := fmt.Sprintf("%s/rest/api/2/issue/%s/comment", c.endpoint, issue)
|
||||||
if c.getOptBool("dryrun", false) {
|
if c.getOptBool("dryrun", false) {
|
||||||
log.Debugf("POST: %s", json)
|
log.Debugf("POST: %s", json)
|
||||||
@@ -669,7 +663,6 @@ func (c *Cli) CmdLabels(action string, issue string, labels []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handlePut := func(json string) error {
|
handlePut := func(json string) error {
|
||||||
log.Debugf("JSON: %s", json)
|
|
||||||
uri := fmt.Sprintf("%s/rest/api/2/issue/%s", c.endpoint, issue)
|
uri := fmt.Sprintf("%s/rest/api/2/issue/%s", c.endpoint, issue)
|
||||||
if c.getOptBool("dryrun", false) {
|
if c.getOptBool("dryrun", false) {
|
||||||
log.Debugf("PUT: %s", json)
|
log.Debugf("PUT: %s", json)
|
||||||
|
|||||||
Reference in New Issue
Block a user