corvusoft / restbed Goto Github PK
View Code? Open in Web Editor NEWCorvusoft's Restbed framework brings asynchronous RESTful functionality to C++14 applications.
Home Page: http://www.corvusoft.co.uk
License: Other
Corvusoft's Restbed framework brings asynchronous RESTful functionality to C++14 applications.
Home Page: http://www.corvusoft.co.uk
License: Other
$ make
Linking CXX shared library librestbed.dylib
Undefined symbols for architecture x86_64:
"_ERR_remove_thread_state", referenced from:
asio::ssl::detail::openssl_init_base::do_init::~do_init() in service.cpp.o
asio::ssl::detail::openssl_init_base::do_init::~do_init() in service_impl.cpp.o
asio::ssl::detail::openssl_init_base::do_init::~do_init() in session.cpp.o
asio::ssl::detail::openssl_init_base::do_init::~do_init() in session_impl.cpp.o
asio::ssl::detail::openssl_init_base::do_init::~do_init() in socket_impl.cpp.o
"_TLSv1_1_client_method", referenced from:
asio::ssl::context::context(asio::ssl::context_base::method) in service_impl.cpp.o
"_TLSv1_1_method", referenced from:
asio::ssl::context::context(asio::ssl::context_base::method) in service_impl.cpp.o
"_TLSv1_1_server_method", referenced from:
asio::ssl::context::context(asio::ssl::context_base::method) in service_impl.cpp.o
"_TLSv1_2_client_method", referenced from:
asio::ssl::context::context(asio::ssl::context_base::method) in service_impl.cpp.o
"_TLSv1_2_method", referenced from:
asio::ssl::context::context(asio::ssl::context_base::method) in service_impl.cpp.o
"_TLSv1_2_server_method", referenced from:
asio::ssl::context::context(asio::ssl::context_base::method) in service_impl.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [librestbed.dylib] Error 1
make[1]: *** [CMakeFiles/restbed.dir/all] Error 2
make: *** [all] Error 2
Move all ASIO logic into the socket abstraction.
Brief
Give greater control over error path logic. As shown in the example below, supplying an error_handler will override any Service and/or Resource error handlers.
Example
void post_method_handler( const shared_ptr< Session >& session )
{
const auto request = session->get_request( );
size_t content_length = 0;
request->get_header( "Content-Length", content_length );
session->fetch( content_length, success_handler, error_handler );
}
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;
}
Hi,
I'm having some troubles compiling your library on linux mint 17.2.. I'm not an expert in C++, especially with third-party libraries..
My problem is that I try to compile with librestbed.so and it doesn't work.. I used your example and just changed #include <restbed>
by #include </usr/local/lib/restbed/build/librestbed.so>
.. (The restbed folder being in /usr/local/lib/)
Then, I compile the file example.cpp using the command: g++ -lx/usr/local/lib/restbed/build/librestbed.so example.cpp
If I do this, I get this error:
^ In file included from test.cpp:3:0: /usr/local/lib/restbed/build/librestbed.so:33:72: error: expected unqualified-id before ‘!’ token �0�Hm�n��״�ҿ���}�rm!����b���K��OOk6+����S�&��o͎���-?z��>%a0C�~�� ^ In file included from test.cpp:3:0: /usr/local/lib/restbed/build/librestbed.so:35:140: error: expected unqualified-id before ‘<’ token q� z,��Y��*4����S�/\(�|/��,�xo����C?�j��Jh��Fgv���.�Y�.�ڜ�-q͑�F6��0/�=ݳ��m}��3J:�'11��U�ܐ5�����U�Co�}��f��W�O������3�&�}�N��U6�e\����Y�S��v��}��7����Je���,f�j����$�̫�m��<.�1I�s�Z��u�&ә�k<F�� ^
Does anyone have any idea why??
Thanks for your answers
The linux build has many warnings around ASIO. This is due to our use of -Wall -Wextra -Weffc++ -pedantic
.
I've tried implementing your example code in an executable called m3 but it seems to die randomly after a few hours (when not being called upon over http). Here's the SIGSEV backtrace.
./m3(_Z7handleri+0x15)[0x430845]
/lib/x86_64-linux-gnu/libc.so.6(+0x35180)[0x7f5d2c65c180]
./m3(_ZN4asio3ssl6detail5io_opINS_19basic_stream_socketINS_2ip3tcpENS_21stream_socket_serviceIS5_EEEENS1_8write_opINS_15const_buffers_1EEENS_6detail8write_opINS0_6streamIS8_EESA_NSC_14transfer_all_tESt8functionIFvRKSt10error_codemEEEEEclESI_mi+0x138)[0x475ef8]
./m3(_ZN4asio6detail8write_opINS_3ssl6streamINS_19basic_stream_socketINS_2ip3tcpENS_21stream_socket_serviceIS6_EEEEEENS_15const_buffers_1ENS0_14transfer_all_tESt8functionIFvRKSt10error_codemEEEclESG_mi+0xea)[0x47656a]
./m3(_ZN7restbed6detail10SocketImpl5writeERKSt6vectorIhSaIhEERKSt8functionIFvRKSt10error_codemEE+0x13a)[0x472e5a]
./m3(_ZNK7restbed6detail11SessionImpl8transmitERKNS_8ResponseERKSt8functionIFvRKSt10error_codemEE+0x2aa)[0x46f7fa]
./m3(_ZN7restbed7Session5closeERKNS_8ResponseE+0x98)[0x46b4c8]
./m3(_ZN7restbed7Session5closeEiRKSt6vectorIhSaIhEERKSt8multimapISsSsSt4lessISsESaISt4pairIKSsSsEEE+0x48)[0x46c8e8]
./m3(_ZN7restbed7Session5closeEiRKSsRKSt8multimapISsSsSt4lessISsESaISt4pairIS1_SsEEE+0x2b)[0x46c9db]
./m3(_ZNK7restbed6detail11SessionImpl7failureESt10shared_ptrINS_7SessionEEiRKSt9exception+0x27b)[0x46e21b]
Brief
Give sessions the ability to retain data between asynchronous calls. This should help aid readability and put a halt to std::bind( callback, this, MyCustomSessionContextContainer ).
Example
void get_method_handler( const shared_ptr< Session >& session )
{
set< string > filters = { "age", "name" };
session.set( "filters", filters );
session->wait_for( chrono::seconds( 5 ), [ ]( const shared_ptr< Session >& session )
{
set< string > filters = session->get( "filters" );
} );
}
int main( const int, const char** )
{
auto resource = make_shared< Resource >( );
resource->set_path( "/resources/item" );
resource->set_method_handler( "GET", get_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;
}
API
set< string > Session::keys( void ) const;
void erase( const std::string& name = "" );
bool Session::has( const std::string& name ) const;
const std::typeinfo Session::type( const std::string& name );
ContextValue Session::get( const std::string& name ) const;
ContextValue Session::get( const std::string& name, ContextValue default ) const;
void Session::set( const std::string& name, ContextValue );
A random selection of acceptance tests fail when run all at once i.e make test
. If you run each test case individually, everything thing is 5-by-5. It appears to be caused by service::start( ) and then service::stop( ) being called in close succession; Catch++ causing issues?
Further work is required to produce a workaround.
Using the framework is not hindered by this situation.
restbed/dependency/framework/source/corvusoft/framework/detail/date_impl.cpp:78:66: error: ‘put_time’ was not declared in this scope
I have a server that a callback to a request takes a while. Meanwhile if the client closes the connection, I got a segmentation fault in the server. Here is the full backtrace
(gdb) bt
#0 0x000055555578bbd3 in asio::ssl::detail::engine::perform(int (asio::ssl::detail::engine::*)(void*, unsigned long), void*, unsigned long, std::error_code&, unsigned long*) ()
#1 0x000055555578ebde in asio::ssl::detail::io_op<asio::basic_stream_socket<asio::ip::tcp, asio::stream_socket_service<asio::ip::tcp> >, asio::ssl::detail::write_op<asio::const_buffers_1>, asio::detail::write_op<asio::ssl::stream<asio::basic_stream_socket<asio::ip::tcp, asio::stream_socket_service<asio::ip::tcp> > >, asio::const_buffers_1, asio::detail::transfer_all_t, std::function<void (std::error_code const&, unsigned long)> > >::operator()(std::error_code, unsigned long, int) ()
#2 0x000055555578f464 in asio::detail::write_op<asio::ssl::stream<asio::basic_stream_socket<asio::ip::tcp, asio::stream_socket_service<asio::ip::tcp> > >, asio::const_buffers_1, asio::detail::transfer_all_t, std::function<void (std::error_code const&, unsigned long)> >::operator()(std::error_code const&, unsigned long, int) ()
#3 0x000055555578b390 in restbed::detail::SocketImpl::write(std::vector<unsigned char, std::allocator<unsigned char> > const&, std::function<void (std::error_code const&, unsigned long)> const&) ()
#4 0x00005555557874dc in restbed::detail::SessionImpl::transmit(restbed::Response const&, std::function<void (std::error_code const&, unsigned long)> const&) const ()
#5 0x0000555555781f47 in restbed::Session::close(restbed::Response const&) ()
#6 0x0000555555783fe3 in restbed::Session::close(int, std::vector<unsigned char, std::allocator<unsigned char> > const&, std::multimap<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&) ()
#7 0x0000555555784146 in restbed::Session::close(int, std::string const&, std::multimap<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&) ()
#8 0x0000555555785a2d in restbed::detail::SessionImpl::failure(std::shared_ptr<restbed::Session>, int, std::exception const&) const ()
#9 0x0000555555780512 in std::_Function_handler<void (std::error_code const&, unsigned long), restbed::Session::close(restbed::Response const&)::{lambda(std::error_code const&, unsigned long)#1}>::_M_invoke(std::_Any_data const&, std::error_code const&, unsigned long&&) ()
#10 0x000055555578c2b7 in asio::detail::write_op<asio::basic_stream_socket<asio::ip::tcp, asio::stream_socket_service<asio::ip::tcp> >, asio::const_buffers_1, asio::detail::transfer_all_t, std::function<void (std::error_code const&, unsigned long)> >::operator()(std::error_code const&, unsigned long, int) ()
#11 0x000055555578c45e in asio::detail::reactive_socket_send_op<asio::const_buffers_1, asio::detail::write_op<asio::basic_stream_socket<asio::ip::tcp, asio::stream_socket_service<asio::ip::tcp> >, asio::const_buffers_1, asio::detail::transfer_all_t, std::function<void (std::error_code const&, unsigned long)> > >::do_complete(asio::detail::task_io_service*, asio::detail::task_io_service_operation*, std::error_code const&, unsigned long) ()
#12 0x0000555555752d0b in std::thread::_Impl<std::_Bind_simple<restbed::Service::start(std::shared_ptr<restbed::Settings const> const&)::{lambda()#2} ()> >::_M_run()
()
#13 0x00007ffff5411350 in std::(anonymous namespace)::execute_native_thread_routine (__p=<optimized out>)
at /build/gcc-multilib/src/gcc-5.2.0/libstdc++-v3/src/c++11/thread.cc:84
#14 0x00007ffff5dd24a4 in start_thread () from /usr/lib/libpthread.so.0
#15 0x00007ffff4b8913d in clone () from /usr/lib/libc.so.6
void get_query_parameter( const std::string& name,
int& value, /*out*/
const int default_value = 0,
const bool ignore_case = true,
const std::function< std::string ( const std::string& ) >& transform = nullptr ) const;
void get_path_parameter( const std::string& name,
unsigned int& value, /*out*/
const unsigned int default_value = 0,
const bool ignore_case = true,
const std::function< std::string ( const std::string& ) >& transform = nullptr ) const;
When the rules engine runs the HTTP request has not yet be parsed for path parameters.
Example
Service service;
service.set_signal_handler( int signal, [ ]( void )
{
} );
Source
signal_handler_setup( asio::io_service& service, asio::signal_set& signals )
{
signals.async_wait( [ &service, &signals ]( system::error_code error, int signal )
{
if ( not error )
{
std::cerr << "received signal " << signalNumber << std::endl;
signal_handler_setup( service, signals );
}
} );
}
asio::io_service service;
asio::signal_set signals { service };
signals.add( SIGTERM );
signal_handler_setup( service, signals );
service.run( );
It has been suggested we rename all headers from '.h' suffix to '.hpp'
struct Http::Request
{
Bytes body;
uint16_t port;
string method;
struct SSL { ... }
...
std::future< Http::Response > response;
};
Http::sync( Http::Request request );
Http::sync( std::list< Http::Request >& requests );
Http::async( Http::Request request );
Http::async( std::list< Http::Request >& requests );
The async methods fire off as many requests as possible and return without blocking. It is then the responsibility of the developer to check each future for a ready response.
for ( auto request : requests )
{
if ( request.response.wait_for( std::chrono::seconds( 0 ) ) == std::future_status::ready )
{
//process response.
}
}
Further discussion is required around sharing the same asio::io_service as the restbed::Service or having a separate instance.
I have a large project which is already link with boost and boost::asio as static link, now I would like to integrate restbed into my project, but it's seems restbed is depends on non-boost::asio, it will causes the conflict unless I wrapper the restbed as dynamic link.
So my question is possible compile restbed with boost::asio ?
Convert the Corvusoft Framework over to a header only library.
We've had a fair amount of success with Visual Studio 2015.
cmake -DBUILD_SHARED=YES
set( BUILD_SHARED_LIBS ON )
void get_property( const std::string& name, int& value, const int default_value = 0 ) const;
void get_property( const std::string& name, long& value, const long default_value = 0 ) const;
void get_property( const std::string& name, float& value, const float default_value = 0 ) const;
void get_property( const std::string& name, double& value, const double default_value = 0 ) const;
void get_property( const std::string& name, long long& value, const long long default_value = 0 ) const;
void get_property( const std::string& name, unsigned int& value, const unsigned int default_value = 0 ) const;
void get_property( const std::string& name, unsigned long& value, const unsigned long default_value = 0 ) const;
void get_property( const std::string& name, unsigned long long& value, const unsigned long long default_value = 0 ) const;
Example
int timeout = 0;
settings.get_property( "timeout", timeout, 10 );
Session session;
session->set( "style", true );
session->set( "style", false ); //has no effect.
The ability to store session data and retrieve it at a later date.
SessionManager manager;
Service service;
service.set_session_manager( manager );
This (^(.*): *(.*)\\s*$)
does not look like correct headers handling, for instance try to parse User-Agent: Mozilla:4.0
. Are you aware of RFC 7230 to speak about compliance?
It has been suggested that the Request::get_header methods would benefit from the use of templates.
Investigate parallel builds to increase project compilation time.
make -j5
URLs in general are case-sensitive (with the exception of machine names). There may be URLs, or parts of URLs, where case doesn't matter, but identifying these may not be easy. Users should always consider that URLs are case-sensitive.
auto settings = make_shared< Settings >( );
Service service;
service.add_application_layer( http_10_instance );
service.add_application_layer( http_11_instance );
service.add_application_layer( http2_instance );
service.add_application_layer( spdy_instance );
service.start( settings );
Service service;
service.schedule( const function< void ( Service& ) >& work );
asio::socket_base::linger option( true, 30 );
socket->set_option( option );
void get_method_handler( const shared_ptr< Session >& session )
{
session->close( OK, "Hello, World!", { { "Content-Length", "13" }, { "Connection", "close" } } );
}
void service_ready_handler( Service& service )
{
thread thread1( bind( &Service::start, service );
thread thread2( bind( &Service::start, service );
...
}
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;
}
API
http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/io_service/run/overload2.html
Currently due to the limitations of the framework::Http logic. We are unable to perform client-side HTTPS requests.
The VS project which generated from CMAKE just include the 32bit project, I'm created the X64 project which copy from the x86, but got the compile error :
Error 2 error LNK1112: module machine type 'x64' conflicts with target machine type 'X86' E:\Stacks\restbed\x64\Debug\uri.obj 1 1 restbed
Does the restbed test with VS2013?
Error 1 error C2797: 'restbed::detail::SettingsImpl::m_status_messages': list initialization inside member initializer list or non-static data member initializer is not implemented E:\Stacks\restbed\source\corvusoft\restbed\detail\settings_impl.hpp 116 1 restbed
Error 2 error C2668: 'restbed::Session::close' : ambiguous call to overloaded function E:\Stacks\restbed\source\corvusoft\restbed\detail\service_impl.cpp 398 1 restbed
Settings settings;
settings.set_bind_address( "127.0.0.1" );
SSLSettings ssl_settings;
ssl_settings.set_bind_address( "198.2.0.1" );
Originally it was decided not to implement this feature. However after some consideration it appears to be straight forward enough.
settings.set_pipelining_enabled( true ) );
This regex seems to not allow relative paths : https://github.com/Corvusoft/restbed/blob/master/source/corvusoft/restbed/uri.cpp#L207
(https://github.com/Corvusoft/restbed/blob/3.0/source/corvusoft/restbed/uri.cpp#L200)
e.g.:
auto uri = Uri("file://certs/server.key");
cout << uri.get_path() << endl;
prints: /server.key
I guess this is only relevant for file:// though since http://blah.com/blah would be fine.
We don't check for 'Connection: keep-alive' before calling close on 404, 501, and 500 error logic.
Resource resource;
resource.set_handler( "POST", rules, callback );
This can be useful/cleaner when creating rules that should only run for particular methods or deviate from the larger ruleset.
auto request = session->get_request( );
if ( request.get_header( "connection", String::lowercase ) == "upgrade" )
{
if ( request.get_header( "upgrade", String::lowercase ) == "websocket" )
{
//validate client headers.
session->yield( SwitchingProtocols, { response headers... }, [ ]( shared_ptr< WebSocket >& socket )
{
socket.on( "close", callback );
socket.on( "error", callback );
socket.on( "message", callback );
socket.close( callback );
socket.send( "", callback );
}
} //else...
}
Request
GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Origin: http://example.com
Server response:
Response
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat
http://stackoverflow.com/questions/17364318/asynchronously-waiting-until-a-socket-is-available-for-reading-writing-in-asio
API
http://enterprisewebbook.com/ch8_websockets.html
https://developer.mozilla.org/en-US/docs/WebSockets/Writing_WebSocket_servers
http://stackoverflow.com/questions/17364318/asynchronously-waiting-until-a-socket-is-available-for-reading-writing-in-asio
When building on Ubuntu systems with -DBUILD_EXAMPLES the build fails if pthreads, PAM dev, OpenSSL dev, and/or syslog headers are not present. We should add checks to determine the state of the system before building.
Currently we don't pull down OpenSSL when cloning the repository. And we don't force the dependency to build if we fail to locate a preinstalled installation.
How should we manage this dependency?
Service service;
service.add_protocol( "HTTP/2", instance );
service.add_protocol( "SPDY", instance );
During the development of the SSL feature a slew of #ifdef conditionals were placed in the codebase to allow optional compilation. This has resulted in an unreadable layout that has my OCD ringing.
Hi,
Do you have an example of receiving binary files (such as jpeg images) via transfer encoding chunked. The example transfer_encoding_request shows sending text files, but when sending binary data is does not work.
Thanks.
Not all of the query/path parameters are being returned when invoking:
Request::get_path_parameters( );
Request::get_query_parameters( );
Allow a resource cache to be injected into the service. This cache would be consulted with the incoming request m_cache->find_entry( request )
to determine if we should call the method handler or throw back the cache entry.
Cache cache;
Service service;
service.set_cache( cache );
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.