Factor out duplicated code in the AnyConnect compatibility handlers. --- src/worker-http-handlers.c | 182 ++++++++++----------------------------------- 1 file changed, 39 insertions(+), 143 deletions(-) V1->V2: Use sizeof instead of strlen Use '#define FOO "string"' consistently diff --git a/src/worker-http-handlers.c b/src/worker-http-handlers.c index 57f77ed7fcd8..bb5651d50d42 100644 --- a/src/worker-http-handlers.c +++ b/src/worker-http-handlers.c @@ -40,12 +40,35 @@ #include <tlslib.h> #ifdef ANYCONNECT_CLIENT_COMPAT -const char empty_msg[] = "<html></html>\n"; +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 content_length) +{ + /* don't bother uncorking on error - the connection will be closed anyway */ + cstp_cork(ws); + if (send_headers(ws, http_ver, content_type, content_length) < 0 || + cstp_send(ws, data, content_length) < 0 || + cstp_uncork(ws) < 0) + return -1; + return 0; +} 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 +80,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 +88,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 +106,14 @@ struct stat st; int get_string_handler(worker_st *ws, unsigned http_ver) { -int ret; -const char *data; -int len; - 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; + return send_string(ws, http_ver, "text/xml", VPN_VERSION, + sizeof(VPN_VERSION) - 1); } else { - data = XML_START; - len = sizeof(XML_START)-1; + return send_string(ws, http_ver, "text/xml", XML_START, + 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; } #define SH_SCRIPT "#!/bin/sh\n\n" \ @@ -157,85 +121,17 @@ 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, + sizeof(SH_SCRIPT) - 1); } +#define EMPTY_MSG "<html></html>\n" + 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, + sizeof(EMPTY_MSG) - 1); } #endif -- 2.7.0