lib/rhack/frame.rb in rhack-1.2.1 vs lib/rhack/frame.rb in rhack-1.2.7
- old
+ new
@@ -23,27 +23,28 @@
__init__
attr_reader :loc, :static, :ss, :opts, :use_cache, :write_to
@@cache = {}
def initialize *args
- args << 10 unless args[-1].is Fixnum
- args.insert -2, {} unless args[-2].is Hash
- opts = args[-2]
- if scouts = (opts[:scouts] || opts[:threads])
- args[-1] = scouts
- end
+ #args << 10 unless args[-1].is Fixnum
+ #args.insert -2, {} unless args[-2].is Hash
+ #opts = args[-2]
+ #if scouts = (opts[:scouts] || opts[:threads])
+ # args[-1] = scouts
+ #end
+ opts = args.find_by_class Hash
+ scouts_count = opts[:scouts] || opts[:threads] || 10
@opts = {:eval => Johnson::Enabled, :redir => true, :cp => true, :result => Page}.merge!(opts)
- args[-2] = @opts
if args[0].is String
- url = args[0]
+ url = args[0].dup
'http://' >> url if url !~ /^\w+:\/\//
update_loc url
else
@loc = {}
@static = false
end
- @ss = ScoutSquad *args
+ @ss = ScoutSquad @loc.href, @opts, scouts_count
end
def update_loc url
@loc = url.parse :uri
# be careful, if you set :static => false, frame will be unable to use "path" url
@@ -89,12 +90,18 @@
def inspect
sssize = @ss.size
"<#Frame @ #{@ss.untargeted ? 'no target' : @loc.root}: #{sssize} #{sssize == 1 ? 'scout' : 'scouts'}#{', static'+(' => '+@static.protocol if @static.is(Hash)) if @static}, cookies #{@ss[0].cookieProc ? 'on' : 'off'}>"
end
- # opts are :eval, :json, :hash, :wait, :proc_result, :save_result, :load_scripts,
- # :zip, :thread_safe, :result, :stream, :raw, :xhr + any opts for Scouts in one hash
+ # All opts going in one hash.
+ # Opts for Frame:
+ # :wait, :proc_result, :save_result, :zip, :thread_safe, :result, :stream, :raw, :xhr, :content_type
+ # Opts passed to Page:
+ # :xml, :html, :json, :hash, :eval, :load_scripts
+ # Opts for Scout:
+ # :headers, :redir, :relvl
+ # TODO: describe options
def exec *args, &callback
many, order, orders, with_opts = interpret_request *args
L.log({:many => many, :order => order, :orders => orders, :with_opts => with_opts})
if !Johnson::Enabled and with_opts[:eval]
@@ -114,35 +121,46 @@
def interpret_request(*args)
body, mp, url, opts = args.dup.get_opts [nil, false, nil], @opts
L.log [body, mp, url, opts]
zip = opts.delete :zip
verb = opts.delete :verb
- many = order = orders = post = false
- # Default options set is for POST
- if mp.is String or mp.kinda Array and !(url.is String or url.kinda Array)
- # if second arg is String, then that's url
- url, mp, post = mp.dup, false, true
- # L.debug "url #{url.inspect} has been passed as second argument instead of third"
- # But if we have only one argument actually passed
- # except for options hash, then believe it's GET
- elsif body.is String or body.kinda [String]
- L.debug "first parameter (#{body.inspect}) was implicitly taken as url#{' '+body.class if body.kinda Array}, but last paramter is of type #{url.class}, too" if url
- url = body.dup
- elsif !body
- url = nil
+ post = put = verb == :put
+ many = order = orders = false
+
+ if put
+ # If request is PUT then first argument is always body
+ if mp.is String
+ # and second is URL if specified
+ url = mp.dup
+ else
+ url = nil
+ end
else
- url = url.dup if url
- mp, post = !!mp, true
+ # Default options set is for POST
+ if mp.is String or mp.kinda Array and !(url.is String or url.kinda Array)
+ # if second arg is String then it's URL
+ url, mp, post = mp.dup, false, true
+ # L.debug "URL #{url.inspect} has been passed as second argument instead of third"
+ # But if we have only one argument actually passed
+ # except for options hash then believe it's GET
+ elsif body.is String or body.kinda [String]
+ L.debug "first parameter (#{body.inspect}) was implicitly taken as url#{' '+body.class if body.kinda Array}, but last paramter is of type #{url.class}, too" if url
+ url = body.dup
+ elsif !body
+ url = nil
+ else
+ url = url.dup if url
+ mp, post = !!mp, true
+ end
end
if post
- put = verb == :put
validate_zip url, body if zip
if zip or url.kinda Array or body.kinda Array
many = true
unless put or body.kinda [Hash]
- raise TypeError, "body of post request must be a hash array, params was
+ raise TypeError, "body of POST request must be a hash array, params was
(#{args.inspect[1..-2]})"
end
if zip or url.kinda Array
validate_some url
@@ -155,13 +173,20 @@
orders.each {|o| o.unshift :loadPut}
else
orders.each {|o| o.unshift :loadPost and o.insert 2, mp}
end
else
- unless put or body.is Hash
- raise TypeError, "body of post request must be a hash, params was
- (#{args.inspect[1..-2]})"
+ if put
+ unless body.is String
+ raise TypeError, "body of PUT request must be a string, params was
+ (#{args.inspect[1..-2]})"
+ end
+ else
+ unless body.is Hash
+ raise TypeError, "body of POST request must be a hash, params was
+ (#{args.inspect[1..-2]})"
+ end
end
url = validate url
order = put ? [:loadPut, body, url] : [:loadPost, body, mp, url]
end
@@ -182,14 +207,25 @@
end
opts[:wait] = opts[:sync] if :sync.in opts
opts[:wait] = true if !:wait.in(opts) and
:proc_result.in(opts) ? !opts[:proc_result] : opts[:save_result]
+
opts[:eval] = false if opts[:json] or opts[:hash] or opts[:raw]
opts[:load_scripts] = self if opts[:load_scripts]
opts[:stream] = true if opts[:raw]
+
(opts[:headers] ||= {})['X-Requested-With'] = 'XMLHttpRequest' if opts[:xhr]
+ if opts[:content_type]
+ if mime_type = Mime::Type.lookup_by_extension(opts[:content_type])
+ (opts[:headers] ||= {})['Content-Type'] = mime_type
+ else
+ raise ArgumentError, "failed to detect Mime::Type by extension: #{opts[:content_type]}
+ (#{args.inspect[1..-2]})"
+ end
+ end
+
[many, order, orders, opts]
end
private
def validate_zip(url, body)
@@ -290,14 +326,16 @@
# sometimes (about 2% for 100-threads-dling) when this string is calling
# no matter what +curl.res.body+ has contained here
RMTools.rw @write_to+'/'+order[-2].sub(/^[a-z]+:\/\//, ''), curl.res.body.xml_to_utf
end
if opts[:raw]
- page.res = yield curl
+ page.res = block_given? ? yield(curl) : curl.body_str
# here +curl.res.body+ becomes empty
+ # curl.res.body.+xml_to_utf+ -- maybe this is problem?
elsif page.process(curl, opts)
@@cache[page.href] = page if order[0] == :loadGet and @use_cache
run_callbacks! page, opts, &callback
+ # nothing to do here if process returns nil or false
end
}
# > Carier.requests++
unless opts[:wait] and opts[:thread_safe] or opts[:exec] == false
Curl.execute :unless_already
\ No newline at end of file