use libjeffpc's qstring parsing code

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
15 files changed, 5 insertions(+), 310 deletions(-)

M .hgignore
M CMakeLists.txt
M comment.c
R qstring.c => 
R qstring.h => 
M req.c
M test/CMakeLists.txt
R test/qstring-basic/CMakeLists.txt => 
R test/qstring-basic/empty.qs => 
R test/qstring-basic/multi-char.qs => 
R test/qstring-basic/no-value-long.qs => 
R test/qstring-basic/no-value.qs => 
R test/qstring-basic/one.qs => 
R test/qstring-basic/two.qs => 
R test_qstring.c => 
M .hgignore +0 -1
@@ 5,7 5,6 @@ blahgd
 mathd
 lisplint
 test_fmt3
-test_qstring
 test_lisp_parser
 
 hgversion.h

          
M CMakeLists.txt +0 -9
@@ 90,7 90,6 @@ add_library(blahg
 
 	# nvlist related things
 	nvl.c
-	qstring.c
 	vars.c
 
 	# misc

          
@@ 156,14 155,6 @@ target_link_libraries(test_fmt3
 	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}"

          
M comment.c +2 -2
@@ 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 @@ static const char *save_comment(struct r
 
 	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;

          
R qstring.c =>  +0 -150
@@ 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;
-}

          
R qstring.h =>  +0 -45
@@ 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

          
M req.c +2 -2
@@ 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 @@ int req_dispatch(struct req *req)
 	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 */
 	}

          
M test/CMakeLists.txt +1 -2
@@ 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)

          
R test/qstring-basic/CMakeLists.txt =>  +0 -26
@@ 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()

          
R test/qstring-basic/empty.qs =>  +0 -0

        
R test/qstring-basic/multi-char.qs =>  +0 -1
@@ 1,1 0,0 @@ 
-preview=1234&p=5
  No newline at end of file

          
R test/qstring-basic/no-value-long.qs =>  +0 -1
@@ 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

          
R test/qstring-basic/no-value.qs =>  +0 -1
@@ 1,1 0,0 @@ 
-abc
  No newline at end of file

          
R test/qstring-basic/one.qs =>  +0 -1
@@ 1,1 0,0 @@ 
-a=b
  No newline at end of file

          
R test/qstring-basic/two.qs =>  +0 -1
@@ 1,1 0,0 @@ 
-a=b&c=d
  No newline at end of file

          
R test_qstring.c =>  +0 -68
@@ 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;
-}