net-ptth 0.0.1 → 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.
data/Gemfile.lock CHANGED
@@ -1,9 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- net-ptth (0.0.1)
4
+ net-ptth (0.0.2)
5
5
  celluloid-io (~> 0.12.1)
6
6
  http-parser-lite (~> 0.6.0)
7
+ rack (~> 1.4.4)
7
8
 
8
9
  GEM
9
10
  remote: http://rubygems.org/
@@ -18,6 +19,7 @@ GEM
18
19
  http-parser-lite (0.6.0)
19
20
  minitest (4.4.0)
20
21
  nio4r (0.4.3)
22
+ rack (1.4.4)
21
23
  rake (10.0.3)
22
24
  timers (1.0.2)
23
25
 
data/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  `Net::HTTP` compatible `PTTH` client
4
4
 
5
+ ## You misspelled HTTP
6
+
7
+ No I don't: http://wiki.secondlife.com/wiki/Reverse_HTTP
8
+
5
9
  ## Installation
6
10
 
7
11
  ```bash
@@ -16,7 +20,11 @@ require 'net/http'
16
20
 
17
21
  ptth = Net::PTTH.new("http://localhost:23045")
18
22
  request = Net::HTTP::Post.new("/reverse")
19
- ptth.request(request) do |body, headers|
23
+ ptth.request(request) do |incomming_request|
20
24
  # Handle the body of the incomming request through the reverse connection
25
+ # This will be executed with each new request.
26
+ # incomming_request it's a Rack::Request object
27
+ # You can close the connection with:
28
+ # ptth.close
21
29
  end
22
30
  ```
@@ -0,0 +1,53 @@
1
+ require "net/http"
2
+
3
+ module Net
4
+ class PTTH
5
+ class TestServer
6
+ # Public: Initialize the PTTH test server
7
+ #
8
+ # port: the port in which the server will listen
9
+ #
10
+ def initialize(configuration = {})
11
+ port = configuration.fetch(:port, 23045)
12
+ response = Net::HTTP::Post.new("/reverse")
13
+ response.body = "reversed"
14
+
15
+ @response = configuration.fetch(:response, response)
16
+ @server = TCPServer.new(port)
17
+ end
18
+
19
+ # Public: Starts the test server
20
+ #
21
+ def start
22
+ loop do
23
+ client = @server.accept
24
+
25
+ switch_protocols = <<-EOS.gsub(/^\s+/, '')
26
+ HTTP/1.1 101 Switching Protocols
27
+ Date: Mon, 14 Jan 2013 11:54:24 GMT
28
+ Upgrade: PTTH/1.0
29
+ Connection: Upgrade
30
+
31
+
32
+ EOS
33
+
34
+ post_response = "#{@response.method} #{@response.path} HTTP/1.1\n"
35
+ post_response += "Content-Length: #{@response.body.length}\n" if @response.body
36
+ post_response += "Accept: */*\n"
37
+ post_response += "\n"
38
+ post_response += @response.body if @response.body
39
+
40
+ client.puts switch_protocols
41
+ sleep 0.5
42
+ client.puts post_response
43
+ end
44
+ end
45
+
46
+ # Public: Stops the current server
47
+ #
48
+ def close
49
+ @server.close
50
+ end
51
+ end
52
+ end
53
+ end
data/lib/net/ptth.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "uri"
2
+ require "rack"
2
3
  require "net/http"
3
4
  require "http-parser"
4
5
  require "celluloid/io"
@@ -66,13 +67,32 @@ class Net::PTTH
66
67
  log res
67
68
 
68
69
  headers = {}
69
- body = nil
70
+ path = nil
71
+ body = ""
70
72
 
71
73
  @parser.reset
72
74
  parse_headers(headers)
73
75
 
74
- @parser.on_body { |response_body| body = response_body }
75
- @parser.on_message_complete { block.call(body, headers) }
76
+ @parser.on_url { |url| path = url }
77
+ @parser.on_body { |response_body| body = StringIO.new(response_body) }
78
+ @parser.on_message_complete do
79
+ env = {
80
+ "PATH_INFO" => path,
81
+ "rack.input" => body,
82
+ "REQUEST_METHOD" => @parser.http_method,
83
+ }
84
+
85
+ env.tap do |h|
86
+ h["CONTENT_LENGTH"] = body.length if body
87
+ end
88
+
89
+ request = Rack::Request.new(env)
90
+ headers.each do |header, value|
91
+ request[header] = value
92
+ end
93
+
94
+ block.call(request)
95
+ end
76
96
 
77
97
  @parser << res
78
98
  end
data/net-ptth.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "net-ptth"
3
- s.version = "0.0.1"
3
+ s.version = "0.0.2"
4
4
  s.summary = "Net::HTTP compatible reverse HTTP version"
5
5
  s.description = "PTTH Ruby client. Net::HTTP compatible... kind of"
6
6
  s.authors = ["elcuervo"]
@@ -10,6 +10,7 @@ Gem::Specification.new do |s|
10
10
  s.files = `git ls-files`.split("\n")
11
11
  s.test_files = `git ls-files test`.split("\n")
12
12
 
13
+ s.add_dependency("rack", "~> 1.4.4")
13
14
  s.add_dependency("celluloid-io", "~> 0.12.1")
14
15
  s.add_dependency("http-parser-lite", "~> 0.6.0")
15
16
 
data/test/ptth_test.rb CHANGED
@@ -1,46 +1,46 @@
1
1
  require_relative "./test_helper"
2
2
  require "net/ptth"
3
+ require "net/ptth/test"
3
4
 
4
5
  describe "PTTH connection" do
5
6
  before do
6
- Thread.new do
7
- @server = TCPServer.new(23045)
8
-
9
- loop do
10
- client = @server.accept
7
+ @server = Net::PTTH::TestServer.new(port: 23045)
8
+ Thread.new { @server.start }
9
+ end
11
10
 
12
- switch_protocols = <<-EOS.gsub(/^\s+/, '')
13
- HTTP/1.1 101 Switching Protocols
14
- Date: Mon, 14 Jan 2013 11:54:24 GMT
15
- Upgrade: PTTH/1.0
16
- Connection: Upgrade
11
+ after do
12
+ @server.close
13
+ end
17
14
 
15
+ it "should stablish a reverse connection" do
16
+ ptth = Net::PTTH.new("http://localhost:23045")
17
+ request = Net::HTTP::Post.new("/reverse")
18
18
 
19
- EOS
19
+ ptth.request(request) do |incoming_request|
20
+ assert_equal "reversed", incoming_request.body.read
21
+ ptth.close
22
+ end
23
+ end
24
+ end
20
25
 
21
- post_response = "POST /reversed HTTP/1.1\n"
22
- post_response += "Content-Length: 8\n"
23
- post_response += "Accept: */*\n"
24
- post_response += "\n"
25
- post_response += "reversed"
26
+ describe "PTTH Test server" do
27
+ before do
28
+ response = Net::HTTP::Get.new("/other")
29
+ @server = Net::PTTH::TestServer.new(port: 23045, response: response)
26
30
 
27
- client.puts switch_protocols
28
- sleep 0.5
29
- client.puts post_response
30
- end
31
- end
31
+ Thread.new { @server.start }
32
32
  end
33
33
 
34
34
  after do
35
35
  @server.close
36
36
  end
37
37
 
38
- it "should stablish a reverse connection" do
38
+ it "should be able to switch the incoming test request" do
39
39
  ptth = Net::PTTH.new("http://localhost:23045")
40
40
  request = Net::HTTP::Post.new("/reverse")
41
41
 
42
- ptth.request(request) do |body|
43
- assert_equal "reversed", body
42
+ ptth.request(request) do |incoming_request|
43
+ assert_equal "/other", incoming_request.path
44
44
  ptth.close
45
45
  end
46
46
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: net-ptth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-14 00:00:00.000000000 Z
12
+ date: 2013-01-15 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.4.4
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.4.4
14
30
  - !ruby/object:Gem::Dependency
15
31
  name: celluloid-io
16
32
  requirement: !ruby/object:Gem::Requirement
@@ -73,6 +89,7 @@ files:
73
89
  - README.md
74
90
  - Rakefile
75
91
  - lib/net/ptth.rb
92
+ - lib/net/ptth/test.rb
76
93
  - net-ptth.gemspec
77
94
  - test/ptth_test.rb
78
95
  - test/test_helper.rb