Sha256: e8def1b99de21b6b16a9390240dfbde2043014a1e17c6729ce3d541ef8f7b0c8

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

require 'net/http'

# Addtions to the Net::HTTP classes

# Adds some convenience functions for checking response status.
class Net::HTTPResponse
  attr_accessor :success
  attr_writer :body

  def ok?
    success
  end

  def success
    code == '200'
  end

  def not_found
    code = '404'
  end

  def to_s
    body
  end
end

# Modification of Net::HTTP base class to enable larger chunk sizes to be used
# when streaming data from large files.
# 
# N.B. does _not_ alter the behaviour of this class unless you set chunk_size
# using the accessor.
class Net::HTTPGenericRequest
  # Size of chunks (in bytes) to send when streaming body data.
  attr_accessor :chunk_size

  # Dump initial line and raw request headers (useful for visual debugging).
  # 
  # Switch on request debugging by passing <tt>:dump_requests => true</tt> to S33r::Client
  # constructor.
  def to_s
    str = "*******\n" +
    "#{self.class::METHOD} #{@path} HTTP/1.1\n" +
    "Host: #{S33r::HOST}\n"

    self.each_capitalized do |key, value|
      str += "#{key}: #{value}\n"
    end
    str += "*******\n\n"
    str
  end

  private
  # Minor alterations to original source to enable variable chunk sizes.
  def send_request_with_body_stream(sock, ver, path, f) # :doc:
    raise ArgumentError, "Content-Length not given and Transfer-Encoding is not `chunked'" unless content_length() or chunked?
    unless content_type()
      warn 'net/http: warning: Content-Type did not set; using application/x-www-form-urlencoded' if $VERBOSE
      set_content_type 'application/x-www-form-urlencoded'
    end
    @chunk_size ||= 1024
    write_header sock, ver, path
    if chunked?
      while s = f.read(@chunk_size)
        sock.write(sprintf("%x\r\n", s.length) << s << "\r\n")
      end
      sock.write "0\r\n\r\n"
    else
      while s = f.read(@chunk_size)
        sock.write s
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
s33r-0.3 lib/s33r/s33r_http.rb