17
17
# Unique missing object.
18
18
_missing = object ()
19
19
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
24
31
25
32
26
33
class MultipartState (IntEnum ):
@@ -727,7 +734,7 @@ class QuerystringParser(BaseParser):
727
734
728
735
def __init__ (self , callbacks = {}, strict_parsing = False , max_size = float ("inf" )):
729
736
super ().__init__ ()
730
- self .state = STATE_BEFORE_FIELD
737
+ self .state = QuerystringState . BEFORE_FIELD
731
738
self ._found_sep = False
732
739
733
740
self .callbacks = callbacks
@@ -783,7 +790,7 @@ def _internal_write(self, data, length):
783
790
ch = data [i ]
784
791
785
792
# Depending on our state...
786
- if state == STATE_BEFORE_FIELD :
793
+ if state == QuerystringState . BEFORE_FIELD :
787
794
# If the 'found_sep' flag is set, we've already encountered
788
795
# and skipped a single separator. If so, we check our strict
789
796
# parsing flag and decide what to do. Otherwise, we haven't
@@ -810,10 +817,10 @@ def _internal_write(self, data, length):
810
817
# this state.
811
818
self .callback ("field_start" )
812
819
i -= 1
813
- state = STATE_FIELD_NAME
820
+ state = QuerystringState . FIELD_NAME
814
821
found_sep = False
815
822
816
- elif state == STATE_FIELD_NAME :
823
+ elif state == QuerystringState . FIELD_NAME :
817
824
# Try and find a separator - we ensure that, if we do, we only
818
825
# look for the equal sign before it.
819
826
sep_pos = data .find (b"&" , i )
@@ -836,11 +843,11 @@ def _internal_write(self, data, length):
836
843
# added to it below, which means the next iteration of this
837
844
# loop will inspect the character after the equals sign.
838
845
i = equals_pos
839
- state = STATE_FIELD_DATA
846
+ state = QuerystringState . FIELD_DATA
840
847
else :
841
848
# No equals sign found.
842
849
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.
844
851
# If we found the separator, we emit the name and just
845
852
# end - there's no data callback at all (not even with
846
853
# a blank value).
@@ -849,7 +856,7 @@ def _internal_write(self, data, length):
849
856
self .callback ("field_end" )
850
857
851
858
i = sep_pos - 1
852
- state = STATE_BEFORE_FIELD
859
+ state = QuerystringState . BEFORE_FIELD
853
860
else :
854
861
# Otherwise, no separator in this block, so the
855
862
# rest of this chunk must be a name.
@@ -873,7 +880,7 @@ def _internal_write(self, data, length):
873
880
self .callback ("field_name" , data , i , length )
874
881
i = length
875
882
876
- elif state == STATE_FIELD_DATA :
883
+ elif state == QuerystringState . FIELD_DATA :
877
884
# Try finding either an ampersand or a semicolon after this
878
885
# position.
879
886
sep_pos = data .find (b"&" , i )
@@ -891,7 +898,7 @@ def _internal_write(self, data, length):
891
898
# "field_start" events only when we actually have data for
892
899
# a field of some sort.
893
900
i = sep_pos - 1
894
- state = STATE_BEFORE_FIELD
901
+ state = QuerystringState . BEFORE_FIELD
895
902
896
903
# Otherwise, emit the rest as data and finish.
897
904
else :
@@ -917,7 +924,7 @@ def finalize(self):
917
924
then the on_end callback.
918
925
"""
919
926
# 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 :
921
928
self .callback ("field_end" )
922
929
self .callback ("end" )
923
930
0 commit comments