HTTPal 19

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig ADDED
Binary file
data/README ADDED
@@ -0,0 +1,8 @@
1
+ httpal
2
+ ================
3
+
4
+ See the test cases for how to use. Basically it's a web browser class that keeps cookies,
5
+ referer, and other state items around.
6
+
7
+ Caveat: Individual Browsers aren't thread-safe in any sense of the word. So if you use
8
+ multiple threads all browsin', each one should have their own HTTPal::Browser object.
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ #
2
+ # Rakefile
3
+ # httpal
4
+ #
5
+ # Created by Bryce Kerley on 2006-12-31.
6
+ # Copyright (c) 2006 Bryce Kerley. All rights reserved.
7
+ #
8
+ # Subversion info:
9
+ # $Id: Rakefile 19 2007-02-06 01:43:59Z bkerley $
10
+ #
11
+ # pillaged wholesale from Sam Stephenson's
12
+ # Rakefile for minischeme
13
+ require 'rake'
14
+ require 'rake/testtask'
15
+ require 'rake/gempackagetask'
16
+ require 'rubygems'
17
+
18
+ ID = "$Id: Rakefile 19 2007-02-06 01:43:59Z bkerley $"
19
+ GEM_VERSION = ID.split[2]
20
+
21
+ #this will probably fail in win32
22
+ HOMEDIR = ENV['HOME']
23
+
24
+ Rake::TestTask.new do |t|
25
+ t.libs << 'test'
26
+ t.verbose = true
27
+ t.pattern = 'tests/**/*_test.rb'
28
+ end
29
+
30
+ task :default => :test
31
+
32
+ spec = Gem::Specification.new do |s|
33
+ s.platform = Gem::Platform::RUBY
34
+ s.summary = "HTTP browser library"
35
+ s.name = 'HTTPal'
36
+ s.author = "Bryce Kerley"
37
+ s.email = "bkerley@brycekerley.net"
38
+ s.version = GEM_VERSION
39
+ s.requirements << 'none'
40
+ s.require_path = 'lib'
41
+ s.autorequire = 'httpal'
42
+ s.files = FileList["{lib,tests}/**/*"].to_a + ["README", "Rakefile"]
43
+ s.signing_key = ENV['HOME']+'/.rubygems/gem-private_key.pem'
44
+ s.cert_chain = ['bkerley-public_cert.pem']
45
+ s.description = <<EOF
46
+ HTTPal is a Ruby HTTP library that provides browser-like
47
+ functionality in terms of saving cookies between requests
48
+ and proper REFERER handling.
49
+ EOF
50
+ end
51
+
52
+ Rake::GemPackageTask.new(spec) do |pkg|
53
+ pkg.need_zip = true
54
+ pkg.need_tar = true
55
+ end
data/lib/httpal.rb ADDED
@@ -0,0 +1,15 @@
1
+ #
2
+ # httpal.rb
3
+ # httpal
4
+ #
5
+ # Created by Bryce Kerley on 2006-12-31.
6
+ # Copyright (c) 2006 Bryce Kerley. All rights reserved.
7
+ #
8
+ # Subversion info:
9
+ # $Id: httpal.rb 1 2007-01-01 19:20:16Z bkerley $
10
+ $:.unshift File.dirname(__FILE__)
11
+ require 'net/http'
12
+ require 'uri'
13
+
14
+ require 'httpal/browser.rb'
15
+ require 'httpal/cookie.rb'
@@ -0,0 +1,98 @@
1
+ #
2
+ # httpal.rb
3
+ # httpal
4
+ #
5
+ # Created by Bryce Kerley on 2006-12-23.
6
+ # Copyright (c) 2006 Bryce Kerley. All rights reserved.
7
+ #
8
+ # Subversion info:
9
+ # $Id: browser.rb 12 2007-02-01 22:37:02Z bkerley $
10
+ module HTTPal
11
+ class Browser
12
+
13
+ def initialize
14
+ @cookies = []
15
+ @referer = nil
16
+ end
17
+
18
+ def use(&block)
19
+ instance_eval(&block)
20
+ end
21
+
22
+ def get(uri)
23
+ uri,path = parseuri(uri)
24
+ req = Net::HTTP::Get.new(path)
25
+ send_request(req, uri)
26
+ end
27
+
28
+ def post(uri, fields)
29
+ uri,path = parseuri(uri)
30
+ req = Net::HTTP::Post.new(path)
31
+ req.set_form_data fields
32
+ send_request(req, uri)
33
+ end
34
+
35
+ private
36
+
37
+ def parseuri(uri)
38
+ uri = URI.parse(uri) unless uri.is_a? URI::HTTP
39
+ path = uri.path
40
+ path << '?' << uri.query if uri.query
41
+ return [uri, path]
42
+ end
43
+
44
+ def send_request(req, uri)
45
+ req['cookie'] = get_cookies_for_uri(uri)
46
+ req['referer'] =get_referer
47
+
48
+ res = Net::HTTP.new(uri.host, uri.port ? uri.port : 80).start {|http| http.request(req)}
49
+ set_cookie_for_uri(uri, res['set-cookie'])
50
+ set_referer(uri)
51
+
52
+ return res
53
+ end
54
+
55
+ def get_referer
56
+ @referer if @referer
57
+ ""
58
+ end
59
+
60
+ def set_referer(referer)
61
+ @referer = referer.to_s
62
+ end
63
+
64
+ def get_cookies_for_uri(uri)
65
+ hs = uri.host.split('.')
66
+ hm = (1..hs.length).inject([]) do |c,n|
67
+ c << hs[(-n)..-1].join('.')
68
+ end
69
+ ps = uri.path.split('/')
70
+ pm = ['/']
71
+ ps.length.times do |n|
72
+ pm << ps[0..n].join('/')
73
+ end
74
+ pm.delete ''
75
+
76
+ hostmatch = @cookies.inject([]) do |set, cur|
77
+ set << cur if hm.include? cur.domain
78
+ set
79
+ end
80
+
81
+ # TODO: PATH-MATCH COOKIES
82
+ end
83
+
84
+ def set_cookie_for_uri(uri, setcookie)
85
+ return unless setcookie
86
+ newcookies = CookieMonster.parse_set_cookie(setcookie)
87
+ @cookies.instance_eval do
88
+ newcookies.each do |c|
89
+ c.domain = uri.host unless c.domain
90
+ self[index(c)] = c if include?(c)
91
+ self << c unless include?(c)
92
+ end
93
+ end
94
+
95
+ end
96
+
97
+ end
98
+ end
@@ -0,0 +1,76 @@
1
+ #
2
+ # cookie.rb
3
+ # httpal
4
+ #
5
+ # Created by Bryce Kerley on 2006-12-23.
6
+ # Copyright (c) 2006 Bryce Kerley. All rights reserved.
7
+ #
8
+ # Subversion info:
9
+ # $Id: cookie.rb 1 2007-01-01 19:20:16Z bkerley $
10
+ module HTTPal
11
+ class CookieMonster
12
+ def self.parse_set_cookie(sc)
13
+ r = []
14
+ # need to figure out how to do the multi-cookie split
15
+ # since the comma separates both cookies and dates
16
+
17
+ # TODO: IMPLEMENT A LESS BAD SOLUTION
18
+ sc = sc.gsub(/expires=(\w\w\w)\,/,'expires=\1')
19
+
20
+ sc.split(',').each do |str|
21
+ str.chomp!
22
+ r << Cookie.new(str)
23
+ end
24
+ return r
25
+ end
26
+ end
27
+
28
+ class Cookie
29
+ @@fields = [:path, :domain, :expires]
30
+ attr_accessor :name, :value
31
+ attr_accessor *@@fields
32
+ def initialize(string)
33
+ components = string.split(';')
34
+ firstpair = nil
35
+ components.each do |p|
36
+ sp = p.split('=',2)
37
+ k = sp[0]
38
+ v = sp[1]
39
+ k.strip!
40
+ v.strip!
41
+ if firstpair == nil
42
+ send(:name=, k)
43
+ send(:value=, v)
44
+ firstpair = true
45
+ else
46
+ next unless @@fields.include? k.downcase.to_sym
47
+ v = v.gsub(/^\./,'') if k.downcase.to_sym == :domain
48
+ send((k+'=').downcase.to_sym, v)
49
+ end
50
+ end
51
+ end
52
+
53
+ def to_s
54
+ r = "#{name}=#{value}"
55
+ @@fields.each do |f|
56
+ r << "; #{f.to_s}=#{send(f)}" if send(f)
57
+ end
58
+ return r
59
+ end
60
+
61
+ def inspect
62
+ "\#<Cookie #{name}=#{value} from #{domain}#{path} until #{expires}>"
63
+ end
64
+
65
+ include Comparable
66
+ def <=> (anOther)
67
+ #sort by domain, then path, then name
68
+ d = self.domain.<=>(anOther.domain)
69
+ p = self.path.<=>(anOther.path)
70
+ n = self.name.<=>(anOther.name)
71
+ return d unless d == 0
72
+ return p unless p == 0
73
+ return n
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+ # test.rb
3
+ # httpal
4
+ #
5
+ # Created by Bryce Kerley on 2006-12-28.
6
+ # Copyright (c) 2006 Bryce Kerley. All rights reserved.
7
+ #
8
+ # Subversion info:
9
+ # $Id: original_test.rb 1 2007-01-01 19:20:16Z bkerley $
10
+ require File.join(File.dirname(__FILE__), 'test_helper')
11
+
12
+ class OriginalTest < Test::Unit::TestCase
13
+ def test_get
14
+ resp = nil
15
+ HTTPal::Browser.new.use {
16
+ resp = get('http://brycekerley.net/')
17
+ }
18
+ assert_not_nil resp
19
+ end
20
+
21
+ def test_post
22
+ resp = nil
23
+ val = rand(32598)
24
+ HTTPal::Browser.new.use {
25
+ resp = post('http://bonzoesc.net/provingground/post.php',
26
+ :value=>val
27
+ )
28
+ }
29
+ assert_equal val.to_s, resp.body
30
+ end
31
+
32
+ def test_err
33
+ resp = nil
34
+ HTTPal::Browser.new.use {
35
+ resp = get('http://bonzoesc.net/provingground/noexist.404')
36
+ }
37
+ assert_equal '404', resp.code
38
+ end
39
+
40
+ def test_cookie_correct
41
+ resp = nil
42
+ HTTPal::Browser.new.use {
43
+ #get the cookies
44
+ get('http://bonzoesc.net/provingground/cookie.php')
45
+ #check the cookies
46
+ resp = get('http://bonzoesc.net/provingground/cookie.php')
47
+ }
48
+ assert_equal 'you do', resp.body
49
+ end
50
+ end
@@ -0,0 +1,14 @@
1
+ #
2
+ # test_helper.rb
3
+ # httpal
4
+ #
5
+ # Created by Bryce Kerley on 2006-12-31.
6
+ # Copyright (c) 2006 Bryce Kerley. All rights reserved.
7
+ #
8
+ # Subversion info:
9
+ # $Id: test_helper.rb 1 2007-01-01 19:20:16Z bkerley $
10
+
11
+ $:.unshift File.dirname(__FILE__)
12
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
13
+ require 'test/unit'
14
+ require 'lib/httpal.rb'
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: HTTPal
5
+ version: !ruby/object:Gem::Version
6
+ version: "19"
7
+ date: 2007-02-05 00:00:00 -05:00
8
+ summary: HTTP browser library
9
+ require_paths:
10
+ - lib
11
+ email: bkerley@brycekerley.net
12
+ homepage:
13
+ rubyforge_project:
14
+ description: HTTPal is a Ruby HTTP library that provides browser-like functionality in terms of saving cookies between requests and proper REFERER handling.
15
+ autorequire: httpal
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
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V8OzepbxCGkT/o/8++OYVgAH
45
+ OY5Lj1IDyG55Hb8Iw3gdPzQQATySNG6BFkcUFbWHsEBGIcZjiltEz661msnbaJj6
46
+ +nino5jISVrYg5xJYSE92FTDGqNzTYedfNF2Cb+aKlY6pioSjk5XNILq+ksqAKJF
47
+ YEW6Ol4DMyXi5G4pZ297gw==
48
+ -----END CERTIFICATE-----
49
+
50
+ post_install_message:
51
+ authors:
52
+ - Bryce Kerley
53
+ files:
54
+ - lib/httpal
55
+ - lib/httpal.rb
56
+ - lib/httpal/browser.rb
57
+ - lib/httpal/cookie.rb
58
+ - tests/test_helper.rb
59
+ - tests/original_test.rb
60
+ - README
61
+ - Rakefile
62
+ test_files: []
63
+
64
+ rdoc_options: []
65
+
66
+ extra_rdoc_files: []
67
+
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ requirements:
73
+ - none
74
+ dependencies: []
75
+
metadata.gz.sig ADDED
@@ -0,0 +1 @@
1
+ D\���ck�jd���)Pӧ4��B;��͗�� t��ő7�ŷ��=����Gh�h��<���D�zS�X�0�{�