Rewrite srvuser API to parse application/x-www-form-urlencoded

This commit is contained in:
Anthony Wang 2022-05-21 14:57:31 -05:00
parent aa548d1d7f
commit 1c73d74f75
Signed by: a
GPG key ID: BC96B00AEC5F2D76

View file

@ -1,5 +1,5 @@
from subprocess import run, check_output
from json import loads
from urllib.parse import parse_qs
from http.server import BaseHTTPRequestHandler
from adduser import adduser
@ -9,14 +9,29 @@ from ldappass import code
class srvuser(BaseHTTPRequestHandler):
"""User management HTTP server"""
def do_GET(self):
"""Handle GET requests"""
body = 'It worked!'
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_POST(self):
"""Handle registration POST requests"""
"""Handle API POST requests"""
content_length = int(self.headers['Content-Length'])
data = loads(self.rfile.read(content_length).decode('utf-8'))
print([data[key] for key in data.keys() if key != 'password']) # Print data
data = parse_qs(self.rfile.read(content_length).decode('utf-8'))
if data['type'] == 'new':
# Print debug data
debug = data.copy()
debug.pop('password')
debug.pop('confirmpassword')
print(debug)
if self.path == '/api/join':
# New user
if data['code'] != code():
print('Incorrect code')