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

$: << File.join(File.dirname(__FILE__), '..', 'lib')

require 'net/socket'

# A HTTP server that returns the request, in the body of an HTML document.
class DumpServer < Net::Socket::TCP::ProtocolServer
  protocol do
    initial_state :request

    # Request.
    # [method] [url] HTTP/[version]
    state(:request, "\r\n") do |_, line|
      @method, @url, protocol = line.split(' ')
      @version = protocol.split('/').last

      next :header
    end

    # Headers.
    # [key]: [value]
    state(:header, "\r\n") do |_, line|
      # TODO: Define transitions separately?
      next :body if line.strip.empty?

      key, value = line.split(': ', 2)

      @headers ||= {}
      @headers[key] = value.strip

      next :header
    end

    # Body (everything that is left).
    # FIXME: If Content-Length is not specified and no EOF is received
    # (technically making the connection a stream), it will blow up
    # because @headers['Content-Length'] is nil, and nil.to_i makes
    # Ruby angry.
    state(:body, ->(buff) {buff.length == @headers['Content-Length'].to_i}) do |conn, body|
      # Start headers
      conn.write "HTTP/1.0 200 OK\r\n"
      conn.write "Server: some trashy Ruby httpd\r\n"
      conn.write "Content-Type: text/html\r\n"
      conn.write "Content-Length: 59\r\n"
      conn.write "\r\n"
      # End headers

      # Content
      conn.write "<html><body><pre>"
      conn.write "#{@method} #{@url} HTTP/#{@version}\r\n"
      conn.write @headers.map{|k, v| "#{k}: #{v}"}.join("\r\n")
      conn.write "</pre></body></html>"

      next :done
    end
  end
end

async  = ARGV[0] == '--async'
puts "Server listening #{async ? 'a' : ''}synchronously at 0.0.0.0:8000."

DumpServer.new('0.0.0.0', 8000).listen(async)
<commit_msg>Apparently the only bug is me writing code at 6am.<commit_after>#!/usr/bin/env ruby

$: << File.join(File.dirname(__FILE__), '..', 'lib')

require 'net/socket'

# A HTTP server that returns the request, in the body of an HTML document.
class DumpServer < Net::Socket::TCP::ProtocolServer
  protocol do
    initial_state :request

    # Request.
    # [method] [url] HTTP/[version]
    state(:request, "\r\n") do |_, line|
      @method, @url, protocol = line.split(' ')
      @version = protocol.split('/').last

      next :header
    end

    # Headers.
    # [key]: [value]
    state(:header, "\r\n") do |_, line|
      # TODO: Define transitions separately?
      next :body if line.strip.empty?

      key, value = line.split(': ', 2)

      @headers ||= {}
      @headers[key] = value.strip

      next :header
    end

    # Body (everything that is left).
    state(:body, ->(buff) {buff.length == @headers['Content-Length'].to_i}) do |conn, body|
      # Start headers
      conn.write "HTTP/1.0 200 OK\r\n"
      conn.write "Server: some trashy Ruby httpd\r\n"
      conn.write "Content-Type: text/html\r\n"
      conn.write "Content-Length: 59\r\n"
      conn.write "\r\n"
      # End headers

      # Content
      conn.write "<html><body><pre>"
      conn.write "#{@method} #{@url} HTTP/#{@version}\r\n"
      conn.write @headers.map{|k, v| "#{k}: #{v}"}.join("\r\n")
      conn.write "</pre></body></html>"

      next :done
    end
  end
end

async  = ARGV[0] == '--async'
puts "Server listening #{async ? 'a' : ''}synchronously at 0.0.0.0:8000."

DumpServer.new('0.0.0.0', 8000).listen(async)

{'fraction_non_alphanumeric': 0.17704051673517324, 'fraction_numerical': 0.014973576042278332, 'mean_word_length': 2.6283280085197016, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '646425', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1156, 'n_words': 309}