Add user agent logging
This commit is contained in:
parent
2ced36a6f0
commit
b5cdd379cd
21
neomi.py
21
neomi.py
|
@ -297,6 +297,17 @@ def get_request(sockreader, *, config):
|
||||||
|
|
||||||
rest = selector
|
rest = selector
|
||||||
|
|
||||||
|
# Try to extract user agent
|
||||||
|
useragent = None
|
||||||
|
for line in request.split(b'\n'):
|
||||||
|
ua_string = b'user-agent:'
|
||||||
|
if len(line) >= len(ua_string) and line.lower()[:len(ua_string)] == ua_string:
|
||||||
|
try:
|
||||||
|
useragent = line[len(ua_string):].decode('utf-8')
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
useragent = line[len(ua_string):].decode('latin-1')
|
||||||
|
useragent = useragent.strip()
|
||||||
|
|
||||||
elif protocol == Protocol.gopher:
|
elif protocol == Protocol.gopher:
|
||||||
rest = None
|
rest = None
|
||||||
|
|
||||||
|
@ -324,12 +335,15 @@ def get_request(sockreader, *, config):
|
||||||
protocol = Protocol.gopherplus
|
protocol = Protocol.gopherplus
|
||||||
rest = field
|
rest = field
|
||||||
|
|
||||||
|
# No useragents in gopher
|
||||||
|
useragent = None
|
||||||
|
|
||||||
else:
|
else:
|
||||||
unreachable()
|
unreachable()
|
||||||
|
|
||||||
path = normalize_path(path, config = config)
|
path = normalize_path(path, config = config)
|
||||||
|
|
||||||
return path, protocol, rest
|
return path, protocol, useragent, rest
|
||||||
|
|
||||||
infofiles_cached = set()
|
infofiles_cached = set()
|
||||||
infofiles_cached_lock = threading.Lock()
|
infofiles_cached_lock = threading.Lock()
|
||||||
|
@ -549,7 +563,7 @@ class Serve(threading.Thread):
|
||||||
def handle_request(self):
|
def handle_request(self):
|
||||||
sockreader = SocketReader(self.sock)
|
sockreader = SocketReader(self.sock)
|
||||||
|
|
||||||
path, protocol, rest = get_request(sockreader, config = self.config)
|
path, protocol, useragent, rest = get_request(sockreader, config = self.config)
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
full_path = get_full_path(path, config = self.config)
|
full_path = get_full_path(path, config = self.config)
|
||||||
|
@ -577,6 +591,9 @@ class Serve(threading.Thread):
|
||||||
send_file(self.sock, reader, protocol, 'text/plain', config = self.config)
|
send_file(self.sock, reader, protocol, 'text/plain', config = self.config)
|
||||||
raise err
|
raise err
|
||||||
|
|
||||||
|
if useragent is not None:
|
||||||
|
log('User agent: %s' % useragent)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
global threads_amount, threads_lock
|
global threads_amount, threads_lock
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue