Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for gzip Content-Encoding in HTTP requests #10872

Closed
vkuznet opened this issue Oct 15, 2021 · 1 comment
Closed

Implement support for gzip Content-Encoding in HTTP requests #10872

vkuznet opened this issue Oct 15, 2021 · 1 comment

Comments

@vkuznet
Copy link
Contributor

vkuznet commented Oct 15, 2021

Impact of the new feature
Currently, our HTTP clients (WMCore and elsewhere) do not use gzip encoding neither when they send or request data from DBS. Due to increasing size of our data in DBS, e.g. bulkblock API can post 180MB in one call, it is desired to use gzip Content-Encoding which can reduce this size by factor of 25 !!!

Is your feature request related to a problem? Please describe.
Not necessary, but it is desired to add such support to reduce HTTP request time and network bandwidth

Describe the solution you'd like
Here is a fully working example how to achieve gzip encoding with HTTP request with CherryPy server:

#!/usr/bin/env python
#-*- coding: utf-8 -*-
# CherryPy server to support gzip Encoding
"""
File       : cpy_gzip.py
Author     : Valentin Kuznetsov <vkuznet AT gmail dot com>
Description: CherryPy server for POST gzipped requests
"""

# system modules
import os
import sys
import json
import cherrypy

try:
    import cStringIO as StringIO
except ImportError: # python3
    import io
except:
    import StringIO

def decompress(body):
    import gzip

    if  sys.version.startswith('3.'):
        zbuf = io.BytesIO()
    else:
        zbuf = StringIO.StringIO()
    zbuf.write(body)
    zbuf.seek(0)
    zfile = gzip.GzipFile(mode='rb', fileobj=zbuf)
    data = zfile.read()
    zfile.close()
    return data

class Test(object):
    @cherrypy.expose
    @cherrypy.tools.accept(media='application/json')
    def index(self):
        raw = cherrypy.request.body.read(int(cherrypy.request.headers['Content-Length']))
        if cherrypy.request.headers['Content-Encoding'] == 'gzip':
            raw = decompress(raw)
            return raw
        b = json.loads(raw)
        return json.dumps(b)

if __name__ == '__main__':
    cherrypy.quickstart(Test())

If you can run this code it will provide CherryPy server which supports gzip encoding.

Now, the client only needs to add appropriate gzip HTTP header as follows:

curl -H "Content-Encoding: gzip" -H "Content-type: application/json" --data-binary @/Users/vk/Downloads/bb.json.gz
http://localhost:8080

Describe alternatives you've considered
None

Additional context
New dbs2go support this encoding and clients can take advantage of it, i.e. clients can supply Content-Encoding: gzip and results from dbs2go will be gzip'ed and returned to the client. Of course, client should take care how to unzip them and this can be implemented via decompress function shown above.

@amaltaro
Copy link
Contributor

@vkuznet Valentin, we already have such issue opened, see: #10451
I'm going to make a reference to this issue over there and close this one as duplicate. If I'm wrong, please re-open it.
Thanks for creating it though and providing some extra examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants