net-ptth 0.0.3 → 0.0.4
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 +1 -1
- data/README.md +1 -1
- data/lib/net/ptth.rb +49 -23
- data/lib/net/ptth/test.rb +2 -2
- data/net-ptth.gemspec +1 -1
- data/test/ptth_test.rb +4 -14
- metadata +2 -2
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
data/lib/net/ptth.rb
CHANGED
@@ -42,7 +42,7 @@ class Net::PTTH
|
|
42
42
|
# Public: Closes de current connection
|
43
43
|
#
|
44
44
|
def close
|
45
|
-
log "
|
45
|
+
log "Closing connection"
|
46
46
|
@socket.close if @socket
|
47
47
|
end
|
48
48
|
|
@@ -55,27 +55,19 @@ class Net::PTTH
|
|
55
55
|
def request(req, &block)
|
56
56
|
@socket ||= TCPSocket.new(@host, @port)
|
57
57
|
|
58
|
-
log "
|
58
|
+
log "Connecting to #{@host}:#{@port}"
|
59
59
|
|
60
60
|
packet = build(req)
|
61
61
|
|
62
|
-
|
63
|
-
log packet
|
62
|
+
write(packet)
|
64
63
|
|
65
|
-
|
66
|
-
|
67
|
-
response = @socket.readpartial(1024)
|
68
|
-
log "[Reading response]"
|
69
|
-
log response
|
64
|
+
response = read
|
70
65
|
@parser << response
|
71
66
|
|
72
67
|
if @parser.http_status == 101
|
73
|
-
log "
|
74
|
-
|
75
|
-
while res = @socket.readpartial(1024)
|
76
|
-
log "[Incoming response]"
|
77
|
-
log res
|
68
|
+
log "Switching protocols"
|
78
69
|
|
70
|
+
while res = read
|
79
71
|
request = Request.new
|
80
72
|
build_request(request)
|
81
73
|
|
@@ -94,6 +86,23 @@ class Net::PTTH
|
|
94
86
|
|
95
87
|
private
|
96
88
|
|
89
|
+
def read(bytes = 1024)
|
90
|
+
response = @socket.readpartial(bytes)
|
91
|
+
|
92
|
+
log "Reading response"
|
93
|
+
log response, "-> "
|
94
|
+
|
95
|
+
response
|
96
|
+
end
|
97
|
+
|
98
|
+
def write(string)
|
99
|
+
log "Writting response"
|
100
|
+
log string[0..200], "<- "
|
101
|
+
|
102
|
+
bytes = @socket.write(string)
|
103
|
+
log "#{bytes} bytes written"
|
104
|
+
end
|
105
|
+
|
97
106
|
# Private: Executes the app and/or block callbacks
|
98
107
|
#
|
99
108
|
# env: The Rack compatible env
|
@@ -101,7 +110,9 @@ class Net::PTTH
|
|
101
110
|
#
|
102
111
|
def callbacks(env, &block)
|
103
112
|
case
|
104
|
-
when app
|
113
|
+
when app
|
114
|
+
response = build_response(*app.call(env))
|
115
|
+
write response
|
105
116
|
when block
|
106
117
|
request = Rack::Request.new(env)
|
107
118
|
block.call(request)
|
@@ -110,6 +121,16 @@ class Net::PTTH
|
|
110
121
|
end
|
111
122
|
end
|
112
123
|
|
124
|
+
def build_response(status, headers, body)
|
125
|
+
log "Building Response"
|
126
|
+
response = "HTTP/1.1 #{status} OK\n"
|
127
|
+
headers.each { |key, value| response += "#{key}: #{value}\n" }
|
128
|
+
response += "\n\r"
|
129
|
+
body.each { |chunk| response += chunk }
|
130
|
+
|
131
|
+
response
|
132
|
+
end
|
133
|
+
|
113
134
|
# Private: Builds a Rack compatible env from a PTTH::Request
|
114
135
|
#
|
115
136
|
# request: A PTTH parsed request
|
@@ -123,7 +144,7 @@ class Net::PTTH
|
|
123
144
|
}
|
124
145
|
|
125
146
|
env.tap do |h|
|
126
|
-
h["CONTENT_LENGTH"] = request.body.length if request.body
|
147
|
+
h["CONTENT_LENGTH"] = request.body.length if !request.body.nil?
|
127
148
|
end
|
128
149
|
|
129
150
|
env.merge!(request.headers) if request.headers
|
@@ -149,8 +170,10 @@ class Net::PTTH
|
|
149
170
|
#
|
150
171
|
# message: The string to be logged
|
151
172
|
#
|
152
|
-
def log(message)
|
153
|
-
|
173
|
+
def log(message, prepend = "* ")
|
174
|
+
parts = (message || "").split("\n")
|
175
|
+
@debug_output << parts.map { |line| prepend + line }.join("\n")
|
176
|
+
@debug_output << "\n"
|
154
177
|
end
|
155
178
|
|
156
179
|
# Private: Parses the incoming request headers and adds the information to a
|
@@ -182,15 +205,18 @@ class Net::PTTH
|
|
182
205
|
# req: The request to be build
|
183
206
|
#
|
184
207
|
def build(req)
|
185
|
-
req["Upgrade"]
|
186
|
-
req["Connection"]
|
208
|
+
req["Upgrade"] = "PTTH/1.0"
|
209
|
+
req["Connection"] ||= "Upgrade"
|
210
|
+
req["Content-Length"] ||= req.body.length if !req.body.nil?
|
187
211
|
|
188
212
|
package = "#{req.method} #{req.path} HTTP/1.1\n"
|
189
213
|
req.each_header do |header, value|
|
190
|
-
|
214
|
+
header_parts = header.split("-").map(&:capitalize)
|
215
|
+
package += "#{header_parts.join("-")}: #{value}\n"
|
191
216
|
end
|
192
217
|
|
193
|
-
package += "\n\r#{req.body}"
|
194
|
-
package += "\n\r\n
|
218
|
+
package += "\n\r#{req.body}" if req.body
|
219
|
+
package += "\r\n\r\n"
|
220
|
+
package
|
195
221
|
end
|
196
222
|
end
|
data/lib/net/ptth/test.rb
CHANGED
@@ -26,9 +26,8 @@ module Net
|
|
26
26
|
HTTP/1.1 101 Switching Protocols
|
27
27
|
Date: Mon, 14 Jan 2013 11:54:24 GMT
|
28
28
|
Upgrade: PTTH/1.0
|
29
|
+
Content-Length: 0
|
29
30
|
Connection: Upgrade
|
30
|
-
|
31
|
-
|
32
31
|
EOS
|
33
32
|
|
34
33
|
post_response = "#{@response.method} #{@response.path} HTTP/1.1\n"
|
@@ -40,6 +39,7 @@ module Net
|
|
40
39
|
client.puts switch_protocols
|
41
40
|
sleep 0.5
|
42
41
|
client.puts post_response
|
42
|
+
client.read
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
data/net-ptth.gemspec
CHANGED
data/test/ptth_test.rb
CHANGED
@@ -31,6 +31,7 @@ describe "Using a Rack compatible app to receive requests" do
|
|
31
31
|
|
32
32
|
@server = Net::PTTH::TestServer.new(port: 23045, response: response)
|
33
33
|
@ptth = Net::PTTH.new("http://localhost:23045")
|
34
|
+
@ptth.set_debug_output = $stdout if ENV['HTTP_DEBUG']
|
34
35
|
@request = Net::HTTP::Post.new("/reverse")
|
35
36
|
|
36
37
|
Thread.new { @server.start }
|
@@ -41,24 +42,18 @@ describe "Using a Rack compatible app to receive requests" do
|
|
41
42
|
end
|
42
43
|
|
43
44
|
def check_app_compatibility(app)
|
44
|
-
app.ptth = @ptth
|
45
45
|
@ptth.app = app
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
@ptth.async.request(@request)
|
48
|
+
sleep 1
|
49
|
+
@ptth.close
|
50
50
|
end
|
51
51
|
|
52
52
|
it "should be able to receive a Cuba app" do
|
53
53
|
CubaApp = Class.new(Cuba) do
|
54
|
-
class << self
|
55
|
-
attr_accessor :ptth
|
56
|
-
end
|
57
|
-
|
58
54
|
define do
|
59
55
|
on "custom_app" do
|
60
56
|
res.write "indeeed!"
|
61
|
-
self.class.ptth.close
|
62
57
|
end
|
63
58
|
end
|
64
59
|
end
|
@@ -68,12 +63,7 @@ describe "Using a Rack compatible app to receive requests" do
|
|
68
63
|
|
69
64
|
it "should be able to receive a sinatra app" do
|
70
65
|
SinatraApp = Sinatra.new do
|
71
|
-
class << self
|
72
|
-
attr_accessor :ptth
|
73
|
-
end
|
74
|
-
|
75
66
|
get "/custom_app" do
|
76
|
-
self.class.ptth.close
|
77
67
|
"indeeed!"
|
78
68
|
end
|
79
69
|
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.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|