Fix: Preserve HTTP headers in ParallelChannel sub-calls#3061
Fix: Preserve HTTP headers in ParallelChannel sub-calls#3061XiaoYingGee wants to merge 2 commits intoapache:masterfrom
Conversation
Add judgement to avoid create new object when cntl dont have http_request
Fix: Preserve HTTP headers in ParallelChannel sub-callsThere was a problem hiding this comment.
Pull Request Overview
This PR fixes an issue where HTTP headers set on parent controllers are lost when making sub-calls through ParallelChannel. The fix ensures that application-set headers like Authorization are properly propagated to each sub-controller.
- Adds header propagation logic to preserve parent controller headers in sub-calls
- Maintains backward compatibility by only copying headers when they exist
- Improves functionality for applications that rely on HTTP headers for authentication/authorization
| if (parent_hdr.HeaderBegin() != parent_hdr.HeaderEnd()) { | ||
| auto& sub_hdr = d->sub_done(i)->cntl.http_request(); | ||
| for (auto it = parent_hdr.HeaderBegin(); it != parent_hdr.HeaderEnd(); ++it) { | ||
| sub_hdr.AppendHeader(it->first, it->second); | ||
| } |
There was a problem hiding this comment.
[nitpick] The check for empty headers is redundant since the for loop will naturally handle empty header collections. This condition can be removed to simplify the code.
| if (parent_hdr.HeaderBegin() != parent_hdr.HeaderEnd()) { | |
| auto& sub_hdr = d->sub_done(i)->cntl.http_request(); | |
| for (auto it = parent_hdr.HeaderBegin(); it != parent_hdr.HeaderEnd(); ++it) { | |
| sub_hdr.AppendHeader(it->first, it->second); | |
| } | |
| auto& sub_hdr = d->sub_done(i)->cntl.http_request(); | |
| for (auto it = parent_hdr.HeaderBegin(); it != parent_hdr.HeaderEnd(); ++it) { | |
| sub_hdr.AppendHeader(it->first, it->second); |
| if (parent_hdr.HeaderBegin() != parent_hdr.HeaderEnd()) { | ||
| auto& sub_hdr = d->sub_done(i)->cntl.http_request(); | ||
| for (auto it = parent_hdr.HeaderBegin(); it != parent_hdr.HeaderEnd(); ++it) { | ||
| sub_hdr.AppendHeader(it->first, it->second); |
There was a problem hiding this comment.
Using AppendHeader may result in duplicate headers if the sub-controller already has headers with the same name. Consider using SetHeader instead to avoid potential header duplication, or check if the header already exists before appending.
| sub_hdr.AppendHeader(it->first, it->second); | |
| sub_hdr.SetHeader(it->first, it->second); |
| d->sub_done(i)->cntl.allow_done_to_run_in_place(); | ||
|
|
||
| // Propagate all HTTP headers from parent controller to sub-controllers. | ||
| // This preserves application-set headers (e.g., Authorization) on each sub-call. |
There was a problem hiding this comment.
Does it need to check if sub-controller headers has exists key before AppendHeader or SetHeader? Avoid overwrite existing header.
Problem
When using ParallelChannel, HTTP headers set on the parent controller (e.g., Authorization headers) are lost during sub-calls because they are not propagated to the sub-controllers.
Solution