Skip to content

Commit 10c2068

Browse files
committed
Remove proxy protocol, chdir, preload from plain server
1 parent 7ecb6ec commit 10c2068

File tree

6 files changed

+0
-202
lines changed

6 files changed

+0
-202
lines changed

plain/plain/server/app.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,6 @@ class Application(BaseApplication):
7777
# 'init' and 'load' methods are implemented by WSGIApplication.
7878
# pylint: disable=abstract-method
7979

80-
def chdir(self):
81-
# chdir to the configured path before loading,
82-
# default is the current dir
83-
os.chdir(self.cfg.chdir)
84-
85-
# add the path to sys.path
86-
if self.cfg.chdir not in sys.path:
87-
sys.path.insert(0, self.cfg.chdir)
88-
8980
def run(self):
9081
if self.cfg.print_config:
9182
print(self.cfg)

plain/plain/server/arbiter.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,6 @@ def setup(self, app):
121121
for k, v in self.cfg.env.items():
122122
os.environ[k] = v
123123

124-
if self.cfg.preload_app:
125-
self.app.wsgi()
126-
127124
def start(self):
128125
"""\
129126
Initialize the arbiter. Start listening and set pidfile if needed.

plain/plain/server/config.py

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -855,23 +855,6 @@ class PrintConfig(Setting):
855855
"""
856856

857857

858-
class PreloadApp(Setting):
859-
name = "preload_app"
860-
section = "Server Mechanics"
861-
cli = ["--preload"]
862-
validator = validate_bool
863-
action = "store_true"
864-
default = False
865-
desc = """\
866-
Load application code before the worker processes are forked.
867-
868-
By preloading an application you can save some RAM resources as well as
869-
speed up server boot times. Although, if you defer application loading
870-
to each worker process, you can reload your application code easily by
871-
restarting workers.
872-
"""
873-
874-
875858
class Sendfile(Setting):
876859
name = "sendfile"
877860
section = "Server Mechanics"
@@ -910,18 +893,6 @@ class ReusePort(Setting):
910893
"""
911894

912895

913-
class Chdir(Setting):
914-
name = "chdir"
915-
section = "Server Mechanics"
916-
cli = ["--chdir"]
917-
validator = validate_chdir
918-
default = util.getcwd()
919-
default_doc = "``'.'``"
920-
desc = """\
921-
Change directory to specified directory before loading apps.
922-
"""
923-
924-
925896
class Env(Setting):
926897
name = "raw_env"
927898
action = "append"
@@ -1762,53 +1733,6 @@ def ssl_context(conf, default_ssl_context_factory):
17621733
"""
17631734

17641735

1765-
class ProxyProtocol(Setting):
1766-
name = "proxy_protocol"
1767-
section = "Server Mechanics"
1768-
cli = ["--proxy-protocol"]
1769-
validator = validate_bool
1770-
default = False
1771-
action = "store_true"
1772-
desc = """\
1773-
Enable detect PROXY protocol (PROXY mode).
1774-
1775-
Allow using HTTP and Proxy together. It may be useful for work with
1776-
stunnel as HTTPS frontend and Gunicorn as HTTP server.
1777-
1778-
PROXY protocol: http://haproxy.1wt.eu/download/1.5/doc/proxy-protocol.txt
1779-
1780-
Example for stunnel config::
1781-
1782-
[https]
1783-
protocol = proxy
1784-
accept = 443
1785-
connect = 80
1786-
cert = /etc/ssl/certs/stunnel.pem
1787-
key = /etc/ssl/certs/stunnel.key
1788-
"""
1789-
1790-
1791-
class ProxyAllowFrom(Setting):
1792-
name = "proxy_allow_ips"
1793-
section = "Server Mechanics"
1794-
cli = ["--proxy-allow-from"]
1795-
validator = validate_string_to_addr_list
1796-
default = "127.0.0.1,::1"
1797-
desc = """\
1798-
Front-end's IPs from which allowed accept proxy requests (comma separated).
1799-
1800-
Set to ``*`` to disable checking of front-end IPs. This is useful for setups
1801-
where you don't know in advance the IP address of front-end, but
1802-
instead have ensured via other means that only your
1803-
authorized front-ends can access Gunicorn.
1804-
1805-
.. note::
1806-
1807-
This option does not affect UNIX socket connections. Connections not associated with
1808-
an IP address are treated as allowed, unconditionally.
1809-
"""
1810-
1811-
18121736
class KeyFile(Setting):
18131737
name = "keyfile"
18141738
section = "SSL"

plain/plain/server/http/message.py

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77

88
import io
99
import re
10-
import socket
1110

1211
from ..util import bytes_to_str, split_request_uri
1312
from .body import Body, ChunkedReader, EOFReader, LengthReader
1413
from .errors import (
15-
ForbiddenProxyRequest,
1614
InvalidHeader,
1715
InvalidHeaderName,
1816
InvalidHTTPVersion,
19-
InvalidProxyLine,
2017
InvalidRequestLine,
2118
InvalidRequestMethod,
2219
InvalidSchemeHeaders,
@@ -259,7 +256,6 @@ def __init__(self, cfg, unreader, peer_addr, req_number=1):
259256
self.limit_request_line = MAX_REQUEST_LINE
260257

261258
self.req_number = req_number
262-
self.proxy_protocol_info = None
263259
super().__init__(cfg, unreader, peer_addr)
264260

265261
def get_data(self, unreader, buf, stop=False):
@@ -277,13 +273,6 @@ def parse(self, unreader):
277273
# get request line
278274
line, rbuf = self.read_line(unreader, buf, self.limit_request_line)
279275

280-
# proxy protocol
281-
if self.proxy_protocol(bytes_to_str(line)):
282-
# get next request line
283-
buf = io.BytesIO()
284-
buf.write(rbuf)
285-
line, rbuf = self.read_line(unreader, buf, self.limit_request_line)
286-
287276
self.parse_request_line(line)
288277
buf = io.BytesIO()
289278
buf.write(rbuf)
@@ -335,81 +324,6 @@ def read_line(self, unreader, buf, limit=0):
335324
data[idx + 2 :],
336325
) # residue in the buffer, skip \r\n
337326

338-
def proxy_protocol(self, line):
339-
"""\
340-
Detect, check and parse proxy protocol.
341-
342-
:raises: ForbiddenProxyRequest, InvalidProxyLine.
343-
:return: True for proxy protocol line else False
344-
"""
345-
if not self.cfg.proxy_protocol:
346-
return False
347-
348-
if self.req_number != 1:
349-
return False
350-
351-
if not line.startswith("PROXY"):
352-
return False
353-
354-
self.proxy_protocol_access_check()
355-
self.parse_proxy_protocol(line)
356-
357-
return True
358-
359-
def proxy_protocol_access_check(self):
360-
# check in allow list
361-
if (
362-
"*" not in self.cfg.proxy_allow_ips
363-
and isinstance(self.peer_addr, tuple)
364-
and self.peer_addr[0] not in self.cfg.proxy_allow_ips
365-
):
366-
raise ForbiddenProxyRequest(self.peer_addr[0])
367-
368-
def parse_proxy_protocol(self, line):
369-
bits = line.split(" ")
370-
371-
if len(bits) != 6:
372-
raise InvalidProxyLine(line)
373-
374-
# Extract data
375-
proto = bits[1]
376-
s_addr = bits[2]
377-
d_addr = bits[3]
378-
379-
# Validation
380-
if proto not in ["TCP4", "TCP6"]:
381-
raise InvalidProxyLine(f"protocol '{proto}' not supported")
382-
if proto == "TCP4":
383-
try:
384-
socket.inet_pton(socket.AF_INET, s_addr)
385-
socket.inet_pton(socket.AF_INET, d_addr)
386-
except OSError:
387-
raise InvalidProxyLine(line)
388-
elif proto == "TCP6":
389-
try:
390-
socket.inet_pton(socket.AF_INET6, s_addr)
391-
socket.inet_pton(socket.AF_INET6, d_addr)
392-
except OSError:
393-
raise InvalidProxyLine(line)
394-
395-
try:
396-
s_port = int(bits[4])
397-
d_port = int(bits[5])
398-
except ValueError:
399-
raise InvalidProxyLine(f"invalid port {line}")
400-
401-
if not ((0 <= s_port <= 65535) and (0 <= d_port <= 65535)):
402-
raise InvalidProxyLine(f"invalid port {line}")
403-
404-
# Set data
405-
self.proxy_protocol_info = {
406-
"proxy_protocol": proto,
407-
"client_addr": s_addr,
408-
"client_port": s_port,
409-
"proxy_addr": d_addr,
410-
"proxy_port": d_port,
411-
}
412-
413327
def parse_request_line(self, line_bytes):
414328
bits = [bytes_to_str(bit) for bit in line_bytes.split(b" ", 2)]
415329
if len(bits) != 3:

plain/plain/server/http/wsgi.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,6 @@ def default_environ(req, sock, cfg):
9696
return env
9797

9898

99-
def proxy_environ(req):
100-
info = req.proxy_protocol_info
101-
102-
if not info:
103-
return {}
104-
105-
return {
106-
"PROXY_PROTOCOL": info["proxy_protocol"],
107-
"REMOTE_ADDR": info["client_addr"],
108-
"REMOTE_PORT": str(info["client_port"]),
109-
"PROXY_ADDR": info["proxy_addr"],
110-
"PROXY_PORT": str(info["proxy_port"]),
111-
}
112-
113-
11499
def create(req, sock, client, server, cfg):
115100
resp = Response(req, sock, cfg)
116101

@@ -195,9 +180,6 @@ def create(req, sock, client, server, cfg):
195180
environ["PATH_INFO"] = util.unquote_to_wsgi_str(path_info)
196181
environ["SCRIPT_NAME"] = script_name
197182

198-
# override the environ with the correct remote and server address if
199-
# we are behind a proxy using the proxy protocol.
200-
environ.update(proxy_environ(req))
201183
return resp, environ
202184

203185

plain/plain/server/workers/base.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
from .. import util
1919
from ..http.errors import (
2020
ConfigurationProblem,
21-
ForbiddenProxyRequest,
2221
InvalidHeader,
2322
InvalidHeaderName,
2423
InvalidHTTPVersion,
25-
InvalidProxyLine,
2624
InvalidRequestLine,
2725
InvalidRequestMethod,
2826
InvalidSchemeHeaders,
@@ -223,8 +221,6 @@ def handle_error(self, req, client, addr, exc):
223221
| InvalidHeaderName
224222
| LimitRequestLine
225223
| LimitRequestHeaders
226-
| InvalidProxyLine
227-
| ForbiddenProxyRequest
228224
| InvalidSchemeHeaders
229225
| UnsupportedTransferCoding
230226
| ConfigurationProblem
@@ -258,12 +254,6 @@ def handle_error(self, req, client, addr, exc):
258254
reason = "Request Header Fields Too Large"
259255
mesg = f"Error parsing headers: '{str(exc)}'"
260256
status_int = 431
261-
elif isinstance(exc, InvalidProxyLine):
262-
mesg = f"'{str(exc)}'"
263-
elif isinstance(exc, ForbiddenProxyRequest):
264-
reason = "Forbidden"
265-
mesg = "Request forbidden"
266-
status_int = 403
267257
elif isinstance(exc, InvalidSchemeHeaders):
268258
mesg = f"{str(exc)}"
269259
elif isinstance(exc, SSLError):

0 commit comments

Comments
 (0)