Class: DCI::Proxy::BaseHttpServer

Inherits:
Object
  • Object
show all
Defined in:
lib/dci/proxy/http.rb

Direct Known Subclasses

HttpServer

Instance Method Summary collapse

Constructor Details

#initialize(address, port) ⇒ BaseHttpServer

Returns a new instance of BaseHttpServer.



7
8
9
# File 'lib/dci/proxy/http.rb', line 7

def initialize address, port
	@srv = TCPServer.new address, port
end

Instance Method Details

#handle_request(client, params) ⇒ Object



71
72
73
# File 'lib/dci/proxy/http.rb', line 71

def handle_request client, params
	client.write(Mongrel::Const::ERROR_404_RESPONSE)
end

#process(client) ⇒ Object

Copied from Mongrel



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dci/proxy/http.rb', line 20

def process client
	begin
		parser = Mongrel::HttpParser.new
		params = Mongrel::HttpParams.new
		request = nil
		data = client.readpartial(Mongrel::Const::CHUNK_SIZE)
		nparsed = 0

		while nparsed < data.length
			nparsed = parser.execute(params, data, nparsed)

			if parser.finished?
				if not params[Mongrel::Const::REQUEST_PATH]
					# it might be a dumbass full host request header
					uri = URI.parse(params[Const::REQUEST_URI])
					params[Const::REQUEST_PATH] = uri.path
				end

				raise "No REQUEST PATH" if not params[Mongrel::Const::REQUEST_PATH]
				handle_request client, params
			else
				# Parser is not done, queue up more data to read and continue parsing
				chunk = client.readpartial(Mongrel::Const::CHUNK_SIZE)
				break if !chunk or chunk.length == 0  # read failed, stop processing

				data << chunk
				if data.length >= Mongrel::Const::MAX_HEADER
					raise Mongrel::HttpParserError.new("HEADER is longer than allowed, aborting client early.")
				end
			end
		end
	rescue EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL,Errno::EBADF
		client.close rescue nil
	rescue Mongrel::HttpParserError => e
		log.warn "HTTP parse error, malformed request (#{params[Mongrel::Const::HTTP_X_FORWARDED_FOR] || client.peeraddr.last}): #{e.inspect}"
		log.warn "REQUEST DATA: #{data.inspect}\n---\nPARAMS: #{params.inspect}\n---\n"
	rescue Object => e
		log.warn "Read error: #{e.inspect}"
		log.warn e.backtrace.join("\n")
	ensure
		begin
			client.close
		rescue IOError
			# Already closed
		rescue Object => e
			log.warn "Client error: #{e.inspect}"
			log.warn e.backtrace.join("\n")
		end
	end
end

#runObject



11
12
13
14
15
16
17
# File 'lib/dci/proxy/http.rb', line 11

def run
	while s = @srv.accept
		Thread.new do
			process s
		end
	end
end

#write_headers(client, headers) ⇒ Object



79
80
81
# File 'lib/dci/proxy/http.rb', line 79

def write_headers client, headers
	headers.each { |k,v| client.write "#{k}: #{v}\r\n" }
end

#write_separator(client) ⇒ Object



83
84
85
# File 'lib/dci/proxy/http.rb', line 83

def write_separator client
	client.write "\r\n"
end

#write_status(client, status, reason) ⇒ Object



75
76
77
# File 'lib/dci/proxy/http.rb', line 75

def write_status client, status, reason
	client.write(Mongrel::Const::STATUS_FORMAT % [status, reason || Mongrel::HTTP_STATUS_CODES[status]])
end