def __call__(self, env, start_response, client_cert=None): """ Finds the configuration for the given URL and passes the control on to the main request handler. In case no config for the given URL is found, a 404 Not Found will be returned to the calling side. """ ctx = InvocationContext(self.instance_name, self.instance_unique, self.msg_counter.next(), self.now()) ctx.auth_result = AuthResult() ctx.env = env if self.sign_invocation_id: h = hashlib.sha256() h.update('{0}:{1}'.format(self.instance_secret, ctx.invocation_id)) ctx.invocation_id_signed = h.hexdigest() path_info = env['PATH_INFO'] if self.quote_path_info: path_info = quote_plus(path_info) query_string = env.get('QUERY_STRING') if query_string: query_string = '?' + query_string if self.quote_query_string: query_string = quote_plus(query_string) ctx.path_info = path_info ctx.query_string = query_string ctx.remote_address = env.get('REMOTE_ADDR') ctx.request_method = env.get('REQUEST_METHOD') for c, url_config in self.urls: match = c.test(path_info) if match: ctx.url_config = url_config return self._on_request(ctx, start_response, env, url_config, client_cert, match) else: # No config for that URL, we can't let the client in. return self._404(ctx, start_response)
def test_invocation_context_init_parameters(): """ Makes sure the parameters passed into InocationContext.__init___ are being assigned to the instance correctly. """ (_instance_name, _instance_unique, _message_number, _proc_start, _proc_end, _ext_start, _ext_end, _env, _url_config, _client_cert, _data, _remote_address, _auth_result, _config_type, _path_info, _query_string, _client_address, _request_method) = [uuid4().hex for x in range(18)] ctx = InvocationContext(_instance_name, _instance_unique, _message_number, _proc_start, _proc_end, _ext_start, _ext_end, _env, _url_config, _client_cert, _data, _remote_address, _auth_result, _config_type, _path_info, _query_string, _client_address, _request_method) eq_(ctx.instance_name, _instance_name) eq_(ctx.instance_unique, _instance_unique) eq_(ctx.message_number, _message_number) eq_(ctx.proc_start, _proc_start) eq_(ctx.proc_end, _proc_end) eq_(ctx.ext_start, _ext_start) eq_(ctx.ext_end, _ext_end) eq_(ctx.env, _env) eq_(ctx.url_config, _url_config) eq_(ctx.client_cert, _client_cert) eq_(ctx.data, _data) eq_(ctx.remote_address, _remote_address) eq_(ctx.auth_result, _auth_result) eq_(ctx.config_type, _config_type) eq_(ctx.path_info, _path_info) eq_(ctx.query_string, _query_string) eq_(ctx.client_address, _client_address) eq_(ctx.request_method, _request_method) eq_(ctx.stop_watch_format, '{0.seconds}.{0.microseconds:06d}') eq_( ctx.invocation_id, '{0}/{1}/{2}'.format(_instance_name, _instance_unique, _message_number))
def test_invocation_context_format_log_message(): """ Tests the correctness of formatting of logging messages. """ _auth1 = AuthResult(True) _auth2 = AuthResult(False, uuid4().hex) for _auth_result in _auth1, _auth2: for _needs_details in True, False: _now = datetime.now() _start_to_ext_start = timedelta(seconds=1, microseconds=129) _ext_took = timedelta(seconds=3, microseconds=9017) _ext_end_to_proc_end = timedelta(seconds=7, microseconds=3511) _proc_start = _now _proc_end = _now + _start_to_ext_start + _ext_took + _ext_end_to_proc_end _ext_start = _now + _start_to_ext_start _ext_end = _now + _start_to_ext_start + _ext_took _env = {'HTTP_USER_AGENT':uuid4().hex, 'SERVER_SOFTWARE':uuid4().hex, 'SERVER_NAME':uuid4().hex, 'SERVER_PORT':uuid4().hex} _code = uuid4().hex (_instance_name, _instance_unique, _message_number, _url_config, _client_cert, _data, _remote_address, _config_type, _path_info, _query_string, _client_address, _request_method) = [uuid4().hex for x in range(12)] ctx = InvocationContext(_instance_name, _instance_unique, _message_number, _proc_start, _proc_end, _ext_start, _ext_end, _env, _url_config, _client_cert, _data, _remote_address, _auth_result, _config_type, _path_info, _query_string, _client_address, _request_method) msg = ctx.format_log_message(_code, _needs_details) if _needs_details: (invocation_id, code, proc_start, remote_address, req_info, secwall_overhead, ext_overhead, proc_total, auth_result, auth_code, http_user_agent, server_software, server_name, server_port, config_type, data) = msg.split(';') else: (invocation_id, code, proc_start, remote_address, req_info, secwall_overhead, ext_overhead, proc_total, auth_result, auth_code) = msg.split(';') eq_(invocation_id, ctx.invocation_id) eq_(code, _code) eq_(proc_start, str(_proc_start)) eq_(remote_address, _remote_address) eq_(req_info, _request_method + ' ' + _path_info + _query_string) _proc_total = _proc_end - _proc_start _ext_overhead = _ext_end - _ext_start _secwall_overhead = _proc_total - _ext_overhead eq_(proc_total, str(_proc_total.seconds) + '.' + str(_proc_total.microseconds).zfill(6)) eq_(ext_overhead, str(_ext_overhead.seconds) + '.' + str(_ext_overhead.microseconds).zfill(6)) eq_(secwall_overhead, str(_secwall_overhead.seconds) + '.' + str(_secwall_overhead.microseconds).zfill(6)) if _auth_result: eq_(auth_result, '0') else: eq_(auth_result, '1') eq_(auth_code, _auth_result.code) if _needs_details: eq_(http_user_agent, '"{0}"'.format(_env.get('HTTP_USER_AGENT'))) eq_(server_software, _env.get('SERVER_SOFTWARE')) eq_(server_name, _env.get('SERVER_NAME')) eq_(server_port, _env.get('SERVER_PORT')) eq_(config_type, _config_type) eq_(data, _data)
def __call__(self, env, start_response, client_cert=None, client_cert_der=None): """ Finds the configuration for the given URL and passes the control on to the main request handler. In case no config for the given URL is found, a 404 Not Found will be returned to the calling side. """ ctx = InvocationContext(self.instance_name, self.instance_unique, self.msg_counter.next(), self.now()) ctx.auth_result = AuthResult() ctx.env = env ctx.client_cert = client_cert ctx.client_cert_der = client_cert_der if self.sign_invocation_id: h = hashlib.sha256() h.update('{0}:{1}'.format(self.instance_secret, ctx.invocation_id)) ctx.invocation_id_signed = h.hexdigest() path_info = env['PATH_INFO'] if self.quote_path_info: path_info = quote_plus(path_info) query_string = env.get('QUERY_STRING') if query_string: query_string = '?' + query_string if self.quote_query_string: query_string = quote_plus(query_string) ctx.path_info = path_info ctx.query_string = query_string ctx.remote_address = env.get('REMOTE_ADDR') ctx.request_method = env.get('REQUEST_METHOD') for c, url_config in self.urls: match = c.test(path_info) if match: ctx.url_config = url_config return self._on_request(ctx, start_response, env, url_config, client_cert, match) else: # No config for that URL, we can't let the client in. return self._404(ctx, start_response)
def test_invocation_context_format_log_message(): """ Tests the correctness of formatting of logging messages. """ _auth1 = AuthResult(True) _auth2 = AuthResult(False, uuid4().hex) for _auth_result in _auth1, _auth2: for _needs_details in True, False: _now = datetime.now() _start_to_ext_start = timedelta(seconds=1, microseconds=129) _ext_took = timedelta(seconds=3, microseconds=9017) _ext_end_to_proc_end = timedelta(seconds=7, microseconds=3511) _proc_start = _now _proc_end = _now + _start_to_ext_start + _ext_took + _ext_end_to_proc_end _ext_start = _now + _start_to_ext_start _ext_end = _now + _start_to_ext_start + _ext_took _env = { 'HTTP_USER_AGENT': uuid4().hex, 'SERVER_SOFTWARE': uuid4().hex, 'SERVER_NAME': uuid4().hex, 'SERVER_PORT': uuid4().hex } _code = uuid4().hex (_instance_name, _instance_unique, _message_number, _url_config, _client_cert, _data, _remote_address, _config_type, _path_info, _query_string, _client_address, _request_method) = [uuid4().hex for x in range(12)] ctx = InvocationContext(_instance_name, _instance_unique, _message_number, _proc_start, _proc_end, _ext_start, _ext_end, _env, _url_config, _client_cert, _data, _remote_address, _auth_result, _config_type, _path_info, _query_string, _client_address, _request_method) msg = ctx.format_log_message(_code, _needs_details) if _needs_details: (invocation_id, code, proc_start, remote_address, req_info, secwall_overhead, ext_overhead, proc_total, auth_result, auth_code, http_user_agent, server_software, server_name, server_port, config_type, data) = msg.split(';') else: (invocation_id, code, proc_start, remote_address, req_info, secwall_overhead, ext_overhead, proc_total, auth_result, auth_code) = msg.split(';') eq_(invocation_id, ctx.invocation_id) eq_(code, _code) eq_(proc_start, str(_proc_start)) eq_(remote_address, _remote_address) eq_(req_info, _request_method + ' ' + _path_info + _query_string) _proc_total = _proc_end - _proc_start _ext_overhead = _ext_end - _ext_start _secwall_overhead = _proc_total - _ext_overhead eq_( proc_total, str(_proc_total.seconds) + '.' + str(_proc_total.microseconds).zfill(6)) eq_( ext_overhead, str(_ext_overhead.seconds) + '.' + str(_ext_overhead.microseconds).zfill(6)) eq_( secwall_overhead, str(_secwall_overhead.seconds) + '.' + str(_secwall_overhead.microseconds).zfill(6)) if _auth_result: eq_(auth_result, '0') else: eq_(auth_result, '1') eq_(auth_code, _auth_result.code) if _needs_details: eq_(http_user_agent, '"{0}"'.format(_env.get('HTTP_USER_AGENT'))) eq_(server_software, _env.get('SERVER_SOFTWARE')) eq_(server_name, _env.get('SERVER_NAME')) eq_(server_port, _env.get('SERVER_PORT')) eq_(config_type, _config_type) eq_(data, _data)