<commit_before>module BasicServer
  
  using HTTP
  using Base
  
  include("BasicServer/Parser.jl")
  
  function accept_handler(server::TcpSocket, status::Int32, app, debug)
    if status != 0
      uv_error("Error (" * string(status) * ")")
    end
    client = TcpSocket()
    err = accept(server, client)
    if err != 0
      uv_error("accept error: " * string(err))
      return
    end
    
    try
      # keepalive = true
      # while keepalive
        request = handle_request(client)
      
        if request.method == "POST" || request.method == "PUT"
          request.data = handle_data(request, client)
        end
        
        response, elapsed = @timed handle_response(request, app)
      
        Base.write(client, build_response(response))
        
        log(request, response, elapsed)
        
        # if request.headers["Connection"][1] == "keep-alive"
        #   keepalive = true
        # else
          # TODO: Make keepalive functional (requires putting a timeout on the
          # or else it will get stuck in a loop if a client drops
          # a connection).
          # keepalive = false
        # end
      # end#while keepalive
      
    catch e
      println(e)
      # TODO: Fix this so it can show a better output
      # Current output if error happens with no try-catch block:
      #   
      #   accept error: -1: resource temporarily unavailable (EAGAIN)
      #    in uv_error at stream.jl:470
      #    in accept_handler at /Users/dirk/.julia/HTTP/src/BasicServer.jl:15
      #    in anonymous at /Users/dirk/.julia/HTTP/src/BasicServer.jl:64
      #    in _uv_hook_connectioncb at stream.jl:200
      #    in event_loop at multi.jl:1392
      #    in bind at /Users/dirk/.julia/HTTP/src/BasicServer.jl:71
      #    in include_from_node1 at loading.jl:76
      #    in process_options at client.jl:259
      #    in _start at client.jl:322
    end
    
    Base.close(client)
  end#accept_handler
  
  function log(request, response, elapsed)
    # Sinatra's pretty log format:
    # 127.0.0.1 - - [29/Jan/2013 12:31:51] "GET / HTTP/1.1" 200 11 0.0006
    
    function format(e)
      s = int(floor(e))
      msec = int((e - floor(e)) * 1000)
      
      return string(s) * "." * lpad(string(msec), 3, "0")
    end
    
    println(request.method*" "*request.path*" "*string(response.status)*" "*format(elapsed))
  end
  
  function read_block(client)
    # Accept lines until you hit a double-newline
    raw = ""<commit_msg>Cleanup<commit_after>module BasicServer
  
  using HTTP
  using Base
  
  include("BasicServer/Parser.jl")
  
  function accept_handler(server::TcpSocket, status::Int32, app, debug)
    if status != 0
      uv_error("Error (" * string(status) * ")")
    end
    client = TcpSocket()
    err = accept(server, client)
    if err != 0
      uv_error("accept error: " * string(err))
      return
    end
    
    try
      # keepalive = true
      # while keepalive
        request = handle_request(client)
      
        if request.method == "POST" || request.method == "PUT"
          request.data = handle_data(request, client)
        end
        
        response, elapsed = @timed handle_response(request, app)
      
        Base.write(client, build_response(response))
        
        log(request, response, elapsed)
        
        # if request.headers["Connection"][1] == "keep-alive"
        #   keepalive = true
        # else
          # TODO: Make keepalive functional (requires putting a timeout on the
          # or else it will get stuck in a loop if a client drops
          # a connection).
          # keepalive = false
        # end
      # end#while keepalive
      
    catch e
      println(e)
      # TODO: Fix this so it can show a better output
      # Current output if error happens with no try-catch block:
      #   
      #   accept error: -1: resource temporarily unavailable (EAGAIN)
      #    in uv_error at stream.jl:470
      #    in accept_handler at /Users/dirk/.julia/HTTP/src/BasicServer.jl:15
      #    in anonymous at /Users/dirk/.julia/HTTP/src/BasicServer.jl:64
      #    in _uv_hook_connectioncb at stream.jl:200
      #    in event_loop at multi.jl:1392
      #    in bind at /Users/dirk/.julia/HTTP/src/BasicServer.jl:71
      #    in include_from_node1 at loading.jl:76
      #    in process_options at client.jl:259
      #    in _start at client.jl:322
    end
    
    Base.close(client)
  end#accept_handler
  
  function log(request, response, elapsed)
    # Sinatra's pretty log format:
    # 127.0.0.1 - - [29/Jan/2013 12:31:51] "GET / HTTP/1.1" 200 11 0.0006
    
    # Makes a float into a pretty "12.345" string
    function format(e)
      s = int(floor(e))
      msec = int((e - floor(e)) * 1000)
      return string(s) * "." * lpad(string(msec), 3, "0")
    end
    println(request.method*" "*request.path*" "*string(response.status)*" "*format(elapsed))
  end
  
  function read_block(client)
    # Accept lines until you hit a double-newline
    raw = ""
{'fraction_non_alphanumeric': 0.10702875399361023, 'fraction_numerical': 0.0277555910543131, 'mean_word_length': 2.1286695815115553, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4581822', 'n_tokens_mistral': 1703, 'n_tokens_neox': 1570, 'n_words': 420}