class EmmyHttp::Server::Server

Attributes

app[RW]
config[RW]
connections[RW]
parser[RW]

Public Class Methods

new(config, app=nil) click to toggle source
# File lib/emmy_http/server/server.rb, line 14
def initialize(config, app=nil)
  @config = config
  @app = app
  @connections = []

  if app
    on :request do |req, res|
      req.prepare_env
      res.status, res.headers, res.body = app.call(req.env)
    end
  else
    on :request do |req, res|
      error!('No app found', req, res)
    end
  end

  on :error do |reason, req, res|
    if res.headers_sent?
      # big problems
    else
      body = reason ? reason.to_s : 'An error happened'
      res.update_attributes(
        status: 500,
        headers: {
          'Content-Type'   => 'text/plain',
          'Content-Length' => body.bytesize
        },
        body: body
      )
    end
  end
end

Public Instance Methods

initialize_connection(conn) click to toggle source

initialize a new connection

# File lib/emmy_http/server/server.rb, line 48
def initialize_connection(conn)
  conn.comm_inactivity_timeout = config.timeout.to_i
  conn.start_tls(config.ssl ? config.ssl.serializable_hash : {}) if ssl?
  # Create a new request
  req = new_request(conn)
  # Connected event
  connect!(req, conn)
end
new_request(conn) click to toggle source
# File lib/emmy_http/server/server.rb, line 57
def new_request(conn)
  req = Server::Request.new(
    #decoder: config.decoding ? new_decoder_by_encoding(headers['Content-Encoding']) : nil
  )

  req.on :complete do |req, conn|
    res = Server::Response.new
    res.keep_alive = req.keep_alive?
    res.attach(conn)

    EmmyMachine.async do
      # Process request event
      begin
        request!(req, res)
      rescue StandardError => err
        error!(err, req, res)
      end

      # Send response
      res.write_all
      # Close/Dettach response
      res.close
      # Disconnect after response
      if res.keep_alive
        new_request(conn)
      else
        conn.close_connection_after_writing
      end
    end
  end

  req.on :error do |reason, req, conn|
    error!(reason)
    conn.close_connection_after_writing rescue nil
  end

  req.attach(conn)
  req
end
ssl?() click to toggle source
# File lib/emmy_http/server/server.rb, line 97
def ssl?
  config.ssl || config.url.scheme == 'https' || config.url.port == 443
end
to_a() click to toggle source

def stop(reason=nil)

@stop_reason ||= reason
connection.close_connection if connection

end

# File lib/emmy_http/server/server.rb, line 106
def to_a
  [config.url, EmmyMachine::Connection, method(:initialize_connection)]
end