http server with casablanca crashes if I access the page

夙愿已清 提交于 2019-12-11 20:24:55

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/28833651/http-server-with-casablanca-crashes-if-i-access-the-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!