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

feat: proxy-rewrite support config add set and remove header #8336

Merged
merged 22 commits into from
Nov 22, 2022

Conversation

mscb402
Copy link
Contributor

@mscb402 mscb402 commented Nov 16, 2022

Description

Fixes #8239

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@mscb402 mscb402 marked this pull request as ready for review November 16, 2022 07:53
apisix/plugins/proxy-rewrite.lua Show resolved Hide resolved
apisix/plugins/proxy-rewrite.lua Outdated Show resolved Hide resolved
| headers | object | False | | | New Upstream headers. Headers are overwritten if they are already present otherwise, they are added to the present headers. To remove a header, set the header value to an empty string. The values in the header can contain Nginx variables like `$remote_addr` and `$client_addr`. |
| headers | object | False | | | |
| headers.add | object | false | | | Append the new headers. The format is `{"name: value",...}`. The values in the header can contain Nginx variables like $remote_addr and $balancer_ip. |
| headers.set | object | false | | | Rewriting the headers. The format is `{"name": "value", ...}`. The values in the header can contain Nginx variables like $remote_addr and $balancer_ip. |
Copy link
Member

Choose a reason for hiding this comment

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

I think use headers.rewrite is more clear.

What happens if headers.set is set, but the actual request does not have a header name that needs to be rewritten?

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 headers.set is the same as the response-rewrite plugin. When the header does not exist will add it.

docs/en/latest/plugins/proxy-rewrite.md Outdated Show resolved Hide resolved
docs/en/latest/plugins/proxy-rewrite.md Show resolved Hide resolved
t/plugin/proxy-rewrite3.t Outdated Show resolved Hide resolved
end

local function addHeader(ctx, header_name, header_value)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
local function addHeader(ctx, header_name, header_value)
local function add_header(ctx, header_name, header_value)

Copy link
Contributor Author

@mscb402 mscb402 Nov 17, 2022

Choose a reason for hiding this comment

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

done

end

local function addHeader(ctx, header_name, header_value)
local oldHeader = core.request.header(ctx, header_name)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
local oldHeader = core.request.header(ctx, header_name)
local old_header = core.request.header(ctx, header_name)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

test: sssss
test: bbb
--- response_headers
test: sssss, bbb, 123
Copy link
Member

Choose a reason for hiding this comment

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

why it is

test: sssss
test: bbb
test: 123

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 is for test header combination. First combine two existing headers test: sssss and test: bbb. And then add a new header test: 123 from proxy_rewrite. Finally, it will combine all headers in one.

Copy link
Member

Choose a reason for hiding this comment

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

Is it necessary to merge header with the same name? I'm not sure. cc @spacewander

Copy link
Member

Choose a reason for hiding this comment

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

We need to use https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/req.md#add_header. Merging multiple request headers with , doesn't obey the standard.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

apisix/plugins/proxy-rewrite.lua Outdated Show resolved Hide resolved
apisix/plugins/proxy-rewrite.lua Show resolved Hide resolved
t/plugin/proxy-rewrite3.t Show resolved Hide resolved
minItems = 1,
items = {
type = "string",
-- "Set-Cookie"
Copy link
Member

Choose a reason for hiding this comment

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

Set-Cookie is a response header. Better to use another example.

test: sssss
test: bbb
--- response_headers
test: sssss, bbb, 123
Copy link
Member

Choose a reason for hiding this comment

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

We need to use https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/req.md#add_header. Merging multiple request headers with , doesn't obey the standard.

}
}
--- request
GET /t
Copy link
Member

Choose a reason for hiding this comment

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

Please remove duplicate sections which are set in

if (!$block->request) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

GET /t
--- response_body
passed
--- no_error_log
Copy link
Member

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

apisix/core/request.lua Outdated Show resolved Hide resolved
--- response_headers
X-Forwarded-Host: test.com
test: sssss, bbb, 123
--- no_error_log
Copy link
Member

Choose a reason for hiding this comment

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

Need to deal with #8336 (comment) in other test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done




=== TEST 13: rewrite X-Forwarded-Host
Copy link
Member

Choose a reason for hiding this comment

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

This test isn't relative to this feature?

Copy link
Contributor Author

@mscb402 mscb402 Nov 21, 2022

Choose a reason for hiding this comment

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

Nope, but this change is to remove duplicate sections from your advice. So I changed some test code.

#8336 (comment)

docs/en/latest/plugins/proxy-rewrite.md Outdated Show resolved Hide resolved
error(err)
end

req_add_header(header_name,header_value)
Copy link
Member

Choose a reason for hiding this comment

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

Please ensure a space is after the ','. Let's deal with similar places in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

mscb402 and others added 2 commits November 21, 2022 17:34
Co-authored-by: 罗泽轩 <spacewanderlzx@gmail.com>
@mscb402 mscb402 requested review from spacewander and tzssangglass and removed request for tzssangglass and spacewander November 21, 2022 10:02
@@ -39,6 +44,7 @@ local req_get_uri_args = ngx.req.get_uri_args
local req_set_uri_args = ngx.req.set_uri_args



Copy link
Member

Choose a reason for hiding this comment

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

delete this line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@mscb402 mscb402 requested review from spacewander and removed request for tzssangglass November 22, 2022 05:45
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.

feat: Support adding header in proxy-rewrite plugin
4 participants