Webrick override HTTPResponse::setup_header

Hi, all.

My subj solution:

class MyServlet < HTTPServlet::AbstractServlet
def do_GET(req, res)
class <<res
def setup_header()
… my code …
end
end

end

end

I think my solution is’t good and is’t true ruby way.
Another solution?

On Apr 18, 2008, at 03:40 AM, andrey wrote:

end

end

I think my solution is’t good and is’t true ruby way.
Another solution?

What are you trying to accomplish with this code?

Hi,

As Eric said, it’s not obvious what end result you are expecting but
it appears you want to change the headers of the response. If so, you
can do something like this:

class MyServlet < HTTPServlet::AbstractServlet
def do_Get(req, res)
res[“header_name”] = “header_value”

end
end

Webrick is not very well documented but the method names and their
source code are available here: RDoc Documentation

Dan

Hi andrey,

To clear out all of the existing headers you can use
res.instance_variable_set("@header", {“header_name” => “header_value”,
“other_header_name” => “other_header_value”})

Dan

On Sat, Apr 19, 2008 at 10:28 AM, Andrey Nikitin

Hi,

Generally you should reply to the ruby-talk list instead of one
particular person as someone on the list might have more knowledge of
this subject.

Are you sure that is the behavior of res[“header”]? The source code
does not look like it differentiates between pre-existing fields and
new fields. If you want to change the body of the response, use
res.body = “…”

Dan

2008/4/19 andrey [email protected]:

On 19 ÁÐÒ, 18:44, Daniel F. [email protected] wrote:

To clear out all of the existing headers you can use
res.instance_variable_set(“@header”, {“header_name” => “header_value”,
“other_header_name” => “other_header_value”})

Yes, but after exiting do_Get() Webrick add self headers:

 def do_GET(req, res)
    ...
     res.instance_variable_set("@header",
                              {"header_name" => "header_value",
                              "other_header_name" =>

“other_header_value”})

end

% nc localhost 60001
HEAD /mjpeg.cgi HTTP/1.0

HTTP/1.1 200 OK
Connection: close
Date: Sat, 19 Apr 2008 14:52:47 GMT
Content-Type:text/html
Server: WEBrick/1.3.1 (Ruby/1.8.5/2006-08-25)
Other_header_name: other_header_value
Header_name: header_value

On 19 апр, 18:44, Daniel F. [email protected] wrote:

To clear out all of the existing headers you can use
res.instance_variable_set(“@header”, {“header_name” => “header_value”,
“other_header_name” => “other_header_value”})

Yes, but after exiting do_Get() Webrick add self headers:

If you want to change the Content-Type, use

req[‘Content-Type’] = ‘application/x-my-world’

mfg, simon … l