Class: Mongrel::HttpResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/mongrel.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ HttpResponse

Returns a new instance of HttpResponse.



332
333
334
335
336
337
338
339
340
341
# File 'lib/mongrel.rb', line 332

def initialize(socket)
  @socket = socket
  @body = StringIO.new
  @status = 404
  @header = HeaderOut.new(StringIO.new)
  @header[Const::DATE] = Time.now.httpdate
  @body_sent = false
  @header_sent = false
  @status_sent = false
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



323
324
325
# File 'lib/mongrel.rb', line 323

def body
  @body
end

#body_sentObject (readonly)

Returns the value of attribute body_sent.



328
329
330
# File 'lib/mongrel.rb', line 328

def body_sent
  @body_sent
end

#headerObject (readonly)

Returns the value of attribute header.



325
326
327
# File 'lib/mongrel.rb', line 325

def header
  @header
end

#header_sentObject (readonly)

Returns the value of attribute header_sent.



329
330
331
# File 'lib/mongrel.rb', line 329

def header_sent
  @header_sent
end

#socketObject (readonly)

Returns the value of attribute socket.



322
323
324
# File 'lib/mongrel.rb', line 322

def socket
  @socket
end

#statusObject

Returns the value of attribute status.



326
327
328
# File 'lib/mongrel.rb', line 326

def status
  @status
end

#status_sentObject (readonly)

Returns the value of attribute status_sent.



330
331
332
# File 'lib/mongrel.rb', line 330

def status_sent
  @status_sent
end

Instance Method Details

#doneObject



449
450
451
# File 'lib/mongrel.rb', line 449

def done
  (@status_sent and @header_sent and @body_sent)
end

#done=(val) ⇒ Object

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



443
444
445
446
447
# File 'lib/mongrel.rb', line 443

def done=(val)
  @status_sent = true
  @header_sent = true
  @body_sent = true
end

#finishedObject

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.



435
436
437
438
439
# File 'lib/mongrel.rb', line 435

def finished
  send_status
  send_header
  send_body
end

#resetObject

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.



360
361
362
363
364
365
366
367
368
369
# File 'lib/mongrel.rb', line 360

def reset
  if @body_sent
    raise "You have already sent the request body."
  elsif @header_sent
    raise "You have already sent the request headers."
  else
    @header.out.rewind
    @body.rewind
  end
end

#send_bodyObject



387
388
389
390
391
392
393
# File 'lib/mongrel.rb', line 387

def send_body
  if not @body_sent
    @body.rewind
    write(@body.read)
    @body_sent = true
  end
end

#send_file(path) ⇒ Object

Appends the contents of path to the response stream. The file is opened for binary reading and written in chunks to the socket. If the <a href=“rubyforge.org/projects/ruby-sendfile”>sendfile</a> library is found, it is used to send the file, often with greater speed and less memory/cpu usage.

The presence of ruby-sendfile is determined by @socket.response_to? :sendfile, which means that if you have your own sendfile implementation you can use it without changing this function, just make sure it follows the ruby-sendfile signature.



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/mongrel.rb', line 403

def send_file(path)
  File.open(path, "rb") do |f|
    if @socket.respond_to? :sendfile
      begin
        @socket.sendfile(f)
      rescue => details
        socket_error(details)
      end
    else
      while chunk = f.read(Const::CHUNK_SIZE) and chunk.length > 0
        write(chunk)
      end
    end
    @body_sent = true
  end
end

#send_headerObject



379
380
381
382
383
384
385
# File 'lib/mongrel.rb', line 379

def send_header
  if not @header_sent
    @header.out.rewind
    write(@header.out.read + Const::LINE_END)
    @header_sent = true
  end
end

#send_status(content_length = nil) ⇒ Object



371
372
373
374
375
376
377
# File 'lib/mongrel.rb', line 371

def send_status(content_length=nil)
  if not @status_sent
    content_length ||= @body.length
    write(Const::STATUS_FORMAT % [status, HTTP_STATUS_CODES[@status], content_length])
    @status_sent = true
  end
end

#socket_error(details) ⇒ Object



420
421
422
423
424
425
# File 'lib/mongrel.rb', line 420

def socket_error(details)
  # ignore these since it means the client closed off early
  @socket.close unless @socket.closed?
  done = true
  raise details
end

#start(status = 200, finalize = false) {|@header, @body| ... } ⇒ Object

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.

Yields:



351
352
353
354
355
# File 'lib/mongrel.rb', line 351

def start(status=200, finalize=false)
  @status = status.to_i
  yield @header, @body
  finished if finalize
end

#write(data) ⇒ Object



427
428
429
430
431
# File 'lib/mongrel.rb', line 427

def write(data)
  @socket.write(data)
rescue => details
  socket_error(details)
end