lib/nitcorn: rename http_request_parser to http_request_buffer
[nit.git] / lib / nitcorn / reactor.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Jean-Philippe Caissy <jpcaissy@piji.ca>
4 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Copyright 2014 Alexandre Terrasa <alexandre@moz-code.org>
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18
19 # Core of the `nitcorn` project, provides `HttpFactory` and `Action`
20 module reactor
21
22 import more_collections
23
24 import vararg_routes
25 import http_request
26 import http_request_buffer
27 import http_response
28
29 # A server handling a single connection
30 class HttpServer
31 super Connection
32
33 # The associated `HttpFactory`
34 var factory: HttpFactory
35
36 # Init the server using `HttpFactory`.
37 init(buf_ev: NativeBufferEvent, factory: HttpFactory) is old_style_init do
38 self.factory = factory
39 end
40
41 private var parser = new HttpRequestParser is lazy
42
43 redef fun read_callback(str)
44 do
45 var request_object = parser.parse_http_request(str.to_s)
46 if request_object != null then delegate_answer request_object
47 end
48
49 # Answer to a request
50 fun delegate_answer(request: HttpRequest)
51 do
52 # Find target virtual host
53 var virtual_host = null
54 if request.header.keys.has("Host") then
55 var host = request.header["Host"]
56 if host.index_of(':') == -1 then host += ":80"
57 for vh in factory.config.virtual_hosts do
58 for i in vh.interfaces do if i.to_s == host then
59 virtual_host = vh
60 break label
61 end
62 end label
63 end
64
65 # Use default virtual host if none already responded
66 if virtual_host == null then virtual_host = factory.config.default_virtual_host
67
68 # Get a response from the virtual host
69 var response
70 if virtual_host != null then
71 var route = virtual_host.routes[request.uri]
72 if route != null then
73 # include uri parameters in request
74 request.uri_params = route.parse_params(request.uri)
75
76 var handler = route.handler
77 var root = route.path
78 var turi
79 if root != null then
80 turi = ("/" + request.uri.substring_from(root.length)).simplify_path
81 else turi = request.uri
82
83 # Delegate the responsibility to respond to the `Action`
84 handler.prepare_respond_and_close(request, turi, self)
85 return
86 else response = new HttpResponse(405)
87 else response = new HttpResponse(405)
88
89 respond response
90 close
91 end
92
93 # Send back `response` to the client
94 fun respond(response: HttpResponse)
95 do
96 write response.to_s
97 for path in response.files do write_file path
98 end
99 end
100
101 redef abstract class Action
102 # Prepare a `HttpResponse` destined to the client in response to the `request`
103 #
104 # `request` is fully formed request object with a reference to the session
105 # if one already exists.
106 #
107 # `truncated_uri` is the ending of the full request URI, truncated from the route
108 # leading to this `Action`.
109 fun answer(request: HttpRequest, truncated_uri: String): HttpResponse is abstract
110
111 # Full to a `request` with sending the response and closing of the `http_server`
112 #
113 # Must users only need to implement `answer`, this method is for advanced use only.
114 # It can be used to delay an answer until an event is raised or work is done on a different thread.
115 #
116 # By default this method calls `answer`, relays the response to `http_server.respond` and closes `http_server`.
117 protected fun prepare_respond_and_close(request: HttpRequest, truncated_uri: String, http_server: HttpServer)
118 do
119 var response = answer(request, truncated_uri)
120 http_server.respond response
121 http_server.close
122 end
123 end
124
125 # Factory to create `HttpServer` instances, and hold the libevent base handler
126 class HttpFactory
127 super ConnectionFactory
128
129 # Configuration of this server
130 #
131 # It should be populated after this object has instanciated
132 var config = new ServerConfig.with_factory(self)
133
134 # Instantiate a server and libvent
135 #
136 # You can use this to create the first `HttpFactory`, which is the most common.
137 init and_libevent do init(new NativeEventBase)
138
139 redef fun spawn_connection(buf_ev) do return new HttpServer(buf_ev, self)
140
141 # Launch the main loop of this server
142 fun run
143 do
144 event_base.dispatch
145 event_base.destroy
146 end
147 end
148
149 redef class ServerConfig
150 # Handle to retreive the `HttpFactory` on config change
151 private var factory: HttpFactory
152
153 private init with_factory(factory: HttpFactory) do self.factory = factory
154 end
155
156 redef class Sys
157 # Active listeners
158 private var listeners = new HashMap2[String, Int, ConnectionListener]
159
160 # Hosts needong each listener
161 private var listeners_count = new HashMap2[String, Int, Int]
162
163 # Activate a listener on `interfac` if there's not already one
164 private fun listen_on(interfac: Interface, factory: HttpFactory)
165 do
166 if interfac.registered then return
167
168 var name = interfac.name
169 var port = interfac.port
170
171 var listener = listeners[name, port]
172 if listener == null then
173 listener = factory.bind_to(name, port)
174 if listener != null then
175 sys.listeners[name, port] = listener
176 listeners_count[name, port] = 1
177 end
178 else
179 listeners_count[name, port] += 1
180 end
181
182 interfac.registered = true
183 end
184
185 # TODO close listener
186 end
187
188 redef class Interface
189 # Has `self` been registered by `listen_on`?
190 private var registered = false
191 end
192
193 redef class Interfaces
194 redef fun add(e)
195 do
196 super
197 if vh.server_config != null then sys.listen_on(e, vh.server_config.factory)
198 end
199
200 # TODO remove
201 end
202
203 redef class VirtualHosts
204 redef fun add(e)
205 do
206 super
207 for i in e.interfaces do sys.listen_on(i, config.factory)
208 end
209
210 # TODO remove
211 end