Compressing HTTP Post Data sent from browser

前端 未结 3 1728
故里飘歌
故里飘歌 2020-11-30 02:24

I want to send a compressed POST data with Javascript to a server I control. Is there any way to let the HTTP layer deal with the compression.

I\'m sending JSON. If

相关标签:
3条回答
  • 2020-11-30 02:46

    Will the browser automatically gzip-encode your data for you? The short answer is no.

    The long answer is that some user-agents can do things like this, but you definitely can't rely on it. The apache mod_deflate docs state:

    some special applications actually do support request compression, for instance some WebDAV clients.

    So, no, that's not going to work. You'll need to generate the appropriate HTTP request message yourself. The appropriate header in this case is Content-Encoding: gzip and NOT Content-Type: because the content itself is application/json, you're just looking to encode the entity body of your HTTP request message for transport.

    Note that you need to also add the appropriate Content-Length: header specifying the size in bytes of the message entity body after compression -OR- send your HTTP message using Transfer-Encoding: chunked and forego the content-length specification.

    On the receiving end, you can instruct mod_deflate to use an input filter to decompress the information:

    <Location /dav-area>
    SetInputFilter DEFLATE
    </Location>
    

    This is a bit heavy handed if you're only receiving compressed message bodies for a couple of resources. Instead, you should probably just use the client-side script to check for the Content-Encoding: gzip header and decompress the request body manually. How to do this in say, PHP, is another question entirely. If you need details for that you should post another question.

    0 讨论(0)
  • 2020-11-30 02:48

    It's possible, but I would recommend strongly against accepting gzipped data incoming to your server. The main reason is to prevent your server from getting gzip bombed. It's generally not possible to know what the uncompressed data looks like before actually uncompressing it, so a user could send you a web request that looks like a harmless 1 KB or 1 MB of data but is really 100 GB of data, and then your web server (nginx or apache) hangs the next 10 minutes trying to decompress it all, eventually running out of memory/locking up.

    0 讨论(0)
  • 2020-11-30 03:05

    Just achieved this using https://github.com/dankogai/js-deflate However the postdata for whatever reason will strip the + signs and replace them with spaces.

    To send the data via javascript:

    params.mapdata=  btoa(RawDeflate.deflate(JSON.stringify(mapdata)));
    

    To receive the data via php:

    $value = gzinflate(base64_decode(preg_replace('/\s/', '+',$value)));
    
    0 讨论(0)
提交回复
热议问题