This file is indexed.

/usr/lib/python2.7/dist-packages/haproxy/tests/test_regex.py is in python-haproxy-log-analysis 2.0~b0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# -*- coding: utf-8 -*-
from datetime import datetime
from haproxy.line import HAPROXY_LINE_REGEX
from haproxy.line import HTTP_REQUEST_REGEX

import unittest


LINE = '{0} [{1}] {2} {3} {4} - - ---- {5} {6} {7} "{8}"'


class HaproxyLogLineRegexTest(unittest.TestCase):

    def setUp(self):
        self.client_ip_and_port = '127.0.0.1:39759'
        self.accept_date = '09/Dec/2013:12:59:46.633'
        self.server_names = 'loadbalancer default/instance8'
        self.timers = '0/51536/1/48082/99627'
        self.status_and_bytes = '200 83285'
        self.connections_and_retries = '87/87/87/1/0'
        self.queues = '0/67'
        self.headers = '{77.24.148.74}'
        self.http_request = 'GET /path/to/image HTTP/1.1'

    def _build_test_string(self):
        log_line = LINE.format(
            self.client_ip_and_port,
            self.accept_date,
            self.server_names,
            self.timers,
            self.status_and_bytes,
            self.connections_and_retries,
            self.queues,
            self.headers,
            self.http_request,
        )
        return log_line

    def test_line_regex_default_values(self):
        log_line = self._build_test_string()
        matches = HAPROXY_LINE_REGEX.match(log_line)

        self.assertEqual(matches.group('http_request'), self.http_request)

    def test_line_regex_client_ip_and_port(self):
        client_ip = '192.168.0.250'
        client_port = '34'
        self.client_ip_and_port = '{0}:{1}'.format(client_ip, client_port)

        log_line = self._build_test_string()
        matches = HAPROXY_LINE_REGEX.match(log_line)

        self.assertEqual(matches.group('client_ip'), client_ip)
        self.assertEqual(matches.group('client_port'), client_port)

    def test_line_regex_accept_date(self):
        self.accept_date = datetime.now().strftime('%d/%b/%Y:%H:%M:%S.%f')

        log_line = self._build_test_string()
        matches = HAPROXY_LINE_REGEX.match(log_line)

        self.assertTrue(matches.group('accept_date') in self.accept_date)

    def test_line_regex_server_names(self):
        frontend_name = 'SomeThing4'
        backend_name = 'Another1'
        server_name = 'Cloud9'
        self.server_names = '{0} {1}/{2}'.format(
            frontend_name, backend_name, server_name
        )

        log_line = self._build_test_string()
        matches = HAPROXY_LINE_REGEX.match(log_line)

        self.assertEqual(matches.group('frontend_name'), frontend_name)
        self.assertEqual(matches.group('backend_name'), backend_name)
        self.assertEqual(matches.group('server_name'), server_name)

    def test_line_regex_timers(self):
        tq = '23'
        tw = '0'
        tc = '3'
        tr = '4'
        tt = '5'
        self.timers = '{0}/{1}/{2}/{3}/{4}'.format(tq, tw, tc, tr, tt)

        log_line = self._build_test_string()
        matches = HAPROXY_LINE_REGEX.match(log_line)

        self.assertEqual(matches.group('tq'), tq)
        self.assertEqual(matches.group('tw'), tw)
        self.assertEqual(matches.group('tc'), tc)
        self.assertEqual(matches.group('tr'), tr)
        self.assertEqual(matches.group('tt'), tt)

    def test_line_regex_timers_with_sign(self):
        tq = '-23'
        tw = '-10'
        tc = '-3'
        tr = '-4'
        tt = '+5'
        self.timers = '{0}/{1}/{2}/{3}/{4}'.format(tq, tw, tc, tr, tt)

        log_line = self._build_test_string()
        matches = HAPROXY_LINE_REGEX.match(log_line)

        self.assertEqual(matches.group('tq'), tq)
        self.assertEqual(matches.group('tw'), tw)
        self.assertEqual(matches.group('tc'), tc)
        self.assertEqual(matches.group('tr'), tr)
        self.assertEqual(matches.group('tt'), tt)

    def test_line_regex_status_and_bytes(self):
        status = '23'
        bytes_read = '10'
        self.status_and_bytes = '{0} {1}'.format(status, bytes_read)

        log_line = self._build_test_string()
        matches = HAPROXY_LINE_REGEX.match(log_line)

        self.assertEqual(matches.group('status_code'), status)
        self.assertEqual(matches.group('bytes_read'), bytes_read)

    def test_line_regex_status_and_bytes_with_sign(self):
        status = '404'
        bytes_read = '+10'
        self.status_and_bytes = '{0} {1}'.format(status, bytes_read)

        log_line = self._build_test_string()
        matches = HAPROXY_LINE_REGEX.match(log_line)

        self.assertEqual(matches.group('status_code'), status)
        self.assertEqual(matches.group('bytes_read'), bytes_read)

    def test_line_regex_status_and_bytes_for_terminated_request(self):
        status = '-1'
        bytes_read = '0'
        self.status_and_bytes = '{0} {1}'.format(status, bytes_read)

        log_line = self._build_test_string()
        matches = HAPROXY_LINE_REGEX.match(log_line)

        self.assertEqual(matches.group('status_code'), status)
        self.assertEqual(matches.group('bytes_read'), bytes_read)

    def test_line_regex_connections_and_retries(self):
        act = '40'
        fe = '10'
        be = '11'
        srv = '12'
        retries = '14'
        self.connections_and_retries = '{0}/{1}/{2}/{3}/{4}'.format(
            act, fe, be, srv, retries,
        )

        log_line = self._build_test_string()
        matches = HAPROXY_LINE_REGEX.match(log_line)

        self.assertEqual(matches.group('act'), act)
        self.assertEqual(matches.group('fe'), fe)
        self.assertEqual(matches.group('be'), be)
        self.assertEqual(matches.group('srv'), srv)
        self.assertEqual(matches.group('retries'), retries)

    def test_line_regex_connections_and_retries_with_sign(self):
        act = '30'
        fe = '0'
        be = '111'
        srv = '412'
        retries = '+314'
        self.connections_and_retries = '{0}/{1}/{2}/{3}/{4}'.format(
            act, fe, be, srv, retries,
        )

        log_line = self._build_test_string()
        matches = HAPROXY_LINE_REGEX.match(log_line)

        self.assertEqual(matches.group('act'), act)
        self.assertEqual(matches.group('fe'), fe)
        self.assertEqual(matches.group('be'), be)
        self.assertEqual(matches.group('srv'), srv)
        self.assertEqual(matches.group('retries'), retries)

    def test_line_regex_queues(self):
        server = '30'
        backend = '0'
        self.queues = '{0}/{1}'.format(server, backend)

        log_line = self._build_test_string()
        matches = HAPROXY_LINE_REGEX.match(log_line)

        self.assertEqual(matches.group('queue_server'), server)
        self.assertEqual(matches.group('queue_backend'), backend)

    def test_line_regex_headers_empty(self):
        self.headers = ''

        log_line = self._build_test_string()
        matches = HAPROXY_LINE_REGEX.match(log_line)

        self.assertEqual(matches.group('request_headers'), None)
        self.assertEqual(matches.group('response_headers'), None)

    def test_line_regex_headers(self):
        request = 'something in the air'
        response = 'something not in the air'
        self.headers = '{{{0}}} {{{1}}}'.format(request, response)

        log_line = self._build_test_string()
        matches = HAPROXY_LINE_REGEX.match(log_line)

        self.assertTrue(request in matches.group('request_headers'))
        self.assertTrue(response in matches.group('response_headers'))

    def test_line_regex_headers_only_one(self):
        request = 'something in the air'
        self.headers = '{{{0}}}'.format(request)

        log_line = self._build_test_string()
        matches = HAPROXY_LINE_REGEX.match(log_line)

        self.assertEqual(matches.group('request_headers'), None)
        self.assertEqual(matches.group('response_headers'), None)
        self.assertTrue(request in matches.group('headers'))

    def test_line_regex_http_request(self):
        http_request = 'something in the air'
        self.http_request = http_request

        log_line = self._build_test_string()
        matches = HAPROXY_LINE_REGEX.match(log_line)

        self.assertEqual(matches.group('http_request'), http_request)


class HttpRequestRegexTest(unittest.TestCase):

    def setUp(self):
        self.method = 'GET'
        self.path = '/path/to/image'
        self.protocol = 'HTTP/1.1'

    def _build_test_request(self):
        log_line = '{0} {1} {2}'.format(
            self.method,
            self.path,
            self.protocol,
        )
        return log_line

    def test_http_request_regex(self):
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('method'), self.method)
        self.assertEqual(matches.group('path'), self.path)
        self.assertEqual(matches.group('protocol'), self.protocol)

    def test_http_request_regex_with_port(self):
        self.path = '/path/with/port:80'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_domain(self):
        self.path = '/path/with/example.com'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_anchor(self):
        self.path = '/path/to/article#section'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_parameters(self):
        self.path = '/path/to/article?hello=world&goodbye=lennin'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_dashes_and_underscores(self):
        self.path = '/path/to/article-with-dashes_and_underscores'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_double_slashes(self):
        self.path = '/redirect_to?http://example.com'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_at_signs(self):
        self.path = '/@@funny'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_percent_sign(self):
        self.path = '/something%20encoded'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_plus_sign(self):
        self.path = '/++adding++is+always+fun'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_vertical_bar_sign(self):
        self.path = '/here_or|here'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_tilde_sign(self):
        self.path = '/here~~~e'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_asterisk_sign(self):
        self.path = '/here_*or'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_colon_sign(self):
        self.path = '/something;or-not'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_exclamation_mark_sign(self):
        self.path = '/something-important!'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_dollar_sign(self):
        self.path = '/something$important'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_single_quote_sign(self):
        self.path = '/there\'s-one\'s-way-or-another\'s'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_comma_sign(self):
        self.path = '/there?la=as,is'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_parenthesis(self):
        self.path = '/here_or(here)'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_square_brackets(self):
        self.path = '/here_or[here]'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_less_than_symbol(self):
        self.path = '/here_or<'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_greater_than_symbol(self):
        self.path = '/here_or>'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_back_slash_symbol(self):
        self.path = '/georg-von-grote/\\'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_curly_brackets_symbol(self):
        self.path = '/georg}von{grote/\\'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_diacritics_symbol(self):
        self.path = '/georg`vonĀ“grote/\\'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)

    def test_http_request_regex_with_caret_symbol(self):
        self.path = '/georg`von^grote/\\'
        line = self._build_test_request()
        matches = HTTP_REQUEST_REGEX.match(line)

        self.assertEqual(matches.group('path'), self.path)