cookie_http_client 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0ff55f1850c78b28d1384b7f8f2fed0b2ac85cfb
4
+ data.tar.gz: 1d68442de8c882aad981235cdc44fea36d00dcad
5
+ SHA512:
6
+ metadata.gz: 66e8bdf23882505215f37a25a3913552f8c349136fd940df1077fbbf74f3a5b58063f5bb59f08bc945be3fe52988d8f1ffe85e7d81975cc073ff620923e4c31c
7
+ data.tar.gz: 30298cf2bf8725597c60c212980a934b51c41dbe03b5e4c13a69c4e40f0323ac8389132a160de0de99f8a2ef9893fe17df76ec34f630a15b1bf0deeef3d27b78
data/.gitignore ADDED
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ # Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cookie_http_client.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Toshiyuki Suzumura
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # CookieHttpClient
2
+
3
+ HTTP clinet with cookie suppot.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'cookie_http_client'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install cookie_http_client
19
+
20
+ ## Usage
21
+
22
+
23
+ ### Basic GET/POST request
24
+
25
+ client = CookieHTTPClient.new("http://goo.gl/IwTffS")
26
+ res = client.get #=> Net::HTTPResponse
27
+ client.last_uri #=> https://github.com/suzumura-ss/cookie_http_client
28
+
29
+ client = CookieHTTPClient.new("http://whois.jprs.jp/")
30
+ params = {'type'=>'DOM', 'key'=>'amazon.co.jp'}
31
+ res = client.post_form(params) #=> Net::HTTPResponse
32
+
33
+
34
+ ### Hooking redirection
35
+
36
+ client = CookieHTTPClient.new("http://goo.gl/IwTffS")
37
+ res = client.get{|uri|
38
+ # `uri` is URI of location-header.
39
+ # You can modify uri.
40
+ uri
41
+ }
42
+
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it ( http://github.com/<my-github-username>/cookie_http_client/fork )
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cookie_http_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cookie_http_client"
8
+ spec.version = CookieHTTPClient::VERSION
9
+ spec.authors = ["Toshiyuki Suzumura"]
10
+ spec.email = ["suz.labo@amail.plala.or.jp"]
11
+ spec.summary = %q{HTTP clinet with cookie support.}
12
+ spec.description = nil
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_dependency "http-cookie"
25
+ end
@@ -0,0 +1,3 @@
1
+ class CookieHTTPClient
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,119 @@
1
+ require "cookie_http_client/version"
2
+ require 'uri'
3
+ require 'cgi'
4
+ require 'net/https'
5
+ require 'http/cookie'
6
+
7
+
8
+ class CookieHTTPClient
9
+ THIS = CookieHTTPClient
10
+ class HTTPFound < Exception
11
+ attr_reader :redirect_uri, :source
12
+ def initialize(httpFound)
13
+ @source = httpFound
14
+ @redirect_uri = URI.parse(httpFound['location'])
15
+ @redirect_uri.path = '/' if @redirect_uri.path.empty?
16
+ end
17
+ end
18
+
19
+ class CookieJar < HTTP::CookieJar
20
+ def cookie_string(uri)
21
+ HTTP::Cookie.cookie_value(self.cookies(uri))
22
+ end
23
+
24
+ def set_cookie(str, uri)
25
+ if str and !str.empty?
26
+ self.parse(str, uri)
27
+ end
28
+ end
29
+ end
30
+
31
+ @@cookie_jar = nil
32
+ def self.cookie_jar
33
+ @@cookie_jar
34
+ end
35
+ def self.clear_cookie
36
+ @@cookie_jar = CookieJar.new unless @@cookie_jar
37
+ @@cookie_jar.clear
38
+ end
39
+
40
+ protected
41
+ def request(uri, header={})
42
+ raise StandardError, "block required." unless block_given?
43
+ path = [uri.path, uri.query].compact.join('?')
44
+ http = @proxy.new(uri.host, uri.port)
45
+ http.use_ssl = uri.scheme=='https'
46
+ c = @@cookie_jar.cookie_string(uri)
47
+ h = header.clone
48
+ h["Cookie"] = c unless c.empty?
49
+ r = yield(http, path, h)
50
+ @@cookie_jar.set_cookie(r['set-cookie'], uri)
51
+ raise HTTPFound.new(r) if r.is_a? Net::HTTPFound or r.is_a? Net::HTTPMovedPermanently
52
+ r
53
+ end
54
+
55
+
56
+ public
57
+ attr_reader :uri, :last_uri
58
+ attr_accessor :max_redirect_count
59
+
60
+ def initialize(uri, proxy=Net::HTTP)
61
+ @@cookie_jar = CookieJar.new unless @@cookie_jar
62
+ if uri.respond_to?(:path)
63
+ @uri = uri
64
+ else
65
+ @uri = URI.parse(uri)
66
+ end
67
+ @uri.path = '/' if @uri.path.empty?
68
+ @last_uri = nil
69
+ @proxy = proxy
70
+ @max_redirect_count = 10
71
+ end
72
+
73
+ # @param header:Hash request headers
74
+ # @param block callback when redirect with `uri`
75
+ # @return Net::HTTPResponse
76
+ def get(header={})
77
+ @last_uri = @uri.clone
78
+ count = @max_redirect_count
79
+ begin
80
+ request(@last_uri, header){|http, path, header2|
81
+ http.get(path, header2)
82
+ }
83
+ rescue HTTPFound =>e
84
+ count-=1
85
+ raise e if count==0
86
+ @last_uri = e.redirect_uri
87
+ @last_uri = yield(@last_uri) if block_given?
88
+ retry
89
+ end
90
+ end
91
+
92
+ # @param data post data
93
+ # @param header:Hash request headers
94
+ # @param block callback when redirect with `uri`
95
+ # @return Net::HTTPResponse
96
+ def post(data, header={}, &block)
97
+ begin
98
+ request(@uri, header){|http, path, header2|
99
+ http.post(path, data, header2)
100
+ }
101
+ rescue HTTPFound =>e
102
+ @last_uri = e.redirect_uri
103
+ @last_uri = yield(@last_uri) if block_given?
104
+ n = THIS.new(@last_uri)
105
+ r = n.get({}, &block)
106
+ @last_uri = n.last_uri
107
+ r
108
+ end
109
+ end
110
+
111
+ # @param params:Hash post data
112
+ # @param header:Hash request headers
113
+ # @param block callback when redirect with `uri`
114
+ # @return Net::HTTPResponse
115
+ def post_form(params, header={}, &block)
116
+ data = params.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join('&')
117
+ post(data, header, &block)
118
+ end
119
+ end
@@ -0,0 +1,40 @@
1
+ require 'nokogiri'
2
+ require 'cookie_http_client'
3
+
4
+
5
+ client = CookieHTTPClient.new("https://theta360.com/authentication")
6
+ puts "#{client.uri.host}/#{client.uri.path}"
7
+ res = client.get{|uri|
8
+ puts "=> #{uri.host}#{uri.path}"
9
+ uri
10
+ }
11
+ select_html = Nokogiri::HTML(res.body)
12
+ path = select_html.xpath("//div[@class=\"login_fb\"]/a").attribute("data-href").value
13
+
14
+
15
+ client = CookieHTTPClient.new("https://theta360.com#{path}")
16
+ res = client.get{|uri|
17
+ puts "=> #{uri.host}#{uri.path}"
18
+ uri
19
+ }
20
+ login_dialog = Nokogiri::HTML(res.body)
21
+ form = login_dialog.xpath("//form[@id=\"login_form\"]")
22
+ params = form.xpath("//input").inject({}){|s,v|
23
+ d = v.attribute('value')
24
+ s[v.attribute('name').value] = d ? d.value: nil
25
+ s
26
+ }
27
+ params['email'] = "valid-user@example.com"
28
+ params['pass'] = "password"
29
+
30
+ submit_u = client.last_uri.clone
31
+ action_u = URI.parse(form.attribute('action').value)
32
+ submit_u.path = action_u.path
33
+ submit_u.query = action_u.query
34
+
35
+
36
+ client = CookieHTTPClient.new(submit_u)
37
+ result = client.post_form(params){|uri|
38
+ puts "=> #{uri.host}#{uri.path}"
39
+ uri
40
+ }
@@ -0,0 +1,55 @@
1
+ require 'cookie_http_client'
2
+
3
+ describe CookieHTTPClient do
4
+ before(:all) do
5
+ @proxy =if ENV['HTTP_PROXY']
6
+ uri = URI.parse(ENV['HTTP_PROXY'])
7
+ Net::HTTP.Proxy(uri.host, uri.port, uri.user, uri.password)
8
+ else
9
+ Net::HTTP
10
+ end
11
+ end
12
+
13
+ context "GET request" do
14
+ before(:all) do
15
+ @client = CookieHTTPClient.new("http://goo.gl/IwTffS", @proxy)
16
+ @redirect = nil
17
+ @res = @client.get{|uri|
18
+ @redirect = uri
19
+ }
20
+ end
21
+
22
+ it {
23
+ expect(@res.code).to be == "200"
24
+ }
25
+ it {
26
+ expect(@client.last_uri.to_s).to be == "https://github.com/suzumura-ss/cookie_http_client"
27
+ }
28
+ it {
29
+ expect(@redirect).not_to be_nil
30
+ }
31
+
32
+ it {
33
+ client = CookieHTTPClient.new("https://github.com/suzumura-ss/cookie_http_client", @proxy)
34
+ res = client.get
35
+ expect(CookieHTTPClient.cookie_jar.cookies(client.last_uri).size).to be > 0
36
+ CookieHTTPClient.cookie_jar.clear
37
+ expect(CookieHTTPClient.cookie_jar.cookies(client.last_uri).size).to be == 0
38
+ }
39
+ end
40
+
41
+ context "POST request" do
42
+ before(:all) do
43
+ q = {'type'=>'DOM', 'key'=>'amazon.co.jp'}
44
+ @client = CookieHTTPClient.new("http://whois.jprs.jp/", @proxy)
45
+ @res = @client.post_form(q)
46
+ end
47
+
48
+ it {
49
+ expect(@res.code).to be == "200"
50
+ }
51
+ it {
52
+ expect(@res.body).to match(/AMAZON.CO.JP/)
53
+ }
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cookie_http_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Toshiyuki Suzumura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: http-cookie
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: ''
70
+ email:
71
+ - suz.labo@amail.plala.or.jp
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - cookie_http_client.gemspec
82
+ - lib/cookie_http_client.rb
83
+ - lib/cookie_http_client/version.rb
84
+ - sample/theta360_login.rb
85
+ - spec/cookie_http_client_spec.rb
86
+ homepage: ''
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.2.2
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: HTTP clinet with cookie support.
110
+ test_files:
111
+ - spec/cookie_http_client_spec.rb