Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parser.py 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # -*- coding: utf-8 -
  2. #
  3. # This file is part of gunicorn released under the MIT license.
  4. # See the NOTICE for more information.
  5. from gunicorn.http.message import Request
  6. from gunicorn.http.unreader import SocketUnreader, IterUnreader
  7. class Parser(object):
  8. mesg_class = None
  9. def __init__(self, cfg, source):
  10. self.cfg = cfg
  11. if hasattr(source, "recv"):
  12. self.unreader = SocketUnreader(source)
  13. else:
  14. self.unreader = IterUnreader(source)
  15. self.mesg = None
  16. # request counter (for keepalive connetions)
  17. self.req_count = 0
  18. def __iter__(self):
  19. return self
  20. def __next__(self):
  21. # Stop if HTTP dictates a stop.
  22. if self.mesg and self.mesg.should_close():
  23. raise StopIteration()
  24. # Discard any unread body of the previous message
  25. if self.mesg:
  26. data = self.mesg.body.read(8192)
  27. while data:
  28. data = self.mesg.body.read(8192)
  29. # Parse the next request
  30. self.req_count += 1
  31. self.mesg = self.mesg_class(self.cfg, self.unreader, self.req_count)
  32. if not self.mesg:
  33. raise StopIteration()
  34. return self.mesg
  35. next = __next__
  36. class RequestParser(Parser):
  37. mesg_class = Request