0

I have used the HTTP server form boost example for creating a server that is accessed for verifying that the application is running. So supposing that the port is 9000, I want to add a path to it, so I can test multiple things. But first I want to add the status, so when I access the localhost:9000/status page I want to get the informations that are now displayed when accessing the localhost:9000 page.

I have tried some approaches to add a path to the endpoint, or to create a query that has the path included, but I have not manage to do it. The application is crashing with Exception: resolve: Host not found (autoritative) when accessing the localhost:9000/status.

I have searched for the path method of the endpoint class, but I couldn't found it (undefined). Shall I include something more?

What is the way of adding the path after the port (like localhost:9000/status)?

sop
  • 3,445
  • 8
  • 41
  • 84

1 Answers1

2

The path is part of the URI, which is something entirely different than the host:port tuple. You'll have to deal with that in the request handler and branch into the desired code path based on the URI.

Update:

On a second note, that "HTTP" server of the boost examples is neither fully fledged nor very robust. You should really use a properly developed, well tested embeddable http server component instead of Cargo-Culting off that rudimentary Boost example code.

Community
  • 1
  • 1
datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • So I have to create/update the handler for the localhost:9000/status? The endpoint remains localhost:9000; or I shall create a server for localhost:9000/status and one for localhost:9000/blabla? – sop Mar 27 '15 at 10:16
  • @sop: Well, that code is just an example how to use Boost ASIO. And the example is a small HTTP server, that does nothing much. I strongly suggest you don't use it for anything serious. Instead use a proper embedded http engine, like libmicrohttp, libshttpd or similar. HTTP is a tricky beast and that Boost example doesn't even scratch the surface. Source: I've written an embedded http engine myself, called litheweb (can be found at github). – datenwolf Mar 27 '15 at 10:19
  • I have tried CPPRest, but it is not responding, but I had some problems with it, I do not remember exactly what, but it was not responding or something like that... – sop Mar 27 '15 at 10:21
  • 2
    @sop: The basic idea behind a HTTP engine is: You start it on a address:port and then for each incoming request it gives you the URI and the Headers of the request which then *you* use to route into the right path. My litheweb is targeted at embedded systems with very little memory, so URI routing happens in the HTTP engine for efficiency reasons. But in general you'll use some string comparison or parsing or pattern matching on the URI to decide which handler to call. – datenwolf Mar 27 '15 at 10:24
  • Ok, the application (with cpprest) is crashing when I access the page... I will study more, and find the problem, thanks. (or maybe I will post a question, if I haven't already) – sop Mar 27 '15 at 10:50
  • @sop: A network facing process crashing is a very bad thing. A crash always indicates that there's a serious problem with your code (and I suspect in your case an invalid memory access) which in 99.9% of all cases can be turned into an exploit, often of the code execution kind, which is the worst kind of exploit. Most likely you're passing a too small buffer to the HTTP engine, or an invalid pointer or doing something else bad. However in this kind of crashes debuggers are your friend, as they will tell you the offending code path in about 80% of the cases correctly. – datenwolf Mar 27 '15 at 11:31
  • What does small buffer means? I have a `void handle_get(http_request request) { TRACE(L"\nhandle GET\n"); request.reply(status_codes::OK, /*json::value::object(answer)*/U("Hello You!")); };`, where `#define TRACE(msg) std::wcout << msg` Shall I make it a http page? – sop Mar 27 '15 at 13:03
  • @sop: The Boost example code class `request` has no method `reply`. The class `request_handler` has a method `handle_request` which you obviously shall overwrite in your subclass; there is a second parameter, a reference to an instance of struct `reply`, which consists of a number of elements, which are to be filled with values by you. Be aware that C++ references are in fact pointers in disguise and may become invalid (just like pointers). Struct reply has a status field and a vector of the to be responded headers and a string which carries the actual reply content. – datenwolf Mar 27 '15 at 14:26
  • 1
    @sop: You use it like `void my_request_handler::handle_request(const request& req, reply& rep) { if(req.method == std::string("GET")) { if(req.uri == std::string("/whatever")) { reply.status = reply::ok; reply.content = std::string("Hello You!"); } } }` – datenwolf Mar 27 '15 at 14:29
  • Sorry, I have not mentioned that the code was for the cpprest case, the boost works fine, but it has no path, just the port. I have not managed to make the cpprest version working, not even a simple thing ([here](https://casablanca.codeplex.com/discussions/581922) in the second part). Their forum does not work now. – sop Mar 27 '15 at 14:47
  • @sop: Due to lack on having documentation of the cpprest thing (and Google being not very helpful) I can only assume that you may find the URI in the `http_request` instances passed to your handler. And it's very likely that before making the call to `http_request::reply` you first have to set a few elements/properties (either by assigning to elements of `http_request` or by calling setter functions). You must at least provided some reply content. – datenwolf Mar 27 '15 at 14:53
  • This is [another question](http://stackoverflow.com/q/28833651/3062311), so let us stop commenting :) here – sop Mar 27 '15 at 14:57