The little things give you away... A collection of various small helper stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

429 lines
15 KiB

  1. #!/usr/bin/env python3
  2. # Tiny tool for WARC stuff.
  3. # Operating modes:
  4. # warc-tiny colour FILES -- coloured output of the WARCs for easier reading
  5. # warc-tiny dump-responses [-m|--meta] FILES -- dump the HTTP response bodies to stdout
  6. # With --meta, prefix every line with the filename, record offset, record ID, and target URI; e.g. 'file.warc.gz:123:<urn:uuid:41b76f1f-f946-4723-91f8-cee6491e92f3>:<https://example.org/>: foobar'
  7. # The record offset may be -1 if it is not known.
  8. # The filename is wrapped in angled brackets if it contains a colon; the target URI is always wrapped in angled brackets (since it virtually always contains a colon).
  9. # warc-tiny verify FILES -- verify the integrity of a WARC by comparing the digests
  10. import base64
  11. import gzip
  12. import hashlib
  13. import sys
  14. import zlib
  15. def GzipDecompressor():
  16. return zlib.decompressobj(16 + zlib.MAX_WBITS)
  17. class DummyDecompressor:
  18. def decompress(self, data):
  19. return data
  20. class Event:
  21. pass
  22. class NewFile(Event):
  23. def __init__(self, filename):
  24. self._filename = filename
  25. @property
  26. def filename(self):
  27. return self._filename
  28. class BeginOfRecord(Event):
  29. def __init__(self, warcHeaders, rawData):
  30. self._warcHeaders = warcHeaders
  31. self._rawData = rawData
  32. @property
  33. def warcHeaders(self):
  34. return self._warcHeaders
  35. @property
  36. def rawData(self):
  37. return self._rawData
  38. class _DataChunk(Event):
  39. def __init__(self, data):
  40. self._data = data
  41. @property
  42. def data(self):
  43. return self._data
  44. def __repr__(self):
  45. return '{}({!r}{})'.format(type(self).__name__, self._data[:50], '...' if len(self._data) > 50 else '')
  46. class WARCBlockChunk(_DataChunk):
  47. def __init__(self, data, isHttpHeader = None):
  48. super().__init__(data)
  49. self._isHttpHeader = isHttpHeader
  50. @property
  51. def isHttpHeader(self):
  52. # True: the chunk represents (part of) the HTTP header; False: the chunk represents (part of) the HTTP body; None: the chunk is not part of an HTTP record
  53. return self._isHttpHeader
  54. class RawHTTPBodyChunk(_DataChunk):
  55. '''
  56. Because many tools misunderstood the WARC specifications, the Payload-Digest was often implemented without stripping transfer encoding.
  57. This is like HTTPBodyChunk but without transfer encoding stripping.
  58. '''
  59. class HTTPBodyChunk(_DataChunk):
  60. '''
  61. Representing a part of the HTTP body with transfer encoding stripped.
  62. '''
  63. class EndOfRecord(Event):
  64. pass
  65. def iter_warc(f):
  66. # Yields Events
  67. # BeginOfRecord's rawData does not include the CRLF CRLF at the end of the headers, and WARCBlockChunk does not contain the CRLF CRLF after the block either.
  68. with gzip.open(f, 'rb') as fp:
  69. buf = b''
  70. while True:
  71. # Read WARC header
  72. while b'\r\n\r\n' not in buf:
  73. try:
  74. buf = buf + fp.read(4096)
  75. except EOFError:
  76. break
  77. if not buf:
  78. break
  79. if not buf:
  80. break
  81. warcHeaderBuf, buf = buf.split(b'\r\n\r\n', 1)
  82. assert warcHeaderBuf.startswith(b'WARC/1.0\r\n') or warcHeaderBuf.startswith(b'WARC/1.1\r\n')
  83. assert b'\r\nContent-Length:' in warcHeaderBuf
  84. warcHeaders = tuple(tuple(map(bytes.strip, x.split(b':', 1))) for x in warcHeaderBuf.split(b'\r\n'))
  85. warcContentType = next(x[1] for x in warcHeaders if x[0] == b'Content-Type')
  86. warcContentLength = int(next(x[1] for x in warcHeaders if x[0] == b'Content-Length'))
  87. warcType = next(x[1] for x in warcHeaders if x[0] == b'WARC-Type')
  88. yield BeginOfRecord(warcHeaders, warcHeaderBuf)
  89. recordID = next(x[1] for x in warcHeaders if x[0] == b'WARC-Record-ID')
  90. # Read WARC block (and skip CRLFCRLF at the end of the record)
  91. if len(buf) < warcContentLength + 4:
  92. try:
  93. buf = buf + fp.read(warcContentLength + 4 - len(buf))
  94. except EOFError:
  95. pass
  96. if len(buf) < warcContentLength + 4:
  97. print('Error: truncated WARC', file = sys.stderr)
  98. break
  99. warcContent = buf[:warcContentLength]
  100. buf = buf[warcContentLength + 4:]
  101. # Decode HTTP body if appropriate
  102. if warcContentType in (b'application/http;msgtype=request', b'application/http; msgtype=request') and warcType == b'request':
  103. httpType = 'request'
  104. elif warcContentType in (b'application/http;msgtype=response', b'application/http; msgtype=response') and warcType == b'response':
  105. httpType = 'response'
  106. else:
  107. httpType = None
  108. if httpType is not None:
  109. if b'\r\n\r\n' in warcContent:
  110. httpHeaders, httpBody = warcContent.split(b'\r\n\r\n', 1)
  111. # Parse headers and extract transfer encoding
  112. httpHeaderLines = [tuple(map(bytes.strip, x.split(b':', 1))) for x in httpHeaders.split(b'\r\n')]
  113. chunked = False
  114. gzipped = False
  115. if b'\r\ntransfer-encoding' in httpHeaders.lower():
  116. transferEncoding = next(x[1] for x in httpHeaderLines if x[0].lower() == b'transfer-encoding')
  117. transferEncodings = set(map(bytes.strip, transferEncoding.split(b',')))
  118. chunked = b'chunked' in transferEncodings
  119. gzipped = b'gzip' in transferEncodings
  120. yield WARCBlockChunk(httpHeaders + b'\r\n\r\n', isHttpHeader = True)
  121. yield WARCBlockChunk(httpBody, isHttpHeader = False)
  122. yield RawHTTPBodyChunk(httpBody)
  123. # Decode body
  124. if gzipped:
  125. httpDecompressor = GzipDecompressor()
  126. else:
  127. httpDecompressor = DummyDecompressor()
  128. if chunked:
  129. while True:
  130. try:
  131. chunkLineEnd = httpBody.index(b'\r\n')
  132. except ValueError:
  133. print('Error: could not find chunk line end in record {}, skipping'.format(recordID), file = sys.stderr)
  134. break
  135. chunkLine = httpBody[:chunkLineEnd]
  136. if b';' in chunkLine:
  137. chunkLength = chunkLine[:chunkLine.index(b';')].strip()
  138. else:
  139. chunkLength = chunkLine.strip()
  140. if chunkLength.lstrip(b'0123456789abcdefABCDEF') != b'':
  141. print('Error: malformed chunk length {!r} in record {}, skipping'.format(chunkLength, recordID), file = sys.stderr)
  142. break
  143. chunkLength = int(chunkLength, base = 16)
  144. if chunkLength == 0:
  145. break
  146. chunk = httpDecompressor.decompress(httpBody[chunkLineEnd + 2 : chunkLineEnd + 2 + chunkLength])
  147. yield HTTPBodyChunk(chunk)
  148. httpBody = httpBody[chunkLineEnd + 2 + chunkLength + 2:]
  149. else:
  150. yield HTTPBodyChunk(httpDecompressor.decompress(httpBody))
  151. else:
  152. print('Warning: malformed HTTP request or response in record {}, skipping'.format(recordID), file = sys.stderr)
  153. yield WARCBlockChunk(warcContent)
  154. else:
  155. yield WARCBlockChunk(warcContent)
  156. yield EndOfRecord()
  157. class ProcessMode:
  158. @classmethod
  159. def split_args(cls, args):
  160. '''Split args into arguments to be passed into __init__ and filenames'''
  161. return (), args
  162. def process_event(self, event):
  163. raise NotImplementedError
  164. class Digest:
  165. def __init__(self, digest):
  166. self._digest = digest
  167. def format(self, digest = None):
  168. raise NotImplementedError
  169. def equals(self, digest):
  170. return self._digest == digest
  171. class Base32Digest(Digest):
  172. def format(self, digest = None):
  173. return base64.b32encode(digest if digest else self._digest)
  174. class HexDigest(Digest):
  175. def format(self, digest = None):
  176. return (digest if digest else self._digest).hex()
  177. class VerifyMode(ProcessMode):
  178. def __init__(self):
  179. self._blockDigester = None
  180. self._recordedBlockDigest = None
  181. self._payloadDigester = None
  182. self._brokenPayloadDigester = None
  183. self._recordedPayloadDigest = None
  184. self._printedBrokenPayloadWarning = False
  185. def parse_digest(self, digest):
  186. if not digest.startswith(b'sha1:'):
  187. print('Warning: don\'t understand hash format: {!r}'.format(digest), file = sys.stderr)
  188. return None
  189. if len(digest) == 37 and digest.rstrip(b'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567') == b'sha1:': # 5 for 'sha1:' + 32 for base-32 hash
  190. return Base32Digest(base64.b32decode(digest[5:]))
  191. if len(digest) == 45 and digest.rstrip(b'0123456789abcdef') == b'sha1:':
  192. return HexDigest(bytes.fromhex(digest[5:].decode('ascii')))
  193. return None
  194. def process_event(self, event):
  195. if type(event) is NewFile:
  196. self._printedBrokenPayloadWarning = False
  197. elif type(event) is BeginOfRecord:
  198. if any(x[0] == b'WARC-Block-Digest' for x in event.warcHeaders):
  199. self._blockDigester = hashlib.sha1()
  200. self._recordedBlockDigest = self.parse_digest(next(x[1] for x in event.warcHeaders if x[0] == b'WARC-Block-Digest'))
  201. else:
  202. self._blockDigester = None
  203. self._recordedBlockDigest = None
  204. if any(x[0] == b'WARC-Payload-Digest' for x in event.warcHeaders):
  205. self._payloadDigester = hashlib.sha1()
  206. self._brokenPayloadDigester = hashlib.sha1()
  207. self._recordedPayloadDigest = self.parse_digest(next(x[1] for x in event.warcHeaders if x[0] == b'WARC-Payload-Digest'))
  208. else:
  209. self._payloadDigester = None
  210. self._brokenPayloadDigester = None
  211. self._recordedPayloadDigest = None
  212. self._recordID = next(x[1] for x in event.warcHeaders if x[0] == b'WARC-Record-ID')
  213. self._recordType = next(x[1] for x in event.warcHeaders if x[0] == b'WARC-Type')
  214. elif type(event) is WARCBlockChunk:
  215. if self._blockDigester:
  216. self._blockDigester.update(event.data)
  217. elif type(event) is HTTPBodyChunk:
  218. if self._payloadDigester:
  219. self._payloadDigester.update(event.data)
  220. elif type(event) is RawHTTPBodyChunk:
  221. if self._brokenPayloadDigester:
  222. self._brokenPayloadDigester.update(event.data)
  223. elif type(event) is EndOfRecord:
  224. if self._blockDigester and self._recordedBlockDigest:
  225. if not self._recordedBlockDigest.equals(self._blockDigester.digest()):
  226. print('Block digest mismatch for record {}: recorded {} v calculated {}'.format(self._recordID, self._recordedBlockDigest.format(), self._recordedBlockDigest.format(self._blockDigester.digest())), file = sys.stderr)
  227. if self._payloadDigester and self._recordType in (b'request', b'response'): #TODO: Support revisit
  228. if not self._recordedPayloadDigest.equals(self._payloadDigester.digest()):
  229. if self._recordedPayloadDigest.equals(self._brokenPayloadDigester.digest()):
  230. if not self._printedBrokenPayloadWarning:
  231. print('Warning: WARC uses incorrect payload digests without stripping the transfer encoding', file = sys.stderr)
  232. self._printedBrokenPayloadWarning = True
  233. else:
  234. print('Payload digest mismatch for record {}: recorded {} vs. calculated {} (calculated broken {})'.format(self._recordID, self._recordedPayloadDigest.format(), self._recordedPayloadDigest.format(self._payloadDigester.digest()), self._recordedPayloadDigest.format(self._brokenPayloadDigester.digest())), file = sys.stderr)
  235. class DumpResponsesMode(ProcessMode):
  236. @classmethod
  237. def split_args(cls, args):
  238. if args[0] == '-m' or args[0] == '--meta':
  239. return (True,), args[1:]
  240. return (False,), args
  241. def __init__(self, withMeta):
  242. self._printEOR = False
  243. self._isResponse = False
  244. self._withMeta = withMeta
  245. if withMeta:
  246. self._recordID = None
  247. self._targetURI = None
  248. self._buffer = b''
  249. def _write(self, data):
  250. if not self._withMeta:
  251. sys.stdout.buffer.write(data)
  252. return
  253. buf = self._buffer + data
  254. lines = buf.split(b'\n')
  255. self._buffer = lines.pop() # Since there's an explicit `_write(b'\r\n')` at the end of the record, this implicitly resets the buffer as well
  256. for line in lines:
  257. sys.stdout.buffer.write(':'.join((self._filename, '-1', self._recordID, '<' + self._targetURI + '>', '')).encode('utf-8'))
  258. sys.stdout.buffer.write(line)
  259. sys.stdout.buffer.write(b'\n')
  260. def process_event(self, event):
  261. if type(event) is NewFile:
  262. self._filename = event.filename
  263. if ':' in self._filename:
  264. self._filename = '<' + self._filename + '>'
  265. elif type(event) is BeginOfRecord:
  266. warcContentType = next(x[1] for x in event.warcHeaders if x[0] == b'Content-Type')
  267. warcType = next(x[1] for x in event.warcHeaders if x[0] == b'WARC-Type')
  268. self._isResponse = warcContentType in (b'application/http;msgtype=response', b'application/http; msgtype=response') and warcType == b'response'
  269. self._printEOR = False
  270. if self._withMeta:
  271. # Both of these are URIs, and per RFC 3986, those can only contain ASCII characters.
  272. self._recordID = next(x[1] for x in event.warcHeaders if x[0] == b'WARC-Record-ID').decode('ascii')
  273. self._targetURI = next((x[1] for x in event.warcHeaders if x[0] == b'WARC-Target-URI'), b'').decode('ascii')
  274. self._buffer = b''
  275. elif type(event) is HTTPBodyChunk:
  276. if self._isResponse:
  277. self._printEOR = True
  278. self._write(event.data)
  279. elif type(event) is EndOfRecord:
  280. if self._printEOR:
  281. self._write(b'\r\n')
  282. class COLOURS:
  283. RESET = b'\x1b[0m'
  284. GREEN = b'\x1b[0;32m'
  285. LIGHTGREEN = b'\x1b[1;32m'
  286. PURPLE = b'\x1b[0;35m'
  287. LIGHTPURPLE = b'\x1b[1;35m'
  288. RED = b'\x1b[0;31m'
  289. INVERTED = b'\x1b[7m'
  290. class ColourMode(ProcessMode):
  291. def __init__(self):
  292. self._hadHttpStatusLine = False
  293. def _replace_esc(self, data):
  294. return data.replace(b'\x1b', COLOURS.INVERTED + b'ESC' + COLOURS.RESET)
  295. def _print_line(self, line, colour, withLF = True, colourOnlyBeforeColon = False):
  296. if colourOnlyBeforeColon:
  297. if b':' in line:
  298. offset = line.index(b':')
  299. else:
  300. offset = 0
  301. else:
  302. offset = len(line)
  303. if offset > 0:
  304. sys.stdout.buffer.write(colour)
  305. sys.stdout.buffer.write(self._replace_esc(line[:offset]))
  306. sys.stdout.buffer.write(COLOURS.RESET)
  307. sys.stdout.buffer.write(line[offset:])
  308. if withLF:
  309. sys.stdout.buffer.write(b'\n')
  310. def _print_data(self, data, colour, colourOnlyBeforeColon):
  311. later = False
  312. for line in data.split(b'\r\n'):
  313. if later:
  314. sys.stdout.buffer.write(b'\n')
  315. self._print_line(line, colour, withLF = False, colourOnlyBeforeColon = colourOnlyBeforeColon)
  316. later = True
  317. def process_event(self, event):
  318. if type(event) is BeginOfRecord:
  319. firstNewline = event.rawData.index(b'\r\n')
  320. self._print_line(event.rawData[:firstNewline], COLOURS.LIGHTGREEN)
  321. self._print_data(event.rawData[firstNewline + 2:], COLOURS.GREEN, True)
  322. sys.stdout.buffer.write(b'\n\n') # separator between header and block
  323. self._hadHttpStatusLine = False
  324. elif type(event) is WARCBlockChunk:
  325. if event.isHttpHeader is True:
  326. if not self._hadHttpStatusLine:
  327. firstNewline = event.data.index(b'\r\n')
  328. self._print_line(event.data[:firstNewline], COLOURS.LIGHTPURPLE)
  329. offset = firstNewline + 2
  330. self._hadHttpStatusLine = True
  331. else:
  332. offset = 0
  333. self._print_data(event.data[offset:], COLOURS.PURPLE, True)
  334. elif event.isHttpHeader is False:
  335. self._print_data(event.data, COLOURS.RED, False)
  336. elif event.isHttpHeader is None:
  337. sys.stdout.buffer.write(self._replace_esc(event.data))
  338. elif type(event) is EndOfRecord:
  339. sys.stdout.buffer.write(b'\n\n')
  340. def main():
  341. processorMap = {'verify': VerifyMode, 'dump-responses': DumpResponsesMode, 'colour': ColourMode}
  342. assert len(sys.argv) - 1 >= 2
  343. mode = sys.argv[1]
  344. assert mode in processorMap
  345. processorArgs, files = processorMap[mode].split_args(sys.argv[2:])
  346. assert files
  347. processor = processorMap[mode](*processorArgs)
  348. try:
  349. for f in files:
  350. print('Info: processing {}'.format(f), file = sys.stderr)
  351. processor.process_event(NewFile(f))
  352. for event in iter_warc(f):
  353. processor.process_event(event)
  354. except BrokenPipeError:
  355. return
  356. if __name__ == '__main__':
  357. main()