Merge branch 'master' of github.com:bytemaster/fc

This commit is contained in:
Daniel Larimer 2012-12-17 14:07:02 -05:00
commit 274146229e
19 changed files with 207 additions and 44 deletions

View file

@ -48,6 +48,7 @@ include_directories( include )
set( sources
src/ssh.cpp
src/url.cpp
src/process.cpp
src/http_connection.cpp
src/url.cpp

View file

@ -1,9 +1,8 @@
#ifndef _FC_BASE58_HPP_
#define _FC_BASE58_HPP_
#pragma once
#include <fc/string.hpp>
namespace fc {
fc::string to_base58( const char* d, uint32_t s );
size_t from_base58( const fc::string& base58_str, char* out_data, size_t out_data_len );
fc::string to_base58( const char* d, uint32_t s );
fc::vector<char> from_base58( const fc::string& base58_str );
size_t from_base58( const fc::string& base58_str, char* out_data, size_t out_data_len );
}
#endif // _FC_BASE58_HPP_

View file

@ -1,7 +1,6 @@
#ifndef _FC_DATASTREAM_HPP_
#define _FC_DATASTREAM_HPP_
#pragma once
#include <fc/utility.hpp>
#include <fc/exception.hpp>
#include <fc/error_report.hpp>
#include <string.h>
#include <stdint.h>
@ -40,8 +39,8 @@ struct datastream {
m_pos += s;
return true;
}
FC_THROW_MSG( "Attempt to read %s bytes beyond end of buffer of size %s",
int64_t(-((m_end-m_pos) - s)), int64_t(m_end-m_start) );
FC_THROW_REPORT( "Attempt to read ${bytes_past} bytes beyond end of buffer with size ${buffer_size} ",
fc::value().set("bytes_past",int64_t(-((m_end-m_pos) - s))).set("buffer_size", int64_t(m_end-m_start)) );
return false;
}
@ -51,7 +50,8 @@ struct datastream {
m_pos += s;
return true;
}
FC_THROW_MSG( "Attempt to write %s bytes beyond end of buffer of size %s", int64_t(-((m_end-m_pos) - s)),int64_t(m_end-m_start) );
FC_THROW_REPORT( "Attempt to write ${bytes_past} bytes beyond end of buffer with size ${buffer_size} ",
fc::value().set("bytes_past",int64_t(-((m_end-m_pos) - s))).set("buffer_size", int64_t(m_end-m_start)) );
return false;
}
@ -61,7 +61,8 @@ struct datastream {
++m_pos;
return true;
}
FC_THROW_MSG( "Attempt to write %s byte beyond end of buffer of size %s", int64_t(-((m_end-m_pos) - 1)), int64_t(m_end-m_start) );
FC_THROW_REPORT( "Attempt to write ${bytes_past} bytes beyond end of buffer with size ${buffer_size} ",
fc::value().set("bytes_past",int64_t(-((m_end-m_pos) - 1))).set("buffer_size", int64_t(m_end-m_start)) );
return false;
}
@ -72,7 +73,8 @@ struct datastream {
++m_pos;
return true;
}
FC_THROW_MSG( "Attempt to read %s byte beyond end of buffer of size %s", int64_t(-((m_end-m_pos) - 1)), int64_t(m_end-m_start) );
FC_THROW_REPORT( "Attempt to read ${bytes_past} bytes beyond end of buffer of size ${buffer_size} ",
fc::value().set("bytes_past",int64_t(-((m_end-m_pos) - 1))).set("buffer_size", int64_t(m_end-m_start)) );
++m_pos;
return false;
}
@ -111,4 +113,3 @@ private:
} // namespace fc
#endif

View file

@ -1,5 +1,4 @@
#ifndef _FC_FILESYSTEM_HPP_
#define _FC_FILESYSTEM_HPP_
#pragma once
#include <fc/string.hpp>
#include <fc/fwd.hpp>
@ -70,6 +69,9 @@ namespace fc {
path unique_path();
path temp_directory_path();
class value;
void pack( fc::value& , const fc::path& );
void unpack( const fc::value& , fc::path& );
}
#endif // _FC_FILESYSTEM_HPP_

View file

@ -32,6 +32,7 @@ namespace fc {
};
struct request {
fc::string get_header( const fc::string& key )const;
fc::string method;
fc::string domain;
fc::string path;
@ -39,6 +40,8 @@ namespace fc {
fc::vector<char> body;
};
fc::vector<header> parse_urlencoded_params( const fc::string& f );
/**
* Connections have reference semantics, all copies refer to the same
* underlying socket.

View file

@ -1,5 +1,4 @@
#ifndef _FC_IP_HPP_
#define _FC_IP_HPP_
#pragma once
#include <fc/string.hpp>
namespace fc {
@ -47,5 +46,7 @@ namespace fc {
uint32_t _port;
};
}
class value;
void pack( fc::value& , const fc::ip::endpoint& );
void unpack( const fc::value& , fc::ip::endpoint& );
}
#endif // _FC_ENDPOINT_HPP_

View file

@ -1,7 +1,7 @@
#ifndef FC_PKE_HPP_
#define FC_PKE_HPP_
#pragma once
#include <fc/sha1.hpp>
#include <fc/vector.hpp>
#include <fc/reflect.hpp>
/**
* Define common crypto methods and data types to abstract underlying implementation.
@ -143,7 +143,13 @@ namespace fc {
typedef private_key<> private_key_t;
typedef signature<> signature_t;
class value;
void pack( fc::value& , const fc::signature_t& );
void unpack( const fc::value& , fc::signature_t& );
void pack( fc::value& , const fc::public_key_t& );
void unpack( const fc::value& , fc::private_key_t& );
void pack( fc::value& , const fc::private_key_t& );
void unpack( const fc::value& , fc::public_key_t& );
} // namespace fc
#endif

View file

@ -1,5 +1,4 @@
#ifndef _TORNET_RPC_RAW_HPP_
#define _TORNET_RPC_RAW_HPP_
#pragma once
#include <fc/reflect.hpp>
#include <fc/datastream.hpp>
#include <fc/varint.hpp>
@ -7,7 +6,6 @@
#include <fc/vector.hpp>
#include <fc/fwd.hpp>
#include <fc/array.hpp>
//#include <fc/value.hpp>
namespace fc {
class value;
@ -280,4 +278,3 @@ namespace fc {
} } // namespace fc::raw
#endif // BOOST_RPC_RAW_HPP

View file

@ -61,12 +61,13 @@ namespace fc {
public:
template<typename Functor>
task( Functor&& f ):task_base(&_functor) {
typedef typename fc::deduce<Functor>::type FunctorType;
static_assert( sizeof(f) <= sizeof(_functor), "sizeof(Functor) is larger than FunctorSize" );
new ((char*)&_functor) Functor( fc::forward<Functor>(f) );
_destroy_functor = &detail::functor_destructor<Functor>::destroy;
new ((char*)&_functor) FunctorType( fc::forward<Functor>(f) );
_destroy_functor = &detail::functor_destructor<FunctorType>::destroy;
_promise_impl = static_cast<promise<R>*>(this);
_run_functor = &detail::functor_run<Functor>::run;
_run_functor = &detail::functor_run<FunctorType>::run;
}
aligned<FunctorSize> _functor;
private:
@ -77,12 +78,13 @@ namespace fc {
public:
template<typename Functor>
task( Functor&& f ):task_base(&_functor) {
typedef typename fc::deduce<Functor>::type FunctorType;
static_assert( sizeof(f) <= sizeof(_functor), "sizeof(Functor) is larger than FunctorSize" );
new ((char*)&_functor) Functor( fc::forward<Functor>(f) );
_destroy_functor = &detail::functor_destructor<Functor>::destroy;
new ((char*)&_functor) FunctorType( fc::forward<Functor>(f) );
_destroy_functor = &detail::functor_destructor<FunctorType>::destroy;
_promise_impl = static_cast<promise<void>*>(this);
_run_functor = &detail::void_functor_run<Functor>::run;
_run_functor = &detail::void_functor_run<FunctorType>::run;
}
aligned<FunctorSize> _functor;
private:

View file

@ -52,8 +52,9 @@ namespace fc {
template<typename Functor>
auto async( Functor&& f, const char* desc ="", priority prio = priority()) -> fc::future<decltype(f())> {
typedef decltype(f()) Result;
fc::task<Result,sizeof(Functor)>* tsk =
new fc::task<Result,sizeof(Functor)>( fc::forward<Functor>(f) );
typedef typename fc::deduce<Functor>::type FunctorType;
fc::task<Result,sizeof(FunctorType)>* tsk =
new fc::task<Result,sizeof(FunctorType)>( fc::forward<Functor>(f) );
fc::future<Result> r(fc::shared_ptr< fc::promise<Result> >(tsk,true) );
async_task(tsk,prio,desc);
return r;

View file

@ -1,7 +1,7 @@
#ifndef _FC_TIME_HPP_
#define _FC_TIME_HPP_
#pragma once
#include <stdint.h>
#include <fc/string.hpp>
#include <fc/raw.hpp>
namespace fc {
class microseconds {
@ -41,5 +41,16 @@ namespace fc {
private:
microseconds elapsed;
};
// forward declare io
class value;
void pack( fc::value& , const fc::time_point& );
void unpack( const fc::value& , fc::time_point& );
namespace raw {
template<typename Stream, typename T>
void unpack( Stream& s, fc::time_point& v );
template<typename Stream, typename T>
void pack( Stream& s, const fc::time_point& v );
}
}
#endif // _FC_TIME_HPP_

18
include/fc/time_io.hpp Normal file
View file

@ -0,0 +1,18 @@
#pragma once
#include <fc/time_point.hpp>
#include <fc/raw.hpp>
namespace fc {
namespace raw {
template<typename Stream, typename T>
void unpack( Stream& s, fc::time_point& v ) {
int64_t micro;
fc::raw::unpack(s, micro );
v = fc::time_point( fc::microseconds(micro);
}
template<typename Stream, typename T>
void pack( Stream& s, const fc::time_point& v ) {
fc::raw::pack( s, v.time_since_epoch().count() );
}
}
}

View file

@ -24,6 +24,7 @@
#include <fc/log.hpp>
#include <fc/string.hpp>
#include <fc/exception.hpp>
#include <fc/error_report.hpp>
#include <stdexcept>
#include <vector>
@ -609,6 +610,13 @@ fc::string to_base58( const char* d, uint32_t s ) {
return EncodeBase58( (const unsigned char*)d, (const unsigned char*)d+s ).c_str();
}
fc::vector<char> from_base58( const fc::string& base58_str ) {
std::vector<unsigned char> out;
if( !DecodeBase58( base58_str.c_str(), out ) ) {
FC_THROW_REPORT( "Unable to decode base58 string ${base58_str}", fc::value().set("base58_str",base58_str) );
}
return fc::vector<char>((const char*)out.data(), ((const char*)out.data())+out.size() );
}
/**
* @return the number of bytes decoded
*/
@ -616,7 +624,7 @@ size_t from_base58( const fc::string& base58_str, char* out_data, size_t out_dat
//slog( "%s", base58_str.c_str() );
std::vector<unsigned char> out;
if( !DecodeBase58( base58_str.c_str(), out ) ) {
FC_THROW_MSG( "Unable to decode base58 string '%s'", base58_str );
FC_THROW_REPORT( "Unable to decode base58 string ${base58_str}", fc::value().set("base58_str",base58_str) );
}
memcpy( out_data, out.data(), out.size() );

View file

@ -86,8 +86,9 @@ fc::string error_frame::to_string()const {
int64_t prev = 0;
auto next = desc.find( '$' );
while( prev != int64_t(fc::string::npos) && prev < int64_t(desc.size()) ) {
// slog( "prev: %d next %d %s", prev, next, desc.substr(prev,next).c_str() );
// print everything from the last pos until the first '$'
ss << desc.substr( prev, next );
ss << desc.substr( prev, next-prev );
// if we got to the end, return it.
if( next == string::npos ) { return ss.str(); }
@ -102,6 +103,7 @@ fc::string error_frame::to_string()const {
if( next != fc::string::npos ) {
// the key is between prev and next
fc::string key = desc.substr( prev+1, (next-prev-1) );
//slog( "key '%s'", key.c_str() );
if( meta ) {
auto itr = meta->find( key.c_str() );
if( itr != meta->end() ) {

View file

@ -4,8 +4,15 @@
#include <fc/utility.hpp>
#include <boost/config.hpp>
#include <boost/filesystem.hpp>
#include <fc/value_cast.hpp>
namespace fc {
void pack( fc::value& v, const fc::path& s ) {
v = s.generic_string();
}
void unpack( const fc::value& v, fc::path& s ) {
s = path(fc::value_cast<fc::string>(v));
}
path::path(){}
path::~path(){};

View file

@ -4,6 +4,8 @@
#include <fc/iostream.hpp>
#include <fc/exception.hpp>
#include <fc/ip.hpp>
#include <fc/error_report.hpp>
#include <fc/hex.hpp>
FC_START_SHARED_IMPL(fc::http::connection)
@ -154,4 +156,42 @@ http::request connection::read_request()const {
return req;
}
fc::string request::get_header( const fc::string& key )const {
for( auto itr = headers.begin(); itr != headers.end(); ++itr ) {
if( itr->key == key ) { return itr->val; }
}
return fc::string();
}
fc::vector<header> parse_urlencoded_params( const fc::string& f ) {
int num_args = 0;
for( size_t i = 0; i < f.size(); ++i ) {
if( f[i] == '=' ) ++num_args;
}
fc::vector<header> h(num_args);
int arg = 0;
for( size_t i = 0; i < f.size(); ++i ) {
while( f[i] != '=' && i < f.size() ) {
if( f[i] == '%' ) {
h[arg].key += char((fc::from_hex(f[i+1]) << 4) | fc::from_hex(f[i+2]));
i += 3;
} else {
h[arg].key += f[i];
++i;
}
}
++i;
while( i < f.size() && f[i] != '&' ) {
if( f[i] == '%' ) {
h[arg].val += char((fc::from_hex(f[i+1]) << 4) | fc::from_hex(f[i+2]));
i += 3;
} else {
h[arg].val += f[i] == '+' ? ' ' : f[i];
++i;
}
}
++arg;
}
return h;
}
} } // fc::http

View file

@ -56,6 +56,7 @@ namespace fc { namespace http {
try {
http::connection con;
while( tcp_serv.accept( con.get_socket() ) ) {
slog( "Accept Connection" );
fc::async( [=](){ handle_connection( con, on_req ); } );
con = http::connection();
}
@ -66,9 +67,14 @@ namespace fc { namespace http {
void handle_connection( const http::connection& c,
std::function<void(const http::request&, const server::response& s )> do_on_req ) {
http::server::response rep( fc::shared_ptr<response::impl>( new response::impl(c, [=](){ this->handle_connection(c,do_on_req); } ) ) );
auto req = c.read_request();
if( do_on_req ) do_on_req( req, rep );
wlog( "reading request.." );
try {
http::server::response rep( fc::shared_ptr<response::impl>( new response::impl(c, [=](){ this->handle_connection(c,do_on_req); } ) ) );
auto req = c.read_request();
if( do_on_req ) do_on_req( req, rep );
} catch ( ... ) {
wlog( "unable to read request %s", fc::except_str().c_str());
}
}
std::function<void(const http::request&, const server::response& s )> on_req;
fc::tcp_server tcp_serv;
@ -126,6 +132,13 @@ namespace fc { namespace http {
}
my->body_bytes_sent += len;
my->con.get_socket().write( data, len );
if( my->body_bytes_sent == int64_t(my->body_length) ) {
if( my->handle_next_req ) {
slog( "handle next request..." );
//fc::async( std::function<void()>(my->handle_next_req) );
fc::async( my->handle_next_req );
}
}
}
server::response::~response(){}

View file

@ -1,4 +1,5 @@
#include <fc/ip.hpp>
#include <fc/value_cast.hpp>
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <string>
@ -53,4 +54,12 @@ namespace fc { namespace ip {
return string(_ip) + ':' + fc::string(boost::lexical_cast<std::string>(_port).c_str());
}
} }
}
void pack( fc::value& v, const fc::ip::endpoint& s ) {
v = fc::string(s);
}
void unpack( const fc::value& v, fc::ip::endpoint& s ) {
s = fc::ip::endpoint::from_string(fc::value_cast<fc::string>(v));
}
}

View file

@ -1,6 +1,10 @@
#include <fc/pke.hpp>
#include <fc/error.hpp>
#include <fc/exception.hpp>
#include <fc/raw.hpp>
#include <fc/value.hpp>
#include <fc/value_cast.hpp>
#include <fc/base58.hpp>
#include <iostream>
#include <fc/sha1.hpp>
#include <openssl/rsa.h>
@ -10,6 +14,44 @@
#include <openssl/err.h>
namespace fc {
void pack( fc::value& v, const fc::public_key_t& s ) {
fc::vector<char> ve = fc::raw::pack( s );
v = to_base58( ve.data(), ve.size() );
}
void unpack( const fc::value& v, fc::public_key_t& s ) {
try {
auto ve = from_base58(fc::value_cast<fc::string>(v));
s = fc::raw::unpack<public_key_t>(ve);
} catch ( ... ) {
wlog( "error unpacking signature" );
}
}
void pack( fc::value& v, const fc::private_key_t& s ) {
fc::vector<char> ve = fc::raw::pack( s );
v = to_base58( ve.data(), ve.size() );
}
void unpack( const fc::value& v, fc::private_key_t& s ) {
try {
auto ve = from_base58(fc::value_cast<fc::string>(v));
s = fc::raw::unpack<private_key_t>(ve);
} catch ( ... ) {
wlog( "error unpacking private_key" );
}
}
void pack( fc::value& v, const fc::signature_t& s ) {
fc::vector<char> ve = fc::raw::pack( s );
v = to_base58( ve.data(), ve.size() );
}
void unpack( const fc::value& v, fc::signature_t& s ) {
try {
auto ve = from_base58(fc::value_cast<fc::string>(v));
s = fc::raw::unpack<signature_t>(ve);
} catch ( ... ) {
wlog( "error unpacking signature" );
}
}
RSA* get_pub( const char* key, uint32_t key_size, uint32_t pe )
{
RSA* rsa = RSA_new();