fix http request

This commit is contained in:
Daniel Larimer 2012-11-17 17:43:34 -05:00
parent 9edcfcf947
commit 8bba5382ed
6 changed files with 67 additions and 42 deletions

View file

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

View file

@ -164,7 +164,6 @@ namespace fc {
struct cast_if_tuple { struct cast_if_tuple {
template<typename T> template<typename T>
static T cast( const value& v ) { static T cast( const value& v ) {
slog( "cast non tuple %s", typeid(T).name() );
typename fc::deduce<T>::type out; typename fc::deduce<T>::type out;
v.visit(cast_visitor<decltype(out)>(out)); v.visit(cast_visitor<decltype(out)>(out));
return 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 ) { 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 ); ::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/tcp_socket.hpp>
#include <fc/sstream.hpp> #include <fc/sstream.hpp>
#include <fc/iostream.hpp> #include <fc/iostream.hpp>
#include <fc/exception.hpp>
#include <fc/ip.hpp>
FC_START_SHARED_IMPL(fc::http::connection) FC_START_SHARED_IMPL(fc::http::connection)
fc::tcp_socket sock; fc::tcp_socket sock;
fc::ip::endpoint ep;
int read_until( char* buffer, char* end, char c = '\n' ) { int read_until( char* buffer, char* end, char c = '\n' ) {
char* p = buffer; char* p = buffer;
@ -26,6 +29,7 @@ FC_START_SHARED_IMPL(fc::http::connection)
fc::http::reply parse_reply() { fc::http::reply parse_reply() {
fc::http::reply rep; fc::http::reply rep;
//try {
fc::vector<char> line(1024*8); fc::vector<char> line(1024*8);
int s = read_until( line.data(), line.data()+line.size(), ' ' ); // HTTP/1.1 int s = read_until( line.data(), line.data()+line.size(), ' ' ); // HTTP/1.1
s = read_until( line.data(), line.data()+line.size(), ' ' ); // CODE s = read_until( line.data(), line.data()+line.size(), ' ' ); // CODE
@ -47,8 +51,17 @@ FC_START_SHARED_IMPL(fc::http::connection)
rep.body.resize( fc::lexical_cast<int>( fc::string(h.val) ) ); rep.body.resize( fc::lexical_cast<int>( fc::string(h.val) ) );
} }
} }
if( rep.body.size() ) sock.read( rep.body.data(), rep.body.size() ); if( rep.body.size() ) {
slog( "Reading body size %d", rep.body.size() );
sock.read( rep.body.data(), rep.body.size() );
}
return rep; return rep;
/* } catch ( ... ) {
elog( "%s", fc::except_str().c_str() );
sock.close();
rep.status = http::reply::InternalServerError;
return rep;
} */
} }
FC_END_SHARED_IMPL FC_END_SHARED_IMPL
@ -63,14 +76,19 @@ FC_REFERENCE_TYPE_IMPL( connection )
// used for clients // used for clients
void connection::connect_to( const fc::ip::endpoint& ep ) { 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, http::reply connection::request( const fc::string& method,
const fc::string& url, const fc::string& url,
const fc::string& body ) { const fc::string& body ) {
if( !my->sock.is_open() ) {
wlog( "Re-open socket!" );
my->sock.connect_to( my->ep );
}
//try {
fc::stringstream req; fc::stringstream req;
req << method <<" "<<url<<" HTTP/1.1\r\n"; req << method <<" "<<url<<" HTTP/1.1\r\n";
req << "Host: localhost\r\n"; req << "Host: localhost\r\n";
@ -80,13 +98,19 @@ http::reply connection::request( const fc::string& method,
fc::string head = req.str(); fc::string head = req.str();
my->sock.write( head.c_str(), head.size() ); my->sock.write( head.c_str(), head.size() );
// fc::cerr.write( head.c_str() );
if( body.size() ) { if( body.size() ) {
my->sock.write( body.c_str(), body.size() ); my->sock.write( body.c_str(), body.size() );
// fc::cerr.write( body.c_str() );
} }
fc::cerr.flush(); // fc::cerr.flush();
return my->parse_reply(); return my->parse_reply();
// } catch ( ... ) {
// my->sock.close();
// return http::reply( http::reply::InternalServerError ); // TODO: replace with connection error
// }
} }
// used for servers // 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. // now we should have a name.
if( name_end >= end -1 ) { if( name_end >= end -1 ) {
temp_set ntemp(name_end,'\0'); 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; return name_end;
} }
if( *name != '"' ) { if( *name != '"' ) {

View file

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