From 47ce2398a4f5d7e4a50814eb8fa8990547f74999 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 11 May 2016 08:41:32 -0400 Subject: [PATCH] fix http unit test SIGBUS in os x --- HTTP.cpp | 27 +++++++++++++-------------- HTTP.h | 4 ++-- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/HTTP.cpp b/HTTP.cpp index 74acfb7a..02b58f99 100644 --- a/HTTP.cpp +++ b/HTTP.cpp @@ -10,23 +10,22 @@ namespace i2p { namespace http { - const char *HTTP_METHODS[] = { + const std::vector HTTP_METHODS = { "GET", "HEAD", "POST", "PUT", "PATCH", - "DELETE", "OPTIONS", "CONNECT", - NULL + "DELETE", "OPTIONS", "CONNECT" }; - const char *HTTP_VERSIONS[] = { - "HTTP/1.0", "HTTP/1.1", NULL + const std::vector HTTP_VERSIONS = { + "HTTP/1.0", "HTTP/1.1" }; - bool in_cstr_array(const char **haystack, const char *needle) { - for (const char *p = haystack[0]; p != NULL; p++) { - if (strcmp(p, needle) == 0) - return true; - } - return false; + inline bool is_http_version(const std::string & str) { + return std::find(HTTP_VERSIONS.begin(), HTTP_VERSIONS.end(), str) != std::end(HTTP_VERSIONS); } + inline bool is_http_method(const std::string & str) { + return std::find(HTTP_METHODS.begin(), HTTP_METHODS.end(), str) != std::end(HTTP_METHODS); + } + void strsplit(const std::string & line, std::vector &tokens, char delim, std::size_t limit = 0) { std::size_t count = 0; std::stringstream ss(line); @@ -205,9 +204,9 @@ namespace http { strsplit(line, tokens, ' '); if (tokens.size() != 3) return -1; - if (!in_cstr_array(HTTP_METHODS, tokens[0].c_str())) + if (!is_http_method(tokens[0])) return -1; - if (!in_cstr_array(HTTP_VERSIONS, tokens[2].c_str())) + if (!is_http_version(tokens[2])) return -1; if (!url.parse(tokens[1])) return -1; @@ -288,7 +287,7 @@ namespace http { strsplit(line, tokens, ' ', 3); if (tokens.size() != 3) return -1; - if (!in_cstr_array(HTTP_VERSIONS, tokens[0].c_str())) + if (!is_http_version(tokens[0])) return -1; code = atoi(tokens[1].c_str()); if (code < 100 || code >= 600) diff --git a/HTTP.h b/HTTP.h index 864ad88b..9bd31c75 100644 --- a/HTTP.h +++ b/HTTP.h @@ -19,8 +19,8 @@ namespace i2p { namespace http { const char CRLF[] = "\r\n"; /**< HTTP line terminator */ const char HTTP_EOH[] = "\r\n\r\n"; /**< HTTP end-of-headers mark */ - extern const char *HTTP_METHODS[]; /**< list of valid HTTP methods */ - extern const char *HTTP_VERSIONS[]; /**< list of valid HTTP versions */ + extern const std::vector HTTP_METHODS; /**< list of valid HTTP methods */ + extern const std::vector HTTP_VERSIONS; /**< list of valid HTTP versions */ struct URL { std::string schema;