Forum: Ruby Net::HTTP and the need for all-capital http header names

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
75b57939380d2520d6bc55d3830cf663?d=identicon&s=25 Christian Folini (Guest)
on 2007-04-26 13:49
Hi there

Net:HTTP will downcase http headers like FOO-BAR to Foo-Bar
upon initialization and upon writing to the socket. While
this behaviour is consistent with most user agents and servers
it is the wrong approach for a small application I am developping.
In fact I want to reissue requests _as is_ and as there are
all-capital header names out there in the wild, I want to
reissue those as all-capitals too.

Now I did a hack that is not particularly elegant, but works
for the time being.

require "net/http"

class Net::HTTP::Get
  # Redefinition of initialize_http_header
  # The original method does a downcase on the header name
  # We need it untouched however as we want to repeat a request as is
  def initialize_http_header(initheader)
    @header = {}
    return unless initheader
    initheader.each do |key, value|
      warn "net/http: warning: duplicated HTTP header: #{key}" if
key?(key) and $VERBOSE
      @header[key] = [value.strip]
    end
  end

end

class Net::HTTP::Post
  # Redefinition of initialize_http_header
  # The original method does a downcase on the header name
  # We need it untouched however as we want to repeat a request as is
  def initialize_http_header(initheader)
    @header = {}
    return unless initheader
    initheader.each do |key, value|
      warn "net/http: warning: duplicated HTTP header: #{key}" if
key?(key) and $VERBOSE
      @header[key] = [value.strip]
    end
  end

end

class Net::HTTPGenericRequest
  # Redefinition of write_header
  # The original one does a downcase on the header name
  # We need it untouched however as we want to repeat a request as is
  # Furthermore there is a duplication of certain headers
  # introduced by our removal of the downcasing in
initialize_http_header
  def write_header(sock, ver, path)
    buf = "#{@method} #{path} HTTP/#{ver}\r\n"
    each do |k,v|
      unless k == "host" or k == "content-length" or k == "content-type"
        # due to our removal of the downcase code, some headers were
reintroduced by
        # the module. We ignore these.
        buf << "#{k}: #{v}\r\n"
      else
      end
    end
    buf << "\r\n"
    sock.write buf
  end
end


Obviously, I would perfer to use a better approach, but I am far
from being a ruby wizard and I do not know much about network
sockets programming. So is there any resource I could be pointed
to or is there an alternative way to use Net::HTTP

regs,

Christian
368f371bcce2b0e6b57dfe1f063f1912?d=identicon&s=25 Edward Liu (el2king)
on 2007-11-08 17:22
Hi,

I also designing an application where I want to use http headers without
ruby automatically changing the casing of my headers.  Does anyone know
of a clean solution without redefining Net::HTTP functions?  I would
like to avoid redefining any library functions.

Thanks,
Edward
This topic is locked and can not be replied to.