Class: Spider::HTTP::RackApplication

Inherits:
Object
  • Object
show all
Defined in:
lib/spiderfw/http/adapters/rack.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/spiderfw/http/adapters/rack.rb', line 75

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?
            end
            controller.call_after(path)
            controller_done = true
        rescue Exception => exc
            Spider.logger.error(exc)
            controller.ensure if controller
            controller = nil
        ensure
            begin
                if multithread
                    controller_response.server_output.send_headers unless controller_response.server_output.headers_sent?
                else
                    controller_response.prepare_headers
                    rack_response_hash[:status] = controller_response.status
                    rack_response_hash[:headers] = {}
                    controller_response.headers.each do |key, val|
                        if (val.is_a?(Array))
                            val.each{ |v| rack_response_hash[:headers][key] = v.to_s }
                        else
                            rack_response_hash[:headers][key] = val.to_s
                        end
                    end
                    w.rewind
                end
                Spider.request_finished
            ensure
                if multithread
                   begin
                       w.close
                   rescue
                   end
                   Spider.remove_thread(Thread.current)
                   Thread.exit 
                end
            end
        end
    end
    
    if multithread
        controllerThread = Thread.start(&run_block)
        t = Time.now
        while !controller_done && !controller_response.server_output.headers_sent? && (Time.now - t) < 60
            Thread.stop
        end
        if (Time.now - t) >= 60
            controllerThread.kill
        end
        Spider.add_thread(controllerThread) unless controller_done
    else
        run_block.call
    end

    return [rack_response_hash[:status], rack_response_hash[:headers], rack_response_hash[:body]]
end

#finalize_serverObject



179
180
181
# File 'lib/spiderfw/http/adapters/rack.rb', line 179

def finalize_server
    Spider.shutdown
end

#initialize_serverObject



175
176
177
# File 'lib/spiderfw/http/adapters/rack.rb', line 175

def initialize_server
    Spider.startup
end

#prepare_env(env) ⇒ Object



170
171
172
173
# File 'lib/spiderfw/http/adapters/rack.rb', line 170

def prepare_env(env)
    env['HTTP_CONTENT_TYPE'] = env['CONTENT_TYPE']
    return env
end