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.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/net/http/server/requests.rb', line 93

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.



125
126
127
128
# File 'lib/net/http/server/requests.rb', line 125

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
81
82
83
84
85
# File 'lib/net/http/server/requests.rb', line 65

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

  if uri.kind_of?(Hash)
    if uri[:scheme]
      uri[:port] = unless uri[:port]
                     DEFAULT_PORTS[uri[:scheme]]
                   else
                     uri[:port].to_i
                   end
    end

    unless uri[:path]
      uri[:path] = '/'
    else
      uri[:path].insert(0,'/')
    end
  elsif uri == '*'
    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