diff --git a/src/http_utils.cpp b/src/http_utils.cpp index 3dde0b7f..3f856f65 100644 --- a/src/http_utils.cpp +++ b/src/http_utils.cpp @@ -28,7 +28,8 @@ #include #include #else -#include +#include +#include #endif #include #include @@ -244,36 +245,8 @@ void get_ip_str( { if(sa) { - char to_ret[INET6_ADDRSTRLEN] = { '\0' }; - switch(sa->sa_family) - { - case AF_INET: - if(maxlen == 0) - maxlen = INET_ADDRSTRLEN; - - inet_ntop(AF_INET, - &(((struct sockaddr_in *)sa)->sin_addr), - to_ret, - maxlen - ); - - break; - - case AF_INET6: - if(maxlen == 0) - maxlen = INET6_ADDRSTRLEN; - - inet_ntop(AF_INET6, - &(((struct sockaddr_in6 *)sa)->sin6_addr), - to_ret, - maxlen - ); - - break; - default: - strncpy(to_ret, "Unknown AF", 11); - return; - } + char to_ret[NI_MAXHOST]; + getnameinfo(sa, sizeof (struct sockaddr), to_ret, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); result = to_ret; } } @@ -288,20 +261,6 @@ std::string get_ip_str_new( return to_ret; } -const struct sockaddr str_to_ip(const std::string& src) -{ - struct sockaddr s; - if(src.find(":") != std::string::npos) - { - inet_pton(AF_INET6, src.c_str(), (void*) &s); - } - else - { - inet_pton(AF_INET, src.c_str(), (void*) &s); - } - return s; -} - short get_port(const struct sockaddr* sa) { if(sa) diff --git a/src/httpserver/http_utils.hpp b/src/httpserver/http_utils.hpp index edc19552..441efe2f 100644 --- a/src/httpserver/http_utils.hpp +++ b/src/httpserver/http_utils.hpp @@ -347,7 +347,7 @@ void dump_header_map(std::ostream &os, const std::string &prefix, **/ void dump_arg_map(std::ostream &os, const std::string &prefix, const std::map &map); - + /** * Process escape sequences ('+'=space, %HH) Updates val in place; the * result should be UTF-8 encoded and cannot be larger than the input. @@ -359,8 +359,6 @@ void dump_arg_map(std::ostream &os, const std::string &prefix, */ size_t http_unescape (char *val); -const struct sockaddr str_to_ip(const std::string& src); - char* load_file (const char *filename); size_t load_file (const char* filename, char** content); diff --git a/test/unit/http_utils_test.cpp b/test/unit/http_utils_test.cpp index 8736a2c3..4b058144 100644 --- a/test/unit/http_utils_test.cpp +++ b/test/unit/http_utils_test.cpp @@ -18,8 +18,19 @@ USA */ +#if defined(__MINGW32__) || defined(__CYGWIN32__) +#define _WINDOWS +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x600 +#include +#include +#else +#include +#endif + #include "littletest.hpp" #include "http_utils.hpp" + #include using namespace httpserver; @@ -69,6 +80,19 @@ LT_BEGIN_AUTO_TEST(http_utils_suite, standardize_url) LT_CHECK_EQ(result, "/abc/pqr"); LT_END_AUTO_TEST(standardize_url) +LT_BEGIN_AUTO_TEST(http_utils_suite, ip_to_str) + struct sockaddr_in ip4addr; + + ip4addr.sin_family = AF_INET; + ip4addr.sin_port = htons(3490); + ip4addr.sin_addr.s_addr = inet_addr("127.0.0.1"); + + string result = ""; + http::get_ip_str((struct sockaddr *) &ip4addr, result); + + LT_CHECK_EQ(result, "127.0.0.1"); +LT_END_AUTO_TEST(ip_to_str) + LT_BEGIN_AUTO_TEST_ENV() AUTORUN_TESTS() LT_END_AUTO_TEST_ENV()