In Files

Parent

Class/Module Index [+]

Quicksearch

Accept

a library to accept connections as a server, and send back what it received on request.

Constants

URI_REXEGP

Attributes

server[RW]
session[RW]
opts[R]

Public Class Methods

new(args = {}) click to toggle source
# File spec/accept.rb, line 14
def initialize(args = {})
  opts = { :port => 9292, :debug => false }
  opts.update(args)

  puts "Starting up server on port #{opts[:port]} ..."
  @opts           = opts
  @server         = TCPServer.new(opts[:port])
  @@input_history = []
  @handle         = Thread.start do
    while (@session = server.accept)
      Thread.start do
        # puts "log: Connection from #{session.peeraddr[2]} at #{session.peeraddr[3]}"
        # session.puts "Server: Connection from #{session.peeraddr[2]}\n"
        handle_input
        session.close
      end
    end
  end
end

Public Instance Methods

clear() click to toggle source

clear history

# File spec/accept.rb, line 80
def clear
  @@input_history.clear
end
close() click to toggle source
# File spec/accept.rb, line 75
def close
  session = nil
  server.close
end
handle_input() click to toggle source
# File spec/accept.rb, line 40
def handle_input
  input = session.gets
  if input
    puts "received: #{input.inspect}" if opts[:debug]
    case input
    when /clear/
      clear
    when /history/
      session.puts input_history.to_json
    when /exit/
      begin
        close
      rescue Exception
      end
      return
    when /^\s*(GET|POST|PUT|DELETE)\s+([^ ]*)\s+(.*)$/
      @@input_history << parse_input(input)
    else
      @@input_history << input.chomp
    end
  end
end
input_history() click to toggle source
# File spec/accept.rb, line 33
def input_history
  @@input_history
end
parse_input(input) click to toggle source
# File spec/accept.rb, line 62
def parse_input(input)
  data = {}
  data[:raw] = input.chomp
  (method,uri,http) = input.scan(/^\s*(\w+)\s+([^ ]*)\s+(.*)$/).flatten

  data[:method] = method
  data[:http] = http.chomp
  data[:uri] = uri
  u = URI(uri)
  data[:path] = u.path
  data[:query] = CGI.parse(u.query)
  return data
end
wait() click to toggle source
# File spec/accept.rb, line 36
def wait
  @handle.join
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.