diff options
author | Unknwon <joe2010xtmf@163.com> | 2014-11-18 15:13:08 -0500 |
---|---|---|
committer | Unknwon <joe2010xtmf@163.com> | 2014-11-18 15:13:08 -0500 |
commit | ce8d4cc80ba908022f0bbaf796b9025cfb68956e (patch) | |
tree | 32cee38f40a7ba70fb68c56db23d495cbf5fdfed /modules/httplib | |
parent | 37d8d3afe9ec589574c0cc6380a36fa93b1be8f2 (diff) | |
download | gitea-ce8d4cc80ba908022f0bbaf796b9025cfb68956e.tar.gz gitea-ce8d4cc80ba908022f0bbaf796b9025cfb68956e.zip |
#634
Diffstat (limited to 'modules/httplib')
-rw-r--r-- | modules/httplib/httplib.go | 58 | ||||
-rw-r--r-- | modules/httplib/httplib_test.go | 44 |
2 files changed, 57 insertions, 45 deletions
diff --git a/modules/httplib/httplib.go b/modules/httplib/httplib.go index 5f592168dc..171dfe0139 100644 --- a/modules/httplib/httplib.go +++ b/modules/httplib/httplib.go @@ -5,7 +5,7 @@ package httplib -// NOTE: last sync c07b1d8 on Aug 23, 2014. +// NOTE: last sync 57e62e5 on Oct 29, 2014. import ( "bytes" @@ -14,6 +14,7 @@ import ( "encoding/xml" "io" "io/ioutil" + "log" "mime/multipart" "net" "net/http" @@ -252,35 +253,36 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { } else { b.url = b.url + "?" + paramBody } - } else if b.req.Method == "POST" && b.req.Body == nil && len(paramBody) > 0 { + } else if b.req.Method == "POST" && b.req.Body == nil { if len(b.files) > 0 { - bodyBuf := &bytes.Buffer{} - bodyWriter := multipart.NewWriter(bodyBuf) - for formname, filename := range b.files { - fileWriter, err := bodyWriter.CreateFormFile(formname, filename) - if err != nil { - return nil, err + pr, pw := io.Pipe() + bodyWriter := multipart.NewWriter(pw) + go func() { + for formname, filename := range b.files { + fileWriter, err := bodyWriter.CreateFormFile(formname, filename) + if err != nil { + log.Fatal(err) + } + fh, err := os.Open(filename) + if err != nil { + log.Fatal(err) + } + //iocopy + _, err = io.Copy(fileWriter, fh) + fh.Close() + if err != nil { + log.Fatal(err) + } } - fh, err := os.Open(filename) - if err != nil { - return nil, err + for k, v := range b.params { + bodyWriter.WriteField(k, v) } - //iocopy - _, err = io.Copy(fileWriter, fh) - fh.Close() - if err != nil { - return nil, err - } - } - for k, v := range b.params { - bodyWriter.WriteField(k, v) - } - contentType := bodyWriter.FormDataContentType() - bodyWriter.Close() - b.Header("Content-Type", contentType) - b.req.Body = ioutil.NopCloser(bodyBuf) - b.req.ContentLength = int64(bodyBuf.Len()) - } else { + bodyWriter.Close() + pw.Close() + }() + b.Header("Content-Type", bodyWriter.FormDataContentType()) + b.req.Body = ioutil.NopCloser(pr) + } else if len(paramBody) > 0 { b.Header("Content-Type", "application/x-www-form-urlencoded") b.Body(paramBody) } @@ -332,7 +334,7 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { Jar: jar, } - if b.setting.UserAgent != "" { + if len(b.setting.UserAgent) > 0 && len(b.req.Header.Get("User-Agent")) == 0 { b.req.Header.Set("User-Agent", b.setting.UserAgent) } diff --git a/modules/httplib/httplib_test.go b/modules/httplib/httplib_test.go index 8a529c53a6..e3f6b5d409 100644 --- a/modules/httplib/httplib_test.go +++ b/modules/httplib/httplib_test.go @@ -57,23 +57,23 @@ func TestSimplePost(t *testing.T) { } } -func TestPostFile(t *testing.T) { - v := "smallfish" - req := Post("http://httpbin.org/post") - req.Param("username", v) - req.PostFile("uploadfile", "httplib_test.go") - - str, err := req.String() - if err != nil { - t.Fatal(err) - } - t.Log(str) - - n := strings.Index(str, v) - if n == -1 { - t.Fatal(v + " not found in post") - } -} +// func TestPostFile(t *testing.T) { +// v := "smallfish" +// req := Post("http://httpbin.org/post") +// req.Param("username", v) +// req.PostFile("uploadfile", "httplib_test.go") + +// str, err := req.String() +// if err != nil { +// t.Fatal(err) +// } +// t.Log(str) + +// n := strings.Index(str, v) +// if n == -1 { +// t.Fatal(v + " not found in post") +// } +// } func TestSimplePut(t *testing.T) { str, err := Put("http://httpbin.org/put").String() @@ -194,3 +194,13 @@ func TestToFile(t *testing.T) { t.Fatal(err) } } + +func TestHeader(t *testing.T) { + req := Get("http://httpbin.org/headers") + req.Header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36") + str, err := req.String() + if err != nil { + t.Fatal(err) + } + t.Log(str) +} |