Skip to content
Snippets Groups Projects
server.c 75.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * libwebsockets - small server side websockets and web server implementation
     *
    
    Andy Green's avatar
    Andy Green committed
     * Copyright (C) 2010-2017 Andy Green <andy@warmcat.com>
    
     *
     *  This library is free software; you can redistribute it and/or
     *  modify it under the terms of the GNU Lesser General Public
     *  License as published by the Free Software Foundation:
     *  version 2.1 of the License.
     *
     *  This library is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     *  Lesser General Public License for more details.
     *
     *  You should have received a copy of the GNU Lesser General Public
     *  License along with this library; if not, write to the Free Software
     *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     *  MA  02110-1301  USA
     */
    
    #include "private-libwebsockets.h"
    
    
    const char * const method_names[] = {
    
    Andy Green's avatar
    Andy Green committed
    	"GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE", "CONNECT", "HEAD",
    
    #ifdef LWS_WITH_HTTP2
    
    Andy Green's avatar
    Andy Green committed
    	":path",
    
    Andy Green's avatar
    Andy Green committed
    #if defined (LWS_WITH_ESP8266)
    #undef memcpy
    void *memcpy(void *dest, const void *src, size_t n)
    {
    	return ets_memcpy(dest, src, n);
    }
    #endif
    
    
    int
    lws_context_init_server(struct lws_context_creation_info *info,
    
    Andy Green's avatar
    Andy Green committed
    			struct lws_vhost *vhost)
    
    Andy Green's avatar
    Andy Green committed
    #if LWS_POSIX
    
    Andy Green's avatar
    Andy Green committed
    	int n, opt = 1, limit = 1;
    
    Andy Green's avatar
    Andy Green committed
    #endif
    
    	lws_sockfd_type sockfd;
    
    Andy Green's avatar
    Andy Green committed
    	struct lws_vhost *vh;
    
    Andy Green's avatar
    Andy Green committed
    	int m = 0;
    
    Andy Green's avatar
    Andy Green committed
    	(void)method_names;
    
    Andy Green's avatar
    Andy Green committed
    	(void)opt;
    
    	/* set up our external listening socket we serve on */
    
    
    Andy Green's avatar
    Andy Green committed
    	if (info->port == CONTEXT_PORT_NO_LISTEN ||
    	    info->port == CONTEXT_PORT_NO_LISTEN_SERVER)
    
    Andy Green's avatar
    Andy Green committed
    	vh = vhost->context->vhost_list;
    	while (vh) {
    		if (vh->listen_port == info->port) {
    			if ((!info->iface && !vh->iface) ||
    			    (info->iface && vh->iface &&
    			    !strcmp(info->iface, vh->iface))) {
    				vhost->listen_port = info->port;
    				vhost->iface = info->iface;
    				lwsl_notice(" using listen skt from vhost %s\n",
    					    vh->name);
    				return 0;
    			}
    		}
    		vh = vh->vhost_next;
    	}
    
    
    Andy Green's avatar
    Andy Green committed
    #if LWS_POSIX
    
    	(void)n;
    
    Andy Green's avatar
    Andy Green committed
    #if defined(__linux__)
    
    Andy Green's avatar
    Andy Green committed
    	limit = vhost->context->count_threads;
    
    Andy Green's avatar
    Andy Green committed
    #endif
    
    	for (m = 0; m < limit; m++) {
    
    #ifdef LWS_WITH_UNIX_SOCK
    
    	if (LWS_UNIX_SOCK_ENABLED(vhost))
    
    Yeonjun Lim's avatar
    Yeonjun Lim committed
    		sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
    	else
    #endif
    
    #ifdef LWS_WITH_IPV6
    
    	if (LWS_IPV6_ENABLED(vhost))
    
    		sockfd = socket(AF_INET6, SOCK_STREAM, 0);
    	else
    #endif
    		sockfd = socket(AF_INET, SOCK_STREAM, 0);
    
    	if (sockfd == -1) {
    
    Andy Green's avatar
    Andy Green committed
    #else
    #if defined(LWS_WITH_ESP8266)
    	sockfd = esp8266_create_tcp_listen_socket(vhost);
    	if (!lws_sockfd_valid(sockfd)) {
    #endif
    
    Andy Green's avatar
    Andy Green committed
    #endif
    
    Andy Green's avatar
    Andy Green committed
    #if LWS_POSIX && !defined(LWS_WITH_ESP32)
    
    
    #if (defined(WIN32) || defined(_WIN32)) && defined(SO_EXCLUSIVEADDRUSE)
    	/*
    	 * only accept that we are the only listener on the port
    
    	 * https://msdn.microsoft.com/zh-tw/library/
    	 *    windows/desktop/ms740621(v=vs.85).aspx
    
    	 *
    	 * for lws, to match Linux, we default to exclusive listen
    	 */
    
    	if (!lws_check_opt(vhost->options,
    			LWS_SERVER_OPTION_ALLOW_LISTEN_SHARE)) {
    
    		if (setsockopt(sockfd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
    			       (const void *)&opt, sizeof(opt)) < 0) {
    			lwsl_err("reuseaddr failed\n");
    			compatible_close(sockfd);
    			return 1;
    		}
    	} else
    #endif
    
    
    	/*
    	 * allow us to restart even if old sockets in TIME_WAIT
    	 */
    
    	if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
    
    		       (const void *)&opt, sizeof(opt)) < 0) {
    
    Andy Green's avatar
    Andy Green committed
    		lwsl_err("reuseaddr failed\n");
    
    		compatible_close(sockfd);
    
    #if defined(LWS_WITH_IPV6) && defined(IPV6_V6ONLY)
    
    	if (LWS_IPV6_ENABLED(vhost)) {
    		if (vhost->options & LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY) {
    
    			int value = (vhost->options &
    				LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE) ? 1 : 0;
    
    			if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
    
    				      (const void*)&value, sizeof(value)) < 0) {
    
    				compatible_close(sockfd);
    				return 1;
    			}
    		}
    	}
    #endif
    
    
    #if defined(__linux__) && defined(SO_REUSEPORT)
    	n = lws_check_opt(vhost->options, LWS_SERVER_OPTION_ALLOW_LISTEN_SHARE);
    #if LWS_MAX_SMP > 1
    	n = 1;
    #endif
    
    	if (n)
    		if (vhost->context->count_threads > 1)
    			if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT,
    					(const void *)&opt, sizeof(opt)) < 0) {
    				compatible_close(sockfd);
    				return 1;
    			}
    
    Andy Green's avatar
    Andy Green committed
    #endif
    
    Andy Green's avatar
    Andy Green committed
    #endif
    
    Andy Green's avatar
    Andy Green committed
    	lws_plat_set_socket_options(vhost, sockfd);
    
    Andy Green's avatar
    Andy Green committed
    #if LWS_POSIX
    
    	n = lws_socket_bind(vhost, sockfd, info->port, info->iface);
    
    Andy Green's avatar
    Andy Green committed
    		goto bail;
    
    Andy Green's avatar
    Andy Green committed
    #endif
    
    Andy Green's avatar
    Andy Green committed
    	vhost->listen_port = info->port;
    	vhost->iface = info->iface;
    
    Andy Green's avatar
    Andy Green committed
    	wsi = lws_zalloc(sizeof(struct lws), "listen wsi");
    
    	if (wsi == NULL) {
    		lwsl_err("Out of mem\n");
    
    Andy Green's avatar
    Andy Green committed
    		goto bail;
    
    Andy Green's avatar
    Andy Green committed
    	wsi->context = vhost->context;
    
    	wsi->desc.sockfd = sockfd;
    
    Andy Green's avatar
    Andy Green committed
    	wsi->mode = LWSCM_SERVER_LISTENER;
    
    Andy Green's avatar
    Andy Green committed
    	wsi->protocol = vhost->protocols;
    
    Andy Green's avatar
    Andy Green committed
    	wsi->tsi = m;
    
    Andy Green's avatar
    Andy Green committed
    	wsi->vhost = vhost;
    	wsi->listener = 1;
    
    #ifdef LWS_WITH_LIBUV
    
    	if (LWS_LIBUV_ENABLED(vhost->context))
    		lws_uv_initvhost(vhost, wsi);
    #endif
    
    
    Andy Green's avatar
    Andy Green committed
    	if (insert_wsi_socket_into_fds(vhost->context, wsi))
    
    Andy Green's avatar
    Andy Green committed
    		goto bail;
    
    Andy Green's avatar
    Andy Green committed
    	vhost->context->count_wsi_allocated++;
    	vhost->lserv_wsi = wsi;
    
    Andy Green's avatar
    Andy Green committed
    #if LWS_POSIX
    
    	n = listen(wsi->desc.sockfd, LWS_SOMAXCONN);
    
    	if (n < 0) {
    		lwsl_err("listen failed with error %d\n", LWS_ERRNO);
    		vhost->lserv_wsi = NULL;
    		vhost->context->count_wsi_allocated--;
    		remove_wsi_socket_from_fds(wsi);
    		goto bail;
    	}
    
    Andy Green's avatar
    Andy Green committed
    	} /* for each thread able to independently listen */
    
    Andy Green's avatar
    Andy Green committed
    #else
    #if defined(LWS_WITH_ESP8266)
    
    	esp8266_tcp_stream_bind(wsi->desc.sockfd, info->port, wsi);
    
    Andy Green's avatar
    Andy Green committed
    #endif
    
    Andy Green's avatar
    Andy Green committed
    #endif
    
    Yeonjun Lim's avatar
    Yeonjun Lim committed
    	if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS)) {
    
    #ifdef LWS_WITH_UNIX_SOCK
    
    		if (LWS_UNIX_SOCK_ENABLED(vhost))
    
    			lwsl_info(" Listening on \"%s\"\n", info->iface);
    
    Yeonjun Lim's avatar
    Yeonjun Lim committed
    		else
    #endif
    
    			lwsl_info(" Listening on port %d\n", info->port);
    
    Yeonjun Lim's avatar
    Yeonjun Lim committed
            }
    
    Andy Green's avatar
    Andy Green committed
    
    bail:
    	compatible_close(sockfd);
    
    	return 1;
    
    Andy Green's avatar
    Andy Green committed
    #if defined(LWS_WITH_ESP8266)
    #undef strchr
    #define strchr ets_strchr
    #endif
    
    
    Andy Green's avatar
    Andy Green committed
    struct lws_vhost *
    lws_select_vhost(struct lws_context *context, int port, const char *servername)
    {
    	struct lws_vhost *vhost = context->vhost_list;
    
    	const char *p;
    	int n, m, colon;
    
    	n = strlen(servername);
    	colon = n;
    	p = strchr(servername, ':');
    	if (p)
    		colon = p - servername;
    
    
    	/* Priotity 1: first try exact matches */
    
    Andy Green's avatar
    Andy Green committed
    
    	while (vhost) {
    		if (port == vhost->listen_port &&
    
    		    !strncmp(vhost->name, servername, colon)) {
    
    Andy Green's avatar
    Andy Green committed
    			lwsl_info("SNI: Found: %s\n", servername);
    			return vhost;
    		}
    		vhost = vhost->vhost_next;
    	}
    
    
    	 * Priority 2: if no exact matches, try matching *.vhost-name
    
    	 * unintentional matches are possible but resolve to x.com for *.x.com
    	 * which is reasonable.  If exact match exists we already chose it and
    	 * never reach here.  SSL will still fail it if the cert doesn't allow
    	 * *.x.com.
    	 */
    	vhost = context->vhost_list;
    	while (vhost) {
    		m = strlen(vhost->name);
    		if (port == vhost->listen_port &&
    		    m <= (colon - 2) &&
    		    servername[colon - m - 1] == '.' &&
    		    !strncmp(vhost->name, servername + colon - m, m)) {
    			lwsl_info("SNI: Found %s on wildcard: %s\n",
    				    servername, vhost->name);
    			return vhost;
    		}
    		vhost = vhost->vhost_next;
    	}
    
    
    	/* Priority 3: match the first vhost on our port */
    
    	vhost = context->vhost_list;
    	while (vhost) {
    		if (port == vhost->listen_port) {
    			lwsl_info("vhost match to %s based on port %d\n",
    					vhost->name, port);
    			return vhost;
    		}
    		vhost = vhost->vhost_next;
    	}
    
    	/* no match */
    
    
    Andy Green's avatar
    Andy Green committed
    	return NULL;
    }
    
    
    Andy Green's avatar
    Andy Green committed
    LWS_VISIBLE LWS_EXTERN const char *
    lws_get_mimetype(const char *file, const struct lws_http_mount *m)
    
    Andy Green's avatar
    Andy Green committed
    {
    	int n = strlen(file);
    
    	const struct lws_protocol_vhost_options *pvo = NULL;
    
    	if (m)
    		pvo = m->extra_mimetypes;
    
    Andy Green's avatar
    Andy Green committed
    
    	if (n < 5)
    		return NULL;
    
    	if (!strcmp(&file[n - 4], ".ico"))
    		return "image/x-icon";
    
    
    	if (!strcmp(&file[n - 4], ".gif"))
    		return "image/gif";
    
    	if (!strcmp(&file[n - 3], ".js"))
    		return "text/javascript";
    
    
    Andy Green's avatar
    Andy Green committed
    	if (!strcmp(&file[n - 4], ".png"))
    		return "image/png";
    
    
    Andy Green's avatar
    Andy Green committed
    	if (!strcmp(&file[n - 4], ".jpg"))
    		return "image/jpeg";
    
    
    	if (!strcmp(&file[n - 3], ".gz"))
    		return "application/gzip";
    
    	if (!strcmp(&file[n - 4], ".JPG"))
    		return "image/jpeg";
    
    
    Andy Green's avatar
    Andy Green committed
    	if (!strcmp(&file[n - 5], ".html"))
    		return "text/html";
    
    	if (!strcmp(&file[n - 4], ".css"))
    		return "text/css";
    
    
    	if (!strcmp(&file[n - 4], ".txt"))
    		return "text/plain";
    
    
    	if (!strcmp(&file[n - 4], ".svg"))
    		return "image/svg+xml";
    
    
    	if (!strcmp(&file[n - 4], ".ttf"))
    		return "application/x-font-ttf";
    
    
    	if (!strcmp(&file[n - 4], ".otf"))
    		return "application/font-woff";
    
    
    	if (!strcmp(&file[n - 5], ".woff"))
    		return "application/font-woff";
    
    	if (!strcmp(&file[n - 4], ".xml"))
    		return "application/xml";
    
    
    	while (pvo) {
    
    		if (pvo->name[0] == '*') /* ie, match anything */
    			return pvo->value;
    
    
    		if (!strcmp(&file[n - strlen(pvo->name)], pvo->name))
    			return pvo->value;
    
    		pvo = pvo->next;
    	}
    
    
    Andy Green's avatar
    Andy Green committed
    	return NULL;
    }
    
    Andy Green's avatar
    Andy Green committed
    static lws_fop_flags_t
    lws_vfs_prepare_flags(struct lws *wsi)
    {
    	lws_fop_flags_t f = 0;
    
    	if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING))
    		return f;
    
    	if (strstr(lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING),
    		   "gzip")) {
    		lwsl_info("client indicates GZIP is acceptable\n");
    		f |= LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP;
    	}
    
    	return f;
    }
    
    Andy Green's avatar
    Andy Green committed
    
    
    static int
    lws_http_serve(struct lws *wsi, char *uri, const char *origin,
    	       const struct lws_http_mount *m)
    
    Andy Green's avatar
    Andy Green committed
    {
    
    Andy Green's avatar
    Andy Green committed
    	const struct lws_protocol_vhost_options *pvo = m->interpret;
    	struct lws_process_html_args args;
    
    	const char *mimetype;
    
    #if !defined(_WIN32_WCE) && !defined(LWS_WITH_ESP8266)
    
    Andy Green's avatar
    Andy Green committed
    	const struct lws_plat_file_ops *fops;
    
    	const char *vpath;
    
    Andy Green's avatar
    Andy Green committed
    	lws_fop_flags_t fflags = LWS_O_RDONLY;
    
    #if defined(WIN32) && defined(LWS_HAVE__STAT32I64)
    	struct _stat32i64 st;
    #else
    
    Andy Green's avatar
    Andy Green committed
    	struct stat st;
    
    Andy Green's avatar
    Andy Green committed
    	int spin = 0;
    
    Andy Green's avatar
    Andy Green committed
    	char path[256], sym[512];
    
    Andy Green's avatar
    Andy Green committed
    	unsigned char *p = (unsigned char *)sym + 32 + LWS_PRE, *start = p;
    	unsigned char *end = p + sizeof(sym) - 32 - LWS_PRE;
    
    #if !defined(WIN32) && LWS_POSIX && !defined(LWS_WITH_ESP32)
    
    Stephan Eberle's avatar
    Stephan Eberle committed
    	size_t len;
    
    Andy Green's avatar
    Andy Green committed
    	int n;
    
    Andy Green's avatar
    Andy Green committed
    
    
    Andy Green's avatar
    Andy Green committed
    	lws_snprintf(path, sizeof(path) - 1, "%s/%s", origin, uri);
    
    Andy Green's avatar
    Andy Green committed
    
    
    #if !defined(_WIN32_WCE) && !defined(LWS_WITH_ESP8266)
    
    
    	fflags |= lws_vfs_prepare_flags(wsi);
    
    
    Andy Green's avatar
    Andy Green committed
    	do {
    		spin++;
    
    Andy Green's avatar
    Andy Green committed
    		fops = lws_vfs_select_fops(wsi->context->fops, path, &vpath);
    
    		if (wsi->u.http.fop_fd)
    			lws_vfs_file_close(&wsi->u.http.fop_fd);
    
    		wsi->u.http.fop_fd = fops->LWS_FOP_OPEN(wsi->context->fops,
    							path, vpath, &fflags);
    		if (!wsi->u.http.fop_fd) {
    			lwsl_err("Unable to open '%s'\n", path);
    
    			return -1;
    		}
    
    Andy Green's avatar
    Andy Green committed
    		/* if it can't be statted, don't try */
    		if (fflags & LWS_FOP_FLAG_VIRTUAL)
    			break;
    
    #if defined(LWS_WITH_ESP32)
    		break;
    #endif
    
    Andy Green's avatar
    Andy Green committed
    #if !defined(WIN32)
    
    Andy Green's avatar
    Andy Green committed
    		if (fstat(wsi->u.http.fop_fd->fd, &st)) {
    
    Andy Green's avatar
    Andy Green committed
    			lwsl_info("unable to stat %s\n", path);
    
    Andy Green's avatar
    Andy Green committed
    			goto bail;
    		}
    
    #else
    #if defined(LWS_HAVE__STAT32I64)
    		if (_stat32i64(path, &st)) {
    			lwsl_info("unable to stat %s\n", path);
    			goto bail;
    		}
    
    Andy Green's avatar
    Andy Green committed
    #else
    		if (stat(path, &st)) {
    			lwsl_info("unable to stat %s\n", path);
    			goto bail;
    		}
    
    Andy Green's avatar
    Andy Green committed
    #endif
    
    Andy Green's avatar
    Andy Green committed
    		wsi->u.http.fop_fd->mod_time = (uint32_t)st.st_mtime;
    		fflags |= LWS_FOP_FLAG_MOD_TIME_VALID;
    
    
    #if !defined(WIN32) && LWS_POSIX && !defined(LWS_WITH_ESP32)
    
    Andy Green's avatar
    Andy Green committed
    		if ((S_IFMT & st.st_mode) == S_IFLNK) {
    
    			len = readlink(path, sym, sizeof(sym) - 1);
    			if (len) {
    
    Andy Green's avatar
    Andy Green committed
    				lwsl_err("Failed to read link %s\n", path);
    				goto bail;
    			}
    
    			sym[len] = '\0';
    
    Andy Green's avatar
    Andy Green committed
    			lwsl_debug("symlink %s -> %s\n", path, sym);
    
    Andy Green's avatar
    Andy Green committed
    			lws_snprintf(path, sizeof(path) - 1, "%s", sym);
    
    Andy Green's avatar
    Andy Green committed
    		}
    
    Andy Green's avatar
    Andy Green committed
    #endif
    
    Andy Green's avatar
    Andy Green committed
    		if ((S_IFMT & st.st_mode) == S_IFDIR) {
    			lwsl_debug("default filename append to dir\n");
    
    Andy Green's avatar
    Andy Green committed
    			lws_snprintf(path, sizeof(path) - 1, "%s/%s/index.html",
    
    Andy Green's avatar
    Andy Green committed
    				 origin, uri);
    		}
    
    	} while ((S_IFMT & st.st_mode) != S_IFREG && spin < 5);
    
    
    Andy Green's avatar
    Andy Green committed
    	if (spin == 5)
    
    Andy Green's avatar
    Andy Green committed
    		lwsl_err("symlink loop %s \n", path);
    
    Andy Green's avatar
    Andy Green committed
    
    
    Andy Green's avatar
    Andy Green committed
    	n = sprintf(sym, "%08llX%08lX",
    		    (unsigned long long)lws_vfs_get_length(wsi->u.http.fop_fd),
    
    Andy Green's avatar
    Andy Green committed
    		    (unsigned long)lws_vfs_get_mod_time(wsi->u.http.fop_fd));
    
    Andy Green's avatar
    Andy Green committed
    
    
    	/* disable ranges if IF_RANGE token invalid */
    
    	if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_IF_RANGE))
    		if (strcmp(sym, lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_IF_RANGE)))
    			/* differs - defeat Range: */
    			wsi->u.http.ah->frag_index[WSI_TOKEN_HTTP_RANGE] = 0;
    
    
    Andy Green's avatar
    Andy Green committed
    	if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_IF_NONE_MATCH)) {
    		/*
    		 * he thinks he has some version of it already,
    		 * check if the tag matches
    		 */
    
    Andy Green's avatar
    Andy Green committed
    		if (!strcmp(sym, lws_hdr_simple_ptr(wsi,
    					WSI_TOKEN_HTTP_IF_NONE_MATCH))) {
    
    Andy Green's avatar
    Andy Green committed
    
    
    Andy Green's avatar
    Andy Green committed
    			lwsl_debug("%s: ETAG match %s %s\n", __func__,
    				   uri, origin);
    
    Andy Green's avatar
    Andy Green committed
    
    			/* we don't need to send the payload */
    
    			if (lws_add_http_header_status(wsi,
    					HTTP_STATUS_NOT_MODIFIED, &p, end))
    
    Andy Green's avatar
    Andy Green committed
    				return -1;
    
    Andy Green's avatar
    Andy Green committed
    			if (lws_add_http_header_by_token(wsi,
    					WSI_TOKEN_HTTP_ETAG,
    					(unsigned char *)sym, n, &p, end))
    				return -1;
    
    Andy Green's avatar
    Andy Green committed
    			if (lws_finalize_http_header(wsi, &p, end))
    				return -1;
    
    			n = lws_write(wsi, start, p - start,
    
    Andy Green's avatar
    Andy Green committed
    				      LWS_WRITE_HTTP_HEADERS |
    				      LWS_WRITE_H2_STREAM_END);
    
    Andy Green's avatar
    Andy Green committed
    			if (n != (p - start)) {
    
    				lwsl_err("_write returned %d from %ld\n", n,
    					 (long)(p - start));
    
    Andy Green's avatar
    Andy Green committed
    				return -1;
    			}
    
    
    Andy Green's avatar
    Andy Green committed
    			lws_vfs_file_close(&wsi->u.http.fop_fd);
    
    
    Andy Green's avatar
    Andy Green committed
    			return lws_http_transaction_completed(wsi);
    		}
    
    Andy Green's avatar
    Andy Green committed
    	if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_ETAG,
    			(unsigned char *)sym, n, &p, end))
    		return -1;
    
    Andy Green's avatar
    Andy Green committed
    
    
    Andy Green's avatar
    Andy Green committed
    	mimetype = lws_get_mimetype(path, m);
    
    Andy Green's avatar
    Andy Green committed
    	if (!mimetype) {
    
    Andy Green's avatar
    Andy Green committed
    		lwsl_err("unknown mimetype for %s\n", path);
    
    Andy Green's avatar
    Andy Green committed
    	}
    
    	if (!mimetype[0])
    		lwsl_debug("sending no mimetype for %s\n", path);
    
    Andy Green's avatar
    Andy Green committed
    
    
    Andy Green's avatar
    Andy Green committed
    	wsi->sending_chunked = 0;
    
    	/*
    	 * check if this is in the list of file suffixes to be interpreted by
    	 * a protocol
    	 */
    	while (pvo) {
    		n = strlen(path);
    		if (n > (int)strlen(pvo->name) &&
    		    !strcmp(&path[n - strlen(pvo->name)], pvo->name)) {
    			wsi->sending_chunked = 1;
    
    			wsi->protocol_interpret_idx =
    					(char)(lws_intptr_t)pvo->value;
    
    Andy Green's avatar
    Andy Green committed
    			lwsl_info("want %s interpreted by %s\n", path,
    
    				    wsi->vhost->protocols[
    				         (int)(lws_intptr_t)(pvo->value)].name);
    			wsi->protocol = &wsi->vhost->protocols[
    			                       (int)(lws_intptr_t)(pvo->value)];
    
    Andy Green's avatar
    Andy Green committed
    			if (lws_ensure_user_space(wsi))
    				return -1;
    			break;
    		}
    		pvo = pvo->next;
    	}
    
    	if (m->protocol) {
    		const struct lws_protocols *pp = lws_vhost_name_to_protocol(
    
    						       wsi->vhost, m->protocol);
    
    Andy Green's avatar
    Andy Green committed
    		if (lws_bind_protocol(wsi, pp))
    			return 1;
    
    Andy Green's avatar
    Andy Green committed
    		args.p = (char *)p;
    		args.max_len = end - p;
    		if (pp->callback(wsi, LWS_CALLBACK_ADD_HEADERS,
    					  wsi->user_space, &args, 0))
    			return -1;
    		p = (unsigned char *)args.p;
    	}
    
    
    Andy Green's avatar
    Andy Green committed
    	n = lws_serve_http_file(wsi, path, mimetype, (char *)start, p - start);
    
    Andy Green's avatar
    Andy Green committed
    
    
    Andy Green's avatar
    Andy Green committed
    	if (n < 0 || ((n > 0) && lws_http_transaction_completed(wsi)))
    		return -1; /* error or can't reuse connection: close the socket */
    
    	return 0;
    
    Andy Green's avatar
    Andy Green committed
    bail:
    
    	return -1;
    
    Andy Green's avatar
    Andy Green committed
    }
    
    
    Andy Green's avatar
    Andy Green committed
    const struct lws_http_mount *
    lws_find_mount(struct lws *wsi, const char *uri_ptr, int uri_len)
    {
    	const struct lws_http_mount *hm, *hit = NULL;
    	int best = 0;
    
    	hm = wsi->vhost->mount_list;
    	while (hm) {
    		if (uri_len >= hm->mountpoint_len &&
    		    !strncmp(uri_ptr, hm->mountpoint, hm->mountpoint_len) &&
    		    (uri_ptr[hm->mountpoint_len] == '\0' ||
    		     uri_ptr[hm->mountpoint_len] == '/' ||
    		     hm->mountpoint_len == 1)
    		    ) {
    			if (hm->origin_protocol == LWSMPRO_CALLBACK ||
    			    ((hm->origin_protocol == LWSMPRO_CGI ||
    			     lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) ||
    
    Andy Green's avatar
    Andy Green committed
    			     (wsi->http2_substream &&
    
    				lws_hdr_total_length(wsi,
    						WSI_TOKEN_HTTP_COLON_PATH)) ||
    
    Andy Green's avatar
    Andy Green committed
    			     hm->protocol) &&
    			    hm->mountpoint_len > best)) {
    				best = hm->mountpoint_len;
    				hit = hm;
    			}
    		}
    		hm = hm->mount_next;
    	}
    
    	return hit;
    }
    
    
    Andy Green's avatar
    Andy Green committed
    
    static int
    lws_find_string_in_file(const char *filename, const char *string, int stringlen)
    {
    	char buf[128];
    	int fd, match = 0, pos = 0, n = 0, hit = 0;
    
    	fd = open(filename, O_RDONLY);
    	if (fd < 0) {
    		lwsl_err("can't open auth file: %s\n", filename);
    		return 1;
    	}
    
    	while (1) {
    		if (pos == n) {
    			n = read(fd, buf, sizeof(buf));
    			if (n <= 0) {
    				if (match == stringlen)
    					hit = 1;
    				break;
    			}
    			pos = 0;
    		}
    
    		if (match == stringlen) {
    			if (buf[pos] == '\r' || buf[pos] == '\n') {
    				hit = 1;
    				break;
    			}
    			match = 0;
    		}
    
    		if (buf[pos] == string[match])
    			match++;
    		else
    			match = 0;
    
    		pos++;
    	}
    
    	close(fd);
    
    	return hit;
    }
    
    static int
    lws_unauthorised_basic_auth(struct lws *wsi)
    {
    	struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
    	unsigned char *start = pt->serv_buf + LWS_PRE,
    		      *p = start, *end = p + 512;
    	char buf[64];
    	int n;
    
    	/* no auth... tell him it is required */
    
    	if (lws_add_http_header_status(wsi, HTTP_STATUS_UNAUTHORIZED, &p, end))
    		return -1;
    
    	n = lws_snprintf(buf, sizeof(buf), "Basic realm=\"lwsws\"");
    	if (lws_add_http_header_by_token(wsi,
    			WSI_TOKEN_HTTP_WWW_AUTHENTICATE,
    			(unsigned char *)buf, n, &p, end))
    		return -1;
    
    	if (lws_finalize_http_header(wsi, &p, end))
    		return -1;
    
    
    	n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS |
    					     LWS_WRITE_H2_STREAM_END);
    
    Andy Green's avatar
    Andy Green committed
    	if (n < 0)
    		return -1;
    
    	return lws_http_transaction_completed(wsi);
    
    }
    
    #endif
    
    
    Andy Green's avatar
    Andy Green committed
    int lws_clean_url(char *p)
    {
    
    	if (p[0] == 'h' && p[1] == 't' && p[2] == 't' && p[3] == 'p') {
    		p += 4;
    		if (*p == 's')
    		p++;
    		if (*p == ':') {
    			p++;
    			if (*p == '/')
    			p++;
    		}
    	}
    
    
    Andy Green's avatar
    Andy Green committed
    	while (*p) {
    		if (p[0] == '/' && p[1] == '/') {
    			char *p1 = p;
    			while (*p1) {
    				*p1 = p1[1];
    				p1++;
    			}
    			continue;
    		}
    		p++;
    	}
    
    	return 0;
    }
    
    
    static const unsigned char methods[] = {
    	WSI_TOKEN_GET_URI,
    	WSI_TOKEN_POST_URI,
    	WSI_TOKEN_OPTIONS_URI,
    	WSI_TOKEN_PUT_URI,
    	WSI_TOKEN_PATCH_URI,
    	WSI_TOKEN_DELETE_URI,
    	WSI_TOKEN_CONNECT,
    
    Andy Green's avatar
    Andy Green committed
    	WSI_TOKEN_HEAD_URI,
    
    #ifdef LWS_WITH_HTTP2
    
    	WSI_TOKEN_HTTP_COLON_PATH,
    #endif
    };
    
    static int
    lws_http_get_uri_and_method(struct lws *wsi, char **puri_ptr, int *puri_len)
    {
    	int n, count = 0;
    
    	for (n = 0; n < (int)ARRAY_SIZE(methods); n++)
    
    		if (lws_hdr_total_length(wsi, methods[n]))
    			count++;
    	if (!count) {
    
    		lwsl_warn("Missing URI in HTTP request\n");
    
    Andy Green's avatar
    Andy Green committed
    	if (count != 1 &&
    	    !(wsi->http2_substream &&
    	      lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COLON_PATH))) {
    
    		lwsl_warn("multiple methods?\n");
    
    	for (n = 0; n < (int)ARRAY_SIZE(methods); n++)
    
    		if (lws_hdr_total_length(wsi, methods[n])) {
    
    			*puri_ptr = lws_hdr_simple_ptr(wsi, methods[n]);
    			*puri_len = lws_hdr_total_length(wsi, methods[n]);
    			return n;
    
    	return -1;
    }
    
    int
    lws_http_action(struct lws *wsi)
    {
    	struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
    	enum http_connection_type connection_type;
    	enum http_version request_version;
    	char content_length_str[32];
    	struct lws_process_html_args args;
    	const struct lws_http_mount *hit = NULL;
    	unsigned int n;
    	char http_version_str[10];
    	char http_conn_str[20];
    	int http_version_len;
    	char *uri_ptr = NULL, *s;
    	int uri_len = 0, meth;
    	static const char * const oprot[] = {
    		"http://", "https://"
    	};
    
    	meth = lws_http_get_uri_and_method(wsi, &uri_ptr, &uri_len);
    
    	if (meth < 0 || meth >= (int)ARRAY_SIZE(method_names))
    
    		goto bail_nuke_ah;
    
    	/* we insist on absolute paths */
    
    
    	if (!uri_ptr || uri_ptr[0] != '/') {
    
    		lws_return_http_status(wsi, HTTP_STATUS_FORBIDDEN, NULL);
    
    		goto bail_nuke_ah;
    	}
    
    
    	lwsl_info("Method: '%s' (%d), request for '%s'\n", method_names[meth],
    		  meth, uri_ptr);
    
    
    	if (lws_ensure_user_space(wsi))
    		goto bail_nuke_ah;
    
    
    	/* HTTP header had a content length? */
    
    
    Andy Green's avatar
    Andy Green committed
    	wsi->u.http.rx_content_length = 0;
    
    	if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI) ||
    		lws_hdr_total_length(wsi, WSI_TOKEN_PATCH_URI) ||
    		lws_hdr_total_length(wsi, WSI_TOKEN_PUT_URI))
    
    Andy Green's avatar
    Andy Green committed
    		wsi->u.http.rx_content_length = 100 * 1024 * 1024;
    
    
    	if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
    		lws_hdr_copy(wsi, content_length_str,
    
    			     sizeof(content_length_str) - 1,
    			     WSI_TOKEN_HTTP_CONTENT_LENGTH);
    
    Andy Green's avatar
    Andy Green committed
    		wsi->u.http.rx_content_length = atoll(content_length_str);
    
    Andy Green's avatar
    Andy Green committed
    	if (wsi->http2_substream) {
    		wsi->u.http.request_version = HTTP_VERSION_2;
    	} else {
    		/* http_version? Default to 1.0, override with token: */
    		request_version = HTTP_VERSION_1_0;
    
    		/* Works for single digit HTTP versions. : */
    		http_version_len = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP);
    		if (http_version_len > 7) {
    			lws_hdr_copy(wsi, http_version_str,
    
    				     sizeof(http_version_str) - 1,
    				     WSI_TOKEN_HTTP);
    			if (http_version_str[5] == '1' &&
    			    http_version_str[7] == '1')
    
    Andy Green's avatar
    Andy Green committed
    				request_version = HTTP_VERSION_1_1;
    		}
    		wsi->u.http.request_version = request_version;
    
    Andy Green's avatar
    Andy Green committed
    		/* HTTP/1.1 defaults to "keep-alive", 1.0 to "close" */
    		if (request_version == HTTP_VERSION_1_1)
    
    			connection_type = HTTP_CONNECTION_KEEP_ALIVE;
    		else
    
    Andy Green's avatar
    Andy Green committed
    			connection_type = HTTP_CONNECTION_CLOSE;
    
    		/* Override default if http "Connection:" header: */
    		if (lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) {
    
    			lws_hdr_copy(wsi, http_conn_str,
    				     sizeof(http_conn_str) - 1,
    
    Andy Green's avatar
    Andy Green committed
    				     WSI_TOKEN_CONNECTION);
    			http_conn_str[sizeof(http_conn_str) - 1] = '\0';
    			if (!strcasecmp(http_conn_str, "keep-alive"))
    				connection_type = HTTP_CONNECTION_KEEP_ALIVE;
    			else
    				if (!strcasecmp(http_conn_str, "close"))
    					connection_type = HTTP_CONNECTION_CLOSE;
    		}
    		wsi->u.http.connection_type = connection_type;
    
    	n = wsi->protocol->callback(wsi, LWS_CALLBACK_FILTER_HTTP_CONNECTION,
    
    				    wsi->user_space, uri_ptr, uri_len);
    
    Andy Green's avatar
    Andy Green committed
    	if (n) {
    		lwsl_info("LWS_CALLBACK_HTTP closing\n");
    
    Andy Green's avatar
    Andy Green committed
    		return 1;
    
    Andy Green's avatar
    Andy Green committed
    	 * if there is content supposed to be coming,
    	 * put a timeout on it having arrived
    
    Andy Green's avatar
    Andy Green committed
    	lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
    
    Andy Green's avatar
    Andy Green committed
    			wsi->context->timeout_secs);
    
    #ifdef LWS_OPENSSL_SUPPORT
    
    	if (wsi->redirect_to_https) {
    		/*
    
    		 * we accepted http:// only so we could redirect to
    
    		 * https://, so issue the redirect.  Create the redirection
    		 * URI from the host: header and ignore the path part
    		 */
    		unsigned char *start = pt->serv_buf + LWS_PRE, *p = start,
    			      *end = p + 512;
    
    		if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST))
    			goto bail_nuke_ah;
    
    Andy Green's avatar
    Andy Green committed
    		n = sprintf((char *)end, "https://%s/",
    
    			    lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST));
    
    Andy Green's avatar
    Andy Green committed
    		n = lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY,
    				      end, n, &p, end);
    
    			goto bail_nuke_ah;
    
    		return lws_http_transaction_completed(wsi);
    	}
    
    Andy Green's avatar
    Andy Green committed
    #endif
    
    #ifdef LWS_WITH_ACCESS_LOG
    
    	lws_prepare_access_log_info(wsi, uri_ptr, meth);
    
    Andy Green's avatar
    Andy Green committed
    	/* can we serve it from the mount list? */
    
    
    Andy Green's avatar
    Andy Green committed
    	hit = lws_find_mount(wsi, uri_ptr, uri_len);
    	if (!hit) {
    		/* deferred cleanup and reset to protocols[0] */
    
    Andy Green's avatar
    Andy Green committed
    		lwsl_info("no hit\n");
    
    Andy Green's avatar
    Andy Green committed
    		if (lws_bind_protocol(wsi, &wsi->vhost->protocols[0]))
    			return 1;
    
    Andy Green's avatar
    Andy Green committed
    		n = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
    				    wsi->user_space, uri_ptr, uri_len);
    
    Andy Green's avatar
    Andy Green committed
    		goto after;
    	}
    
    Andy Green's avatar
    Andy Green committed
    	s = uri_ptr + hit->mountpoint_len;
    
    Andy Green's avatar
    Andy Green committed
    	/*
    	 * if we have a mountpoint like https://xxx.com/yyy
    	 * there is an implied / at the end for our purposes since
    	 * we can only mount on a "directory".
    	 *
    	 * But if we just go with that, the browser cannot understand
    	 * that he is actually looking down one "directory level", so
    	 * even though we give him /yyy/abc.html he acts like the
    	 * current directory level is /.  So relative urls like "x.png"
    	 * wrongly look outside the mountpoint.
    	 *
    	 * Therefore if we didn't come in on a url with an explicit
    	 * / at the end, we must redirect to add it so the browser
    	 * understands he is one "directory level" down.
    	 */
    	if ((hit->mountpoint_len > 1 ||
    	     (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
    	      hit->origin_protocol == LWSMPRO_REDIR_HTTPS)) &&
    	    (*s != '/' ||
    	     (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
    	      hit->origin_protocol == LWSMPRO_REDIR_HTTPS)) &&
    	    (hit->origin_protocol != LWSMPRO_CGI &&
    
    Andy Green's avatar
    Andy Green committed
    	     hit->origin_protocol != LWSMPRO_CALLBACK)) {
    
    Andy Green's avatar
    Andy Green committed
    		unsigned char *start = pt->serv_buf + LWS_PRE,
    			      *p = start, *end = p + 512;
    
    		lwsl_debug("Doing 301 '%s' org %s\n", s, hit->origin);
    
    		/* > at start indicates deal with by redirect */
    		if (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
    		    hit->origin_protocol == LWSMPRO_REDIR_HTTPS)
    
    Andy Green's avatar
    Andy Green committed
    			n = lws_snprintf((char *)end, 256, "%s%s",
    
    Andy Green's avatar
    Andy Green committed
    				    oprot[hit->origin_protocol & 1],
    				    hit->origin);
    
    Andy Green's avatar
    Andy Green committed
    		else {
    			if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST)) {
    
    				if (!lws_hdr_total_length(wsi,
    						WSI_TOKEN_HTTP_COLON_AUTHORITY))
    
    Andy Green's avatar
    Andy Green committed
    					goto bail_nuke_ah;
    				n = lws_snprintf((char *)end, 256,
    				    "%s%s%s/", oprot[!!lws_is_ssl(wsi)],
    
    				    lws_hdr_simple_ptr(wsi,
    						WSI_TOKEN_HTTP_COLON_AUTHORITY),
    
    Andy Green's avatar
    Andy Green committed
    				    uri_ptr);
    			} else
    				n = lws_snprintf((char *)end, 256,
    				    "%s%s%s/", oprot[!!lws_is_ssl(wsi)],
    				    lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST),
    				    uri_ptr);
    		}
    
    Andy Green's avatar
    Andy Green committed
    		lws_clean_url((char *)end);
    
    Andy Green's avatar
    Andy Green committed
    		n = lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY,
    				      end, n, &p, end);
    		if ((int)n < 0)
    			goto bail_nuke_ah;
    
    Andy Green's avatar
    Andy Green committed
    		return lws_http_transaction_completed(wsi);
    	}
    
    
    Andy Green's avatar
    Andy Green committed
    	/* basic auth? */
    
    	if (hit->basic_auth_login_file) {
    		char b64[160], plain[(sizeof(b64) * 3) / 4];
    		int m;
    
    		/* Did he send auth? */
    		if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_AUTHORIZATION))