Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PageCompiler: Extracted page write from handleRequest #3919

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from

Conversation

Fabio3rs
Copy link
Contributor

Hi.
I would like to use the Poco PageCompiler to create HTML emails, so I made these tweaks. I hope you will find it useful.

Thanks.

Example:
Input page template

<%@ page class="TimeHandler" %>
<%!
    #include "Poco/DateTime.h"
    #include "Poco/DateTimeFormatter.h"
    #include "Poco/DateTimeFormat.h"


    using Poco::DateTime;
    using Poco::DateTimeFormatter;
    using Poco::DateTimeFormat;
%><%
    DateTime now;
    std::string dt(DateTimeFormatter::format(now, DateTimeFormat::SORTABLE_FORMAT));
%>
<html>
<head>
<title>Time Sample</title>
</head>
<body>
<h1>Time Sample</h1>
<p><%= dt %></p>
</body>
</html>

Output header TimeHandler.h:

#ifndef TimeHandler_INCLUDED
#define TimeHandler_INCLUDED


#include "Poco/Net/HTTPRequestHandler.h"
namespace Poco {
namespace Net {
class HTMLForm;
}
}


class TimeHandler: public Poco::Net::HTTPRequestHandler
{
public:
	void handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response) override;
	void stringify(std::ostream &responseStream, Poco::Net::HTMLForm &form);
};


#endif // TimeHandler_INCLUDED

Ouput source TimeHandler.cpp:

#include "TimeHandler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/HTMLForm.h"
#line 2 "/mnt/projects/Projects/poco/build/bin/aaa/page.cpsp"

    #include "Poco/DateTime.h"
    #include "Poco/DateTimeFormatter.h"
    #include "Poco/DateTimeFormat.h"


    using Poco::DateTime;
    using Poco::DateTimeFormatter;
    using Poco::DateTimeFormat;


using namespace std::string_literals;


void TimeHandler::handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
{
	response.setChunkedTransferEncoding(true);
	response.setContentType("text/html"s);

	Poco::Net::HTMLForm form(request, request.stream());
	std::ostream& responseStream = response.send();
	stringify(responseStream, form);
}

void TimeHandler::stringify(std::ostream &responseStream, [[maybe_unused]] Poco::Net::HTMLForm &form)
{
	responseStream << "\n";
#line 11 "/mnt/projects/Projects/poco/build/bin/aaa/page.cpsp"

    DateTime now;
    std::string dt(DateTimeFormatter::format(now, DateTimeFormat::SORTABLE_FORMAT));
	responseStream << "\n";
	responseStream << "<html>\n";
	responseStream << "<head>\n";
	responseStream << "<title>Time Sample</title>\n";
	responseStream << "</head>\n";
	responseStream << "<body>\n";
	responseStream << "<h1>Time Sample</h1>\n";
	responseStream << "<p>";
#line 21 "/mnt/projects/Projects/poco/build/bin/aaa/page.cpsp"
	responseStream << ( dt );
	responseStream << "</p>\n";
	responseStream << "</body>\n";
	responseStream << "</html>\n";
	responseStream << "\n";
}

Example use with MailMessage:


        Poco::Net::MailMessage message;

        std::stringstream sstr;

        TimeHandler msg;
        Poco::Net::HTMLForm form;
        msg.stringify(sstr,  form);
        
        message.setContentType("text/html; charset=UTF-8");
        message.setContent(sstr.str(), Poco::Net::MailMessage::ENCODING_8BIT);

@aleks-f aleks-f requested a review from obiltschnig June 10, 2023 03:08
@matejk matejk deleted the branch pocoproject:devel April 15, 2024 11:20
@matejk matejk closed this Apr 15, 2024
@matejk matejk reopened this Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants