Skip to content
Snippets Groups Projects
Commit dc92c531 authored by Roman Khabibov's avatar Roman Khabibov Committed by Kirill Yukhin
Browse files

httpc: allow only strings for opts.headers values

There are two reasons to disable support of tables with __tostring
metamethods as opts.headers values:

* It seems this feature is not much needed: a type convertion can be
  easily done on a user's end with explicit tostring() call if needed.
* It never did work since introduction in 2.1.1-311-g85e1d78bc ('httpc:
  add checking of headers in httpc:request'): __tostring presence was
  verified, but a function in the field was not invoked.

Now http client accepts only a Lua string as a header key or a value.

Closes #3679 (again).
parent 35a48688
No related branches found
No related tags found
No related merge requests found
......@@ -184,18 +184,9 @@ luaT_httpc_request(lua_State *L)
while (lua_next(L, -2) != 0) {
int header_type = lua_type(L, -1);
if (header_type != LUA_TSTRING) {
const char *err_msg =
"opts.headers values should be strings "
"or tables with \"__tostring\"";
if (header_type != LUA_TTABLE) {
httpc_request_delete(req);
return luaL_error(L, err_msg);
} else if (!luaL_getmetafield(L, -1,
"__tostring")) {
httpc_request_delete(req);
return luaL_error(L, err_msg);
}
lua_pop(L, 1);
httpc_request_delete(req);
return luaL_error(L, "opts.headers values "
"should be strings");
}
if (lua_type(L, -2) != LUA_TSTRING) {
httpc_request_delete(req);
......
......@@ -273,15 +273,13 @@ local function test_errors(test)
local r = http:get("http://do_not_exist_8ffad33e0cb01e6a01a03d00089e71e5b2b7e9930dfcba.ru")
end
-- gh-3679 Check that opts.headers values can be strings or table
-- convertible to string only.
-- gh-3679 Check that opts.headers values can be strings only.
-- gh-4281 Check that opts.headers can be a table and opts.headers
-- keys can be strings only.
local function test_request_headers(test, url, opts)
local exp_err_bad_opts_headers = 'opts.headers should be a table'
local exp_err_bad_key = 'opts.headers keys should be strings'
local exp_err_bad_value = 'opts.headers values should be strings or ' ..
'tables with "__tostring"'
local exp_err_bad_value = 'opts.headers values should be strings'
local cases = {
-- Verify opts.headers type checks.
{
......@@ -326,18 +324,6 @@ local function test_request_headers(test, url, opts)
opts = {headers = {aaa = 'aaa'}},
exp_err = nil,
},
{
'header value with __tostring() metamethod',
opts = {headers = {aaa = setmetatable({}, {
__tostring = function(self)
return 'aaa'
end})}},
exp_err = nil,
postrequest_check = function(opts)
assert(type(opts.headers.aaa) == 'table',
'"aaa" header was modified in http_client')
end,
},
{
'boolean header value',
opts = {headers = {aaa = true}},
......@@ -359,15 +345,10 @@ local function test_request_headers(test, url, opts)
exp_err = exp_err_bad_value,
},
{
'table header value w/o metatable',
'table header value',
opts = {headers = {aaa = {}}},
exp_err = exp_err_bad_value,
},
{
'table header value w/o __tostring() metamethod',
opts = {headers = {aaa = setmetatable({}, {})}},
exp_err = exp_err_bad_value,
},
}
test:plan(#cases)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment