mirror of https://github.com/PurpleI2P/i2pd.git
Browse Source
+ new http/url classes * extract most page/cmd handlers from HTTPConnection class * general cleanup of HTTPServer.{cpp,h} + http basic auth for webconsole + gracefull/quick shutdown commands - drop direct access for b32 addresses from webconsole: use proxy insteadpull/489/head
hagen
9 years ago
14 changed files with 1207 additions and 560 deletions
@ -0,0 +1,389 @@
@@ -0,0 +1,389 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2016, The PurpleI2P Project |
||||
* |
||||
* This file is part of Purple i2pd project and licensed under BSD3 |
||||
* |
||||
* See full license text in LICENSE file at top of project tree |
||||
*/ |
||||
|
||||
#include "HTTP.h" |
||||
|
||||
namespace i2p { |
||||
namespace http { |
||||
const char *HTTP_METHODS[] = { |
||||
"GET", "HEAD", "POST", "PUT", "PATCH", |
||||
"DELETE", "OPTIONS", "CONNECT", |
||||
NULL |
||||
}; |
||||
const char *HTTP_VERSIONS[] = { |
||||
"HTTP/1.0", "HTTP/1.1", NULL |
||||
}; |
||||
|
||||
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; |
||||
} |
||||
|
||||
void strsplit(const std::string & line, std::vector<std::string> &tokens, char delim, std::size_t limit = 0) { |
||||
std::size_t count = 0; |
||||
std::stringstream ss(line); |
||||
std::string token; |
||||
while (1) { |
||||
count++; |
||||
if (limit > 0 && count >= limit) |
||||
delim = '\n'; /* reset delimiter */ |
||||
if (!std::getline(ss, token, delim)) |
||||
break; |
||||
tokens.push_back(token); |
||||
} |
||||
} |
||||
|
||||
bool parse_header_line(const std::string & line, std::map<std::string, std::string> & headers) { |
||||
std::size_t pos = 0; |
||||
std::size_t len = 2; /* strlen(": ") */ |
||||
if ((pos = line.find(": ", pos)) == std::string::npos) |
||||
return false; |
||||
while (isspace(line.at(pos + len))) |
||||
len++; |
||||
std::string name = line.substr(0, pos); |
||||
std::string value = line.substr(pos + len); |
||||
headers[name] = value; |
||||
return true; |
||||
} |
||||
|
||||
bool URL::parse(const char *str, std::size_t len) { |
||||
std::string url(str, len ? len : strlen(str)); |
||||
return parse(url); |
||||
} |
||||
|
||||
bool URL::parse(const std::string& url) { |
||||
std::size_t pos_p = 0; /* < current parse position */ |
||||
std::size_t pos_c = 0; /* < work position */ |
||||
if (url.at(0) != '/') { |
||||
/* schema */ |
||||
pos_c = url.find("://"); |
||||
if (pos_c != std::string::npos) { |
||||
schema = url.substr(0, pos_c); |
||||
pos_p = pos_c + 3; |
||||
} |
||||
/* user[:pass] */ |
||||
pos_c = url.find('@', pos_p); |
||||
if (pos_c != std::string::npos) { |
||||
std::size_t delim = url.find(':', pos_p); |
||||
if (delim != std::string::npos && delim < pos_c) { |
||||
user = url.substr(pos_p, delim - pos_p); |
||||
delim += 1; |
||||
pass = url.substr(delim, pos_c - delim); |
||||
} else { |
||||
user = url.substr(pos_p, pos_c - pos_p); |
||||
} |
||||
pos_p = pos_c + 1; |
||||
} |
||||
/* hostname[:port][/path] */ |
||||
pos_c = url.find_first_of(":/", pos_p); |
||||
if (pos_c == std::string::npos) { |
||||
/* only hostname, without post and path */ |
||||
host = url.substr(pos_p, std::string::npos); |
||||
return true; |
||||
} else if (url.at(pos_c) == ':') { |
||||
host = url.substr(pos_p, pos_c - pos_p); |
||||
/* port[/path] */ |
||||
pos_p = pos_c + 1; |
||||
pos_c = url.find('/', pos_p); |
||||
std::string port_str = (pos_c == std::string::npos) |
||||
? url.substr(pos_p, std::string::npos) |
||||
: url.substr(pos_p, pos_c - pos_p); |
||||
/* stoi throws exception on failure, we don't need it */ |
||||
for (char c : port_str) { |
||||
if (c < '0' || c > '9') |
||||
return false; |
||||
port *= 10; |
||||
port += c - '0'; |
||||
} |
||||
if (pos_c == std::string::npos) |
||||
return true; /* no path part */ |
||||
pos_p = pos_c; |
||||
} else { |
||||
/* start of path part found */ |
||||
host = url.substr(pos_p, pos_c - pos_p); |
||||
pos_p = pos_c; |
||||
} |
||||
} |
||||
|
||||
/* pos_p now at start of path part */ |
||||
pos_c = url.find_first_of("?#", pos_p); |
||||
if (pos_c == std::string::npos) { |
||||
/* only path, without fragment and query */ |
||||
path = url.substr(pos_p, std::string::npos); |
||||
return true; |
||||
} else if (url.at(pos_c) == '?') { |
||||
/* found query part */ |
||||
path = url.substr(pos_p, pos_c - pos_p); |
||||
pos_p = pos_c + 1; |
||||
pos_c = url.find('#', pos_p); |
||||
if (pos_c == std::string::npos) { |
||||
/* no fragment */ |
||||
query = url.substr(pos_p, std::string::npos); |
||||
return true; |
||||
} else { |
||||
query = url.substr(pos_p, pos_c - pos_p); |
||||
pos_p = pos_c + 1; |
||||
} |
||||
} else { |
||||
/* found fragment part */ |
||||
path = url.substr(pos_p, pos_c - pos_p); |
||||
pos_p = pos_c + 1; |
||||
} |
||||
|
||||
/* pos_p now at start of fragment part */ |
||||
frag = url.substr(pos_p, std::string::npos); |
||||
return true; |
||||
} |
||||
|
||||
bool URL::parse_query(std::map<std::string, std::string> & params) { |
||||
std::vector<std::string> tokens; |
||||
strsplit(query, tokens, '&'); |
||||
|
||||
params.clear(); |
||||
for (auto it : tokens) { |
||||
std::size_t eq = it.find ('='); |
||||
if (eq != std::string::npos) { |
||||
auto e = std::pair<std::string, std::string>(it.substr(0, eq), it.substr(eq + 1)); |
||||
params.insert(e); |
||||
} else { |
||||
auto e = std::pair<std::string, std::string>(it, ""); |
||||
params.insert(e); |
||||
} |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
std::string URL::to_string() { |
||||
std::string out = ""; |
||||
if (schema != "") { |
||||
out = schema + "://"; |
||||
if (user != "" && pass != "") { |
||||
out += user + ":" + pass + "@"; |
||||
} else if (user != "") { |
||||
out += user + "@"; |
||||
} |
||||
if (port) { |
||||
out += host + ":" + std::to_string(port); |
||||
} else { |
||||
out += host; |
||||
} |
||||
} |
||||
out += path; |
||||
if (query != "") |
||||
out += "?" + query; |
||||
if (frag != "") |
||||
out += "#" + frag; |
||||
return out; |
||||
} |
||||
|
||||
int HTTPReq::parse(const char *buf, size_t len) { |
||||
std::string str(buf, len); |
||||
return parse(str); |
||||
} |
||||
|
||||
int HTTPReq::parse(const std::string& str) { |
||||
enum { REQ_LINE, HEADER_LINE } expect = REQ_LINE; |
||||
std::size_t eoh = str.find(HTTP_EOH); /* request head size */ |
||||
std::size_t eol = 0, pos = 0; |
||||
URL url; |
||||
|
||||
if (eoh == std::string::npos) |
||||
return 0; /* str not contains complete request */ |
||||
|
||||
while ((eol = str.find(CRLF, pos)) != std::string::npos) { |
||||
if (expect == REQ_LINE) { |
||||
std::string line = str.substr(pos, eol - pos); |
||||
std::vector<std::string> tokens; |
||||
strsplit(line, tokens, ' '); |
||||
if (tokens.size() != 3) |
||||
return -1; |
||||
if (!in_cstr_array(HTTP_METHODS, tokens[0].c_str())) |
||||
return -1; |
||||
if (!in_cstr_array(HTTP_VERSIONS, tokens[2].c_str())) |
||||
return -1; |
||||
if (!url.parse(tokens[1])) |
||||
return -1; |
||||
/* all ok */ |
||||
method = tokens[0]; |
||||
uri = tokens[1]; |
||||
version = tokens[2]; |
||||
expect = HEADER_LINE; |
||||
} else { |
||||
std::string line = str.substr(pos, eol - pos); |
||||
if (!parse_header_line(line, headers)) |
||||
return -1; |
||||
} |
||||
pos = eol + strlen(CRLF); |
||||
if (pos >= eoh) |
||||
break; |
||||
} |
||||
auto it = headers.find("Host"); |
||||
if (it != headers.end ()) { |
||||
host = it->second; |
||||
} else if (version == "HTTP/1.1") { |
||||
return -1; /* 'Host' header required for HTTP/1.1 */ |
||||
} else if (url.host != "") { |
||||
host = url.host; |
||||
} |
||||
return eoh + strlen(HTTP_EOH); |
||||
} |
||||
|
||||
std::string HTTPReq::to_string() { |
||||
std::stringstream ss; |
||||
ss << method << " " << uri << " " << version << CRLF; |
||||
ss << "Host: " << host << CRLF; |
||||
for (auto & h : headers) { |
||||
ss << h.first << ": " << h.second << CRLF; |
||||
} |
||||
ss << CRLF; |
||||
return ss.str(); |
||||
} |
||||
|
||||
bool HTTPRes::is_chunked() { |
||||
auto it = headers.find("Transfer-Encoding"); |
||||
if (it == headers.end()) |
||||
return false; |
||||
if (it->second.find("chunked") == std::string::npos) |
||||
return true; |
||||
return false; |
||||
} |
||||
|
||||
long int HTTPRes::length() { |
||||
unsigned long int length = 0; |
||||
auto it = headers.find("Content-Length"); |
||||
if (it == headers.end()) |
||||
return -1; |
||||
errno = 0; |
||||
length = std::strtoul(it->second.c_str(), (char **) NULL, 10); |
||||
if (errno != 0) |
||||
return -1; |
||||
return length; |
||||
} |
||||
|
||||
int HTTPRes::parse(const char *buf, size_t len) { |
||||
std::string str(buf, len); |
||||
return parse(str); |
||||
} |
||||
|
||||
int HTTPRes::parse(const std::string& str) { |
||||
enum { RES_LINE, HEADER_LINE } expect = RES_LINE; |
||||
std::size_t eoh = str.find(HTTP_EOH); /* request head size */ |
||||
std::size_t eol = 0, pos = 0; |
||||
|
||||
if (eoh == std::string::npos) |
||||
return 0; /* str not contains complete request */ |
||||
|
||||
while ((eol = str.find(CRLF, pos)) != std::string::npos) { |
||||
if (expect == RES_LINE) { |
||||
std::string line = str.substr(pos, eol - pos); |
||||
std::vector<std::string> tokens; |
||||
strsplit(line, tokens, ' ', 3); |
||||
if (tokens.size() != 3) |
||||
return -1; |
||||
if (!in_cstr_array(HTTP_VERSIONS, tokens[0].c_str())) |
||||
return -1; |
||||
code = atoi(tokens[1].c_str()); |
||||
if (code < 100 || code >= 600) |
||||
return -1; |
||||
/* all ok */ |
||||
version = tokens[0]; |
||||
status = tokens[2]; |
||||
expect = HEADER_LINE; |
||||
} else { |
||||
std::string line = str.substr(pos, eol - pos); |
||||
if (!parse_header_line(line, headers)) |
||||
return -1; |
||||
} |
||||
pos = eol + strlen(CRLF); |
||||
if (pos >= eoh) |
||||
break; |
||||
} |
||||
|
||||
return eoh + strlen(HTTP_EOH); |
||||
} |
||||
|
||||
std::string HTTPRes::to_string() { |
||||
std::stringstream ss; |
||||
ss << version << " " << code << " " << status << CRLF; |
||||
for (auto & h : headers) { |
||||
ss << h.first << ": " << h.second << CRLF; |
||||
} |
||||
ss << CRLF; |
||||
return ss.str(); |
||||
} |
||||
|
||||
const char * HTTPCodeToStatus(int code) { |
||||
const char *ptr; |
||||
switch (code) { |
||||
case 105: ptr = "Name Not Resolved"; break; |
||||
/* success */ |
||||
case 200: ptr = "OK"; break; |
||||
case 206: ptr = "Partial Content"; break; |
||||
/* redirect */ |
||||
case 301: ptr = "Moved Permanently"; break; |
||||
case 302: ptr = "Found"; break; |
||||
case 304: ptr = "Not Modified"; break; |
||||
case 307: ptr = "Temporary Redirect"; break; |
||||
/* client error */ |
||||
case 400: ptr = "Bad Request"; break; |
||||
case 401: ptr = "Unauthorized"; break; |
||||
case 403: ptr = "Forbidden"; break; |
||||
case 404: ptr = "Not Found"; break; |
||||
case 407: ptr = "Proxy Authentication Required"; break; |
||||
case 408: ptr = "Request Timeout"; break; |
||||
/* server error */ |
||||
case 500: ptr = "Internal Server Error"; break; |
||||
case 502: ptr = "Bad Gateway"; break; |
||||
case 503: ptr = "Not Implemented"; break; |
||||
case 504: ptr = "Gateway Timeout"; break; |
||||
default: ptr = "Unknown Status"; break; |
||||
} |
||||
return ptr; |
||||
} |
||||
|
||||
std::string UrlDecode(const std::string& data, bool allow_null) { |
||||
std::string decoded(data); |
||||
size_t pos = 0; |
||||
while ((pos = decoded.find('%', pos)) != std::string::npos) { |
||||
char c = strtol(decoded.substr(pos + 1, 2).c_str(), NULL, 16); |
||||
if (c == '\0' && !allow_null) { |
||||
pos += 3; |
||||
continue; |
||||
} |
||||
decoded.replace(pos, 3, 1, c); |
||||
pos++; |
||||
} |
||||
return decoded; |
||||
} |
||||
|
||||
bool MergeChunkedResponse (std::istream& in, std::ostream& out) { |
||||
std::string hexLen; |
||||
long int len; |
||||
while (!in.eof ()) { |
||||
std::getline (in, hexLen); |
||||
errno = 0; |
||||
len = strtoul(hexLen.c_str(), (char **) NULL, 16); |
||||
if (errno != 0) |
||||
return false; /* conversion error */ |
||||
if (len == 0) |
||||
return true; /* end of stream */ |
||||
if (len < 0 || len > 10 * 1024 * 1024) /* < 10Mb */ |
||||
return false; /* too large chunk */ |
||||
char * buf = new char[len]; |
||||
in.read (buf, len); |
||||
out.write (buf, len); |
||||
delete[] buf; |
||||
std::getline (in, hexLen); // read \r\n after chunk
|
||||
} |
||||
return true; |
||||
} |
||||
} // http
|
||||
} // i2p
|
@ -0,0 +1,121 @@
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2016, The PurpleI2P Project |
||||
* |
||||
* This file is part of Purple i2pd project and licensed under BSD3 |
||||
* |
||||
* See full license text in LICENSE file at top of project tree |
||||
*/ |
||||
|
||||
#ifndef HTTP_H__ |
||||
#define HTTP_H__ |
||||
|
||||
#include <cstring> |
||||
#include <map> |
||||
#include <sstream> |
||||
#include <string> |
||||
#include <vector> |
||||
|
||||
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 */ |
||||
|
||||
struct URL { |
||||
std::string schema; |
||||
std::string user; |
||||
std::string pass; |
||||
std::string host; |
||||
unsigned short int port; |
||||
std::string path; |
||||
std::string query; |
||||
std::string frag; |
||||
|
||||
URL(): schema(""), user(""), pass(""), host(""), port(0), path(""), query(""), frag("") {}; |
||||
|
||||
/**
|
||||
* @brief Tries to parse url from string |
||||
* @return true on success, false on invalid url |
||||
*/ |
||||
bool parse (const char *str, size_t len = 0); |
||||
bool parse (const std::string& url); |
||||
|
||||
/**
|
||||
* @brief Parse query part of url to key/value map |
||||
* @note Honestly, this should be implemented with std::multimap |
||||
*/ |
||||
bool parse_query(std::map<std::string, std::string> & params); |
||||
|
||||
/**
|
||||
* @brief Serialize URL structure to url |
||||
* @note Returns relative url if schema if empty, absolute url otherwise |
||||
*/ |
||||
std::string to_string (); |
||||
}; |
||||
|
||||
struct HTTPReq { |
||||
std::map<std::string, std::string> headers; |
||||
std::string version; |
||||
std::string method; |
||||
std::string uri; |
||||
std::string host; |
||||
|
||||
HTTPReq (): version("HTTP/1.0"), method("GET"), uri("/") {}; |
||||
|
||||
/**
|
||||
* @brief Tries to parse HTTP request from string |
||||
* @return -1 on error, 0 on incomplete query, >0 on success |
||||
* @note Positive return value is a size of header |
||||
*/ |
||||
int parse(const char *buf, size_t len); |
||||
int parse(const std::string& buf); |
||||
|
||||
/** @brief Serialize HTTP request to string */ |
||||
std::string to_string(); |
||||
}; |
||||
|
||||
struct HTTPRes { |
||||
std::map<std::string, std::string> headers; |
||||
std::string version; |
||||
std::string status; |
||||
unsigned short int code; |
||||
|
||||
HTTPRes (): version("HTTP/1.1"), status("OK"), code(200) {} |
||||
|
||||
/**
|
||||
* @brief Tries to parse HTTP response from string |
||||
* @return -1 on error, 0 on incomplete query, >0 on success |
||||
* @note Positive return value is a size of header |
||||
*/ |
||||
int parse(const char *buf, size_t len); |
||||
int parse(const std::string& buf); |
||||
|
||||
/** @brief Serialize HTTP response to string */ |
||||
std::string to_string(); |
||||
|
||||
/** @brief Checks that response declared as chunked data */ |
||||
bool is_chunked(); |
||||
|
||||
/** @brief Returns declared response length or -1 if unknown */ |
||||
long int length(); |
||||
}; |
||||
|
||||
/**
|
||||
* @brief returns HTTP status string by integer code |
||||
* @param code HTTP code [100, 599] |
||||
* @return Immutable string with status |
||||
*/ |
||||
const char * HTTPCodeToStatus(int code); |
||||
|
||||
/**
|
||||
* @brief Replaces %-encoded characters in string with their values |
||||
* @param data Source string |
||||
* @param null If set to true - decode also %00 sequence, otherwise - skip |
||||
* @return Decoded string |
||||
*/ |
||||
std::string UrlDecode(const std::string& data, bool null = false); |
||||
} // http
|
||||
} // i2p
|
||||
|
||||
#endif /* HTTP_H__ */ |
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
CXXFLAGS += -Wall -Wextra -pedantic -O0 -g -std=c++11 -D_GLIBCXX_USE_NANOSLEEP=1 |
||||
|
||||
TESTS = test-http-url test-http-req test-http-res test-http-url_decode |
||||
|
||||
all: $(TESTS) run |
||||
|
||||
test-http-%: test-http-%.cpp ../HTTP.cpp |
||||
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) -o $@ $^ |
||||
|
||||
run: $(TESTS) |
||||
@for TEST in $(TESTS); do ./$$TEST ; done |
||||
|
||||
clean: |
||||
rm -f $(TESTS) |
@ -0,0 +1,82 @@
@@ -0,0 +1,82 @@
|
||||
#include <cassert> |
||||
#include "../HTTP.h" |
||||
|
||||
using namespace i2p::http; |
||||
|
||||
int main(int argc, char *argv[]) { |
||||
HTTPReq *req; |
||||
int ret = 0, len = 0; |
||||
const char *buf; |
||||
|
||||
buf = |
||||
"GET / HTTP/1.0\r\n" |
||||
"User-Agent: curl/7.26.0\r\n" |
||||
"Host: inr.i2p\r\n" |
||||
"Accept: */*\r\n" |
||||
"\r\n" |
||||
"test"; |
||||
len = strlen(buf); |
||||
req = new HTTPReq; |
||||
assert((ret = req->parse(buf, len)) == len - 4); |
||||
assert(req->version == "HTTP/1.0"); |
||||
assert(req->method == "GET"); |
||||
assert(req->uri == "/"); |
||||
assert(req->host == "inr.i2p"); |
||||
assert(req->headers.size() == 3); |
||||
assert(req->headers.count("Host") == 1); |
||||
assert(req->headers.count("Accept") == 1); |
||||
assert(req->headers.count("User-Agent") == 1); |
||||
assert(req->headers.find("Host")->second == "inr.i2p"); |
||||
assert(req->headers.find("Accept")->second == "*/*"); |
||||
assert(req->headers.find("User-Agent")->second == "curl/7.26.0"); |
||||
delete req; |
||||
|
||||
buf = |
||||
"GET / HTTP/1.0\r\n" |
||||
"\r\n"; |
||||
len = strlen(buf); |
||||
req = new HTTPReq; |
||||
assert((ret = req->parse(buf, len)) == len); |
||||
assert(req->version == "HTTP/1.0"); |
||||
assert(req->method == "GET"); |
||||
assert(req->uri == "/"); |
||||
assert(req->host == ""); |
||||
assert(req->headers.size() == 0); |
||||
delete req; |
||||
|
||||
buf = |
||||
"GET / HTTP/1.1\r\n" |
||||
"\r\n"; |
||||
len = strlen(buf); |
||||
req = new HTTPReq; |
||||
assert((ret = req->parse(buf, len)) == -1); /* no host header */ |
||||
delete req; |
||||
|
||||
buf = |
||||
"GET / HTTP/1.0\r\n" |
||||
""; |
||||
len = strlen(buf); |
||||
req = new HTTPReq; |
||||
assert((ret = req->parse(buf, len)) == 0); /* request not completed */ |
||||
delete req; |
||||
|
||||
buf = |
||||
"GET http://inr.i2p HTTP/1.1\r\n" |
||||
"Host: stats.i2p\r\n" |
||||
"Accept: */*\r\n" |
||||
"\r\n"; |
||||
len = strlen(buf); |
||||
req = new HTTPReq; |
||||
assert((ret = req->parse(buf, len)) == len); /* no host header */ |
||||
assert(req->method == "GET"); |
||||
assert(req->uri == "http://inr.i2p"); |
||||
assert(req->host == "stats.i2p"); |
||||
assert(req->headers.size() == 2); |
||||
assert(req->headers.count("Host") == 1); |
||||
assert(req->headers.count("Accept") == 1); |
||||
delete req; |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
/* vim: expandtab:ts=2 */ |
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
#include <cassert> |
||||
#include "../HTTP.h" |
||||
|
||||
using namespace i2p::http; |
||||
|
||||
int main(int argc, char *argv[]) { |
||||
HTTPRes *res; |
||||
int ret = 0, len = 0; |
||||
const char *buf; |
||||
|
||||
buf = |
||||
"HTTP/1.1 304 Not Modified\r\n" |
||||
"Date: Thu, 14 Apr 2016 00:00:00 GMT\r\n" |
||||
"Server: nginx/1.2.1\r\n" |
||||
"Content-Length: 536\r\n" |
||||
"\r\n"; |
||||
len = strlen(buf); |
||||
res = new HTTPRes; |
||||
assert((ret = res->parse(buf, len)) == len); |
||||
assert(res->version == "HTTP/1.1"); |
||||
assert(res->status == "Not Modified"); |
||||
assert(res->code == 304); |
||||
assert(res->headers.size() == 3); |
||||
assert(res->headers.count("Date") == 1); |
||||
assert(res->headers.count("Server") == 1); |
||||
assert(res->headers.count("Content-Length") == 1); |
||||
assert(res->headers.find("Date")->second == "Thu, 14 Apr 2016 00:00:00 GMT"); |
||||
assert(res->headers.find("Server")->second == "nginx/1.2.1"); |
||||
assert(res->headers.find("Content-Length")->second == "536"); |
||||
assert(res->is_chunked() == false); |
||||
assert(res->length() == 536); |
||||
delete res; |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
/* vim: expandtab:ts=2 */ |
@ -0,0 +1,110 @@
@@ -0,0 +1,110 @@
|
||||
#include <cassert> |
||||
#include "../HTTP.h" |
||||
|
||||
using namespace i2p::http; |
||||
|
||||
int main(int argc, char *argv[]) { |
||||
std::map<std::string, std::string> params; |
||||
URL *url; |
||||
|
||||
url = new URL; |
||||
assert(url->parse("https://127.0.0.1:7070/asdasd?12345") == true); |
||||
assert(url->schema == "https"); |
||||
assert(url->user == ""); |
||||
assert(url->pass == ""); |
||||
assert(url->host == "127.0.0.1"); |
||||
assert(url->port == 7070); |
||||
assert(url->path == "/asdasd"); |
||||
assert(url->query == "12345"); |
||||
assert(url->to_string() == "https://127.0.0.1:7070/asdasd?12345"); |
||||
delete url; |
||||
|
||||
url = new URL; |
||||
assert(url->parse("http://user:password@site.com:8080/asdasd?123456") == true); |
||||
assert(url->schema == "http"); |
||||
assert(url->user == "user"); |
||||
assert(url->pass == "password"); |
||||
assert(url->host == "site.com"); |
||||
assert(url->port == 8080); |
||||
assert(url->path == "/asdasd"); |
||||
assert(url->query == "123456"); |
||||
delete url; |
||||
|
||||
url = new URL; |
||||
assert(url->parse("http://user:password@site.com/asdasd?name=value") == true); |
||||
assert(url->schema == "http"); |
||||
assert(url->user == "user"); |
||||
assert(url->pass == "password"); |
||||
assert(url->host == "site.com"); |
||||
assert(url->port == 0); |
||||
assert(url->path == "/asdasd"); |
||||
assert(url->query == "name=value"); |
||||
delete url; |
||||
|
||||
url = new URL; |
||||
assert(url->parse("http://user:@site.com/asdasd?name=value1&name=value2") == true); |
||||
assert(url->schema == "http"); |
||||
assert(url->user == "user"); |
||||
assert(url->pass == ""); |
||||
assert(url->host == "site.com"); |
||||
assert(url->port == 0); |
||||
assert(url->path == "/asdasd"); |
||||
assert(url->query == "name=value1&name=value2"); |
||||
delete url; |
||||
|
||||
url = new URL; |
||||
assert(url->parse("http://user@site.com/asdasd?name1=value1&name2&name3=value2") == true); |
||||
assert(url->schema == "http"); |
||||
assert(url->user == "user"); |
||||
assert(url->pass == ""); |
||||
assert(url->host == "site.com"); |
||||
assert(url->port == 0); |
||||
assert(url->path == "/asdasd"); |
||||
assert(url->query == "name1=value1&name2&name3=value2"); |
||||
assert(url->parse_query(params)); |
||||
assert(params.size() == 3); |
||||
assert(params.count("name1") == 1); |
||||
assert(params.count("name2") == 1); |
||||
assert(params.count("name3") == 1); |
||||
assert(params.find("name1")->second == "value1"); |
||||
assert(params.find("name2")->second == ""); |
||||
assert(params.find("name3")->second == "value2"); |
||||
delete url; |
||||
|
||||
url = new URL; |
||||
assert(url->parse("http://@site.com:800/asdasd?") == true); |
||||
assert(url->schema == "http"); |
||||
assert(url->user == ""); |
||||
assert(url->pass == ""); |
||||
assert(url->host == "site.com"); |
||||
assert(url->port == 800); |
||||
assert(url->path == "/asdasd"); |
||||
assert(url->query == ""); |
||||
delete url; |
||||
|
||||
url = new URL; |
||||
assert(url->parse("http://@site.com:17") == true); |
||||
assert(url->schema == "http"); |
||||
assert(url->user == ""); |
||||
assert(url->pass == ""); |
||||
assert(url->host == "site.com"); |
||||
assert(url->port == 17); |
||||
assert(url->path == ""); |
||||
assert(url->query == ""); |
||||
delete url; |
||||
|
||||
url = new URL; |
||||
assert(url->parse("http://user:password@site.com:err_port/asdasd") == false); |
||||
assert(url->schema == "http"); |
||||
assert(url->user == "user"); |
||||
assert(url->pass == "password"); |
||||
assert(url->host == "site.com"); |
||||
assert(url->port == 0); |
||||
assert(url->path == ""); |
||||
assert(url->query == ""); |
||||
delete url; |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
/* vim: expandtab:ts=2 */ |
@ -0,0 +1,19 @@
@@ -0,0 +1,19 @@
|
||||
#include <cassert> |
||||
#include "../HTTP.h" |
||||
|
||||
using namespace i2p::http; |
||||
|
||||
int main(int argc, char *argv[]) { |
||||
std::string in("/%D1%81%D1%82%D1%80%D0%B0%D0%BD%D0%B8%D1%86%D0%B0/"); |
||||
std::string out = UrlDecode(in); |
||||
|
||||
assert(strcmp(out.c_str(), "/страница/") == 0); |
||||
|
||||
in = "/%00/"; |
||||
out = UrlDecode(in, false); |
||||
assert(strcmp(out.c_str(), "/%00/") == 0); |
||||
out = UrlDecode(in, true); |
||||
assert(strcmp(out.c_str(), "/\0/") == 0); |
||||
|
||||
return 0; |
||||
} |
Loading…
Reference in new issue