Class: GThang::HttpServer

Inherits:
GServer
  • Object
show all
Defined in:
lib/g_thang/http_server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HttpServer

Returns a new instance of HttpServer.



10
11
12
13
14
# File 'lib/g_thang/http_server.rb', line 10

def initialize(options={})
  @port = options[:Port] || 8080
  @rack_app = options[:rack_app]
  super(@port)
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/g_thang/http_server.rb', line 8

def port
  @port
end

#rack_appObject (readonly)

Returns the value of attribute rack_app.



8
9
10
# File 'lib/g_thang/http_server.rb', line 8

def rack_app
  @rack_app
end

Class Method Details

.run(app, options = {}) ⇒ Object



33
34
35
36
37
# File 'lib/g_thang/http_server.rb', line 33

def self.run(app, options={})
  server = new({:rack_app => app}.merge(options))
  server.start
  server.join
end

Instance Method Details

#h(str) ⇒ Object



29
30
31
# File 'lib/g_thang/http_server.rb', line 29

def h(str)
  CGI::escapeHTML(str.to_s)
end

#serve(io) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/g_thang/http_server.rb', line 16

def serve(io)
  RackHandler.new(io, rack_app, port).handle_request
rescue Exception => ex
  response = "HTTP/1.1 500 Internal Server Error\r\n"
  response << "Connection: close\r\n"
  response << "Content-Type: text/html\r\n\r\n"
  response << "<html><head><title>Internal Server Error</title></head><body>\n"
  response << "<h1>Internal Server Error</h1>\n<pre>\n"
  response << h("#{ex.class}: #{ex.message}\n#{ex.backtrace.join("\n    ")}\n")
  response << "</pre></body></html>"
  io << response
end