srctree

Robin Linden parent 9131254e 2984f423
protocol/http: Include query-parameters in requests

inlinesplit
protocol/http.cpp added: 24, removed: 3, total 21
@@ -43,7 +43,13 @@ bool Http::use_port(uri::Uri const &uri) {
 
std::string Http::create_get_request(uri::Uri const &uri, std::optional<std::string_view> user_agent) {
std::stringstream ss;
ss << fmt::format("GET {} HTTP/1.1\r\n", uri.path);
ss << fmt::format("GET {}", uri.path);
if (!uri.query.empty()) {
ss << '?' << uri.query;
}
 
ss << " HTTP/1.1\r\n";
 
if (Http::use_port(uri)) {
ss << fmt::format("Host: {}:{}\r\n", uri.authority.host, uri.authority.port);
} else {
 
protocol/http_test.cpp added: 24, removed: 3, total 21
@@ -13,6 +13,7 @@
#include <optional>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
 
using namespace std::string_view_literals;
@@ -304,5 +305,19 @@ int main() {
});
});
 
etest::test("query parameters are included", [] {
FakeSocket socket{};
auto uri = uri::Uri{
.uri = {"http://example.com/hello?target=world"},
.scheme = "http",
.authority = {.host = "example.com"},
.path = "/hello",
.query = "target=world",
};
std::ignore = protocol::Http::get(socket, uri, std::nullopt);
auto first_request_line = socket.write_data.substr(0, socket.write_data.find("\r\n"));
expect_eq(first_request_line, "GET /hello?target=world HTTP/1.1");
});
 
return etest::run_all_tests();
}