cleanup, error pages
This commit is contained in:
+15
-13
@@ -375,18 +375,25 @@ static bool uce_header_name_safe(String name)
|
||||
static DValue uce_http_request_value(const DValue& req)
|
||||
{
|
||||
DValue r; r["status"]=(f64)0; r["headers"].set_array(); r["body"]=""; r["error"]="";
|
||||
String method = req.key("method") ? to_upper(req.key("method")->to_string()) : String("GET");
|
||||
String url = req.key("url") ? req.key("url")->to_string() : String("");
|
||||
const DValue* method_value = req.key("method");
|
||||
const DValue* url_value = req.key("url");
|
||||
const DValue* timeout_value = req.key("timeout_ms");
|
||||
const DValue* follow_redirects_value = req.key("follow_redirects");
|
||||
const DValue* headers_value = req.key("headers");
|
||||
const DValue* body_value = req.key("body");
|
||||
String method = method_value ? to_upper(method_value->to_string()) : String("GET");
|
||||
String url = url_value ? url_value->to_string() : String("");
|
||||
if(url=="" || url.find('\0')!=String::npos) { r["error"]="missing url"; return(r); }
|
||||
if(method=="") method="GET";
|
||||
std::vector<String> argv = {"curl", "-sS", "--http1.0", "-X", method, "-D", "-", "-w", "\nUCE_HTTP_STATUS:%{http_code}", "--max-time", std::to_string(std::max<u64>(1, (req.key("timeout_ms") ? req.key("timeout_ms")->to_u64(5000) : 5000) / 1000))};
|
||||
if(req.key("follow_redirects") && req.key("follow_redirects")->to_bool()) argv.push_back("-L");
|
||||
if(req.key("headers")) req.key("headers")->each([&](const DValue& v, String k){ if(uce_header_name_safe(k)) { argv.push_back("-H"); argv.push_back(k + ": " + replace(replace(v.to_string(), "\r", " "), "\n", " ")); } });
|
||||
String body = req.key("body") ? req.key("body")->to_string() : String("");
|
||||
if(req.key("body")) { argv.push_back("--data-binary"); argv.push_back("@-"); }
|
||||
u64 timeout_ms = timeout_value ? timeout_value->to_u64(5000) : 5000;
|
||||
std::vector<String> argv = {"curl", "-sS", "--http1.0", "-X", method, "-D", "-", "-w", "\nUCE_HTTP_STATUS:%{http_code}", "--max-time", std::to_string(std::max<u64>(1, timeout_ms / 1000))};
|
||||
if(follow_redirects_value && follow_redirects_value->to_bool()) argv.push_back("-L");
|
||||
if(headers_value) headers_value->each([&](const DValue& v, String k){ if(uce_header_name_safe(k)) { argv.push_back("-H"); argv.push_back(k + ": " + replace(replace(v.to_string(), "\r", " "), "\n", " ")); } });
|
||||
String body = body_value ? body_value->to_string() : String("");
|
||||
if(body_value) { argv.push_back("--data-binary"); argv.push_back("@-"); }
|
||||
argv.push_back(url);
|
||||
if(access("/usr/bin/curl", X_OK)!=0 && access("/bin/curl", X_OK)!=0) { r["error"]="curl binary not found in runtime PATH"; return(r); }
|
||||
DValue pr = uce_exec_argv_capture(argv, body, req.key("timeout_ms") ? req.key("timeout_ms")->to_u64(5000) : 5000);
|
||||
DValue pr = uce_exec_argv_capture(argv, body, timeout_ms);
|
||||
String out = pr["stdout"].to_string();
|
||||
String marker="\nUCE_HTTP_STATUS:"; size_t mp=out.rfind(marker);
|
||||
if(mp!=String::npos) { r["status"]=(f64)strtoull(out.c_str()+mp+marker.size(),0,10); out=out.substr(0,mp); }
|
||||
@@ -989,11 +996,6 @@ public:
|
||||
return("");
|
||||
}
|
||||
|
||||
String render_entry(const String& entry_source_path)
|
||||
{
|
||||
return(invoke_entry(entry_source_path, "render"));
|
||||
}
|
||||
|
||||
String collect(WasmResponse& response)
|
||||
{
|
||||
String error = call_core("uce_wasm_finish_output", {}, 0);
|
||||
|
||||
Reference in New Issue
Block a user