lib/em-http-server/server.rb in em-http-server-0.1.6 vs lib/em-http-server/server.rb in em-http-server-0.1.7
- old
+ new
@@ -39,41 +39,51 @@
# parse the first HTTP header line
# get the http METHOD, URI and PROTOCOL
def parse_first_header(line)
# split the line into: METHOD URI PROTOCOL
- # eg: GET / HTTP1/1
+ # eg: GET / HTTP/1.1
parsed = line.split(' ')
# a correct request has three parts
- send_error(400, "Bad request") unless parsed.size == 3
+ return bad_parsing unless parsed.size == 3
@http_request_method, uri, @http_protocol = parsed
# optional query string
@http_request_uri, @http_query_string = uri.split('?')
end
- # send back to the client an HTTP error
- def send_error(code, desc)
- string = "HTTP1/1 #{code} #{desc}\r\n"
+ def bad_parsing
+ code = 400
+ desc = "Bad request"
+ string = respond_to?(:http_error_string) ? http_error_string(code, desc) : default_error_string(code, desc)
+ send_error string
+ raise("#{code} #{desc}")
+ end
+
+ def default_error_string(code, desc)
+ string = "HTTP/1.1 #{code} #{desc}\r\n"
string << "Connection: close\r\n"
string << "Content-type: text/plain\r\n"
string << "\r\n"
string << "Detected error: HTTP code #{code}"
+ end
+
+ # send back to the client an HTTP error
+ def send_error(string)
send_data string
close_connection_after_writing
- raise("server error #{code}")
end
end
end
end
if __FILE__ == $0
- class HTTPHandler < EM::Http::Server
+ class HTTPHandler < EM::HttpServer::Server
def process_http_request
puts @http_request_method
puts @http_request_uri
puts @http_query_string
\ No newline at end of file