2017-03-02 2 views
1

私はrestbedやHTTP(プログラミング賢い)に関連するものは初めてです。 私は自分のプロジェクトにrestbedを追加し、restbedのgithubのメインページに示されている簡単なサンプルを実行しようとしました。restbedの例

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

私はstart関数をチェックしましたが、何らかの種類のsingnalハンドラが使用されていますが、テストが実際にどのように機能するかはわかりません。

私が何か間違っていると分かってもらえますか、テストは期待どおりに動いていますか?

ありがとうございました。

編集:説明が多く役立ちました。そこで、あなたが投稿したコードを試してみました。

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

「実装されていません」というメッセージは良いことではないと思います。これで私を助けてくれますか?

+0

あなたの更新:あなたは/リソースに対してPOSTハンドラを登録しましたが、あなたがGETを送っています。代わりにPOSTを試してみてください。 CURLを使用すると、 '--data 'なんらかの' 'でこれを行い、明示的な' -X GET'を削除することができます。 – Rup

+0

ありがとう!それがトリックでした。今、私はjsonメッセージ用のクライアント/サーバーを設定しようとします。おそらく私は泣いています。 – user1434770

答えて

2

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'

関連する問題