Class: Rex::Proto::Http::Server
- Inherits:
-
Object
- Object
- Rex::Proto::Http::Server
- Includes:
- Rex::Proto
- Defined in:
- lib/rex/proto/http/server.rb
Overview
Acts as an HTTP server, processing requests and dispatching them to registered procs. Some of this server was modeled after webrick.
Defined Under Namespace
Classes: UnitTest
Constant Summary collapse
- ExtensionMimeTypes =
A hash that associated a file extension with a mime type for use as the content type of responses.
{ "rhtml" => "text/html", "html" => "text/html", "htm" => "text/htm", "jpg" => "image/jpeg", "jpeg" => "image/jpeg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png", "bmp" => "image/bmp", "txt" => "text/plain", "css" => "text/css", "ico" => "image/x-icon", }
- DefaultServer =
The default server name that will be returned in the Server attribute of a response.
"Rex"
Instance Attribute Summary collapse
-
#comm ⇒ Object
Returns the value of attribute comm.
-
#context ⇒ Object
Returns the value of attribute context.
-
#listen_host ⇒ Object
Returns the value of attribute listen_host.
-
#listen_port ⇒ Object
Returns the value of attribute listen_port.
-
#listener ⇒ Object
Returns the value of attribute listener.
-
#resources ⇒ Object
Returns the value of attribute resources.
-
#server_name ⇒ Object
Returns the value of attribute server_name.
-
#ssl ⇒ Object
Returns the value of attribute ssl.
-
#ssl_cert ⇒ Object
Returns the value of attribute ssl_cert.
Class Method Summary collapse
-
.hardcore_alias(*args) ⇒ Object
Returns the hardcore alias for the HTTP service.
Instance Method Summary collapse
-
#add_resource(name, opts) ⇒ Object
Adds a resource handler, such as one for /, which will be called whenever the resource is requested.
-
#add_response_headers(resp) ⇒ Object
Adds Server headers and stuff.
-
#alias ⇒ Object
HTTP server.
-
#close_client(cli) ⇒ Object
Closes the supplied client, if valid.
-
#initialize(port = 80, listen_host = '0.0.0.0', ssl = false, context = {}, comm = nil, ssl_cert = nil) ⇒ Server
constructor
Initializes an HTTP server as listening on the provided port and hostname.
-
#mime_type(file) ⇒ Object
Returns the mime type associated with the supplied file.
-
#mount(root, handler, long_call = false, *args) ⇒ Object
Mounts a directory or resource as being serviced by the supplied handler.
-
#remove_resource(name) ⇒ Object
Removes the supplied resource handler.
-
#send_e404(cli, request) ⇒ Object
Sends a 404 error to the client for a given request.
-
#start ⇒ Object
Listens on the defined port and host and starts monitoring for clients.
-
#stop ⇒ Object
Terminates the monitor thread and turns off the listener.
-
#unmount(root) ⇒ Object
Remove the mount point.
-
#wait ⇒ Object
Waits for the HTTP service to terminate.
Constructor Details
#initialize(port = 80, listen_host = '0.0.0.0', ssl = false, context = {}, comm = nil, ssl_cert = nil) ⇒ Server
Initializes an HTTP server as listening on the provided port and hostname.
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rex/proto/http/server.rb', line 102 def initialize(port = 80, listen_host = '0.0.0.0', ssl = false, context = {}, comm = nil, ssl_cert = nil) self.listen_host = listen_host self.listen_port = port self.ssl = ssl self.context = context self.comm = comm self.ssl_cert = ssl_cert self.listener = nil self.resources = {} self.server_name = DefaultServer end |
Instance Attribute Details
#comm ⇒ Object
Returns the value of attribute comm.
262 263 264 |
# File 'lib/rex/proto/http/server.rb', line 262 def comm @comm end |
#context ⇒ Object
Returns the value of attribute context.
262 263 264 |
# File 'lib/rex/proto/http/server.rb', line 262 def context @context end |
#listen_host ⇒ Object
Returns the value of attribute listen_host.
262 263 264 |
# File 'lib/rex/proto/http/server.rb', line 262 def listen_host @listen_host end |
#listen_port ⇒ Object
Returns the value of attribute listen_port.
262 263 264 |
# File 'lib/rex/proto/http/server.rb', line 262 def listen_port @listen_port end |
#listener ⇒ Object
Returns the value of attribute listener.
263 264 265 |
# File 'lib/rex/proto/http/server.rb', line 263 def listener @listener end |
#resources ⇒ Object
Returns the value of attribute resources.
263 264 265 |
# File 'lib/rex/proto/http/server.rb', line 263 def resources @resources end |
#server_name ⇒ Object
Returns the value of attribute server_name.
262 263 264 |
# File 'lib/rex/proto/http/server.rb', line 262 def server_name @server_name end |
#ssl ⇒ Object
Returns the value of attribute ssl.
262 263 264 |
# File 'lib/rex/proto/http/server.rb', line 262 def ssl @ssl end |
#ssl_cert ⇒ Object
Returns the value of attribute ssl_cert.
262 263 264 |
# File 'lib/rex/proto/http/server.rb', line 262 def ssl_cert @ssl_cert end |
Class Method Details
.hardcore_alias(*args) ⇒ Object
Returns the hardcore alias for the HTTP service
118 119 120 |
# File 'lib/rex/proto/http/server.rb', line 118 def self.hardcore_alias(*args) "#{(args[0] || '')}#{(args[1] || '')}" end |
Instance Method Details
#add_resource(name, opts) ⇒ Object
Adds a resource handler, such as one for /, which will be called whenever the resource is requested. The “opts” parameter can have any of the following:
Proc (proc) - The procedure to call when a request comes in for this resource. LongCall (bool) - Hints to the server that this resource may have long
request processing times.
200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/rex/proto/http/server.rb', line 200 def add_resource(name, opts) if (resources[name]) raise RuntimeError, "The supplied resource '#{name}' is already added.", caller end # If a procedure was passed, mount the resource with it. if (opts['Proc']) mount(name, Handler::Proc, false, opts['Proc'], opts['VirtualDirectory']) else raise ArgumentError, "You must specify a procedure." end end |
#add_response_headers(resp) ⇒ Object
Adds Server headers and stuff.
224 225 226 |
# File 'lib/rex/proto/http/server.rb', line 224 def add_response_headers(resp) resp['Server'] = self.server_name if not resp['Server'] end |
#alias ⇒ Object
HTTP server.
125 126 127 |
# File 'lib/rex/proto/http/server.rb', line 125 def alias super || "HTTP Server" end |
#close_client(cli) ⇒ Object
Closes the supplied client, if valid.
173 174 175 |
# File 'lib/rex/proto/http/server.rb', line 173 def close_client(cli) listener.close_client(cli) end |
#mime_type(file) ⇒ Object
Returns the mime type associated with the supplied file. Right now the set of mime types is fairly limited.
232 233 234 235 236 237 238 239 240 |
# File 'lib/rex/proto/http/server.rb', line 232 def mime_type(file) type = nil if (file =~ /\.(.+?)$/) type = ExtensionMimeTypes[$1.downcase] end type || "text/plain" end |
#mount(root, handler, long_call = false, *args) ⇒ Object
Mounts a directory or resource as being serviced by the supplied handler.
180 181 182 |
# File 'lib/rex/proto/http/server.rb', line 180 def mount(root, handler, long_call = false, *args) resources[root] = [ handler, long_call, args ] end |
#remove_resource(name) ⇒ Object
Removes the supplied resource handler.
217 218 219 |
# File 'lib/rex/proto/http/server.rb', line 217 def remove_resource(name) self.resources.delete(name) end |
#send_e404(cli, request) ⇒ Object
Sends a 404 error to the client for a given request.
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/rex/proto/http/server.rb', line 245 def send_e404(cli, request) resp = Response::E404.new resp['Content-Type'] = 'text/html' resp.body = "<html><head>" + "<title>404 Not Found</title>" + "</head><body>" + "<h1>Not found</h1>" + "The requested URL #{html_escape(request.resource)} was not found on this server.<p><hr>" + "</body></html>" # Send the response to the client like what cli.send_response(resp) end |
#start ⇒ Object
Listens on the defined port and host and starts monitoring for clients.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/rex/proto/http/server.rb', line 132 def start self.listener = Rex::Socket::TcpServer.create( 'LocalHost' => self.listen_host, 'LocalPort' => self.listen_port, 'Context' => self.context, 'SSL' => self.ssl, 'SSLCert' => self.ssl_cert, 'Comm' => self.comm ) # Register callbacks self.listener.on_client_connect_proc = Proc.new { |cli| on_client_connect(cli) } self.listener.on_client_data_proc = Proc.new { |cli| on_client_data(cli) } self.listener.start end |
#stop ⇒ Object
Terminates the monitor thread and turns off the listener.
157 158 159 160 |
# File 'lib/rex/proto/http/server.rb', line 157 def stop self.listener.stop self.listener.close end |
#unmount(root) ⇒ Object
Remove the mount point.
187 188 189 |
# File 'lib/rex/proto/http/server.rb', line 187 def unmount(root) resources.delete(root) end |
#wait ⇒ Object
Waits for the HTTP service to terminate
166 167 168 |
# File 'lib/rex/proto/http/server.rb', line 166 def wait self.listener.wait if self.listener end |