Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small improvments #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/japronto/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

signames = {
int(v): v.name for k, v in signal.__dict__.items()
if isinstance(v, signal.Signals)}
if isinstance(v, signal.Signals)
}


class Application:
Expand Down Expand Up @@ -252,12 +253,12 @@ def run(self, host='0.0.0.0', port=8080, *, worker_num=None, reload=False,
return

reloader_pid = None
if reload:
if '_JAPR_RELOADER' not in os.environ:
from japronto.reloader import exec_reloader
exec_reloader(host=host, port=port, worker_num=worker_num)
else:
reloader_pid = int(os.environ['_JAPR_RELOADER'])
jarp_var = os.environ.get('_JAPR_RELOADER')
if reload and jarp_var:
reloader_pid = int(jarp_var)
elif reload:
from japronto.reloader import exec_reloader
exec_reloader(host=host, port=port, worker_num=worker_num)

self._run(
host=host, port=port, worker_num=worker_num,
Expand Down
1 change: 1 addition & 0 deletions src/japronto/protocol/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def make_class(flavor):
from japronto.parser import cparser

class HttpProtocol(asyncio.Protocol):
__slots__ = ('parser', 'loop', 'response', 'transport', 'queue')
def __init__(self, loop, handler):
self.parser = cparser.HttpRequestParser(
self.on_headers, self.on_body, self.on_error)
Expand Down
17 changes: 7 additions & 10 deletions src/japronto/response/py.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ def factory(status_code=200, text='', mime_type='text/plain',
encoding='utf-8'):
global _responses
if _responses is None:
_responses = []
for _ in range(100):
_responses.append(Response())
_responses = [Response() for _ in range(100)]

response = _responses.pop()

Expand All @@ -34,12 +32,11 @@ def __init__(self, status_code=200, text='', mime_type='text/plain',
self.encoding = encoding

def render(self):
data = ['HTTP/1.1 ', str(self.status_code), ' OK\r\n']
data.append('Connection: keep-alive\r\n')
body = self.text.encode(self.encoding)
data.extend([
'Content-Type: ', self.mime_type,
'; encoding=', self.encoding, '\r\n'])
data.extend(['Content-Length: ', str(len(body)), '\r\n\r\n'])

data = (
'HTTP/1.1 ', str(self.status_code), ' OK\r\n'
'Connection: keep-alive\r\n'
'Content-Type: ', self.mime_type, '; encoding=', self.encoding, '\r\n'
'Content-Length: ', str(len(body)), '\r\n\r\n',
)
return ''.join(data).encode(self.encoding) + body
9 changes: 5 additions & 4 deletions src/japronto/router/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class RouteNotFoundException(Exception):


class Route:
__slots__ = ('pattern', 'handler', 'methods', 'segments', 'placeholder_cnt')

def __init__(self, pattern, handler, methods):
self.pattern = pattern
self.handler = handler
Expand All @@ -31,7 +33,6 @@ def __eq__(self, other):


def parse(pattern):
names = set()
result = []

rest = pattern
Expand All @@ -58,10 +59,10 @@ def parse(pattern):
if rest and rest[0] != '/':
raise ValueError(
'"}" must be followed by "/" or appear at the end')
if name in names:
name_val = ('placeholder', name)
if name_val in result:
raise ValueError('Duplicate name "{}" in pattern'.format(name))
names.add(name)
result.append(('placeholder', name))
result.append(name_val)

return result

Expand Down