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

This commit is contained in:
Daniel Larimer 2012-11-23 17:18:43 -05:00
commit 5719fa0e05
8 changed files with 71 additions and 47 deletions

View file

@ -94,8 +94,8 @@ set( sources
setup_library( fc SOURCES ${sources} )
setup_executable( json_rpc_test SOURCES tests/json_rpc_test.cpp LIBRARIES fc ${pthread_library} ${rt_library} ${Boost_THREAD_LIBRARY} ${Boost_CONTEXT_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_CHRONO_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${rt_library} )
setup_executable( ssh_test SOURCES tests/ssh.cpp LIBRARIES fc ${pthread_library} ${rt_library} ${Boost_THREAD_LIBRARY} ${Boost_CONTEXT_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_CHRONO_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${rt_library} ssh2 ${OPENSSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY} z)
setup_executable( json_rpc_test SOURCES tests/json_rpc_test.cpp LIBRARIES fc ${pthread_library} ${rt_library} ${Boost_THREAD_LIBRARY} ${Boost_CONTEXT_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_CHRONO_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${rt_library} ${Boost_DATE_TIME_LIBRARY})
setup_executable( ssh_test SOURCES tests/ssh.cpp LIBRARIES fc ${pthread_library} ${rt_library} ${Boost_THREAD_LIBRARY} ${Boost_CONTEXT_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_CHRONO_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${rt_library} ssh2 ${OPENSSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY} z ${Boost_DATE_TIME_LIBRARY})
#add_executable( test_vec tests/vector_test.cpp )
#target_link_libraries( test_vec fc ${Boost_SYSTEM_LIBRARY} ${Boost_CHRONO_LIBRARY} ${Boost_THREAD_LIBRARY} ${Boost_CONTEXT_LIBRARY} )

View file

@ -22,6 +22,7 @@ namespace fc {
Found = 302,
InternalServerError = 500
};
reply( status_code c = OK):status(c){}
int status;
fc::vector<header> headers;
fc::vector<char> body;

View file

@ -186,7 +186,6 @@ namespace fc {
struct cast_if_tuple {
template<typename T>
static T cast( const value& v ) {
slog( "cast non tuple %s", typeid(T).name() );
typename fc::deduce<T>::type out;
v.visit(cast_visitor<decltype(out)>(out));
return out;

View file

@ -102,7 +102,8 @@ namespace fc {
const fc::string& a1, const fc::string& a2, const fc::string& a3, const fc::string& a4 ) {
::boost::exception_detail::throw_exception_(fc::generic_exception(msg),func, file, line );
}
fc::string except_str() { return fc::current_exception().diagnostic_information(); }
fc::string except_str() { return boost::current_exception_diagnostic_information(); }//boost::current_exception_diagonstic_information(); }
//fc::current_exception().diagnostic_information(); }
}

View file

@ -2,10 +2,13 @@
#include <fc/tcp_socket.hpp>
#include <fc/sstream.hpp>
#include <fc/iostream.hpp>
#include <fc/exception.hpp>
#include <fc/ip.hpp>
FC_START_SHARED_IMPL(fc::http::connection)
fc::tcp_socket sock;
fc::ip::endpoint ep;
int read_until( char* buffer, char* end, char c = '\n' ) {
char* p = buffer;
@ -26,29 +29,39 @@ FC_START_SHARED_IMPL(fc::http::connection)
fc::http::reply parse_reply() {
fc::http::reply rep;
fc::vector<char> line(1024*8);
int s = read_until( line.data(), line.data()+line.size(), ' ' ); // HTTP/1.1
s = read_until( line.data(), line.data()+line.size(), ' ' ); // CODE
rep.status = fc::lexical_cast<int>(fc::string(line.data()));
s = read_until( line.data(), line.data()+line.size(), '\n' ); // DESCRIPTION
while( (s = read_until( line.data(), line.data()+line.size(), '\n' )) > 1 ) {
fc::http::header h;
char* end = line.data();
while( *end != ':' )++end;
h.key = fc::string(line.data(),end);
++end; // skip ':'
++end; // skip space
char* skey = end;
while( *end != '\r' ) ++end;
h.val = fc::string(skey,end);
rep.headers.push_back(h);
if( h.key == "Content-Length" ) {
rep.body.resize( fc::lexical_cast<int>( fc::string(h.val) ) );
//try {
fc::vector<char> line(1024*8);
int s = read_until( line.data(), line.data()+line.size(), ' ' ); // HTTP/1.1
s = read_until( line.data(), line.data()+line.size(), ' ' ); // CODE
rep.status = fc::lexical_cast<int>(fc::string(line.data()));
s = read_until( line.data(), line.data()+line.size(), '\n' ); // DESCRIPTION
while( (s = read_until( line.data(), line.data()+line.size(), '\n' )) > 1 ) {
fc::http::header h;
char* end = line.data();
while( *end != ':' )++end;
h.key = fc::string(line.data(),end);
++end; // skip ':'
++end; // skip space
char* skey = end;
while( *end != '\r' ) ++end;
h.val = fc::string(skey,end);
rep.headers.push_back(h);
if( h.key == "Content-Length" ) {
rep.body.resize( fc::lexical_cast<int>( fc::string(h.val) ) );
}
}
}
if( rep.body.size() ) sock.read( rep.body.data(), rep.body.size() );
return rep;
if( rep.body.size() ) {
slog( "Reading body size %d", rep.body.size() );
sock.read( rep.body.data(), rep.body.size() );
}
return rep;
/* } catch ( ... ) {
elog( "%s", fc::except_str().c_str() );
sock.close();
rep.status = http::reply::InternalServerError;
return rep;
} */
}
FC_END_SHARED_IMPL
@ -63,30 +76,41 @@ FC_REFERENCE_TYPE_IMPL( connection )
// used for clients
void connection::connect_to( const fc::ip::endpoint& ep ) {
my->sock.connect_to( ep );
my->sock.close();
my->sock.connect_to( my->ep = ep );
}
http::reply connection::request( const fc::string& method,
const fc::string& url,
const fc::string& body ) {
fc::stringstream req;
req << method <<" "<<url<<" HTTP/1.1\r\n";
req << "Host: localhost\r\n";
req << "Content-Type: application/json\r\n";
if( body.size() ) req << "Content-Length: "<< body.size() << "\r\n";
req << "\r\n";
fc::string head = req.str();
my->sock.write( head.c_str(), head.size() );
if( body.size() ) {
my->sock.write( body.c_str(), body.size() );
if( !my->sock.is_open() ) {
wlog( "Re-open socket!" );
my->sock.connect_to( my->ep );
}
fc::cerr.flush();
//try {
fc::stringstream req;
req << method <<" "<<url<<" HTTP/1.1\r\n";
req << "Host: localhost\r\n";
req << "Content-Type: application/json\r\n";
if( body.size() ) req << "Content-Length: "<< body.size() << "\r\n";
req << "\r\n";
fc::string head = req.str();
return my->parse_reply();
my->sock.write( head.c_str(), head.size() );
// fc::cerr.write( head.c_str() );
if( body.size() ) {
my->sock.write( body.c_str(), body.size() );
// fc::cerr.write( body.c_str() );
}
// fc::cerr.flush();
return my->parse_reply();
// } catch ( ... ) {
// my->sock.close();
// return http::reply( http::reply::InternalServerError ); // TODO: replace with connection error
// }
}
// used for servers

View file

@ -603,7 +603,7 @@ char* read_key_val( value::object& obj, bool sc, char* in, char* end, fc::json::
// now we should have a name.
if( name_end >= end -1 ) {
temp_set ntemp(name_end,'\0');
elog( "early end after reading name %1%", name );
elog( "early end after reading name '%s'", name );
return name_end;
}
if( *name != '"' ) {

View file

@ -51,7 +51,7 @@ namespace fc {
});
p->wait();
} else if( ec ) {
wlog( "throw" );
elog( "throw... write data failed %s", boost::system::system_error(ec).what() );
throw boost::system::system_error(ec);
}
return *this;

View file

@ -1,4 +1,5 @@
#include <boost/chrono/system_clocks.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <fc/time.hpp>
#include <sstream>
@ -11,8 +12,6 @@ namespace fc {
bch::system_clock::time_point tp;
tp += bch::microseconds( elapsed._count);
time_t tt = bch::system_clock::to_time_t(tp);
std::stringstream ss;
ss<<tt;
return ss.str();
return boost::posix_time::to_iso_string( boost::posix_time::from_time_t(tt) );
}
}