Skip to content

Commit

Permalink
refactor(circuits.web.utils): replace gzip compress() with httoop
Browse files Browse the repository at this point in the history
  • Loading branch information
spaceone committed Dec 31, 2022
1 parent 71e1f3a commit f8762a5
Showing 1 changed file with 19 additions and 30 deletions.
49 changes: 19 additions & 30 deletions circuits/web/utils.py
Expand Up @@ -4,9 +4,6 @@
"""
import os
import stat
import struct
import time
import zlib

import httoop

Expand All @@ -25,33 +22,25 @@ def is_unix_socket(path):
def compress(body, compress_level):
"""Compress 'body' at the given compress_level."""

# Header
yield b"\037\213\010\0" \
+ struct.pack("<L", int(time.time())) \
+ b"\002\377"

size = 0
crc = zlib.crc32(b"")

zobj = zlib.compressobj(
compress_level,
zlib.DEFLATED,
-zlib.MAX_WBITS,
zlib.DEF_MEM_LEVEL,
0,
)

for chunk in body:
if not isinstance(chunk, bytes):
chunk = chunk.encode("utf-8")

size += len(chunk)
crc = zlib.crc32(chunk, crc)
yield zobj.compress(chunk)

yield zobj.flush() \
+ struct.pack("<l", crc) \
+ struct.pack("<L", size & 0xFFFFFFFF)
from httoop.codecs.application.gzip import GZip

class Gzip(GZip):
compression_level = compress_level

def iterator(body):
if isinstance(body, type(u'')):
body = body.encode('utf-8')
if isinstance(body, bytes):
yield body
return
for chunk in body:
if not isinstance(chunk, bytes):
chunk = chunk.encode("utf-8")
if chunk:
yield chunk

for output in Gzip.iterencode(iterator(body)):
yield output


def get_ranges(headervalue, content_length):
Expand Down

0 comments on commit f8762a5

Please sign in to comment.