Unverified Commit 3ed7a99f authored by Eric_Lee's avatar Eric_Lee Committed by GitHub

Merge pull request #132 from jumpserver/dev_bugfix

[Bugfix] fix unclosed fd issues
parents 4a509ca4 47105d55
...@@ -144,6 +144,9 @@ func (c *Client) NewRequest(method, url string, body interface{}, params []map[s ...@@ -144,6 +144,9 @@ func (c *Client) NewRequest(method, url string, body interface{}, params []map[s
return return
} }
req, err = http.NewRequest(method, url, reader) req, err = http.NewRequest(method, url, reader)
if err != nil {
return req, err
}
c.SetReqHeaders(req, params) c.SetReqHeaders(req, params)
return req, err return req, err
} }
...@@ -219,7 +222,7 @@ func (c *Client) PostForm(url string, data interface{}, res interface{}) (err er ...@@ -219,7 +222,7 @@ func (c *Client) PostForm(url string, data interface{}, res interface{}) (err er
default: default:
attr, err := json.Marshal(val.Field(i).Interface()) attr, err := json.Marshal(val.Field(i).Interface())
if err != nil { if err != nil {
return nil return err
} }
v = string(attr) v = string(attr)
} }
...@@ -229,6 +232,9 @@ func (c *Client) PostForm(url string, data interface{}, res interface{}) (err er ...@@ -229,6 +232,9 @@ func (c *Client) PostForm(url string, data interface{}, res interface{}) (err er
reader := strings.NewReader(values.Encode()) reader := strings.NewReader(values.Encode())
req, err := http.NewRequest("POST", url, reader) req, err := http.NewRequest("POST", url, reader)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return nil return nil
} }
...@@ -238,6 +244,7 @@ func (c *Client) UploadFile(url string, gFile string, res interface{}, params .. ...@@ -238,6 +244,7 @@ func (c *Client) UploadFile(url string, gFile string, res interface{}, params ..
if err != nil { if err != nil {
return err return err
} }
defer f.Close()
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
bodyWriter := multipart.NewWriter(buf) bodyWriter := multipart.NewWriter(buf)
gName := filepath.Base(gFile) gName := filepath.Base(gFile)
...@@ -245,18 +252,23 @@ func (c *Client) UploadFile(url string, gFile string, res interface{}, params .. ...@@ -245,18 +252,23 @@ func (c *Client) UploadFile(url string, gFile string, res interface{}, params ..
if err != nil { if err != nil {
return err return err
} }
_, err = io.Copy(part, f) if _, err = io.Copy(part, f); err != nil {
return err
}
err = bodyWriter.Close() err = bodyWriter.Close()
if err != nil { if err != nil {
return err return err
} }
url = c.parseUrl(url, params) url = c.parseUrl(url, params)
req, err := http.NewRequest("POST", url, buf) req, err := http.NewRequest("POST", url, buf)
if err != nil {
return err
}
req.Header.Set("Content-Type", bodyWriter.FormDataContentType()) req.Header.Set("Content-Type", bodyWriter.FormDataContentType())
c.SetReqHeaders(req, params) c.SetReqHeaders(req, params)
resp, err := c.http.Do(req) resp, err := c.http.Do(req)
if err != nil { if err != nil {
return return err
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment