<commit_before>module RackDAV
  
  # TODO: Update this to use nokogiri instead of slow slow rexml
  class Controller
    include RackDAV::HTTPStatus
    
    attr_reader :request, :response, :resource
    
    def initialize(request, response, options)
      @request = request
      @response = response
      @options = options
      @resource = resource_class.new(request.env['REQUEST_PATH'], implied_path, @options)
      authenticate
      raise Forbidden if request.path_info.include?('..')
    end
    
    def url_escape(s)
      s.gsub(/([^\/a-zA-Z0-9_.-]+)/n) do
        '%' + $1.unpack('H2' * $1.size).join('%').upcase
      end.tr(' ', '+')
    end

    def url_unescape(s)
      s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
        [$1.delete('%')].pack('H*')
      end
    end    
    
    def options
      response["Allow"] = 'OPTIONS,HEAD,GET,PUT,POST,DELETE,PROPFIND,PROPPATCH,MKCOL,COPY,MOVE,LOCK,UNLOCK'
      response["Dav"] = "2"
      response["Ms-Author-Via"] = "DAV"
    end
    
    def head
      raise NotFound if not resource.exist?
      response['Etag'] = resource.etag
      response['Content-Type'] = resource.content_type
      response['Last-Modified'] = resource.last_modified.httpdate
    end
    
    def get
      raise NotFound if not resource.exist?
      map_exceptions do
        resource.get(request, response)
      end
      if(response.status == 200 && !resource.collection?)
        response['Etag'] = resource.etag
        response['Content-Type'] = resource.content_type
        response['Content-Length'] = resource.content_length.to_s
        response['Last-Modified'] = resource.last_modified.httpdate
      end
      response
    end

    def put
      raise Forbidden if resource.collection?
      map_exceptions do
        resource.put(request, response)
      end
    end

    def post
      map_exceptions do
        resource.post(request, response)
      end
    end

    def delete
      raise NotFound unless resource.exist?
      map_exceptions do
        resource.delete
      end
      response.status = Success
    end
    
    def mkcol
      map_exceptions do
        resource.make_collection<commit_msg>Use URI library escaping. Use correct name.<commit_after>module RackDAV
  
  # TODO: Update this to use nokogiri instead of slow slow rexml
  class Controller
    include RackDAV::HTTPStatus
    
    attr_reader :request, :response, :resource
    
    def initialize(request, response, options)
      @request = request
      @response = response
      @options = options
      @resource = resource_class.new(request.env['REQUEST_PATH'], implied_path, @options)
      authenticate
      raise Forbidden if request.path_info.include?('..')
    end
    
    def url_escape(s)
      URI.escape(s)
#       s.gsub(/([^\/a-zA-Z0-9_.-]+)/n) do
#         '%' + $1.unpack('H2' * $1.size).join('%').upcase
#       end.tr(' ', '+')
    end

    def url_unescape(s)
      URI.unescape(s)
#       s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
#         [$1.delete('%')].pack('H*')
#       end
    end    
    
    def options
      response["Allow"] = 'OPTIONS,HEAD,GET,PUT,POST,DELETE,PROPFIND,PROPPATCH,MKCOL,COPY,MOVE,LOCK,UNLOCK'
      response["Dav"] = "2"
      response["Ms-Author-Via"] = "DAV"
    end
    
    def head
      raise NotFound if not resource.exist?
      response['Etag'] = resource.etag
      response['Content-Type'] = resource.content_type
      response['Last-Modified'] = resource.last_modified.httpdate
    end
    
    def get
      raise NotFound if not resource.exist?
      map_exceptions do
        resource.get(request, response)
      end
      if(response.status == 200 && !resource.collection?)
        response['Etag'] = resource.etag
        response['Content-Type'] = resource.content_type
        response['Content-Length'] = resource.content_length.to_s
        response['Last-Modified'] = resource.last_modified.httpdate
      end
      response
    end

    def put
      raise Forbidden if resource.collection?
      map_exceptions do
        resource.put(request, response)
      end
    end

    def post
      map_exceptions do
        resource.post(request, response)
      end
    end

    def delete
      raise NotFound unless resource.exist?
      map_exceptions do
        resource.delete
      end
      response.status = OK
    end
    
    def mkcol
      map_exceptions do
        resource.make_collection
{'fraction_non_alphanumeric': 0.11961182577296321, 'fraction_numerical': 0.005867749943579328, 'mean_word_length': 2.3499622071050643, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4008451', 'n_tokens_mistral': 1400, 'n_tokens_neox': 1381, 'n_words': 295}