# HG changeset patch # User Josef 'Jeff' Sipek # Date 1501525467 -10800 # Mon Jul 31 21:24:27 2017 +0300 # Node ID bd519cc8058c6c15ce882f5c265d55b474638964 # Parent da15e93da693989ad15e1aa4e831c2f09257109d use libjeffpc's qstring parsing code Signed-off-by: Josef 'Jeff' Sipek diff --git a/.hgignore b/.hgignore --- a/.hgignore +++ b/.hgignore @@ -5,7 +5,6 @@ mathd lisplint test_fmt3 -test_qstring test_lisp_parser hgversion.h diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -90,7 +90,6 @@ # nvlist related things nvl.c - qstring.c vars.c # misc @@ -156,14 +155,6 @@ blahg ) -add_executable(test_qstring - test_qstring.c -) - -target_link_libraries(test_qstring - blahg -) - function(simple_c_test type section bin data) add_test(NAME "${type}:${section}:${data}" COMMAND "${CMAKE_BINARY_DIR}/test_${bin}" diff --git a/comment.c b/comment.c --- a/comment.c +++ b/comment.c @@ -36,6 +36,7 @@ #include #include #include +#include #include "req.h" #include "sidebar.h" @@ -43,7 +44,6 @@ #include "utils.h" #include "config.h" #include "post.h" -#include "qstring.h" #include "debug.h" #define INTERNAL_ERR "Ouch! Encountered an internal error. " \ @@ -444,7 +444,7 @@ err = GENERIC_ERR_STR; - ret = parse_query_string(qs, req->scgi->request.body); + ret = qstring_parse(qs, req->scgi->request.body); if (ret) { DBG("failed to parse comment: %s", xstrerror(ret)); goto err; diff --git a/qstring.c b/qstring.c deleted file mode 100644 --- a/qstring.c +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright (c) 2014-2017 Josef 'Jeff' Sipek - * - * 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 -#include - -#include "vars.h" -#include "qstring.h" -#include "utils.h" - -enum { - QS_STATE_ERROR = 0, - QS_STATE_NAME, - QS_STATE_VAL, -}; - -struct qstr { - size_t len; - size_t idx; - char buf[0]; -}; - -static struct qstr *alloc_str(size_t size) -{ - size_t allocsize = size * 3; /* FIXME: is 3x good enough? */ - struct qstr *tmp; - - tmp = malloc(sizeof(struct qstr) + allocsize); - if (!tmp) - return NULL; - - memset(tmp, 0, allocsize); - - tmp->len = allocsize; - tmp->idx = 0; - - return tmp; -} - -static void free_str(struct qstr *str) -{ - free(str); -} - -static int append_char_str(struct qstr *str, char c) -{ - if ((str->idx + 1) == str->len) - return -E2BIG; - - str->buf[str->idx++] = c; - - return 0; -} - -static void reset_str(struct qstr *str) -{ - str->idx = 0; -} - -static void insert(struct nvlist *vars, struct qstr *name, struct qstr *val) -{ - ssize_t outlen; - - outlen = urldecode(name->buf, name->idx, name->buf); - VERIFY3S(outlen, >=, 0); - name->buf[outlen] = '\0'; - - urldecode(val->buf, val->idx, val->buf); - VERIFY3S(outlen, >=, 0); - val->buf[outlen] = '\0'; - - /* now, {name,val}->buf are null-terminated strings */ - - VERIFY0(nvl_set_str(vars, name->buf, STR_DUP(val->buf))); -} - -int parse_query_string_len(struct nvlist *vars, const char *qs, size_t len) -{ - struct qstr *name, *val; - const char *cur, *end; - int state; - int ret; - - if (!qs || !len) - return 0; - - name = alloc_str(VAR_NAME_MAXLEN); - val = alloc_str(64 * 1024); - - if (!name || !val) { - ret = -ENOMEM; - goto out; - } - - state = QS_STATE_NAME; - - end = qs + len; - cur = qs; - - for (; end > cur; cur++) { - char c = *cur; - - if (state == QS_STATE_NAME) { - if (c == '=') - state = QS_STATE_VAL; - else if ((ret = append_char_str(name, c)) != 0) - goto out; - } else if (state == QS_STATE_VAL) { - if (c == '&') { - insert(vars, name, val); - - state = QS_STATE_NAME; - reset_str(name); - reset_str(val); - } else if ((ret = append_char_str(val, c)) != 0) { - goto out; - } - } - } - - if (state == QS_STATE_VAL) - insert(vars, name, val); - - ret = 0; - -out: - free_str(name); - free_str(val); - - return ret; -} diff --git a/qstring.h b/qstring.h deleted file mode 100644 --- a/qstring.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2014-2017 Josef 'Jeff' Sipek - * - * 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. - */ - -#ifndef __QSTRING_H -#define __QSTRING_H - -#include - -#include - -extern int parse_query_string_len(struct nvlist *vars, const char *qs, - size_t len); - -static inline int parse_query_string(struct nvlist *vars, const char *qs) -{ - size_t len; - - if (qs) - len = strlen(qs); - else - len = 0; - - return parse_query_string_len(vars, qs, len); -} - -#endif diff --git a/req.c b/req.c --- a/req.c +++ b/req.c @@ -27,12 +27,12 @@ #include #include +#include #include "req.h" #include "utils.h" #include "sidebar.h" #include "render.h" -#include "qstring.h" #include "static.h" #include "debug.h" #include "version.h" @@ -389,7 +389,7 @@ if (IS_ERR(qs)) return R404(req, NULL); /* FIXME: this should be R400 */ - if (parse_query_string(req->request_qs, str_cstr(qs))) { + if (qstring_parse(req->request_qs, str_cstr(qs))) { str_putref(qs); return R404(req, NULL); /* FIXME: this should be R400 */ } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright (c) 2013-2016 Josef 'Jeff' Sipek +# Copyright (c) 2013-2017 Josef 'Jeff' Sipek # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -22,4 +22,3 @@ add_subdirectory(fmt3-commands) add_subdirectory(fmt3-bugs) -add_subdirectory(qstring-basic) diff --git a/test/qstring-basic/CMakeLists.txt b/test/qstring-basic/CMakeLists.txt deleted file mode 100644 --- a/test/qstring-basic/CMakeLists.txt +++ /dev/null @@ -1,26 +0,0 @@ -# -# Copyright (c) 2014-2015 Josef 'Jeff' Sipek -# -# 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. -# - -file(GLOB TESTS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.qs) -foreach(TEST ${TESTS}) - simple_c_test(qstring basic qstring ${TEST}) -endforeach() diff --git a/test/qstring-basic/empty.qs b/test/qstring-basic/empty.qs deleted file mode 100644 diff --git a/test/qstring-basic/multi-char.qs b/test/qstring-basic/multi-char.qs deleted file mode 100644 --- a/test/qstring-basic/multi-char.qs +++ /dev/null @@ -1,1 +0,0 @@ -preview=1234&p=5 \ No newline at end of file diff --git a/test/qstring-basic/no-value-long.qs b/test/qstring-basic/no-value-long.qs deleted file mode 100644 --- a/test/qstring-basic/no-value-long.qs +++ /dev/null @@ -1,1 +0,0 @@ -a-very-long-string---longer-than-the-max-variable-name-length-times-three-because-we-have-to-account-for-percent-deflation \ No newline at end of file diff --git a/test/qstring-basic/no-value.qs b/test/qstring-basic/no-value.qs deleted file mode 100644 --- a/test/qstring-basic/no-value.qs +++ /dev/null @@ -1,1 +0,0 @@ -abc \ No newline at end of file diff --git a/test/qstring-basic/one.qs b/test/qstring-basic/one.qs deleted file mode 100644 --- a/test/qstring-basic/one.qs +++ /dev/null @@ -1,1 +0,0 @@ -a=b \ No newline at end of file diff --git a/test/qstring-basic/two.qs b/test/qstring-basic/two.qs deleted file mode 100644 --- a/test/qstring-basic/two.qs +++ /dev/null @@ -1,1 +0,0 @@ -a=b&c=d \ No newline at end of file diff --git a/test_qstring.c b/test_qstring.c deleted file mode 100644 --- a/test_qstring.c +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2014-2017 Josef 'Jeff' Sipek - * - * 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 - -#include "utils.h" -#include "vars.h" -#include "qstring.h" - -static int onefile(char *ibuf, size_t len) -{ - struct nvlist *vars; - - vars = nvl_alloc(); - if (!vars) - return -ENOMEM; - - parse_query_string_len(vars, ibuf, len); - - nvl_dump_file(stderr, vars); - - nvl_putref(vars); - - return 0; -} - -int main(int argc, char **argv) -{ - char *in; - int i; - int result; - - result = 0; - - ASSERT0(putenv("UMEM_DEBUG=default,verbose")); - ASSERT0(putenv("BLAHG_DISABLE_SYSLOG=1")); - - for (i = 1; i < argc; i++) { - in = read_file(argv[i]); - ASSERT(!IS_ERR(in)); - - if (onefile(in, strlen(in))) - result = 1; - - free(in); - } - - return result; -}