-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat(http_server source): Add custom response header configuration #20811
base: master
Are you sure you want to change the base?
Conversation
Add a "custom_response_headers" option to `SimpleHttpConfig` to add to all Http responses. Closes: vectordotdev#20395
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this contribution @ChrisCanCompute !
I think it just needs a few things:
- The documentation to be regenerated. You can run
make generate-component-docs
to do so - A changelog entry added. See https://github.com/vectordotdev/vector/blob/master/changelog.d/README.md for details on how to do that
- Could we add a test? I think adding it to src/sources/http_server.rs would be appropriate
Hi, I have seen your comment. I've been having a hard time running Thanks for the quick review! |
Ah, I think this is actually failing to compile at the moment, which is why |
The build should be fixed now. |
The code should all now be good.
I haven't been able to get either of them to generate the docs. I'll sort out the commits that are missing author emails (not sure how that happened). |
Thanks @ChrisCanCompute . It compiles now, but I'm getting this error when generating the docs:
I think we need to add the field description as suggested there. |
Got the docs updated as well. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I've done a force push to my branch to fix the authors (so that the CLA check passes). Let me know if there's anything else you need from me on this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ChrisCanCompute ! I made one suggestion for the changelog, but otherwise this looks good!
changelog.d/20395_custom_response_headers_from_http_server.feature.md
Outdated
Show resolved
Hide resolved
…ture.md Co-authored-by: Jesse Szwedko <[email protected]>
@ChrisCanCompute Apologies, can you merge in |
HTTP allows set multiple headers with the same name, e.g. multiple |
Good point. I'm curious what you think of changing this to take a map of header name to list of header values @ChrisCanCompute ? Ideally it could handle either one value or a list of values since single value for each header is more common but that could be more difficult so I'd be happy to see it take a list of values. |
Merge branch 'vectordotdev:master' into master
I've updated this to take multiple strings. Let me know what you think, and thank you for the feedback so far! |
I think it would be better to use https://docs.rs/http/latest/http/header/struct.HeaderMap.html#method.try_append and get the Insert the first value, and then append the rest |
@jorgehermo9 I've updated the PR to use |
I think it would be a possible panic that the user specifies a lot of headers (for any reason) in the config and then the Maybe should we create an issue stating this possible enhancement so it is tracked? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the additional changes @ChrisCanCompute ! I think this is looking better. I left a couple of comments inline.
v0.2
of the http
crate has try_append
too: https://docs.rs/http/0.2.12/http/header/struct.HeaderMap.html#method.try_append
Just for reference, regarding the upgrade to v1.0.0 of the http
crate, we are tracking that upgrade here: #19179. We are currently blocked by some dependencies. Again, v0.2 seems to have try_append
though.
src/sources/http_server.rs
Outdated
)); | ||
let header_name: HeaderName = key.parse().unwrap(); | ||
if let Some((first, rest)) = values.split_first() { | ||
header_map.insert(header_name.clone(), first.parse().unwrap()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can just use append
rather than needing to split and insert
the first one:
Inserts a key-value pair into the map.
If the map did not previously have this key present, then false is returned.
If the map did have this key present, the new value is pushed to the end of the list of values currently associated with the key. The key is not updated, though; this matters for types that can be == without being identical.
https://docs.rs/http/0.2.12/http/header/struct.HeaderMap.html#method.append
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the docs you point, I understand that if you append without inserting first, it would fail
Thats why I suggested to insert the first and append the rest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it fails. It just returns false
if the key didn't already exist and true
if it did but either way the value is inserted.
See the implementation: https://docs.rs/http/0.2.12/src/http/header/map.rs.html#1415-1449
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, you're right, didn't see the implementation. I think I understood wrong the documentation. I see that Inserts a key-value pair into the map
is not fallible and will be done always. Thanks!!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For sure :) I can see why you would have been confused.
src/sources/http_server.rs
Outdated
key, | ||
values.join(", "), | ||
)); | ||
let header_name: HeaderName = key.parse().unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we do this parsing and validation earlier? That is: parse the configured headers into a HeaderMap
during source start time? I think that would have the advantage of:
- Only parsing the headers once
- Being able to return an error at start time if a name is invalid, rather than panic'ing at runtime
@jszwedko thanks for the feedback. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @kates ! I left one more comment, but otherwise this is looking good to me.
for value in values { | ||
header_map | ||
.try_append(key.clone(), value.clone()) | ||
.expect("Failed to append header. Too many custom http headers specified in custom_response_headers config."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is any better than it was before. Open to feedback on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks @ChrisCanCompute !
Add a
custom_response_headers
option toSimpleHttpConfig
to add the given headers to all Http responses.Closes: #20395