休息示例
Restbed example
这是我第一次使用休息或与http相关的任何东西(明智的编程)。因此,我将其休息添加到了我的项目中,并试图运行在REST BEST的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, "%.*sn", ( 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(设置);
我已经检查了起始功能,使用了某种单身处理程序,但是我不知道测试应该如何真正起作用。
有人可以帮助我了解我是否做错了什么,测试是按预期工作或其他任何事情的?
谢谢。
编辑:解释对此有很大帮助。因此,我尝试了您发布的代码并得到了:
$> 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
我猜想"未实现"消息不是一件好事。你能帮我吗?
服务::开始处理程序正在阻止。调用此方法的线程将用于处理传入请求。
如果您想继续处理,请设置一个就绪处理程序。该处理程序将在服务启动并准备好处理请求时调用该处理程序。
服务器
#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'
相关文章:
- 没有找到相关文章