Fix. tostring metamethod have to return string or raise error
This commit is contained in:
parent
474135372e
commit
ce1be595ca
@ -136,6 +136,31 @@ static int lcurl_url_get(lua_State *L, CURLUPart what, CURLUcode empty) {
|
||||
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){\
|
||||
return lcurl_url_set(L, CURL##S);\
|
||||
}
|
||||
@ -172,7 +197,7 @@ static const struct luaL_Reg lcurl_url_methods[] = {
|
||||
{ "dup", lcurl_url_dup },
|
||||
{ "cleanup", lcurl_url_cleanup },
|
||||
{ "__gc", lcurl_url_cleanup },
|
||||
{ "__tostring", lcurl_url_get_url },
|
||||
{ "__tostring", lcurl_url_to_s },
|
||||
|
||||
{ NULL,NULL }
|
||||
};
|
||||
|
@ -183,6 +183,18 @@ it('should raise error for invalid url', function()
|
||||
assert_match('CURL%-URL', tostring(err))
|
||||
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()
|
||||
-- url = U"http://example.com"
|
||||
-- assert_equal(url, url:set_query("a=hello world", curl.U_URLENCODE))
|
||||
|
Loading…
x
Reference in New Issue
Block a user