From 10992593b29022241cbacfc28b21f20c7dfbde42 Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Sat, 10 Dec 2016 15:45:56 +0100 Subject: [PATCH] Moved is_websocket_protocol to own file --- Makefile | 1 + src/httplib_is_websocket_protocol.c | 75 +++++++++++++++++++++++++++++ src/libhttp-private.h | 1 + src/libhttp.c | 48 ++---------------- 4 files changed, 82 insertions(+), 43 deletions(-) create mode 100644 src/httplib_is_websocket_protocol.c diff --git a/Makefile b/Makefile index a59220ee..44f8ddca 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_handle_form_request.c \ src/httplib_handle_request.c \ src/httplib_initialize_ssl.c \ + src/httplib_is_websocket_protocol.c \ src/httplib_load_dll.c \ src/httplib_log_access.c \ src/httplib_master_thread.c \ diff --git a/src/httplib_is_websocket_protocol.c b/src/httplib_is_websocket_protocol.c new file mode 100644 index 00000000..718105e5 --- /dev/null +++ b/src/httplib_is_websocket_protocol.c @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2016 Lammert Bies + * Copyright (c) 2013-2016 the Civetweb developers + * Copyright (c) 2004-2013 Sergey Lyubka + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + + +#include "libhttp-private.h" + + + +/* + * int XX_httplib_is_websocket_protocol( const struct mg_connection *conn ); + * + * The function XX_httplib_is_websocket_protocol() checks the request headers + * to see if the connection is a valid websocket protocol. + */ + +int XX_httplib_is_websocket_protocol( const struct mg_connection *conn ) { + +#if defined(USE_WEBSOCKET) + const char *upgrade; + const char *connection; + + /* A websocket protocoll has the following HTTP headers: + * + * Connection: Upgrade + * Upgrade: Websocket + */ + + upgrade = mg_get_header(conn, "Upgrade"); + if (upgrade == NULL) return 0; /* fail early, don't waste time checking other header * fields */ + + if (!XX_httplib_strcasestr(upgrade, "websocket")) return 0; + + connection = mg_get_header(conn, "Connection"); + if (connection == NULL) return 0; + + if (!XX_httplib_strcasestr(connection, "upgrade")) return 0; + + /* The headers "Host", "Sec-WebSocket-Key", "Sec-WebSocket-Protocol" and + * "Sec-WebSocket-Version" are also required. + * Don't check them here, since even an unsupported websocket protocol + * request still IS a websocket request (in contrast to a standard HTTP + * request). It will fail later in handle_websocket_request. + */ + + return 1; + +#else /* defined(USE_WEBSOCKET) */ + + return 0; + +#endif /* defined(USE_WEBSOCKET) */ + +} /* XX_httplib_is_websocket_protocol */ diff --git a/src/libhttp-private.h b/src/libhttp-private.h index 034e8096..40f1854d 100644 --- a/src/libhttp-private.h +++ b/src/libhttp-private.h @@ -922,6 +922,7 @@ void XX_httplib_ssl_locking_callback( int mode, int mutex_num, const char *fil int XX_httplib_ssl_use_pem_file( struct mg_context *ctx, const char *pem ); int XX_httplib_sslize( struct mg_connection *conn, SSL_CTX *s, int (*func)(SSL *) ); int XX_httplib_stat( struct mg_connection *conn, const char *path, struct file *filep ); +const char * XX_httplib_strcasestr( const char *big_str, const char *small_str ); char * XX_httplib_strdup( const char *str ); void XX_httplib_strlcpy( register char *dst, register const char *src, size_t n ); int XX_httplib_substitute_index_file( struct mg_connection *conn, char *path, size_t path_len, struct file *filep ); diff --git a/src/libhttp.c b/src/libhttp.c index 54ef3e90..ea8e2725 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -981,7 +981,7 @@ char * XX_httplib_strdup( const char *str ) { } -static const char * mg_strcasestr(const char *big_str, const char *small_str) { +const char * XX_httplib_strcasestr( const char *big_str, const char *small_str ) { size_t i; size_t big_len = strlen(big_str); @@ -994,7 +994,9 @@ static const char * mg_strcasestr(const char *big_str, const char *small_str) { } return NULL; -} + +} /* XX_httplib_strcasestr */ + /* Return null terminated string of given maximum length. @@ -3279,7 +3281,7 @@ int mg_get_cookie(const char *cookie_header, const char *var_name, char *dst, si name_len = (int)strlen(var_name); end = s + strlen(s); - for (; (s = mg_strcasestr(s, var_name)) != NULL; s += name_len) { + for (; (s = XX_httplib_strcasestr(s, var_name)) != NULL; s += name_len) { if (s[name_len] == '=') { /* HCP24: now check is it a substring or a full cookie name */ if ((s == cookie_header) || (s[-1] == ' ')) { @@ -7143,43 +7145,3 @@ void XX_httplib_handle_websocket_request( struct mg_connection *conn, const char } /* XX_httplib_handle_websocket_request */ #endif /* !USE_WEBSOCKET */ - - -int XX_httplib_is_websocket_protocol( const struct mg_connection *conn ) { - -#if defined(USE_WEBSOCKET) - const char *upgrade; - const char *connection; - - /* A websocket protocoll has the following HTTP headers: - * - * Connection: Upgrade - * Upgrade: Websocket - */ - - upgrade = mg_get_header(conn, "Upgrade"); - if (upgrade == NULL) return 0; /* fail early, don't waste time checking other header * fields */ - - if (!mg_strcasestr(upgrade, "websocket")) return 0; - - connection = mg_get_header(conn, "Connection"); - if (connection == NULL) return 0; - - if (!mg_strcasestr(connection, "upgrade")) return 0; - - /* The headers "Host", "Sec-WebSocket-Key", "Sec-WebSocket-Protocol" and - * "Sec-WebSocket-Version" are also required. - * Don't check them here, since even an unsupported websocket protocol - * request still IS a websocket request (in contrast to a standard HTTP - * request). It will fail later in handle_websocket_request. - */ - - return 1; - -#else /* defined(USE_WEBSOCKET) */ - - return 0; - -#endif /* defined(USE_WEBSOCKET) */ - -} /* XX_httplib_is_websocket_protocol */