Parent

Mongrel::HttpResponse

Writes and controls your response to the client using the HTTP/1.1 specification. You use it by simply doing:

 response.start(200) do |head,out|
   head['Content-Type'] = 'text/plain'
   out.write("hello\n")
 end

The parameter to start is the response code—which Mongrel will translate for you based on HTTP_STATUS_CODES. The head parameter is how you write custom headers. The out parameter is where you write your body. The default status code for HttpResponse.start is 200 so the above example is redundant.

As you can see, it’s just like using a Hash and as you do this it writes the proper header to the output on the fly. You can even intermix specifying headers and writing content. The HttpResponse class with write the things in the proper order once the HttpResponse.block is ended.

You may also work the HttpResponse object directly using the various attributes available for the raw socket, body, header, and status codes. If you do this you’re on your own. A design decision was made to force the client to not pipeline requests. HTTP/1.1 pipelining really kills the performance due to how it has to be handled and how unclear the standard is. To fix this the HttpResponse gives a “Connection: close” header which forces the client to close right away. The bonus for this is that it gives a pretty nice speed boost to most clients since they can close their connection immediately.

One additional caveat is that you don’t have to specify the Content-length header as the HttpResponse will write this for you based on the out length.

Attributes

socket[R]
body[R]
body[W]
header[R]
status[R]
status[W]
body_sent[R]
header_sent[R]
status_sent[R]

Public Class Methods

new(socket) click to toggle source
    # File lib/mongrel/http_response.rb, line 42
42:     def initialize(socket)
43:       @socket = socket
44:       @body = StringIO.new
45:       @status = 404
46:       @reason = nil
47:       @header = HeaderOut.new(StringIO.new)
48:       @header[Const::DATE] = Time.now.httpdate
49:       @body_sent = false
50:       @header_sent = false
51:       @status_sent = false
52:     end

Public Instance Methods

done() click to toggle source
     # File lib/mongrel/http_response.rb, line 161
161:     def done
162:       (@status_sent and @header_sent and @body_sent)
163:     end
done=(val) click to toggle source

Used during error conditions to mark the response as “done” so there isn’t any more processing sent to the client.

     # File lib/mongrel/http_response.rb, line 155
155:     def done=(val)
156:       @status_sent = true
157:       @header_sent = true
158:       @body_sent = true
159:     end
finished() click to toggle source

This takes whatever has been done to header and body and then writes it in the proper format to make an HTTP/1.1 response.

     # File lib/mongrel/http_response.rb, line 147
147:     def finished
148:       send_status
149:       send_header
150:       send_body
151:     end
reset() click to toggle source

Primarily used in exception handling to reset the response output in order to write an alternative response. It will abort with an exception if you have already sent the header or the body. This is pretty catastrophic actually.

    # File lib/mongrel/http_response.rb, line 72
72:     def reset
73:       if @body_sent
74:         raise "You have already sent the request body."
75:       elsif @header_sent
76:         raise "You have already sent the request headers."
77:       else
78:         # XXX Dubious ( http://mongrel.rubyforge.org/ticket/19 )
79:         @header.out.close
80:         @header = HeaderOut.new(StringIO.new) 
81:         
82:         @body.close
83:         @body = StringIO.new
84:       end
85:     end
send_body() click to toggle source
     # File lib/mongrel/http_response.rb, line 103
103:     def send_body
104:       if not @body_sent
105:         @body.rewind
106:         write(@body.read)
107:         @body_sent = true
108:       end
109:     end
send_file(path, small_file = false) click to toggle source

Appends the contents of path to the response stream. The file is opened for binary reading and written in chunks to the socket.

Sendfile API support has been removed in 0.3.13.4 due to stability problems.

     # File lib/mongrel/http_response.rb, line 115
115:     def send_file(path, small_file = false)
116:       if small_file
117:         File.open(path, "rb") {|f| @socket << f.read }
118:       else
119:         File.open(path, "rb") do |f|
120:           while chunk = f.read(Const::CHUNK_SIZE) and chunk.length > 0
121:             begin
122:               write(chunk)
123:             rescue Object => exc
124:               break
125:             end
126:           end
127:         end
128:       end
129:       @body_sent = true
130:     end
send_header() click to toggle source
     # File lib/mongrel/http_response.rb, line 95
 95:     def send_header
 96:       if not @header_sent
 97:         @header.out.rewind
 98:         write(@header.out.read + Const::LINE_END)
 99:         @header_sent = true
100:       end
101:     end
send_status(content_length=@body.length) click to toggle source
    # File lib/mongrel/http_response.rb, line 87
87:     def send_status(content_length=@body.length)
88:       if not @status_sent
89:         @header['Content-Length'] = content_length if content_length and @status != 304
90:         write(Const::STATUS_FORMAT % [@status, @reason || HTTP_STATUS_CODES[@status]])
91:         @status_sent = true
92:       end
93:     end
socket_error(details) click to toggle source
     # File lib/mongrel/http_response.rb, line 132
132:     def socket_error(details)
133:       # ignore these since it means the client closed off early
134:       @socket.close rescue nil
135:       done = true
136:       raise details
137:     end
start(status=200, finalize=false, reason=nil) click to toggle source

Receives a block passing it the header and body for you to work with. When the block is finished it writes everything you’ve done to the socket in the proper order. This lets you intermix header and body content as needed. Handlers are able to modify pretty much any part of the request in the chain, and can stop further processing by simple passing “finalize=true” to the start method. By default all handlers run and then mongrel finalizes the request when they’re all done.

    # File lib/mongrel/http_response.rb, line 62
62:     def start(status=200, finalize=false, reason=nil)
63:       @status = status.to_i
64:       @reason = reason
65:       yield @header, @body
66:       finished if finalize
67:     end
write(data) click to toggle source
     # File lib/mongrel/http_response.rb, line 139
139:     def write(data)
140:       @socket.write(data)
141:     rescue => details
142:       socket_error(details)
143:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.