diff options
Diffstat (limited to 'lib/icap/util.cpp')
-rw-r--r-- | lib/icap/util.cpp | 127 |
1 files changed, 63 insertions, 64 deletions
diff --git a/lib/icap/util.cpp b/lib/icap/util.cpp index c383261..91a73e1 100644 --- a/lib/icap/util.cpp +++ b/lib/icap/util.cpp @@ -55,41 +55,45 @@ namespace icap { } - int read_line( socketlibrary::TCPSocket * socket, char * buf, int buf_length, bool incl_endl ) throw() { + int read_line( psocksxx::iosockstream * socket, char * buf, int buf_length, bool incl_endl ) throw() { - int i = 0, n; - char c = '\0'; + int i = 0; + char c = '\0'; + char c_last = '\0'; while ( i < ( buf_length - 1 ) ) { - n = socket->recv( &c, 1 ); - - if ( n > 0 ) { - if ( c == '\r' ) { + // read a char + if ( ( c = socket->get() ) > 0 ) { - if ( incl_endl ) { - buf[i] = c; - i++; - } + // check last read char for \r + if ( c_last == '\r' ) { - // peak for \n - n = socket->peek( &c, 1 ); - - if ( ( n > 0 ) && ( c == '\n' ) ) { - - n = socket->recv( &c, 1 ); + // check for \n + if ( c == '\n' ) { + // include \r\n in the read buffer? if ( incl_endl ) { buf[i] = c; i++; + } else { + + // remove \r + i--; + } - break; // end of line + break; + } + } buf[i] = c; i++; + + c_last = c; + } else { break; // nothing read from socket } @@ -102,52 +106,44 @@ namespace icap { } - std::string read_line( socketlibrary::TCPSocket * socket, bool incl_endl ) throw() { + std::string read_line( psocksxx::iosockstream * socket, bool incl_endl ) throw() { int n; std::string line; char c = '\0'; + char c_last = '\0'; - try { - while ( ( n = socket->recv( &c, 1 ) ) > 0 ) { + while ( ( c = socket->get() ) > 0 ) { - if ( c == '\r' ) { + if ( c_last == '\r' ) { + + if ( c == '\n' ) { if ( incl_endl ) { line += c; + } else { + line.erase( line.size() - 1 ); } - // peak for \n - n = socket->peek( &c, 1 ); - - if ( ( n > 0 ) && ( c == '\n' ) ) { - - n = socket->recv( &c, 1 ); - - if ( incl_endl ) { - line += c; - } + break; - break; // end of line - } } - line += c; - } - } catch ( socketlibrary::SocketException &sex ) { - // TODO: log error? - line = ""; + line += c; + c_last = c; + } + return line; } - std::string read_data( socketlibrary::TCPSocket * socket, int size ) throw() { + std::string read_data( psocksxx::iosockstream * socket, int size ) throw() { char buffer[ICAP_BUFFER_SIZE]; std::string data = ""; @@ -159,20 +155,20 @@ namespace icap { try { // read from socket - n = socket->recv( buffer, min( size, ICAP_BUFFER_SIZE ) ); + socket->read( buffer, std::min( size, ICAP_BUFFER_SIZE ) ); // sanity check - if ( n == 0 ) { + if (! socket->good() ) { break; } // append to data - data.append( buffer, n ); + data.append( buffer, socket->gcount() ); // update size with remaining bytes - size -= n; + size -= socket->gcount(); - } catch ( socketlibrary::SocketException &sex ) { + } catch ( psocksxx::sockexception &e ) { // TODO: log errors ?? } @@ -183,7 +179,7 @@ namespace icap { } - unsigned int read_chunk_size( socketlibrary::TCPSocket * socket ) throw() { + unsigned int read_chunk_size( psocksxx::iosockstream * socket ) throw() { std::string line; std::vector<std::string> chunk_header; @@ -196,7 +192,7 @@ namespace icap { } - void read_chunk_header( socketlibrary::TCPSocket * socket, chunk_t &chunk ) throw() { + void read_chunk_header( psocksxx::iosockstream * socket, chunk_t &chunk ) throw() { std::string line; std::vector<std::string> chunk_header; @@ -223,7 +219,7 @@ namespace icap { } - chunk_t read_chunk( socketlibrary::TCPSocket * socket ) throw() { + chunk_t read_chunk( psocksxx::iosockstream * socket ) throw() { chunk_t chunk; std::string line; @@ -250,7 +246,7 @@ namespace icap { } - std::string read_chunked( socketlibrary::TCPSocket * socket ) throw() { + std::string read_chunked( psocksxx::iosockstream * socket ) throw() { unsigned int chunk_size = 0; unsigned int offset = 0; @@ -282,7 +278,7 @@ namespace icap { } - bool read_chunked_payload( socketlibrary::TCPSocket * socket, std::string &payload ) throw() { + bool read_chunked_payload( psocksxx::iosockstream * socket, std::string &payload ) throw() { chunk_t chunk; bool ieof = false; @@ -313,12 +309,12 @@ namespace icap { } - bool send_line( const std::string &line, socketlibrary::TCPSocket * socket ) throw() { + bool send_line( const std::string &line, psocksxx::iosockstream * socket ) throw() { try { - socket->send( line.c_str(), line.length() ); - socket->send( "\r\n", 2 ); - } catch ( socketlibrary::SocketException &sex ) { + socket->write( line.c_str(), line.length() ); + socket->write( "\r\n", 2 ); + } catch ( psocksxx::sockexception &e ) { // TODO: log errors return false; } @@ -328,11 +324,11 @@ namespace icap { } - bool send_data( const std::string &data, socketlibrary::TCPSocket * socket ) throw() { + bool send_data( const std::string &data, psocksxx::iosockstream * socket ) throw() { try { - socket->send( data.c_str(), data.size() ); - } catch( socketlibrary::SocketException &sex ) { + socket->write( data.c_str(), data.size() ); + } catch ( psocksxx::sockexception &e ) { // TODO: log errors return false; } @@ -342,7 +338,7 @@ namespace icap { } - bool send_chunked( const std::string &data, socketlibrary::TCPSocket * socket ) throw() { + bool send_chunked( const std::string &data, psocksxx::iosockstream * socket ) throw() { std::string chunked_data = ""; unsigned int offset = 0; @@ -388,7 +384,7 @@ namespace icap { return false; } - } catch ( socketlibrary::SocketException &sex ) { + } catch ( psocksxx::sockexception &e ) { // TODO: log errors ?? return false; } @@ -432,7 +428,7 @@ namespace icap { } - icap::RequestHeader * read_req_header( socketlibrary::TCPSocket * socket ) throw() { + icap::RequestHeader * read_req_header( psocksxx::iosockstream * socket ) throw() { char buffer[ICAP_BUFFER_SIZE]; int n = 0; @@ -448,7 +444,7 @@ namespace icap { } - bool read_req_data( icap::Request * request, socketlibrary::TCPSocket * socket ) throw() { + bool read_req_data( icap::Request * request, psocksxx::iosockstream * socket ) throw() { int data_offset = 0; int data_length = 0; @@ -529,7 +525,7 @@ namespace icap { } - bool read_req_continue_data( icap::Request * request, socketlibrary::TCPSocket * socket ) throw() { + bool read_req_continue_data( icap::Request * request, psocksxx::iosockstream * socket ) throw() { std::vector<icap::Header::encapsulated_header_data_t> sorted_encaps_header; icap::Header::encapsulated_header_data_t header_idx; @@ -574,7 +570,7 @@ namespace icap { } - bool send_headers( icap::Header * header, socketlibrary::TCPSocket * socket ) throw() { + bool send_headers( icap::Header * header, psocksxx::iosockstream * socket ) throw() { std::string line; icap::Header::headers_index_t i; @@ -612,7 +608,7 @@ namespace icap { } - bool send_response( icap::Response * response, socketlibrary::TCPSocket * socket ) throw() { + bool send_response( icap::Response * response, psocksxx::iosockstream * socket ) throw() { bool r_success = true; @@ -660,6 +656,9 @@ namespace icap { } + // flush-out socket stream buffer + socket->flush(); + return r_success; } |