【问题标题】:Restbed example休息床示例
【发布时间】:2017-03-02 15:36:09
【问题描述】:

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

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

【问题讨论】:

  • 你的更新:你已经为你的 /resource 注册了一个 POST 处理程序,但是你正在发送 GET。尝试做一个 POST 代替。使用 CURL,您可以使用 --data "something" 执行此操作,并删除显式的 -X GET
  • 非常感谢!这就成功了。现在我将尝试为 json 消息设置客户端/服务器,可能我会哭泣。

标签: c++ restbed


【解决方案1】:

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'

【讨论】:

  • 我应该使用什么命令来链接它以及从哪里链接?我从这里github.com/corvusoft/restbed 克隆了它并执行了构建命令,但找不到如何链接它。我使用了上面相同的代码。
猜你喜欢
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-29
相关资源
最近更新 更多