Skip to content

Commit

Permalink
Parse request line without using regular expressions.
Browse files Browse the repository at this point in the history
Split the request line by spaces. This allows any character to be used in the request URI.
  • Loading branch information
pjdietz committed Oct 11, 2015
1 parent 562be68 commit 28db0bd
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions rester/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@
from urlparse import parse_qs
from urllib import quote

RE_METHOD = """(?P<method>[A-Z]+)"""
RE_URI = """(?P<uri>[a-zA-Z0-9\-\/\.\_\:\?\#\[\]\@\!\$\&\=]+)"""


def _read_request_line_dict(line):
# Return a dicionary containing information about the request.
m = re.search(RE_METHOD + "\s+" + RE_URI, line)
if m:
return m.groupdict()
m = re.search(RE_URI, line)
if m:
return m.groupdict()
return None
"""Return a dict containing the method and uri for a request line"""

# Split the line into words.
words = line.split(" ")
method = "GET"
# If the line contains only one word, assume the line is the URI.
if len(words) == 1:
uri = words[0]
else:
method = words[0]
uri = words[1]

return {
"method": method,
"uri": uri
}


class RequestParser:
Expand Down

0 comments on commit 28db0bd

Please sign in to comment.