From 02be1accd2541623a8b066b9f4d121d2337485e6 Mon Sep 17 00:00:00 2001 From: randomdan Date: Thu, 21 Feb 2013 00:51:41 +0000 Subject: Simple support for redirect on request complete --- project2/cgi/cgiAppEngine.h | 3 ++- project2/cgi/cgiStageRequest.cpp | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/project2/cgi/cgiAppEngine.h b/project2/cgi/cgiAppEngine.h index 147ebe8..50c9a7f 100644 --- a/project2/cgi/cgiAppEngine.h +++ b/project2/cgi/cgiAppEngine.h @@ -92,7 +92,8 @@ class CgiApplicationEngine : public ApplicationEngine, public TransformChainLink virtual NextStage run(); virtual HttpHeaderPtr getHeader() const; protected: - std::string present; + Variable present; + Variable redirect; }; /// Stage to process GET requests and follow up RequestStages diff --git a/project2/cgi/cgiStageRequest.cpp b/project2/cgi/cgiStageRequest.cpp index 967365a..8fa06bf 100644 --- a/project2/cgi/cgiStageRequest.cpp +++ b/project2/cgi/cgiStageRequest.cpp @@ -11,7 +11,8 @@ CgiApplicationEngine::RequestStage::RequestStage(ScriptReaderPtr s) : ::CheckHost(s->root()), CgiApplicationEngine::ResponseStage(s->root()), ::TaskHost(s->root()), - present(s->root()->value("present","").as()) + present(s->root(), "present", Null()), + redirect(s->root(), "redirect", Null()) { } @@ -20,13 +21,23 @@ CgiApplicationEngine::RequestStage::run() { runChecks(); execute(); - return NextStage(present.empty() ? NULL : new PresentStage(env()->resolveScript(env()->presentRoot, present, false)), this); + if (!present().isNull()) { + return NextStage(new PresentStage(env()->resolveScript(env()->presentRoot, present(), false)), this); + } + return NextStage(NULL, this); } CgiApplicationEngine::HttpHeaderPtr CgiApplicationEngine::RequestStage::getHeader() const { - Project2HttpHeader * header = new Project2HttpHeader("200 OK"); + Project2HttpHeader * header; + if (redirect().isNull()) { + header = new Project2HttpHeader("200 OK"); + } + else { + header = new Project2HttpHeader("301 Moved Permanently"); + header->addHeader("Location", redirect()); + } header->addHeader("Cache-control", "no-cache"); return HttpHeaderPtr(header); } -- cgit v1.2.3