File: example.cpp

package info (click to toggle)
restbed 4.0~dfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,388 kB
  • sloc: cpp: 15,273; sh: 37; makefile: 14
file content (49 lines) | stat: -rw-r--r-- 1,443 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * Example illustrating persistent connections.
 *
 * Server Usage:
 *    ./distribution/example/persistent_connection
 *
 * Client Usage:
 *    curl -w'\n' -v 'http://localhost:1984/resources/persistent' 'http://localhost:1984/resources/intermittent'
 */

#include <string>
#include <memory>
#include <cstdlib>
#include <restbed>

using namespace std;
using namespace restbed;

void get_intermittent_method_handler( const shared_ptr< Session > session )
{
    session->close( OK, "intermittent resource request", { { "Content-Length", "29" }, { "Connection", "close" } } );
}

void get_persistent_method_handler( const shared_ptr< Session > session )
{
    session->yield( OK, "persistent resource request", { { "Content-Length", "27" }, { "Connection", "keep-alive" } } );
}

int main( const int, const char** )
{
    auto persistent = make_shared< Resource >( );
    persistent->set_path( "/resources/persistent" );
    persistent->set_method_handler( "GET", get_persistent_method_handler );
    
    auto intermittent = make_shared< Resource >( );
    intermittent->set_path( "/resources/intermittent" );
    intermittent->set_method_handler( "GET", get_intermittent_method_handler );
    
    auto settings = make_shared< Settings >( );
    settings->set_port( 1984 );
    
    Service service;
    service.publish( persistent );
    service.publish( intermittent );
    
    service.start( settings );
    
    return EXIT_SUCCESS;
}