Skip to content

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

Merged
spacewander merged 22 commits into
apache:masterfrom
mscb402:feat_8239
Nov 22, 2022
Merged

feat: proxy-rewrite support config add set and remove header#8336
spacewander merged 22 commits into
apache:masterfrom
mscb402:feat_8239

Conversation

@mscb402

@mscb402 mscb402 commented Nov 16, 2022

Copy link
Copy Markdown
Contributor

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
Comment thread apisix/plugins/proxy-rewrite.lua
Comment thread apisix/plugins/proxy-rewrite.lua Outdated
Comment thread docs/en/latest/plugins/proxy-rewrite.md Outdated
| 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
Copy Markdown
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
Copy Markdown
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.

Comment thread docs/en/latest/plugins/proxy-rewrite.md Outdated
Comment thread docs/en/latest/plugins/proxy-rewrite.md
Comment thread t/plugin/proxy-rewrite3.t Outdated
Comment thread apisix/plugins/proxy-rewrite.lua Outdated
}
end

local function addHeader(ctx, header_name, header_value)

Copy link
Copy Markdown
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)

@mscb402 mscb402 Nov 17, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Comment thread apisix/plugins/proxy-rewrite.lua Outdated
end

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

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Comment thread t/plugin/proxy-rewrite3.t
test: sssss
test: bbb
--- response_headers
test: sssss, bbb, 123

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment thread apisix/plugins/proxy-rewrite.lua Outdated
Comment thread apisix/plugins/proxy-rewrite.lua
Comment thread t/plugin/proxy-rewrite3.t
Comment thread apisix/plugins/proxy-rewrite.lua Outdated
minItems = 1,
items = {
type = "string",
-- "Set-Cookie"

Copy link
Copy Markdown
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.

Comment thread t/plugin/proxy-rewrite3.t
test: sssss
test: bbb
--- response_headers
test: sssss, bbb, 123

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Comment thread t/plugin/proxy-rewrite3.t Outdated
}
}
--- request
GET /t

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Comment thread t/plugin/proxy-rewrite3.t Outdated
GET /t
--- response_body
passed
--- no_error_log

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ditto

Copy link
Copy Markdown
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 a review from tzssangglass November 21, 2022 06:49
Comment thread apisix/core/request.lua Outdated
Comment thread t/plugin/proxy-rewrite3.t Outdated
--- response_headers
X-Forwarded-Host: test.com
test: sssss, bbb, 123
--- no_error_log

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Comment thread t/plugin/proxy-rewrite3.t



=== TEST 13: rewrite X-Forwarded-Host

Copy link
Copy Markdown
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?

@mscb402 mscb402 Nov 21, 2022

Copy link
Copy Markdown
Contributor Author

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)

Comment thread docs/en/latest/plugins/proxy-rewrite.md Outdated
Comment thread apisix/core/request.lua Outdated
error(err)
end

req_add_header(header_name,header_value)

Copy link
Copy Markdown
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
Copy Markdown
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 tzssangglass and removed request for spacewander and tzssangglass November 21, 2022 10:02
Comment thread apisix/core/request.lua Outdated
local req_set_uri_args = ngx.req.set_uri_args



Copy link
Copy Markdown
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
Copy Markdown
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 a review from tzssangglass November 22, 2022 01:06
@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