Skip to content

Commit

Permalink
[http] fix query detection, addresshelper processing, update tests
Browse files Browse the repository at this point in the history
Signed-off-by: R4SAS <r4sas@i2pmail.org>
  • Loading branch information
r4sas committed Feb 14, 2023
1 parent 4156900 commit 77142e5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
2 changes: 1 addition & 1 deletion libi2pd/HTTP.h
Expand Up @@ -37,7 +37,7 @@ namespace http
std::string query;
std::string frag;

URL(): schema(""), user(""), pass(""), host(""), port(0), path(""), query(""), frag("") {};
URL(): schema(""), user(""), pass(""), host(""), port(0), path(""), hasquery(false), query(""), frag("") {};

/**
* @brief Tries to parse url from string
Expand Down
28 changes: 26 additions & 2 deletions libi2pd_client/HTTPProxy.cpp
Expand Up @@ -238,9 +238,33 @@ namespace proxy {
std::string value = params["i2paddresshelper"];
len += value.length();
b64 = i2p::http::UrlDecode(value);

// if we need update exists, request formed with update param
if (params["update"] == "true") { len += std::strlen("&update=true"); confirm = true; }
if (pos != 0 && url.query[pos-1] == '&') { pos--; len++; } // if helper is not only one query option
if (params["update"] == "true")
{
len += std::strlen("&update=true");
confirm = true;
}

// if helper is not only one query option and it placed after user's query
if (pos != 0 && url.query[pos-1] == '&')
{
pos--;
len++;
}
// if helper is not only one query option and it placed before user's query
else if (pos == 0 && url.query.length () > len && url.query[len] == '&')
{
// we don't touch the '?' but remove the trailing '&'
len++;
}
else
{
// there is no more query options, resetting hasquery flag
url.hasquery = false;
}

// reset hasquery flag and remove addresshelper from URL
url.query.replace(pos, len, "");
return true;
}
Expand Down
34 changes: 23 additions & 11 deletions tests/Makefile
@@ -1,3 +1,5 @@
SYS := $(shell $(CXX) -dumpmachine)

CXXFLAGS += -Wall -Wno-unused-parameter -Wextra -pedantic -O0 -g -std=c++11 -D_GLIBCXX_USE_NANOSLEEP=1 -DOPENSSL_SUPPRESS_DEPRECATED -pthread -Wl,--unresolved-symbols=ignore-in-object-files
INCFLAGS += -I../libi2pd

Expand All @@ -7,41 +9,51 @@ TESTS = \
test-http-merge_chunked test-http-req test-http-res test-http-url test-http-url_decode \
test-gost test-gost-sig test-base-64 test-x25519 test-aeadchacha20poly1305 test-blinding test-elligator

ifneq (, $(findstring mingw, $(SYS))$(findstring windows-gnu, $(SYS))$(findstring cygwin, $(SYS)))
CXXFLAGS += -DWIN32_LEAN_AND_MEAN
LDFLAGS += -mwindows -static
BOOST_SUFFIX = -mt
NEEDED_LDLIBS = -lwsock32 -lws2_32 -lgdi32 -liphlpapi -lole32
endif

LDLIBS = \
-lcrypto \
-lboost_filesystem$(BOOST_SUFFIX) \
-lboost_program_options$(BOOST_SUFFIX) \
-lssl \
-lboost_filesystem \
-lboost_program_options \
-lcrypto \
-lz \
$(NEEDED_LDLIBS) \
-lpthread


all: $(TESTS) run

$(LIBI2PD):
@echo "Building libi2pd.a ..." && cd .. && $(MAKE) libi2pd.a

test-http-%: test-http-%.cpp $(LIBI2PD)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) -o $@ $^ $(LDLIBS)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)

test-base-%: test-base-%.cpp $(LIBI2PD)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) -o $@ $^ $(LDLIBS)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)

test-gost: test-gost.cpp $(LIBI2PD)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) -o $@ $^ $(LDLIBS)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)

test-gost-sig: test-gost-sig.cpp $(LIBI2PD)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) -o $@ $^ $(LDLIBS)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)

test-x25519: test-x25519.cpp $(LIBI2PD)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) -o $@ $^ $(LDLIBS)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)

test-aeadchacha20poly1305: test-aeadchacha20poly1305.cpp $(LIBI2PD)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) -o $@ $^ $(LDLIBS)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)

test-blinding: test-blinding.cpp $(LIBI2PD)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) -o $@ $^ $(LDLIBS)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)

test-elligator: test-elligator.cpp $(LIBI2PD)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) -o $@ $^ $(LDLIBS)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)

run: $(TESTS)
@for TEST in $(TESTS); do echo Running $$TEST; ./$$TEST ; done
Expand Down
9 changes: 9 additions & 0 deletions tests/test-http-url.cpp
Expand Up @@ -15,6 +15,7 @@ int main() {
assert(url->host == "127.0.0.1");
assert(url->port == 7070);
assert(url->path == "/asdasd");
assert(url->hasquery == true);
assert(url->query == "12345");
assert(url->to_string() == "https://127.0.0.1:7070/asdasd?12345");
delete url;
Expand All @@ -27,6 +28,7 @@ int main() {
assert(url->host == "site.com");
assert(url->port == 8080);
assert(url->path == "/asdasd");
assert(url->hasquery == true);
assert(url->query == "123456");
delete url;

Expand All @@ -38,6 +40,7 @@ int main() {
assert(url->host == "site.com");
assert(url->port == 0);
assert(url->path == "/asdasd");
assert(url->hasquery == true);
assert(url->query == "name=value");
delete url;

Expand All @@ -49,6 +52,7 @@ int main() {
assert(url->host == "site.com");
assert(url->port == 0);
assert(url->path == "/asdasd");
assert(url->hasquery == true);
assert(url->query == "name=value1&name=value2");
delete url;

Expand All @@ -60,6 +64,7 @@ int main() {
assert(url->host == "site.com");
assert(url->port == 0);
assert(url->path == "/asdasd");
assert(url->hasquery == true);
assert(url->query == "name1=value1&name2&name3=value2");
assert(url->parse_query(params));
assert(params.size() == 3);
Expand All @@ -79,6 +84,7 @@ int main() {
assert(url->host == "site.com");
assert(url->port == 800);
assert(url->path == "/asdasd");
assert(url->hasquery == true);
assert(url->query == "");
delete url;

Expand All @@ -90,6 +96,7 @@ int main() {
assert(url->host == "site.com");
assert(url->port == 17);
assert(url->path == "");
assert(url->hasquery == false);
assert(url->query == "");
delete url;

Expand All @@ -101,6 +108,7 @@ int main() {
assert(url->host == "site.com");
assert(url->port == 0);
assert(url->path == "");
assert(url->hasquery == false);
assert(url->query == "");
delete url;

Expand All @@ -112,6 +120,7 @@ int main() {
assert(url->host == "site.com");
assert(url->port == 84);
assert(url->path == "/asdasd/@17");
assert(url->hasquery == false);
assert(url->query == "");
assert(url->frag == "frag");
delete url;
Expand Down

0 comments on commit 77142e5

Please sign in to comment.