Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer less Large file support on body #136

Merged
merged 7 commits into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Resty first version released on Sep 15, 2015 then it grew gradually as a very ha
* Simple and chainable methods for settings and request
* Request Body can be `string`, `[]byte`, `struct`, `map`, `slice` and `io.Reader` too
* Auto detects `Content-Type`
* Buffer less processing for `io.Reader`
* [Response](https://godoc.org/github.com/go-resty/resty#Response) object gives you more possibility
* Access as `[]byte` array - `response.Body()` OR Access as `string` - `response.String()`
* Know your `response.Time()` and when we `response.ReceivedAt()`
Expand Down
16 changes: 13 additions & 3 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ CL:

func createHTTPRequest(c *Client, r *Request) (err error) {
if r.bodyBuf == nil {
r.RawRequest, err = http.NewRequest(r.Method, r.URL, nil)
if reader, ok := r.Body.(io.Reader); ok {
r.RawRequest, err = http.NewRequest(r.Method, r.URL, reader)
} else {
r.RawRequest, err = http.NewRequest(r.Method, r.URL, nil)
}
} else {
r.RawRequest, err = http.NewRequest(r.Method, r.URL, r.bodyBuf)
}
Expand Down Expand Up @@ -369,8 +373,14 @@ func handleRequestBody(c *Client, r *Request) (err error) {
r.bodyBuf = nil

if reader, ok := r.Body.(io.Reader); ok {
r.bodyBuf = acquireBuffer()
_, err = r.bodyBuf.ReadFrom(reader)
if c.setContentLength || r.setContentLength { // keep backward compability
r.bodyBuf = acquireBuffer()
_, err = r.bodyBuf.ReadFrom(reader)
r.Body = nil
} else {
// Otherwise buffer less processing for `io.Reader`, sounds good.
return
}
} else if b, ok := r.Body.([]byte); ok {
bodyBytes = b
} else if s, ok := r.Body.(string); ok {
Expand Down
5 changes: 5 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,11 @@ func (r *Request) Execute(method, url string) (*Response, error) {
func (r *Request) fmtBodyString() (body string) {
body = "***** NO CONTENT *****"
if isPayloadSupported(r.Method, r.client.AllowGetMethodPayload) {
if _, ok := r.Body.(io.Reader); ok {
body = "***** BODY IS io.Reader *****"
return
}

// multipart or form-data
if r.isMultiPart || r.isFormData {
body = string(r.bodyBuf.Bytes())
Expand Down
30 changes: 30 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1341,3 +1341,33 @@ func TestRequestOverridesClientAuthorizationHeader(t *testing.T) {
assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
}

func TestRequestFileUploadAsReader(t *testing.T) {
ts := createFilePostServer(t)
defer ts.Close()

file, _ := os.Open(getTestDataPath() + "/test-img.png")
defer file.Close()

resp, err := dclr().
SetBody(file).
SetHeader("Content-Type", "image/png").
Post(ts.URL + "/upload")

assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
assertEqual(t, true, strings.Contains(resp.String(), "File Uploaded successfully"))

file, _ = os.Open(getTestDataPath() + "/test-img.png")
defer file.Close()

resp, err = dclr().
SetBody(file).
SetHeader("Content-Type", "image/png").
SetContentLength(true).
Post(ts.URL + "/upload")

assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
assertEqual(t, true, strings.Contains(resp.String(), "File Uploaded successfully"))
}
43 changes: 42 additions & 1 deletion resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,43 @@ func createFormPostServer(t *testing.T) *httptest.Server {
return ts
}

func createFilePostServer(t *testing.T) *httptest.Server {
ts := createTestServer(func(w http.ResponseWriter, r *http.Request) {
t.Logf("Method: %v", r.Method)
t.Logf("Path: %v", r.URL.Path)
t.Logf("Content-Type: %v", r.Header.Get(hdrContentTypeKey))

if r.Method != MethodPost {
t.Log("createPostServer:: Not a Post request")
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, http.StatusText(http.StatusBadRequest))
return
}

targetPath := filepath.Join(getTestDataPath(), "upload-large")
_ = os.MkdirAll(targetPath, 0700)
defer cleanupFiles(targetPath)

switch r.URL.Path {
case "/upload":
f, err := os.OpenFile(filepath.Join(targetPath, "large-file.png"),
os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
t.Logf("Error: %v", err)
return
}
defer func() {
_ = f.Close()
}()
size, _ := io.Copy(f, r.Body)

fmt.Fprintf(w, "File Uploaded successfully, file size: %v", size)
}
})

return ts
}

func createAuthServer(t *testing.T) *httptest.Server {
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Logf("Method: %v", r.Method)
Expand Down Expand Up @@ -554,6 +591,10 @@ func cleanupFiles(files ...string) {
pwd, _ := os.Getwd()

for _, f := range files {
_ = os.RemoveAll(filepath.Join(pwd, f))
if filepath.IsAbs(f) {
_ = os.RemoveAll(f)
} else {
_ = os.RemoveAll(filepath.Join(pwd, f))
}
}
}