module Net::HTTP::Server::Responses

Constants

BAD_REQUEST

Generic Bad Request response

HTTP_STATUSES

The known HTTP Status codes and messages

HTTP_VERSION

The supported HTTP Protocol.

Protected Instance Methods

write_body(stream,body) click to toggle source

Writes the body of a HTTP Response to a stream.

@param [IO] stream

The stream to write the headers back to.

@param [#each] body

The body of the HTTP Response.
# File lib/net/http/server/responses.rb, line 122
def write_body(stream,body)
  body.each do |chunk|
    stream.write(chunk)
    stream.flush
  end
end
write_body_streamed(stream,body) click to toggle source

Writes the body of a HTTP Response to a stream, using Chunked Transfer-Encoding.

@param [IO] stream

The stream to write the headers back to.

@param [#each] body

The body of the HTTP Response.

@since 0.2.0

# File lib/net/http/server/responses.rb, line 141
def write_body_streamed(stream,body)
  chunked_stream = ChunkedStream.new(stream)

  body.each { |chunk| chunked_stream.write(chunk) }

  chunked_stream.close
end
write_headers(stream,headers) click to toggle source

Write the headers of an HTTP Response to a stream.

@param [IO] stream

The stream to write the headers back to.

@param [Hash{String => String,Time,Array<String>}] headers

The headers of the HTTP Response.
# File lib/net/http/server/responses.rb, line 93
def write_headers(stream,headers)
  headers.each do |name,values|
    case values
    when String
      values.each_line("\n") do |value|
        stream.write("#{name}: #{value.chomp}\r\n")
      end
    when Time
      stream.write("#{name}: #{values.httpdate}\r\n")
    when Array
      values.each do |value|
        stream.write("#{name}: #{value}\r\n")
      end
    end
  end

  stream.write("\r\n")
  stream.flush
end
write_response(stream,status,headers,body) click to toggle source

Writes a HTTP Response to a stream.

@param [IO] stream

The stream to write the HTTP Response to.

@param [Integer] status

The status of the HTTP Response.

@param [Hash{String => String,Time,Array<String>}] headers

The headers of the HTTP Response.

@param [#each] body

The body of the HTTP Response.
# File lib/net/http/server/responses.rb, line 164
def write_response(stream,status,headers,body)
  write_status stream, status
  write_headers stream, headers

  if headers['Transfer-Encoding'] == 'chunked'
    write_body_streamed stream, body
  else
    write_body stream, body

    # if neither `Content-Length` or `Transfer-Encoding`
    # were specified, close the stream after writing the response.
    stream.close unless headers['Content-Length']
  end
end
write_status(stream,status) click to toggle source

Writes the status of an HTTP Response to a stream.

@param [IO] stream

The stream to write the headers back to.

@param [Integer] status

The status of the HTTP Response.
# File lib/net/http/server/responses.rb, line 77
def write_status(stream,status)
  status = status.to_i

  reason = HTTP_STATUSES[status]
  stream.write("HTTP/#{HTTP_VERSION} #{status} #{reason}\r\n")
end