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

SetHeader should replace entries with the same key #1134

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

lihe07
Copy link

@lihe07 lihe07 commented Oct 30, 2024

The original SetHeader only appends the key-value pair to the end of headers list. The name SetHeader is therefore misleading.

Adding header to the end sometimes won't have the same effect as replacing the header. For example, if the original response has these headers:

Access-Control-Allow-Origin: https://example.com
Access-Control-Allow-Credentials: true

And after the following operations:

h.Response.SetHeader("Access-Control-Allow-Origin", "https://example.com")

The browser will complain about: The 'Access-Control-Allow-Origin' header contains multiple values 'https://example.com, https://example.com', but only one is allowed.

This PR changes the behavior of SetHeader to replace the value of the header if it exists. If the header does not exist, it will be appended to the end of the headers list.

Also, if one writes:

h.Response.SetHeader(
  "My-Header", "a",
  "My-Header", "b",
)

Only the last value will be kept.

@ysmood
Copy link
Member

ysmood commented Oct 30, 2024

Could you also add a AddHeader function?

@lihe07
Copy link
Author

lihe07 commented Oct 31, 2024

Could you also add a AddHeader function?

Sure! It's there now.

ysmood
ysmood previously approved these changes Oct 31, 2024
@lihe07
Copy link
Author

lihe07 commented Oct 31, 2024

Sorry about that. Forgot to check CI.

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.

2 participants