Skip to content

Commit

Permalink
Add alt handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
dotcomboom committed Feb 8, 2019
1 parent 6329bf7 commit 95ac3b4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
24 changes: 24 additions & 0 deletions examples/tests_server_tls_default_alt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from multiprocessing import Process

import pituophis


def alt(request):
if request.path == '/test':
return [pituophis.Selector(text='test!')]


def reg():
pituophis.serve("127.0.0.1", 50400, pub_dir='pub/', alt_handler=alt, tls=False) # typical Gopher port is 70


def tls():
pituophis.serve("127.0.0.1", 50500, pub_dir='pub/', alt_handler=alt, tls=True,
tls_cert_chain='cacert.pem', tls_private_key='privkey.pem') # typical S/Gopher port is 105


if __name__ == '__main__':
processes = [Process(target=reg),
Process(target=tls)]
for process in processes:
process.start()
32 changes: 25 additions & 7 deletions pituophis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Request:
"""

def __init__(self, host='127.0.0.1', port=70, path='/', query='', itype='9', tls=False, tls_verify=True, client='',
pub_dir='pub/'):
pub_dir='pub/', alt_handler=False):
"""
Initializes a new Request object.
"""
Expand Down Expand Up @@ -112,6 +112,7 @@ def __init__(self, host='127.0.0.1', port=70, path='/', query='', itype='9', tls
"""
*Server.* The default handler uses this as which directory to serve. Default is 'pub/'.
"""
self.alt_handler = alt_handler

def get(self):
"""
Expand Down Expand Up @@ -396,7 +397,14 @@ def handle(request):
path=(request.path + '/' + file).replace('//', '/'),
host=request.host, port=request.port))
else:
return [errors['404']]
if request.alt_handler:
alt = request.alt_handler(request)
if alt:
return alt
else:
return [errors['404']]
else:
return [errors['404']]
else:
# serving files
if os.path.isfile(res_path):
Expand All @@ -405,17 +413,26 @@ def handle(request):
in_file.close()
return data
else:
return [errors['404']]
if request.alt_handler:
alt = request.alt_handler(request)
if alt:
return alt
else:
return [errors['404']]
else:
return [errors['404']]

return menu


def serve(host="127.0.0.1", port=70, handler=handle, pub_dir='pub/', send_period=False, tls=False,
def serve(host="127.0.0.1", port=70, handler=handle, pub_dir='pub/', alt_handler=False, send_period=False, tls=False,
tls_cert_chain='cacert.pem',
tls_private_key='privkey.pem', debug=True):
"""
*Server.* Listens for Gopher requests. Allows for using a custom handler that will return a Bytes, String, or List
object (which can contain either Strings or Selectors) to send to the client.
object (which can contain either Strings or Selectors) to send to the client, or the default handler which can serve
a directory. Along with the default handler, you can set an alternate handler to use if a 404 error is generated for
dynamic applications.
"""
if tls:
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
Expand Down Expand Up @@ -455,8 +472,9 @@ def data_received(self, data):
if self.transport.get_extra_info('sslcontext'):
is_tls = True

resp = handler(Request(path=path, query=query, host=host, port=port, pub_dir=pub_dir,
client=self.transport.get_extra_info('peername')[0], tls=is_tls))
resp = handler(Request(path=path, query=query, host=host, port=port,
client=self.transport.get_extra_info('peername')[0], pub_dir=pub_dir,
alt_handler=alt_handler, tls=is_tls))

if type(resp) == str:
resp = bytes(resp, 'utf-8')
Expand Down

0 comments on commit 95ac3b4

Please sign in to comment.