Skip to content

Commit

Permalink
Making sure Host request header is the first one
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas Kanashiro <kanashiro.duarte@gmail.com>
Signed-off-by: Sergio Oliveira <seocam@seocam.com>
  • Loading branch information
seocam committed Dec 15, 2015
1 parent f5dea88 commit 2fd9a10
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
24 changes: 24 additions & 0 deletions revproxy/connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

from urllib3.connection import HTTPConnection, HTTPSConnection
from urllib3.connectionpool import HTTPConnectionPool, HTTPSConnectionPool


def _output(self, s):
"""Host header should always be first"""

if s.lower().startswith('host: '):
self._buffer.insert(1, s)
else:
self._buffer.append(s)

HTTPConnectionPool.ConnectionCls = type(
'RevProxyHTTPConnection',
(HTTPConnection,),
{'_output': _output},
)

HTTPSConnectionPool.ConnectionCls = type(
'RevProxyHTTPSConnection',
(HTTPSConnection,),
{'_output': _output}
)
27 changes: 27 additions & 0 deletions revproxy/pool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

from urllib3.poolmanager import PoolManager as PoolManager_, SSL_KEYWORDS

from .connection import HTTPConnectionPool, HTTPSConnectionPool

pool_classes_by_scheme = {
'http': HTTPConnectionPool,
'https': HTTPSConnectionPool,
}


class PoolManager(PoolManager_):
def _new_pool(self, scheme, host, port):
"""
Create a new :class:`ConnectionPool` based on host, port and scheme.
This method is used to actually create the connection pools handed out
by :meth:`connection_from_url` and companion methods. It is intended
to be overridden for customization.
"""
pool_cls = pool_classes_by_scheme[scheme]
kwargs = self.connection_pool_kw
if scheme == 'http':
kwargs = self.connection_pool_kw.copy()
for kw in SSL_KEYWORDS:
kwargs.pop(kw, None)

return pool_cls(host, port, **kwargs)
5 changes: 3 additions & 2 deletions revproxy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
from django.views.generic.base import ContextMixin

from .exceptions import InvalidUpstream
from .pool import PoolManager
from .response import get_django_response
from .utils import normalize_request_headers, encode_items
from .transformer import DiazoTransformer
from .utils import normalize_request_headers, encode_items

# Chars that don't need to be quoted. We use same than nginx:
# https://github.com/nginx/nginx/blob/nginx-1.9/src/core/ngx_string.c
Expand All @@ -31,7 +32,7 @@
"'http' or 'https' (%s).")
}

HTTP_POOLS = urllib3.PoolManager()
HTTP_POOLS = PoolManager()


class ProxyView(View):
Expand Down

0 comments on commit 2fd9a10

Please sign in to comment.