Class | Mongrel::HttpResponse |
In: |
lib/mongrel/http_response.rb
lib/mongrel/http_response.rb |
Parent: | Object |
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.
body | [R] | |
body | [R] | |
body | [W] | |
body | [W] | |
body_sent | [R] | |
body_sent | [R] | |
header | [R] | |
header | [R] | |
header_sent | [R] | |
header_sent | [R] | |
socket | [R] | |
socket | [R] | |
status | [W] | |
status | [R] | |
status | [W] | |
status | [R] | |
status_sent | [R] | |
status_sent | [R] |
# 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
# 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
# File lib/mongrel/http_response.rb, line 158 158: def done 159: (@status_sent and @header_sent and @body_sent) 160: end
# File lib/mongrel/http_response.rb, line 158 158: def done 159: (@status_sent and @header_sent and @body_sent) 160: end
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: @header.out.truncate(0) 79: @body.close 80: @body = StringIO.new 81: end 82: end
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: @header.out.truncate(0) 79: @body.close 80: @body = StringIO.new 81: end 82: end
# File lib/mongrel/http_response.rb, line 100 100: def send_body 101: if not @body_sent 102: @body.rewind 103: write(@body.read) 104: @body_sent = true 105: end 106: end
# File lib/mongrel/http_response.rb, line 100 100: def send_body 101: if not @body_sent 102: @body.rewind 103: write(@body.read) 104: @body_sent = true 105: end 106: end
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 112 112: def send_file(path, small_file = false) 113: if small_file 114: File.open(path, "rb") {|f| @socket << f.read } 115: else 116: File.open(path, "rb") do |f| 117: while chunk = f.read(Const::CHUNK_SIZE) and chunk.length > 0 118: begin 119: write(chunk) 120: rescue Object => exc 121: break 122: end 123: end 124: end 125: end 126: @body_sent = true 127: end
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 112 112: def send_file(path, small_file = false) 113: if small_file 114: File.open(path, "rb") {|f| @socket << f.read } 115: else 116: File.open(path, "rb") do |f| 117: while chunk = f.read(Const::CHUNK_SIZE) and chunk.length > 0 118: begin 119: write(chunk) 120: rescue Object => exc 121: break 122: end 123: end 124: end 125: end 126: @body_sent = true 127: end
# File lib/mongrel/http_response.rb, line 92 92: def send_header 93: if not @header_sent 94: @header.out.rewind 95: write(@header.out.read + Const::LINE_END) 96: @header_sent = true 97: end 98: end
# File lib/mongrel/http_response.rb, line 92 92: def send_header 93: if not @header_sent 94: @header.out.rewind 95: write(@header.out.read + Const::LINE_END) 96: @header_sent = true 97: end 98: end
# File lib/mongrel/http_response.rb, line 84 84: def send_status(content_length=@body.length) 85: if not @status_sent 86: @header['Content-Length'] = content_length if content_length and @status != 304 87: write(Const::STATUS_FORMAT % [@status, @reason || HTTP_STATUS_CODES[@status]]) 88: @status_sent = true 89: end 90: end
# File lib/mongrel/http_response.rb, line 84 84: def send_status(content_length=@body.length) 85: if not @status_sent 86: @header['Content-Length'] = content_length if content_length and @status != 304 87: write(Const::STATUS_FORMAT % [@status, @reason || HTTP_STATUS_CODES[@status]]) 88: @status_sent = true 89: end 90: end
# File lib/mongrel/http_response.rb, line 129 129: def socket_error(details) 130: # ignore these since it means the client closed off early 131: @socket.close rescue nil 132: done = true 133: raise details 134: end
# File lib/mongrel/http_response.rb, line 129 129: def socket_error(details) 130: # ignore these since it means the client closed off early 131: @socket.close rescue nil 132: done = true 133: raise details 134: end
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
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
# File lib/mongrel/http_response.rb, line 136 136: def write(data) 137: @socket.write(data) 138: rescue => details 139: socket_error(details) 140: end