Giter Club home page Giter Club logo

Comments (3)

jnunemaker avatar jnunemaker commented on July 4, 2024

Would setting stream_body: false option help?

https://github.com/jnunemaker/httparty/blob/master/lib/httparty/request.rb#L156-L173

I think that would avoid building up the entire body into a string in response. Closing to keep things tidy but happy to keep trying to help.

from httparty.

fauno avatar fauno commented on July 4, 2024

Thanks! I thought stream_body defaulted to false, so I tried stream_body: true and it works for gzip because Net::Http would stream the decompressed body.

content_length = 0

HTTParty.get(url, stream_body: true) do |fragment|
  content_length += fragment.bytesize

  raise "limit reached" if content_length > 1_000_000
end

Then I added brotli decompression, but 2GB of zeroes compress to 1.6KB (while gzip is 1.9MB) and the entire payload fits into a single call to Brotli.inflate, which defeats the purpose. Probably should try with another gem, but ignoring content-encoding: br will do for now.

Here's the final try:

require 'httparty'
require 'brotli'

body = ''.dup
HTTParty.get(url, stream_body: true, headers: { 'accept-encoding': 'br' }) do |fragment|
  case fragment.http_response['content-encoding']
  when 'br'
    body << Brotli.inflate(fragment) # inflates 1.6KB fragment into a 2GB body
  else
    body << fragment
  end

  raise 'limit reached' if body.bytesize > 1_000_000
end

from httparty.

fauno avatar fauno commented on July 4, 2024

Got it!

require 'httparty'
require 'brs'

body = ''.dup
HTTParty.get(url, stream_body: true, headers: { 'accept-encoding': 'br,gzip' }) do |fragment|
  case fragment.http_response['content-encoding']
  when 'br'
    BRS::Stream::Reader.new(StringIO.new(fragment), source_buffer_length: 256, destination_buffer_length: 1024).each_char do |char|
      body << char

      raise "limit" if body.bytesize > 1_000_000
    end
  else
    body << fragment
  end

  raise "limit" if body.bytesize > 1_000_000
end

from httparty.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.