Properly send srvuser response body

This commit is contained in:
Anthony Wang 2022-05-23 22:10:31 -05:00
parent f357f5af3d
commit bc2c7d7981
Signed by: a
GPG key ID: BC96B00AEC5F2D76
2 changed files with 16 additions and 11 deletions

View file

@ -2,4 +2,4 @@ def ldappass():
return open('/etc/ldappass', 'r').read().split()[0]
def code():
return open('/etc/ldappass', 'r').read().split()[1][:-1]
return open('/etc/ldappass', 'r').read().split()[1]

View file

@ -1,5 +1,5 @@
from subprocess import run, check_output
from urllib.parse import parse_qs
from urllib.parse import parse_qsl
from http.server import BaseHTTPRequestHandler
from adduser import adduser
@ -9,21 +9,26 @@ from ldappass import code
class srvuser(BaseHTTPRequestHandler):
"""User management HTTP server"""
def do_GET(self):
"""Handle GET requests"""
body = 'It worked!'
def send(self, code, body):
"""Send HTTP response and body"""
self.send_response(code)
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.send_header('Content-Length', str(len(body)))
self.end_headers()
self.wfile.write(body.encode('utf-8'))
def do_GET(self):
"""Handle GET requests"""
self.send(200, 'Hello')
def do_POST(self):
"""Handle API POST requests"""
content_length = int(self.headers['Content-Length'])
data = parse_qs(self.rfile.read(content_length).decode('utf-8'))
data = dict(parse_qsl(self.rfile.read(content_length).decode('utf-8')))
# Print debug data
debug = data.copy()
@ -35,11 +40,11 @@ class srvuser(BaseHTTPRequestHandler):
# New user
if data['code'] != code():
print('Incorrect code')
self.send_response(403)
self.send(403, 'Incorrect code')
return
if not all(c.isdigit() or c.islower() for c in data['username']) or data['username'][0].isdigit():
print('Bad username')
self.send_response(403)
self.send(403, 'Bad username')
return
# Add the user
@ -47,7 +52,7 @@ class srvuser(BaseHTTPRequestHandler):
data['email'], data['password'])
else:
print('Bad request')
self.send_response(400)
self.send(400, 'Bad request')
return
self.send_response(200)
self.send(200, 'It worked!')