<commit_before>require 'spiderfw/controller/request'
require 'spiderfw/controller/http_controller'

module Spider; module HTTP
    
    class RackRequest < Spider::Request
        attr_accessor :rack_input
        
        
        def body(&proc)
            if block_given?
                @rack_input.each{ |buf| yield buf }
            else
                @rack_input
            end
        end
        
        
    end
    
    class RackIO < ControllerIO

        def initialize(response, controller_response, w, options={})
            @response = response
            @controller_response = controller_response
            @w = w
            @headers_sent = false
            @rack_thread = Thread.current
            @options = options
        end

        def write(msg)
            send_headers unless @headers_sent
            @w.write(msg)
        end

        def send_headers
            Spider::Logger.debug("---SENDING HEADERS----")
            @controller_response.prepare_headers
            @response[:status] = @controller_response.status
            @response[:headers] = {}
            @controller_response.headers.each do |key, val|
                if (val.is_a?(Array))
                    val.each{ |v| @response[:headers][key] = v.to_s }
                else
                    @response[:headers][key] = val.to_s
                end
            end
            @headers_sent = true
            @rack_thread.run if @options[:multithread]
        end

        def headers_sent?
            @headers_sent
        end
        
        def set_body_io(io)
            return super if headers_sent?
            begin
                @response[:body].close
            rescue => exc
            end
            @response[:body] = io
            send_headers
        end


        def flush
        end

    end
    
    class RackApplication

        def call(env)
            Spider.request_started
            env = prepare_env(env)
            controller_request = RackRequest.new(env)
            controller_request.server = RackApplication
            controller_request.rack_input = env['rack.input']
            path = env['PATH_INFO'].chomp('/')
            controller_request.action = path
            controller_request.request_time = DateTime.now

            w = nil
            controller_response = Spider::Response.new
            multithread = env['rack.multithread'] || Spider.conf.get('webserver.force_threads')
            if multithread
                r, w = IO.pipe
                rack_response_hash = {:body => r}
                controller_response.server_output = RackIO.new(rack_response_hash, controller_response, w, :multithread => true)
            else
                w = StringIO.new
                rack_response_hash = {:body => w}
                controller_response.server_output = RackIO.new(rack_response_hash, controller_response, w)
            end


            controller = nil
            controller_done = false

            run_block = lambda do
                begin
                    controller = ::Spider::HTTPController.new(controller_request, controller_response)
                    controller.extend(Spider::FirstResponder)
                    controller.call_before(path)
                    controller.execute(path)
                    if multithread
                        w.close 
                        controller_response.server_output.send_headers unless controller_response.server_output.headers_sent?<commit_msg>Modificato rack.rb per installazione windows, multithread a false<commit_after>require 'spiderfw/controller/request'
require 'spiderfw/controller/http_controller'

module Spider; module HTTP
    
    class RackRequest < Spider::Request
        attr_accessor :rack_input
        
        
        def body(&proc)
            if block_given?
                @rack_input.each{ |buf| yield buf }
            else
                @rack_input
            end
        end
        
        
    end
    
    class RackIO < ControllerIO

        def initialize(response, controller_response, w, options={})
            @response = response
            @controller_response = controller_response
            @w = w
            @headers_sent = false
            @rack_thread = Thread.current
            @options = options
        end

        def write(msg)
            send_headers unless @headers_sent
            @w.write(msg)
        end

        def send_headers
            Spider::Logger.debug("---SENDING HEADERS----")
            @controller_response.prepare_headers
            @response[:status] = @controller_response.status
            @response[:headers] = {}
            @controller_response.headers.each do |key, val|
                if (val.is_a?(Array))
                    val.each{ |v| @response[:headers][key] = v.to_s }
                else
                    @response[:headers][key] = val.to_s
                end
            end
            @headers_sent = true
            @rack_thread.run if @options[:multithread]
        end

        def headers_sent?
            @headers_sent
        end
        
        def set_body_io(io)
            return super if headers_sent?
            begin
                @response[:body].close
            rescue => exc
            end
            @response[:body] = io
            send_headers
        end


        def flush
        end

    end
    
    class RackApplication

        def call(env)
            Spider.request_started
            env = prepare_env(env)
            controller_request = RackRequest.new(env)
            controller_request.server = RackApplication
            controller_request.rack_input = env['rack.input']
            path = env['PATH_INFO'].chomp('/')
            controller_request.action = path
            controller_request.request_time = DateTime.now

            w = nil
            controller_response = Spider::Response.new
            if RUBY_PLATFORM =~ /mingw32/
                multithread = false
            else    
                multithread = env['rack.multithread'] || Spider.conf.get('webserver.force_threads')
            end
            if multithread
                r, w = IO.pipe
                rack_response_hash = {:body => r}
                controller_response.server_output = RackIO.new(rack_response_hash, controller_response, w, :multithread => true)
            else
                w = StringIO.new
                rack_response_hash = {:body => w}
                controller_response.server_output = RackIO.new(rack_response_hash, controller_response, w)
            end


            controller = nil
            controller_done = false

            run_block = lambda do
                begin
                    controller = ::Spider::HTTPController.new(controller_request, controller_response)
                    controller.extend(Spider::FirstResponder)
                    controller.call_before(path)
                    controller.execute(path)
                    if multithread
                        w.close 
                        controller_response.server_output.send_headers unless controller_response.server_output.headers_sent?
{'fraction_non_alphanumeric': 0.07426501323672843, 'fraction_numerical': 0.00027866796711717987, 'mean_word_length': 1.4803040774015204, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '4493255', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1741, 'n_words': 367}