summaryrefslogtreecommitdiff
path: root/lib/icap/util.h
blob: 366bb9c85d52b3d7eb35afa4ebae875b88986009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 *	C++ ICAP library
 *	Copyright (C) 2012	Uditha Atukorala
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef ICAP_UTIL_H
#define ICAP_UTIL_H

#include <sstream>

#include <socket/socket.h>
#include "request.h"
#include "response.h"

#ifndef ICAP_BUFFER_SIZE
#define ICAP_BUFFER_SIZE 1024
#endif


namespace icap {

	namespace util {

		struct chunk_t {
			unsigned int size;
			std::string  extention;
			std::string  data;
		};

		/**
		*   Convert a number into a string
		*
		*   @param number number to be converted
		*   @return converted string
		*/
		template <typename T> std::string itoa( T number ) {
			std::ostringstream ss;
			ss << number;

			return ss.str();
		}

		/**
		*   Convert a hexadecimal number to a decimal number.
		*
		*   @param hex hex to convert to
		*   @return converted decimal value
		*/
		unsigned int hextodec( const std::string &hex ) throw();

		/**
		*   Convert a decimal number to its hexadecimal value
		*
		*   @param dec decimal number
		*   @return converted hex value
		*/
		const std::string dectohex( const unsigned int &dec ) throw();

		/**
		*   Read a line (ending with \r\n) from the socket
		*
		*   @param socket socket to read from
		*   @param buf buffer to read the data into
		*   @param buf_length length / size of the buffer data is read into
		*   @param incl_endl (optional) switch to control whether to include \r\n in the read line,
		*          default is false.
		*   @return number of bytes read
		*/
		int read_line( socketlibrary::TCPSocket * socket, char * buf, int buf_length, bool incl_endl = false ) throw();

		/**
		*   Read a line (ending with \r\n) from the socket
		*
		*   @param socket socket instance to read data from
		*   @param incl_endl (optional) switch to control whether to include \r\n in the read line,
		*          default is false.
		*   @return read data
		*/
		std::string read_line( socketlibrary::TCPSocket * socket, bool incl_endl = false ) throw();

		/**
		*   Read data from the socket
		*
		*   @param socket socket instance to read data from
		*   @param size size / length of data to be read
		*   @return read data
		*/
		std::string read_data( socketlibrary::TCPSocket * socket, int size ) throw();

		/**
		*   Read chunk size. This is a helper method used by read_chunked().
		*
		*   @param socket socket instance to read from
		*   @return chunk size
		*/
		unsigned int read_chunk_size( socketlibrary::TCPSocket * socket ) throw();

		/**
		*   Read chunk header from the given socket.
		*
		*   @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();

		/**
		*   Read a data chunk from a HTTP chunked transfer encoded data stream.
		*
		*   @param socket socket instance to read data from
		*   @return chunk data structure
		*/
		chunk_t read_chunk( socketlibrary::TCPSocket * socket ) throw();

		/**
		*   Read chunked data from the given socket
		*
		*   @param socket socket instance to read data from
		*   @return read data (without the control characters)
		*/
		std::string read_chunked( socketlibrary::TCPSocket * socket ) throw();

		/**
		*   Read chunked payload data from the given socket
		*
		*   @param socket socket instance to read data from
		*   @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();

		/**
		*   Send / write a line (ending with \r\n) to the socket
		*
		*   @param line line to send through the socket without the line-ending chars
		*   @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();

		/**
		*   Send / write data to the socket.
		*   If chunked is set to true then data will be transferred using
		*   "chunked" transfer-encoding.
		*
		*   @param data data to be sent
		*   @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();

		/**
		*   Send / write data to the socket using chunked transfer encoding
		*
		*   @param data data to be sent
		*   @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();

		/**
		*   split a string into a vector by the given delimiter
		*
		*   @param str input string
		*   @param delimiter (optional) delimiter, defaults to " "
		*/
		std::vector<std::string> split( const std::string &str, const std::string &delimiter = " " ) throw();

		/**
		*   Left trim (trim from start) a passed in string
		*
		*   @param str string to trim
		*   @return trimmed string
		*/
		std::string &ltrim( std::string &str ) throw();

		/**
		*   Right trim (trim from end) a passed in string
		*
		*   @param str string to trim
		*   @return trimmed string
		*/
		std::string &rtrim( std::string &str ) throw();

		/**
		*   Trim (trim from both ends) a passed in string
		*
		*   @param str string to trim
		*   @return trimmed string
		*/
		std::string &trim( std::string &str ) throw();

		/**
		*   Read icap request header from the socket passes in
		*
		*   @param socket socket object to read data from
		*   @return icap request header object
		*/
		icap::RequestHeader * read_req_header( socketlibrary::TCPSocket * socket ) throw();

		/**
		*   Read icap request into the icap::Request instance
		*   using the socket passed in
		*
		*   @param request request object to read data into
		*   @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();

		/**
		*   Read icap request data after a '100 Continue' response. This will not look for any
		*   additional headers and will treat any data coming through the socket as payload data.
		*
		*   @param request request object to read data into
		*   @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();

		/**
		*   Send / write header data to a socket
		*
		*   @param headers headers to be sent
		*   @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();

		/**
		*   Output a response using the passed in icap::Response class to the
		*   passed in socket
		*
		*   @param response response object to get the response data from
		*   @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();

		/**
		*   Returns the response status text for the given status
		*
		*   @param status status to get the text for
		*/
		const std::string response_status( const ResponseHeader::status_t &status ) throw();

	} /* end of namespace util */

} /* end of namespace icap */

#endif /* !ICAP_UTIL_H */