Do more refactoring

Start merging the error pages into a single standardized error page
Lessen log spew when site is being crawled by robots
This commit is contained in:
Robin Malley 2021-02-03 04:09:12 +00:00
parent 7462b1e7ac
commit 879e89fa8d
4 changed files with 53 additions and 3 deletions

View File

@ -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},

View File

@ -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

View File

@ -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)

14
src/pages/error.etlua.in Normal file
View File

@ -0,0 +1,14 @@
<{system cat src/pages/parts/header.etlua}>
<h1 class="title">
<% if errcode then -%><%= errcode -%><% end -%>:
<% if errcodemsg then -%><%= errcodemsg -%><% end %>
</h1>
<% if explanation then -%><%= explanation -%><% end -%>
<% if should_traceback then %>
<code><pre>
<%- debug.traceback() %>
</pre></code>
<% end %>
<{system cat src/pages/parts/footer.etlua}>