mirror of
https://github.com/vee1e/mitmproxy.git
synced 2026-09-03 11:17:24 +00:00
106 lines
2.4 KiB
Python
106 lines
2.4 KiB
Python
from __future__ import absolute_import, print_function, division
|
|
|
|
import datetime
|
|
import json
|
|
import time
|
|
|
|
import netlib.utils
|
|
|
|
|
|
def timestamp():
|
|
"""
|
|
Returns a serializable UTC timestamp.
|
|
"""
|
|
return time.time()
|
|
|
|
|
|
def format_timestamp(s):
|
|
s = time.localtime(s)
|
|
d = datetime.datetime.fromtimestamp(time.mktime(s))
|
|
return d.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
|
|
def format_timestamp_with_milli(s):
|
|
d = datetime.datetime.fromtimestamp(s)
|
|
return d.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
|
|
|
|
|
|
def isBin(s):
|
|
"""
|
|
Does this string have any non-ASCII characters?
|
|
"""
|
|
for i in s:
|
|
i = ord(i)
|
|
if i < 9 or 13 < i < 32 or 126 < i:
|
|
return True
|
|
return False
|
|
|
|
|
|
def isMostlyBin(s):
|
|
s = s[:100]
|
|
return sum(isBin(ch) for ch in s) / len(s) > 0.3
|
|
|
|
|
|
def isXML(s):
|
|
for i in s:
|
|
if i in "\n \t":
|
|
continue
|
|
elif i == "<":
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
|
|
def pretty_json(s):
|
|
try:
|
|
p = json.loads(s)
|
|
except ValueError:
|
|
return None
|
|
return json.dumps(p, sort_keys=True, indent=4)
|
|
|
|
|
|
pkg_data = netlib.utils.Data(__name__)
|
|
|
|
|
|
class LRUCache:
|
|
|
|
"""
|
|
A simple LRU cache for generated values.
|
|
"""
|
|
|
|
def __init__(self, size=100):
|
|
self.size = size
|
|
self.cache = {}
|
|
self.cacheList = []
|
|
|
|
def get(self, gen, *args):
|
|
"""
|
|
gen: A (presumably expensive) generator function. The identity of
|
|
gen is NOT taken into account by the cache.
|
|
*args: A list of immutable arguments, used to establish identiy by
|
|
*the cache, and passed to gen to generate values.
|
|
"""
|
|
if args in self.cache:
|
|
self.cacheList.remove(args)
|
|
self.cacheList.insert(0, args)
|
|
return self.cache[args]
|
|
else:
|
|
ret = gen(*args)
|
|
self.cacheList.insert(0, args)
|
|
self.cache[args] = ret
|
|
if len(self.cacheList) > self.size:
|
|
d = self.cacheList.pop()
|
|
self.cache.pop(d)
|
|
return ret
|
|
|
|
|
|
def clean_hanging_newline(t):
|
|
"""
|
|
Many editors will silently add a newline to the final line of a
|
|
document (I'm looking at you, Vim). This function fixes this common
|
|
problem at the risk of removing a hanging newline in the rare cases
|
|
where the user actually intends it.
|
|
"""
|
|
if t and t[-1] == "\n":
|
|
return t[:-1]
|
|
return t
|