From 3449db9022456dc5654316b5775d6e588bbfdadb Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Sun, 11 Dec 2016 15:28:07 +0100 Subject: [PATCH] Moved skip_quoted to own file --- Makefile | 1 + src/httplib_skip_quoted.c | 79 +++++++++++++++++++++++++++++++++++++++ src/libhttp.c | 51 ------------------------- 3 files changed, 80 insertions(+), 51 deletions(-) create mode 100644 src/httplib_skip_quoted.c diff --git a/Makefile b/Makefile index 65a14aec..50906f5b 100644 --- a/Makefile +++ b/Makefile @@ -183,6 +183,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_set_uid_option.c \ src/httplib_set_user_connection_data.c \ src/httplib_set_websocket_handler.c \ + src/httplib_skip_quoted.c \ src/httplib_snprintf.c \ src/httplib_sockaddr_to_string.c \ src/httplib_spawn_process.c \ diff --git a/src/httplib_skip_quoted.c b/src/httplib_skip_quoted.c new file mode 100644 index 00000000..d2c2722b --- /dev/null +++ b/src/httplib_skip_quoted.c @@ -0,0 +1,79 @@ +/* + * 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" + + + +/* Skip the characters until one of the delimiters characters found. + * 0-terminate resulting word. Skip the delimiter and following whitespaces. + * Advance pointer to buffer to the next word. Return found 0-terminated word. + * Delimiters can be quoted with quotechar. */ +char * XX_httplib_skip_quoted( char **buf, const char *delimiters, const char *whitespace, char quotechar ) { + + char *p; + char *begin_word; + char *end_word; + char *end_whitespace; + + begin_word = *buf; + end_word = begin_word + strcspn(begin_word, delimiters); + + /* Check for quotechar */ + if (end_word > begin_word) { + p = end_word - 1; + while (*p == quotechar) { + /* While the delimiter is quoted, look for the next delimiter. */ + /* This happens, e.g., in calls from XX_httplib_parse_auth_header, + * if the user name contains a " character. */ + + /* If there is anything beyond end_word, copy it. */ + if (*end_word != '\0') { + size_t end_off = strcspn(end_word + 1, delimiters); + memmove(p, end_word, end_off + 1); + p += end_off; /* p must correspond to end_word - 1 */ + end_word += end_off + 1; + } else { + *p = '\0'; + break; + } + } + for (p++; p < end_word; p++) *p = '\0'; + } + + if (*end_word == '\0') { + *buf = end_word; + } else { + end_whitespace = end_word + 1 + strspn(end_word + 1, whitespace); + + for (p = end_word; p < end_whitespace; p++) *p = '\0'; + + *buf = end_whitespace; + } + + return begin_word; + +} /* XX_httplib_skip_quoted */ diff --git a/src/libhttp.c b/src/libhttp.c index 944c133a..15455f4b 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -828,57 +828,6 @@ void XX_httplib_set_thread_name(const char *threadName) { -/* Skip the characters until one of the delimiters characters found. - * 0-terminate resulting word. Skip the delimiter and following whitespaces. - * Advance pointer to buffer to the next word. Return found 0-terminated word. - * Delimiters can be quoted with quotechar. */ -char * XX_httplib_skip_quoted( char **buf, const char *delimiters, const char *whitespace, char quotechar ) { - - char *p; - char *begin_word; - char *end_word; - char *end_whitespace; - - begin_word = *buf; - end_word = begin_word + strcspn(begin_word, delimiters); - - /* Check for quotechar */ - if (end_word > begin_word) { - p = end_word - 1; - while (*p == quotechar) { - /* While the delimiter is quoted, look for the next delimiter. */ - /* This happens, e.g., in calls from XX_httplib_parse_auth_header, - * if the user name contains a " character. */ - - /* If there is anything beyond end_word, copy it. */ - if (*end_word != '\0') { - size_t end_off = strcspn(end_word + 1, delimiters); - memmove(p, end_word, end_off + 1); - p += end_off; /* p must correspond to end_word - 1 */ - end_word += end_off + 1; - } else { - *p = '\0'; - break; - } - } - for (p++; p < end_word; p++) *p = '\0'; - } - - if (*end_word == '\0') { - *buf = end_word; - } else { - end_whitespace = end_word + 1 + strspn(end_word + 1, whitespace); - - for (p = end_word; p < end_whitespace; p++) *p = '\0'; - - *buf = end_whitespace; - } - - return begin_word; - -} /* XX_httplib_skip_quoted */ - - /* Simplified version of XX_httplib_skip_quoted without quote char * and whitespace == delimiters */ char *XX_httplib_skip( char **buf, const char *delimiters ) {