0

I have created a simple application with http_listener from casablanca (or cpprest) library:

#include <cpprest/http_listener.h>
#include <functional>

using namespace web::http::experimental::listener;
using namespace web::http;
using namespace web;

void handle_get(http_request message)
{
    message.reply(status_codes::OK, U("Hello, World!"));
};

void handle_post(http_request message)
{
    message.reply(status_codes::NotFound);
};

void handle_put(http_request message)
{
    message.reply(status_codes::NotFound);
};

void handle_delete(http_request message)
{
    message.reply(status_codes::NotFound);
};

#define TRACE(msg)            std::wcout << msg
#define TRACE_ACTION(a, k, v) std::wcout << a << L" (" << k << L", " << v << L")\n"

int main(int argc, char ** argv)
{
  uri_builder uri(U("http://127.0.0.1:61561"));
  http_listener listener(uri.to_uri());

  listener.support(methods::GET, handle_get);
  listener.support(methods::POST, handle_post);
  listener.support(methods::PUT, handle_put);
  listener.support(methods::DEL, handle_delete);

  try
  {
     listener
        .open()
        .then([&listener](){TRACE(L"\nstarting to listen\n");})
       .wait();

      while (true);
  }
  catch (std::exception const & e)
  {
     std::wcout << e.what() << std::endl;
  }
  catch (...)
  {
     std::wcout << "Unknown exception" << std::endl;
  }

  return 0;
}

What I do not get it is that when I try to access the page (X.X.X.X:XXXX; it does not matter which one, I have tested on many), the application is crashing with no exception and I get a "No data received, Unable to load the webpage because the server sent no data. Error code: ERR_EMPTY_RESPONSE" page.

sop
  • 3,445
  • 8
  • 41
  • 84

2 Answers2

0

Well, you're not sending any data in the reply, just a status code. So, yes, if your client expects data and you starve it, then this will happen.

That http_request class that gets passed as the message parameter surely expects the content to be set first before calling the reply method (or reply takes the reply data as another parameter and has an empty data default for that parameter). Anyway, you somehow must supply content data, your client is expecting it and relies on you, the programmer, to satisfy it.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • I still think there is a problem with the cpprest... Can you post some little code, something that it should look like? :) – sop Mar 27 '15 at 16:04
  • I say that because this is very similar to the [tutorial](https://casablanca.codeplex.com/wikipage?title=HTTP%20Listener) on their site (that I have tried and had the same problem...) – sop Mar 30 '15 at 12:14
0

It seems that the problem was the version of CPPREST. I have updated to 2.5 and it worked...

sop
  • 3,445
  • 8
  • 41
  • 84