diff options
author | 0xbigshaq <57250448+0xbigshaq@users.noreply.github.com> | 2022-07-29 04:43:40 +0300 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2022-08-02 21:05:32 +0000 |
commit | af467344976f784fbb273b089fcb48bd80d674a4 (patch) | |
tree | 56f15e970fc151a29624e43330f81c4d021d8217 | |
parent | a4a7efaf5f7688a45a78da92a14b79e2ec11d60c (diff) | |
download | serenity-af467344976f784fbb273b089fcb48bd80d674a4.zip |
LibHTTP+WebServer: Add querystring support
Split the path from querystring when determining the requested resource.
-rw-r--r-- | Userland/Libraries/LibHTTP/HttpRequest.cpp | 12 | ||||
-rw-r--r-- | Userland/Services/WebServer/Client.cpp | 7 |
2 files changed, 15 insertions, 4 deletions
diff --git a/Userland/Libraries/LibHTTP/HttpRequest.cpp b/Userland/Libraries/LibHTTP/HttpRequest.cpp index 61072af838..bf74a0e800 100644 --- a/Userland/Libraries/LibHTTP/HttpRequest.cpp +++ b/Userland/Libraries/LibHTTP/HttpRequest.cpp @@ -182,8 +182,18 @@ Optional<HttpRequest> HttpRequest::from_raw_request(ReadonlyBytes raw_request) else return {}; - request.m_resource = URL::percent_decode(resource); request.m_headers = move(headers); + auto url_parts = resource.split_limit('?', 2, true); + + request.m_url.set_cannot_be_a_base_url(true); + if (url_parts.size() == 2) { + request.m_resource = url_parts[0]; + request.m_url.set_paths({ url_parts[0] }); + request.m_url.set_query(url_parts[1]); + } else { + request.m_resource = resource; + request.m_url.set_paths({ resource }); + } return request; } diff --git a/Userland/Services/WebServer/Client.cpp b/Userland/Services/WebServer/Client.cpp index e76376692a..c0fa3541fa 100644 --- a/Userland/Services/WebServer/Client.cpp +++ b/Userland/Services/WebServer/Client.cpp @@ -98,6 +98,7 @@ ErrorOr<bool> Client::handle_request(ReadonlyBytes raw_request) if (!request_or_error.has_value()) return false; auto& request = request_or_error.value(); + auto resource_decoded = URL::percent_decode(request.resource()); if constexpr (WEBSERVER_DEBUG) { dbgln("Got HTTP request: {} {}", request.method_name(), request.resource()); @@ -120,7 +121,7 @@ ErrorOr<bool> Client::handle_request(ReadonlyBytes raw_request) } } - auto requested_path = LexicalPath::join("/"sv, request.resource()).string(); + auto requested_path = LexicalPath::join("/"sv, resource_decoded).string(); dbgln_if(WEBSERVER_DEBUG, "Canonical requested path: '{}'", requested_path); StringBuilder path_builder; @@ -130,7 +131,7 @@ ErrorOr<bool> Client::handle_request(ReadonlyBytes raw_request) if (Core::File::is_directory(real_path)) { - if (!request.resource().ends_with('/')) { + if (!resource_decoded.ends_with('/')) { StringBuilder red; red.append(requested_path); @@ -362,7 +363,7 @@ ErrorOr<void> Client::send_error_response(unsigned code, HTTP::HttpRequest const void Client::log_response(unsigned code, HTTP::HttpRequest const& request) { - outln("{} :: {:03d} :: {} {}", Core::DateTime::now().to_string(), code, request.method_name(), request.resource()); + outln("{} :: {:03d} :: {} {}", Core::DateTime::now().to_string(), code, request.method_name(), request.url().serialize().substring(1)); } bool Client::verify_credentials(Vector<HTTP::HttpRequest::Header> const& headers) |