Module: Net::HTTP::Server::Requests

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

Constant Summary collapse

DEFAULT_PORTS =

Default ports for common URI schemes

{
  'https' => 443,
  'http'  => 80
}

Instance Method Summary collapse

Instance Method Details

#normalize_headers(request) ⇒ Object (protected)

Normalizes the :headers part of the request.

Parameters:

  • request (Hash)

    The unnormalized HTTP request.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/net/http/server/requests.rb', line 88

def normalize_headers(request)
  headers = request[:headers]
  normalized_headers = {}

  unless headers.empty?
    headers.each do |header|
      name  = header[:name].to_s
      value = header[:value].to_s

      if normalized_headers.has_key?(name)
        previous_value = normalized_headers[name]

        if previous_value.kind_of?(Array)
          previous_value << value
        else
          normalized_headers[name] = [previous_value, value]
        end
      else
        normalized_headers[name] = value
      end
    end
  end

  request[:headers] = normalized_headers
end

#normalize_request(request) ⇒ Object (protected)

Normalizes a HTTP request.

Parameters:

  • request (Hash)

    The unnormalized HTTP request.



120
121
122
123
# File 'lib/net/http/server/requests.rb', line 120

def normalize_request(request)
  normalize_uri(request)
  normalize_headers(request)
end

#normalize_uri(request) ⇒ Object (protected)

Normalizes the :uri part of the request.

Parameters:

  • request (Hash)

    The unnormalized HTTP request.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/net/http/server/requests.rb', line 65

def normalize_uri(request)
  uri = request[:uri]

  case uri
  when Hash
    if uri[:scheme]
      uri[:port] = unless uri[:port]
                     DEFAULT_PORTS[uri[:scheme]]
                   else
                     uri[:port].to_i
                   end
    end
  when '*'
    request[:uri] = {}
  end
end

#read_request(stream) ⇒ String? (protected)

Reads a HTTP Request from the stream.

Parameters:

  • stream (IO)

    The stream to read from.

Returns:

  • (String, nil)

    The raw HTTP Request or nil if the Request was malformed.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/net/http/server/requests.rb', line 24

def read_request(stream)
  buffer = ''

  begin
    request_line = stream.readline("\r\n")

    # the request line must contain 'HTTP/'
    return unless request_line.include?('HTTP/')

    buffer << request_line

    stream.each_line("\r\n") do |header|
      buffer << header

      # a header line must contain a ':' character followed by
      # linear-white-space (either ' ' or "\t").
      unless (header.include?(': ') || header.include?(":\t"))
        # if this is not a header line, check if it is the end
        # of the request
        if header == "\r\n"
          # end of the request
          break
        else
          # invalid header line
          return
        end
      end
    end
  rescue IOError, SystemCallError
    return
  end

  return buffer
end