celluloid-http 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in celluloid-http.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Rozhnov Alexandr
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Celluloid::Http
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'celluloid-http'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install celluloid-http
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'celluloid-http/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "celluloid-http"
8
+ gem.version = Celluloid::Http::VERSION
9
+ gem.authors = ["Rozhnov Alexandr"]
10
+ gem.email = ["gnox73@gmail.com"]
11
+ gem.description = %q{Http requests for celluloid.}
12
+ gem.summary = %q{Http requests for celluloid. Based on Celluloid::IO.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency('celluloid-io')
21
+ gem.add_runtime_dependency('http_parser.rb')
22
+
23
+ gem.add_development_dependency('turn')
24
+ gem.add_development_dependency('minitest')
25
+ gem.add_development_dependency('mocha')
26
+ end
@@ -0,0 +1,39 @@
1
+ class Celluloid::Http::Connection
2
+ include Celluloid::IO
3
+ attr_reader :socket
4
+
5
+ def open(host, port = 80)
6
+ @socket = TCPSocket.new(host, port)
7
+ end
8
+
9
+ def close
10
+ socket.close
11
+ end
12
+
13
+ def send_request(request)
14
+ socket.write(request.to_s)
15
+ end
16
+
17
+ def response
18
+ response = Http::Response.new
19
+
20
+ until response.finished?
21
+ begin
22
+ chunk = socket.readpartial(4096)
23
+ rescue EOFError
24
+ # do nothing
25
+ ensure
26
+ response << chunk
27
+ end
28
+ end
29
+
30
+ response
31
+ end
32
+
33
+ def self.open(host, port = 80)
34
+ connection = self.new
35
+ connection.open(host, 80)
36
+ connection
37
+ end
38
+
39
+ end
@@ -0,0 +1,12 @@
1
+ module Celluloid::Http::Http
2
+
3
+ def send_requst(request)
4
+ connection = Celluloid::Http::Connection.open(request.host, request.port)
5
+ connection.send_request request
6
+
7
+ response = connection.response
8
+ connection.close
9
+ response
10
+ end
11
+
12
+ end
@@ -0,0 +1,22 @@
1
+ class Celluloid::Http::Request
2
+ extend Forwardable
3
+ DEFAULT_METHOD = :get
4
+ DEFAULT_HTTP_VERSION = '1.1'
5
+
6
+ attr_reader :url
7
+ attr_accessor :method
8
+
9
+ def_delegators :@uri, :scheme, :host, :port, :path, :query
10
+
11
+ def initialize(url, options = {})
12
+ @url = url
13
+ @uri = URI.parse url
14
+ @method = options[:method] || DEFAULT_METHOD
15
+ end
16
+
17
+ def to_s
18
+ path = self.path.length.zero? ? "/" : self.path
19
+ "#{method.to_s.upcase} #{path} HTTP/#{DEFAULT_HTTP_VERSION}\nHOST: #{host}\n\n"
20
+ end
21
+
22
+ end
@@ -0,0 +1,38 @@
1
+ class Celluloid::Http::Response
2
+ extend Forwardable
3
+
4
+ def_delegators :parser, :<<
5
+
6
+ attr_writer :status
7
+ attr_accessor :headers, :body
8
+
9
+ def initialize(status = nil, headers = {}, body = "")
10
+ @status, @headers, @body = status, headers, body
11
+ @finished = false
12
+ end
13
+
14
+ def status
15
+ @status || parser.status_code
16
+ end
17
+
18
+ def on_message_complete
19
+ @finished = true
20
+ end
21
+
22
+ def finished?
23
+ @finished
24
+ end
25
+
26
+ def on_body(chunk)
27
+ @body << chunk
28
+ end
29
+
30
+ def on_headers_complete(headers)
31
+ @headers = headers
32
+ end
33
+
34
+ def parser
35
+ @parser ||= Http::Parser.new(self)
36
+ end
37
+
38
+ end
@@ -0,0 +1,5 @@
1
+ module Celluloid
2
+ module Http
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ require "celluloid-http/version"
2
+ require "uri"
3
+ require "http_parser.rb"
4
+ require "celluloid/io"
5
+
6
+ module Celluloid
7
+ module Http
8
+ autoload :Response, 'celluloid-http/response'
9
+ autoload :Request, 'celluloid-http/request'
10
+ autoload :Http, 'celluloid-http/http'
11
+ autoload :Connection, 'celluloid-http/connection'
12
+
13
+ extend Http
14
+ end
15
+ end