Skip to content

Commit

Permalink
Revert "feature: add support for sock:send_cdata()."
Browse files Browse the repository at this point in the history
This reverts commit 362bc05.
  • Loading branch information
zhuizhuhaomeng committed Mar 27, 2024
1 parent 362bc05 commit b1bc48e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 146 deletions.
2 changes: 1 addition & 1 deletion lib/resty/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ require "resty.core.uri"
require "resty.core.exit"
require "resty.core.base64"
require "resty.core.request"
require "resty.core.socket"


if subsystem == 'http' then
require "resty.core.response"
require "resty.core.phase"
require "resty.core.ndk"
require "resty.core.socket"
require "resty.core.coroutine"
require "resty.core.param"
end
Expand Down
171 changes: 26 additions & 145 deletions lib/resty/core/socket.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local base = require "resty.core.base"
base.allows_subsystem("http", "stream")
local subsystem = ngx.config.subsystem
base.allows_subsystem("http")
local debug = require "debug"
local ffi = require "ffi"

Expand Down Expand Up @@ -33,69 +32,35 @@ local option_index = {
["rcvbuf"] = 5,
}

local ngx_lua_ffi_socket_tcp_send
local ngx_lua_ffi_socket_tcp_get_send_result
local ngx_lua_ffi_socket_udp_send

if subsystem == "http" then

ffi.cdef[[
typedef struct ngx_http_lua_socket_tcp_upstream_s
ngx_http_lua_socket_tcp_upstream_t;
typedef struct ngx_http_lua_socket_udp_upstream_s
ngx_http_lua_socket_udp_upstream_t;
int ngx_http_lua_ffi_socket_tcp_getoption(ngx_http_lua_socket_tcp_upstream_t *u,

int
ngx_http_lua_ffi_socket_tcp_getoption(ngx_http_lua_socket_tcp_upstream_t *u,
int opt, int *val, unsigned char *err, size_t *errlen);
int ngx_http_lua_ffi_socket_tcp_setoption(ngx_http_lua_socket_tcp_upstream_t *u,

int
ngx_http_lua_ffi_socket_tcp_setoption(ngx_http_lua_socket_tcp_upstream_t *u,
int opt, int val, unsigned char *err, size_t *errlen);
int ngx_http_lua_ffi_socket_tcp_sslhandshake(ngx_http_request_t *r,

int
ngx_http_lua_ffi_socket_tcp_sslhandshake(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, void *sess,
int enable_session_reuse, ngx_str_t *server_name, int verify,
int ocsp_status_req, void *chain, void *pkey, char **errmsg);
int ngx_http_lua_ffi_socket_tcp_get_sslhandshake_result(ngx_http_request_t *r,

int
ngx_http_lua_ffi_socket_tcp_get_sslhandshake_result(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, void **sess, char **errmsg,
int *openssl_error_code);
void ngx_http_lua_ffi_ssl_free_session(void *sess);
int ngx_http_lua_ffi_socket_tcp_send_cdata(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, void *cdata, size_t len,
size_t *sent, char **errmsg);
int ngx_http_lua_ffi_socket_tcp_get_send_result(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, size_t *sent, char **errmsg);
int ngx_http_lua_ffi_socket_udp_send_cdata(ngx_http_request_t *r,
ngx_http_lua_socket_udp_upstream_t *u, void *cdata, size_t len,
char **errmsg);
]]

ngx_lua_ffi_socket_tcp_send = C.ngx_http_lua_ffi_socket_tcp_send_cdata
ngx_lua_ffi_socket_tcp_get_send_result
= C.ngx_http_lua_ffi_socket_tcp_get_send_result
ngx_lua_ffi_socket_udp_send = C.ngx_http_lua_ffi_socket_udp_send_cdata

elseif subsystem == "stream" then

ffi.cdef[[
typedef struct ngx_stream_lua_socket_tcp_upstream_s
ngx_stream_lua_socket_tcp_upstream_t;
typedef struct ngx_stream_lua_socket_udp_upstream_s
ngx_stream_lua_socket_udp_upstream_t;
int ngx_stream_lua_ffi_socket_tcp_send_cdata(ngx_stream_lua_request_t *r,
ngx_stream_lua_socket_tcp_upstream_t *u, void *cdata, size_t len,
size_t *sent, char **errmsg);
int ngx_stream_lua_ffi_socket_tcp_get_send_result(ngx_stream_lua_request_t *r,
ngx_stream_lua_socket_tcp_upstream_t *u, size_t *sent, char **errmsg);
int ngx_stream_lua_ffi_socket_udp_send_cdata(ngx_stream_lua_request_t *r,
ngx_stream_lua_socket_udp_upstream_t *u, void *cdata, size_t len,
char **errmsg);
void
ngx_http_lua_ffi_ssl_free_session(void *sess);
]]


ngx_lua_ffi_socket_tcp_send = C.ngx_stream_lua_ffi_socket_tcp_send_cdata
ngx_lua_ffi_socket_tcp_get_send_result
= C.ngx_stream_lua_ffi_socket_tcp_get_send_result
ngx_lua_ffi_socket_udp_send = C.ngx_stream_lua_ffi_socket_udp_send_cdata
end


local output_value_buf = ffi_new("int[1]")
local ERR_BUF_SIZE = 4096

Expand All @@ -109,12 +74,6 @@ local SOCKET_CTX_INDEX = 1
local SOCKET_CLIENT_CERT_INDEX = 6
local SOCKET_CLIENT_PKEY_INDEX = 7

local errmsg = base.get_errmsg_ptr()
local sent = ffi_new("size_t[1]")
local session_ptr = ffi_new("void *[1]")
local server_name_str = ffi_new("ngx_str_t[1]")
local openssl_error_code = ffi_new("int[1]")


local function get_tcp_socket(cosocket)
local tcp_socket = cosocket[SOCKET_CTX_INDEX]
Expand All @@ -126,18 +85,6 @@ local function get_tcp_socket(cosocket)
end


local function get_udp_socket(cosocket)
local udp_socket = cosocket[SOCKET_CTX_INDEX]
if not udp_socket then
error("udp socket is never created")
end

return udp_socket
end


if subsystem == "http" then

local function getoption(cosocket, option)
local tcp_socket = get_tcp_socket(cosocket)

Expand Down Expand Up @@ -198,6 +145,12 @@ local function setoption(cosocket, option, value)
end


local errmsg = base.get_errmsg_ptr()
local session_ptr = ffi_new("void *[1]")
local server_name_str = ffi_new("ngx_str_t[1]")
local openssl_error_code = ffi_new("int[1]")


local function setclientcert(cosocket, cert, pkey)
if not cert and not pkey then
cosocket[SOCKET_CLIENT_CERT_INDEX] = nil
Expand Down Expand Up @@ -306,85 +259,13 @@ local function sslhandshake(cosocket, reused_session, server_name, ssl_verify,
end
end

local method_table = registry.__tcp_cosocket_mt
method_table.getoption = getoption
method_table.setoption = setoption
method_table.setclientcert = setclientcert
method_table.sslhandshake = sslhandshake

end


local function tcp_send_cdata(cosocket, cdata, len)
local r = get_request()
if not r then
error("no request found", 2)
end

if type(cdata) ~= "cdata" then
error("#2 expecting cdta, but got " .. type(cdata), 2)
end

local u = get_tcp_socket(cosocket)

local rc = ngx_lua_ffi_socket_tcp_send(r, u, cdata, len,
sent, errmsg)
while true do
if rc == FFI_ERROR then
return nil, ffi_str(errmsg[0])
end

if rc == FFI_OK then
rc = ngx_lua_ffi_socket_tcp_get_send_result(r, u, sent, errmsg)

assert(rc == FFI_OK)
return tonumber(sent[0])
end

assert(rc == FFI_AGAIN)

co_yield()

rc = ngx_lua_ffi_socket_tcp_get_send_result(r, u, sent, errmsg)
end
end


local function udp_send_cdata(cosocket, cdata, len)
local r = get_request()
if not r then
error("no request found", 2)
end

if type(cdata) ~= "cdata" then
error("#2 expecting cdta, but got " .. type(cdata), 2)
end

local u = get_udp_socket(cosocket)

local rc = ngx_lua_ffi_socket_udp_send(r, u, cdata, len, errmsg)
if rc == FFI_ERROR then
return nil, ffi_str(errmsg[0])
end

return 1
end


do
local tcp_mt = registry.__tcp_cosocket_mt
tcp_mt.send_cdata = tcp_send_cdata

local raw_tcp_mt = registry.__raw_req_tcp_cosocket_mt
raw_tcp_mt.send_cdata = tcp_send_cdata

local udp_mt = registry.__udp_cosocket_mt
udp_mt .send_cdata = udp_send_cdata

if subsystem == "stream" then
local raw_udp_mt = registry.__raw_udp_cosocket_mt
raw_udp_mt.send_cdata = udp_send_cdata
end
local method_table = registry.__tcp_cosocket_mt
method_table.getoption = getoption
method_table.setoption = setoption
method_table.setclientcert = setclientcert
method_table.sslhandshake = sslhandshake
end


Expand Down

0 comments on commit b1bc48e

Please sign in to comment.