Class: EventMachine::Protocols::HeaderAndContentProtocol

Inherits:
Connection
  • Object
show all
Includes:
LineText2
Defined in:
lib/protocols/header_and_content.rb

Overview

Originally, this subclassed LineAndTextProtocol, which in turn relies on BufferedTokenizer, which doesn’t gracefully handle the transitions between lines and binary text. Changed 13Sep08 by FCianfrocca.

Constant Summary collapse

ContentLengthPattern =
/Content-length:\s*(\d+)/i

Constants included from LineText2

LineText2::MaxBinaryLength, LineText2::MaxLineLength

Instance Attribute Summary

Attributes inherited from Connection

#signature

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LineText2

#receive_data, #receive_end_of_binary_data, #set_binary_mode, #set_delimiter, #set_line_mode, #set_text_mode, #unbind

Methods inherited from Connection

#associate_callback_target, #close_connection, #close_connection_after_writing, #comm_inactivity_timeout, #comm_inactivity_timeout=, #connection_completed, #detach, #error?, #get_outbound_data_size, #get_peer_cert, #get_peername, #get_pid, #get_sockname, #get_status, new, #post_init, #receive_data, #reconnect, #send_data, #send_datagram, #send_file_data, #set_comm_inactivity_timeout, #ssl_handshake_completed, #start_tls, #stream_file_data, #unbind

Constructor Details

#initialize(*args) ⇒ HeaderAndContentProtocol

Returns a new instance of HeaderAndContentProtocol.



41
42
43
44
# File 'lib/protocols/header_and_content.rb', line 41

def initialize *args
		super
		init_for_request
end

Class Method Details

.headers_2_hash(hdrs) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/protocols/header_and_content.rb', line 114

def headers_2_hash hdrs
    hash = {}
    hdrs.each {|h|
	if /\A([^\s:]+)\s*:\s*/ =~ h
	    tail = $'.dup
	    hash[ $1.downcase.gsub(/-/,"_").intern ] = tail
	end
    }
    hash
end

Instance Method Details

#headers_2_hash(hdrs) ⇒ Object

Basically a convenience method. We might create a subclass that does this automatically. But it’s such a performance killer.



109
110
111
# File 'lib/protocols/header_and_content.rb', line 109

def headers_2_hash hdrs
		self.class.headers_2_hash hdrs
end

#receive_binary_data(text) ⇒ Object



82
83
84
85
# File 'lib/protocols/header_and_content.rb', line 82

def receive_binary_data text
		@hc_content = text
		dispatch_request
end

#receive_line(line) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/protocols/header_and_content.rb', line 46

def receive_line line
		case @hc_mode
		when :discard_blanks
 unless line == ""
			@hc_mode = :headers
			receive_line line
 end
		when :headers
 if line == ""
			raise "unrecognized state" unless @hc_headers.length > 0
			if respond_to?(:receive_headers)
  receive_headers @hc_headers
			end
			# @hc_content_length will be nil, not 0, if there was no content-length header.
			if @hc_content_length.to_i > 0
  set_binary_mode @hc_content_length
			else
  dispatch_request
			end
 else
			@hc_headers << line
			if ContentLengthPattern =~ line
  # There are some attacks that rely on sending multiple content-length
  # headers. This is a crude protection, but needs to become tunable.
  raise "extraneous content-length header" if @hc_content_length
  @hc_content_length = $1.to_i
			end
			if @hc_headers.length == 1 and respond_to?(:receive_first_header_line)
  receive_first_header_line line
			end
 end
		else
 raise "internal error, unsupported mode"
		end
end