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?

What are you trying to accomplish with this code?

···

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

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?

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

···

On Fri, Apr 18, 2008 at 6:40 AM, andrey <Andrey.D.Nikitin@gmail.com> wrote:

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?

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 <Andrey.D.Nikitin@gmail.com>:

On 19 апр, 06:10, Daniel Finnie <d...@danfinnie.com> wrote:

> res["header_name"] = "header_value"

Thanks.
For inclusion in the answer of a server of _additional_ fields it is
convenient to use res ["header_name"].
But I need to change the answer of a server _completely_.

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 <andrey.d.nikitin@gmail.com> wrote:

В сообщении от 19 апреля 2008 18:10 Daniel Finnie написал(a):

> 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.
Sorry.

>
> 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."

-1- Webrick server responce without override HTTPResponse::setup_header()

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

HTTP/1.1 200
Connection: close
Date: Sat, 19 Apr 2008 14:21:33 GMT
Content-Type: text/html
Server: WEBrick/1.3.1 (Ruby/1.8.5/2006-08-25)
Content-Length: 285
cOnTent-tYpE: My-idiotistic-uNsuppotred-Type

-2- full control on server response headers

    def do_GET(req, res)
        class <<res
            def setup_header()
            end
         end # class

        res['cOnTent-tYpE'] = 'My-idiotistic-Type'
        ...
    end

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

HTTP/1.1 200
cOnTent-tYpE: My-idiotistic-uNsuppotred-Type

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
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 Finnie <d...@danfinnie.com> wrote:
Date: Sat, 19 Apr 2008 14:52:47 GMT

* andrey <Andrey.D.Nikitin@gmail.com> (2008-04-19) schrieb:

···

On 19 апр, 18:44, Daniel Finnie <d...@danfinnie.com> 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