1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013 Jean-Philippe Caissy <jpcaissy@piji.ca>
4 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # Core of the `nitcorn` project, provides `HttpFactory` and `Action`
21 import more_collections
28 # A server handling a single connection
32 # The associated `HttpFactory`
33 var factory
: HttpFactory
35 # Init the server using `HttpFactory`.
36 init(buf_ev
: NativeBufferEvent, factory
: HttpFactory) is old_style_init
do
37 self.factory
= factory
40 private var parser
= new HttpRequestParser is lazy
42 redef fun read_callback
(str
)
44 # TODO support bigger inputs (such as big forms and file upload)
46 var request_object
= parser
.parse_http_request
(str
.to_s
)
48 if request_object
!= null then delegate_answer request_object
52 fun delegate_answer
(request
: HttpRequest)
54 # Find target virtual host
55 var virtual_host
= null
56 if request
.header
.keys
.has
("Host") then
57 var host
= request
.header
["Host"]
58 if host
.index_of
(':') == -1 then host
+= ":80"
59 for vh
in factory
.config
.virtual_hosts
do
60 for i
in vh
.interfaces
do if i
.to_s
== host
then
67 # Use default virtual host if none already responded
68 if virtual_host
== null then virtual_host
= factory
.config
.default_virtual_host
70 # Get a response from the virtual host
72 if virtual_host
!= null then
73 var route
= virtual_host
.routes
[request
.uri
]
75 # include uri parameters in request
76 request
.uri_params
= route
.parse_params
(request
.uri
)
78 var handler
= route
.handler
82 turi
= ("/" + request
.uri
.substring_from
(root
.length
)).simplify_path
83 else turi
= request
.uri
84 response
= handler
.answer
(request
, turi
)
85 else response
= new HttpResponse(405)
86 else response
= new HttpResponse(405)
88 # Send back a response
94 redef abstract class Action
95 # Handle a request with the relative URI `truncated_uri`
97 # `request` is fully formed request object and has a reference to the session
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
105 # Factory to create `HttpServer` instances, and hold the libevent base handler
107 super ConnectionFactory
109 # Configuration of this server
111 # It should be populated after this object has instanciated
112 var config
= new ServerConfig.with_factory
(self)
114 # Instanciate a server and libvent
116 # You can use this to create the first `HttpFactory`, which is the most common.
117 init and_libevent
do init(new NativeEventBase)
119 redef fun spawn_connection
(buf_ev
) do return new HttpServer(buf_ev
, self)
121 # Launch the main loop of this server
129 redef class ServerConfig
130 # Handle to retreive the `HttpFactory` on config change
131 private var factory
: HttpFactory
133 private init with_factory
(factory
: HttpFactory) do self.factory
= factory
138 private var listeners
= new HashMap2[String, Int, ConnectionListener]
140 # Hosts needong each listener
141 private var listeners_count
= new HashMap2[String, Int, Int]
143 # Activate a listener on `interfac` if there's not already one
144 private fun listen_on
(interfac
: Interface, factory
: HttpFactory)
146 if interfac
.registered
then return
148 var name
= interfac
.name
149 var port
= interfac
.port
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
159 listeners_count
[name
, port
] += 1
162 interfac
.registered
= true
165 # TODO close listener
168 redef class Interface
169 # Has `self` been registered by `listen_on`?
170 private var registered
= false
173 redef class Interfaces
177 if vh
.server_config
!= null then sys
.listen_on
(e
, vh
.server_config
.factory
)
183 redef class VirtualHosts
187 for i
in e
.interfaces
do sys
.listen_on
(i
, config
.factory
)