-
Notifications
You must be signed in to change notification settings - Fork 5
/
icap-server.py
329 lines (261 loc) · 9.08 KB
/
icap-server.py
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
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
from tempfile import NamedTemporaryFile
from syslog import openlog, syslog
import os
import pdb
import re
import zlib
import gzip
import sys
import ConfigParser
config = ConfigParser.RawConfigParser()
config.read(sys.argv[1])
bind_to = config.get('icap-server', 'bind')
port = config.get('icap-server', 'port')
css_url = config.get('icap-server', 'css_url')
js_url = config.get('icap-server', 'js_url')
spool_dir = config.get('icap-server', 'spool_dir')
OPTIONS_RESP = """ICAP/1.0 200 OK\r
Service: icap-server.py\r
ISTag: icap-server-001\r
Options-TTL: 3600\r
Encapsulated: opt-body=0\r
Methods: RESPMOD\r
Transfer-Complete: *\r
\r
"""
RESPMOD_RESP = """ICAP/1.0 200 OK\r
Server: icap-server.py\r
Connection: close\r
ISTag: icap-server-001\r
Encapsulated: res-hdr=0, %s=%d\r
\r
"""
AD_HEAD = re.compile ('(?P<pre>.*<\s*HEAD[^<]*>)(?P<post>.*)',
re.IGNORECASE | re.MULTILINE | re.DOTALL)
JS_HEAD = """
<link rel="stylesheet" type="text/css" href="%s">
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" src="%s"></script>
""" % (css_url, js_url)
class HeaderError:
FIRST_LINE = 'First line invalid'
HEADER = 'Header error'
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
class ICAP(LineReceiver):
CONTENT_LENGTH = 'content-length'
CONTENT_ENCODING = 'content-encoding'
ENCAPSULATED = 'encapsulated'
REQ_HDR = 'req-hdr'
REP_HDR = 'rep-hdr'
RES_BODY = 'res-body'
NULL_BODY = 'null-body'
GZIP = 'gzip'
REFERER = 'referer'
def connectionMade(self):
self.headers = {}
self.first_line = False
self.length = 0
self.body = False
self.headers_body = 0
self.chunk = 0
self.content_encoding = False
self.descompressor = False
self.referer = False
self.temp_files = []
def lineReceived(self, line):
try:
if self.body:
self.read_body(line)
elif line == '':
self.headersReceived()
elif not self.first_line:
self.first_line = self.parse_first_line(line)
else:
self.parse_header_add(line)
except HeaderError, e:
syslog('ERROR: ' + str(e) + ', ' + str(self.first_line))
self.error(e)
#self.sendLine(line)
def headersReceived(self):
if self.first_line[0] == 'RESPMOD':
self.do_RESPMOD()
elif self.first_line[0] == 'OPTIONS':
self.do_OPTIONS()
else:
raise HeaderError("Invalid method.")
def parse_first_line(self, line):
parts = line.split()
if len(parts) != 3:
raise HeaderError(HeaderError.FIRST_LINE)
return parts
def parse_header(self, line):
i = line.find(':')
if i < 0:
self.transport.loseConnection()
header = line[:i]
value = line[i+1:].lstrip()
return [ header, value ]
def parse_header_add(self, line):
header = self.parse_header(line)
self.headers[header[0].lower()] = header[1]
def setLength(self):
self.length = int(self.headers[self.CONTENT_LENGTH])
def rawDataReceived(self, data):
if self.chunk == -1:
raise HeaderError('Excess data.')
#if self.descompressor:
# data = self.descompressor.decompress(data)
data_len = len(data)
while data_len > 0:
if self.chunk == 0:
crlf_pos = data.find(self.delimiter)
self.chunk = int(data[:crlf_pos], 16)
if self.chunk == 0:
self.chunk = -1
self.finish_RESPMOD()
return
data = data[crlf_pos + 2:]
data_len = len(data)
if data_len > self.chunk:
self.tmp_fd.write(data[:self.chunk])
data = data[self.chunk:]
data_len -= self.chunk
self.chunk = 0
else:
self.tmp_fd.write(data)
self.chunk -= data_len
data_len = 0
if self.chunk < 0:
raise HeaderError('Chunk data is negative.')
if self.chunk == 0 and data_len > 0:
data = data[2:]
data_len -= 2
def do_OPTIONS(self):
self.transport.write(OPTIONS_RESP)
def do_RESPMOD(self):
if not self.ENCAPSULATED in self.headers.keys():
raise HeaderError(self.ENCAPSULATED + ' header missing')
self.parse_encap()
self.tmp_fd = NamedTemporaryFile(delete = False, dir = spool_dir)
self.temp_files.append(self.tmp_fd.name)
self.body = True
def read_body(self, line):
self.tmp_fd.write(line + self.delimiter)
if line.lower().startswith(self.CONTENT_ENCODING):
self.parse_content_encoding(line)
if line.lower().startswith(self.REFERER):
self.referer = self.parse_header(line)[1]
if line == '':
self.headers_body += 1
if self.headers_body == 2:
if self.has_body():
self.setRawMode()
else:
self.finish_RESPMOD()
def finish_RESPMOD(self):
self.tmp_fd.seek(self.encap['res-hdr'], os.SEEK_SET)
encap_hdr = ''
while True:
line = self.tmp_fd.readline()
if not line:
return
if line.lower().startswith(self.CONTENT_LENGTH):
continue
encap_hdr += line
if line == self.delimiter:
break
if not self.has_body():
encap_attr = self.NULL_BODY
encap_body = None
else:
encap_attr = self.RES_BODY
encap_body = self.get_encap_body()
encap_body_size = len(encap_body)
encap_body = str(encap_body_size) + self.delimiter + encap_body
#0 Removed by now. There is a bug somewhere.
encap_body += self.delimiter + ' ' + self.delimiter
encap_hdr_size = len(encap_hdr)
respmod_resp_hdr = RESPMOD_RESP % (encap_attr, encap_hdr_size)
self.transport.write(respmod_resp_hdr)
self.transport.write(encap_hdr)
if encap_body:
self.transport.write(encap_body)
self.transport.loseConnection()
def get_encap_body(self):
encap_body = self.tmp_fd.read()
#uncompress if the body is gzipped
if self.descompressor == None:
return encap_body
elif self.descompressor:
zipped = NamedTemporaryFile(delete = False, dir = spool_dir)
self.temp_files.append(zipped.name)
zipped.write(encap_body)
zipped.flush()
g = gzip.GzipFile(zipped.name)
encap_body = g.read()
try:
ad_head = AD_HEAD.match(encap_body)
if ad_head:
pre = ad_head.group('pre')
post = ad_head.group('post')
encap_body = pre + JS_HEAD + post
except:
syslog('ERROR: applying regex' + str(self.first_line))
#compress it again
if self.descompressor:
f_out = NamedTemporaryFile(delete = False, dir = spool_dir)
self.temp_files.append(f_out.name)
f_out.close()
f_out = gzip.open(f_out.name, 'wb')
name = f_out.name
f_out.write(encap_body)
f_out.close()
encap_body = open(name).read()
return encap_body
def has_body(self):
return (not self.NULL_BODY in self.encap.keys())
def parse_encap(self):
encap = self.headers[self.ENCAPSULATED].replace(' ', '').split(',')
self.encap = {}
for offset in encap:
try:
attr, value = offset.split('=')
self.encap[attr] = int(value)
except:
raise HeaderError(self.ENCAPSULATED + ' malformed: ' + \
str(encap))
def parse_content_encoding(self, line):
content_encoding = self.parse_header(line)
if content_encoding[1].lower() == self.GZIP:
self.descompressor = zlib.decompressobj()
self.content_encoding = True
else:
self.descompressor = None
def error(self, msg):
self.transport.write("HTTP/1.1 400 Bad Request\r\n\r\n%s\r\n" % \
(str(msg)))
def connectionLost(self, reason = None):
#Remove temp files
for file in self.temp_files:
os.unlink(file)
#main()
def main():
openlog('icap-server.py')
icap_factory = Factory()
icap_factory.protocol = ICAP
reactor.listenTCP(int(port), icap_factory, interface = bind_to)
reactor.run()
while True:
pid = os.fork()
if not pid:
main()
break
os.waitpid(pid, 0)
syslog('Child process died: ' + str(pid))