Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 4 additions & 45 deletions src/http_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#endif
#include <sstream>
#include <iomanip>
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions src/httpserver/http_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string,std::string,arg_comparator> &map);

/**
* Process escape sequences ('+'=space, %HH) Updates val in place; the
* result should be UTF-8 encoded and cannot be larger than the input.
Expand All @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions test/unit/http_utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,19 @@
USA
*/

#if defined(__MINGW32__) || defined(__CYGWIN32__)
#define _WINDOWS
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x600
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#endif

#include "littletest.hpp"
#include "http_utils.hpp"

#include <cstdio>

using namespace httpserver;
Expand Down Expand Up @@ -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()