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>
5 # Copyright 2014 Alexandre Terrasa <alexandre@moz-code.org>
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 # Core of the `nitcorn` project, provides `HttpFactory` and `Action`
22 import more_collections
23 import http_request_parser
29 # A server handling a single connection
33 # The associated `HttpFactory`
34 var factory
: HttpFactory
36 # Init the server using `HttpFactory`.
37 init(buf_ev
: NativeBufferEvent, factory
: HttpFactory) is old_style_init
do
38 self.factory
= factory
41 private var parser
= new HttpRequestParser is lazy
43 redef fun read_callback
(str
)
45 var request_object
= parser
.parse_http_request
(str
.to_s
)
46 if request_object
!= null then delegate_answer request_object
50 fun delegate_answer
(request
: HttpRequest)
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
65 # Use default virtual host if none already responded
66 if virtual_host
== null then virtual_host
= factory
.config
.default_virtual_host
68 # Get a response from the virtual host
70 if virtual_host
!= null then
71 var route
= virtual_host
.routes
[request
.uri
]
73 # include uri parameters in request
74 request
.uri_params
= route
.parse_params
(request
.uri
)
76 var handler
= route
.handler
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)
90 # Send back `response` to the client
91 fun respond
(response
: HttpResponse)
94 for path
in response
.files
do write_file path
98 redef abstract class Action
99 # Handle a request with the relative URI `truncated_uri`
101 # `request` is fully formed request object and has a reference to the session
104 # `truncated_uri` is the ending of the full request URI, truncated from the route
105 # leading to this `Action`.
106 fun answer
(request
: HttpRequest, truncated_uri
: String): HttpResponse is abstract
109 # Factory to create `HttpServer` instances, and hold the libevent base handler
111 super ConnectionFactory
113 # Configuration of this server
115 # It should be populated after this object has instanciated
116 var config
= new ServerConfig.with_factory
(self)
118 # Instanciate a server and libvent
120 # You can use this to create the first `HttpFactory`, which is the most common.
121 init and_libevent
do init(new NativeEventBase)
123 redef fun spawn_connection
(buf_ev
) do return new HttpServer(buf_ev
, self)
125 # Launch the main loop of this server
133 redef class ServerConfig
134 # Handle to retreive the `HttpFactory` on config change
135 private var factory
: HttpFactory
137 private init with_factory
(factory
: HttpFactory) do self.factory
= factory
142 private var listeners
= new HashMap2[String, Int, ConnectionListener]
144 # Hosts needong each listener
145 private var listeners_count
= new HashMap2[String, Int, Int]
147 # Activate a listener on `interfac` if there's not already one
148 private fun listen_on
(interfac
: Interface, factory
: HttpFactory)
150 if interfac
.registered
then return
152 var name
= interfac
.name
153 var port
= interfac
.port
155 var listener
= listeners
[name
, port
]
156 if listener
== null then
157 listener
= factory
.bind_to
(name
, port
)
158 if listener
!= null then
159 sys
.listeners
[name
, port
] = listener
160 listeners_count
[name
, port
] = 1
163 listeners_count
[name
, port
] += 1
166 interfac
.registered
= true
169 # TODO close listener
172 redef class Interface
173 # Has `self` been registered by `listen_on`?
174 private var registered
= false
177 redef class Interfaces
181 if vh
.server_config
!= null then sys
.listen_on
(e
, vh
.server_config
.factory
)
187 redef class VirtualHosts
191 for i
in e
.interfaces
do sys
.listen_on
(i
, config
.factory
)