c++ - 休息床示例

标签 c++ restbed

这是我第一次使用 restbed 或任何与 HTTP 相关的东西(编程方面)。 所以我将休息床添加到我的项目中,并尝试运行休息床的 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;
}

它在行中停止:service.start( settings );

我已经检查了启动函数,使用了某种信号处理程序,但我不知道测试应该如何工作。

如果我做错了什么,测试是否按预期工作或其他任何事情,有人可以帮助我理解吗?

谢谢。

已编辑:解释很有帮助。所以我尝试了你发布的代码并得到了这个:

$> 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

我猜“未实现”消息不是一件好事。你能帮我解决这个问题吗?

最佳答案

Service::start 处理程序正在阻塞。调用此方法的线程将用于处理传入请求。

如果您希望继续处理,请设置 ready handler .当服务启动并准备好处理请求时,将调用此处理程序。

服务器

#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;
}

客户端

curl -w'\n' -v -X GET 'http://localhost:1984/resource'

关于c++ - 休息床示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42559624/

相关文章:

c++ - 类树与结构树节点

c++ - Visual Studio - 使用从现有源创建项目向导时使用新筛选器而不是新文件夹

c++ - 如何检查指针指向的对象是否为空?

c++ - 如果将这个函数放在通用库中会导致什么错误?

c++ - 如何使用 Boost Filesystem 复制目录

sockets - 将 HTTP 请求重定向/代理到辅助 HTTP 服务器

c++ - 使用 Restbed 时出现链接器错误

c++ - restbed动态资源发布

c++ - 带有示例的休息床静态库