def parse_range_header(self, header_line, length): if header_line is None: return None if not header_line.startswith("bytes="): raise ServerError("Cannot parse header Range: %s" % (header_line)) regex = re.match(r"^bytes=(\d*)\-$", header_line) range_start = int(regex.group(1)) if range_start >= length: raise ServerError("Range Overflow") return range_start
def RejectHeader(self, header_obj): rej_headers = header_obj.headers for header_line in rej_headers: header_recd = self.headers.get(header_line) if header_recd and header_recd == rej_headers[header_line]: self.send_error(400, 'Blacklisted Header %s received' % header_line) raise ServerError("Header " + header_line + ' received')
def ExpectHeader(self, header_obj): exp_headers = header_obj.headers for header_line in exp_headers: header_recd = self.headers.get(header_line) if header_recd is None or header_recd != exp_headers[header_line]: self.send_error(400, "Expected Header %s not found" % header_line) raise ServerError("Header " + header_line + " not found")
def Authentication(self, auth_rule): try: self.handle_auth(auth_rule) except ServerError as se: self.send_response(401, "Authorization Required") self.send_challenge(auth_rule.auth_type) self.finish_headers() raise ServerError(se.__str__())
def RejectHeader(self, header_obj): rej_headers = header_obj.headers for header_line in rej_headers: header_recd = self.headers.get(header_line) if header_recd is not None and header_recd == rej_headers[ header_line]: self.send_error( 400, 'Blackisted Header ' + header_line + ' received') self.finish_headers() raise ServerError("Header " + header_line + ' received')
def handle_auth(self, auth_rule): is_auth = True auth_header = self.headers.get("Authorization") required_auth = auth_rule.auth_type if required_auth == "Both" or required_auth == "Both_inline": auth_type = auth_header.split( ' ')[0] if auth_header else required_auth else: auth_type = required_auth try: assert hasattr(self, "authorize_" + auth_type) is_auth = getattr(self, "authorize_" + auth_type)(auth_header, auth_rule) except AssertionError: raise ServerError("Authentication Mechanism " + auth_rule + " not supported") except AttributeError as ae: raise ServerError(ae.__str__()) if is_auth is False: raise ServerError("Unable to Authenticate")
def Response(self, resp_obj): self.send_response(resp_obj.response_code) self.finish_headers() if resp_obj.response_code == 304: raise NoBodyServerError("Conditional get falling to head") raise ServerError("Custom Response code sent.")
def Response(self, resp_obj): self.send_response(resp_obj.response_code) self.finish_headers() raise ServerError("Custom Response code sent.")