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
26 import http_request_buffer
29 # A server handling a single connection
33 # The associated `HttpFactory`
34 var factory
: HttpFactory
36 private var parser
= new HttpRequestParser is lazy
38 redef fun read_http_request
(str
)
40 var request_object
= parser
.parse_http_request
(str
.to_s
)
41 if request_object
!= null then delegate_answer request_object
45 fun delegate_answer
(request
: HttpRequest)
47 # Find target virtual host
48 var virtual_host
= null
49 if request
.header
.keys
.has
("Host") then
50 var host
= request
.header
["Host"]
51 if host
.index_of
(':') == -1 then host
+= ":80"
52 for vh
in factory
.config
.virtual_hosts
do
53 for i
in vh
.interfaces
do if i
.to_s
== host
then
60 # Use default virtual host if none already responded
61 if virtual_host
== null then virtual_host
= factory
.config
.default_virtual_host
63 # Get a response from the virtual host
65 if virtual_host
!= null then
66 var route
= virtual_host
.routes
[request
.uri
]
68 # include uri parameters in request
69 request
.uri_params
= route
.parse_params
(request
.uri
)
71 var handler
= route
.handler
75 turi
= ("/" + request
.uri
.substring_from
(root
.length
)).simplify_path
76 else turi
= request
.uri
78 # Delegate the responsibility to respond to the `Action`
79 handler
.prepare_respond_and_close
(request
, turi
, self)
81 else response
= new HttpResponse(405)
82 else response
= new HttpResponse(405)
88 # Send back `response` to the client
89 fun respond
(response
: HttpResponse)
92 for path
in response
.files
do write_file path
96 redef abstract class Action
97 # Prepare a `HttpResponse` destined to the client in response to the `request`
99 # `request` is fully formed request object with a reference to the session
100 # if one already exists.
102 # `truncated_uri` is the ending of the full request URI, truncated from the route
103 # leading to this `Action`.
104 fun answer
(request
: HttpRequest, truncated_uri
: String): HttpResponse is abstract
106 # Full to a `request` with sending the response and closing of the `http_server`
108 # Must users only need to implement `answer`, this method is for advanced use only.
109 # It can be used to delay an answer until an event is raised or work is done on a different thread.
111 # By default this method calls `answer`, relays the response to `http_server.respond` and closes `http_server`.
112 protected fun prepare_respond_and_close
(request
: HttpRequest, truncated_uri
: String, http_server
: HttpServer)
114 var response
= answer
(request
, truncated_uri
)
115 http_server
.respond response
120 # Factory to create `HttpServer` instances, and hold the libevent base handler
122 super ConnectionFactory
124 # Configuration of this server
126 # It should be populated after this object has instanciated
127 var config
= new ServerConfig.with_factory
(self)
129 # Instantiate a server and libvent
131 # You can use this to create the first `HttpFactory`, which is the most common.
132 init and_libevent
do init(new NativeEventBase)
134 redef fun spawn_connection
(buf_ev
) do return new HttpServer(buf_ev
, self)
136 # Execute the main listening loop to accept connections
138 # After the loop ends, the underlying resources are freed.
140 # When the environment variable `NIT_TESTING` is set to `true`,
141 # the loop is not executed but the resources are still freed.
144 if "NIT_TESTING".environ
!= "true" then
152 redef class ServerConfig
153 # Handle to retreive the `HttpFactory` on config change
154 private var factory
: HttpFactory
156 private init with_factory
(factory
: HttpFactory) do self.factory
= factory
161 private var listeners
= new HashMap2[String, Int, ConnectionListener]
163 # Hosts needong each listener
164 private var listeners_count
= new HashMap2[String, Int, Int]
166 # Activate a listener on `interfac` if there's not already one
167 private fun listen_on
(interfac
: Interface, factory
: HttpFactory)
169 if interfac
.registered
then return
171 var name
= interfac
.name
172 var port
= interfac
.port
174 var listener
= listeners
[name
, port
]
175 if listener
== null then
176 listener
= factory
.bind_to
(name
, port
)
177 if listener
!= null then
178 sys
.listeners
[name
, port
] = listener
179 listeners_count
[name
, port
] = 1
182 listeners_count
[name
, port
] += 1
185 interfac
.registered
= true
188 # TODO close listener
191 redef class Interface
192 # Has `self` been registered by `listen_on`?
193 private var registered
= false
196 redef class Interfaces
200 if vh
.server_config
!= null then sys
.listen_on
(e
, vh
.server_config
.factory
)
206 redef class VirtualHosts
210 for i
in e
.interfaces
do sys
.listen_on
(i
, config
.factory
)