I am currently trying to build a web based authentication system for users of a website. This is my HTML-login-form:
<form action="/auth" method="post" class="pure-form pure-form-stacked">
<fieldset>
<legend>Login</legend>
<label for="username">Username</label>
<input id="username" type="username" placeholder="Username">
<label for="password">Password</label>
<input id="password" type="password" placeholder="Password">
<button type="submit" class="pure-button pure-button-primary">
Login
</button>
</fieldset>
</form>
Usually I was expecting it to send a request to the server, with both credentials inside the request body, so my /auth route can check them. But the parser for Post-methods which I use doesn't seem to find anything inside the request body. This is my request-parser (the part of it where it checks for post-parameters:
class Request:
"""
http request data.
"""
def __init__(self):
self.headers = {}
self.method = None
self.protocol = None
self.resource = None
self.path = None
self.params = {}
self.origin = None # will be set from server
def parse(self, conn):
"""Parses an http-Request and return a dictionary with process_request line values and headers."""
self.headers = {}
# read process_request line
request_line = conn.readline().decode('utf-8').strip()
log(1, "Request-Line: %s" % request_line)
if not request_line: # rfc says "server SHOULD ignore blank request lines"
return None
# parse process_request line
try:
self.method, self.resource, self.protocol = request_line.split(" ")
except ValueError:
raise StopProcessing(400, "Bad request-line: %s\n" % request_line)
# parse resource to path and params
# extract GET parameters
from urllib.parse import urlparse, parse_qs # analyse urls and parse query strings
requrl = urlparse(self.resource)
self.path = requrl.path
self.params.update(parse_qs(requrl.query))
# read and parse Request-Headers
while True:
header_line = conn.readline().decode('utf-8').strip()
if not header_line:
break
log(2, "Header-Line: " + header_line)
(headerfield, headervalue) = header_line.split(":", 1)
self.headers[headerfield.strip()] = headervalue.strip()
# read cookies
if 'Cookie' in self.headers:
log(2, "Cookie ist: %s" % self.headers['Cookie'])
self.cookies = Cookie.parse(self.headers['Cookie'])
else:
self.cookies = {}
# parse POST parameters
log(1,"Methode %s" % self.method)
if self.method == 'POST' or self.method == 'post':
postbody = conn.read(int(self.headers['Content-Length'])).decode('utf-8')
self.params.update(parse_qs(postbody))
# all parameter values are lists
# replace lists by the only element if there is only one
for key in self.params:
if len(self.params[key])==1:
self.params[key] = self.params[key][0]
return self.headers
I added some tracing output to the critical passages of the code and what I found was that my parser always returned an empty dictionary {}. What am I missing? There are no add-ons whatsoever used in this framework. it is pure Python.
parse_qs? What isconn? How isself.methodset? Why doesself.methodsupport lower-case and upper-case method names?