Module: Net::HTTP::Server::Responses

Included in:
Daemon
Defined in:
lib/net/http/server/responses.rb

Constant Summary collapse

HTTP_VERSION =

The supported HTTP Protocol.

'1.1'
HTTP_STATUSES =

The known HTTP Status codes and messages

{
  # 1xx
  100 => 'Continue',
  101 => 'Switching Protocols',
  102 => 'Processing',
  # 2xx
  200 => 'OK',
  201 => 'Created',
  202 => 'Accepted',
  203 => 'Non-Authoritative Information',
  204 => 'No Content',
  205 => 'Reset Content',
  206 => 'Partial Content',
  # 3xx
  300 => 'Multiple Choices',
  301 => 'Moved Permanently',
  302 => 'Found',
  303 => 'See Other',
  304 => 'Not Modified',
  305 => 'Use Proxy',
  307 => 'Temporary Redirect',
  # 4xx
  400 => 'Bad Request',
  401 => 'Unauthorized',
  402 => 'Payment Required',
  403 => 'Forbidden',
  404 => 'Not Found',
  405 => 'Method Not Allowed',
  406 => 'Not Acceptable',
  407 => 'Proxy Authentication Required',
  408 => 'Request Time-out',
  409 => 'Conflict',
  410 => 'Gone',
  411 => 'Length Required',
  412 => 'Precondition Failed',
  413 => 'Request Entity Too Large',
  414 => 'Request-URI Too Large',
  415 => 'Unsupported Media Type',
  416 => 'Requested range not satisfiable',
  417 => 'Expectation Failed',
  # 5xx
  500 => 'Internal Server Error',
  501 => 'Not Implemented',
  502 => 'Bad Gateway',
  503 => 'Service Unavailable',
  504 => 'Gateway Time-out',
  505 => 'HTTP Version not supported extension-code'
}
BAD_REQUEST =

Generic Bad Request response

[400, {}, ['Bad Request']]

Instance Method Summary collapse

Instance Method Details

#write_body(stream, body) ⇒ Object (protected)

Writes the body of a HTTP Response to a stream.

Parameters:

  • stream (IO)

    The stream to write the headers back to.

  • body (#each)

    The body of the HTTP Response.



122
123
124
125
126
127
# 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) ⇒ Object (protected)

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

Parameters:

  • stream (IO)

    The stream to write the headers back to.

  • body (#each)

    The body of the HTTP Response.

Since:

  • 0.2.0



141
142
143
144
145
146
147
# 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) ⇒ Object (protected)

Write the headers of an HTTP Response to a stream.

Parameters:

  • stream (IO)

    The stream to write the headers back to.

  • headers (Hash{String => String,Time,Array<String>})

    The headers of the HTTP Response.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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) ⇒ Object (protected)

Writes a HTTP Response to a stream.

Parameters:

  • stream (IO)

    The stream to write the HTTP Response to.

  • status (Integer)

    The status of the HTTP Response.

  • headers (Hash{String => String,Time,Array<String>})

    The headers of the HTTP Response.

  • body (#each)

    The body of the HTTP Response.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
# 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) ⇒ Object (protected)

Writes the status of an HTTP Response to a stream.

Parameters:

  • stream (IO)

    The stream to write the headers back to.

  • status (Integer)

    The status of the HTTP Response.



77
78
79
80
81
82
# 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