errors.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. # We don't need to call super() in __init__ methods of our
  6. # BaseException and Exception classes because we also define
  7. # our own __str__ methods so there is no need to pass 'message'
  8. # to the base class to get a meaningful output from 'str(exc)'.
  9. # pylint: disable=super-init-not-called
  10. class ParseException(Exception):
  11. pass
  12. class NoMoreData(IOError):
  13. def __init__(self, buf=None):
  14. self.buf = buf
  15. def __str__(self):
  16. return "No more data after: %r" % self.buf
  17. class InvalidRequestLine(ParseException):
  18. def __init__(self, req):
  19. self.req = req
  20. self.code = 400
  21. def __str__(self):
  22. return "Invalid HTTP request line: %r" % self.req
  23. class InvalidRequestMethod(ParseException):
  24. def __init__(self, method):
  25. self.method = method
  26. def __str__(self):
  27. return "Invalid HTTP method: %r" % self.method
  28. class InvalidHTTPVersion(ParseException):
  29. def __init__(self, version):
  30. self.version = version
  31. def __str__(self):
  32. return "Invalid HTTP Version: %r" % self.version
  33. class InvalidHeader(ParseException):
  34. def __init__(self, hdr, req=None):
  35. self.hdr = hdr
  36. self.req = req
  37. def __str__(self):
  38. return "Invalid HTTP Header: %r" % self.hdr
  39. class InvalidHeaderName(ParseException):
  40. def __init__(self, hdr):
  41. self.hdr = hdr
  42. def __str__(self):
  43. return "Invalid HTTP header name: %r" % self.hdr
  44. class InvalidChunkSize(IOError):
  45. def __init__(self, data):
  46. self.data = data
  47. def __str__(self):
  48. return "Invalid chunk size: %r" % self.data
  49. class ChunkMissingTerminator(IOError):
  50. def __init__(self, term):
  51. self.term = term
  52. def __str__(self):
  53. return "Invalid chunk terminator is not '\\r\\n': %r" % self.term
  54. class LimitRequestLine(ParseException):
  55. def __init__(self, size, max_size):
  56. self.size = size
  57. self.max_size = max_size
  58. def __str__(self):
  59. return "Request Line is too large (%s > %s)" % (self.size, self.max_size)
  60. class LimitRequestHeaders(ParseException):
  61. def __init__(self, msg):
  62. self.msg = msg
  63. def __str__(self):
  64. return self.msg
  65. class InvalidProxyLine(ParseException):
  66. def __init__(self, line):
  67. self.line = line
  68. self.code = 400
  69. def __str__(self):
  70. return "Invalid PROXY line: %r" % self.line
  71. class ForbiddenProxyRequest(ParseException):
  72. def __init__(self, host):
  73. self.host = host
  74. self.code = 403
  75. def __str__(self):
  76. return "Proxy request from %r not allowed" % self.host
  77. class InvalidSchemeHeaders(ParseException):
  78. def __str__(self):
  79. return "Contradictory scheme headers"