class Rsws_client

Class that represents a client connecting to the web server

Public Class Methods

new(init_server) click to toggle source
# File lib/rsws.rb, line 9
def initialize(init_server)
  @server = init_server
  @total = String.new

  @requestHeaders = Hash.new
  @postData = Hash.new

  # Because we want to send all the data at once, setup a buffer so we can
  # simulate using methods like 'puts' without that sending the data before
  # we want to do so
  @buffer = String.new
  @headers = String.new
  
  # Parse the first part of the request, this should look something like
  # `GET / HTTP/1.1`
  # So we can simply split it among spaces to get the information we need
  request = @server.gets.split(" ")
  @method = request[0]
  @path = request[1]

  line = " "
  while line != "\r\n"
    line = @server.gets
    if line.include?(":") then
      headerData = line.split(":")
      @requestHeaders[headerData[0].chomp] = headerData[1].chomp
    end
  end
  
  # One last step if we're dealing with POST data
  if @method == "POST" then
    # Try to get the content lenght, otherwise we will hope the client
    # will send us an empty line or something to tell us that we are done reading
    if @requestHeaders["Content-Length"] then
      leftToRead = @requestHeaders["Content-Length"].to_i
    end
  
    vars = @server.read(leftToRead)
    vars_split = vars.split("&")
    vars_split.each do | var |
      data = var.split("=")
      @postData[data[0].chomp] = data[1].chomp
    end
  end

end

Public Instance Methods

accept() click to toggle source
# File lib/rsws.rb, line 75
def accept

  # Accept simply sends a response that we are ok, really all this is doing
  # is sending the status 200, you could easily do this as well with
  # `status("200 OK")`
  @headers << "HTTP/1.1 200 OK\r\nServer: Rsws/0.1.0\r\n"
  @accepted = true
end
content_type(type) click to toggle source

Allow a custom content type to be set

# File lib/rsws.rb, line 91
def content_type(type)
  @headers << "Content-Type: #{type}\r\n"
  @content_type = true
end
get(var) click to toggle source
# File lib/rsws.rb, line 68
def get(var)
  if @method != "POST"
    puts "[Rsws] ERROR: Cannot call get unless POST is the current method"
    return 
  end
  return @postData[var]
end
headers() click to toggle source
# File lib/rsws.rb, line 64
def headers
  return @requestHeaders
end
method() click to toggle source
# File lib/rsws.rb, line 60
def method
  return @method
end
not_found() click to toggle source

Basically just the anti of accept, allow for 404 messages to be sent

# File lib/rsws.rb, line 97
def not_found
  content_type("text/plain")
  @headers << "HTTP/1.1 404 Not Found\r\nServer: Rsws/0.1.0\r\n"
  puts "#{@method} #{@path} 404 Not Found"
  send
end
path() click to toggle source

Create methods to get our path, method, post data, and request headers

# File lib/rsws.rb, line 57
def path
  return @path
end
print(msg) click to toggle source
println(msg) click to toggle source
# File lib/rsws.rb, line 118
def println(msg)
  @buffer << "#{msg}\n"
end
puts(msg) click to toggle source

The following print data to the buffer

# File lib/rsws.rb, line 112
def puts(msg)
  @buffer << "#{msg}\n"
end
send() click to toggle source

When we're finally done, send all the data to the client

# File lib/rsws.rb, line 123
def send
  if @accepted then
    # Calculate content length before sending
  
    @headers << "Content-Length: #{@buffer.length}\r\n"
    
    if !@content_type then
      @headers << "Content-Type: text/html\r\n"
    end

    # Send our data and close the connection
    @server.puts @headers
    @server.puts "\r\n"
    @server.puts @buffer
    @server.close
  else
    puts "[Rsws] ERROR: Trying to send response without first accepting it"
  end
end
send_file(path) click to toggle source

Read and send in a file

# File lib/rsws.rb, line 105
def send_file(path)

  @buffer = File.read(path)
  send
end
status(stat) click to toggle source

Allow a cutsom status to be sent if the user doesn't want to send 404 or 200

# File lib/rsws.rb, line 85
def status(stat)
  @headers << "HTTP/1.1 #{stat}\r\n"
  @accepted = true
end