knjappserver 0.0.6

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.
Files changed (47) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +18 -0
  4. data/Gemfile.lock +39 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +19 -0
  7. data/Rakefile +49 -0
  8. data/VERSION +1 -0
  9. data/bin/check_running.rb +71 -0
  10. data/bin/knjappserver_start.rb +50 -0
  11. data/knjappserver.gemspec +107 -0
  12. data/lib/conf/README +1 -0
  13. data/lib/conf/conf_example.rb +109 -0
  14. data/lib/conf/conf_vars_example.rb +3 -0
  15. data/lib/files/database_schema.rb +111 -0
  16. data/lib/files/run/README +1 -0
  17. data/lib/include/class_customio.rb +21 -0
  18. data/lib/include/class_erbhandler.rb +36 -0
  19. data/lib/include/class_httpresp.rb +91 -0
  20. data/lib/include/class_httpserver.rb +91 -0
  21. data/lib/include/class_httpsession.rb +350 -0
  22. data/lib/include/class_httpsession_knjengine.rb +189 -0
  23. data/lib/include/class_httpsession_mongrel.rb +75 -0
  24. data/lib/include/class_httpsession_webrick.rb +75 -0
  25. data/lib/include/class_knjappserver.rb +455 -0
  26. data/lib/include/class_knjappserver_cleaner.rb +109 -0
  27. data/lib/include/class_knjappserver_errors.rb +117 -0
  28. data/lib/include/class_knjappserver_logging.rb +272 -0
  29. data/lib/include/class_knjappserver_mailing.rb +97 -0
  30. data/lib/include/class_knjappserver_threadding.rb +87 -0
  31. data/lib/include/class_knjappserver_web.rb +23 -0
  32. data/lib/include/class_log.rb +81 -0
  33. data/lib/include/class_log_access.rb +103 -0
  34. data/lib/include/class_log_data.rb +42 -0
  35. data/lib/include/class_log_data_link.rb +16 -0
  36. data/lib/include/class_log_data_value.rb +34 -0
  37. data/lib/include/class_log_link.rb +51 -0
  38. data/lib/include/class_session.rb +43 -0
  39. data/lib/include/gettext_funcs.rb +10 -0
  40. data/lib/include/magic_methods.rb +59 -0
  41. data/lib/knjappserver.rb +1 -0
  42. data/lib/pages/logs_latest.rhtml +57 -0
  43. data/lib/pages/logs_show.rhtml +32 -0
  44. data/lib/pages/spec.rhtml +10 -0
  45. data/spec/knjappserver_spec.rb +110 -0
  46. data/spec/spec_helper.rb +12 -0
  47. metadata +188 -0
@@ -0,0 +1,189 @@
1
+ require "uri"
2
+ require "cgi"
3
+
4
+ if RUBY_PLATFORM == "java" or RUBY_ENGINE == "rbx"
5
+ BasicSocket.do_not_reverse_lookup = true
6
+ end
7
+
8
+ class Knjappserver::Httpsession::Knjengine
9
+ attr_reader :get, :post, :cookie, :meta, :page_path, :headers
10
+
11
+ def initialize(args)
12
+ @args = args
13
+ @kas = @args[:kas]
14
+ @crlf = "\r\n"
15
+ end
16
+
17
+ def read_socket
18
+ loop do
19
+ raise Errno::ECONNRESET, "Socket closed." if @socket.closed?
20
+ read = @socket.gets
21
+ raise Errno::ECONNRESET, "Socket returned non-string." if !read.is_a?(String)
22
+ @cont += read
23
+ break if @cont[-4..-1] == "\r\n\r\n" or @cont[-2..-1] == "\n\n"
24
+ end
25
+ end
26
+
27
+ def socket_parse(socket)
28
+ @cont = ""
29
+ @socket = socket
30
+ self.read_socket
31
+
32
+ #Parse URI (page_path and get).
33
+ match = @cont.match(/^(GET|POST|HEAD) (.+) HTTP\/1\.(\d+)\s*/)
34
+ if !match
35
+ raise "Could not parse request: '#{@cont.split("\n").first}'."
36
+ end
37
+
38
+ method = match[1]
39
+ @cont = @cont.gsub(match[0], "")
40
+ uri = URI.parse(match[2])
41
+
42
+ page_filepath = CGI.unescape(uri.path)
43
+ if page_filepath.length <= 0 or page_filepath == "/" or File.directory?("#{@kas.config[:doc_root]}/#{page_filepath}")
44
+ page_filepath = "#{page_filepath}/#{@kas.config[:default_page]}"
45
+ end
46
+
47
+ @page_path = "#{@kas.config[:doc_root]}/#{page_filepath}"
48
+ @get = Knj::Web.parse_urlquery(uri.query.to_s, {:urldecode => true})
49
+
50
+
51
+ #Parse headers, cookies and meta.
52
+ if RUBY_PLATFORM == "java" or RUBY_ENGINE == "rbx"
53
+ if @kas.config[:peeraddr_static]
54
+ addr_peer = [0, 0, @kas.config[:peeraddr_static]]
55
+ else
56
+ addr_peer = @socket.peeraddr
57
+ end
58
+
59
+ addr = @socket.addr
60
+ else
61
+ addr = @socket.addr(false)
62
+ addr_peer = @socket.peeraddr(false)
63
+ end
64
+
65
+ @headers = {}
66
+ @cookie = {}
67
+ @meta = {
68
+ "REQUEST_METHOD" => method,
69
+ "QUERY_STRING" => uri.query,
70
+ "REQUEST_URI" => match[2],
71
+ "REMOTE_ADDR" => addr[2],
72
+ "REMOTE_PORT" => addr[1],
73
+ "SERVER_ADDR" => addr_peer[2],
74
+ "SERVER_PORT" => addr_peer[1],
75
+ "SCRIPT_NAME" => uri.path
76
+ }
77
+
78
+ @cont.scan(/(\S+):\s*(.+)\r\n/) do |header_match|
79
+ key = header_match[0].downcase
80
+ val = header_match[1]
81
+
82
+ @headers[key] = [] if !@headers.has_key?(key)
83
+ @headers[key] << val
84
+
85
+ case key
86
+ when "host"
87
+ @meta["HTTP_HOST"] = val
88
+ when "connection"
89
+ @meta["HTTP_CONNECTION"] = val
90
+ when "accept"
91
+ @meta["HTTP_ACCEPT"] = val
92
+ when "accept-encoding"
93
+ @meta["HTTP_ACCEPT_ENCODING"] = val
94
+ when "accept-language"
95
+ @meta["HTTP_ACCEPT_LANGUAGE"] = val
96
+ when "accept-charset"
97
+ @meta["HTTP_ACCEPT_CHARSET"] = val
98
+ when "user-agent"
99
+ @meta["HTTP_USER_AGENT"] = val
100
+ when "referer"
101
+ @meta["HTTP_REFERER"] = val
102
+ when "cookie"
103
+ Knj::Web.parse_cookies(val).each do |key, val|
104
+ @cookie[key] = val
105
+ end
106
+ end
107
+ end
108
+
109
+
110
+ #Parse post
111
+ @post = {}
112
+
113
+ if method == "POST"
114
+ post_treated = {}
115
+ post_data = @socket.read(@headers["content-length"][0].to_i)
116
+
117
+ if @headers["content-type"] and match = @headers["content-type"][0].match(/^multipart\/form-data; boundary=(.+)\Z/)
118
+ io = StringIO.new(post_data)
119
+ post_treated = parse_form_data(io, match[1])
120
+ else
121
+ post_data.split("&").each do |splitted|
122
+ splitted = splitted.split("=")
123
+ post_treated[Knj::Php.urldecode(splitted[0])] = splitted[1]
124
+ end
125
+ end
126
+
127
+ self.convert_webrick_post(@post, post_treated, {:urldecode => true})
128
+ end
129
+ end
130
+
131
+ def convert_webrick_post(seton, webrick_post, args = {})
132
+ webrick_post.each do |varname, value|
133
+ Knj::Web.parse_name(seton, varname, value, args)
134
+ end
135
+ end
136
+
137
+ #Thanks to WEBrick
138
+ def parse_form_data(io, boundary)
139
+ boundary_regexp = /\A--#{boundary}(--)?#{@crlf}\z/
140
+ form_data = Hash.new
141
+ return form_data unless io
142
+ data = nil
143
+ io.each{|line|
144
+ if boundary_regexp =~ line
145
+ if data
146
+ data.chop!
147
+ key = data.name
148
+ if form_data.has_key?(key)
149
+ form_data[key].append_data(data)
150
+ else
151
+ form_data[key] = data
152
+ end
153
+ end
154
+ data = WEBrick::HTTPUtils::FormData.new
155
+ next
156
+ else
157
+ if data
158
+ data << line
159
+ end
160
+ end
161
+ }
162
+ return form_data
163
+ end
164
+
165
+ def destroy
166
+ @args.clear if @args
167
+ @args = nil
168
+ @kas = nil
169
+ @socket = nil
170
+ @cont = nil
171
+
172
+ @meta.clear if @meta
173
+ @meta = nil
174
+
175
+ @page_path = nil
176
+
177
+ @get.clear if @get
178
+ @get = nil
179
+
180
+ @post.clear if @post
181
+ @post = nil
182
+
183
+ @headers.clear if @headers
184
+ @headers = nil
185
+
186
+ @cookie.clear if @cookie
187
+ @cookie = nil
188
+ end
189
+ end
@@ -0,0 +1,75 @@
1
+ class Knjappserver::Httpsession::Webrick
2
+ attr_reader :get, :post, :cookie, :meta, :page_path, :headers
3
+
4
+ def initialize(args)
5
+ @args = args
6
+ @kas = @args[:kas]
7
+ end
8
+
9
+ def socket_parse(socket)
10
+ req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
11
+ req.parse(socket)
12
+
13
+
14
+ #Set meta.
15
+ @meta = req.meta_vars
16
+
17
+
18
+ #Set page_path.
19
+ page_filepath = meta["PATH_INFO"]
20
+ if page_filepath.length <= 0 or page_filepath == "/" or File.directory?("#{@kas.config[:doc_root]}/#{page_filepath}")
21
+ page_filepath = "#{page_filepath}/#{@kas.config[:default_page]}"
22
+ end
23
+
24
+ @page_path = "#{@kas.config[:doc_root]}/#{page_filepath}"
25
+
26
+
27
+ #Set get and headers.
28
+ @get = Knj::Web.parse_urlquery(@meta["QUERY_STRING"])
29
+ @headers = req.header
30
+
31
+
32
+ #Set post.
33
+ @post = {}
34
+ if meta["REQUEST_METHOD"] == "POST"
35
+ self.convert_webrick_post(@post, req.query)
36
+ end
37
+
38
+
39
+ #Set cookie.
40
+ @cookie = {}
41
+
42
+ req.cookies.each do |cookie_enum|
43
+ @cookie[cookie_enum.name] = CGI.unescape(cookie_enum.value)
44
+ end
45
+
46
+
47
+ req.fixup if req and req.keep_alive?
48
+ end
49
+
50
+ def convert_webrick_post(seton, webrick_post, args = {})
51
+ webrick_post.each do |varname, value|
52
+ Knj::Web.parse_name(seton, varname, value, args)
53
+ end
54
+ end
55
+
56
+ def destroy
57
+ @args.clear if @args
58
+ @args = nil
59
+ @kas = nil
60
+
61
+ @meta.clear if @meta
62
+ @meta = nil
63
+
64
+ @page_path = nil
65
+
66
+ @get.clear if @get
67
+ @get = nil
68
+
69
+ @post.clear if @post
70
+ @post = nil
71
+
72
+ @cookie.clear if @cookie
73
+ @cookie = nil
74
+ end
75
+ end
@@ -0,0 +1,75 @@
1
+ class Knjappserver::Httpsession::Webrick
2
+ attr_reader :get, :post, :cookie, :meta, :page_path, :headers
3
+
4
+ def initialize(args)
5
+ @args = args
6
+ @kas = @args[:kas]
7
+ end
8
+
9
+ def socket_parse(socket)
10
+ req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
11
+ req.parse(socket)
12
+
13
+
14
+ #Set meta.
15
+ @meta = req.meta_vars
16
+
17
+
18
+ #Set page_path.
19
+ page_filepath = meta["PATH_INFO"]
20
+ if page_filepath.length <= 0 or page_filepath == "/" or File.directory?("#{@kas.config[:doc_root]}/#{page_filepath}")
21
+ page_filepath = "#{page_filepath}/#{@kas.config[:default_page]}"
22
+ end
23
+
24
+ @page_path = "#{@kas.config[:doc_root]}/#{page_filepath}"
25
+
26
+
27
+ #Set get and headers.
28
+ @get = Knj::Web.parse_urlquery(@meta["QUERY_STRING"])
29
+ @headers = req.header
30
+
31
+
32
+ #Set post.
33
+ @post = {}
34
+ if meta["REQUEST_METHOD"] == "POST"
35
+ self.convert_webrick_post(@post, req.query)
36
+ end
37
+
38
+
39
+ #Set cookie.
40
+ @cookie = {}
41
+
42
+ req.cookies.each do |cookie_enum|
43
+ @cookie[cookie_enum.name] = CGI.unescape(cookie_enum.value)
44
+ end
45
+
46
+
47
+ req.fixup if req and req.keep_alive?
48
+ end
49
+
50
+ def convert_webrick_post(seton, webrick_post, args = {})
51
+ webrick_post.each do |varname, value|
52
+ Knj::Web.parse_name(seton, varname, value, args)
53
+ end
54
+ end
55
+
56
+ def destroy
57
+ @args.clear if @args
58
+ @args = nil
59
+ @kas = nil
60
+
61
+ @meta.clear if @meta
62
+ @meta = nil
63
+
64
+ @page_path = nil
65
+
66
+ @get.clear if @get
67
+ @get = nil
68
+
69
+ @post.clear if @post
70
+ @post = nil
71
+
72
+ @cookie.clear if @cookie
73
+ @cookie = nil
74
+ end
75
+ end