Overview
Features
Download
Documentation
Community
Add-Ons & Services

httpformserver questions

Please post support and help requests here.

Re: httpformserver questions

Postby Sqarzz » 15 Apr 2012, 20:24

in the main HTMLFormServer class, a connection is established:
Code: Select all
// set-up a server socket
ServerSocket svs(port);
// set-up a HTTPServer instance
HTTPServer srv(new FormRequestHandlerFactory, svs, new HTTPServerParams);
// start the HTTPServer
srv.start();
// wait for CTRL-C or kill
waitForTerminationRequest();
// Stop the HTTPServer
srv.stop();


The new "FormRequestHandlerFactory" object that is created, will return a specific requesthandler based on the url of the resulting request.
Code: Select all
class FormRequestHandlerFactory: public HTTPRequestHandlerFactory
{public:
FormRequestHandlerFactory()   {   }
HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
{
if (request.getURI() == "/")   return new FormRequestHandler;
else return new LoginHandler;
}
};

rakesh wrote:It would be more efficient to directly call your other form handlers directly after setting any relevant data member values.

How would this be done? Do you have to make a new HTTPServer object as in the fist piece of code is done (HTTPServer srv2(new Form1Handler, svs, new HTTPServerParams). Or do you first have to terminate the original HTTPServer srv object.
I am basically stuck on how to call a formhandler inside an actual handler..
thanks
Sqarzz
 
Posts: 24
Joined: 09 Apr 2012, 11:11
Location: Belgium

Re: httpformserver questions

Postby fbraem » 15 Apr 2012, 21:48

Like you create FormRequestHandler in the factory, you can always create another handler directly in your handler and call the handleRequest method yourself.

Code: Select all
FormRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response)
{
 ... do some work
 Form2RequestHandler form2;
 form2.handleRequest(request, response); // Forward request to Form2RequestHandler
}
Zumuta!, that's the way to do IT!
fbraem
 
Posts: 96
Joined: 11 Aug 2008, 22:47
Location: Belgium

Re: httpformserver questions

Postby Sqarzz » 16 Apr 2012, 00:08

I have put it inside the first formrequesthandler as you suggested but it keeps crashing.
In essence, this is what i did (see at the end of this code chunck):
Code: Select all
class FormRequestHandler: public HTTPRequestHandler
{
public:
   FormRequestHandler() {}
   
   void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
   {
      Application& app = Application::instance();
      app.logger().information("Request from " + request.clientAddress().toString());
      HTMLForm form(request, request.stream());
      response.setChunkedTransferEncoding(true);
      response.setContentType("text/html");
      std::ostream& ostr = response.send();
      ostr <<
         ...
         "<form method=\"POST\" action=\"/\">\n"
         "Number: <input type=\"text\" name=\"number\" size=\"15\">\n"
         "<input type=\"submit\" value=\"INPUT\">\n"
         "</form>\n";
         ...
      NameValueCollection::ConstIterator it = request.begin();
      NameValueCollection::ConstIterator end = request.end();

string temp1, temp2, num;
if (!form.empty())
{
   it = form.begin();
   end = form.end();
   for (; it != end; ++it)
   {
      temp1=it->first;
      temp2=it->second;
      if (temp1=="number") {num=temp2;}
if (num.empty()) {ostr<<"No input!\n";}
else if (num=="1") {

Form2RequestHandler test;
test.handleRequest(request, response);
      
         }
         


The error i get:

Assertion violation: !_pStream [in file "c:\poco-1.4.3p1\net\src\httpserverresponseimpl.cpp", line 89]
this would be the line: poco_assert (!_pStream); in the function std::ostream& HTTPServerResponseImpl::send()

Do I maybe have to change the parameters in test.handleRequest?

Or maybe because I call the handler within the void handleRequest of the first handler?
I would guess I have to call it inhere or else response and request are not defined.

thanks guys!
Sqarzz
 
Posts: 24
Joined: 09 Apr 2012, 11:11
Location: Belgium

Re: httpformserver questions

Postby Sqarzz » 17 Apr 2012, 14:25

About my current error:

Sqarzz wrote:Assertion violation: !_pStream [in file "c:\poco-1.4.3p1\net\src\httpserverresponseimpl.cpp", line 89]
this would be the line: poco_assert (!_pStream); in the function std::ostream& HTTPServerResponseImpl::send()


fbraem suggested that the issue means the stream is closed to soon.

fbraem wrote:
Sqarzz wrote:poco_assert (!_pStream) gives me the error.

It means that _pStream is pointing to NULL. Probably the stream is closed and you are still trying to use it ...


I tried tracking down the issue using breakpoints. The error takes place in the second form,
right after the line: std::ostream& ostr = response.send();
But this line is needed for outputting the stream right?

so in short:
The first form where i request the second form.
Code: Select all
class FormRequestHandler: public HTTPRequestHandler
{
public:
   FormRequestHandler() {}
   
   void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
   {
      Application& app = Application::instance();
      app.logger().information("Request from " + request.clientAddress().toString());
      HTMLForm form(request, request.stream());
      response.setChunkedTransferEncoding(true);
      response.setContentType("text/html");
      std::ostream& ostr = response.send();
      ostr << "<html>\n<head>\n<title>TEST</title>\n</head>\n<body>\n<h2>FORM1</h2>\n</form>\n</body>\n";
      Form2RequestHandler test;
      test.handleRequest(request, response);
     }
};


The second form:
Code: Select all
class Form2RequestHandler: public HTTPRequestHandler
{
public:
   Form2RequestHandler() {}
   
   void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
   {
      Application& app = Application::instance();
      app.logger().information("Request from " + request.clientAddress().toString());
      HTMLForm form(request, request.stream());
      response.setChunkedTransferEncoding(true);
      response.setContentType("text/html");

std::ostream& ostr = response.send(); //after this line I get the error.
Code: Select all
      ostr << "<html>\n<head>\n<title>TEST</title>\n</head>\n<body>\n<h2>FORM2</h2>\n</form>\n</body>\n";         
   }
};


Does this mean I am doing something i shouldn't do in form2?
Sqarzz
 
Posts: 24
Joined: 09 Apr 2012, 11:11
Location: Belgium

Re: httpformserver questions

Postby fbraem » 17 Apr 2012, 16:00

Can form2 be accessed directly? If not, don't use a second form handler and do everything in one form handler.
Otherwise, add a method to the handler of form2 with a HTMLForm and outputstream as arguments and pass them from the form1 handler.

You are now setting headers in form2 which can't work because you already send some data to the response in form1. And creating a new HTMLForm in the form2 handler will not work either because the input stream is already handled in form1.
Zumuta!, that's the way to do IT!
fbraem
 
Posts: 96
Joined: 11 Aug 2008, 22:47
Location: Belgium

Re: httpformserver questions

Postby Sqarzz » 18 Apr 2012, 21:32

fbraem wrote:Can form2 be accessed directly? If not, don't use a second form handler and do everything in one form handler.

My main goal is to make formhandler an access control. If the data from form1 is correct, then a second page (form2handler) has te be opened.
because my c++ isn't that good yet, and I don't eally have a great overview on how the handlers work, I will try to make the html output depend of an internal if-structure,
and test if there is form data before the ostr stream is outputted.
Thanx for your answers and your time fbraem and rakesh!
Sqarzz
 
Posts: 24
Joined: 09 Apr 2012, 11:11
Location: Belgium

Re: httpformserver questions

Postby Sqarzz » 19 Apr 2012, 17:05

I would like to make a form where there is only a submit, and no input.
I would like to use a button to do an action, and add some data to the form myself : form.add("text","data"):
How could I check if the submit button was pressed and how could i add the form data ONLY if a specific submit was pushed.



EDIT:

Fixed by adding a name tag to the form, then even without other dat, the form outputs the name as an element of the form
Sqarzz
 
Posts: 24
Joined: 09 Apr 2012, 11:11
Location: Belgium

Re: httpformserver questions

Postby rakesh » 20 Apr 2012, 05:08

If you are planning to create non-trivial HTML output, you should take a look at the Page Compiler and CSP.
rakesh
 
Posts: 78
Joined: 13 Apr 2011, 17:43
Location: Chicago

Re: httpformserver questions

Postby Sqarzz » 23 Apr 2012, 15:05

Thanx for the suggestion rakesh
Sqarzz
 
Posts: 24
Joined: 09 Apr 2012, 11:11
Location: Belgium

Re: httpformserver questions

Postby Sqarzz » 25 Apr 2012, 18:26

My HTMLformserver now runs the way I want it to.
Next step would be to allow for multiple clients to connect to it at the same time,
but each user having another instance of the application.
Also I would like to be able to be able to call other functions to the console.
Problem is that when i call:
Code: Select all
HtmlServer app;
app.run(argc, argv);

the application is paused while the httpconnection is up.

I guess that means the application should run multithreated..
I would like to start with multithreading the htmlserver connections.
Are there any examples in the application that are relevant for my next step?

If this succeeds I will go on with making the application itself multithreaded.
Sqarzz
 
Posts: 24
Joined: 09 Apr 2012, 11:11
Location: Belgium

PreviousNext

Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron