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

Handle EOF from multipart form file readers #438

Merged
merged 1 commit into from Sep 13, 2021
Merged

Handle EOF from multipart form file readers #438

merged 1 commit into from Sep 13, 2021

Conversation

NathanBaulch
Copy link
Contributor

@NathanBaulch NathanBaulch commented Jul 15, 2021

I stumbled on a bug when trying to send a multipart file that happened to be a gzip reader downloaded from elsewhere. Looks like the multipart content type detection logic doesn't handle EOF correctly. This can also be easily reproduced by sending an empty file (bytes.NewReader(nil)).

Here's a self contained example that reproduces the issue with a gzipped string:

b := bytes.NewBuffer(nil)
w := gzip.NewWriter(b)
if _, err := w.Write([]byte("foo")); err != nil {
	panic(err)
}
if err := w.Close(); err != nil {
	panic(err)
}
r, err := gzip.NewReader(b)
if err != nil {
	panic(err)
}
_, err = resty.New().NewRequest().SetFileReader("file", "foo.bar", r).Post("http://foo.bar")
if err != nil {
	panic(err) // EOF!
}

Closes #431

@NathanBaulch
Copy link
Contributor Author

Bump @jeevatkm

@jeevatkm
Copy link
Member

jeevatkm commented Sep 12, 2021

@NathanBaulch Thank you for your PR appreciated, I'm sorry for the delayed response.

Travis CI build link was broken. I have migrated travis-ci.org to travis-ci.com

Can you please edit the PR to trigger the CI build?

@codecov
Copy link

codecov bot commented Sep 12, 2021

Codecov Report

Merging #438 (716ebeb) into master (c1fa358) will increase coverage by 0.15%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #438      +/-   ##
==========================================
+ Coverage   96.66%   96.81%   +0.15%     
==========================================
  Files          10       10              
  Lines        1320     1320              
==========================================
+ Hits         1276     1278       +2     
+ Misses         25       24       -1     
+ Partials       19       18       -1     
Impacted Files Coverage Δ
util.go 95.55% <100.00%> (+1.11%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c1fa358...716ebeb. Read the comment docs.

Copy link
Member

@jeevatkm jeevatkm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NathanBaulch It seems the test case is not covered for this PR update, can you please take a look at it?

https://app.codecov.io/gh/go-resty/resty/compare/438/diff#diff-dXRpbC5nbw==

@NathanBaulch
Copy link
Contributor Author

The test case doesn't increase coverage because my fix is all about avoiding a false error. There is no new code path that the test could hit.

The test fails without the fix, passes with the fix and doesn't impact coverage, as expected. Hopefully that makes sense.

@jeevatkm
Copy link
Member

@NathanBaulch Yes, it makes sense.
Actually, I was aiming to get coverage for the "error happened before reaching EOF" use case.

@NathanBaulch
Copy link
Contributor Author

Not really relevant to my fix but sure, I've added a test that borrows brokenReadCloser from the request tests.

Copy link
Member

@jeevatkm jeevatkm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NathanBaulch Thanks for your PR.

@jeevatkm jeevatkm added bug v2 For resty v2 labels Sep 13, 2021
@jeevatkm jeevatkm added this to the v2.7.0 Milestone milestone Sep 13, 2021
@jeevatkm jeevatkm merged commit 3eedf3b into go-resty:master Sep 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Upload empty file get "EOF" error
2 participants