diff --git a/src/libkore.c b/src/libkore.c index 621a4cd..1972be0 100644 --- a/src/libkore.c +++ b/src/libkore.c @@ -108,6 +108,29 @@ lhttp_response_header(lua_State *L){ return 0; } +/* +http_request_header(request::userdata, header::string)::(string || false, string) +*/ +int +lhttp_request_header(lua_State *L){ + const char *header = luaL_checkstring(L,-1); + struct http_request *req = luaL_checkrequest(L,-2); + lua_pop(L,2); + const char *data; + int err = http_request_header(req,header,&data); + if(err == KORE_RESULT_OK){ + lua_pushstring(L,data); + return 1; + }else{ + lua_pushboolean(L,0); + lua_pushstring(L,"Failed to get header: "); + lua_pushstring(L,header); + lua_concat(L,2); + return 2; + } +} + + /* http_response_cookie(req::userdata, name::string, value::string, path::string, expires::number, maxage::number) */ @@ -270,6 +293,7 @@ lkore_log(lua_State *L){ static const luaL_Reg kore_funcs[] = { {"http_response", lhttp_response}, {"http_response_header", lhttp_response_header}, + {"http_request_header", lhttp_request_header}, {"http_method_text",lhttp_method_text}, {"http_request_get_path",lhttp_request_get_path}, {"http_request_get_host",lhttp_request_get_host}, diff --git a/src/lua/endpoints/read_get.lua b/src/lua/endpoints/read_get.lua index 670dd97..17fb549 100644 --- a/src/lua/endpoints/read_get.lua +++ b/src/lua/endpoints/read_get.lua @@ -98,11 +98,21 @@ local function read_get(req) path = http_request_get_path(req), method = http_method_text(req), } - + local err --Get our story id assert(string.len(ps.path) > 0,"Tried to read 0-length story id") ps.idp = string.sub(ps.path,2)--remove leading "/" - ps.storyid = util.decode_id(ps.idp) + ps.storyid,err = util.decode_id(ps.idp) + if not ps.storyid then + local page = pages.error{ + errcode = 400, + errcodemsg = "Bad Request", + explanation = string.format("Failed to find story id %q: %s",ps.path,err) + } + http_response(req,400,page) + return + end + add_view(ps.storyid) --If we're logged in, set author and authorid diff --git a/src/lua/util.lua b/src/lua/util.lua index 103ec97..f85f21c 100644 --- a/src/lua/util.lua +++ b/src/lua/util.lua @@ -119,12 +119,14 @@ function util.decode_id(s) if res then return id else - error("Failed to decode id:" .. s) + print("Failed to decode id:" .. s) + return false,"Failed to decode id:" .. s end end --arbitary data to hex encoded string function util.encode_unlisted(str) + assert(type(str) == "string","Tried to encode something not a string:" .. type(Str)) local safe = {} for i = 1,#str do local byte = str:byte(i) diff --git a/src/pages/error.etlua.in b/src/pages/error.etlua.in new file mode 100644 index 0000000..df3f9ce --- /dev/null +++ b/src/pages/error.etlua.in @@ -0,0 +1,14 @@ +<{system cat src/pages/parts/header.etlua}> +

+ <% if errcode then -%><%= errcode -%><% end -%>: + <% if errcodemsg then -%><%= errcodemsg -%><% end %> +

+ +<% if explanation then -%><%= explanation -%><% end -%> + +<% if should_traceback then %> +
+	<%- debug.traceback() %>
+	
+<% end %> +<{system cat src/pages/parts/footer.etlua}>