summaryrefslogtreecommitdiff
path: root/lib/icap/util.cpp
blob: 91a73e18b0fb983731f06a3f0a291dba85db7a93 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*
 *	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
 */

#include "util.h"

#include <string>
#include <algorithm>
#include <locale>
#include <iostream>


namespace icap {

	namespace util {

		unsigned int hextodec( const std::string &hex ) throw() {

			unsigned int dec;
			std::stringstream ss;

			ss << std::hex << hex;
			ss >> dec;

			return dec;

		}


		const std::string dectohex( const unsigned int &dec ) throw() {

			std::string hex;
			std::stringstream ss;

			ss << std::hex << dec;
			ss >> hex;

			return hex;

		}


		int read_line( psocksxx::iosockstream * socket, char * buf, int buf_length, bool incl_endl ) throw() {

			int i = 0;
			char c      = '\0';
			char c_last = '\0';

			while ( i < ( buf_length - 1 ) ) {

				// read a char
				if ( ( c = socket->get() ) > 0 ) {

					// check last read char for \r
					if ( c_last == '\r' ) {

						// 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;

						}

					}

					buf[i] = c;
					i++;

					c_last = c;

				} else {
					break;    // nothing read from socket
				}

			}

			buf[i] = '\0';
			return i;

		}


		std::string read_line( psocksxx::iosockstream * socket, bool incl_endl ) throw() {

			int n;
			std::string line;
			char c = '\0';
			char c_last = '\0';


			while ( ( c = socket->get() ) > 0 ) {

				if ( c_last == '\r' ) {

					if ( c == '\n' ) {

						if ( incl_endl ) {
							line += c;
						} else {
							line.erase( line.size() - 1 );
						}

						break;

					}

				}

				line  += c;
				c_last = c;

			}


			return line;

		}


		std::string read_data( psocksxx::iosockstream * socket, int size ) throw() {

			char buffer[ICAP_BUFFER_SIZE];
			std::string data = "";
			int n;

			// loop until we have read all the bytes
			while ( size > 0 ) {

				try {

					// read from socket
					socket->read( buffer, std::min( size, ICAP_BUFFER_SIZE ) );

					// sanity check
					if (! socket->good() ) {
						break;
					}

					// append to data
					data.append( buffer, socket->gcount() );

					// update size with remaining bytes
					size -= socket->gcount();

				} catch ( psocksxx::sockexception &e ) {
					// TODO: log errors ??
				}

			}

			return data;

		}


		unsigned int read_chunk_size( psocksxx::iosockstream * socket ) throw() {

			std::string line;
			std::vector<std::string> chunk_header;

			line = read_line( socket );
			chunk_header = split( line, ";" );

			return hextodec( chunk_header.at( 0 ) );

		}


		void read_chunk_header( psocksxx::iosockstream * socket, chunk_t &chunk ) throw() {

			std::string line;
			std::vector<std::string> chunk_header;

			line = read_line( socket );
			chunk_header = split( line, ";" );

			// sanity check
			if ( chunk_header.size() > 0 ) {

				// sanity check
				if ( chunk_header.at( 0 ).size() > 0 ) {
					chunk.size = hextodec( chunk_header.at( 0 ) );
				}

				// check for chunk-extension
				if ( chunk_header.size() == 2 ) {
					chunk.extention = trim( chunk_header.at( 1 ) );
				}
			}

			return;

		}


		chunk_t read_chunk( psocksxx::iosockstream * socket ) throw() {

			chunk_t chunk;
			std::string line;
			std::vector<std::string> chunk_header;

			// initialise chunk
			chunk.size      = 0;
			chunk.extention = "";
			chunk.data      = "";

			// read chunk header
			read_chunk_header( socket, chunk );

			// read chunk data
			if ( chunk.size > 0 ) {
				chunk.data = read_data( socket, chunk.size );
			}

			// read \r\n ending for the chunk
			read_data( socket, 2 );

			return chunk;

		}


		std::string read_chunked( psocksxx::iosockstream * socket ) throw() {

			unsigned int chunk_size  = 0;
			unsigned int offset      = 0;
			std::string chunked_data = "";

			while ( ( chunk_size = read_chunk_size( socket ) ) > 0 ) {

				offset = chunked_data.size();

				// read chunk-data
				chunked_data.append( read_data( socket, chunk_size ) );

				// sanity check
				if ( ( chunked_data.size() - offset ) != chunk_size ) {
					// something went wrong
					break;
				}

				// extra \r\n
				read_data( socket, 2 );

			}

			// read until the end of chunked data
			while ( read_line( socket, true ).size() > 2 ) ;

			return chunked_data;

		}


		bool read_chunked_payload( psocksxx::iosockstream * socket, std::string &payload ) throw() {

			chunk_t chunk;
			bool ieof = false;

			do {

				// read chunk
				chunk = read_chunk( socket );

				// append to payload
				payload.append( chunk.data );

				// sanity check
				if ( chunk.data.size() != chunk.size ) {
					// something went wrong
					break;
				}

			} while( chunk.size > 0 );

			// check for ieof
			if ( chunk.extention == "ieof" ) {
				ieof = true;
			}

			return ieof;

		}


		bool send_line( const std::string &line, psocksxx::iosockstream * socket ) throw() {

			try {
				socket->write( line.c_str(), line.length() );
				socket->write( "\r\n", 2 );
			} catch ( psocksxx::sockexception &e ) {
				// TODO: log errors
				return false;
			}

			return true;

		}


		bool send_data( const std::string &data, psocksxx::iosockstream * socket ) throw() {

			try {
				socket->write( data.c_str(), data.size() );
			} catch ( psocksxx::sockexception &e ) {
				// TODO: log errors
				return false;
			}

			return true;

		}


		bool send_chunked( const std::string &data, psocksxx::iosockstream * socket ) throw() {

			std::string chunked_data = "";
			unsigned int offset      = 0;
			int chunks               = 0;

			// calculate the number of chunks we need
			if ( data.size() > ICAP_BUFFER_SIZE ) {
				chunks = ( data.size() / ICAP_BUFFER_SIZE );
			}

			try {

				do {

					// prepare data for this chunk
					chunked_data = data.substr( offset, ICAP_BUFFER_SIZE );

					// sanity check
					if ( chunked_data.size() <= 0 ) {
						// we shouldn't get here
						break;
					}

					// update offset
					offset += chunked_data.size();

					// send chunk size
					if (! send_line( dectohex( chunked_data.size() ), socket ) ) {
						return false;
					}

					// send chunk
					if (! send_data( chunked_data, socket ) ) {
						return false;
					}

					chunks--;

				} while ( chunks > 0 );

				// end of chunk
				if (! send_data( "\r\n0\r\n\r\n", socket ) ) {
					return false;
				}

			} catch ( psocksxx::sockexception &e ) {
				// TODO: log errors ??
				return false;
			}

			return true;

		}


		std::vector<std::string> split( const std::string &str, const std::string &delimiter ) throw() {

			std::vector<std::string> result;
			size_t current;
			size_t next = -1;

			do {
				current = next + 1;
				next    = str.find_first_of( delimiter, current );
				result.push_back( str.substr( current, ( next - current ) ) );
			} while ( next != std::string::npos );

			return result;

		}


		std::string &ltrim( std::string &str ) throw() {
			str.erase( str.begin(), std::find_if( str.begin(), str.end(), std::not1( std::ptr_fun<int, int>( std::isspace ) ) ) );
			return str;
		}


		std::string &rtrim( std::string &str ) throw() {
			str.erase( std::find_if( str.rbegin(), str.rend(), std::not1( std::ptr_fun<int, int>( std::isspace ) ) ).base(), str.end() );
			return str;
		}


		std::string &trim( std::string &str ) throw() {
			return ltrim( rtrim( str ) );
		}


		icap::RequestHeader * read_req_header( psocksxx::iosockstream * socket ) throw() {

			char buffer[ICAP_BUFFER_SIZE];
			int  n = 0;
			std::string data = "";

			while ( ( n = read_line( socket, buffer, ICAP_BUFFER_SIZE, true ) ) > 2 ) {
				data.append( buffer );
			}

			icap::RequestHeader * req_header = new icap::RequestHeader( data );
			return req_header;

		}


		bool read_req_data( icap::Request * request, psocksxx::iosockstream * socket ) throw() {

			int data_offset = 0;
			int data_length = 0;
			int data_read   = 0;
			std::vector<icap::Header::encapsulated_header_data_t> sorted_encaps_header;
			std::vector<icap::Header::encapsulated_header_data_t>::iterator sorted_idx;

			// payload
			icap::payload_t payload;
			payload.req_header = "";
			payload.req_body   = "";
			payload.res_header = "";
			payload.res_body   = "";
			payload.ieof       = false;

			// header
			icap::Header * header = request->header();
			sorted_encaps_header  = header->sort_encapsulated_header();

			// loop through the sorted header
			for ( sorted_idx = sorted_encaps_header.begin(); sorted_idx != sorted_encaps_header.end(); sorted_idx++ ) {

				// don't want to read negative headers
				if ( sorted_idx->second < 0 ) {
					continue;
				}

				// if this is the last header entity then check for chunked content
				if ( sorted_idx == ( sorted_encaps_header.end() - 1 ) ) {

					if ( sorted_idx->first == "req-body" ) {
						payload.ieof = read_chunked_payload( socket, payload.req_body );
					} else if ( sorted_idx->first == "res-body" ) {
						payload.ieof = read_chunked_payload( socket, payload.res_body );
					} else {
						/*
						*  null-body is the only other legal possibility here
						*  we take that into account in the previous iterations
						*/
						break;
					}

				} else {

					data_offset = sorted_idx->second;
					data_length = ( ( sorted_idx + 1 )->second - data_offset );


					/* read request data */

					// is there anything to read?
					if ( data_length > 0  ) {

						// update payload
						if ( sorted_idx->first == "req-hdr" ) {
							payload.req_header = read_data( socket, data_length );
						} else if (  sorted_idx->first == "req-body" ) {
							payload.req_body   = read_data( socket, data_length );
						} else if (  sorted_idx->first == "res-hdr" ) {
							payload.res_header = read_data( socket, data_length );
						} else if (  sorted_idx->first == "res-body" ) {
							payload.res_body   = read_data( socket, data_length );
						} else {
							// TODO: error?
						}

					}

				}

			}

			// update request
			request->payload( payload );

			return true;

		}


		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;

			// copy the payload from request so we can append to it
			icap::payload_t payload;
			payload.req_header = request->payload().req_header;
			payload.req_body   = request->payload().req_body;
			payload.res_header = request->payload().res_header;
			payload.res_body   = request->payload().res_body;
			payload.ieof       = request->payload().ieof;

			// header
			icap::Header * header = request->header();
			sorted_encaps_header  = header->sort_encapsulated_header();

			// sanity check
			if ( sorted_encaps_header.size() > 0 ) {

				// we are only interested in the last header entity
				header_idx = sorted_encaps_header.back();

				// read payload data
				if ( header_idx.first == "req-body" ) {
					payload.ieof = read_chunked_payload( socket, payload.req_body );
				} else if ( header_idx.first == "res-body" ) {
					payload.ieof = read_chunked_payload( socket, payload.res_body );
				}

			} else {

				// something isn't quite right
				return false;

			}

			// update request
			request->payload( payload );

			return true;

		}


		bool send_headers( icap::Header * header, psocksxx::iosockstream * socket ) throw() {

			std::string line;
			icap::Header::headers_index_t i;
			icap::ResponseHeader::headers_t headers;

			// headers
			headers = header->headers();

			for ( i = headers.begin(); i != headers.end(); i++ ) {

				line = i->first;
				line.append( ": " );
				line.append( i->second );

				if (! send_line( line, socket ) ) {
					return false;
				}

			}

			// send encapsulated header
			line = "Encapsulated: ";
			line.append( header->encapsulated_header_str() );
			if (! send_line( line, socket ) ) {
				return false;
			}

			// end of header
			if (! send_data( "\r\n", socket ) ) {
				return false;
			}

			return true;

		}


		bool send_response( icap::Response * response, psocksxx::iosockstream * socket ) throw() {

			bool r_success = true;

			icap::ResponseHeader * header;

			// grab the response header
			header = response->header();

			// response status
			std::string line = header->protocol();
			line.append( " " );
			line.append( itoa( header->status() ) );
			line.append( " " );
			line.append( response_status( header->status() ) );

			r_success = send_line( line, socket );

			// response headers
			if ( r_success ) {
				r_success = send_headers( header, socket );
			}

			// response content (if there are any)
			if ( r_success ) {

				// req-hdr
				if ( response->payload().req_header.size() > 0 ) {
					send_data( response->payload().req_header, socket );
				}

				// red-body
				if ( response->payload().req_body.size() > 0 ) {
					send_chunked( response->payload().req_body, socket );
				}

				// res-hdr
				if ( response->payload().res_header.size() > 0 ) {
					send_data( response->payload().res_header, socket );
				}

				// res-body
				if ( response->payload().res_body.size() > 0 ) {
					send_chunked( response->payload().res_body, socket );
				}

			}

			// flush-out socket stream buffer
			socket->flush();

			return r_success;

		}


		const std::string response_status( const ResponseHeader::status_t &status ) throw() {

			// FIXME: probably there's a better way of mapping this
			std::map<ResponseHeader::status_t, std::string> status_text;

			status_text[ResponseHeader::CONTINUE]           = "Continue";
			status_text[ResponseHeader::OK]                 = "OK";
			status_text[ResponseHeader::NO_CONTENT]         = "No modifications needed";
			status_text[ResponseHeader::BAD_REQUEST]        = "Bad request";
			status_text[ResponseHeader::NOT_FOUND]          = "ICAP Service not found";
			status_text[ResponseHeader::NOT_ALLOWED]        = "Method not allowed for service";
			status_text[ResponseHeader::REQ_TIMEOUT]        = "Request timeout";
			status_text[ResponseHeader::SERVER_ERROR]       = "Server error";
			status_text[ResponseHeader::NOT_IMPLEMENTED]    = "Method not implemented";
			status_text[ResponseHeader::BAD_GATEWAY]        = "Bad gateway";
			status_text[ResponseHeader::SERVICE_OVERLOADED] = "Service overloaded";
			status_text[ResponseHeader::NOT_SUPPORTED]      = "ICAP version not supported by server";

			return status_text[status];

		}

	} /* end of namespace util */

} /* end of namespace icap */