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

Proxy forms without relying on the request body #61

Merged
merged 6 commits into from
Aug 5, 2020

Conversation

PreferLinux
Copy link
Contributor

Fixes #57 and replaced #60.

For requests with a form content type .Net seems to consume the request body if model binding / method parameters are present on a controller, even if they should be using route or query string values. Avoid the problem by re-creating the request body from the form data.

@twitchax
Copy link
Owner

twitchax commented Aug 4, 2020

This is awesome: thanks!

Copy link
Owner

@twitchax twitchax left a comment

Choose a reason for hiding this comment

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

LGTM, would like some comments.

@@ -67,5 +72,35 @@ internal static string TrimTrailingSlashes(this string s)

return s.Substring(0, s.Length - count);
}

internal static HttpContent ToHttpContent(this IFormCollection collection, string contentType)
{
Copy link
Owner

Choose a reason for hiding this comment

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

Can you add just a few comments?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, I hope they're what you're after. I've also made a few other changes, including at least one that was a fix.

Unfortunately that for some reason has created a code coverage issue for the invalid content type path, and said path is impossible to hit due to how this is used...

Copy link
Owner

Choose a reason for hiding this comment

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

That's fine. I can write unit test to wriggle in there.

if (!contentType.StartsWith("multipart/form-data;", StringComparison.OrdinalIgnoreCase))
throw new Exception($"Unknown form content type \"{contentType[0]}\"");

const string boundary = "boundary=";
Copy link
Owner

Choose a reason for hiding this comment

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

For example, a comment with a link to how this boundary works would be cool. 😄

This allows avoiding string manipulation to get a multipart boundary,
and also ensures no non-interesting parameters interefere with matching.

I've removed the check for an empty content type, as it is
unnecessary.

Also added comments.
@codecov-commenter
Copy link

Codecov Report

Merging #61 into master will decrease coverage by 0.15%.
The diff coverage is 96.96%.

Impacted file tree graph

@@             Coverage Diff             @@
##            master      #61      +/-   ##
===========================================
- Coverage   100.00%   99.84%   -0.16%     
===========================================
  Files           12       12              
  Lines          628      658      +30     
===========================================
+ Hits           628      657      +29     
- Misses           0        1       +1     
Impacted Files Coverage Δ
src/Core/Helpers/Helpers.cs 97.29% <95.45%> (-2.71%) ⬇️
src/Core/Extensions/Http.cs 100.00% <100.00%> (ø)

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 46fbd3f...e44b8b5. Read the comment docs.

@twitchax twitchax merged commit afa132d into twitchax:master Aug 5, 2020
@twitchax
Copy link
Owner

twitchax commented Aug 5, 2020

@PreferLinux, didn't think it needed a PR...

8fdd471

@PreferLinux
Copy link
Contributor Author

Looks good!

@PreferLinux PreferLinux deleted the form_fix branch August 5, 2020 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hang when using a controller to proxy form
3 participants