knjappserver 0.0.19 → 0.0.20

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.19
1
+ 0.0.20
data/knjappserver.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{knjappserver}
8
- s.version = "0.0.19"
8
+ s.version = "0.0.20"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kasper Johansen"]
12
- s.date = %q{2012-03-04}
12
+ s.date = %q{2012-03-16}
13
13
  s.description = %q{Which supports a lot of undocumented stuff.}
14
14
  s.email = %q{k@spernj.org}
15
15
  s.executables = ["check_running.rb", "knjappserver_start.rb"]
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
30
30
  "bin/knjappserver_start.rb",
31
31
  "knjappserver.gemspec",
32
32
  "lib/conf/README",
33
+ "lib/conf/apache2_cgi_rhtml_conf.conf",
33
34
  "lib/conf/conf_example.rb",
34
35
  "lib/conf/conf_vars_example.rb",
35
36
  "lib/files/database_schema.rb",
@@ -0,0 +1,11 @@
1
+ ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
2
+
3
+ <Files *.fcgi>
4
+ Options ExecCGI
5
+ AddHandler fastcgi-script .fcgi
6
+ </Files>
7
+
8
+ <Files *.rhtml>
9
+ AddType application/x-httpd-knj .rhtml
10
+ Action application/x-httpd-knj "/cgi-bin/knjappserver_cgi.rb"
11
+ </Files>
@@ -14,6 +14,8 @@ class Knjappserver
14
14
  {"name" => "cookie_values_data_id", "type" => "bigint"},
15
15
  {"name" => "meta_keys_data_id", "type" => "bigint"},
16
16
  {"name" => "meta_values_data_id", "type" => "bigint"},
17
+ {"name" => "session_keys_data_id", "type" => "bigint"},
18
+ {"name" => "session_values_data_id", "type" => "bigint"},
17
19
  {"name" => "tag_data_id", "type" => "bigint"},
18
20
  {"name" => "comment_data_id", "type" => "bigint"}
19
21
  ],
@@ -91,7 +93,7 @@ class Knjappserver
91
93
  {"name" => "idhash", "type" => "varchar"},
92
94
  {"name" => "sess_data", "type" => "text"},
93
95
  {"name" => "date_added", "type" => "datetime"},
94
- {"name" => "date_lastused", "type" => "datetime", "on_created" => proc{|d| d["db"].query("UPDATE Session SET date_lastused = '#{Knj::Datet.new.dbstr}")}},
96
+ {"name" => "date_lastused", "type" => "datetime", "on_created" => proc{|d| d["db"].query("UPDATE Session SET date_lastused = '#{Knj::Datet.new.dbstr}'")}},
95
97
  {"name" => "ip", "type" => "varchar", "maxlength" => 15},
96
98
  {"name" => "user_agent", "type" => "text"},
97
99
  {"name" => "remember", "type" => "enum", "maxlength" => "'0','1'", "default" => 0, "comment" => "If the session should be remembered or not."}
@@ -28,10 +28,13 @@ class Knjappserver::Httpserver
28
28
  self.spawn_httpsession(@server.accept)
29
29
  STDOUT.print "Starting new HTTP-request.\n" if @debug
30
30
  rescue => e
31
- STDOUT.puts e.inspect
32
- STDOUT.puts e.backtrace
33
- STDOUT.print "\n"
34
- STDOUT.print "Could not accept HTTP-request - waiting 1 sec and then trying again.\n"
31
+ if @debug
32
+ STDOUT.puts e.inspect
33
+ STDOUT.puts e.backtrace
34
+ STDOUT.print "\n"
35
+ STDOUT.print "Could not accept HTTP-request - waiting 1 sec and then trying again.\n"
36
+ end
37
+
35
38
  sleep 1
36
39
  end
37
40
  end
@@ -70,7 +70,7 @@ class Knjappserver::Httpsession::Contentgroup
70
70
  def write_begin
71
71
  begin
72
72
  @resp.write if @httpsession.meta["METHOD"] != "HEAD"
73
- rescue Errno::ECONNRESET, Errno::ENOTCONN, Errno::EPIPE, Timeout::Error
73
+ rescue Errno::ECONNRESET, Errno::ENOTCONN, Errno::EPIPE
74
74
  #Ignore - the user probaly left.
75
75
  end
76
76
  end
@@ -195,11 +195,20 @@ class Knjappserver
195
195
 
196
196
  #Writes a custom log to the database.
197
197
  def log(msg, objs, args = {})
198
+ #This can come in handy if migrating logs to appserver-database.
199
+ if args[:date_saved]
200
+ date_saved = args[:date_saved]
201
+ else
202
+ date_saved = Time.now
203
+ end
204
+
205
+ objs = [objs] if !objs.is_a?(Array)
206
+
198
207
  @logs_mutex.synchronize do
199
- objs = [objs] if !objs.is_a?(Array)
200
208
  log_value_id = @ob.static(:Log_data_value, :force_id, msg)
209
+
201
210
  ins_data = {
202
- :date_saved => Time.now,
211
+ :date_saved => date_saved,
203
212
  :text_value_id => log_value_id
204
213
  }
205
214
 
@@ -227,6 +236,12 @@ class Knjappserver
227
236
  ins_data[:meta_values_data_id] = meta_hash[:values_data_id]
228
237
  end
229
238
 
239
+ session_hash = log_hash_ins(_session) if _session
240
+ if session_hash
241
+ ins_data[:session_keys_data_id] = session_hash[:keys_data_id]
242
+ ins_data[:session_values_data_id] = session_hash[:values_data_id]
243
+ end
244
+
230
245
  if args[:tag]
231
246
  tag_value_id = @ob.static(:Log_data_value, :force_id, args[:tag])
232
247
  ins_data[:tag_data_id] = tag_value_id
@@ -1,6 +1,10 @@
1
1
  class Knjappserver
2
2
  #Imports a .rhtml-file and executes it.
3
3
  def import(filepath)
4
+ if filepath.to_s.index("../proc/self") != nil
5
+ raise Knj::Errors::NoAccess, "Possible attempt to hack the appserver."
6
+ end
7
+
4
8
  _httpsession.eruby.import(filepath)
5
9
  end
6
10
 
@@ -122,6 +126,7 @@ class Knjappserver
122
126
 
123
127
  #Returns the socket-port the appserver is currently running on.
124
128
  def port
129
+ raise "Http-server not spawned yet. Call Knjappserver#start to spawn it." if !@httpserv
125
130
  return @httpserv.server.addr[1]
126
131
  end
127
132
  end
@@ -1,5 +1,9 @@
1
1
  class Knjappserver::Log < Knj::Datarow
2
- def self.list(d)
2
+ has_many [
3
+ {:class => :Log_link, :col => :log_id, :method => :links, :depends => true, :autodelete => true}
4
+ ]
5
+
6
+ def self.list(d, &block)
3
7
  sql = "SELECT #{table}.* FROM #{table}"
4
8
 
5
9
  if d.args["object_lookup"]
@@ -30,7 +34,7 @@ class Knjappserver::Log < Knj::Datarow
30
34
  sql << ret[:sql_order]
31
35
  sql << ret[:sql_limit]
32
36
 
33
- return d.ob.list_bysql(:Log, sql)
37
+ return d.ob.list_bysql(:Log, sql, &block)
34
38
  end
35
39
 
36
40
  def self.add(d)
@@ -71,6 +75,10 @@ class Knjappserver::Log < Knj::Datarow
71
75
  ob.args[:knjappserver].log_data_hash(self[:meta_keys_data_id], self[:meta_values_data_id])
72
76
  end
73
77
 
78
+ def session
79
+ ob.args[:knjappserver].log_data_hash(self[:session_keys_data_id], self[:session_values_data_id])
80
+ end
81
+
74
82
  def ip
75
83
  meta_d = self.meta
76
84
 
@@ -83,10 +91,6 @@ class Knjappserver::Log < Knj::Datarow
83
91
  lines = self.text.to_s.split("\n").first.to_s
84
92
  end
85
93
 
86
- def links(args = {})
87
- return ob.list(:Log_link, {"log" => self}.merge(args))
88
- end
89
-
90
94
  def objects_html(ob_use)
91
95
  html = ""
92
96
  first = true
@@ -12,8 +12,7 @@ class Knjappserver::Log_data_value < Knj::Datarow
12
12
  end
13
13
 
14
14
  def self.force_id(d, value)
15
- q_val = d.db.select(:Log_data_value, {"value" => value})
16
- while d_val = q_val.fetch
15
+ d.db.select(:Log_data_value, {"value" => value}) do |d_val|
17
16
  return d_val[:id].to_i if d_val[:value].to_s == value.to_s #MySQL doesnt take upper/lower-case into consideration because value is a text-column... lame! - knj
18
17
  end
19
18
 
@@ -1,9 +1,9 @@
1
1
  class Knjappserver::Log_link < Knj::Datarow
2
2
  has_one [
3
- {:class => :Log, :col => :log_id}
3
+ {:class => :Log, :col => :log_id, :method => :log}
4
4
  ]
5
5
 
6
- def self.list(d)
6
+ def self.list(d, &block)
7
7
  sql = "SELECT * FROM #{table} WHERE 1=1"
8
8
 
9
9
  ret = list_helper(d)
@@ -22,7 +22,7 @@ class Knjappserver::Log_link < Knj::Datarow
22
22
  sql << ret[:sql_order]
23
23
  sql << ret[:sql_limit]
24
24
 
25
- return d.ob.list_bysql(:Log_link, sql)
25
+ return d.ob.list_bysql(:Log_link, sql, &block)
26
26
  end
27
27
 
28
28
  def self.add(d)
@@ -3,10 +3,20 @@
3
3
  def _(str)
4
4
  kas = _kas
5
5
  session = _session
6
+ locale = nil
6
7
 
7
- return str.to_s.encode("utf-8") if !kas or !session
8
- session[:locale] = kas.config[:locale_default] if !session[:locale] and kas.config[:locale_default]
9
- raise "No locale set for session and ':locale_default' not set in config." if !session[:locale]
10
- str = kas.gettext.trans(session[:locale], str)
11
- return str.to_s.encode("utf-8")
8
+ if Thread.current[:locale].to_s.length > 0
9
+ locale = Thread.current[:locale]
10
+ elsif session and session[:locale].to_s.strip.length > 0
11
+ locale = session[:locale]
12
+ elsif kas and kas.config[:locale_default].to_s.strip.length > 0
13
+ session[:locale] = kas.config[:locale_default] if session
14
+ locale = kas.config[:locale_default]
15
+ elsif !session and !kas
16
+ return str
17
+ else
18
+ raise "No locale set for session and ':locale_default' not set in config."
19
+ end
20
+
21
+ return kas.gettext.trans(locale, str)
12
22
  end
@@ -6,6 +6,45 @@ time_begin = Time.now
6
6
  #Good for programming appserver-supported projects without running an appserver all the time,
7
7
  #but really slow because of startup for every request.
8
8
 
9
+ class Cgi_is_retarded
10
+ def env_table
11
+ return ENV
12
+ end
13
+
14
+ def request_method
15
+ return ENV["REQUEST_METHOD"]
16
+ end
17
+
18
+ def content_type
19
+ return ENV["CONTENT_TYPE"]
20
+ end
21
+
22
+ def cgi
23
+ @cgi = CGI.new if !@cgi
24
+ return @cgi
25
+ end
26
+
27
+ def params
28
+ return self.cgi.params
29
+ end
30
+
31
+ def print(arg)
32
+ Kernel.print arg.to_s
33
+ end
34
+ end
35
+
36
+ class Knjappserver
37
+ def self.convert_fcgi_post(params)
38
+ post_hash = {}
39
+
40
+ params.each do |key, val|
41
+ post_hash[key] = val.first
42
+ end
43
+
44
+ return post_hash
45
+ end
46
+ end
47
+
9
48
  begin
10
49
  require "knj/autoload"
11
50
  require "#{File.dirname(Knj::Os.realpath(__FILE__))}/../knjappserver.rb"
@@ -19,23 +58,15 @@ begin
19
58
  raise "No 'Knjappserver::CGI_CONF'-constant was spawned by '#{ENV["HTTP_KNJAPPSERVER_CGI_CONFIG"]}'."
20
59
  end
21
60
 
22
-
23
- headers = {}
24
- ENV.each do |key, val|
25
- if key[0, 5] == "HTTP_" and key != "HTTP_KNJAPPSERVER_CGI_CONFIG"
26
- key = Knj::Php.ucwords(key[5, key.length].gsub("_", " ")).gsub(" ", "-")
27
- headers[key] = val
28
- end
29
- end
30
-
31
- knjappserver_conf = Knjappserver::CGI_CONF["knjappserver"].merge(
61
+ #Spawn appserver.
62
+ knjappserver_conf = {
32
63
  :cmdline => false,
33
64
  :events => false,
34
65
  :cleaner => false,
35
66
  :dbrev => false,
36
67
  :mail_require => false,
37
68
  :port => 0 #Ruby picks random port and we get the actual port after starting the appserver.
38
- )
69
+ }.merge(Knjappserver::CGI_CONF["knjappserver"])
39
70
  knjappserver = Knjappserver.new(knjappserver_conf)
40
71
  knjappserver.start
41
72
  port = knjappserver.port
@@ -44,16 +75,69 @@ begin
44
75
  #Make request.
45
76
  http = Knj::Http2.new(:host => "localhost", :port => port)
46
77
 
47
- count = 0
48
- http.get(ENV["PATH_INFO"][1, ENV["PATH_INFO"].length], {
49
- :default_headers => headers,
50
- :cookies => false,
51
- :on_content => proc{|line|
52
- print line if count > 0
53
- count += 1
54
- }
55
- })
78
+
79
+ #Spawn CGI-variable to emulate FCGI part.
80
+ cgi = Cgi_is_retarded.new
81
+
82
+
83
+ #The rest is copied from the FCGI-part.
84
+ headers = {}
85
+ cgi.env_table.each do |key, val|
86
+ if key[0, 5] == "HTTP_" and key != "HTTP_KNJAPPSERVER_CGI_CONFIG"
87
+ key = Knj::Php.ucwords(key[5, key.length].gsub("_", " ")).gsub(" ", "-")
88
+ headers[key] = val
89
+ end
90
+ end
91
+
92
+ #Make request.
93
+ if cgi.env_table["PATH_INFO"].length > 0 and cgi.env_table["PATH_INFO"] != "/"
94
+ url = cgi.env_table["PATH_INFO"][1, cgi.env_table["PATH_INFO"].length]
95
+ else
96
+ url = "index.rhtml"
97
+ end
98
+
99
+ if cgi.env_table["QUERY_STRING"].to_s.length > 0
100
+ url << "?#{cgi.env_table["QUERY_STRING"]}"
101
+ end
102
+
103
+ #cgi.print "Content-Type: text/html\r\n"
104
+ #cgi.print "\r\n"
105
+ #cgi.print Knj::Php.print_r(cgi.params, true)
106
+
107
+ if cgi.request_method == "POST" and cgi.content_type.to_s.downcase.index("multipart/form-data") != nil
108
+ count = 0
109
+ http.post_multipart(url, Knjappserver.convert_fcgi_post(cgi.params), {
110
+ :default_headers => headers,
111
+ :cookies => false,
112
+ :on_content => proc{|line|
113
+ cgi.print(line) if count > 0
114
+ count += 1
115
+ }
116
+ })
117
+ elsif cgi.request_method == "POST"
118
+ count = 0
119
+ http.post(url, Knjappserver.convert_fcgi_post(cgi.params), {
120
+ :default_headers => headers,
121
+ :cookies => false,
122
+ :on_content => proc{|line|
123
+ cgi.print(line) if count > 0
124
+ count += 1
125
+ }
126
+ })
127
+ else
128
+ count = 0
129
+ http.get(url, {
130
+ :default_headers => headers,
131
+ :cookies => false,
132
+ :on_content => proc{|line|
133
+ cgi.print(line) if count > 0
134
+ count += 1
135
+ }
136
+ })
137
+ end
56
138
  rescue Exception => e
139
+ knjappserver.stop
140
+
57
141
  print "Content-Type: text/html\r\n"
58
142
  print "\n\n"
59
143
  print Knj::Errors.error_str(e, {:html => true})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knjappserver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.19
4
+ version: 0.0.20
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-04 00:00:00.000000000 +01:00
12
+ date: 2012-03-16 00:00:00.000000000 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: knjrbfw
17
- requirement: &28196100 !ruby/object:Gem::Requirement
17
+ requirement: &12366780 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *28196100
25
+ version_requirements: *12366780
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: erubis
28
- requirement: &28195520 !ruby/object:Gem::Requirement
28
+ requirement: &12365600 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *28195520
36
+ version_requirements: *12365600
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: mail
39
- requirement: &28194940 !ruby/object:Gem::Requirement
39
+ requirement: &12365060 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *28194940
47
+ version_requirements: *12365060
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rspec
50
- requirement: &28194360 !ruby/object:Gem::Requirement
50
+ requirement: &12364340 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 2.3.0
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *28194360
58
+ version_requirements: *12364340
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: bundler
61
- requirement: &28193840 !ruby/object:Gem::Requirement
61
+ requirement: &12363160 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ~>
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: 1.0.0
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *28193840
69
+ version_requirements: *12363160
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: jeweler
72
- requirement: &28193300 !ruby/object:Gem::Requirement
72
+ requirement: &12362140 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ~>
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: 1.6.3
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *28193300
80
+ version_requirements: *12362140
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: rcov
83
- requirement: &28192780 !ruby/object:Gem::Requirement
83
+ requirement: &12352800 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: '0'
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *28192780
91
+ version_requirements: *12352800
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: sqlite3
94
- requirement: &28192240 !ruby/object:Gem::Requirement
94
+ requirement: &12352260 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ! '>='
@@ -99,10 +99,10 @@ dependencies:
99
99
  version: '0'
100
100
  type: :development
101
101
  prerelease: false
102
- version_requirements: *28192240
102
+ version_requirements: *12352260
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: json
105
- requirement: &28190420 !ruby/object:Gem::Requirement
105
+ requirement: &12351520 !ruby/object:Gem::Requirement
106
106
  none: false
107
107
  requirements:
108
108
  - - ! '>='
@@ -110,7 +110,7 @@ dependencies:
110
110
  version: '0'
111
111
  type: :development
112
112
  prerelease: false
113
- version_requirements: *28190420
113
+ version_requirements: *12351520
114
114
  description: Which supports a lot of undocumented stuff.
115
115
  email: k@spernj.org
116
116
  executables:
@@ -133,6 +133,7 @@ files:
133
133
  - bin/knjappserver_start.rb
134
134
  - knjappserver.gemspec
135
135
  - lib/conf/README
136
+ - lib/conf/apache2_cgi_rhtml_conf.conf
136
137
  - lib/conf/conf_example.rb
137
138
  - lib/conf/conf_vars_example.rb
138
139
  - lib/files/database_schema.rb
@@ -206,7 +207,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
206
207
  version: '0'
207
208
  segments:
208
209
  - 0
209
- hash: -2708344382107765429
210
+ hash: 894779464839235539
210
211
  required_rubygems_version: !ruby/object:Gem::Requirement
211
212
  none: false
212
213
  requirements: