Browse Source

[http] fix query detection, addresshelper processing, update tests

Signed-off-by: R4SAS <r4sas@i2pmail.org>
pull/1881/head
R4SAS 2 years ago
parent
commit
77142e59ee
Signed by: r4sas
GPG Key ID: 66F6C87B98EBCFE2
  1. 2
      libi2pd/HTTP.h
  2. 28
      libi2pd_client/HTTPProxy.cpp
  3. 34
      tests/Makefile
  4. 9
      tests/test-http-url.cpp

2
libi2pd/HTTP.h

@ -37,7 +37,7 @@ namespace http
std::string query; std::string query;
std::string frag; 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 * @brief Tries to parse url from string

28
libi2pd_client/HTTPProxy.cpp

@ -238,9 +238,33 @@ namespace proxy {
std::string value = params["i2paddresshelper"]; std::string value = params["i2paddresshelper"];
len += value.length(); len += value.length();
b64 = i2p::http::UrlDecode(value); b64 = i2p::http::UrlDecode(value);
// if we need update exists, request formed with update param // if we need update exists, request formed with update param
if (params["update"] == "true") { len += std::strlen("&update=true"); confirm = true; } if (params["update"] == "true")
if (pos != 0 && url.query[pos-1] == '&') { pos--; len++; } // if helper is not only one query option {
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, ""); url.query.replace(pos, len, "");
return true; return true;
} }

34
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 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 INCFLAGS += -I../libi2pd
@ -7,41 +9,51 @@ TESTS = \
test-http-merge_chunked test-http-req test-http-res test-http-url test-http-url_decode \ 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 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 = \ LDLIBS = \
-lcrypto \ -lboost_filesystem$(BOOST_SUFFIX) \
-lboost_program_options$(BOOST_SUFFIX) \
-lssl \ -lssl \
-lboost_filesystem \ -lcrypto \
-lboost_program_options \ -lz \
$(NEEDED_LDLIBS) \
-lpthread -lpthread
all: $(TESTS) run all: $(TESTS) run
$(LIBI2PD): $(LIBI2PD):
@echo "Building libi2pd.a ..." && cd .. && $(MAKE) libi2pd.a @echo "Building libi2pd.a ..." && cd .. && $(MAKE) libi2pd.a
test-http-%: test-http-%.cpp $(LIBI2PD) 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) 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) 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) 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) 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) 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) 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) test-elligator: test-elligator.cpp $(LIBI2PD)
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) -o $@ $^ $(LDLIBS) $(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
run: $(TESTS) run: $(TESTS)
@for TEST in $(TESTS); do echo Running $$TEST; ./$$TEST ; done @for TEST in $(TESTS); do echo Running $$TEST; ./$$TEST ; done

9
tests/test-http-url.cpp

@ -15,6 +15,7 @@ int main() {
assert(url->host == "127.0.0.1"); assert(url->host == "127.0.0.1");
assert(url->port == 7070); assert(url->port == 7070);
assert(url->path == "/asdasd"); assert(url->path == "/asdasd");
assert(url->hasquery == true);
assert(url->query == "12345"); assert(url->query == "12345");
assert(url->to_string() == "https://127.0.0.1:7070/asdasd?12345"); assert(url->to_string() == "https://127.0.0.1:7070/asdasd?12345");
delete url; delete url;
@ -27,6 +28,7 @@ int main() {
assert(url->host == "site.com"); assert(url->host == "site.com");
assert(url->port == 8080); assert(url->port == 8080);
assert(url->path == "/asdasd"); assert(url->path == "/asdasd");
assert(url->hasquery == true);
assert(url->query == "123456"); assert(url->query == "123456");
delete url; delete url;
@ -38,6 +40,7 @@ int main() {
assert(url->host == "site.com"); assert(url->host == "site.com");
assert(url->port == 0); assert(url->port == 0);
assert(url->path == "/asdasd"); assert(url->path == "/asdasd");
assert(url->hasquery == true);
assert(url->query == "name=value"); assert(url->query == "name=value");
delete url; delete url;
@ -49,6 +52,7 @@ int main() {
assert(url->host == "site.com"); assert(url->host == "site.com");
assert(url->port == 0); assert(url->port == 0);
assert(url->path == "/asdasd"); assert(url->path == "/asdasd");
assert(url->hasquery == true);
assert(url->query == "name=value1&name=value2"); assert(url->query == "name=value1&name=value2");
delete url; delete url;
@ -60,6 +64,7 @@ int main() {
assert(url->host == "site.com"); assert(url->host == "site.com");
assert(url->port == 0); assert(url->port == 0);
assert(url->path == "/asdasd"); assert(url->path == "/asdasd");
assert(url->hasquery == true);
assert(url->query == "name1=value1&name2&name3=value2"); assert(url->query == "name1=value1&name2&name3=value2");
assert(url->parse_query(params)); assert(url->parse_query(params));
assert(params.size() == 3); assert(params.size() == 3);
@ -79,6 +84,7 @@ int main() {
assert(url->host == "site.com"); assert(url->host == "site.com");
assert(url->port == 800); assert(url->port == 800);
assert(url->path == "/asdasd"); assert(url->path == "/asdasd");
assert(url->hasquery == true);
assert(url->query == ""); assert(url->query == "");
delete url; delete url;
@ -90,6 +96,7 @@ int main() {
assert(url->host == "site.com"); assert(url->host == "site.com");
assert(url->port == 17); assert(url->port == 17);
assert(url->path == ""); assert(url->path == "");
assert(url->hasquery == false);
assert(url->query == ""); assert(url->query == "");
delete url; delete url;
@ -101,6 +108,7 @@ int main() {
assert(url->host == "site.com"); assert(url->host == "site.com");
assert(url->port == 0); assert(url->port == 0);
assert(url->path == ""); assert(url->path == "");
assert(url->hasquery == false);
assert(url->query == ""); assert(url->query == "");
delete url; delete url;
@ -112,6 +120,7 @@ int main() {
assert(url->host == "site.com"); assert(url->host == "site.com");
assert(url->port == 84); assert(url->port == 84);
assert(url->path == "/asdasd/@17"); assert(url->path == "/asdasd/@17");
assert(url->hasquery == false);
assert(url->query == ""); assert(url->query == "");
assert(url->frag == "frag"); assert(url->frag == "frag");
delete url; delete url;

Loading…
Cancel
Save