update http code
This commit is contained in:
parent
3c59eebe92
commit
5f9dfa9a42
4 changed files with 32 additions and 12 deletions
|
|
@ -60,6 +60,7 @@ namespace fc {
|
||||||
|
|
||||||
/// Allows to convert current public key object into base58 number.
|
/// Allows to convert current public key object into base58 number.
|
||||||
std::string to_base58() const;
|
std::string to_base58() const;
|
||||||
|
static public_key from_base58( const std::string& b58 );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class private_key;
|
friend class private_key;
|
||||||
|
|
|
||||||
|
|
@ -262,15 +262,28 @@ namespace fc { namespace ecc {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string public_key::to_base58() const
|
std::string public_key::to_base58() const
|
||||||
{
|
{
|
||||||
public_key_data key = serialize();
|
public_key_data key = serialize();
|
||||||
uint32_t check = uint32_t(city_hash64(key.data, sizeof(key)));
|
uint32_t check = sha256::hash(key.data, sizeof(key))._hash[0];
|
||||||
assert(key.size() + sizeof(check) == 37);
|
assert(key.size() + sizeof(check) == 37);
|
||||||
array<char, 37> data;
|
array<char, 37> data;
|
||||||
memcpy(data.data, key.begin(), key.size());
|
memcpy(data.data, key.begin(), key.size());
|
||||||
memcpy(data.begin() + key.size(), (const char*)&check, sizeof(check));
|
memcpy(data.begin() + key.size(), (const char*)&check, sizeof(check));
|
||||||
return fc::to_base58(data.begin(), data.size());
|
return fc::to_base58(data.begin(), data.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public_key public_key::from_base58( const std::string& b58 )
|
||||||
|
{
|
||||||
|
array<char, 37> data;
|
||||||
|
size_t s = fc::from_base58(b58, (char*)&data, sizeof(data) );
|
||||||
|
FC_ASSERT( s == sizeof(data) );
|
||||||
|
|
||||||
|
public_key_data key;
|
||||||
|
uint32_t check = sha256::hash(data.data, sizeof(key))._hash[0];
|
||||||
|
FC_ASSERT( memcmp( (char*)&check, data.data + sizeof(key), sizeof(check) ) == 0 );
|
||||||
|
memcpy( (char*)key.data, data.data, sizeof(key) );
|
||||||
|
return public_key(key);
|
||||||
|
}
|
||||||
|
|
||||||
private_key::private_key()
|
private_key::private_key()
|
||||||
{}
|
{}
|
||||||
|
|
|
||||||
|
|
@ -152,6 +152,8 @@ http::request connection::read_request()const {
|
||||||
h.val = fc::string(skey,end);
|
h.val = fc::string(skey,end);
|
||||||
req.headers.push_back(h);
|
req.headers.push_back(h);
|
||||||
if( h.key == "Content-Length" ) {
|
if( h.key == "Content-Length" ) {
|
||||||
|
auto s = static_cast<size_t>(to_uint64( fc::string(h.val) ) );
|
||||||
|
FC_ASSERT( s < 1024*1024 );
|
||||||
req.body.resize( static_cast<size_t>(to_uint64( fc::string(h.val) ) ));
|
req.body.resize( static_cast<size_t>(to_uint64( fc::string(h.val) ) ));
|
||||||
}
|
}
|
||||||
if( h.key == "Host" ) {
|
if( h.key == "Host" ) {
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ namespace fc { namespace http {
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void send_header() {
|
void send_header() {
|
||||||
|
//ilog( "sending header..." );
|
||||||
fc::stringstream ss;
|
fc::stringstream ss;
|
||||||
ss << "HTTP/1.1 " << rep.status << " ";
|
ss << "HTTP/1.1 " << rep.status << " ";
|
||||||
switch( rep.status ) {
|
switch( rep.status ) {
|
||||||
|
|
@ -55,7 +56,8 @@ namespace fc { namespace http {
|
||||||
~impl() {
|
~impl() {
|
||||||
try {
|
try {
|
||||||
tcp_serv.close();
|
tcp_serv.close();
|
||||||
accept_complete.wait();
|
if( accept_complete.valid() )
|
||||||
|
accept_complete.wait();
|
||||||
}catch(...){}
|
}catch(...){}
|
||||||
}
|
}
|
||||||
void accept_loop() {
|
void accept_loop() {
|
||||||
|
|
@ -63,7 +65,7 @@ namespace fc { namespace http {
|
||||||
{
|
{
|
||||||
http::connection_ptr con = std::make_shared<http::connection>();
|
http::connection_ptr con = std::make_shared<http::connection>();
|
||||||
tcp_serv.accept( con->get_socket() );
|
tcp_serv.accept( con->get_socket() );
|
||||||
ilog( "Accept Connection" );
|
//ilog( "Accept Connection" );
|
||||||
fc::async( [=](){ handle_connection( con, on_req ); } );
|
fc::async( [=](){ handle_connection( con, on_req ); } );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -71,14 +73,14 @@ namespace fc { namespace http {
|
||||||
void handle_connection( const http::connection_ptr& c,
|
void handle_connection( const http::connection_ptr& c,
|
||||||
std::function<void(const http::request&, const server::response& s )> do_on_req ) {
|
std::function<void(const http::request&, const server::response& s )> do_on_req ) {
|
||||||
try {
|
try {
|
||||||
http::server::response rep( fc::shared_ptr<response::impl>( new response::impl(c, [=](){ this->handle_connection(c,do_on_req); } ) ) );
|
http::server::response rep( fc::shared_ptr<response::impl>( new response::impl(c) ) );
|
||||||
auto req = c->read_request();
|
auto req = c->read_request();
|
||||||
if( do_on_req ) do_on_req( req, rep );
|
if( do_on_req ) do_on_req( req, rep );
|
||||||
c->get_socket().close();
|
c->get_socket().close();
|
||||||
} catch ( fc::exception& e ) {
|
} catch ( fc::exception& e ) {
|
||||||
wlog( "unable to read request ${1}", ("1", e.to_detail_string() ) );//fc::except_str().c_str());
|
wlog( "unable to read request ${1}", ("1", e.to_detail_string() ) );//fc::except_str().c_str());
|
||||||
}
|
}
|
||||||
wlog( "done handle connection" );
|
//wlog( "done handle connection" );
|
||||||
}
|
}
|
||||||
std::function<void(const http::request&, const server::response& s )> on_req;
|
std::function<void(const http::request&, const server::response& s )> on_req;
|
||||||
fc::tcp_server tcp_serv;
|
fc::tcp_server tcp_serv;
|
||||||
|
|
@ -86,7 +88,7 @@ namespace fc { namespace http {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
server::server(){}
|
server::server():my( new impl() ){}
|
||||||
server::server( uint16_t port ) :my( new impl(fc::ip::endpoint( fc::ip::address(),port)) ){}
|
server::server( uint16_t port ) :my( new impl(fc::ip::endpoint( fc::ip::address(),port)) ){}
|
||||||
server::server( server&& s ):my(fc::move(s.my)){}
|
server::server( server&& s ):my(fc::move(s.my)){}
|
||||||
|
|
||||||
|
|
@ -109,7 +111,6 @@ namespace fc { namespace http {
|
||||||
server::response& server::response::operator=(server::response&& s) { fc_swap(my,s.my); return *this; }
|
server::response& server::response::operator=(server::response&& s) { fc_swap(my,s.my); return *this; }
|
||||||
|
|
||||||
void server::response::add_header( const fc::string& key, const fc::string& val )const {
|
void server::response::add_header( const fc::string& key, const fc::string& val )const {
|
||||||
wlog( "Attempt to add header after sending headers" );
|
|
||||||
my->rep.headers.push_back( fc::http::header( key, val ) );
|
my->rep.headers.push_back( fc::http::header( key, val ) );
|
||||||
}
|
}
|
||||||
void server::response::set_status( const http::reply::status_code& s )const {
|
void server::response::set_status( const http::reply::status_code& s )const {
|
||||||
|
|
@ -135,7 +136,7 @@ namespace fc { namespace http {
|
||||||
my->body_bytes_sent += len;
|
my->body_bytes_sent += len;
|
||||||
my->con->get_socket().write( data, static_cast<size_t>(len) );
|
my->con->get_socket().write( data, static_cast<size_t>(len) );
|
||||||
if( my->body_bytes_sent == int64_t(my->body_length) ) {
|
if( my->body_bytes_sent == int64_t(my->body_length) ) {
|
||||||
if( my->handle_next_req ) {
|
if( false || my->handle_next_req ) {
|
||||||
ilog( "handle next request..." );
|
ilog( "handle next request..." );
|
||||||
//fc::async( std::function<void()>(my->handle_next_req) );
|
//fc::async( std::function<void()>(my->handle_next_req) );
|
||||||
fc::async( my->handle_next_req );
|
fc::async( my->handle_next_req );
|
||||||
|
|
@ -144,8 +145,11 @@ namespace fc { namespace http {
|
||||||
}
|
}
|
||||||
|
|
||||||
server::response::~response(){}
|
server::response::~response(){}
|
||||||
|
|
||||||
void server::on_request( const std::function<void(const http::request&, const server::response& s )>& cb )
|
void server::on_request( const std::function<void(const http::request&, const server::response& s )>& cb )
|
||||||
{ my->on_req = cb; }
|
{
|
||||||
|
my->on_req = cb;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue