Merge branch 'phoenix' of https://github.com/InvictusInnovations/fc into phoenix

Conflicts:
	src/crypto/elliptic.cpp
This commit is contained in:
dnotestein 2014-03-30 18:32:20 -04:00
commit 10127a854c
7 changed files with 57 additions and 18 deletions

View file

@ -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;
@ -99,7 +100,7 @@ namespace fc {
*/ */
fc::sha512 get_shared_secret( const public_key& pub )const; 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; compact_signature sign_compact( const fc::sha256& digest )const;
bool verify( const fc::sha256& digest, const signature& sig ); bool verify( const fc::sha256& digest, const signature& sig );

View file

@ -45,7 +45,7 @@ namespace fc { namespace http {
fc::shared_ptr<impl> my; fc::shared_ptr<impl> my;
}; };
void listen( uint16_t p ); void listen( const fc::ip::endpoint& p );
/** /**
* Set the callback to be called for every http request made. * Set the callback to be called for every http request made.

View file

@ -21,6 +21,8 @@ namespace fc
class variant_object; class variant_object;
fc::string format_string( const fc::string&, const variant_object& ); fc::string format_string( const fc::string&, const variant_object& );
fc::string trim( const fc::string& ); fc::string trim( const fc::string& );
fc::string to_lower( const fc::string& );
string trim_and_normalize_spaces( const string& s );
} }
#else #else

View file

@ -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(fc::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()
{} {}
@ -357,7 +370,7 @@ namespace fc { namespace ecc {
return self; 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); unsigned int buf_len = ECDSA_size(my->_key);
// fprintf( stderr, "%d %d\n", buf_len, sizeof(sha256) ); // fprintf( stderr, "%d %d\n", buf_len, sizeof(sha256) );

View file

@ -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" ) {

View file

@ -2,6 +2,7 @@
#include <fc/thread/thread.hpp> #include <fc/thread/thread.hpp>
#include <fc/network/tcp_socket.hpp> #include <fc/network/tcp_socket.hpp>
#include <fc/io/sstream.hpp> #include <fc/io/sstream.hpp>
#include <fc/network/ip.hpp>
#include <fc/io/stdio.hpp> #include <fc/io/stdio.hpp>
#include <fc/log/logger.hpp> #include <fc/log/logger.hpp>
@ -16,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 ) {
@ -46,7 +48,7 @@ namespace fc { namespace http {
{ {
public: public:
impl(){} impl(){}
impl(uint16_t p ) { impl(const fc::ip::endpoint& p ) {
tcp_serv.listen(p); tcp_serv.listen(p);
accept_complete = fc::async([this](){ this->accept_loop(); }); accept_complete = fc::async([this](){ this->accept_loop(); });
} }
@ -54,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() {
@ -62,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 ); } );
} }
} }
@ -70,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;
@ -85,15 +88,15 @@ namespace fc { namespace http {
server::server(){} server::server():my( new impl() ){}
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( server&& s ):my(fc::move(s.my)){}
server& server::operator=(server&& s) { fc_swap(my,s.my); return *this; } server& server::operator=(server&& s) { fc_swap(my,s.my); return *this; }
server::~server(){} server::~server(){}
void server::listen( uint16_t p ) { void server::listen( const fc::ip::endpoint& p ) {
my.reset( new impl(p) ); my.reset( new impl(p) );
} }
@ -108,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 {
@ -134,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 );
@ -143,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;
}

View file

@ -104,9 +104,25 @@ namespace fc {
} }
std::string trim( const std::string& s ) std::string trim( const std::string& s )
{ {
return boost::algorithm::trim_copy(s);
/*
std::string cpy(s); std::string cpy(s);
boost::algorithm::trim(cpy); boost::algorithm::trim(cpy);
return 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 } // namespace fc