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


#1

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


#2

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