Project

General

Profile

Send HTTP status from app

Added by El SysMan over 2 years ago

Hello!

A newbie question: how to send HTTP status from the :

HelloApplication::HelloApplication(const Wt::WEnvironment& env)
: WApplication(env)
{

what piece of code must ne here ?

}

Thanks in advance.


Replies (3)

RE: Send HTTP status from app - Added by Plug Gulp over 2 years ago

If you could explain what HTTP status you are trying to send to the client and why, then it will be easy to provide some solution.

Wt, out of the box, works a bit differently. It mimics a desktop app development experience and behaviour as well. So if you are using Wt as it is meant to be used, then sending HTTP status to client should not be required. Wt handles all the magic for you, as if it is a desktop app!

Now, if you want to develop your web app in a traditional Web style way, then that is possible by using "static resource". Check documentation for WResource and WServer classes for details. In that style of development, you inherit/subclass from the WResource class and implement the handleRequest method. In that method you get Http::Request and Http::Response objects. You can set the HTTP response status in the HTTP::Response class. Once you have the request handler resource class ready, then you register instance of that resource class with WServer using its method addResource. For an example of how this is all done, check the urlparams example that is available with Wt distribution. The file that will be of interest to you is ...//examples/feature/urlparams/urlparams.C

To return HTTP status to the client, take a look at the documentation of the method Wt::Http::Response::setStatus

RE: Send HTTP status from app - Added by El SysMan over 2 years ago

Hi !
Thanks for the answer.

I want to check client's IP and send back 403 if client's IP don't match a local one.

RE: Send HTTP status from app - Added by Korneel Dumon over 2 years ago

If you really want to return a 403 status code, then you will have to add reverse proxy that filters on the client IP.

An alternative is to show a message as part of the application. To fill in the code from your original question:

if (!isAllowedIP(env.clientAddress())) {
  root()->addNew<Wt::WText>("Access Denied");
}

(see WEnvironment::clientAddress)

    (1-3/3)