diff options
| author | Joel Klinghed <the_jk@yahoo.com> | 2015-06-03 00:07:58 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@yahoo.com> | 2015-06-03 00:07:58 +0200 |
| commit | 0cbc7dd8dd00c92570f35f901b820a1ea96acdf0 (patch) | |
| tree | 7591b3cae8191a4bf4ec4092edb252c874792445 | |
| parent | 5bc49de682eec79f3dc8096c6812bee70fe2d496 (diff) | |
Add space separated argument parser with quoting
| -rw-r--r-- | src/Makefile.am | 1 | ||||
| -rw-r--r-- | src/args.cc | 63 | ||||
| -rw-r--r-- | src/args.hh | 22 | ||||
| -rw-r--r-- | test/.gitignore | 1 | ||||
| -rw-r--r-- | test/Makefile.am | 6 | ||||
| -rw-r--r-- | test/test-args.cc | 104 |
6 files changed, 196 insertions, 1 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 5a779fa..54ff40d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,6 +12,7 @@ libcgi_la_SOURCES = cgi.hh common.hh cgi.cc \ query_parser.hh query_parser.cc \ header_parser.hh header_parser.cc \ strutils.hh strutils.cc \ + args.hh args.cc \ multipart_formdata_parser.hh multipart_formdata_parser.cc libcgi_la_CPPFLAGS = $(AM_CPPFLAGS) @FASTCGI_CFLAGS@ libcgi_la_LIBADD = @FASTCGI_LIBS@ diff --git a/src/args.cc b/src/args.cc new file mode 100644 index 0000000..1c0ce17 --- /dev/null +++ b/src/args.cc @@ -0,0 +1,63 @@ +#include "common.hh" + +#include "args.hh" + +namespace stuff { + +// static +bool Args::parse(const std::string& input, std::vector<std::string>* output, + bool nice) { + size_t last = 0, i = 0; + std::string arg; + output->clear(); + while (i < input.size() && input[i] == ' ') ++i; + if (i == input.size()) return true; + while (i < input.size()) { + if (input[i] == ' ') { + arg.append(input.substr(last, i - last)); + output->push_back(arg); + arg.clear(); + ++i; + while (i < input.size() && input[i] == ' ') ++i; + if (i == input.size()) return true; + last = i; + } else if (input[i] == '\'' || input[i] == '"') { + const char closing = input[i]; + std::string text; + size_t j = i + 1; + size_t last_j = j; + while (j < input.size()) { + if (input[j] == closing) { + break; + } else if (input[j] == '\\') { + text.append(input.substr(last_j, j - last_j)); + if (j == input.size()) { + if (!nice) return false; + break; + } + text.push_back(input[++j]); + last_j = ++j; + } else { + ++j; + } + } + if (j < input.size()) { + arg.append(input.substr(last, i - last)); + arg.append(text); + arg.append(input.substr(last_j, j - last_j)); + last = i = j + 1; + } else { + // No closing char + if (!nice) return false; + ++i; + } + } else { + ++i; + } + } + arg.append(input.substr(last, i - last)); + output->push_back(arg); + return true; +} + +} // namespace stuff diff --git a/src/args.hh b/src/args.hh new file mode 100644 index 0000000..7a0f4fc --- /dev/null +++ b/src/args.hh @@ -0,0 +1,22 @@ +#ifndef ARGS_HH +#define ARGS_HH + +#include <string> +#include <vector> + +namespace stuff { + +class Args { +public: + static bool parse(const std::string& input, + std::vector<std::string>* output, + bool nice = true); + +private: + Args() = delete; + ~Args() = delete; +}; + +} // namespace stuff + +#endif /* ARGS_HH */ diff --git a/test/.gitignore b/test/.gitignore index 8ab7990..d859557 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -3,3 +3,4 @@ /test-header-parser /test-multipart-formdata-parser /test-query-parser +/test-args diff --git a/test/Makefile.am b/test/Makefile.am index 3017b6f..5711aa4 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -2,7 +2,8 @@ MAINTAINERCLEANFILES = Makefile.in AM_CPPFLAGS = @DEFINES@ -I$(top_builddir)/src -TESTS = test-query-parser test-header-parser test-multipart-formdata-parser +TESTS = test-query-parser test-header-parser test-multipart-formdata-parser \ + test-args check_PROGRAMS = $(TESTS) @@ -14,3 +15,6 @@ test_header_parser_LDADD = $(top_srcdir)/src/libcgi.la test_multipart_formdata_parser_SOURCES = test-multipart-formdata-parser.cc test_multipart_formdata_parser_LDADD = $(top_srcdir)/src/libcgi.la + +test_args_SOURCES = test-args.cc +test_args_LDADD = $(top_srcdir)/src/libcgi.la diff --git a/test/test-args.cc b/test/test-args.cc new file mode 100644 index 0000000..a9d4328 --- /dev/null +++ b/test/test-args.cc @@ -0,0 +1,104 @@ +#include "common.hh" + +#include <cstdarg> +#include <iostream> + +#include "args.hh" + +using namespace stuff; + +namespace { + +bool compare(const std::string& in, const std::vector<std::string>& out, + va_list args) { + for (const auto& arg : out) { + const char* str = va_arg(args, const char*); + if (!str) { + std::cerr << "expected less arguments: " << in + << ", first extra: " << arg << std::endl; + return false; + } + if (arg.compare(str) != 0) { + std::cerr << "expected " << str << " in " << in + << ", got: " << arg << std::endl; + return false; + } + } + const char* str = va_arg(args, const char*); + if (str) { + std::cerr << "expected more arguments: " << in + << ", missing: " << str << std::endl; + return false; + } + return true; +} + +bool test(const char* in, ...) { + std::vector<std::string> out; + if (!Args::parse(in, &out, false)) { + std::cerr << "expected success: " << in << std::endl; + return false; + } + va_list args; + va_start(args, in); + bool ret = compare(in, out, args); + va_end(args); + return ret; +} + +bool test_nice(const char* in, ...) { + std::vector<std::string> out; + if (!Args::parse(in, &out, true)) { + std::cerr << "expected success: " << in << std::endl; + return false; + } + va_list args; + va_start(args, in); + bool ret = compare(in, out, args); + va_end(args); + return ret; +} + +bool test_fail(const std::string& in) { + std::vector<std::string> out; + if (Args::parse(in, &out, false)) { + std::cerr << "expected fail: " << in << std::endl; + return false; + } + return true; +} + +} // namespace + +int main() { + unsigned int ok = 0, tot = 0; + + tot++; if (test("", NULL)) ok++; + tot++; if (test("foo", "foo", NULL)) ok++; + tot++; if (test("hello world", "hello", "world", NULL)) ok++; + tot++; if (test("\"hello world\"", "hello world", NULL)) ok++; + tot++; if (test("'hello world'", "hello world", NULL)) ok++; + tot++; if (test("a' b'", "a b", NULL)) ok++; + tot++; if (test("a'b'ba", "abba", NULL)) ok++; + tot++; if (test("a'b'b '' a", "abb", "", "a", NULL)) ok++; + tot++; if (test("'\"'", "\"", NULL)) ok++; + tot++; if (test("'\\\"'", "\"", NULL)) ok++; + tot++; if (test("'\\\\'", "\\", NULL)) ok++; + tot++; if (test("'\\''", "'", NULL)) ok++; + tot++; if (test("'a\\'b'", "a'b", NULL)) ok++; + tot++; if (test("a'\\''b", "a'b", NULL)) ok++; + + tot++; if (test_nice("a'b", "a'b", NULL)) ok++; + tot++; if (test_nice("a'b foo", "a'b", "foo", NULL)) ok++; + tot++; if (test_nice("'\\", "'\\", NULL)) ok++; + tot++; if (test_nice("'\\'", "'\\'", NULL)) ok++; + + tot++; if (test_fail("'")) ok++; + tot++; if (test_fail("'''")) ok++; + tot++; if (test_fail("\"")) ok++; + tot++; if (test_fail("'\\")) ok++; + tot++; if (test_fail("'\\'")) ok++; + + std::cout << "OK " << ok << "/" << tot << std::endl; + return ok == tot ? EXIT_SUCCESS : EXIT_FAILURE; +} |
