From 987568e31b9f8ec5ca6ff032fd7e6b7201b95e5a Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Thu, 27 Mar 2014 01:09:08 -0400 Subject: [PATCH 1/3] update apis --- include/fc/crypto/elliptic.hpp | 2 +- include/fc/string.hpp | 2 ++ src/crypto/elliptic.cpp | 2 +- src/string.cpp | 16 ++++++++++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/fc/crypto/elliptic.hpp b/include/fc/crypto/elliptic.hpp index ac411a0..71c4d12 100644 --- a/include/fc/crypto/elliptic.hpp +++ b/include/fc/crypto/elliptic.hpp @@ -99,7 +99,7 @@ namespace fc { */ fc::sha512 get_shared_secret( const public_key& pub )const; - signature sign( const fc::sha256& digest ); + signature sign( const fc::sha256& digest )const; compact_signature sign_compact( const fc::sha256& digest )const; bool verify( const fc::sha256& digest, const signature& sig ); diff --git a/include/fc/string.hpp b/include/fc/string.hpp index df2cac8..4ba03cd 100644 --- a/include/fc/string.hpp +++ b/include/fc/string.hpp @@ -21,6 +21,8 @@ namespace fc class variant_object; fc::string format_string( const fc::string&, const variant_object& ); fc::string trim( const fc::string& ); + fc::string to_lower( const fc::string& ); + string trim_and_normalize_spaces( const string& s ); } #else diff --git a/src/crypto/elliptic.cpp b/src/crypto/elliptic.cpp index dda84f2..b68ad0d 100644 --- a/src/crypto/elliptic.cpp +++ b/src/crypto/elliptic.cpp @@ -357,7 +357,7 @@ namespace fc { namespace ecc { return self; } - signature private_key::sign( const fc::sha256& digest ) + signature private_key::sign( const fc::sha256& digest )const { unsigned int buf_len = ECDSA_size(my->_key); // fprintf( stderr, "%d %d\n", buf_len, sizeof(sha256) ); diff --git a/src/string.cpp b/src/string.cpp index d09ab2f..ea5bbab 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -104,9 +104,25 @@ namespace fc { } std::string trim( const std::string& s ) { + return boost::algorithm::trim_copy(s); + /* std::string cpy(s); boost::algorithm::trim(cpy); return cpy; + */ + } + std::string to_lower( const std::string& s ) + { + auto tmp = s; + boost::algorithm::to_lower(tmp); + return tmp; + } + string trim_and_normalize_spaces( const string& s ) + { + string result = boost::algorithm::trim_copy( s ); + while( result.find( " " ) != result.npos ) + boost::algorithm::replace_all( result, " ", " " ); + return result; } } // namespace fc From 3c59eebe92f5488fdcf8b69c927a0df27b4f766c Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Thu, 27 Mar 2014 01:55:52 -0400 Subject: [PATCH 2/3] Update HTTP server api to specify network interface --- include/fc/network/http/server.hpp | 2 +- src/network/http/http_server.cpp | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/fc/network/http/server.hpp b/include/fc/network/http/server.hpp index 711ea50..a90764e 100644 --- a/include/fc/network/http/server.hpp +++ b/include/fc/network/http/server.hpp @@ -45,7 +45,7 @@ namespace fc { namespace http { fc::shared_ptr my; }; - void listen( uint16_t p ); + void listen( const fc::ip::endpoint& p ); /** * Set the callback to be called for every http request made. diff --git a/src/network/http/http_server.cpp b/src/network/http/http_server.cpp index 1f7578d..678f4db 100644 --- a/src/network/http/http_server.cpp +++ b/src/network/http/http_server.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -46,7 +47,7 @@ namespace fc { namespace http { { public: impl(){} - impl(uint16_t p ) { + impl(const fc::ip::endpoint& p ) { tcp_serv.listen(p); accept_complete = fc::async([this](){ this->accept_loop(); }); } @@ -86,14 +87,14 @@ namespace fc { namespace http { server::server(){} - server::server( uint16_t port ) :my( new impl(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::operator=(server&& s) { fc_swap(my,s.my); return *this; } server::~server(){} - void server::listen( uint16_t p ) { + void server::listen( const fc::ip::endpoint& p ) { my.reset( new impl(p) ); } From 5f9dfa9a420e943f026f1c213293c1e20659fe15 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Thu, 27 Mar 2014 19:53:40 -0400 Subject: [PATCH 3/3] update http code --- include/fc/crypto/elliptic.hpp | 1 + src/crypto/elliptic.cpp | 19 ++++++++++++++++--- src/network/http/http_connection.cpp | 2 ++ src/network/http/http_server.cpp | 22 +++++++++++++--------- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/include/fc/crypto/elliptic.hpp b/include/fc/crypto/elliptic.hpp index 71c4d12..2fbec10 100644 --- a/include/fc/crypto/elliptic.hpp +++ b/include/fc/crypto/elliptic.hpp @@ -60,6 +60,7 @@ namespace fc { /// Allows to convert current public key object into base58 number. std::string to_base58() const; + static public_key from_base58( const std::string& b58 ); private: friend class private_key; diff --git a/src/crypto/elliptic.cpp b/src/crypto/elliptic.cpp index b68ad0d..ac79aed 100644 --- a/src/crypto/elliptic.cpp +++ b/src/crypto/elliptic.cpp @@ -262,15 +262,28 @@ namespace fc { namespace ecc { } std::string public_key::to_base58() const - { + { 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); array data; memcpy(data.data, key.begin(), key.size()); memcpy(data.begin() + key.size(), (const char*)&check, sizeof(check)); return fc::to_base58(data.begin(), data.size()); - } + } + + public_key public_key::from_base58( const std::string& b58 ) + { + array 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() {} diff --git a/src/network/http/http_connection.cpp b/src/network/http/http_connection.cpp index af0a91b..7921bf0 100644 --- a/src/network/http/http_connection.cpp +++ b/src/network/http/http_connection.cpp @@ -152,6 +152,8 @@ http::request connection::read_request()const { h.val = fc::string(skey,end); req.headers.push_back(h); if( h.key == "Content-Length" ) { + auto s = static_cast(to_uint64( fc::string(h.val) ) ); + FC_ASSERT( s < 1024*1024 ); req.body.resize( static_cast(to_uint64( fc::string(h.val) ) )); } if( h.key == "Host" ) { diff --git a/src/network/http/http_server.cpp b/src/network/http/http_server.cpp index 678f4db..28a12cd 100644 --- a/src/network/http/http_server.cpp +++ b/src/network/http/http_server.cpp @@ -17,6 +17,7 @@ namespace fc { namespace http { {} void send_header() { + //ilog( "sending header..." ); fc::stringstream ss; ss << "HTTP/1.1 " << rep.status << " "; switch( rep.status ) { @@ -55,7 +56,8 @@ namespace fc { namespace http { ~impl() { try { tcp_serv.close(); - accept_complete.wait(); + if( accept_complete.valid() ) + accept_complete.wait(); }catch(...){} } void accept_loop() { @@ -63,7 +65,7 @@ namespace fc { namespace http { { http::connection_ptr con = std::make_shared(); tcp_serv.accept( con->get_socket() ); - ilog( "Accept Connection" ); + //ilog( "Accept Connection" ); fc::async( [=](){ handle_connection( con, on_req ); } ); } } @@ -71,14 +73,14 @@ namespace fc { namespace http { void handle_connection( const http::connection_ptr& c, std::function do_on_req ) { try { - http::server::response rep( fc::shared_ptr( new response::impl(c, [=](){ this->handle_connection(c,do_on_req); } ) ) ); + http::server::response rep( fc::shared_ptr( new response::impl(c) ) ); auto req = c->read_request(); if( do_on_req ) do_on_req( req, rep ); - c->get_socket().close(); + c->get_socket().close(); } catch ( fc::exception& e ) { 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 on_req; 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( 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; } 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 ) ); } 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->con->get_socket().write( data, static_cast(len) ); 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..." ); //fc::async( std::function(my->handle_next_req) ); fc::async( my->handle_next_req ); @@ -144,8 +145,11 @@ namespace fc { namespace http { } server::response::~response(){} + void server::on_request( const std::function& cb ) - { my->on_req = cb; } + { + my->on_req = cb; + }