class RequestHandler < EM::P::HeaderAndContentProtocol def receive_request headers, content p [:request, headers, content] end end EM.run{ EM.start_server 'localhost', 80, RequestHandler }
# File lib/em/protocols/header_and_content.rb, line 124 124: def headers_2_hash hdrs 125: hash = {} 126: hdrs.each {|h| 127: if /\A([^\s:]+)\s*:\s*/ =~ h 128: tail = $'.dup 129: hash[ $1.downcase.gsub(/-/,"_").intern ] = tail 130: end 131: } 132: hash 133: end
Basically a convenience method. We might create a subclass that does this automatically. But it’s such a performance killer.
# File lib/em/protocols/header_and_content.rb, line 119 119: def headers_2_hash hdrs 120: self.class.headers_2_hash hdrs 121: end
# File lib/em/protocols/header_and_content.rb, line 92 92: def receive_binary_data text 93: @hc_content = text 94: dispatch_request 95: end
# File lib/em/protocols/header_and_content.rb, line 56 56: def receive_line line 57: case @hc_mode 58: when :discard_blanks 59: unless line == "" 60: @hc_mode = :headers 61: receive_line line 62: end 63: when :headers 64: if line == "" 65: raise "unrecognized state" unless @hc_headers.length > 0 66: if respond_to?(:receive_headers) 67: receive_headers @hc_headers 68: end 69: # @hc_content_length will be nil, not 0, if there was no content-length header. 70: if @hc_content_length.to_i > 0 71: set_binary_mode @hc_content_length 72: else 73: dispatch_request 74: end 75: else 76: @hc_headers << line 77: if ContentLengthPattern =~ line 78: # There are some attacks that rely on sending multiple content-length 79: # headers. This is a crude protection, but needs to become tunable. 80: raise "extraneous content-length header" if @hc_content_length 81: @hc_content_length = $1.to_i 82: end 83: if @hc_headers.length == 1 and respond_to?(:receive_first_header_line) 84: receive_first_header_line line 85: end 86: end 87: else 88: raise "internal error, unsupported mode" 89: end 90: end
# File lib/em/protocols/header_and_content.rb, line 97 97: def dispatch_request 98: if respond_to?(:receive_request) 99: receive_request @hc_headers, @hc_content 100: end 101: init_for_request 102: end
# File lib/em/protocols/header_and_content.rb, line 105 105: def init_for_request 106: @hc_mode = :discard_blanks 107: @hc_headers = [] 108: # originally was @hc_headers ||= []; @hc_headers.clear to get a performance 109: # boost, but it's counterproductive because a subclassed handler will have to 110: # call dup to use the header array we pass in receive_headers. 111: 112: @hc_content_length = nil 113: @hc_content = "" 114: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.