[PATCH ocserv 2/3] Use helper functions to send common HTTP headers/responses

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


Factor out duplicated code in the AnyConnect compatibility handlers.
---
 src/worker-http-handlers.c | 175 ++++++++++-----------------------------------
 1 file changed, 36 insertions(+), 139 deletions(-)

diff --git a/src/worker-http-handlers.c b/src/worker-http-handlers.c
index 57f77ed7fcd8..8303fb262156 100644
--- a/src/worker-http-handlers.c
+++ b/src/worker-http-handlers.c
@@ -40,12 +40,39 @@
 #include <tlslib.h>
 
 #ifdef ANYCONNECT_CLIENT_COMPAT
+static int send_headers(worker_st *ws, unsigned http_ver, const char *content_type,
+			unsigned content_length)
+{
+	if (cstp_printf(ws, "HTTP/1.%u 200 OK\r\n", http_ver) < 0 ||
+	    cstp_puts  (ws, "Connection: Keep-Alive\r\n") < 0 ||
+	    cstp_printf(ws, "Content-Type: %s\r\n", content_type) < 0 ||
+	    cstp_puts  (ws, "X-Transcend-Version: 1\r\n") < 0 ||
+	    cstp_printf(ws, "Content-Length: %u\r\n", content_length) < 0 ||
+	    cstp_puts  (ws, "\r\n") < 0)
+		return -1;
+	return 0;
+}
+
+static int send_string(worker_st *ws, unsigned http_ver, const char *content_type,
+		       const char *data)
+{
+	int len = strlen(data);
+
+	/* don't bother uncorking on error - the connection will be closed anyway */
+	cstp_cork(ws);
+	if (send_headers(ws, http_ver, content_type, len) < 0 ||
+	    cstp_send(ws, data, len) < 0 ||
+	    cstp_uncork(ws) < 0)
+		return -1;
+	return 0;
+}
+
 const char empty_msg[] = "<html></html>\n";
 
 int get_config_handler(worker_st *ws, unsigned http_ver)
 {
-int ret;
-struct stat st;
+	int ret;
+	struct stat st;
 
 	oclog(ws, LOG_HTTP_DEBUG, "requested config: %s", ws->req.url); 
 
@@ -57,7 +84,7 @@ struct stat st;
 		return -1;
 	}
 	
-	ret = stat( ws->user_config->xml_config_file, &st);
+	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);
@@ -65,32 +92,8 @@ struct stat st;
 	}
 
 	cstp_cork(ws);
-	ret = cstp_printf(ws, "HTTP/1.%u 200 OK\r\n", http_ver);
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_puts(ws, "Connection: Keep-Alive\r\n");
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_puts(ws, "Content-Type: text/xml\r\n");
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_puts(ws, "X-Transcend-Version: 1\r\n");
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_printf(ws, "Content-Length: %u\r\n", (unsigned)st.st_size);
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_puts(ws, "\r\n");
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_uncork(ws);
-	if (ret < 0)
+	if (send_headers(ws, http_ver, "text/xml", (unsigned)st.st_size) < 0 ||
+	    cstp_uncork(ws) < 0)
 		return -1;
 
 	ret = cstp_send_file(ws, ws->user_config->xml_config_file);
@@ -107,49 +110,15 @@ struct stat st;
 
 int get_string_handler(worker_st *ws, unsigned http_ver)
 {
-int ret;
-const char *data;
-int len;
+	const char *data;
 
 	oclog(ws, LOG_HTTP_DEBUG, "requested fixed string: %s", ws->req.url); 
 	if (!strcmp(ws->req.url, "/1/binaries/update.txt")) {
 		data = VPN_VERSION;
-		len = sizeof(VPN_VERSION)-1;
 	} else {
 		data = XML_START;
-		len = sizeof(XML_START)-1;
 	}
-
-	cstp_cork(ws);
-	ret = cstp_printf(ws, "HTTP/1.%u 200 OK\r\n", http_ver);
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_puts(ws, "Connection: Keep-Alive\r\n");
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_puts(ws, "Content-Type: text/xml\r\n");
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_puts(ws, "X-Transcend-Version: 1\r\n");
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_printf(ws, "Content-Length: %d\r\n\r\n", len);
-	if (ret < 0)
-		return -1;
-		
-	ret = cstp_send(ws, data, len);
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_uncork(ws);
-	if (ret < 0)
-		return -1;
-	
-	return 0;
+	return send_string(ws, http_ver, "text/xml", data);
 }
 
 #define SH_SCRIPT "#!/bin/sh\n\n" \
@@ -157,85 +126,13 @@ int len;
 
 int get_dl_handler(worker_st *ws, unsigned http_ver)
 {
-int ret;
-const char *data;
-int len;
-
 	oclog(ws, LOG_HTTP_DEBUG, "requested downloader: %s", ws->req.url); 
-
-	data = SH_SCRIPT;
-	len = sizeof(SH_SCRIPT)-1;
-
-	cstp_cork(ws);
-	ret = cstp_printf(ws, "HTTP/1.%u 200 OK\r\n", http_ver);
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_puts(ws, "Connection: Keep-Alive\r\n");
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_puts(ws, "Content-Type: application/x-shellscript\r\n");
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_puts(ws, "X-Transcend-Version: 1\r\n");
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_printf(ws, "Content-Length: %d\r\n\r\n", len);
-	if (ret < 0)
-		return -1;
-		
-	ret = cstp_send(ws, data, len);
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_uncork(ws);
-	if (ret < 0)
-		return -1;
-	
-	return 0;
+	return send_string(ws, http_ver, "application/x-shellscript", SH_SCRIPT);
 }
 
 int get_empty_handler(worker_st *ws, unsigned http_ver)
 {
-int ret;
-
-	cstp_cork(ws);
-	ret = cstp_printf(ws, "HTTP/1.%u 200 OK\r\n", http_ver);
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_puts(ws, "Connection: Keep-Alive\r\n");
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_puts(ws, "Content-Type: text/html\r\n");
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_printf(ws, "Content-Length: %u\r\n", (unsigned int)sizeof(empty_msg)-1);
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_puts(ws, "X-Transcend-Version: 1\r\n");
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_puts(ws, "\r\n");
-	if (ret < 0)
-		return -1;
-
-	ret = cstp_send(ws, empty_msg, sizeof(empty_msg)-1);
-	if (ret < 0)
-		return -1;
-	
-	ret = cstp_uncork(ws);
-	if (ret < 0)
-		return -1;
-	
-	return 0;
+	return send_string(ws, http_ver, "text/html", empty_msg);
 }
 
 #endif
-- 
2.7.0




More information about the openconnect-devel mailing list