commit e3c1cbab1623421ba65631719de71534f41597a3 from: Omar Polo date: Tue Sep 20 15:49:23 2022 UTC add an HTML error page commit - 2df73ca57f0f74cefb6136e53c74a1c09c03c5c7 commit + e3c1cbab1623421ba65631719de71534f41597a3 blob - 47972ac4e436e4ff4bf9a5a6c0ada9064adb0042 blob + 9f6ee305974090f96125a11031ea41ed33f50917 --- fragments.tmpl +++ fragments.tmpl @@ -31,7 +31,11 @@ !} {{ define tp_head(struct template *tp, const char *lang, - const char *stylesheet) }} + const char *title) }} +{! struct client *clt = tp->tp_arg; !} +{! struct proxy_config *pc = clt->clt_pc; !} +{! const char *stylesheet = pc->stylesheet; !} +{! const char *path = clt->clt_path_info; !} @@ -39,6 +43,13 @@ {{ if *stylesheet != '\0' }} {{ end }} + + {{ if title }} + {{ title }} + {{ else }} + gemini://{{ pc->proxy_name }}{{ path }} + {{ end }} + {{ end }} @@ -86,3 +97,13 @@ gemini://{{ host }}{{ path }} {{ end }} + +{{ define tp_error(struct template *tp, const char *reason) }} + {{ render tp_head(tp, "en", "Proxy error") }} +
+

Proxy error

+

Unable to serve the page due to:

+
{{ reason }}
+
+ {{ render tp_foot(tp) }} +{{ end }} blob - 595f05ec0916cb194741e3f8f6d831dd3430c142 blob + bede9bcd83c52199ed920b3b4a3b8ac63d7a072a --- galileo.h +++ galileo.h @@ -164,6 +164,7 @@ int fcgi_client_cmp(struct client *, struct client *) int tp_head(struct template *, const char *, const char *); int tp_foot(struct template *); int tp_figure(struct template *, const char *, const char *); +int tp_error(struct template *, const char *); /* galileo.c */ int accept_reserve(int, struct sockaddr *, socklen_t *, int, blob - 3af008c7e46ff921680aae893019c2ed5ea69d83 blob + 44302b7b6add1c4ed72d4a41d01762fe09db3ca5 --- proxy.c +++ proxy.c @@ -358,8 +358,10 @@ proxy_start_request(struct galileo *env, struct client char port[32]; if ((clt->clt_pc = proxy_server_match(env, clt)) == NULL) { - if (proxy_start_reply(clt, 501, NULL) == -1) + if (proxy_start_reply(clt, 501, "text/html") == -1) return; + if (tp_error(clt->clt_tp, "unknown server") == -1) + return; fcgi_end_request(clt, 1); return; } @@ -398,9 +400,9 @@ proxy_resolved(struct asr_result *res, void *d) log_warnx("failed to resolve %s:%d: %s", pc->proxy_addr, pc->proxy_port, gai_strerror(res->ar_gai_errno)); - if (proxy_start_reply(clt, 501, "text/plain") == -1) + if (proxy_start_reply(clt, 501, "text/html") == -1) return; - if (clt_printf(clt, "Proxy error; connection failed") == -1) + if (tp_error(clt->clt_tp, "Can't resolve host") == -1) return; fcgi_end_request(clt, 1); return; @@ -514,9 +516,9 @@ done: err: log_warn("failed to connect to %s:%d", clt->clt_pc->proxy_addr, clt->clt_pc->proxy_port); - if (proxy_start_reply(clt, 501, "text/plain") == -1) + if (proxy_start_reply(clt, 501, "text/html") == -1) return; - if (clt_printf(clt, "Proxy error; connection failed") == -1) + if (tp_error(clt->clt_tp, "Can't connect") == -1) return; fcgi_end_request(clt, 1); } @@ -622,7 +624,6 @@ void proxy_read(struct bufferevent *bev, void *d) { struct client *clt = d; - struct proxy_config *pc = clt->clt_pc; struct evbuffer *src = EVBUFFER_INPUT(bev); const char *ctype; char lang[16]; @@ -658,12 +659,9 @@ proxy_read(struct bufferevent *bev, void *d) /* handled below */ break; default: - if (proxy_start_reply(clt, 501, "text/plain") == -1) - goto err; - if (clt_printf(clt, "Request failed with code %c%c\n\n", - hdr[0], hdr[1]) == -1) + if (proxy_start_reply(clt, 501, "text/html") == -1) goto err; - if (clt_printf(clt, "The server says: %s\n", &hdr[3]) == -1) + if (tp_error(clt->clt_tp, &hdr[3]) == -1) goto err; fcgi_end_request(clt, 1); goto err; @@ -671,10 +669,10 @@ proxy_read(struct bufferevent *bev, void *d) mime = hdr + 2 + strspn(hdr + 2, " \t"); if (parse_mime(clt, mime, lang, sizeof(lang)) == -1) { - if (proxy_start_reply(clt, 501, "text/plain") == -1) + if (proxy_start_reply(clt, 501, "text/html") == -1) goto err; - if (clt_printf(clt, "Failed to parse the Gemini response\n") - == -1) + if (tp_error(clt->clt_tp, "Bad response") == -1) + goto err; fcgi_end_request(clt, 1); goto err; } @@ -692,7 +690,7 @@ proxy_read(struct bufferevent *bev, void *d) clt->clt_headersdone = 1; if (clt->clt_translate && - tp_head(clt->clt_tp, lang, pc->stylesheet) == -1) + tp_head(clt->clt_tp, lang, NULL) == -1) goto err; /* @@ -723,9 +721,9 @@ proxy_error(struct bufferevent *bev, short err, void * err); if (!clt->clt_headersdone) { - if (proxy_start_reply(clt, 501, "text/plain") == -1) + if (proxy_start_reply(clt, 501, "text/html") == -1) return; - if (clt_printf(clt, "Proxy error\n") == -1) + if (tp_error(clt->clt_tp, "Proxy error") == -1) return; } else if (status == 0) { if (clt->clt_translate & TR_PRE) {