Class: EventMachine::Protocols::HeaderAndContentProtocol

### Home Classes Methods

### In Files

- lib/em/protocols/header_and_content.rb

### Parent

EventMachine::Connection

### Methods

- ::headers_2_hash
- ::new
- #headers_2_hash
- #receive_binary_data
- #receive_line

### Included Modules

- EventMachine::Protocols::LineText2

### Files

- ChangeLog
- DEFERRABLES
- EPOLL
- INSTALL
- KEYBOARD
- LEGAL
- LIGHTWEIGHT_CONCURRENCY
- PURE_RUBY
- RELEASE_NOTES
- SMTP
- SPAWNED_PROCESSES
- TODO

### Class/Module Index

Quicksearch

- EventMachine
- EventMachine
- EventMachine::Channel
- EventMachine::Completion
- EventMachine::Connection
- EventMachine::DNS
- EventMachine::DNS::Request
- EventMachine::DNS::RequestIdAlreadyUsed
- EventMachine::DNS::Resolver
- EventMachine::DNS::Socket
- EventMachine::DatagramObject
- EventMachine::DefaultDeferrable
- EventMachine::Deferrable
- EventMachine::DeferrableChildProcess
- EventMachine::Error
- EventMachine::EvmaKeyboard
- EventMachine::EvmaTCPClient
- EventMachine::EvmaTCPServer
- EventMachine::EvmaUDPSocket
- EventMachine::EvmaUNIXClient
- EventMachine::EvmaUNIXServer
- EventMachine::FileNotFoundException
- EventMachine::FileStreamer
- EventMachine::FileWatch
- EventMachine::Iterator
- EventMachine::LoopbreakReader
- EventMachine::PeriodicTimer
- EventMachine::Pool
- EventMachine::ProcessWatch
- EventMachine::Protocols
- EventMachine::Protocols
- EventMachine::Protocols::HeaderAndContentProtocol
- EventMachine::Protocols::HttpClient
- EventMachine::Protocols::HttpClient2
- EventMachine::Protocols::HttpClient2::Request
- EventMachine::Protocols::LineAndTextProtocol
- EventMachine::Protocols::LineProtocol
- EventMachine::Protocols::LineText2
- EventMachine::Protocols::Memcache
- EventMachine::Protocols::Memcache::ParserError
- EventMachine::Protocols::ObjectProtocol
- EventMachine::Protocols::Postgres3
- EventMachine::Protocols::SASLauth
- EventMachine::Protocols::SASLauthclient
- EventMachine::Protocols::SmtpClient
- EventMachine::Protocols::SmtpServer
- EventMachine::Protocols::Socks4
- EventMachine::Protocols::Stomp
- EventMachine::Protocols::Stomp::Message
- EventMachine::Protocols::TcpConnectTester
- EventMachine::Queue
- EventMachine::Reactor
- EventMachine::Selectable
- EventMachine::SpawnedProcess
- EventMachine::StreamObject
- EventMachine::SystemCmd
- EventMachine::ThreadedResource
- EventMachine::TickLoop
- EventMachine::Timer
- EventMachine::UuidGenerator
- EventMachine::YieldBlockFromSpawnedProcess
- Object
- Object::EM
- Object::P
- BufferedTokenizer
- IO
- StringIO
- TestConnection

No matching classes.

# EventMachine::Protocols::HeaderAndContentProtocol

### Usage

    class RequestHandler < EM::P::HeaderAndContentProtocol
      def receive_request headers, content
        p [:request, headers, content]
      end
    end

    EM.run{
      EM.start_server 'localhost', 80, RequestHandler
    }

### Constants

ContentLengthPattern

### Public Class Methods

headers_2_hash(hdrs) click to toggle source

    # File lib/em/protocols/header_and_content.rb, line 124
    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

new(*args) click to toggle source

    # File lib/em/protocols/header_and_content.rb, line 51
    def initialize *args
      super
      init_for_request
    end

### Public Instance Methods

headers_2_hash(hdrs) click to toggle source

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
    def headers_2_hash hdrs
      self.class.headers_2_hash hdrs
    end

receive_binary_data(text) click to toggle source

    # File lib/em/protocols/header_and_content.rb, line 92
    def receive_binary_data text
      @hc_content = text
      dispatch_request
    end

receive_line(line) click to toggle source

    # File lib/em/protocols/header_and_content.rb, line 56
    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

[Validate]

Generated with the Darkfish Rdoc Generator 2.