Module: HTTP

Defined in:
lib/http.rb

Overview

Created by me on 2007-12-28.

Copyright (c) 2007. All pwnage reserved.

Class Method Summary collapse

Class Method Details

.cleanObject


79
80
81
# File 'lib/http.rb', line 79

def clean
  remove_extra_files
end

.fetchObject


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/http.rb', line 35

def fetch
  i = 0
  array = []
  start = 0
  jump = @url.length / @opts[:threads]
  finish = -1                     # -1 so that start becomes 0

  @opts[:threads].times do |t|
    start = finish + 1
    finish = (start + jump) - 1
    array << [start, finish, t + 1]
  end

  array.each_simultaneously { |d, b, w|
    puts "[LOG] Setting up a new thread..." if @opts[:verbose]
    open(@opts[:out], "a+") do |f|
      begin
        f.seek(d)
        Net::HTTP.new(@url.host, @url.port).start do |http|
          http.get(@url.page, {'range' => "bytes=#{d}-#{b}"}) do |str|
            i += 1
            print '.' if i % @opts[:dots] == 0
            STDOUT.flush
            f.write str if str
          end
        end
      rescue Timeout::Error => e
        print "\n[Error] Error in server response in thread #{w}. Retrying!\n"
        puts e
        STDOUT.flush
        sleep 5
        retry
      rescue => e
        print "\n[Error] Error in server response in thread #{w}. Retrying!\n"
        puts e
        STDOUT.flush
        sleep 5
        retry
      end
    end
    puts "[LOG] Thread #{w} finished!" if @opts[:verbose]
  }
end

.fetch_singleObject


25
26
27
28
29
30
31
32
33
# File 'lib/http.rb', line 25

def fetch_single
  p single
  open(@opts[:out], "w") do |f|
    http = Net::HTTP.new(@url.host, @url.port)
    http.get(@url.page) do |str|
      f.write str
    end
  end
end

.prepObject


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/http.rb', line 13

def prep
  req = Net::HTTP.new(@url.host, @url.port)
  len = req.request_head(@url.page)['content-length']
  @url.length = len.to_i

  if len.nil? or @url.length == 0
    @opts[:threads] = 1
    puts "--> Uhoh! Server isn't cooperating..."
    puts "--> Changing threadcount to 1"
  end
end

.remove_extra_filesObject


83
84
85
86
87
88
89
90
# File 'lib/http.rb', line 83

def remove_extra_files
  puts "[LOG] Writing #{@opts[:out]}" if @opts[:verbose]

  if @opts[:clean]
    File.delete(@opts[:out])
    puts "[LOG] Removing #{@opts[:out]}" if @opts[:verbose]
  end
end

.setup(opts, uri) ⇒ Object


8
9
10
11
# File 'lib/http.rb', line 8

def setup(opts, uri)
  @opts = opts
  @url = uri
end