Merge: Java FFI: add GC pinning support
[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 import http_request_parser
24
25 import vararg_routes
26 import http_request
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 response = handler.answer(request, turi)
83 else response = new HttpResponse(405)
84 else response = new HttpResponse(405)
85
86 # Send back a response
87 write response.to_s
88 for path in response.files do write_file path
89
90 close
91 end
92 end
93
94 redef abstract class Action
95 # Handle a request with the relative URI `truncated_uri`
96 #
97 # `request` is fully formed request object and has a reference to the session
98 # if one preexists.
99 #
100 # `truncated_uri` is the ending of the full request URI, truncated from the route
101 # leading to this `Action`.
102 fun answer(request: HttpRequest, truncated_uri: String): HttpResponse is abstract
103 end
104
105 # Factory to create `HttpServer` instances, and hold the libevent base handler
106 class HttpFactory
107 super ConnectionFactory
108
109 # Configuration of this server
110 #
111 # It should be populated after this object has instanciated
112 var config = new ServerConfig.with_factory(self)
113
114 # Instanciate a server and libvent
115 #
116 # You can use this to create the first `HttpFactory`, which is the most common.
117 init and_libevent do init(new NativeEventBase)
118
119 redef fun spawn_connection(buf_ev) do return new HttpServer(buf_ev, self)
120
121 # Launch the main loop of this server
122 fun run
123 do
124 event_base.dispatch
125 event_base.destroy
126 end
127 end
128
129 redef class ServerConfig
130 # Handle to retreive the `HttpFactory` on config change
131 private var factory: HttpFactory
132
133 private init with_factory(factory: HttpFactory) do self.factory = factory
134 end
135
136 redef class Sys
137 # Active listeners
138 private var listeners = new HashMap2[String, Int, ConnectionListener]
139
140 # Hosts needong each listener
141 private var listeners_count = new HashMap2[String, Int, Int]
142
143 # Activate a listener on `interfac` if there's not already one
144 private fun listen_on(interfac: Interface, factory: HttpFactory)
145 do
146 if interfac.registered then return
147
148 var name = interfac.name
149 var port = interfac.port
150
151 var listener = listeners[name, port]
152 if listener == null then
153 listener = factory.bind_to(name, port)
154 if listener != null then
155 sys.listeners[name, port] = listener
156 listeners_count[name, port] = 1
157 end
158 else
159 listeners_count[name, port] += 1
160 end
161
162 interfac.registered = true
163 end
164
165 # TODO close listener
166 end
167
168 redef class Interface
169 # Has `self` been registered by `listen_on`?
170 private var registered = false
171 end
172
173 redef class Interfaces
174 redef fun add(e)
175 do
176 super
177 if vh.server_config != null then sys.listen_on(e, vh.server_config.factory)
178 end
179
180 # TODO remove
181 end
182
183 redef class VirtualHosts
184 redef fun add(e)
185 do
186 super
187 for i in e.interfaces do sys.listen_on(i, config.factory)
188 end
189
190 # TODO remove
191 end