在STM32MP157C 上编译restbed 库遇到了麻烦,几天没有过。问题出在openssl 上面。经过很长时间的摸索,可能问题出在restbed 目录中的 test 要用到openssl 库,cmake 指定在dependency目录中。后来干脆将test 文件夹删除掉。产生出错时,将cmake.txt 中有关test 的部分(89~97行)删除掉。编译通过了。产生了librestbed.a 文件。
$CC webServer.cpp -I./include -L./ librestbed.a -o restbedTest -lstdc++ -pthread -lssl -lcrypto
#include <string>
#include <memory>
#include <cstdlib>
#include <fstream>
#include <restbed>
#include <streambuf>
#include <iostream>
using namespace std;
using namespace restbed;
void get_index( const shared_ptr< Session > session )
{
// const auto request = session->get_request( );
ifstream stream( "./www/views/index.html", ifstream::in );
if ( stream.is_open( ) )
{
const string body = string( istreambuf_iterator< char >( stream ), istreambuf_iterator< char >( ) );
const multimap< string, string > headers
{
{ "Content-Type", "text/html" },
{ "Content-Length", ::to_string( body.length( ) ) }
};
session->close( OK, body, headers );
}
else
{
session->close( NOT_FOUND );
}
}
void get_html( const shared_ptr< Session > session )
{
const auto request = session->get_request( );
const string filename = request->get_path_parameter( "filename" );
ifstream stream( "./www/views/" + filename, ifstream::in );
if ( stream.is_open( ) )
{
const string body = string( istreambuf_iterator< char >( stream ), istreambuf_iterator< char >( ) );
const multimap< string, string > headers
{
{ "Content-Type", "text/html" },
{ "Content-Length", ::to_string( body.length( ) ) }
};
session->close( OK, body, headers );
}
else
{
session->close( NOT_FOUND );
}
}
void get_css( const shared_ptr< Session > session )
{
const auto request = session->get_request( );
const string filename = request->get_path_parameter( "filename" );
ifstream stream( "./www/css/" + filename, ifstream::in );
if ( stream.is_open( ) )
{
const string body = string( istreambuf_iterator< char >( stream ), istreambuf_iterator< char >( ) );
const multimap< string, string > headers
{
{ "Content-Type", "text/css" },
{ "Content-Length", ::to_string( body.length( ) ) }
};
session->close( OK, body, headers );
}
else
{
session->close( NOT_FOUND );
}
}
void get_js( const shared_ptr< Session > session )
{
const auto request = session->get_request( );
const string filename = request->get_path_parameter( "filename" );
ifstream stream( "./www/js/" + filename, ifstream::in );
if ( stream.is_open( ) )
{
const string body = string( istreambuf_iterator< char >( stream ), istreambuf_iterator< char >( ) );
const multimap< string, string > headers
{
{ "Content-Type", "text/js" },
{ "Content-Length", ::to_string( body.length( ) ) }
};
session->close( OK, body, headers );
}
else
{
session->close( NOT_FOUND );
}
}
void get_image( const shared_ptr< Session > session )
{
const auto request = session->get_request( );
const string filename = request->get_path_parameter( "filename" );
ifstream stream( "./www/image/" + filename, ifstream::in );
if ( stream.is_open( ) )
{
const string body = string( istreambuf_iterator< char >( stream ), istreambuf_iterator< char >( ) );
const multimap< string, string > headers
{
{ "Content-Type", "image/jpg" },
{ "Content-Length", ::to_string( body.length( ) ) }
};
session->close( OK, body, headers );
}
else
{
session->close( NOT_FOUND );
}
}
void get_value( const shared_ptr< Session > session )
{
const auto request = session->get_request( );
const string id = request->get_query_parameter( "id" );
const string val = request->get_query_parameter( "val" );
const string body = "id="+id+",val="+val;
session->close( OK, body, { { "Content-Length", ::to_string( body.size( ) ) } } );
}
int main( const int, const char** )
{ cout<<"restbed web server"<<endl;
auto resource1 = make_shared< Resource >( );
resource1->set_path( "/index" );
resource1->set_method_handler( "GET", get_index );
auto resource2 = make_shared< Resource >( );
resource2->set_path( "/views/{filename: [a-z]*\\.html}" );
resource2->set_method_handler( "GET", get_html);
auto resource3 = make_shared< Resource >( );
resource3->set_path( "/css/{filename: [a-z]*\\.css}" );
resource3->set_method_handler( "GET", get_css);
auto resource4 = make_shared< Resource >( );
resource4->set_path( "/js/{filename: [a-z]*\\.js}" );
resource4->set_method_handler( "GET", get_js);
auto resource5 = make_shared< Resource >( );
resource5->set_path( "/image/{filename: .*}" );
resource5->set_method_handler( "GET", get_image);
auto resource6 = make_shared< Resource >( );
resource6->set_path( "/query/param");
resource6->set_method_handler( "GET", get_value);
auto settings = make_shared< Settings >( );
settings->set_port(7999);
settings->set_default_header( "Connection", "close" );
Service service;
service.publish( resource1 );
service.publish( resource2 );
service.publish( resource3 );
service.publish( resource4 );
service.publish( resource5 );
service.publish( resource6 );
service.start( settings );
return EXIT_SUCCESS;
}
(base) yao@yao-dc:~/yao2020/C2020/restbedTest$ scp restbedTest root@192.168.31.194://home/root/workspace
(base) yao@yao-dc:~/yao2020/C2020/restbedTest$ scp -r www root@192.168.31.194://home/root/workspace/www/