add support for logging XML request conditionally for profiling
This commit is contained in:
@@ -204,7 +204,7 @@ class Application(ApplicationPartDelete, ApplicationPartHead,
|
||||
"%s", environ.get("REQUEST_METHOD", "unknown"),
|
||||
environ.get("PATH_INFO", ""), e, exc_info=True)
|
||||
# Make minimal response
|
||||
status, raw_headers, raw_answer = (
|
||||
status, raw_headers, raw_answer, xml_request = (
|
||||
httputils.INTERNAL_SERVER_ERROR)
|
||||
assert isinstance(raw_answer, str)
|
||||
answer = raw_answer.encode("ascii")
|
||||
@@ -224,12 +224,14 @@ class Application(ApplicationPartDelete, ApplicationPartHead,
|
||||
unsafe_path = environ.get("PATH_INFO", "")
|
||||
https = environ.get("HTTPS", "")
|
||||
profiler = None
|
||||
xml_request = None
|
||||
|
||||
context = AuthContext()
|
||||
|
||||
"""Manage a request."""
|
||||
def response(status: int, headers: types.WSGIResponseHeaders,
|
||||
answer: Union[None, str, bytes]) -> _IntermediateResponse:
|
||||
answer: Union[None, str, bytes],
|
||||
xml_request: Union[None, str] = None) -> _IntermediateResponse:
|
||||
"""Helper to create response from internal types.WSGIResponse"""
|
||||
headers = dict(headers)
|
||||
content_encoding = "plain"
|
||||
@@ -468,7 +470,7 @@ class Application(ApplicationPartDelete, ApplicationPartHead,
|
||||
elif self._profiling_per_request_method:
|
||||
self.profiler_per_request_method[request_method].enable()
|
||||
|
||||
status, headers, answer = function(
|
||||
status, headers, answer, xml_request = function(
|
||||
environ, base_prefix, path, user, remote_host, remote_useragent)
|
||||
|
||||
# Profiling
|
||||
@@ -478,13 +480,13 @@ class Application(ApplicationPartDelete, ApplicationPartHead,
|
||||
elif self._profiling_per_request_method:
|
||||
self.profiler_per_request_method[request_method].disable()
|
||||
|
||||
if (status, headers, answer) == httputils.NOT_ALLOWED:
|
||||
if (status, headers, answer, xml_request) == httputils.NOT_ALLOWED:
|
||||
logger.info("Access to %r denied for %s", path,
|
||||
repr(user) if user else "anonymous user")
|
||||
else:
|
||||
status, headers, answer = httputils.NOT_ALLOWED
|
||||
status, headers, answer, xml_request = httputils.NOT_ALLOWED
|
||||
|
||||
if ((status, headers, answer) == httputils.NOT_ALLOWED and not user and
|
||||
if ((status, headers, answer, xml_request) == httputils.NOT_ALLOWED and not user and
|
||||
not external_login):
|
||||
# Unknown or unauthorized user
|
||||
logger.debug("Asking client for authentication")
|
||||
@@ -494,4 +496,4 @@ class Application(ApplicationPartDelete, ApplicationPartHead,
|
||||
"WWW-Authenticate":
|
||||
"Basic realm=\"%s\"" % self._auth_realm})
|
||||
|
||||
return response(status, headers, answer)
|
||||
return response(status, headers, answer, xml_request)
|
||||
|
||||
@@ -93,7 +93,7 @@ class ApplicationBase:
|
||||
"""Generate XML error response."""
|
||||
headers = {"Content-Type": "text/xml; charset=%s" % self._encoding}
|
||||
content = self._xml_response(xmlutils.webdav_error(human_tag))
|
||||
return status, headers, content
|
||||
return status, headers, content, None
|
||||
|
||||
|
||||
class Access:
|
||||
|
||||
@@ -110,4 +110,4 @@ class ApplicationPartDelete(ApplicationBase):
|
||||
for notification_item in hook_notification_item_list:
|
||||
self._hook.notify(notification_item)
|
||||
headers = {"Content-Type": "text/xml; charset=%s" % self._encoding}
|
||||
return client.OK, headers, self._xml_response(xml_answer)
|
||||
return client.OK, headers, self._xml_response(xml_answer), None
|
||||
|
||||
@@ -109,4 +109,4 @@ class ApplicationPartGet(ApplicationBase):
|
||||
if content_disposition:
|
||||
headers["Content-Disposition"] = content_disposition
|
||||
answer = item.serialize()
|
||||
return client.OK, headers, answer
|
||||
return client.OK, headers, answer, None
|
||||
|
||||
@@ -89,4 +89,4 @@ class ApplicationPartMkcalendar(ApplicationBase):
|
||||
logger.warning(
|
||||
"Bad MKCALENDAR request on %r: %s", path, e, exc_info=True)
|
||||
return httputils.BAD_REQUEST
|
||||
return client.CREATED, {}, None
|
||||
return client.CREATED, {}, None, xmlutils.pretty_xml(xml_content)
|
||||
|
||||
@@ -94,4 +94,4 @@ class ApplicationPartMkcol(ApplicationBase):
|
||||
"Bad MKCOL request on %r (type:%s): %s", path, collection_type, e, exc_info=True)
|
||||
return httputils.BAD_REQUEST
|
||||
logger.info("MKCOL request %r (type:%s): %s", path, collection_type, "successful")
|
||||
return client.CREATED, {}, None
|
||||
return client.CREATED, {}, None, xmlutils.pretty_xml(xml_content)
|
||||
|
||||
@@ -127,4 +127,4 @@ class ApplicationPartMove(ApplicationBase):
|
||||
logger.warning(
|
||||
"Bad MOVE request on %r: %s", path, e, exc_info=True)
|
||||
return httputils.BAD_REQUEST
|
||||
return client.NO_CONTENT if to_item else client.CREATED, {}, None
|
||||
return client.NO_CONTENT if to_item else client.CREATED, {}, None, None
|
||||
|
||||
@@ -33,4 +33,4 @@ class ApplicationPartOptions(ApplicationBase):
|
||||
"Allow": ", ".join(
|
||||
name[3:] for name in dir(self) if name.startswith("do_")),
|
||||
"DAV": httputils.DAV_HEADERS}
|
||||
return client.OK, headers, None
|
||||
return client.OK, headers, None, None
|
||||
|
||||
@@ -410,4 +410,4 @@ class ApplicationPartPropfind(ApplicationBase):
|
||||
allowed_items, user, self._encoding)
|
||||
if xml_answer is None:
|
||||
return httputils.NOT_ALLOWED
|
||||
return client.MULTI_STATUS, headers, self._xml_response(xml_answer)
|
||||
return client.MULTI_STATUS, headers, self._xml_response(xml_answer), xmlutils.pretty_xml(xml_content)
|
||||
|
||||
@@ -131,4 +131,4 @@ class ApplicationPartProppatch(ApplicationBase):
|
||||
logger.warning(
|
||||
"Bad PROPPATCH request on %r: %s", path, e, exc_info=True)
|
||||
return httputils.BAD_REQUEST
|
||||
return client.MULTI_STATUS, headers, self._xml_response(xml_answer)
|
||||
return client.MULTI_STATUS, headers, self._xml_response(xml_answer), xmlutils.pretty_xml(xml_content)
|
||||
|
||||
@@ -334,7 +334,7 @@ class ApplicationPartPut(ApplicationBase):
|
||||
if (item and item.uid == prepared_item.uid):
|
||||
logger.debug("PUT request updated existing item %r", path)
|
||||
headers = {"ETag": etag}
|
||||
return client.NO_CONTENT, headers, None
|
||||
return client.NO_CONTENT, headers, None, None
|
||||
|
||||
headers = {"ETag": etag}
|
||||
return client.CREATED, headers, None
|
||||
return client.CREATED, headers, None, None
|
||||
|
||||
@@ -815,7 +815,7 @@ class ApplicationPartReport(ApplicationBase):
|
||||
"Bad REPORT request on %r: %s", path, e, exc_info=True)
|
||||
return httputils.BAD_REQUEST
|
||||
headers = {"Content-Type": "text/calendar; charset=%s" % self._encoding}
|
||||
return status, headers, str(body)
|
||||
return status, headers, str(body), xmlutils.pretty_xml(xml_content)
|
||||
else:
|
||||
try:
|
||||
status, xml_answer = xml_report(
|
||||
@@ -826,4 +826,4 @@ class ApplicationPartReport(ApplicationBase):
|
||||
"Bad REPORT request on %r: %s", path, e, exc_info=True)
|
||||
return httputils.BAD_REQUEST
|
||||
headers = {"Content-Type": "text/xml; charset=%s" % self._encoding}
|
||||
return status, headers, self._xml_response(xml_answer)
|
||||
return status, headers, self._xml_response(xml_answer), xmlutils.pretty_xml(xml_content)
|
||||
|
||||
Reference in New Issue
Block a user