summaryrefslogtreecommitdiff
path: root/uhttpd-lua.c
diff options
context:
space:
mode:
authorjow <jow@3c298f89-4303-0410-b956-a3cf2f4a3e73>2012-05-03 17:19:18 +0000
committerjow <jow@3c298f89-4303-0410-b956-a3cf2f4a3e73>2012-05-03 17:19:18 +0000
commitf978449dac910d3379e44ea438198c8f29df8008 (patch)
tree46b1359d5041652550fecbc06438cd2c8b67aec5 /uhttpd-lua.c
parent0fcbf2d4eae475903f40fd94223671744d75109e (diff)
downloaduhttpd-f978449dac910d3379e44ea438198c8f29df8008.tar.gz
Fixed: [PATCH 2/3] uhttpd URL-codec enhancements.
My apologies, the 2nd of those patches had a syntax error -- that's what I get for making a last-minute edit, even to the comments, without testing! :-p Here is the corrected patch. -- David From d259cff104d2084455476b82e92a3a27524f4263 Mon Sep 17 00:00:00 2001 From: David Favro <openwrt@meta-dynamic.com> Date: Fri, 27 Apr 2012 14:17:52 -0400 Subject: [PATCH] uhttpd URL-codec enhancements. * uh_urlencode() and uh_urldecode() now return an error condition for buffer-overflow and malformed-encoding rather than normal return with corrupt or truncated data. As HTTP request processing is currently implemented, this causes a 404 HTTP status returned to the client, while 400 is more appropriate. * Exposed urlencode() to Lua. * Lua's uhttpd.urlencode() and .urldecode() now raise an error condition for buffer-overflow and malformed-encoding rather than normal return with incorrect data. git-svn-id: svn://svn.openwrt.org/openwrt/trunk/package/uhttpd/src@31570 3c298f89-4303-0410-b956-a3cf2f4a3e73
Diffstat (limited to 'uhttpd-lua.c')
-rw-r--r--uhttpd-lua.c24
1 files changed, 21 insertions, 3 deletions
diff --git a/uhttpd-lua.c b/uhttpd-lua.c
index c2efe33..a140dc2 100644
--- a/uhttpd-lua.c
+++ b/uhttpd-lua.c
@@ -108,19 +108,34 @@ static int uh_lua_sendc(lua_State *L)
return uh_lua_send_common(L, 1);
}
-static int uh_lua_urldecode(lua_State *L)
+static int uh_lua_str2str(lua_State *L, int (*xlate_func) (char *, int, const char *, int))
{
- size_t inlen, outlen;
+ size_t inlen;
+ int outlen;
const char *inbuf;
char outbuf[UH_LIMIT_MSGHEAD];
inbuf = luaL_checklstring(L, 1, &inlen);
- outlen = uh_urldecode(outbuf, sizeof(outbuf), inbuf, inlen);
+ outlen = (* xlate_func)(outbuf, sizeof(outbuf), inbuf, inlen);
+ if( outlen < 0 )
+ luaL_error( L, "%s on URL-encode codec",
+ (outlen==-1) ? "buffer overflow" : "malformed string" );
lua_pushlstring(L, outbuf, outlen);
return 1;
}
+static int uh_lua_urldecode(lua_State *L)
+{
+ return uh_lua_str2str( L, uh_urldecode );
+}
+
+
+static int uh_lua_urlencode(lua_State *L)
+{
+ return uh_lua_str2str( L, uh_urlencode );
+}
+
lua_State * uh_lua_init(const char *handler)
{
@@ -146,6 +161,9 @@ lua_State * uh_lua_init(const char *handler)
lua_pushcfunction(L, uh_lua_urldecode);
lua_setfield(L, -2, "urldecode");
+ lua_pushcfunction(L, uh_lua_urlencode);
+ lua_setfield(L, -2, "urlencode");
+
/* _G.uhttpd = { ... } */
lua_setfield(L, LUA_GLOBALSINDEX, "uhttpd");