summaryrefslogtreecommitdiff
path: root/src/bitz/worker.cpp
blob: efbeef3082afc200ed4b7224ad79e45f74633794 (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
/*
 *	bitz-server, An ICAP server implementation in C++
 *	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 "worker.h"
#include "logger.h"
#include "util.h"
#include "options_request_handler.h"
#include "reqmod_request_handler.h"
#include "respmod_request_handler.h"

#include <icap/util.h>
#include <icap/request_header.h>


namespace bitz {

	Worker::Worker() {

		// load request handlers
		load_req_handlers();

	}


	Worker::~Worker() {

		// logger
		Logger &logger = Logger::instance();
		logger.debug( "[worker] exiting" );

		// cleanup request handlers
		util::delete_req_handlers( _req_handlers );
		delete _req_handlers["OPTIONS"];

	}


	void Worker::run( socketlibrary::TCPServerSocket * server_sock, unsigned int max_requests ) throw() {

		Logger &logger = Logger::instance();

		socketlibrary::TCPSocket * client_sock;
		icap::RequestHeader * req_header;
		icap::Response * response;
		RequestHandler * req_handler;


		try {

			while ( max_requests > 0 ) {

				logger.debug( std::string( "[worker] waiting for a connection" ) );

				client_sock = server_sock->accept();
				logger.debug( std::string( "[worker] new connection accepted on " ).append( client_sock->getForeignAddress() )
						.append( ":" ).append( util::itoa( client_sock->getForeignPort() ) ) );

				// request header
				req_header  = icap::util::read_req_header( client_sock );
				logger.debug( std::string( "[worker] request header:\r\n" ).append( req_header->raw_data() ) );

				// try to find a handler for the request
				req_handler = util::find_req_handler( _req_handlers, req_header->method() );

				if ( req_handler != NULL ) {

					logger.debug( std::string( "[worker] handling request: " ).append( req_header->method() ) );

					// process the request and grab the response
					response = req_handler->process( req_header, client_sock );

				} else {

					// unsupported request
					logger.info( std::string( "[worker] unsupported request: " ).append( req_header->method() ) );
					response = new icap::Response( new icap::ResponseHeader( icap::ResponseHeader::NOT_ALLOWED ) );

				}

				// send the response back to the client
				icap::util::send_response( response, client_sock );

				// cleanup
				delete response;
				delete req_header;

				// destroy / close connection
				delete client_sock;

				max_requests--;

			}

		} catch( socketlibrary::SocketException &sex ) {
			logger.error( std::string( "[worker] ERROR: " ).append( sex.what() ) );
		}

	}


	void Worker::load_req_handlers() throw() {

		OptionsRequestHandler * options_handler;

		// OPTIONS handler
		options_handler = new OptionsRequestHandler();
		_req_handlers["OPTIONS"] = options_handler;

		/* request handlers */

		// REQMOD
		_req_handlers["REQMOD"]  = new ReqmodRequestHandler();
		options_handler->register_handler( _req_handlers["REQMOD"] );

		// RESPMOD
		_req_handlers["RESPMOD"] = new RespmodRequestHandler();
		options_handler->register_handler( _req_handlers["RESPMOD"] );

	}

} /* end of namespace bitz */