Class: GThang::HttpServer
- Inherits:
-
GServer
- Object
- GServer
- GThang::HttpServer
- Defined in:
- lib/g_thang/http_server.rb
Instance Attribute Summary collapse
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#rack_app ⇒ Object
readonly
Returns the value of attribute rack_app.
Class Method Summary collapse
Instance Method Summary collapse
- #h(str) ⇒ Object
-
#initialize(options = {}) ⇒ HttpServer
constructor
A new instance of HttpServer.
- #serve(io) ⇒ Object
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(={}) @port = [:Port] || 8080 @rack_app = [:rack_app] super(@port) end |
Instance Attribute Details
#port ⇒ Object (readonly)
Returns the value of attribute port.
8 9 10 |
# File 'lib/g_thang/http_server.rb', line 8 def port @port end |
#rack_app ⇒ Object (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, ={}) server = new({:rack_app => app}.merge()) 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.}\n#{ex.backtrace.join("\n ")}\n") response << "</pre></body></html>" io << response end |