Sha256: b8388ec3138c426a0d1fc7c885e6e6f36c1741868292cf1b1234cd854d8c2d79

Contents?: true

Size: 815 Bytes

Versions: 1

Compression:

Stored size: 815 Bytes

Contents

require "erik_server/version"
require 'socket'

  module ErikServer

    def self.run
      server = TCPServer.new 1337

      loop do
        client = server.accept
        request = client.gets
        verb, path, protocol = request.split(" ")
        path = path.split('?')[0]
        path = "." + path

        if File.file?(path)
          body = File.open(path, "r"){|file| file.read}
          @status = "200 OK"
        else
          body = "File not found\n"
          @status = "404 Not Found"
        end

        response = "#{protocol} #{@status}\n"
        response += "Content-Type: text/html\n"
        response += "Content-Length: #{body.length}\n"
        response += "Connection: close\n\n"
        response += body

        client.puts response
        client.close
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
erik_server-0.1.0 lib/erik_server.rb