Mercurial > blahgd > fmt4
changeset 1030:bd519cc8058c v4.4-rc1
use libjeffpc's qstring parsing code
Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author | Josef 'Jeff' Sipek <jeffpc@josefsipek.net> |
---|---|
date | Mon, 31 Jul 2017 21:24:27 +0300 |
parents | da15e93da693 |
children | 1fca58c24122 |
files | .hgignore CMakeLists.txt comment.c qstring.c qstring.h req.c test/CMakeLists.txt test/qstring-basic/CMakeLists.txt test/qstring-basic/empty.qs test/qstring-basic/multi-char.qs test/qstring-basic/no-value-long.qs test/qstring-basic/no-value.qs test/qstring-basic/one.qs test/qstring-basic/two.qs test_qstring.c |
diffstat | 14 files changed, 5 insertions(+), 310 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgignore Mon Jul 31 19:45:04 2017 +0300 +++ b/.hgignore Mon Jul 31 21:24:27 2017 +0300 @@ -5,7 +5,6 @@ mathd lisplint test_fmt3 -test_qstring test_lisp_parser hgversion.h
--- a/CMakeLists.txt Mon Jul 31 19:45:04 2017 +0300 +++ b/CMakeLists.txt Mon Jul 31 21:24:27 2017 +0300 @@ -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}"
--- a/comment.c Mon Jul 31 19:45:04 2017 +0300 +++ b/comment.c Mon Jul 31 21:24:27 2017 +0300 @@ -36,6 +36,7 @@ #include <jeffpc/sexpr.h> #include <jeffpc/str.h> #include <jeffpc/io.h> +#include <jeffpc/qstring.h> #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;
--- a/qstring.c Mon Jul 31 19:45:04 2017 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,150 +0,0 @@ -/* - * Copyright (c) 2014-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> - * - * 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 <jeffpc/error.h> -#include <jeffpc/urldecode.h> - -#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; -}
--- a/qstring.h Mon Jul 31 19:45:04 2017 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2014-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> - * - * 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 <string.h> - -#include <jeffpc/nvl.h> - -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
--- a/req.c Mon Jul 31 19:45:04 2017 +0300 +++ b/req.c Mon Jul 31 21:24:27 2017 +0300 @@ -27,12 +27,12 @@ #include <jeffpc/atomic.h> #include <jeffpc/mem.h> +#include <jeffpc/qstring.h> #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 */ }
--- a/test/CMakeLists.txt Mon Jul 31 19:45:04 2017 +0300 +++ b/test/CMakeLists.txt Mon Jul 31 21:24:27 2017 +0300 @@ -1,5 +1,5 @@ # -# Copyright (c) 2013-2016 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> +# Copyright (c) 2013-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> # # 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)
--- a/test/qstring-basic/CMakeLists.txt Mon Jul 31 19:45:04 2017 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,26 +0,0 @@ -# -# Copyright (c) 2014-2015 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> -# -# 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()
--- a/test/qstring-basic/multi-char.qs Mon Jul 31 19:45:04 2017 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -preview=1234&p=5 \ No newline at end of file
--- a/test/qstring-basic/no-value-long.qs Mon Jul 31 19:45:04 2017 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -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
--- a/test/qstring-basic/no-value.qs Mon Jul 31 19:45:04 2017 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -abc \ No newline at end of file
--- a/test/qstring-basic/one.qs Mon Jul 31 19:45:04 2017 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -a=b \ No newline at end of file
--- a/test/qstring-basic/two.qs Mon Jul 31 19:45:04 2017 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -a=b&c=d \ No newline at end of file
--- a/test_qstring.c Mon Jul 31 19:45:04 2017 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2014-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> - * - * 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 <stdlib.h> - -#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; -}