Stop using cv2pdb and improve HTTP handling a little
This commit is contained in:
@@ -1143,6 +1143,24 @@ eng::string HttpParser::first_path_component(std::string_view defval) const {
|
||||
}
|
||||
}
|
||||
|
||||
eng::string HttpParser::to_lua_identifier(std::string_view pathcomp) {
|
||||
eng::ostringstream oss;
|
||||
for (char c : pathcomp) {
|
||||
if (sv::ascii_islower(c)) {
|
||||
oss << c;
|
||||
} else if (sv::ascii_isupper(c)) {
|
||||
oss << char(c + 'a' - 'A');
|
||||
} else if (sv::ascii_isdigit(c)) {
|
||||
oss << c;
|
||||
} else if ((c == '.') || (c == '_')) {
|
||||
oss << '_';
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
void HttpParser::fail(int code, std::string_view message) {
|
||||
status_ = code;
|
||||
error_ = message;
|
||||
|
||||
@@ -421,6 +421,13 @@ public:
|
||||
// Synthesize an error and store it in lua.
|
||||
//
|
||||
static void store_fail(LuaCoreStack &LS, LuaSlot tab, int status, std::string_view error);
|
||||
|
||||
// Convert a path component into a lua identifier.
|
||||
//
|
||||
// Returns the empty string if the path component cannot be converted
|
||||
// to a lua identifier.
|
||||
//
|
||||
static eng::string to_lua_identifier(std::string_view pathcomp);
|
||||
};
|
||||
|
||||
class HttpClientRequestMap : public eng::map<int64_t, HttpClientRequest> {
|
||||
|
||||
@@ -457,13 +457,6 @@ HttpServerResponse World::http_serve(const HttpParser &request) {
|
||||
return response;
|
||||
}
|
||||
|
||||
// Get the name of the desired function.
|
||||
std::string_view fn = request.first_path_component("index");
|
||||
if (!sv::is_lua_id(fn)) {
|
||||
response.fail(404, util::ss("not a function name: ", fn));
|
||||
return response;
|
||||
}
|
||||
|
||||
lua_State *L = state();
|
||||
LuaVar www, func, reqtab;
|
||||
LuaExtStack LS(L, www, func, reqtab);
|
||||
@@ -476,11 +469,19 @@ HttpServerResponse World::http_serve(const HttpParser &request) {
|
||||
return response;
|
||||
}
|
||||
|
||||
// Get the name of the desired function.
|
||||
std::string_view orig_fn = request.first_path_component("index");
|
||||
eng::string lua_fn = HttpParser::to_lua_identifier(orig_fn);
|
||||
if (lua_fn.empty()) {
|
||||
response.fail(404, util::ss("cannot convert to lua function name: ", orig_fn));
|
||||
return response;
|
||||
}
|
||||
|
||||
// Get the closure. If there's no such closure,
|
||||
// return a 404 Not Found to the client.
|
||||
LS.rawget(func, www, fn);
|
||||
LS.rawget(func, www, lua_fn);
|
||||
if (!LS.isfunction(func)) {
|
||||
response.fail(404, util::ss("no such function: www.", fn));
|
||||
response.fail(404, util::ss("no such lua function: www.", lua_fn));
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -507,7 +508,7 @@ HttpServerResponse World::http_serve(const HttpParser &request) {
|
||||
// a 500 Internal Server Error to the client.
|
||||
int newtop = lua_gettop(L);
|
||||
if ((newtop != oldtop + 1) || (!lua_istable(L, newtop))) {
|
||||
response.fail(500, util::ss("lua function www.", fn, " didn't return a table"));
|
||||
response.fail(500, util::ss("lua function www.", lua_fn, " didn't return a table"));
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user