ht2p 0.0.0

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jun Kikuchi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,43 @@
1
+ = HT2P
2
+
3
+ HT2P is a Ruby library for HTTP.
4
+
5
+ == Usage
6
+
7
+ require 'rubygems'
8
+ require 'ht2p'
9
+
10
+ HT2P::Client.new('http://localhost:4567/echo/content') do |request|
11
+ p request.uri
12
+ request.method = :put
13
+ request.header['Accepts'] = 'text/html'
14
+ request.header['Content-Length'] = ("Hello World!\n" * 1000).size
15
+ response = request.send do |io|
16
+ 1000.times do
17
+ io.write 'Hello World!'
18
+ end
19
+ end
20
+
21
+ p response.code
22
+ p response.header
23
+ response.receive do |io|
24
+ while chunk = io.read(1024)
25
+ print chunk
26
+ end
27
+ end
28
+ end
29
+
30
+ == Note on Patches/Pull Requests
31
+
32
+ * Fork the project.
33
+ * Make your feature addition or bug fix.
34
+ * Add tests for it. This is important so I don't break it in a
35
+ future version unintentionally.
36
+ * Commit, do not mess with rakefile, version, or history.
37
+ (if you want to have your own version, that is fine but
38
+ bump version in a commit by itself I can ignore when I pull)
39
+ * Send me a pull request. Bonus points for topic branches.
40
+
41
+ == Copyright
42
+
43
+ Copyright (c) 2009 Jun Kikuchi. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "ht2p"
8
+ gem.summary = %Q{ht2p}
9
+ gem.description = %Q{ht2p}
10
+ gem.email = "kikuchi@bonnou.com"
11
+ gem.homepage = "http://github.com/JunKikuchi/ht2p"
12
+ gem.authors = ["Jun Kikuchi"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "ht2p #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,27 @@
1
+ class HT2P::Client::Request
2
+ METHODS = %w'get head post put delete options trace connect'
3
+
4
+ attr_accessor :uri, :method, :header
5
+
6
+ def initialize(client, params)
7
+ @client = client
8
+ @method = params[:method] || :get
9
+ @header = HT2P::Header.new.merge!(params[:header] || {})
10
+ end
11
+
12
+ def send(body=nil, &block)
13
+ @client.request_header(@method, @header)
14
+ @client.write(body) if body
15
+ block.call(self) if block_given?
16
+ @client.flush
17
+ HT2P::Client::Response.new(@client)
18
+ end
19
+
20
+ METHODS.each do |val|
21
+ define_method(val, instance_method(:send))
22
+ end
23
+
24
+ def write(val)
25
+ @client.write(val)
26
+ end
27
+ end
@@ -0,0 +1,61 @@
1
+ class HT2P::Client::Response
2
+ attr_reader :code, :header
3
+
4
+ def initialize(client)
5
+ @client, @code, @header = client, *client.response_header
6
+
7
+ @body = if @header['transfer-encoding'].to_s.downcase == 'chunked'
8
+ Chunked.new(@client)
9
+ else
10
+ Transfer.new(@client, @header['content-length'].to_i)
11
+ end
12
+ end
13
+
14
+ def receive(&block)
15
+ if block_given?
16
+ block.call(self)
17
+ else
18
+ read
19
+ end
20
+ end
21
+
22
+ def read(length=nil)
23
+ @body.read(length)
24
+ end
25
+
26
+ class Transfer
27
+ def initialize(client, size)
28
+ @client, @size = client, size
29
+ end
30
+
31
+ def read(length=nil)
32
+ if @size.nil?
33
+ @client.read(length)
34
+ elsif @size > 0
35
+ length ||= @size
36
+ length = @size if @size < length
37
+ @size -= length
38
+
39
+ @client.read(length)
40
+ else
41
+ nil
42
+ end
43
+ end
44
+ end
45
+
46
+ class Chunked < Transfer
47
+ def initialize(client)
48
+ super(client, nil)
49
+ parse_size
50
+ end
51
+
52
+ def read(length=nil)
53
+ parse_size unless @size > 0
54
+ super(length)
55
+ end
56
+
57
+ def parse_size
58
+ @size = @client.gets.chop!.hex
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,80 @@
1
+ require 'uri'
2
+ require 'socket'
3
+ require 'openssl'
4
+
5
+ class HT2P::Client
6
+ attr_reader :uri
7
+
8
+ def initialize(uri, params={}, &block)
9
+ @uri = URI.parse(uri)
10
+
11
+ ip = IPSocket.getaddress(@uri.host)
12
+ ip = nil if /\A127\.|\A::1\z/ =~ ip
13
+
14
+ TCPSocket.open(ip, @uri.port) do |socket|
15
+ if @uri.scheme == 'https'
16
+ begin
17
+ @socket = OpenSSL::SSL::SSLSocket.new(socket)
18
+ @socket.connect
19
+ block.call HT2P::Client::Request.new(self, params)
20
+ ensure
21
+ @socket.close
22
+ end
23
+ else
24
+ @socket = socket
25
+ block.call HT2P::Client::Request.new(self, params)
26
+ end
27
+ end
28
+ end
29
+
30
+ def request_header(method, header)
31
+ @socket.write "%s %s%s HTTP/1.1\r\n" % [
32
+ method.to_s.upcase,
33
+ @uri.path,
34
+ @uri.query && "?#{@uri.query}"
35
+ ]
36
+ @socket.write "Host: #{@uri.host}\r\n"
37
+ @socket.write header.to_s
38
+ @socket.write "\r\n"
39
+ end
40
+
41
+ def response_header
42
+ code = nil
43
+ header = HT2P::Header.new
44
+
45
+ key = nil
46
+ while line = @socket.gets.chop!
47
+ line.empty? && break
48
+
49
+ if md = %r!HTTP[\w\./]+\s+(\d+)!.match(line)
50
+ code = md[1]
51
+ elsif md = /(.+?):\s*(.*)/.match(line)
52
+ key, val = md[1].downcase, md[2]
53
+ header[key] = val
54
+ elsif md = /\s+(.*)/.match(line)
55
+ header[key] = md[1]
56
+ end
57
+ end
58
+
59
+ [code.to_i, header]
60
+ end
61
+
62
+ def write(val)
63
+ @socket.write val.to_s
64
+ end
65
+
66
+ def read(length=nil)
67
+ @socket.read length
68
+ end
69
+
70
+ def gets
71
+ @socket.gets
72
+ end
73
+
74
+ def flush
75
+ @socket.flush
76
+ end
77
+
78
+ autoload :Request, 'ht2p/client/request'
79
+ autoload :Response, 'ht2p/client/response'
80
+ end
@@ -0,0 +1,27 @@
1
+ class HT2P::Header < Hash
2
+ def []=(key, val)
3
+ if self.key? key
4
+ if self[key].is_a? Array
5
+ self[key] << val
6
+ else
7
+ super(key, [self[key], val])
8
+ end
9
+ else
10
+ super(key, val)
11
+ end
12
+ end
13
+
14
+ def to_s
15
+ inject('') do |ret, (key, val)|
16
+ if val.is_a? Array
17
+ val.each do |v|
18
+ ret << "#{key}: #{v}\r\n"
19
+ end
20
+ else
21
+ ret << "#{key}: #{val}\r\n"
22
+ end
23
+
24
+ ret
25
+ end
26
+ end
27
+ end
data/lib/ht2p.rb ADDED
@@ -0,0 +1,4 @@
1
+ class HT2P
2
+ autoload :Header, 'ht2p/header'
3
+ autoload :Client, 'ht2p/client'
4
+ end
data/spec/ht2p_spec.rb ADDED
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe HT2P::Client do
4
+ %w'get post put delete head'.each do |method|
5
+ describe method do
6
+ it "should perform a #{method}" do
7
+ HT2P::Client.new('http://localhost:4567/echo/body') do |request|
8
+ request.method = method.to_sym
9
+ request.header['Accepts'] = 'text/html'
10
+ if method != 'head'
11
+ request.header['Content-Length'] = ("Hello World!\n" * 10000).size
12
+ response = request.send do |io|
13
+ 10000.times do
14
+ io.write "Hello World!\n"
15
+ end
16
+ end
17
+ else
18
+ request.header['Content-Length'] = 0
19
+ response = request.send
20
+ end
21
+
22
+ response.code.should == 200
23
+ response.header['content-type'] == 'text/html'
24
+ body = ''
25
+ response.receive do |io|
26
+ while chunk = io.read(5)
27
+ body << chunk
28
+ end
29
+ end
30
+
31
+ body.should == ("Hello World!\n" * 10000) if method != 'head'
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
data/spec/server.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'sinatra'
3
+
4
+ %w'get post put delete head'.each do |method|
5
+ self.__send__ method.to_sym, '/echo/body' do
6
+ request.body.read
7
+ end
8
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'ht2p'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ht2p
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jun Kikuchi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-10 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ description: ht2p
26
+ email: kikuchi@bonnou.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/ht2p.rb
42
+ - lib/ht2p/client.rb
43
+ - lib/ht2p/client/request.rb
44
+ - lib/ht2p/client/response.rb
45
+ - lib/ht2p/header.rb
46
+ - spec/ht2p_spec.rb
47
+ - spec/server.rb
48
+ - spec/spec.opts
49
+ - spec/spec_helper.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/JunKikuchi/ht2p
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --charset=UTF-8
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.3.5
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: ht2p
78
+ test_files:
79
+ - spec/ht2p_spec.rb
80
+ - spec/server.rb
81
+ - spec/spec_helper.rb