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,
InternalServerError = 500
};
reply( status_code c = OK):status(c){}
int status;
fc::vector<header> headers;
fc::vector<char> body;

View file

@ -164,7 +164,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;