Skip to content
This repository
Browse code

Substitute HTTP Host header when proxying and fix HTTP socket reading.

  • Loading branch information...
commit ed1c7568a041d9871394b5bac41ca167a9d66008 1 parent c6e286e
Jeff Terrace jterrace authored

Showing 2 changed files with 11 additions and 31 deletions. Show diff stats Hide diff stats

  1. +9 31 vaurien/protocols/http.py
  2. +2 0  vaurien/proxy.py
40 vaurien/protocols/http.py
@@ -4,11 +4,9 @@
4 4 from vaurien.util import chunked
5 5
6 6
7   -RE_LEN = re.compile('Content-Length: (\d+)', re.M | re.I)
8 7 RE_KEEPALIVE = re.compile('Connection: Keep-Alive')
9   -RE_MEMCACHE_COMMAND = re.compile('(.*)\r\n')
  8 +HOST_REPLACE = re.compile(r'\r\nHost: .+\r\n')
10 9
11   -EOH = '\r\n\r\n'
12 10 CRLF = '\r\n'
13 11
14 12
@@ -22,6 +20,7 @@ def _handle(self, source, dest, to_backend):
22 20
23 21 # Getting the HTTP query
24 22 data = self._get_data(source)
  23 + data = HOST_REPLACE.sub('\r\nHost: %s\r\n' % self.proxy.backend, data)
25 24
26 25 if not data:
27 26 self._abort_handling(to_backend, dest)
@@ -31,39 +30,18 @@ def _handle(self, source, dest, to_backend):
31 30 dest.sendall(data)
32 31
33 32 # Receiving the response
34   - buffer = self._get_data(dest, buffer_size)
  33 + data = self._get_data(dest, buffer_size)
  34 + source.sendall(data)
35 35
36   - source.sendall(buffer)
37   -
38   - # Reading the HTTP Headers
39   - while EOH not in buffer:
40   - data = self._get_data(dest, buffer_size)
41   - buffer += data
42   - source.sendall(data)
  36 + buffer = data
  37 + while data:
  38 + data = self._get_data(dest, buffer_size)
  39 + buffer += data
  40 + source.sendall(data)
43 41
44 42 # keep alive header ?
45 43 keep_alive = RE_KEEPALIVE.search(buffer) is not None
46 44
47   - # content-length header - to see if we need to suck more
48   - # data.
49   - match = RE_LEN.search(buffer)
50   - if match:
51   - resp_len = int(match.group(1))
52   - left_to_read = resp_len - len(buffer)
53   - if left_to_read > 0:
54   - for chunk in chunked(left_to_read, buffer_size):
55   - data = self._get_data(dest, chunk)
56   - buffer += data
57   - source.sendall(data)
58   - else:
59   - # embarrassing...
60   - # just sucking until recv() returns ''
61   - while True:
62   - data = self._get_data(dest, buffer_size)
63   - if data == '':
64   - break
65   - source.sendall(data)
66   -
67 45 # do we close the client ?
68 46 if not keep_alive and not self.option('keep_alive'):
69 47 source.close()
2  vaurien/proxy.py
@@ -25,6 +25,7 @@ def __init__(self, proxy, backend, protocol='tcp', behaviors=None,
25 25
26 26 logger.info('Starting the Chaos TCP Server')
27 27 parsed_proxy = parse_address(proxy)
  28 + self.backend = backend
28 29 dest = parse_address(backend)
29 30 backlog = cfg.get('backlog', 8192)
30 31 StreamServer.__init__(self, parsed_proxy, backlog=backlog, **kwargs)
@@ -58,6 +59,7 @@ def __init__(self, proxy, backend, protocol='tcp', behaviors=None,
58 59 args = settings['args']
59 60 self.handler.update_settings(extract_settings(args, 'protocol',
60 61 self.protocol))
  62 + self.handler.proxy = self
61 63
62 64 logger.info('Options:')
63 65 logger.info('* proxies from %s to %s' % (proxy, backend))

0 comments on commit ed1c756

Please sign in to comment.
Something went wrong with that request. Please try again.