From 8bba5382ed80f4b162881186a17d3ffcf969330b Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Sat, 17 Nov 2012 17:43:34 -0500 Subject: [PATCH 1/2] fix http request --- include/fc/http/connection.hpp | 1 + include/fc/value_cast.hpp | 1 - src/exception.cpp | 3 +- src/http_connection.cpp | 100 ++++++++++++++++++++------------- src/json.cpp | 2 +- src/tcp_socket.cpp | 2 +- 6 files changed, 67 insertions(+), 42 deletions(-) diff --git a/include/fc/http/connection.hpp b/include/fc/http/connection.hpp index 0f33ed4..e975eda 100644 --- a/include/fc/http/connection.hpp +++ b/include/fc/http/connection.hpp @@ -22,6 +22,7 @@ namespace fc { Found = 302, InternalServerError = 500 }; + reply( status_code c = OK):status(c){} int status; fc::vector
headers; fc::vector body; diff --git a/include/fc/value_cast.hpp b/include/fc/value_cast.hpp index 19f78fb..6688b45 100644 --- a/include/fc/value_cast.hpp +++ b/include/fc/value_cast.hpp @@ -164,7 +164,6 @@ namespace fc { struct cast_if_tuple { template static T cast( const value& v ) { - slog( "cast non tuple %s", typeid(T).name() ); typename fc::deduce::type out; v.visit(cast_visitor(out)); return out; diff --git a/src/exception.cpp b/src/exception.cpp index d69d34c..9b1f64d 100644 --- a/src/exception.cpp +++ b/src/exception.cpp @@ -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(); } } diff --git a/src/http_connection.cpp b/src/http_connection.cpp index 28f4231..621b1ad 100644 --- a/src/http_connection.cpp +++ b/src/http_connection.cpp @@ -2,10 +2,13 @@ #include #include #include +#include +#include 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 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(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( fc::string(h.val) ) ); + //try { + fc::vector 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(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( 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 <<" "<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 <<" "<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 diff --git a/src/json.cpp b/src/json.cpp index 8097b03..e881ce1 100644 --- a/src/json.cpp +++ b/src/json.cpp @@ -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 != '"' ) { diff --git a/src/tcp_socket.cpp b/src/tcp_socket.cpp index a4e089e..3613e7b 100644 --- a/src/tcp_socket.cpp +++ b/src/tcp_socket.cpp @@ -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; From 3b7827ec0a9baadfc29c871590b9801132fc46c8 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Sun, 18 Nov 2012 01:07:10 -0500 Subject: [PATCH 2/2] update string format --- CMakeLists.txt | 4 ++-- src/time.cpp | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fdf7fc3..b636d6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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} ) diff --git a/src/time.cpp b/src/time.cpp index 0574e4c..1093a47 100644 --- a/src/time.cpp +++ b/src/time.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -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<