From a55a225899dbd47dd4238adf3a49f62846cb87f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Mon, 14 Dec 2015 06:48:14 +0100 Subject: Imported Upstream version 1.0.0 --- lib/icap/Makefile.am | 4 +- lib/icap/util.cpp | 127 +++++++++++++++++++++++++-------------------------- lib/icap/util.h | 34 +++++++------- 3 files changed, 82 insertions(+), 83 deletions(-) (limited to 'lib/icap') diff --git a/lib/icap/Makefile.am b/lib/icap/Makefile.am index 02d8bb4..f73a8f6 100644 --- a/lib/icap/Makefile.am +++ b/lib/icap/Makefile.am @@ -1,9 +1,9 @@ ## [icap-library] lib/icap/ -AM_CPPFLAGS = -I$(top_srcdir)/lib +AM_CPPFLAGS = -I$(top_srcdir)/lib ${psocksxx_CFLAGS} libicapincludedir = $(includedir)/icap lib_LTLIBRARIES = libicap.la -libicap_la_LIBADD = $(top_builddir)/lib/socket/libsocket.la +libicap_la_LIBADD = ${psocksxx_LIBS} libicap_la_LDFLAGS = -version-info @ICAP_LT_VERSION@ -no-undefined libicap_la_SOURCES = \ header.cpp \ 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 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 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 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; } diff --git a/lib/icap/util.h b/lib/icap/util.h index 366bb9c..14765ac 100644 --- a/lib/icap/util.h +++ b/lib/icap/util.h @@ -22,7 +22,7 @@ #include -#include +#include #include "request.h" #include "response.h" @@ -80,7 +80,7 @@ namespace icap { * default is false. * @return number of bytes read */ - int read_line( socketlibrary::TCPSocket * socket, char * buf, int buf_length, bool incl_endl = false ) throw(); + int read_line( psocksxx::iosockstream * socket, char * buf, int buf_length, bool incl_endl = false ) throw(); /** * Read a line (ending with \r\n) from the socket @@ -90,7 +90,7 @@ namespace icap { * default is false. * @return read data */ - std::string read_line( socketlibrary::TCPSocket * socket, bool incl_endl = false ) throw(); + std::string read_line( psocksxx::iosockstream * socket, bool incl_endl = false ) throw(); /** * Read data from the socket @@ -99,7 +99,7 @@ namespace icap { * @param size size / length of data to be read * @return read data */ - std::string read_data( socketlibrary::TCPSocket * socket, int size ) throw(); + std::string read_data( psocksxx::iosockstream * socket, int size ) throw(); /** * Read chunk size. This is a helper method used by read_chunked(). @@ -107,7 +107,7 @@ namespace icap { * @param socket socket instance to read from * @return chunk size */ - unsigned int read_chunk_size( socketlibrary::TCPSocket * socket ) throw(); + unsigned int read_chunk_size( psocksxx::iosockstream * socket ) throw(); /** * Read chunk header from the given socket. @@ -115,7 +115,7 @@ namespace icap { * @param socket socket instance to read data from * @param chunk chunk data structure to store header data */ - void read_chunk_header( socketlibrary::TCPSocket * socket, chunk_t &chunk ) throw(); + void read_chunk_header( psocksxx::iosockstream * socket, chunk_t &chunk ) throw(); /** * Read a data chunk from a HTTP chunked transfer encoded data stream. @@ -123,7 +123,7 @@ namespace icap { * @param socket socket instance to read data from * @return chunk data structure */ - chunk_t read_chunk( socketlibrary::TCPSocket * socket ) throw(); + chunk_t read_chunk( psocksxx::iosockstream * socket ) throw(); /** * Read chunked data from the given socket @@ -131,7 +131,7 @@ namespace icap { * @param socket socket instance to read data from * @return read data (without the control characters) */ - std::string read_chunked( socketlibrary::TCPSocket * socket ) throw(); + std::string read_chunked( psocksxx::iosockstream * socket ) throw(); /** * Read chunked payload data from the given socket @@ -140,7 +140,7 @@ namespace icap { * @param payload payload to read data into * @return boolean flag to denote the presence of "ieof" */ - bool read_chunked_payload( socketlibrary::TCPSocket * socket, std::string &payload ) throw(); + bool read_chunked_payload( psocksxx::iosockstream * socket, std::string &payload ) throw(); /** * Send / write a line (ending with \r\n) to the socket @@ -149,7 +149,7 @@ namespace icap { * @param socket socket object to write the data to * @return boolean to denote success or failure */ - bool send_line( const std::string &line, socketlibrary::TCPSocket * socket ) throw(); + bool send_line( const std::string &line, psocksxx::iosockstream * socket ) throw(); /** * Send / write data to the socket. @@ -160,7 +160,7 @@ namespace icap { * @param socket socket instance to write to * @return boolean to denote success or failure */ - bool send_data( const std::string &data, socketlibrary::TCPSocket * socket ) throw(); + bool send_data( const std::string &data, psocksxx::iosockstream * socket ) throw(); /** * Send / write data to the socket using chunked transfer encoding @@ -169,7 +169,7 @@ namespace icap { * @param socket socket instance to write to * @return boolean to denote success or failure */ - bool send_chunked( const std::string &data, socketlibrary::TCPSocket * socket ) throw(); + bool send_chunked( const std::string &data, psocksxx::iosockstream * socket ) throw(); /** * split a string into a vector by the given delimiter @@ -209,7 +209,7 @@ namespace icap { * @param socket socket object to read data from * @return icap request header object */ - icap::RequestHeader * read_req_header( socketlibrary::TCPSocket * socket ) throw(); + icap::RequestHeader * read_req_header( psocksxx::iosockstream * socket ) throw(); /** * Read icap request into the icap::Request instance @@ -219,7 +219,7 @@ namespace icap { * @param socket socket object to read data from * @return boolean to denote success or failure */ - bool read_req_data( icap::Request * request, socketlibrary::TCPSocket * socket ) throw(); + bool read_req_data( icap::Request * request, psocksxx::iosockstream * socket ) throw(); /** * Read icap request data after a '100 Continue' response. This will not look for any @@ -229,7 +229,7 @@ namespace icap { * @param socket socket object to read data from * @return boolean to denote success or failure */ - bool read_req_continue_data( icap::Request * request, socketlibrary::TCPSocket * socket ) throw(); + bool read_req_continue_data( icap::Request * request, psocksxx::iosockstream * socket ) throw(); /** * Send / write header data to a socket @@ -238,7 +238,7 @@ namespace icap { * @param socket socket object to write the data to * @return boolean to denote success or failure */ - bool send_headers( icap::Header::headers_t headers, socketlibrary::TCPSocket * socket ) throw(); + bool send_headers( icap::Header::headers_t headers, psocksxx::iosockstream * socket ) throw(); /** * Output a response using the passed in icap::Response class to the @@ -248,7 +248,7 @@ namespace icap { * @param socket socket object to send the data to * @return boolean to denote success or failure */ - bool send_response( icap::Response * response, socketlibrary::TCPSocket * socket ) throw(); + bool send_response( icap::Response * response, psocksxx::iosockstream * socket ) throw(); /** * Returns the response status text for the given status -- cgit v1.2.3