Re: [PR] feat: support lua proxy upstream [apisix]

2025-08-03 Thread via GitHub


github-actions[bot] closed pull request #12086: feat: support lua proxy upstream
URL: https://github.com/apache/apisix/pull/12086


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-08-03 Thread via GitHub


github-actions[bot] commented on PR #12086:
URL: https://github.com/apache/apisix/pull/12086#issuecomment-3148313777

   This pull request/issue has been closed due to lack of activity. If you 
think that is incorrect, or the pull request requires review, you can revive 
the PR at any time.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-07-06 Thread via GitHub


github-actions[bot] commented on PR #12086:
URL: https://github.com/apache/apisix/pull/12086#issuecomment-3041274356

   This pull request has been marked as stale due to 60 days of inactivity. It 
will be closed in 4 weeks if no further activity occurs. If you think that's 
incorrect or this pull request should instead be reviewed, please simply write 
any comment. Even if closed, you can still revive the PR at any time or discuss 
it on the [email protected] list. Thank you for your contributions.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-05-06 Thread via GitHub


Baoyuantop commented on PR #12086:
URL: https://github.com/apache/apisix/pull/12086#issuecomment-2857174090

   Hi @LiteSun, is this PR still being processed? Can it be marked as draft 
first?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-04-05 Thread via GitHub


nic-6443 commented on code in PR #12086:
URL: https://github.com/apache/apisix/pull/12086#discussion_r2022047346


##
apisix/init.lua:
##
@@ -550,6 +550,12 @@ function _M.handle_upstream(api_ctx, route, 
enable_websocket)
 
 set_upstream_headers(api_ctx, server)
 
+-- lua proxy the request to upstream
+if api_ctx.lua_proxy_upstream then

Review Comment:
   I thought about the naming of the existing variables `bypass_nginx_upstream` 
and `lua_proxy_upstream`, and felt that their meanings are unclear. I think we 
can design them as two: 
   * `bypass_nginx_upstream` to indicate bypassing nginx's upstream module
   * `bypass_balancer` to indicate bypassing the existing balancer logic
   cc @membphis 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-04-05 Thread via GitHub


AlinsRan commented on code in PR #12086:
URL: https://github.com/apache/apisix/pull/12086#discussion_r2022219243


##
apisix/lua_proxy.lua:
##
@@ -0,0 +1,165 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local _M = {}
+local core = require("apisix.core")
+local http = require("resty.http")
+
+local HTTP_GATEWAY_TIMEOUT = ngx.HTTP_GATEWAY_TIMEOUT
+local HTTP_INTERNAL_SERVER_ERROR = ngx.HTTP_INTERNAL_SERVER_ERROR
+
+
+local function handle_error(err)
+if core.string.find(err, "timeout") then
+return HTTP_GATEWAY_TIMEOUT
+end
+return HTTP_INTERNAL_SERVER_ERROR
+end
+
+
+local function build_request_opts(conf, ctx)
+-- Get upstream server
+local server = ctx.picked_server
+if not server then
+return nil, "no picked server"
+end
+
+local headers = core.request.headers(ctx)
+-- When content-length is cleared, the HTTP server will automatically 
calculate and
+-- set the correct content length when sending the response. This ensures 
that the
+-- content length of the response matches the actual data sent, thereby 
avoiding mismatches.
+headers["content-length"] = nil
+
+-- Build request options
+local opts = {
+scheme = server.scheme or ctx.upstream_scheme or "http",
+host = server.domain or server.host,
+port = server.port,
+path = ctx.var.uri,
+query = ctx.var.args,
+method = core.request.get_method(),
+headers = headers,
+ssl_verify = conf.ssl_verify,
+keepalive = conf.keepalive,
+keepalive_timeout = conf.timeout,
+keepalive_pool = conf.keepalive_pool
+}
+
+-- Set upstream URI
+if ctx.var.upstream_uri ~= "" then
+opts.path = ctx.var.upstream_uri
+end
+
+-- Get request body
+local body, err = core.request.get_body()
+if err then
+core.log.error("failed to get request body: ", err)
+end
+if body then
+opts.body = body
+end
+
+return opts
+end
+
+
+local function read_response(ctx, res)
+local body_reader = res.body_reader
+if not body_reader then
+core.log.error("failed to get response body reader")
+return HTTP_INTERNAL_SERVER_ERROR
+end
+
+local content_type = res.headers["Content-Type"]
+core.response.set_header("Content-Type", content_type)
+
+-- TODO: support event stream
+if content_type and core.string.find(content_type, "text/event-stream") 
then
+core.log.error("event stream is not supported")
+   return HTTP_INTERNAL_SERVER_ERROR
+end
+
+local raw_res_body, err = res:read_body()
+if err then
+core.log.error("failed to read response body: ", err)
+return handle_error(err)
+end
+
+return res.status, raw_res_body
+end
+
+
+function _M.request(conf, ctx)
+-- Build request options
+local opts, err = build_request_opts(conf, ctx)
+if err then
+core.log.error("failed to build request options: ", err)
+return HTTP_INTERNAL_SERVER_ERROR
+end
+
+-- Create HTTP client
+local httpc, err = http.new()
+if err then
+return nil, "failed to create http client: " .. err
+end
+httpc:set_timeout(opts.timeout)
+
+-- Connect to upstream
+local ok, err = httpc:connect({
+scheme = opts.scheme,
+host = opts.host,
+port = opts.port,
+ssl_verify = opts.ssl_verify,
+ssl_server_name = opts.host,
+pool_size = opts.keepalive,
+})
+
+if not ok then
+return nil, "failed to connect to upstream: " .. err
+end
+
+-- Prepare request parameters
+local params = {
+method = opts.method,
+headers = opts.headers,
+keepalive = opts.keepalive,
+ssl_verify = opts.ssl_verify,
+path = opts.path,
+query = opts.query,
+body = opts.body
+}
+
+-- Send request
+local res, err = httpc:request(params)
+if err then
+return nil, err
+end
+
+-- Handle response
+local code, body = read_response(ctx, res)
+
+-- Set keepalive for connection reuse
+if opts.keepalive then

Review Comment:
   Do I not need to close the connection wh

Re: [PR] feat: support lua proxy upstream [apisix]

2025-04-05 Thread via GitHub


AlinsRan commented on code in PR #12086:
URL: https://github.com/apache/apisix/pull/12086#discussion_r2021976657


##
apisix/plugins/example-plugin.lua:
##
@@ -85,6 +90,10 @@ function _M.access(conf, ctx)
 core.log.warn("plugin access phase, conf: ", core.json.encode(conf))
 -- return 200, {message = "hit example plugin"}
 
+if conf.lua_proxy_upstream then

Review Comment:
   This is a feature PR that has added plugin configuration and requires 
updating schema and documentation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-04-04 Thread via GitHub


shreemaan-abhishek commented on code in PR #12086:
URL: https://github.com/apache/apisix/pull/12086#discussion_r2029719062


##
apisix/init.lua:
##
@@ -550,6 +550,12 @@ function _M.handle_upstream(api_ctx, route, 
enable_websocket)
 
 set_upstream_headers(api_ctx, server)
 
+-- lua proxy the request to upstream
+if api_ctx.lua_proxy_upstream then

Review Comment:
   I agree with this



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-04-04 Thread via GitHub


AlinsRan commented on code in PR #12086:
URL: https://github.com/apache/apisix/pull/12086#discussion_r2022185525


##
apisix/lua_proxy.lua:
##
@@ -0,0 +1,165 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local _M = {}
+local core = require("apisix.core")
+local http = require("resty.http")
+
+local HTTP_GATEWAY_TIMEOUT = ngx.HTTP_GATEWAY_TIMEOUT
+local HTTP_INTERNAL_SERVER_ERROR = ngx.HTTP_INTERNAL_SERVER_ERROR
+
+
+local function handle_error(err)
+if core.string.find(err, "timeout") then
+return HTTP_GATEWAY_TIMEOUT
+end
+return HTTP_INTERNAL_SERVER_ERROR
+end
+
+
+local function build_request_opts(conf, ctx)
+-- Get upstream server
+local server = ctx.picked_server
+if not server then
+return nil, "no picked server"
+end
+
+local headers = core.request.headers(ctx)
+-- When content-length is cleared, the HTTP server will automatically 
calculate and
+-- set the correct content length when sending the response. This ensures 
that the
+-- content length of the response matches the actual data sent, thereby 
avoiding mismatches.
+headers["content-length"] = nil
+
+-- Build request options
+local opts = {
+scheme = server.scheme or ctx.upstream_scheme or "http",
+host = server.domain or server.host,
+port = server.port,
+path = ctx.var.uri,
+query = ctx.var.args,
+method = core.request.get_method(),
+headers = headers,
+ssl_verify = conf.ssl_verify,
+keepalive = conf.keepalive,
+keepalive_timeout = conf.timeout,
+keepalive_pool = conf.keepalive_pool
+}
+
+-- Set upstream URI
+if ctx.var.upstream_uri ~= "" then
+opts.path = ctx.var.upstream_uri
+end
+
+-- Get request body
+local body, err = core.request.get_body()
+if err then
+core.log.error("failed to get request body: ", err)

Review Comment:
   Why isn't there a return for this error?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-04-04 Thread via GitHub


AlinsRan commented on code in PR #12086:
URL: https://github.com/apache/apisix/pull/12086#discussion_r2022184552


##
apisix/lua_proxy.lua:
##
@@ -0,0 +1,165 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local _M = {}
+local core = require("apisix.core")
+local http = require("resty.http")
+
+local HTTP_GATEWAY_TIMEOUT = ngx.HTTP_GATEWAY_TIMEOUT
+local HTTP_INTERNAL_SERVER_ERROR = ngx.HTTP_INTERNAL_SERVER_ERROR
+
+
+local function handle_error(err)
+if core.string.find(err, "timeout") then
+return HTTP_GATEWAY_TIMEOUT
+end
+return HTTP_INTERNAL_SERVER_ERROR
+end
+
+
+local function build_request_opts(conf, ctx)
+-- Get upstream server
+local server = ctx.picked_server
+if not server then
+return nil, "no picked server"
+end
+
+local headers = core.request.headers(ctx)
+-- When content-length is cleared, the HTTP server will automatically 
calculate and
+-- set the correct content length when sending the response. This ensures 
that the
+-- content length of the response matches the actual data sent, thereby 
avoiding mismatches.
+headers["content-length"] = nil
+
+-- Build request options
+local opts = {
+scheme = server.scheme or ctx.upstream_scheme or "http",
+host = server.domain or server.host,
+port = server.port,
+path = ctx.var.uri,
+query = ctx.var.args,
+method = core.request.get_method(),
+headers = headers,
+ssl_verify = conf.ssl_verify,
+keepalive = conf.keepalive,
+keepalive_timeout = conf.timeout,

Review Comment:
   This `timeout` is unclear.
   The meanings of connection timeout and idle connection timeout are different.
   I suggest using `keepalive_timeout`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-04-04 Thread via GitHub


AlinsRan commented on code in PR #12086:
URL: https://github.com/apache/apisix/pull/12086#discussion_r2021993576


##
apisix/init.lua:
##
@@ -550,6 +550,12 @@ function _M.handle_upstream(api_ctx, route, 
enable_websocket)
 
 set_upstream_headers(api_ctx, server)
 
+-- lua proxy the request to upstream
+if api_ctx.lua_proxy_upstream then
+common_phase("before_proxy")
+return
+end
+
 -- run the before_proxy method in access phase first to avoid always 
reinit request
 common_phase("before_proxy")

Review Comment:
   ```lua
   common_phase("before_proxy")
   if api_ctx.lua_proxy_upstream then
return
   end
   ```
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-03-31 Thread via GitHub


AlinsRan commented on code in PR #12086:
URL: https://github.com/apache/apisix/pull/12086#discussion_r2022196515


##
apisix/plugins/example-plugin.lua:
##
@@ -107,6 +116,49 @@ function _M.access(conf, ctx)
 return
 end
 
+
+function _M.before_proxy(conf, ctx)
+core.log.warn("plugin before_proxy phase, conf: ", core.json.encode(conf))
+
+if ctx.lua_proxy_upstream then
+local status, body = lua_proxy_request(conf, ctx)
+if status ~= 200 then
+return status, body
+end
+core.log.warn("lua proxy upstream response: ", core.json.encode(body))
+plugin_lua_body_filter(conf, ctx, body)
+end
+end
+
+
+function _M.lua_body_filter(conf, ctx, body)
+core.log.warn("plugin lua_body_filter phase, conf: ", 
core.json.encode(conf))
+core.log.warn("plugin lua_body_filter phase, body: ", 
core.json.encode(body))

Review Comment:
   ditto



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-03-31 Thread via GitHub


AlinsRan commented on code in PR #12086:
URL: https://github.com/apache/apisix/pull/12086#discussion_r2022196214


##
apisix/plugins/example-plugin.lua:
##
@@ -107,6 +116,49 @@ function _M.access(conf, ctx)
 return
 end
 
+
+function _M.before_proxy(conf, ctx)
+core.log.warn("plugin before_proxy phase, conf: ", core.json.encode(conf))

Review Comment:
   This is not a warm level log, please use core.json.delay_encode



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-03-31 Thread via GitHub


nic-6443 commented on code in PR #12086:
URL: https://github.com/apache/apisix/pull/12086#discussion_r2022068084


##
t/plugin/example.t:
##
@@ -339,3 +339,56 @@ plugin body_filter phase, eof: false
 plugin delayed_body_filter phase, eof: false
 plugin body_filter phase, eof: true
 plugin delayed_body_filter phase, eof: true
+
+
+
+=== TEST 14: lua body filter

Review Comment:
   We need to add more test cases that combine example-plugin with other 
plugins like proxy-rewrite, which modify downstream requests. This will help 
verify that the new Lua HTTP client way works with existing plugins in the 
rewrite/access phase.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-03-31 Thread via GitHub


nic-6443 commented on code in PR #12086:
URL: https://github.com/apache/apisix/pull/12086#discussion_r2022051743


##
apisix/lua_proxy.lua:
##
@@ -0,0 +1,165 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local _M = {}
+local core = require("apisix.core")
+local http = require("resty.http")
+
+local HTTP_GATEWAY_TIMEOUT = ngx.HTTP_GATEWAY_TIMEOUT
+local HTTP_INTERNAL_SERVER_ERROR = ngx.HTTP_INTERNAL_SERVER_ERROR
+
+
+local function handle_error(err)
+if core.string.find(err, "timeout") then
+return HTTP_GATEWAY_TIMEOUT
+end
+return HTTP_INTERNAL_SERVER_ERROR
+end
+
+
+local function build_request_opts(conf, ctx)
+-- Get upstream server
+local server = ctx.picked_server
+if not server then
+return nil, "no picked server"
+end
+
+local headers = core.request.headers(ctx)
+-- When content-length is cleared, the HTTP server will automatically 
calculate and

Review Comment:
   ```suggestion
   -- When content-length is cleared, the HTTP client will automatically 
calculate and
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-03-31 Thread via GitHub


nic-6443 commented on code in PR #12086:
URL: https://github.com/apache/apisix/pull/12086#discussion_r2022064466


##
apisix/plugins/example-plugin.lua:
##
@@ -107,6 +116,49 @@ function _M.access(conf, ctx)
 return
 end
 
+
+function _M.before_proxy(conf, ctx)
+core.log.warn("plugin before_proxy phase, conf: ", core.json.encode(conf))
+
+if ctx.lua_proxy_upstream then
+local status, body = lua_proxy_request(conf, ctx)
+if status ~= 200 then
+return status, body
+end
+core.log.warn("lua proxy upstream response: ", core.json.encode(body))
+plugin_lua_body_filter(conf, ctx, body)
+end
+end
+
+
+function _M.lua_body_filter(conf, ctx, body)
+core.log.warn("plugin lua_body_filter phase, conf: ", 
core.json.encode(conf))
+core.log.warn("plugin lua_body_filter phase, body: ", 
core.json.encode(body))

Review Comment:
   you can assert those log in test cases to confirm the body come from 
upstream is passed to `lua_body_filter` as expect.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-03-31 Thread via GitHub


nic-6443 commented on code in PR #12086:
URL: https://github.com/apache/apisix/pull/12086#discussion_r2022063258


##
t/plugin/example.t:
##
@@ -339,3 +339,56 @@ plugin body_filter phase, eof: false
 plugin delayed_body_filter phase, eof: false
 plugin body_filter phase, eof: true
 plugin delayed_body_filter phase, eof: true
+
+
+
+=== TEST 14: lua body filter
+--- config
+location /t {
+content_by_lua_block {
+local t = require("lib.test_admin").test
+local code, body = t('/apisix/admin/routes/1',
+ngx.HTTP_PUT,
+[[{
+"uri": "/anything",
+"plugins": {
+"example-plugin": {
+"i": 0,
+"lua_proxy_upstream": true,
+"request_uri": "http://httpbin.org/get";,

Review Comment:
   rename this `request_uri`  to indicate it's used to change the rseponse 
body, it make me confused with `upstream.nodes`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-03-31 Thread via GitHub


nic-6443 commented on code in PR #12086:
URL: https://github.com/apache/apisix/pull/12086#discussion_r2022055910


##
apisix/lua_proxy.lua:
##
@@ -0,0 +1,165 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local _M = {}
+local core = require("apisix.core")
+local http = require("resty.http")
+
+local HTTP_GATEWAY_TIMEOUT = ngx.HTTP_GATEWAY_TIMEOUT
+local HTTP_INTERNAL_SERVER_ERROR = ngx.HTTP_INTERNAL_SERVER_ERROR
+
+
+local function handle_error(err)
+if core.string.find(err, "timeout") then
+return HTTP_GATEWAY_TIMEOUT
+end
+return HTTP_INTERNAL_SERVER_ERROR
+end
+
+
+local function build_request_opts(conf, ctx)
+-- Get upstream server
+local server = ctx.picked_server
+if not server then
+return nil, "no picked server"
+end
+
+local headers = core.request.headers(ctx)
+-- When content-length is cleared, the HTTP server will automatically 
calculate and
+-- set the correct content length when sending the response. This ensures 
that the
+-- content length of the response matches the actual data sent, thereby 
avoiding mismatches.
+headers["content-length"] = nil
+
+-- Build request options
+local opts = {
+scheme = server.scheme or ctx.upstream_scheme or "http",
+host = server.domain or server.host,
+port = server.port,
+path = ctx.var.uri,
+query = ctx.var.args,
+method = core.request.get_method(),
+headers = headers,
+ssl_verify = conf.ssl_verify,
+keepalive = conf.keepalive,
+keepalive_timeout = conf.timeout,
+keepalive_pool = conf.keepalive_pool
+}
+
+-- Set upstream URI
+if ctx.var.upstream_uri ~= "" then
+opts.path = ctx.var.upstream_uri
+end
+
+-- Get request body
+local body, err = core.request.get_body()
+if err then
+core.log.error("failed to get request body: ", err)
+end
+if body then
+opts.body = body
+end
+
+return opts
+end
+
+
+local function read_response(ctx, res)
+local body_reader = res.body_reader
+if not body_reader then
+core.log.error("failed to get response body reader")
+return HTTP_INTERNAL_SERVER_ERROR
+end
+
+local content_type = res.headers["Content-Type"]
+core.response.set_header("Content-Type", content_type)
+
+-- TODO: support event stream
+if content_type and core.string.find(content_type, "text/event-stream") 
then
+core.log.error("event stream is not supported")
+   return HTTP_INTERNAL_SERVER_ERROR
+end

Review Comment:
   This already has example code in ai-proxy.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-03-31 Thread via GitHub


nic-6443 commented on code in PR #12086:
URL: https://github.com/apache/apisix/pull/12086#discussion_r2022047346


##
apisix/init.lua:
##
@@ -550,6 +550,12 @@ function _M.handle_upstream(api_ctx, route, 
enable_websocket)
 
 set_upstream_headers(api_ctx, server)
 
+-- lua proxy the request to upstream
+if api_ctx.lua_proxy_upstream then

Review Comment:
   I thought about the naming of the existing variables `bypass_nginx_upstream` 
and `lua_proxy_upstream`, and felt that their meanings are unclear. I think we 
can design them as two: 
   * `bypass_nginx_upstream` to indicate bypassing nginx's upstream module
   * `bypass_balancer` to indicate bypassing the existing balancer logic
   cc @membphis What do you think?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat: support lua proxy upstream [apisix]

2025-03-31 Thread via GitHub


AlinsRan commented on code in PR #12086:
URL: https://github.com/apache/apisix/pull/12086#discussion_r2021976657


##
apisix/plugins/example-plugin.lua:
##
@@ -85,6 +90,10 @@ function _M.access(conf, ctx)
 core.log.warn("plugin access phase, conf: ", core.json.encode(conf))
 -- return 200, {message = "hit example plugin"}
 
+if conf.lua_proxy_upstream then

Review Comment:
   This is a feature PR that has added plugin configuration and requires 
updating schema.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]