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

fix(server): fallthrough non GET and HEAD request to routes defined in after option #2374

Merged
merged 1 commit into from Dec 29, 2019

Conversation

jvasseur
Copy link
Contributor

@jvasseur jvasseur commented Dec 26, 2019

  • This is a bugfix
  • This is a feature
  • This is a code refactor
  • This is a test update
  • This is a docs update
  • This is a metadata update

For Bugs and Features; did you add new tests?

Yes

Motivation / Use-Case

Before #2150, non-GET request where forwarded to any middleware defined in the after option, since #2150, they are handled by serve-index instead that always reply with a 405 error.

This fix issue #2370.

Breaking Changes

No

Additional Info

Closes #2370

@jsf-clabot
Copy link

jsf-clabot commented Dec 26, 2019

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Dec 26, 2019

Codecov Report

Merging #2374 into master will decrease coverage by 0.2%.
The diff coverage is 62.5%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2374      +/-   ##
==========================================
- Coverage   93.95%   93.75%   -0.21%     
==========================================
  Files          34       34              
  Lines        1291     1297       +6     
  Branches      368      370       +2     
==========================================
+ Hits         1213     1216       +3     
- Misses         77       79       +2     
- Partials        1        2       +1
Impacted Files Coverage Δ
lib/Server.js 96.71% <62.5%> (-0.63%) ⬇️

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 f4c8f94...5b86099. Read the comment docs.

lib/Server.js Outdated
@@ -390,13 +390,25 @@ class Server {

if (Array.isArray(contentBase)) {
contentBase.forEach((item) => {
this.app.use(contentBasePublicPath, serveIndex(item));
this.app.use(contentBasePublicPath, (req, res, next) => {
if (req.method !== 'GET') {
Copy link
Member

Choose a reason for hiding this comment

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

Not sure it is good solution, developers can want to handle GET in own middleware too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

AFAIK, serve-index let GET request that it can't handle fall-back to the next middleware, it's just non-GET request that are blindly replied as 405.

This is basically a revert of the change from app.get to app.use done in #2150 while still keeping the contentBasePublicPath feature.

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (req.method !== 'GET') {
if (req.method !== 'GET' && req.method !== 'HEAD') {

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

But based on previous implementation, we handle only GET, so i think we can merge this, we need fix it for server-static too https://github.com/webpack/webpack-dev-server/pull/2150/files#diff-15fb51940da53816af13330d8ce69b4eR341

/cc @iamandrewluca

@jvasseur
Copy link
Contributor Author

jvasseur commented Dec 26, 2019

@evilebottnawi the serve-static doesn't exhibit this problem because it already forward non-GET request to the next middleware by default : https://github.com/expressjs/serve-static/blob/master/index.js#L73-L76

@iamandrewluca
Copy link
Contributor

I'm taking a look into serve-index implementation

lib/Server.js Outdated Show resolved Hide resolved
@iamandrewluca
Copy link
Contributor

iamandrewluca commented Dec 26, 2019

serve-static has fallthrough, but we don't use it in our case, it must be added.
serve-index does not have fallthrough, and I agree with the workaround in this PR.

@jvasseur also make sure you check also for HEAD method

if (req.method !== 'GET' && req.method !== 'HEAD') {
// ...

Also I think serveIndex needs to be returned

return serveIndex(item)(req, res, next);

@jvasseur
Copy link
Contributor Author

@iamandrewluca true is already the default value for the fallthrough parameter of serve-static.

I've added the check for HEAD in the code.

@iamandrewluca
Copy link
Contributor

iamandrewluca commented Dec 26, 2019

recommend to rename PR into

fix(server): fallthrough non `GET` and `HEAD` request to routes defined in after option

iamandrewluca
iamandrewluca previously approved these changes Dec 26, 2019
Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Good job, thanks!

/cc @hiroppy @Loonride need review

@iamandrewluca
Copy link
Contributor

iamandrewluca commented Dec 26, 2019

@jvasseur pls add a comment in code before if, that serve-index does not fallthrough like serve-static

@jvasseur jvasseur changed the title fix(server): fix POST request forward to routes defined in after option fix(server): fallthrough non GET and HEAD request to routes defined in after option Dec 26, 2019
Fix a bug introduced in cee700d where the serveIndex feature where always replying instead of forwarding requests to the next middleware.
@jvasseur
Copy link
Contributor Author

@iamandrewluca done

@iamandrewluca
Copy link
Contributor

@jvasseur add at the end of PR description this. It will automatically close the issue when PR is merged.

Closes #2370

@jvasseur
Copy link
Contributor Author

Done

Copy link

@wizardofhogwarts wizardofhogwarts left a comment

Choose a reason for hiding this comment

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

lgtm

@hiroppy hiroppy merged commit ebe8eca into webpack:master Dec 29, 2019
@hiroppy
Copy link
Member

hiroppy commented Dec 29, 2019

Thanks!

@iamandrewluca
Copy link
Contributor

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.

The [before] and [after] option hook not allow http POST request?
9 participants