comparison net/http/server/requests.rb @ 0:1eef88068f9f tip

initial commit of maze game source
author ferencd
date Sun, 15 Sep 2019 11:46:47 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1eef88068f9f
1 require 'net/protocol'
2
3 module Net
4 class HTTP < Protocol
5 module Server
6 module Requests
7 # Default ports for common URI schemes
8 DEFAULT_PORTS = {
9 'https' => 443,
10 'http' => 80
11 }
12
13 protected
14
15 #
16 # Reads a HTTP Request from the stream.
17 #
18 # @param [IO] stream
19 # The stream to read from.
20 #
21 # @return [String, nil]
22 # The raw HTTP Request or `nil` if the Request was malformed.
23 #
24 def read_request(stream)
25 buffer = ''
26
27 begin
28 request_line = stream.readline("\r\n")
29 post_req = false
30 if request_line.start_with? 'POST'
31 post_req = true
32 end
33 # the request line must contain 'HTTP/'
34 return unless request_line.include?('HTTP/')
35
36 buffer << request_line
37
38 stream.each_line("\r\n") do |header|
39 buffer << header
40 # a header line must contain a ':' character followed by
41 # linear-white-space (either ' ' or "\t").
42 unless (header.include?(': ') || header.include?(":\t"))
43 # if this is not a header line, check if it is the end
44 # of the request
45 if header == "\r\n"
46 if post_req
47 # end of the request, what comes here is the possible POST data
48 extra = ''
49 begin
50 extra = stream.read_nonblock(256)
51 rescue
52 extra = ''
53 end
54 buffer = buffer.strip
55 buffer << "\r\nX-Extra: #{extra}"
56 buffer += "\r\n\r\n"
57 end
58 break
59 else
60 # invalid header line
61 return
62 end
63 end
64 end
65
66 rescue IOError, SystemCallError
67 return
68 end
69
70 return buffer
71
72 end
73
74 #
75 # Normalizes the `:uri` part of the request.
76 #
77 # @param [Hash] request
78 # The unnormalized HTTP request.
79 #
80 def normalize_uri(request)
81 uri = request[:uri]
82
83 case uri
84 when Hash
85 if uri[:scheme]
86 uri[:port] = unless uri[:port]
87 DEFAULT_PORTS[uri[:scheme]]
88 else
89 uri[:port].to_i
90 end
91 end
92 when '*'
93 request[:uri] = {}
94 end
95 end
96
97 #
98 # Normalizes the `:headers` part of the request.
99 #
100 # @param [Hash] request
101 # The unnormalized HTTP request.
102 #
103 def normalize_headers(request)
104 headers = request[:headers]
105 normalized_headers = {}
106
107 unless headers.empty?
108 headers.each do |header|
109 name = header[:name].to_s
110 value = header[:value].to_s
111
112 if normalized_headers.has_key?(name)
113 previous_value = normalized_headers[name]
114
115 if previous_value.kind_of?(Array)
116 previous_value << value
117 else
118 normalized_headers[name] = [previous_value, value]
119 end
120 else
121 normalized_headers[name] = value
122 end
123 end
124 end
125
126 request[:headers] = normalized_headers
127 end
128
129 #
130 # Normalizes a HTTP request.
131 #
132 # @param [Hash] request
133 # The unnormalized HTTP request.
134 #
135 def normalize_request(request)
136 normalize_uri(request)
137 normalize_headers(request)
138 end
139
140 end
141 end
142 end
143 end