Skip to content

Commit 9c936b7

Browse files
authored
Add QuerystringState (#97)
1 parent 4e24a3e commit 9c936b7

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ lib64
2828
pip-log.txt
2929

3030
# Unit test / coverage reports
31-
.coverage
31+
.coverage.*
3232
.tox
3333
nosetests.xml
3434

multipart/multipart.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@
1717
# Unique missing object.
1818
_missing = object()
1919

20-
# States for the querystring parser.
21-
STATE_BEFORE_FIELD = 0
22-
STATE_FIELD_NAME = 1
23-
STATE_FIELD_DATA = 2
20+
21+
class QuerystringState(IntEnum):
22+
"""Querystring parser states.
23+
24+
These are used to keep track of the state of the parser, and are used to determine
25+
what to do when new data is encountered.
26+
"""
27+
28+
BEFORE_FIELD = 0
29+
FIELD_NAME = 1
30+
FIELD_DATA = 2
2431

2532

2633
class MultipartState(IntEnum):
@@ -727,7 +734,7 @@ class QuerystringParser(BaseParser):
727734

728735
def __init__(self, callbacks={}, strict_parsing=False, max_size=float("inf")):
729736
super().__init__()
730-
self.state = STATE_BEFORE_FIELD
737+
self.state = QuerystringState.BEFORE_FIELD
731738
self._found_sep = False
732739

733740
self.callbacks = callbacks
@@ -783,7 +790,7 @@ def _internal_write(self, data, length):
783790
ch = data[i]
784791

785792
# Depending on our state...
786-
if state == STATE_BEFORE_FIELD:
793+
if state == QuerystringState.BEFORE_FIELD:
787794
# If the 'found_sep' flag is set, we've already encountered
788795
# and skipped a single separator. If so, we check our strict
789796
# parsing flag and decide what to do. Otherwise, we haven't
@@ -810,10 +817,10 @@ def _internal_write(self, data, length):
810817
# this state.
811818
self.callback("field_start")
812819
i -= 1
813-
state = STATE_FIELD_NAME
820+
state = QuerystringState.FIELD_NAME
814821
found_sep = False
815822

816-
elif state == STATE_FIELD_NAME:
823+
elif state == QuerystringState.FIELD_NAME:
817824
# Try and find a separator - we ensure that, if we do, we only
818825
# look for the equal sign before it.
819826
sep_pos = data.find(b"&", i)
@@ -836,11 +843,11 @@ def _internal_write(self, data, length):
836843
# added to it below, which means the next iteration of this
837844
# loop will inspect the character after the equals sign.
838845
i = equals_pos
839-
state = STATE_FIELD_DATA
846+
state = QuerystringState.FIELD_DATA
840847
else:
841848
# No equals sign found.
842849
if not strict_parsing:
843-
# See also comments in the STATE_FIELD_DATA case below.
850+
# See also comments in the QuerystringState.FIELD_DATA case below.
844851
# If we found the separator, we emit the name and just
845852
# end - there's no data callback at all (not even with
846853
# a blank value).
@@ -849,7 +856,7 @@ def _internal_write(self, data, length):
849856
self.callback("field_end")
850857

851858
i = sep_pos - 1
852-
state = STATE_BEFORE_FIELD
859+
state = QuerystringState.BEFORE_FIELD
853860
else:
854861
# Otherwise, no separator in this block, so the
855862
# rest of this chunk must be a name.
@@ -873,7 +880,7 @@ def _internal_write(self, data, length):
873880
self.callback("field_name", data, i, length)
874881
i = length
875882

876-
elif state == STATE_FIELD_DATA:
883+
elif state == QuerystringState.FIELD_DATA:
877884
# Try finding either an ampersand or a semicolon after this
878885
# position.
879886
sep_pos = data.find(b"&", i)
@@ -891,7 +898,7 @@ def _internal_write(self, data, length):
891898
# "field_start" events only when we actually have data for
892899
# a field of some sort.
893900
i = sep_pos - 1
894-
state = STATE_BEFORE_FIELD
901+
state = QuerystringState.BEFORE_FIELD
895902

896903
# Otherwise, emit the rest as data and finish.
897904
else:
@@ -917,7 +924,7 @@ def finalize(self):
917924
then the on_end callback.
918925
"""
919926
# If we're currently in the middle of a field, we finish it.
920-
if self.state == STATE_FIELD_DATA:
927+
if self.state == QuerystringState.FIELD_DATA:
921928
self.callback("field_end")
922929
self.callback("end")
923930

0 commit comments

Comments
 (0)