Ejemplo n.º 1
0
def timedLogFormatter(timestamp, request):
    duration = -1
    if hasattr(request, 'start_time'):
        duration = timedelta_to_milliseconds(datetime.now() - request.start_time)

    return (u'%(code)s %(method)s %(uri)s %(length)dB %(duration)dms' % dict(
              duration=duration,
              method=_escape(request.method),
              uri=_escape(request.uri),
              proto=_escape(request.clientproto),
              code=request.code,
              length=request.sentLength))
Ejemplo n.º 2
0
def combinedLogFormatter(timestamp, request):
    """
    @return: A combined log formatted log line for the given request.

    @see: L{IAccessLogFormatter}
    """
    referrer = _escape(request.getHeader(b"referer") or b"-")
    agent = _escape(request.getHeader(b"user-agent") or b"-")

    if hasattr(request, "user") and request.user is not None:
        username = request.user.shortNames[0]
        try:
            username = _escape(username)
        except Exception:
            username = _escape(repr(username))
    else:
        username = u"-"

    line = (
        u'"%(ip)s" %(user)s - %(timestamp)s "%(method)s %(uri)s %(protocol)s" '
        u'%(code)d %(length)s "%(referrer)s" "%(agent)s"' % dict(
            ip=_escape(request.getClientIP() or b"-"),
            timestamp=timestamp,
            method=_escape(request.method),
            uri=_escape(request.uri),
            protocol=_escape(request.clientproto),
            code=request.code,
            length=request.sentLength or u"-",
            referrer=referrer,
            agent=agent,
            user=username,
        ))
    return line
Ejemplo n.º 3
0
def timedLogFormatter(timestamp, request):
    duration = -1
    if hasattr(request, 'start_time'):
        duration = round((datetime.now() - request.start_time).microseconds / 1000, 4)

    line = (u'%(code)s %(method)s %(uri)s %(length)s %(duration)dms' % dict(
              duration=duration,
              method=_escape(request.method),
              uri=_escape(request.uri),
              proto=_escape(request.clientproto),
              code=request.code,
              length=request.sentLength or u"-"))

    return line
Ejemplo n.º 4
0
def combinedLogFormatter(timestamp, request):
    """
    @return: A combined log formatted log line for the given request.

    @see: L{IAccessLogFormatter}
    """
    referrer = _escape(request.getHeader(b"referer") or b"-")
    agent = _escape(request.getHeader(b"user-agent") or b"-")

    if hasattr(request, b"user") and request.user:
        user = _escape(request.user)
    else:
        user = b"-"

    line = (
        u'"%(ip)s" %(user)s - %(timestamp)s "%(method)s %(uri)s %(protocol)s" '
        u'%(code)d %(length)s "%(referrer)s" "%(agent)s"' % dict(
            ip=_escape(request.getClientIP() or b"-"),
            timestamp=timestamp,
            method=_escape(request.method),
            uri=_escape(request.uri),
            protocol=_escape(request.clientproto),
            code=request.code,
            length=request.sentLength or u"-",
            referrer=referrer,
            agent=agent,
            user=user,
        )
    )
    return line
Ejemplo n.º 5
0
def debugLogFormatter(timestamp, request):
    """
    @return: A combined log formatted log line for the given request.

    @see: L{IAccessLogFormatter}
    """
    referrer = _escape(request.getHeader(b"referer") or b"-")
    agent = _escape(request.getHeader(b"user-agent") or b"-")
    line = (u'"%(ip)s" - - %(timestamp)s "%(method)s %(uri)s %(protocol)s" '
            u'%(code)d %(length)s "%(referrer)s" "%(agent)s"' % dict(
                ip=_escape(request.getClientIP() or b"-"),
                timestamp=timestamp,
                method=_escape(request.method),
                uri=_escape(request.uri),
                protocol=_escape(request.clientproto),
                code=request.code,
                length=request.sentLength or u"-",
                referrer=referrer,
                agent=agent,
            ))
    additional_data = request.getHeader(b"x-portia")
    if additional_data:
        split_data = _escape(additional_data).split(':')
        if len(split_data) == 4:
            session, session_time, user, command = split_data
            line += (u' id=%(session)s t=%(session_time)s user=%(user)s '
                     u'command=%(command)s' % dict(session=session,
                                                   session_time=session_time,
                                                   user=user,
                                                   command=command))
    return line
Ejemplo n.º 6
0
    def log(self, request):
        host = http._escape(request.getRequestHostname() or "-")
        referrer = http._escape(request.getHeader(b"referer") or "-")
        agent = http._escape(request.getHeader(b"user-agent") or "-")
        line = '"{ip:s}" - {scheme:s}://{host:s}:{port:d} {timestamp:s} "{method:s} {uri:s} {protocol:s}" {code:d} {length:s} "{referrer:s}" "{agent:s}"'
        line = line.format(
            ip=http._escape(request.getClientIP() or "-"),
            scheme="https" if request.isSecure() else "http",
            host=host,
            port=request.getHost().port,
            timestamp=self._logDateTime,
            method=http._escape(request.method),
            uri=http._escape(request.uri),
            protocol=http._escape(request.clientproto),
            code=request.code,
            length=str(request.sentLength) or "-",
            referrer=referrer,
            agent=agent,
        )
        logger.info(line)

        if request.content is not None:
            try:
                request.content.seek(0, 0)
                content = request.content.read()
                if len(content):
                    logger.info("Content: {}".format(content.decode("UTF-8")))
            except:
                pass
Ejemplo n.º 7
0
def timedLogFormatter(timestamp, request):
    """
    A custom request formatter.  This is whats used when each request line is formatted.

    :param timestamp: 
    :param request: A twisted.web.server.Request instance
    """

    agent = _escape(request.getHeader(b"user-agent") or b"-")

    if hasattr(request, "started"):
        duration = round(time.time() - request.started, 4)
    else:
        duration = 0
    line = (
        u'%(duration)ss "%(ip)s" "%(method)s %(uri)s %(protocol)s %(args)s" '
        u'%(code)d %(length)s"' % dict(
            ip=_escape(request.getClientIP() or b"-"),
            duration=duration,
            method=_escape(request.method),
            uri=_escape(request.uri),
            args=_escape(str(request.args)),
            protocol=_escape(request.clientproto),
            code=request.code,
            length=request.sentLength or u"-",
        ))
    return line
Ejemplo n.º 8
0
def logFormatter(timestamp, request):
    duration = -1

    if hasattr(request, 'start_time'):
        duration = timedelta_to_milliseconds(datetime.now() - request.start_time)

    client_ip = '[REMOVED_IP_ADDRESS]'
    client_ua = '[REMOVED_USER_AGENT]'

    if request.log_ip_and_ua:
        client_ip = request.client_ip
        client_ua = request.client_ua

    return (u'%(vhost)s %(ip)s - - %(timestamp)s "%(method)s %(uri)s %(clientproto)s" %(code)s %(length)d %(duration)dms - "%(user_agent)s" %(tid)d' % dict(
            vhost=_escape(request.hostname),
            timestamp=timestamp,
            duration=duration,
            ip=_escape(client_ip),
            method=_escape(request.method),
            uri=_escape(request.uri),
            clientproto=_escape(request.clientproto),
            code=request.code,
            length=request.sentLength,
            user_agent=_escape(client_ua),
            tid=request.tid))
Ejemplo n.º 9
0
def logFormatter(timestamp, request):
    duration = -1

    if hasattr(request, 'start_time'):
        duration = timedelta_to_milliseconds(datetime.now() - request.start_time)

    client_ip = '[REMOVED_IP_ADDRESS]'
    client_ua = '[REMOVED_USER_AGENT]'

    if request.log_ip_and_ua:
        client_ip = request.client_ip
        client_ua = request.client_ua

    return (u'%(ip)s %(tid)s %(code)s %(method)s %(uri)s %(length)d %(duration)dms "%(user_agent)s"' % dict(
            duration=duration,
            ip=_escape(client_ip),
            method=_escape(request.method),
            uri=_escape(request.uri),
            code=request.code,
            length=request.sentLength,
            user_agent=_escape(client_ua),
            tid=request.tid))
Ejemplo n.º 10
0
def timedLogFormatter(timestamp, request):
    duration = -1

    if hasattr(request, 'start_time'):
        duration = timedelta_to_milliseconds(datetime.now() - request.start_time)

    if request.to_be_anonymized:
        client_ip = '[REMOVED_IP_ADDRESS]'
        client_ua = '[REMOVED_USER_AGENT]'
    else:
        client_ip = request.client_ip
        client_ua = request.client_ua

    return (u'%(ip)s %(tid)s %(code)s %(method)s %(uri)s %(length)d %(duration)dms "%(user_agent)s"' % dict(
            duration=duration,
            ip=_escape(client_ip),
            method=_escape(request.method),
            uri=_escape(request.uri),
            code=request.code,
            length=request.sentLength,
            user_agent=_escape(client_ua),
            tid=request.tid))
Ejemplo n.º 11
0
def combinedLogFormatter(timestamp: str, request: IRequest) -> str:
    """
    @return: A combined log formatted log line for the given request.

    @see: L{IAccessLogFormatter}
    """
    referrer = _escape(request.getHeader("referer") or "-")
    agent = _escape(request.getHeader("user-agent") or "-")

    clientIP = request.getClientIP()

    forwardedFor = (
        request.requestHeaders
        .getRawHeaders(b"x-forwarded-for", [b""])[0]
        .split(b",")[0]
        .strip()
    ).decode("charmap")

    if forwardedFor:
        ip = f"{forwardedFor} > {clientIP}"
    else:
        ip = clientIP


    if hasattr(request, "user") and request.user is not None:
        username = request.user.shortNames[0]
        try:
            username = _escape(username)
        except Exception:
            username = _escape(repr(username))
    else:
        username = "******"

    line = (
        '"{ip}" {user} - {timestamp} "{method} {uri} {protocol}" '
        '{code} {length} "{referrer}" "{agent}"'.format(
            ip=_escape(ip or "-"),
            timestamp=timestamp,
            method=_escape(request.method),
            uri=_escape(request.uri),
            protocol=_escape(request.clientproto),
            code=request.code,
            length=request.sentLength or "-",
            referrer=referrer,
            agent=agent,
            user=username,
        )
    )
    return line
Ejemplo n.º 12
0
def combinedLogFormatter(timestamp: str, request: IRequest) -> str:
    """
    @return: A combined log formatted log line for the given request.

    @see: L{IAccessLogFormatter}
    """
    referrer = _escape(request.getHeader("referer") or "-")
    agent = _escape(request.getHeader("user-agent") or "-")

    clientIP = request.getClientIP()

    forwardedFor = (
        request.requestHeaders.getRawHeaders(b"x-forwarded-for", [b""])[0]
        .split(b",")[0]
        .strip()
    ).decode("charmap")

    if forwardedFor:
        ip = f"{forwardedFor} > {clientIP}"
    else:
        ip = clientIP

    if hasattr(request, "user") and request.user is not None:
        username = request.user.shortNames[0]
        try:
            username = _escape(username)
        except Exception:
            username = _escape(repr(username))
    else:
        username = "******"

    line = (
        '"{ip}" {user} - {timestamp} "{method} {uri} {protocol}" '
        '{code} {length} "{referrer}" "{agent}"'.format(
            ip=_escape(ip or "-"),
            timestamp=timestamp,
            method=_escape(request.method),
            uri=_escape(request.uri),
            protocol=_escape(request.clientproto),
            code=request.code,
            length=request.sentLength or "-",
            referrer=referrer,
            agent=agent,
            user=username,
        )
    )
    return line
Ejemplo n.º 13
0
    def _log(result, timestamp, request):
        host = http._escape(request.getRequestHostname() or "-")
        referrer = http._escape(request.getHeader(b"referer") or "-")
        agent = http._escape(request.getHeader(b"user-agent") or "-")

        line = 'HTTP [{timestamp:s}]: "{ip:s}" - {scheme:s}://{host:s}:{port:d} "{method:s} {uri:s} {protocol:s}" {code:d} {length:s} "{referrer:s}" "{agent:s}"'
        line = line.format(
            ip=http._escape(request.getClientIP() or "-"),
            scheme="https" if request.isSecure() else "http",
            host=host,
            port=request.getHost().port,
            timestamp=timestamp,
            method=http._escape(request.method),
            uri=http._escape(request.uri),
            protocol=http._escape(request.clientproto),
            code=request.code,
            length=str(request.sentLength) or "-",
            referrer=referrer,
            agent=agent,
        )
        logger.info(line)
Ejemplo n.º 14
0
    def timedLogFormatter(cls, timestamp, request):
        from twisted.web.http import _escape

        referrer = _escape(request.getHeader("referer") or "-")
        agent = _escape(request.getHeader("user-agent") or "-")
        tc = round(time.time() - request.started, 4)
        line = u'%(fmt)s | "%(ip)s" %(tc)ss %(code)d %(length)s "%(method)s %(uri)s %(proto)s" "%(agent)s" "%(ref)s"' % {
            'fmt': Time.current_time('%m-%d %H:%M:%S.%f'),
            'ip': _escape(request.getClientIP() or "-"),
            'tc': tc,
            'method': _escape(request.method),
            'uri': _escape(request.uri),
            'proto': _escape(request.clientproto),
            'code': request.code,
            'length': request.sentLength or "-",
            'agent': agent,
            'ref': referrer,
        }
        return line
Ejemplo n.º 15
0
 def log_formatter(self, timestamp, request):
     if request.code < self.log_min_http_code:
         return None
     # duration = round(time.time() - request.started, 4)
     duration = 0.0
     referrer = _escape(request.getHeader(b"referer") or b"-")
     agent = _escape(request.getHeader(b"user-agent") or b"-")
     line = (
         u'"%(ip)s" - %(duration)ss - %(timestamp)s "%(method)s %(uri)s %(protocol)s" '
         u'%(code)d %(length)s "%(referrer)s" "%(agent)s"' % dict(
             ip=_escape(request.getClientIP() or b"-"),
             timestamp=timestamp,
             duration=duration,
             method=_escape(request.method),
             uri=_escape(request.uri),
             protocol=_escape(request.clientproto),
             code=request.code,
             length=request.sentLength or u"-",
             referrer=referrer,
             agent=agent,
         ))
     return line
Ejemplo n.º 16
0
def debugLogFormatter(timestamp, request):
    """
    @return: A combined log formatted log line for the given request.

    @see: L{IAccessLogFormatter}
    """
    referrer = _escape(request.getHeader(b"referer") or b"-")
    agent = _escape(request.getHeader(b"user-agent") or b"-")
    line = (
        u'"%(ip)s" - - %(timestamp)s "%(method)s %(uri)s %(protocol)s" '
        u'%(code)d %(length)s "%(referrer)s" "%(agent)s"' % dict(
            ip=_escape(request.getClientIP() or b"-"),
            timestamp=timestamp,
            method=_escape(request.method),
            uri=_escape(request.uri),
            protocol=_escape(request.clientproto),
            code=request.code,
            length=request.sentLength or u"-",
            referrer=referrer,
            agent=agent,
        )
    )
    return line
Ejemplo n.º 17
0
def proxiedLogFormatter(timestamp, request):
    """
    @return: A combined log formatted log line for the given request but use
        the value of the I{X-Forwarded-For} header as the value for the client
        IP address.

    @see: L{IAccessLogFormatter}
    """

    line = (
        u'"%(ip)s" - "%(method)s %(uri)s %(protocol)s" %(code)d %(length)s "%(referrer)s" "%(agent)s"'
        % dict(
            ip=_escape(request.getClientIP() or b"-"),
            timestamp=timestamp,
            method=_escape(request.method),
            uri=_escape(request.uri),
            protocol=_escape(request.clientproto),
            code=request.code,
            length=request.sentLength or u"-",
            referrer=_escape(request.getHeader(b"referer") or b"-"),
            agent=_escape(request.getHeader(b"user-agent") or b"-"),
        ))
    return line
Ejemplo n.º 18
0
 def _escape(self, s):
     if hasattr(http, '_escape'):
         return http._escape(s)
     return server.Site._escape(self, s)
Ejemplo n.º 19
0
 def _escape(self, s):
     if hasattr(http, '_escape'):
         return http._escape(s)
     return server.Site._escape(self, s)