minimal-http-ruby 0.0.10 → 0.0.11
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 +4 -4
- data/lib/minimal-http-ruby.rb +44 -10
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eef69bbeb0cfad8e22dcbafca9716c671772002c
|
4
|
+
data.tar.gz: 8ca4da27082de1966273176c016db7e2578dd95e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0bf337dfcf3e152f2d1c89176aef70fb7626a799271f5f185ee16118844bd57bf932e98719e156cbceed95c19c860ef0313391750bf22958f7b3d139aff85fce
|
7
|
+
data.tar.gz: 37eca392caa5f0a1766496aaaabe69263dc679f31ec4f66e2ece79373ad8d362ea97059d69843dc80a2d0e87b2a98986c4fd5b1899dc5213d818f5e2f93544fa
|
data/lib/minimal-http-ruby.rb
CHANGED
@@ -11,8 +11,9 @@ require 'ipaddr'
|
|
11
11
|
require 'time'
|
12
12
|
require 'thread'
|
13
13
|
require 'mimemagic'
|
14
|
-
require
|
14
|
+
require 'cgi'
|
15
15
|
|
16
|
+
$sessions={}
|
16
17
|
|
17
18
|
def minimal_http_server options={}
|
18
19
|
prev_t={}
|
@@ -54,8 +55,8 @@ def minimal_http_server options={}
|
|
54
55
|
end
|
55
56
|
raw=raw.chop
|
56
57
|
method,req,http_proto = raw.split " "
|
57
|
-
puts "method:#{method}"
|
58
|
-
pp req
|
58
|
+
#puts "method:#{method}"
|
59
|
+
#pp req
|
59
60
|
status="200"
|
60
61
|
type="text/html"
|
61
62
|
req="/#{http_app||'index'}.html" if req=="/" or req=="/index.htm" or req=="/index.html"
|
@@ -70,15 +71,37 @@ def minimal_http_server options={}
|
|
70
71
|
len=$1.to_i if line[/Content-Length: (\d+)/]
|
71
72
|
break if line=="\r\n"
|
72
73
|
end
|
73
|
-
argss=
|
74
|
+
argss=""
|
75
|
+
while argss.size<len
|
76
|
+
argss+=client.readpartial(len-argss.size)
|
77
|
+
puts argss
|
78
|
+
end
|
79
|
+
#argss=URI.decode(argss)
|
80
|
+
puts argss
|
74
81
|
end
|
75
82
|
if argss
|
76
83
|
argss.split("&").each do |a|
|
84
|
+
#puts ">>><found '#{a}'"
|
77
85
|
if a
|
78
|
-
k,v=
|
79
|
-
|
86
|
+
k,v=a.split "="
|
87
|
+
next if not v
|
88
|
+
next if not k
|
89
|
+
k=CGI.unescape(k).force_encoding("UTF-8")
|
90
|
+
v=CGI.unescape(v).force_encoding("UTF-8")
|
91
|
+
#args[k]=CGI.unescape(CGI.unescape(v)) #.force_encoding("UTF-8")
|
92
|
+
if k[/(.+)\[\]$/]
|
93
|
+
#puts "IS ARRAY--------------------- #{k} #{$1}"
|
94
|
+
args[$1]=[] if not args[$1]
|
95
|
+
args[$1]<<v
|
96
|
+
else
|
97
|
+
args[k]=v #CGI.unescape(v) #.force_encoding("UTF-8")
|
98
|
+
end
|
99
|
+
#puts "'#{v}' --> #{args[k]}"
|
80
100
|
end
|
81
101
|
end
|
102
|
+
#puts "args:"
|
103
|
+
#pp args
|
104
|
+
#puts "------"
|
82
105
|
end
|
83
106
|
if req[/\.html$/] and File.file?(fn="#{$http_dir}haml#{req.gsub('.html','.haml')}")
|
84
107
|
contents = File.read(fn) # never cached -- may be dynamically generated
|
@@ -89,10 +112,12 @@ def minimal_http_server options={}
|
|
89
112
|
if not prev_t[fn] or prev_t[fn]<t
|
90
113
|
contents = File.read(fn)
|
91
114
|
begin
|
92
|
-
response=CoffeeScript.compile contents
|
115
|
+
response=CoffeeScript.compile contents.force_encoding("UTF-8")
|
93
116
|
prev_t[fn]=t
|
94
117
|
cache[fn]=response
|
95
118
|
rescue => e
|
119
|
+
puts "**** COFFEE #{fn} failed: #{e}"
|
120
|
+
pp e.backtrace
|
96
121
|
type="text/html"
|
97
122
|
status="500"
|
98
123
|
response="Coffee Compile error: #{e}"
|
@@ -119,13 +144,13 @@ def minimal_http_server options={}
|
|
119
144
|
if type!="text/event-stream" and status=="200"
|
120
145
|
begin
|
121
146
|
type,response=eval "json_#{act} [method,req,http_proto],args,0,0" #event handlers get called with zero session => init :)
|
147
|
+
response=response.to_json
|
122
148
|
rescue => e
|
123
149
|
puts "**** AJAX EXEC #{fn} failed: #{e}"
|
124
150
|
pp e.backtrace
|
125
151
|
response=[{act: :error, msg:"Error executing JSON",alert: "Syntax error '#{e}' in '#{fn}'"}].to_json
|
126
152
|
type="application/json"
|
127
153
|
end
|
128
|
-
response=response.to_json
|
129
154
|
end
|
130
155
|
elsif File.file?(fnc="#{$http_dir}#{req}")
|
131
156
|
type=MimeMagic.by_path(req)
|
@@ -146,16 +171,20 @@ def minimal_http_server options={}
|
|
146
171
|
if type!="text/event-stream"
|
147
172
|
client.print "Content-Length: #{response.bytesize}\r\n"
|
148
173
|
client.print "Connection: close\r\n"
|
174
|
+
client.print "Access-Control-Allow-Origin: *\r\n"
|
149
175
|
client.print "\r\n"
|
150
176
|
client.print response
|
151
177
|
else
|
152
178
|
client.print "Expires: -1\r\n"
|
179
|
+
client.print "Access-Control-Allow-Origin: *\r\n"
|
153
180
|
client.print "\r\n"
|
181
|
+
my_session=nil
|
154
182
|
begin
|
155
183
|
my_session=client.peeraddr[1]
|
156
184
|
if not @http_sessions[my_session]
|
157
185
|
@http_sessions[my_session]={client_port:client.peeraddr[1],client_ip:client.peeraddr[2] , log_position:0 }
|
158
186
|
end
|
187
|
+
$sessions[my_session]={}
|
159
188
|
my_event=0
|
160
189
|
loop do
|
161
190
|
begin
|
@@ -172,8 +201,8 @@ def minimal_http_server options={}
|
|
172
201
|
client.print "retry: 1000\n"
|
173
202
|
client.print "data: #{response.to_json}\n\n"
|
174
203
|
end
|
175
|
-
sleep
|
176
|
-
break if my_event>
|
204
|
+
sleep 0.001
|
205
|
+
break if my_event>10000
|
177
206
|
end
|
178
207
|
rescue Errno::EPIPE
|
179
208
|
#quite normal ;)
|
@@ -181,6 +210,10 @@ def minimal_http_server options={}
|
|
181
210
|
puts "stream #{client} died #{e}"
|
182
211
|
pp e.backtrace
|
183
212
|
end
|
213
|
+
if my_session and $sessions[my_session] and $sessions[my_session][:thread] and $sessions[my_session][:thread].status
|
214
|
+
puts "HEY SOMETHING WAS RUNNING"
|
215
|
+
$sessions[my_session][:thread].kill
|
216
|
+
end
|
184
217
|
end
|
185
218
|
dur=sprintf "%.2f",(Time.now.to_f-@start.to_f)
|
186
219
|
puts "#{client_ip}:#{client_port} #{Time.now.iso8601} \"#{method} #{req}\" #{status} #{response.bytesize} \"#{type}\" #{dur}" if options[:verbose]
|
@@ -194,6 +227,7 @@ def minimal_http_server options={}
|
|
194
227
|
client.print "HTTP/1.1 #{status} #{statuses[status]||'???'}\r\nContent-Type: #{type}\r\n\r\n"
|
195
228
|
client.print response
|
196
229
|
client.close
|
230
|
+
puts e
|
197
231
|
pp e.backtrace
|
198
232
|
end
|
199
233
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minimal-http-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ari Siitonen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: haml
|