It´s the first time I use restbed or anything related to HTTP (programming wise). So I added the restbed to my project and tried to run the simple example showed in the main page of the restbed's github.
#include <memory>
#include <cstdlib>
#include <restbed>
using namespace std;
using namespace restbed;
void post_method_handler( const shared_ptr< Session > session )
{
const auto request = session->get_request( );
int content_length = request->get_header( "Content-Length", 0 );
session->fetch( content_length, [ ]( const shared_ptr< Session > session, const Bytes & body )
{
fprintf( stdout, "%.*s\n", ( int ) body.size( ), body.data( ) );
session->close( OK, "Hello, World!", { { "Content-Length", "13" } } );
} );
}
int main( const int, const char** )
{
auto resource = make_shared< Resource >( );
resource->set_path( "/resource" );
resource->set_method_handler( "POST", post_method_handler );
auto settings = make_shared< Settings >( );
settings->set_port( 1984 );
settings->set_default_header( "Connection", "close" );
Service service;
service.publish( resource );
service.start( settings );
return EXIT_SUCCESS;
}
It stops in the line: service.start( settings );
I have checked the start function, some sort of singnal handler is used, but I don´t know how the test should really work.
Could somebody help me understand if I´m doing something wrong, the test is working as expected or anything else?
Thanks.
Edited: The explanation helped a lot. So I tried the code you posted and got this:
$> curl -w'\n' -v -X GET 'http://localhost:1984/resource'
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 1984 (#0)
> GET /resource HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:1984
> Accept: */*
>
< HTTP/1.1 501 Not Implemented
* no chunk, no close, no size. Assume close to signal end
<
* Closing connection 0
I´m guessing that the "Not implemented" message is not a good thing. Could you help me with this?
The Service::start handler is blocking. The thread that invokes this method will be used to handle incoming requests.
If you wish to continue processing set a ready handler. This handler will be invoked when the service is up and ready to handle requests.
Server
#include <thread>
#include <memory>
#include <chrono>
#include <cstdlib>
#include <restbed>
using namespace std;
using namespace restbed;
void get_method_handler( const shared_ptr< Session > session )
{
session->close( OK, "Hello, World!", { { "Content-Length", "13" }, { "Connection", "close" } } );
}
void service_ready_handler( Service& )
{
fprintf( stderr, "Hey! The service is up and running." );
}
int main( const int, const char** )
{
auto resource = make_shared< Resource >( );
resource->set_path( "/resource" );
resource->set_method_handler( "GET", get_method_handler );
auto settings = make_shared< Settings >( );
settings->set_port( 1984 );
auto service = make_shared< Service >( );
service->publish( resource );
service->set_ready_handler( service_ready_handler );
service->start( settings );
return EXIT_SUCCESS;
}
Client
curl -w'\n' -v -X GET 'http://localhost:1984/resource'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With