From 0db641014f9163df475018a618ceb8bdb01250d1 Mon Sep 17 00:00:00 2001 From: cflip Date: Mon, 19 Sep 2022 14:56:20 -0600 Subject: Implement the PATH_INFO and QUERY_STRING CGI environment variables This allows scripts to differentiate between the requested path and the query parameters. --- src/main.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index cd338b2..481dc41 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,10 +55,21 @@ static HttpResponse serve_from_cgi(const std::string& script_path, HttpRequest r HttpResponse response; response.add_header("Server", "cfws"); + // Split URI between the path and the query parameter string + std::stringstream uri_stream(request.uri()); + std::string segment; + std::vector segment_list; + + while (std::getline(uri_stream, segment, '?')) { + segment_list.push_back(segment); + } + CGIScript script(script_path); script.set_environment("REQUEST_METHOD", "GET"); script.set_environment("REQUEST_URI", request.uri().c_str()); - script.set_environment("PATH_INFO", request.uri().c_str()); + script.set_environment("PATH_INFO", segment_list[0].c_str()); + if (segment_list.size() > 1) + script.set_environment("QUERY_STRING", segment_list[1].c_str()); script.set_environment("CONTENT_LENGTH", "0"); if (!script.open()) { -- cgit v1.2.3