[PATCH ocserv 3/3] Return HTML error message on 404

Kevin Cernekee cernekee at gmail.com
Sun Jan 24 13:11:40 PST 2016


Currently ocserv's 404 errors show up as a blank page in most web
browsers.  Add a simple HTML error page.
---
 src/worker-http-handlers.c | 16 ++++++++++++++--
 src/worker-vpn.c           |  9 ++++-----
 src/worker.h               |  1 +
 3 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/src/worker-http-handlers.c b/src/worker-http-handlers.c
index 8303fb262156..e698bc28f13f 100644
--- a/src/worker-http-handlers.c
+++ b/src/worker-http-handlers.c
@@ -39,6 +39,18 @@
 #include <cookies.h>
 #include <tlslib.h>
 
+static const char html_404[] = "<html><body><h1>404 Not Found</h1></body></html>\r\n";
+
+int response_404(worker_st *ws, unsigned http_ver)
+{
+	if (cstp_printf(ws, "HTTP/1.%u 404 Not found\r\n", http_ver) < 0 ||
+	    cstp_printf(ws, "Content-length: %u\r\n", (unsigned)strlen(html_404)) < 0 ||
+	    cstp_puts  (ws, "Connection: close\r\n\r\n") < 0 ||
+	    cstp_puts  (ws, html_404) < 0)
+		return -1;
+	return 0;
+}
+
 #ifdef ANYCONNECT_CLIENT_COMPAT
 static int send_headers(worker_st *ws, unsigned http_ver, const char *content_type,
 			unsigned content_length)
@@ -80,14 +92,14 @@ int get_config_handler(worker_st *ws, unsigned http_ver)
 
 	if (ws->user_config->xml_config_file == NULL) {
 		oclog(ws, LOG_INFO, "requested config but no config file is set");
-		cstp_printf(ws, "HTTP/1.%u 404 Not found\r\n", http_ver);
+		response_404(ws, http_ver);
 		return -1;
 	}
 	
 	ret = stat(ws->user_config->xml_config_file, &st);
 	if (ret == -1) {
 		oclog(ws, LOG_INFO, "cannot load config file '%s'", ws->user_config->xml_config_file);
-		cstp_printf(ws, "HTTP/1.%u 404 Not found\r\n", http_ver);
+		response_404(ws, http_ver);
 		return -1;
 	}
 
diff --git a/src/worker-vpn.c b/src/worker-vpn.c
index 3a0214d67c89..676415f47755 100644
--- a/src/worker-vpn.c
+++ b/src/worker-vpn.c
@@ -524,7 +524,7 @@ void vpn_server(struct worker_st *ws)
 		fn = http_get_url_handler(ws->req.url);
 		if (fn == NULL) {
 			oclog(ws, LOG_HTTP_DEBUG, "unexpected URL %s", ws->req.url);
-			cstp_puts(ws, "HTTP/1.1 404 Not found\r\n\r\n");
+			response_404(ws, parser.http_minor);
 			goto finish;
 		}
 		ret = fn(ws, parser.http_minor);
@@ -559,7 +559,7 @@ void vpn_server(struct worker_st *ws)
 		if (fn == NULL) {
 			oclog(ws, LOG_HTTP_DEBUG, "unexpected POST URL %s",
 			      ws->req.url);
-			cstp_puts(ws, "HTTP/1.1 404 Not found\r\n\r\n");
+			response_404(ws, parser.http_minor);
 			goto finish;
 		}
 
@@ -578,8 +578,7 @@ void vpn_server(struct worker_st *ws)
 	} else {
 		oclog(ws, LOG_HTTP_DEBUG, "unexpected HTTP method %s",
 		      http_method_str(parser.method));
-		cstp_printf(ws, "HTTP/1.%u 404 Nah, go away\r\n\r\n",
-			   parser.http_minor);
+		response_404(ws, parser.http_minor);
 	}
 
  finish:
@@ -1346,7 +1345,7 @@ static int connect_handler(worker_st * ws)
 
 	if (strcmp(req->url, "/CSCOSSLC/tunnel") != 0) {
 		oclog(ws, LOG_INFO, "bad connect request: '%s'\n", req->url);
-		cstp_puts(ws, "HTTP/1.1 404 Nah, go away\r\n\r\n");
+		response_404(ws, 1);
 		cstp_fatal_close(ws, GNUTLS_A_ACCESS_DENIED);
 		exit_worker(ws);
 	}
diff --git a/src/worker.h b/src/worker.h
index 04751a7290b5..bd0ffc2f1fdf 100644
--- a/src/worker.h
+++ b/src/worker.h
@@ -288,6 +288,7 @@ int get_auth_handler(worker_st *server, unsigned http_ver);
 int post_auth_handler(worker_st *server, unsigned http_ver);
 int post_kkdcp_handler(worker_st *server, unsigned http_ver);
 
+int response_404(worker_st *ws, unsigned http_ver);
 int get_empty_handler(worker_st *server, unsigned http_ver);
 int get_config_handler(worker_st *ws, unsigned http_ver);
 int get_string_handler(worker_st *ws, unsigned http_ver);
-- 
2.7.0




More information about the openconnect-devel mailing list