Class: InternetHakai::RequestAction

Inherits:
BaseAction show all
Defined in:
lib/internethakai/action.rb

Overview

httpリクエストを行なうアクション

Constant Summary collapse

ERROR_MAX =
3
REDIRECT_MAX =
10

Instance Attribute Summary

Attributes inherited from BaseAction

#performance_id

Instance Method Summary collapse

Methods inherited from BaseAction

#dispatch_next, #has_on_complete?, #quit, #set_on_complete

Constructor Details

#initialize(opt, scenario) ⇒ RequestAction

attr_accessor :client_queue



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/internethakai/action.rb', line 8

def initialize opt, scenario
    super
    if @opt.has_key? "scan"
        @opt["scan"] = Util::convert_encoding(@opt["scan"], @opt)
    end
    @logger = BaseHandler::get_handler(@opt["logger"])
    @logging = @opt['log_level'] >= 3
    if ! @logging
        #メソッド書き換え!
        def log *args
        end
        def http_request( method, path, bodystring = nil)
            if @redirect > REDIRECT_MAX
                return on_error('too many redirect')
            end
            path = @client_handler.handle_url(@http_client, path, @opt)
            bodystring = @client_handler.handle_body(@http_client, bodystring, @opt) if bodystring

            # リクエスト処理
            @http_client.request_send(method, path, bodystring)
        end
    end
    if @opt['rev']
        @rev = true
        @task_queue = BaseHandler::get_handler('TaskQueue')
        def call_on_complete response
            @task_queue.add(@on_complete_method)
        end
        def on_free
            if @http_client
                @http_client.release
                @http_client = nil
            end
        end
    else
        def call_on_complete response
            on_complete
        end
        def on_free
        end
    end
    @register = BaseHandler::get_handler(@opt["response_handler"])
    clcls = BaseHandler::get_class(@opt["client_handler"])
    @client_handler = clcls::new(@scenario)
    @method_run = method(:run)
    #@call_on_complete_method = method(:call_on_complete)
    @on_complete_method = method(:on_complete)
    @on_error_method = method(:on_error)
    @ruby19 = RUBY_VERSION >= "1.9"
end

Instance Method Details

#check_scan(body, scan) ⇒ Object



268
269
270
271
272
273
274
275
# File 'lib/internethakai/action.rb', line 268

def check_scan body, scan
    unless body.include? scan
        @debug_msgs << "SCAN:::false:::"+scan
        @errorcount += 1
    else
        @debug_msgs << "SCAN:::true"
    end
end

#check_size(body, size) ⇒ Object



260
261
262
263
264
265
266
267
# File 'lib/internethakai/action.rb', line 260

def check_size body, size
    unless body.size > size
        @debug_msgs << "SIZE_CHECK:::false:::"+body.size.to_s
        @errorcount += 1
    else
        @debug_msgs << "SIZE_CHECK:::true"
    end
end

#freeObject



103
104
105
106
107
108
# File 'lib/internethakai/action.rb', line 103

def free
    on_free
    @scenario.next
    #@_on_complete_calback.call if @_on_complete_calback
    @scenario_handler.quit_action(@scenario_id)
end

#get_clientObject



61
62
63
64
65
66
# File 'lib/internethakai/action.rb', line 61

def get_client
    client = @http_client
    client.reconnect
    client.set_callback(@on_complete_method, @on_error_method)
    client
end

#get_defaultObject



75
76
77
78
79
# File 'lib/internethakai/action.rb', line 75

def get_default
    {
        "method" => "GET"
    }
end

#http_request(method, path, bodystring = nil) ⇒ Object

http リクエスト発行 path: urlのpath body: postの場合のbody



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/internethakai/action.rb', line 115

def http_request( method, path, bodystring = nil)
    if @redirect > REDIRECT_MAX
        return on_error('too many redirect')
    end
    url = path
    url = @client_handler.handle_url(@http_client, url, @opt)
    #p [self.object_id, self.performance_id, url]
    #bodystring = @client_handler.handle_body(@http_client, bodystring, @opt) unless bodystring.nil?
    bodystring = @client_handler.handle_body(@http_client, bodystring, @opt) if bodystring

    log("URL:::", url)
    log "BODY:::", bodystring.to_s
    # リクエスト処理
    @http_client.request_send(method, url, bodystring)
end

#initObject



96
97
98
99
100
# File 'lib/internethakai/action.rb', line 96

def init
    @redirect = 0
    @debug_msgs = []
    @errorcount = 0
end

#log(*args) ⇒ Object



109
110
111
# File 'lib/internethakai/action.rb', line 109

def log *args
    @debug_msgs << args.join
end

#mkclientObject



67
68
69
70
71
72
73
74
# File 'lib/internethakai/action.rb', line 67

def mkclient
    #puts "error: run out of http client: create"
    clientcls = @opt['client_class_obj']
    cl = clientcls::create2(@opt['host'], @opt['port'])
    cl.timeout = @opt['timeout']
    cl.set_headers(@opt['header'])
    return cl
end

#on_complete(response) ⇒ Object

リクエスト完了時の処理



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/internethakai/action.rb', line 160

def on_complete response
    if response.nil? #レスポンスが空の場合。時間だけ記録
        @http_client.release
        @http_client = nil

        log "CONTENT_LENGTH:::", 0
        @register.regist(@register_key, 0, response, @errorcount)
        free
        return
    end

    if (response.status / 100 == 3)
        #リダイレクトの場合
        begin
            ok = false
            url = response.location
            tmp = url.size
            idx = url.index('/', 8) || url.size
            path = url[idx, tmp ]
            path = '/'+path if path[0,1]!='/'
            @logger.run(response.body, 4) if response && @errorcount>0 && (response.content_type.include?('text') || response.content_type.include?('xml') || response.content_type.include?('json'))
            on_redirect path
            @redirect += 1
            @redirect_url = path
            #リダイレクトの場合は次のリクエストへ
            @opt['url'] = url
            log "REDIRECT:::", url
            log "++++++++++++++++++++++++++++++++++++++\n"
            @logger.run(@debug_msgs.join("\n")+"\n",3)
            @debug_msgs = []

            @logger.run("\r\n+++++++++++++++++++++++++++++++\n", 4)
            @rtime = response.time unless response.nil?
            @register.regist(@register_key, @rtime, response, @errorcount)
            @scenario.set_cookie(response.cookie)
            #@response = nil
            if @opt['follow_redirect']
                @register_key = @redirect_url.gsub( /\?.*$/, '' ).gsub( /[#;].*/, '' )
                @http_client.reconnect
                ok = true
                http_request(GET, @redirect_url, nil)
                return
            else
                #リダイレクトリクエストしない / freen呼ぶ
                free
                return
            end
        rescue
            raise
            free unless ok
            return
        end
    end

    begin
        #puts "on complete1: #{Time::now.usec/1000.to_f}"
        @http_client.release
        @http_client = nil
        log "STATUS_CODE:::",response.status
        size = 0
        size = response.body.size if response.body
        log "CONTENT_LENGTH:::",size
        if response.status != 200
            if response.content_type != 'application/x-shockwave-flash'
                log "RESPONSE:::", response.body
            end
        end
        @rtime = response.time
        @scenario.set_cookie(response.cookie)
        log "RESPONSE_TIME:::",@rtime
        #puts "on complete1.1: #{Time::now.usec/1000.to_f}"

        response.body.force_encoding(@opt['encoding']) if @ruby19 and (response.content_type.include?('text') || response.content_type.include?('xml') || response.content_type.include?('json'))
        if(@opt["scan"])
            check_scan response.body, @opt["scan"]
        end
        if(@opt["assert_size"])
            check_size response.body, @opt["assert_size"]
        end
        on_response(response)

        #puts "on complete2.1: #{Time::now.usec/1000.to_f}"
        @register.regist(@register_key, @rtime, response, @errorcount)
        #puts "on complete2: #{Time::now.usec/1000.to_f}"

        @debug_msgs << "++++++++++++++++++++++++++++++++++++++\n"
        @logger.run(@debug_msgs.join("\n")+"\n",3)
        @debug_msgs = []
        @logger.run(response.body, 4) if response && (response.content_type.include?('text') || response.content_type.include?('xml') || response.content_type.include?('json'))
        @logger.run("\r\n+++++++++++++++++++++++++++++++\n", 4)
    ensure
        free #絶対freeを呼ぶ!
    end
end

#on_error(err, response = nil) ⇒ Object



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
# File 'lib/internethakai/action.rb', line 131

def on_error err, response=nil
    if err.is_a? String
        @logger.run(err, 2)
        @errorcount += 1
        on_complete response
        return
    elsif err.is_a? TimeoutError
        @logger.run("x", 2)
        @errorcount += 1
        on_complete response
        return
    elsif err.is_a? SystemCallError
        free
        raise err
    elsif err.is_a? SocketError
        free
        raise err
    elsif err.is_a? Interrupt
        free
        raise err
    elsif err.is_a? Exception
        free
    end
    @logger.run("error: #{err.to_s}\n", 2)
    @errorcount += 1
    return on_complete(response)
end

#on_freeObject



101
102
# File 'lib/internethakai/action.rb', line 101

def on_free
end

#on_redirect(newurl) ⇒ Object



255
256
257
# File 'lib/internethakai/action.rb', line 255

def on_redirect newurl

end

#on_response(response) ⇒ Object



258
259
# File 'lib/internethakai/action.rb', line 258

def on_response response
end

#runObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/internethakai/action.rb', line 80

def run
    super
    return unless @opt['path']
    init
    @client_handler.performance_id = @performance_id
    #p [object_id, @client_handler.object_id]
    @client_handler.set_opt(@opt)
    @http_client = get_client
    #return dispatch_next unless @http_client
    #return unless @http_client
    @http_client = @client_handler.handle_client(@http_client)
    @register_key = @opt["type"]

    @http_client.set_cookie(@scenario.cookie) if @scenario.cookie
    http_request(@opt['method'], @opt['path'], @opt['post_string'])
end

#set_client(cl) ⇒ Object



58
59
60
# File 'lib/internethakai/action.rb', line 58

def set_client cl
    @http_client = cl
end

#set_waitObject

黒魔術



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/internethakai/action.rb', line 280

def set_wait
    @oldrun = method(:run)
    if @opt['rev']
        def self.fire
            @timer.disable
            @oldrun.call
        end
        def self.run
            @timer.reset
            @timer.enable unless @timer.enabled?
        end
        @timer = Rev::TimerWatcher::new(@opt['wait'], true)
        @timer.attach(Rev::Loop::default)
        @timer.disable
        @timer.on_timer(&method(:fire))
    end
end

#to_sObject



276
277
278
# File 'lib/internethakai/action.rb', line 276

def to_s
    "<#{self.class}:#{object_id}:#{@opt['path']}>"
end