Project

General

Profile

Bug #1383 ยป bug_header.cpp

source code to test the bug - Thomas Saquet, 08/01/2012 04:08 PM

 
#include <Wt/WServer>
#include <Wt/Http/Client>
#include <Wt/Http/Response>
#include <Wt/WLogger>
#include <Wt/Utils>
#include <Wt/WResource>

class ExampleGoodRequest : public Wt::WResource
{
public:
virtual ~ExampleGoodRequest()
{
beingDeleted();
}

protected:
virtual void handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response)
{
Wt::Http::Client *client = new Wt::Http::Client(this);
client->done().connect(boost::bind(&ExampleGoodRequest::handle, this, _1, _2));

std::string bodyText = "param=value";
Wt::Http::Message message;
message.addBodyText(bodyText);
message.setHeader("Content-Type", "application/x-www-form-urlencoded");
// If the setHeader is commented, it works
// message.setHeader("Content-Length", "11");

std::string apiAddress = "http://127.0.0.1:8080/dontg";
if (client->post(apiAddress,message))
{
Wt::log("info") << "[TEST] Message sent to API. Address : " << apiAddress;
}
else
{
Wt::log("error") << "[TEST] Failed to send message to API. Address : " << apiAddress;
}
}
void handle(boost::system::error_code err, const Wt::Http::Message& response)
{
if (!err)
{
if (response.status() == 200)
{
std::cout << "REPONSE : " << response.body().c_str();
}
else
{
Wt::log("error") << "Http::Client error: " << response.status();
}
}
}
};

class ExampleBadRequest : public Wt::WResource
{
public:
virtual ~ExampleBadRequest()
{
beingDeleted();
}

protected:
virtual void handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response)
{
Wt::Http::Client *client = new Wt::Http::Client(this);
client->done().connect(boost::bind(&ExampleBadRequest::handle, this, _1, _2));

std::string bodyText = "param=value";
Wt::Http::Message message;
message.addBodyText(bodyText);
message.setHeader("Content-Type", "application/x-www-form-urlencoded");
// Setting header, bad request
message.setHeader("Content-Length", "11");

std::string apiAddress = "http://127.0.0.1:8080/dontg";
if (client->post(apiAddress,message))
{
Wt::log("info") << "[TEST] Message sent to API. Address : " << apiAddress;
}
else
{
Wt::log("error") << "[TEST] Failed to send message to API. Address : " << apiAddress;
}
}
void handle(boost::system::error_code err, const Wt::Http::Message& response)
{
if (!err)
{
if (response.status() == 200)
{
std::cout << "RESPONSE : " << response.body().c_str();
}
else
{
Wt::log("error") << "Http::Client error: " << response.status();
}
}
}
};

class DoNothing : public Wt::WResource
{
public:
virtual ~DoNothing()
{
beingDeleted();
}

protected:
virtual void handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response)
{
response.out() << "DOING NOTHING \n";
}
};


int main(int argc, char **argv)
{
try
{

ExampleBadRequest exBR;
ExampleGoodRequest exGR;
DoNothing dontg;
Wt::WServer server(argv[0]);

server.setServerConfiguration(argc, argv);

server.addResource(&exBR, "/exbr");
server.addResource(&exGR, "/exgr");
server.addResource(&dontg, "/dontg");
if (server.start())
{
Wt::WServer::waitForShutdown();
server.stop();
}
}
catch (Wt::WServer::Exception& e)
{
std::cerr << e.what() << std::endl;
}
catch (std::exception &e)
{
std::cerr << "exception: " << e.what() << std::endl;
}
}
    (1-1/1)