7

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?

user1434770
  • 85
  • 1
  • 5
  • Your update: you've registered a POST handler for your /resource, but you're sending GET. Try making a POST instead. Using CURL you can do this with `--data "something"` and remove the explicit `-X GET`. – Rup Mar 03 '17 at 11:50
  • Thanks a lot! That made the trick. Now i´ll try to set up a client/server for json messages, probably I´ll be back crying. – user1434770 Mar 03 '17 at 12:39

1 Answers1

8

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'

Ben Crowhurst
  • 8,204
  • 6
  • 48
  • 78
  • what command should I use to link it and from where? I have cloned it from here https://github.com/corvusoft/restbed and executed the build command but could not find how to link it . I have used the same code above. – Udesh Feb 22 '21 at 04:25