Skip to content

Commit

Permalink
updated PageCompiler
Browse files Browse the repository at this point in the history
  • Loading branch information
obiltschnig committed Oct 13, 2012
1 parent 045c23a commit 8f00fdb
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 48 deletions.
38 changes: 28 additions & 10 deletions PageCompiler/File2Page/src/File2Page.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// File2Page.cpp
//
// $Id: //poco/1.4/PageCompiler/File2Page/src/File2Page.cpp#3 $
// $Id: //poco/1.4/PageCompiler/File2Page/src/File2Page.cpp#4 $
//
// An application that creates a Page Compiler source file from an
// ordinary file.
Expand Down Expand Up @@ -88,46 +88,53 @@ class File2PageApp: public Application
Application::defineOptions(options);

options.addOption(
Option("help", "h", "display help information on command line arguments")
Option("help", "h", "Display help information on command line arguments.")
.required(false)
.repeatable(false)
.callback(OptionCallback<File2PageApp>(this, &File2PageApp::handleHelp)));

options.addOption(
Option("contentType", "t", "specify a content type")
Option("contentType", "t", "Specify a content type.")
.required(false)
.repeatable(false)
.argument("MIME-Type")
.callback(OptionCallback<File2PageApp>(this, &File2PageApp::handleContentType)));

options.addOption(
Option("contentLanguage", "l", "specify a content language")
Option("contentLanguage", "l", "Specify a content language.")
.required(false)
.repeatable(false)
.argument("language")
.callback(OptionCallback<File2PageApp>(this, &File2PageApp::handleContentLang)));

options.addOption(
Option("class", "c", "specify the handler class name")
Option("class", "c", "Specify the handler class name.")
.required(false)
.repeatable(false)
.argument("class-name")
.callback(OptionCallback<File2PageApp>(this, &File2PageApp::handleClassName)));

options.addOption(
Option("namespace", "n", "specify the handler class namespace name")
Option("namespace", "n", "Specify the handler class namespace name.")
.required(false)
.repeatable(false)
.argument("namespace-name")
.callback(OptionCallback<File2PageApp>(this, &File2PageApp::handleNamespace)));

options.addOption(
Option("output", "o", "specify the output file name")
Option("output", "o", "Specify the output file name.")
.required(false)
.repeatable(false)
.argument("path")
.callback(OptionCallback<File2PageApp>(this, &File2PageApp::handleOutput)));
}

options.addOption(
Option("path", "p", "Specify the server path of the file.")
.required(false)
.repeatable(false)
.argument("path")
.callback(OptionCallback<File2PageApp>(this, &File2PageApp::handlePath)));
}

void handleHelp(const std::string& name, const std::string& value)
{
Expand Down Expand Up @@ -161,12 +168,18 @@ class File2PageApp: public Application
_output = value;
}

void handlePath(const std::string& name, const std::string& value)
{
_path = value;
}

void displayHelp()
{
HelpFormatter helpFormatter(options());
helpFormatter.setCommand(commandName());
helpFormatter.setUsage("OPTIONS");
helpFormatter.setHeader("Create a PageCompiler source file from a binary file.");
helpFormatter.setIndent(8);
helpFormatter.format(std::cout);
}

Expand Down Expand Up @@ -200,8 +213,12 @@ class File2PageApp: public Application
}
ostr << " form=\"false\"\n"
<< " namespace=\"" << _namespace << "\"\n"
<< " class=\"" << _clazz << "\"\n"
<< " precondition=\"checkModified(request)\"%><%@"
<< " class=\"" << _clazz << "\"\n";
if (!_path.empty())
{
ostr << " path=\"" << _path << "\"\n";
}
ostr << " precondition=\"checkModified(request)\"%><%@"
<< " impl include=\"Poco/DateTime.h\"\n"
<< " include=\"Poco/DateTimeParser.h\"\n"
<< " include=\"Poco/DateTimeFormatter.h\"\n"
Expand Down Expand Up @@ -292,6 +309,7 @@ class File2PageApp: public Application
std::string _clazz;
std::string _namespace;
std::string _output;
std::string _path;
};


Expand Down
42 changes: 33 additions & 9 deletions PageCompiler/doc/PageCompilerUserGuide.page
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,28 @@ Allows you to specify whether the response is sent using chunked transfer encodi
Defaults to <[true]>.
Set the value to <[false]> to disable chunked transfer encoding.

!compressed

Enables or disables response body compression. If set to <[true]>, and the client supports
the "gzip" content encoding (indicated by the "Accept-Encoding" header),
the response body will be compressed using the "gzip" format and the
"Content-Encoding" header will be set accordingly.
Defaults to <[false]>. Cannot be enabled together with response buffering.

!compressionLevel

Specify the compression level, if response body compression is enabled.
Valid values are 1 (fastest) to 9 (best compression). Defaults to 1.

!buffered

Enables or disables response buffering. Response buffering is disabled by default.
Set to <[true]> to enable buffering, or to <[false]> to disable it.
If response buffering is enabled, everything written to the response stream
is actually written to a string stream (<[std::ostringstream]>).
Sending of the HTTP response back to the client is deferred to
when the page is complete.

!session (OSP only)

For use with the POCO Open Service Platform only.
Expand Down Expand Up @@ -273,15 +295,6 @@ a new session will be created if the request does not contain a (valid) session
If set to <[false]> and there is no existing session that matches the session
cookie. the <[session]> variable will be null.

!buffered

Enables or disables response buffering. Response buffering is disabled by default.
Set to <[true]> to enable buffering, or to <[false]> to disable it.
If response buffering is enabled, everything written to the response stream
is actually written to a string stream (<[std::ostringstream]>).
Sending of the HTTP response back to the client is deferred to
when the page is complete.

!precondition

Allows to specify a C++ boolean expression which will be evaluated before processing
Expand All @@ -295,6 +308,17 @@ Example:
<%@ page precondition="checkCredentials(request, response)" %>
----

!path

Specify a server path for the page. If specified, the generated handler class
will contain a public static <[const std::string]> member variable named <[PATH]>
containing the specified path.

Example:
<%@ page path="/index.html" %>
----


!!Implicit Objects

The following objects are available in the handler code.
Expand Down
46 changes: 44 additions & 2 deletions PageCompiler/src/CodeWriter.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// CodeWriter.cpp
//
// $Id: //poco/1.4/PageCompiler/src/CodeWriter.cpp#2 $
// $Id: //poco/1.4/PageCompiler/src/CodeWriter.cpp#3 $
//
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
Expand Down Expand Up @@ -34,6 +34,7 @@
#include "Page.h"
#include "Poco/Path.h"
#include "Poco/StringTokenizer.h"
#include "Poco/String.h"


using Poco::Path;
Expand Down Expand Up @@ -76,6 +77,10 @@ void CodeWriter::writeImpl(std::ostream& ostr, const std::string& headerFileName
{
ostr << "#include \"" << headerFileName << "\"\n";
writeImplIncludes(ostr);
if (_page.getBool("page.compressed", false))
{
ostr << "#include \"Poco/DeflatingStream.h\"\n";
}
if (_page.getBool("page.buffered", false))
{
ostr << "#include \"Poco/StreamCopier.h\"\n";
Expand All @@ -90,6 +95,13 @@ void CodeWriter::writeImpl(std::ostream& ostr, const std::string& headerFileName
}

beginNamespace(ostr);

std::string path = _page.get("page.path", "");
if (!path.empty())
{
ostr << "\tconst std::string " << _class << "::PATH(\"" << path << "\");\n\n\n";
}

writeConstructor(ostr);
writeHandler(ostr);
writeFactory(ostr);
Expand Down Expand Up @@ -132,7 +144,9 @@ void CodeWriter::beginGuard(std::ostream& ostr, const std::string& headerFileNam
{
Path p(headerFileName);
std::string guard(p.getBaseName());
Poco::translateInPlace(guard, ".-", "__");
guard += "_INCLUDED";

ostr << "#ifndef " << guard << "\n";
ostr << "#define " << guard << "\n";
ostr << "\n\n";
Expand All @@ -143,6 +157,7 @@ void CodeWriter::endGuard(std::ostream& ostr, const std::string& headerFileName)
{
Path p(headerFileName);
std::string guard(p.getBaseName());
Poco::translateInPlace(guard, ".-", "__");
guard += "_INCLUDED";
ostr << "\n\n";
ostr << "#endif // " << guard << "\n";
Expand All @@ -164,6 +179,13 @@ void CodeWriter::handlerClass(std::ostream& ostr, const std::string& base, const
}
ostr << "\tvoid handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response);\n";
writeHandlerMembers(ostr);

std::string path = _page.get("page.path", "");
if (!path.empty())
{
ostr << "\n\tstatic const std::string PATH;\n";
}

ostr << "};\n";
}

Expand Down Expand Up @@ -313,7 +335,10 @@ void CodeWriter::writeResponse(std::ostream& ostr)
std::string contentLang(_page.get("page.contentLanguage", ""));
bool buffered(_page.getBool("page.buffered", false));
bool chunked(_page.getBool("page.chunked", !buffered));

bool compressed(_page.getBool("page.compressed", false));
if (buffered) compressed = false;
if (compressed) chunked = true;

if (chunked)
{
ostr << "\tresponse.setChunkedTransferEncoding(true);\n";
Expand All @@ -325,6 +350,11 @@ void CodeWriter::writeResponse(std::ostream& ostr)
ostr << "\tif (request.has(\"Accept-Language\"))\n"
<< "\t\tresponse.set(\"Content-Language\", \"" << contentLang << "\");\n";
}
if (compressed)
{
ostr << "\tbool _compressResponse(request.hasToken(\"Accept-Encoding\", \"gzip\"));\n"
<< "\tif (_compressResponse) response.set(\"Content-Encoding\", \"gzip\");\n";
}
ostr << "\n";
}

Expand All @@ -333,6 +363,10 @@ void CodeWriter::writeContent(std::ostream& ostr)
{
bool buffered(_page.getBool("page.buffered", false));
bool chunked(_page.getBool("page.chunked", !buffered));
bool compressed(_page.getBool("page.compressed", false));
int compressionLevel(_page.getInt("page.compressionLevel", 1));
if (buffered) compressed = false;
if (compressed) chunked = true;

if (buffered)
{
Expand All @@ -344,6 +378,14 @@ void CodeWriter::writeContent(std::ostream& ostr)
}
ostr << "\tPoco::StreamCopier::copyStream(responseStream, response.send());\n";
}
else if (compressed)
{
ostr << "\tstd::ostream& _responseStream = response.send();\n"
<< "\tPoco::DeflatingOutputStream _gzipStream(_responseStream, Poco::DeflatingStreamBuf::STREAM_GZIP, " << compressionLevel << ");\n"
<< "\tstd::ostream& responseStream = _compressResponse ? _gzipStream : _responseStream;\n";
ostr << _page.handler().str();
ostr << "\tif (_compressResponse) _gzipStream.close();\n";
}
else
{
ostr << "\tstd::ostream& responseStream = response.send();\n";
Expand Down
35 changes: 23 additions & 12 deletions PageCompiler/src/OSPCodeWriter.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// OSPCodeWriter.cpp
//
// $Id: //poco/1.4/PageCompiler/src/OSPCodeWriter.cpp#2 $
// $Id: //poco/1.4/PageCompiler/src/OSPCodeWriter.cpp#3 $
//
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
Expand Down Expand Up @@ -64,15 +64,19 @@ void OSPCodeWriter::writeHandlerClass(std::ostream& ostr)

void OSPCodeWriter::writeHandlerMembers(std::ostream& ostr)
{
ostr << "\n";
ostr << "protected:\n";
ostr << "\tPoco::OSP::BundleContext::Ptr context() const\n";
ostr << "\t{\n";
ostr << "\t\treturn _pContext;\n";
ostr << "\t}\n";
ostr << "\n";
ostr << "private:\n";
ostr << "\tPoco::OSP::BundleContext::Ptr _pContext;\n";
std::string base(page().get("page.baseClass", ""));
if (base.empty())
{
ostr << "\n";
ostr << "protected:\n";
ostr << "\tPoco::OSP::BundleContext::Ptr context() const\n";
ostr << "\t{\n";
ostr << "\t\treturn _pContext;\n";
ostr << "\t}\n";
ostr << "\n";
ostr << "private:\n";
ostr << "\tPoco::OSP::BundleContext::Ptr _pContext;\n";
}
}


Expand All @@ -97,9 +101,16 @@ void OSPCodeWriter::writeImplIncludes(std::ostream& ostr)

void OSPCodeWriter::writeConstructor(std::ostream& ostr)
{
std::string base(page().get("page.baseClass", "Poco::Net::HTTPRequestHandler"));
std::string base(page().get("page.baseClass", ""));
ostr << clazz() << "::" << clazz() << "(Poco::OSP::BundleContext::Ptr pContext):\n";
ostr << "\t_pContext(pContext)\n";
if (base.empty())
{
ostr << "\t_pContext(pContext)\n";
}
else
{
ostr << "\t" << base << "(pContext)\n";
}
ostr << "{\n}\n";
ostr << "\n\n";
}
Expand Down
13 changes: 12 additions & 1 deletion PageCompiler/src/Page.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// Page.cpp
//
// $Id: //poco/1.4/PageCompiler/src/Page.cpp#1 $
// $Id: //poco/1.4/PageCompiler/src/Page.cpp#2 $
//
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
Expand Down Expand Up @@ -32,6 +32,7 @@

#include "Page.h"
#include "Poco/String.h"
#include "Poco/NumberParser.h"


Page::Page()
Expand All @@ -55,3 +56,13 @@ bool Page::getBool(const std::string& property, bool deflt) const
}
else return deflt;
}


int Page::getInt(const std::string& property, int deflt) const
{
if (has(property))
{
return Poco::NumberParser::parse(get(property));
}
else return deflt;
}
Loading

0 comments on commit 8f00fdb

Please sign in to comment.