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(request-transformer) change HTTP method of upstream request #1635

Merged
merged 3 commits into from
Oct 28, 2016

Conversation

subnetmarco
Copy link
Member

  • Request-Transfomer: can change the HTTP method of the upstream request.

Issues resolved

Fix #1631.

@subnetmarco subnetmarco added this to the 0.9.2 milestone Sep 14, 2016
@subnetmarco subnetmarco force-pushed the plugins/transform-request-method branch from 60e213a to bd8f0f0 Compare September 14, 2016 19:10
@subnetmarco subnetmarco force-pushed the plugins/transform-request-method branch from bd8f0f0 to 1f813c9 Compare September 14, 2016 19:36
@thibaultcha thibaultcha changed the title feature(request-transformer) change HTTP method of upstream request feat(request-transformer) change HTTP method of upstream request Sep 19, 2016
Copy link
Member

@thibaultcha thibaultcha left a comment

Choose a reason for hiding this comment

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

Reluctant to adding a new feature to 0.9.2. Should be targeted for 0.10.

local content_type_value = req_get_headers()[CONTENT_TYPE]
local content_type = get_content_type(content_type_value)
if content_type == ENCODED then
-- Also put the body into querystring
Copy link
Member

Choose a reason for hiding this comment

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

Such behavior should be configurable.

@@ -303,7 +303,33 @@ local function transform_body(conf)
end
end

local function transform_method(conf)
if conf.http_method then
ngx.req.set_method(ngx["HTTP_"..conf.http_method:upper()])
Copy link
Member

Choose a reason for hiding this comment

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

All ngx methods are cached except this one. It should be too for consistency and efficiency.

local method = value:upper()
local ngx_method = ngx["HTTP_"..method]
if not ngx_method then
return false, method.." is not supported"
Copy link
Member

@thibaultcha thibaultcha Sep 20, 2016

Choose a reason for hiding this comment

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

By convention our codebase returns nil on error. Some parts are not yet updated, but new code should comply.

Copy link
Member Author

Choose a reason for hiding this comment

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

Think of this value as being stored into a is_valid property, that's why it's true or false.

local function check_method(value)
  if not value then return true end
  local method = value:upper()
  local ngx_method = ngx["HTTP_"..method]
  if not ngx_method then
    return false, method.." is not supported"
  end
  return true
end

local is_valid, err = check_method("hello")

Makes sense that the return value is boolean, with an optional error message into err.

Copy link
Member

@thibaultcha thibaultcha Sep 22, 2016

Choose a reason for hiding this comment

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

But that's not how this method is being used. It's passed to func, and it is a common Lua idiom when the second parameter is an error to return nil as the first one:

local ok, err = check_method("hello")
if not ok then

end

Returning false would make sense if, say, there was no error as the 2nd return value.

local function transform_method(conf)
if conf.http_method then
ngx.req.set_method(ngx["HTTP_"..conf.http_method:upper()])
if conf.http_method == "GET" then
Copy link
Member

Choose a reason for hiding this comment

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

Maybe HEAD too? Should probably be configurable as well.

@thibaultcha thibaultcha modified the milestones: 0.10, 0.9.2 Sep 20, 2016
@subnetmarco subnetmarco merged commit f6b4801 into next Oct 28, 2016
@subnetmarco subnetmarco deleted the plugins/transform-request-method branch October 28, 2016 00:06
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