<commit_before>#!/usr/bin/env ruby

require 'socket'

@serv = TCPServer.new 'localhost', 9999

def route(client, request, resource, params)
  case request
  when "GET"
    if File.exists? (Dir.pwd+resource)
      page = File.open(Dir.pwd+resource)
      content_type = mime_type(resource)
      content_length = page.size
      client.print "HTTP/1.1 200 OK\r\n"
      client.print "Content-Length: #{content_length}\r\n"
      client.print "Content-Type: #{content_type}\r\n" if content_type != nil
      client.print current_time
      client.print "\r\n"
      send_page(client, page, content_length)
    else
      client.print"HTTP/1.1 404 NOT FOUND\r\n"
      client.print current_time
      client.print "\r\n"
    end
    client.close
  #when "POST"
  #when "PUT"
  #when "DELETE"
  end
end

def listen(serv)
  loop do
    Thread.fork(serv.accept) { |client|
      request, resource, version = client.readline.split(" ")
      params = []
      until params.last == "\r\n"
        line = client.gets
        params << line
      end
      route(client, request, resource, params)
      client.close
    }
  end
end

def current_time
  d = Time.new.strftime("%a,%e %b %Y %H:%M:%S %Z")
  return "Date: #{d}\r\n"
end

@mimes = {}
def load_mimes
  f = File.open("/etc/mime.types", "r")
  mimes = f.readlines
  mimes.each {|mime|
    mime_type, *extensions = mime.split
    extensions.each { |ext|
      @mimes[ext] = mime_type
    }
  }<commit_msg>Hashed headers, add method for http status.<commit_after>#!/usr/bin/env ruby

require 'socket'

@serv = TCPServer.new 'localhost', 9999

def route(client, request, resource, params)
  case request
  when "GET"
    if File.exists? (Dir.pwd+resource)
      page = File.open(Dir.pwd+resource)
      headers = get_headers(page)
      send_headers(client, headers)
      send_page(client, page, page.size)
    else
      send_status(404, client)
    end
    client.close
  #when "POST"
  #when "PUT"
  #when "DELETE"
  end
end

def send_status(code, client)
  case code
  when 200
    client.print "HTTP/1.1 200 OK\r\n"
  when 404
    client.print "HTTP/1.1 404 NOT FOUND\r\n"
    client.print "Date: #{current_time}\r\n"
    client.print "\r\n"
  end
end

def get_headers(page)
  headers = {
    "Date" => current_time,
    "Content Type" => mime_type(page.path),
    "Content Length" => page.size
  }
  return headers
end

def send_headers(client, headers)
  send_status(200, client)
  headers.each { |type, value|
    client.print "#{type}: #{value}\r\n"
  }
  client.print "\r\n"
end

def listen(serv)
  loop do
    Thread.fork(serv.accept) { |client|
      request, resource, version = client.readline.split(" ")
      params = []
      until params.last == "\r\n"
        line = client.gets
        params << line
      end
      route(client, request, resource, params)
      client.close
    }
  end
end

def current_time
  Time.new.strftime("%a, %e %b %Y %H:%M:%S %Z")
end

@mimes = {}
def load_mimes
  f = File.open("/etc/mime.types", "r")
  mimes = f.readlines
  mimes.each {|mime|
    mime_type, *extensions = mime.split
    extensions.each { |ext|
      @mimes[ext] = mime_type
    }
  }
{'fraction_non_alphanumeric': 0.13949793454083254, 'fraction_numerical': 0.01271051795360661, 'mean_word_length': 2.703529411764706, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1236922', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1132, 'n_words': 273}