Fix. tostring metamethod have to return string or raise error

This commit is contained in:
Alexey Melnichuk 2018-11-04 19:15:35 +03:00
parent 474135372e
commit ce1be595ca
2 changed files with 38 additions and 1 deletions

View File

@ -136,6 +136,31 @@ static int lcurl_url_get(lua_State *L, CURLUPart what, CURLUcode empty) {
return 1; return 1;
} }
static int lcurl_url_to_s(lua_State *L) {
lcurl_url_t *p = lcurl_geturl(L);
char *part = NULL;
CURLUcode code = curl_url_get(p->url, CURLUPART_URL, &part, 0);
if (code != CURLUE_OK) {
if (part) {
curl_free(part);
}
return lcurl_fail_ex(L, LCURL_ERROR_RAISE, LCURL_ERROR_URL, code);
}
if (part == NULL) {
lua_pushliteral(L, "");
}
else {
lua_pushstring(L, part);
curl_free(part);
}
return 1;
}
#define ENTRY_PART(N, S, E) static int lcurl_url_set_##N(lua_State *L){\ #define ENTRY_PART(N, S, E) static int lcurl_url_set_##N(lua_State *L){\
return lcurl_url_set(L, CURL##S);\ return lcurl_url_set(L, CURL##S);\
} }
@ -172,7 +197,7 @@ static const struct luaL_Reg lcurl_url_methods[] = {
{ "dup", lcurl_url_dup }, { "dup", lcurl_url_dup },
{ "cleanup", lcurl_url_cleanup }, { "cleanup", lcurl_url_cleanup },
{ "__gc", lcurl_url_cleanup }, { "__gc", lcurl_url_cleanup },
{ "__tostring", lcurl_url_get_url }, { "__tostring", lcurl_url_to_s },
{ NULL,NULL } { NULL,NULL }
}; };

View File

@ -183,6 +183,18 @@ it('should raise error for invalid url', function()
assert_match('CURL%-URL', tostring(err)) assert_match('CURL%-URL', tostring(err))
end) end)
it('should raise error for tostring', function()
url = curl.url()
local _, err = assert_false(pcall(tostring, url))
assert_match('CURL%-URL', tostring(err))
end)
it('should raise error for tostring in safe mode', function()
url = scurl.url()
local _, err = assert_false(pcall(tostring, url))
assert_match('CURL%-URL', tostring(err))
end)
-- it('should set encoded query', function() -- it('should set encoded query', function()
-- url = U"http://example.com" -- url = U"http://example.com"
-- assert_equal(url, url:set_query("a=hello world", curl.U_URLENCODE)) -- assert_equal(url, url:set_query("a=hello world", curl.U_URLENCODE))