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

Do not overwrite Content-Type #377

Merged

Conversation

dmohns
Copy link
Contributor

@dmohns dmohns commented Mar 6, 2019

What kind of change does this PR introduce?

When the Content-Type header is already present (for example it has been
set by other middleware) we do not overwrite it.

Did you add tests for your changes?

Yes, added unit test to the suite.

Summary

Closes #376

Does this PR introduce a breaking change?

No

Other information

When the Content-Type header is already present (for example it has been 
set by other middleware) we do not overwrite it.

Closes webpack#376
@jsf-clabot
Copy link

jsf-clabot commented Mar 6, 2019

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Mar 6, 2019

Codecov Report

Merging #377 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #377      +/-   ##
==========================================
+ Coverage   96.95%   96.96%   +0.01%     
==========================================
  Files           7        7              
  Lines         263      264       +1     
==========================================
+ Hits          255      256       +1     
  Misses          8        8
Impacted Files Coverage Δ
lib/middleware.js 94.54% <100%> (+0.1%) ⬆️

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 e5bd8f8...b7561de. Read the comment docs.

1 similar comment
@codecov
Copy link

codecov bot commented Mar 6, 2019

Codecov Report

Merging #377 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #377      +/-   ##
==========================================
+ Coverage   96.95%   96.96%   +0.01%     
==========================================
  Files           7        7              
  Lines         263      264       +1     
==========================================
+ Hits          255      256       +1     
  Misses          8        8
Impacted Files Coverage Δ
lib/middleware.js 94.54% <100%> (+0.1%) ⬆️

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 e5bd8f8...b7561de. Read the comment docs.

res.setHeader('Content-Type', contentType);
if (!res.getHeader('Content-Type')) {
res.setHeader('Content-Type', contentType);
}
res.setHeader('Content-Length', content.length);
Copy link
Member

Choose a reason for hiding this comment

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

What do you think should we add check here too? I think in theory it is also possible to set own Content-Length, don't known real use case, but still think what we should add check here 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.

This was my initial thought too. But I think matters are more complicated... It looks like ExpressJS itself sets the Content-Length

https://github.com/expressjs/express/blob/3d10279826f59bf68e28995ce423f7bc4d2f11cf/lib/response.js#L194

In fact line 85 seems to be not functional at all.

Copy link
Member

Choose a reason for hiding this comment

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

@dmohns good catch, can you create issue? Will be great if you help to use investigate this, let's merge this as is

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 Will do.

Copy link

Choose a reason for hiding this comment

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

hello,this commit make error,"TypeError: res.getHeader is not a function",my node version is v10.15.2,how can i solve this problem

Copy link

Choose a reason for hiding this comment

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

👍 Will do.

hello,this commit make error,"TypeError: res.getHeader is not a function",my node version is v10.15.2,how can i solve this problem

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.

Looks good, one note

@alexander-akait alexander-akait merged commit b2a6fed into webpack:master Mar 6, 2019
@dmohns
Copy link
Contributor Author

dmohns commented Mar 6, 2019

Unfortunately, I just realized the test in this PR is "wrong". The headers get set after Content-Type already got overwritten. Hence we are not really testing the scope of this PR. I'll shoot a separate PR with a fix.

@alexander-akait
Copy link
Member

@dmohns 👍

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.

4 participants