apache-config-generator 0.2 → 0.2.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ v0.2.2.1. Fix another stupid bug, yanking old version.
2
+
3
+ v0.2.2. Fix a stupid bug, yanking old version.
4
+
5
+ v0.2.1. Lots of code cleanup and documentation added.
6
+
1
7
  v0.2. Another minor release, lots of improvements since 0.1.
2
8
 
3
9
  v0.1.2. Some more small fixes.
data/Manifest CHANGED
@@ -1,8 +1,11 @@
1
1
  CHANGELOG
2
2
  README.rdoc
3
3
  Rakefile
4
+ apache-config-generator.gemspec
4
5
  bin/apache-configurator
6
+ config/config.reek
5
7
  lib/apache.rb
8
+ lib/apache/apachify.rb
6
9
  lib/apache/config.rb
7
10
  lib/apache/directory.rb
8
11
  lib/apache/logging.rb
@@ -11,7 +14,6 @@ lib/apache/modules.rb
11
14
  lib/apache/mpm_prefork.rb
12
15
  lib/apache/performance.rb
13
16
  lib/apache/permissions.rb
14
- lib/apache/quoteize.rb
15
17
  lib/apache/rake/create.rb
16
18
  lib/apache/rewrites.rb
17
19
  lib/apache/ssl.rb
data/README.rdoc CHANGED
@@ -19,6 +19,8 @@ Configs center around the Apache::Config.build method:
19
19
  server_name 'my-cool-website.cool.wow'
20
20
  document_root '/var/www/my-cool-website'
21
21
 
22
+ server_admin! "john@coswellproductions.com"
23
+
22
24
  directory '/' do
23
25
  options :follow_sym_links, :indexes
24
26
  allow_from_all
@@ -30,5 +32,54 @@ Configs center around the Apache::Config.build method:
30
32
  basic_authentication "My secret", '/etc/apache2/users/global.users', :user => :john
31
33
  satisfy :any
32
34
  end
35
+
36
+ rewrites "My old content" do
37
+ cond "%{HTTP_REFERER}", '!^my-cool-website\.cool\.wow$'
38
+ rule %r{\.(gif|jpg|png|pdf)$}, '/lol-image-stealer.html', :last => true, :redirect => true
39
+
40
+ rewrite_test '/index.html', '/index.html', :http_referer => 'other.site'
41
+ rewrite_test '/index.gif', '/lol-image-stealer.html', :http_referer => 'other.site'
42
+ rewrite_test '/index.gif', '/index.gif', :http_referer => 'my-cool-website.cool.wow'
43
+ end
33
44
  end
34
45
 
46
+ Notes on how the conversion works:
47
+
48
+ * Methods within the build block are translated into NerdCapsed Apache directives.
49
+ * Directives that house children take blocks that contain the child methods.
50
+ * Directives that expect regular expressions take a Regexp object.
51
+ * Passing a String as a parameter, by default, double-quotes it.
52
+ * Passing in a Symbol does not quote the parameter.
53
+ ** Some directives NerdCap Symbols, such as Options
54
+ * Appending an exclamation point to the method turns off quoting.
55
+ * Shortcut methods are defined as modules under the Apache module.
56
+
57
+ There are also sanity checks that occur when configuration is being generated:
58
+
59
+ * Directives that rely on a path will check to see if the path exists.
60
+ * Since you need to use Regexp objects for directives that require a regular expression,
61
+ bad expressions will be flagged by the Ruby interpreter.
62
+ * Rewrite rules can be tested with the rewrite_test method.
63
+
64
+ The above config is transformed into the following:
65
+
66
+ ServerName "my-cool-website.cool.wow"
67
+ DocumentRoot "/var/www/my-cool-website"
68
+ ServerAdmin john@coswellproductions.com
69
+
70
+ <Directory "/">
71
+ Options FollowSymLinks, Indexes
72
+ Allow from all
73
+ </Directory>
74
+
75
+ <LocationMatch "^/secret">
76
+ Deny from all
77
+
78
+ AuthType Basic
79
+ AuthName "My secret"
80
+ AuthUserFile "/etc/apache2/users/global.users"
81
+ Require user john
82
+ </LocationMatch>
83
+
84
+ RewriteCond "%{HTTP_REFERER}" "^!my-cool-website\.cool\.wow"
85
+ RewriteRule "\.(gif|jpg|png|pdf)$" "/lol-image-stealer.html" [L,R]
data/Rakefile CHANGED
@@ -1,5 +1,7 @@
1
1
  $LOAD_PATH << 'lib'
2
2
 
3
+ require 'rubygems'
4
+
3
5
  require 'apache'
4
6
  require 'spec/rake/spectask'
5
7
  require 'sdoc'
@@ -39,3 +41,7 @@ Rake::RDocTask.new do |rdoc|
39
41
  rdoc.main = 'README.rdoc'
40
42
  rdoc.rdoc_dir = 'docs'
41
43
  end
44
+
45
+ task :reek do
46
+ system('reek -c config/config.reek lib/*')
47
+ end
@@ -2,17 +2,17 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{apache-config-generator}
5
- s.version = "0.2"
5
+ s.version = "0.2.2.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["John Bintz"]
9
- s.date = %q{2010-05-12}
9
+ s.date = %q{2010-05-19}
10
10
  s.default_executable = %q{apache-configurator}
11
11
  s.description = %q{A Ruby DSL for programmatically generating Apache configs}
12
12
  s.email = %q{}
13
13
  s.executables = ["apache-configurator"]
14
- s.extra_rdoc_files = ["CHANGELOG", "README.rdoc", "bin/apache-configurator", "lib/apache.rb", "lib/apache/config.rb", "lib/apache/directory.rb", "lib/apache/logging.rb", "lib/apache/master.rb", "lib/apache/modules.rb", "lib/apache/mpm_prefork.rb", "lib/apache/performance.rb", "lib/apache/permissions.rb", "lib/apache/quoteize.rb", "lib/apache/rake/create.rb", "lib/apache/rewrites.rb", "lib/apache/ssl.rb"]
15
- s.files = ["CHANGELOG", "README.rdoc", "Rakefile", "bin/apache-configurator", "lib/apache.rb", "lib/apache/config.rb", "lib/apache/directory.rb", "lib/apache/logging.rb", "lib/apache/master.rb", "lib/apache/modules.rb", "lib/apache/mpm_prefork.rb", "lib/apache/performance.rb", "lib/apache/permissions.rb", "lib/apache/quoteize.rb", "lib/apache/rake/create.rb", "lib/apache/rewrites.rb", "lib/apache/ssl.rb", "skel/Rakefile", "skel/config.yml", "Manifest", "apache-config-generator.gemspec"]
14
+ s.extra_rdoc_files = ["CHANGELOG", "README.rdoc", "bin/apache-configurator", "lib/apache.rb", "lib/apache/apachify.rb", "lib/apache/config.rb", "lib/apache/directory.rb", "lib/apache/logging.rb", "lib/apache/master.rb", "lib/apache/modules.rb", "lib/apache/mpm_prefork.rb", "lib/apache/performance.rb", "lib/apache/permissions.rb", "lib/apache/rake/create.rb", "lib/apache/rewrites.rb", "lib/apache/ssl.rb"]
15
+ s.files = ["CHANGELOG", "README.rdoc", "Rakefile", "apache-config-generator.gemspec", "bin/apache-configurator", "config/config.reek", "lib/apache.rb", "lib/apache/apachify.rb", "lib/apache/config.rb", "lib/apache/directory.rb", "lib/apache/logging.rb", "lib/apache/master.rb", "lib/apache/modules.rb", "lib/apache/mpm_prefork.rb", "lib/apache/performance.rb", "lib/apache/permissions.rb", "lib/apache/rake/create.rb", "lib/apache/rewrites.rb", "lib/apache/ssl.rb", "skel/Rakefile", "skel/config.yml", "Manifest"]
16
16
  s.homepage = %q{}
17
17
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Apache-config-generator", "--main", "README.rdoc"]
18
18
  s.require_paths = ["lib"]
@@ -0,0 +1,4 @@
1
+ ---
2
+ FeatureEnvy:
3
+ exclude:
4
+ - Apache::Logging#handle_log
@@ -0,0 +1,140 @@
1
+ module Apache
2
+ module Apachify
3
+ # Apachify a string
4
+ #
5
+ # Split the provided name on underscores and capitalize the individual parts
6
+ # Certain character strings are capitalized to match Apache directive names:
7
+ # * Cgi => CGI
8
+ # * Ssl => SSL
9
+ # * Ldap => LDAP
10
+ def apachify
11
+ self.to_s.split("_").collect { |part|
12
+ part.capitalize!
13
+
14
+ case part
15
+ when 'Ssl', 'Cgi', 'Ldap', 'Url'; part.upcase
16
+ when 'Etag'; 'ETag'
17
+ else; part
18
+ end
19
+ }.join
20
+ end
21
+ end
22
+ end
23
+
24
+ # Ruby strings
25
+ class String
26
+ include Apache::Apachify
27
+
28
+ alias :optionify :apachify
29
+
30
+ def commentize
31
+ self.split("\n")
32
+ end
33
+
34
+ def quoteize
35
+ %{"#{self}"}
36
+ end
37
+
38
+ alias :blockify :quoteize
39
+
40
+ def headerize
41
+ "#{self.quoteize}"
42
+ end
43
+
44
+ def replace_placeholderize(opts)
45
+ self.gsub(%r{%\{([^\}]+)\}}) do |match|
46
+ key = $1.downcase.to_sym
47
+ opts[key] || ''
48
+ end
49
+ end
50
+ end
51
+
52
+ # Ruby symbols
53
+ class Symbol
54
+ include Apache::Apachify
55
+
56
+ # Turn this into an option for IndexOptions
57
+ def optionify
58
+ output = self.apachify
59
+ output = "-#{output[3..-1]}" if self.to_s[0..3] == 'not_'
60
+ output
61
+ end
62
+
63
+ def quoteize
64
+ self.to_s.gsub('_', ' ')
65
+ end
66
+
67
+ def blockify
68
+ self.to_s
69
+ end
70
+
71
+ def headerize
72
+ "#{self.quoteize}"
73
+ end
74
+ end
75
+
76
+ class Fixnum
77
+ def quoteize; self; end
78
+ end
79
+
80
+ # Ruby arrays
81
+ class Array
82
+ # Apachify all the elements within this array
83
+ def apachify
84
+ self.collect(&:apachify)
85
+ end
86
+
87
+ def quoteize
88
+ self.collect(&:quoteize)
89
+ end
90
+
91
+ def quoteize!
92
+ self.collect!(&:quoteize)
93
+ end
94
+
95
+ def blockify
96
+ self.quoteize * " "
97
+ end
98
+
99
+ alias :commentize :to_a
100
+
101
+ def headerize
102
+ "#{self.first.quoteize} #{self.last}"
103
+ end
104
+
105
+ def rewrite_cond_optionify
106
+ self.collect do |opt|
107
+ {
108
+ :or => 'OR',
109
+ :case_insensitive => 'NC',
110
+ :no_vary => 'NV'
111
+ }[opt]
112
+ end
113
+ end
114
+
115
+ def rewrite_option_listify
116
+ (!self.empty?) ? "[#{self * ','}]" : nil
117
+ end
118
+ end
119
+
120
+ # Ruby hashes
121
+ class Hash
122
+ REWRITE_RULE_CONDITIONS = {
123
+ :last => 'L',
124
+ :forbidden => 'F',
125
+ :no_escape => 'NE',
126
+ :redirect => lambda { |val| val == true ? 'R' : "R=#{val}" },
127
+ :pass_through => 'PT',
128
+ :preserve_query_string => 'QSA',
129
+ :query_string_append => 'QSA',
130
+ :env => lambda { |val| "E=#{val}" }
131
+ }
132
+
133
+ def rewrite_rule_optionify
134
+ self.collect do |key, value|
135
+ what = REWRITE_RULE_CONDITIONS[key]
136
+ what = what.call(value) if what.kind_of? Proc
137
+ what
138
+ end.compact.sort
139
+ end
140
+ end
data/lib/apache/config.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'rubygems'
1
2
  require 'fileutils'
2
3
  require 'rainbow'
3
4
 
@@ -56,7 +57,6 @@ module Apache
56
57
  attr_accessor :line_indent, :rotate_logs_path
57
58
 
58
59
  include Apache::Master
59
- include Apache::Quoteize
60
60
  include Apache::Permissions
61
61
  include Apache::Directories
62
62
  include Apache::Logging
@@ -71,19 +71,27 @@ module Apache
71
71
  end
72
72
 
73
73
  # Build the provided configuration
74
- def build(target = nil, &block)
74
+ def build_and_return(&block)
75
75
  reset!
76
76
 
77
77
  self.instance_eval(&block)
78
78
 
79
- if target
80
- FileUtils.mkdir_p File.split(target).first
81
- File.open(target, 'w') { |f| f.puts [ "# Generated by apache-config-generator #{Time.now.to_s}", @config ].flatten * "\n" }
82
- end
83
-
84
79
  @config
85
80
  end
86
81
 
82
+ def build_and_return_if(*conditions, &block)
83
+ build_and_return(&block) if conditions.include? APACHE_ENV
84
+ end
85
+
86
+ def build(target, &block)
87
+ config = build_and_return(&block)
88
+
89
+ FileUtils.mkdir_p File.split(target).first
90
+ File.open(target, 'w') { |file| file.puts [ "# Generated by apache-config-generator #{Time.now.to_s}", config ].flatten * "\n" }
91
+
92
+ config
93
+ end
94
+
87
95
  # Reset the current settings
88
96
  def reset!
89
97
  @config = []
@@ -94,7 +102,7 @@ module Apache
94
102
  def indent(string_or_array)
95
103
  case string_or_array
96
104
  when Array
97
- string_or_array.collect { |s| indent(s) }
105
+ string_or_array.collect { |line| indent(line) }
98
106
  else
99
107
  " " * (@line_indent * 2) + string_or_array.to_s
100
108
  end
@@ -115,35 +123,18 @@ module Apache
115
123
  @config
116
124
  end
117
125
 
118
- # Apachify a string
119
- #
120
- # Split the provided name on underscores and capitalize the individual parts
121
- # Certain character strings are capitalized to match Apache directive names:
122
- # * Cgi => CGI
123
- # * Ssl => SSL
124
- # * Ldap => LDAP
125
- def apachify(name)
126
- case name
127
- when String, Symbol
128
- name.to_s.split("_").collect(&:capitalize).join.gsub('Ssl', 'SSL').
129
- gsub('Cgi', 'CGI').gsub('Ldap', 'LDAP').gsub('Url', 'URL').
130
- gsub('Etag', 'ETag')
131
- when Array
132
- name.collect { |n| apachify(n) }
133
- end
134
- end
135
-
136
126
  # Handle options that aren't specially handled
137
127
  #
138
128
  # Method names are NerdCapsed and paramters are quoted, unless the method ends with !
139
129
  def method_missing(method, *args)
140
- if method.to_s[-1..-1] == "!"
141
- method = method.to_s[0..-2].to_sym
130
+ method_name = method.to_s
131
+ if method_name[-1..-1] == "!"
132
+ method = method_name[0..-2].to_sym
142
133
  else
143
- args = *quoteize(*args)
134
+ args.quoteize!
144
135
  end
145
136
 
146
- self << [ apachify(method), *args ].compact * ' '
137
+ self << [ method.apachify, *args ].compact * ' '
147
138
  end
148
139
 
149
140
  # Handle creating block methods
@@ -156,7 +147,7 @@ module Apache
156
147
  methods.each do |method|
157
148
  self.class.class_eval <<-EOT
158
149
  def #{method}(*name, &block)
159
- blockify(apachify("#{method}"), name, &block)
150
+ blockify("#{method}".apachify, name, &block)
160
151
  end
161
152
  EOT
162
153
  end
@@ -167,25 +158,25 @@ module Apache
167
158
  # The provided module name is converted into Apache module name format:
168
159
  # if_module(:php5) do #=> <IfModule mod_php5>
169
160
  def if_module(mod, &block)
170
- blockify(apachify('if_module'), "#{mod}_module".to_sym, &block)
161
+ blockify('if_module'.apachify, "#{mod}_module".to_sym, &block)
171
162
  end
172
163
 
173
164
  # Create a directory block, checking to see if the source directory exists.
174
165
  def directory(dir, &block)
175
166
  directory? dir
176
- blockify(apachify('directory'), dir, &block)
167
+ blockify('directory'.apachify, dir, &block)
177
168
  end
178
169
 
179
170
  # Create a LocationMatch block with the provided Regexp:
180
171
  # location_match %r{^/my/location/[a-z0-9]+\.html} do #=> <LocationMatch "^/my/location/[a-z0-9]+\.html">
181
172
  def location_match(regexp, &block)
182
- blockify(apachify('location_match'), regexp.source, &block)
173
+ blockify('location_match'.apachify, regexp.source, &block)
183
174
  end
184
175
 
185
176
  # Create a FilesMatch block with the provied Regexp:
186
177
  # files_match %r{\.html$} do #=> FilesMatch "\.html$">
187
178
  def files_match(regexp, &block)
188
- blockify(apachify('files_match'), regexp.source, &block)
179
+ blockify('files_match'.apachify, regexp.source, &block)
189
180
  end
190
181
 
191
182
  # Only execute the provided block if APACHE_ENV matches one of the provided enviroment symbols:
@@ -194,31 +185,16 @@ module Apache
194
185
  self.instance_eval(&block) if env.include?(APACHE_ENV)
195
186
  end
196
187
 
197
- # Blockify the second parameter of a block
198
- #
199
- # The name is processed differently based on input object type:
200
- # * String - the name is quoteized
201
- # * Array - all of the array members are quoteized
202
- # * Symbol - the name is to_s
203
- def blockify_name(name)
204
- case name
205
- when String
206
- quoteize(name).first
207
- when Array
208
- (quoteize(*name) * " ")
209
- when Symbol
210
- name.to_s
211
- end
212
- end
213
-
214
188
  # Handle the blockification of a provided block
215
189
  def blockify(tag_name, name, &block)
216
- self << ""
217
- self << "<#{[ tag_name, blockify_name(name) ].compact * ' '}>"
190
+ self + [ '', "<#{[ tag_name, name.blockify ].compact * ' '}>" ]
218
191
  @line_indent += 1
219
192
  self.instance_eval(&block)
220
193
  @line_indent -= 1
221
- self << "</#{tag_name}>"
194
+ self + [ "</#{tag_name}>", '' ]
195
+ end
196
+
197
+ def blank_line!
222
198
  self << ""
223
199
  end
224
200
 
@@ -6,7 +6,7 @@ module Apache
6
6
  # The options passed into this method are Apachified:
7
7
  # options :exec_cgi, :follow_sym_links #=> Options ExecCGI FollowSymLinks
8
8
  def options(*opt)
9
- create_options_list('Options', *opt)
9
+ create_options_list('options'.apachify, *opt)
10
10
  end
11
11
 
12
12
  # Create an IndexOptions directive
@@ -14,25 +14,12 @@ module Apache
14
14
  # The options passed into this method are Apachified:
15
15
  # index_options :fancy_indexing, :suppress_description #=> IndexOptions FancyIndexing SuppressDescription
16
16
  def index_options(*opt)
17
- create_options_list('IndexOptions', *opt)
17
+ create_options_list('index_options'.apachify, *opt)
18
18
  end
19
19
 
20
20
  private
21
21
  def create_options_list(tag, *opt)
22
- opt = opt.collect do |o|
23
- case o
24
- when Symbol
25
- if o.to_s[0..3] == 'not_'
26
- "-#{apachify(o.to_s[4..-1])}"
27
- else
28
- apachify(o)
29
- end
30
- else
31
- apachify(o)
32
- end
33
- end
34
-
35
- self << "#{tag} #{opt * " "}"
22
+ self << "#{tag} #{opt.collect(&:optionify) * " "}"
36
23
  end
37
24
  end
38
25
  end
@@ -19,11 +19,16 @@ module Apache
19
19
  [ :custom, :error, :script, :rewrite ].each do |type|
20
20
  class_eval <<-EOT
21
21
  def #{type}_log(*opts)
22
- handle_log '#{type.to_s.capitalize}Log', opts.first, quoteize(opts.first), opts[1..-1]
22
+ handle_log :tag => '#{type.to_s.capitalize}Log',
23
+ :path => opts.first,
24
+ :additional_options => opts[1..-1]
23
25
  end
24
26
 
25
27
  def rotate_#{type}_log(*opts)
26
- handle_log '#{type.to_s.capitalize}Log', opts.first, quoteize(rotatelogs(*opts[0..1])), opts[2..-1]
28
+ handle_log :tag => '#{type.to_s.capitalize}Log',
29
+ :path => opts.first,
30
+ :real_path => rotatelogs(*opts[0..1]),
31
+ :additional_options => opts[2..-1]
27
32
  end
28
33
  EOT
29
34
  end
@@ -37,9 +42,12 @@ module Apache
37
42
  end
38
43
 
39
44
  private
40
- def handle_log(tag, path, real_path, *opts)
41
- writable? path
42
- self << "#{tag} #{[real_path, opts].flatten * " "}"
45
+ def handle_log(info)
46
+ writable? (path = info[:path])
47
+
48
+ real_path = (info[:real_path] || path).quoteize
49
+
50
+ self << "#{info[:tag]} #{[real_path, info[:additional_options]].flatten * " "}"
43
51
  end
44
52
  end
45
53
  end
data/lib/apache/master.rb CHANGED
@@ -8,8 +8,14 @@ module Apache
8
8
  @config += Modules.build(*modules, &block)
9
9
  end
10
10
 
11
+ # Listen on network interfaces and ports
12
+ #
13
+ # Each provided parameter generates a new Listen line:
14
+ # listen "1.2.3.4:80", "2.3.4.5:80" #=>
15
+ # Listen "1.2.3.4:80"
16
+ # Listen "2.3.4.5:80"
11
17
  def listen(*opt)
12
- opt.each { |o| self << "Listen #{quoteize(o)}" }
18
+ opt.each { |adapter| self << "Listen #{adapter.quoteize}" }
13
19
  end
14
20
  alias :listen! :listen
15
21
 
@@ -17,9 +23,9 @@ module Apache
17
23
  # runner('www', 'www-data') #=>
18
24
  # User www
19
25
  # Group www-data
20
- def runner(user, group = nil)
26
+ def runner(user, group)
21
27
  user! user
22
- group! group if group
28
+ group! group
23
29
  end
24
30
 
25
31
  # Enable Passenger on this server
@@ -42,27 +48,19 @@ module Apache
42
48
  end
43
49
 
44
50
  # Set the TCP timeout. Defined here to get around various other timeout methods.
45
- def timeout(t)
46
- self << "Timeout #{t}"
51
+ def timeout(time)
52
+ self << "Timeout #{time}"
47
53
  end
48
54
 
49
55
  # Add a comment to the Apache config. Can pass in either a String or Array of comment lines.
50
- def comment(c)
51
- out = [ '' ]
52
- case c
53
- when String
54
- out += c.split("\n")
55
- when Array
56
- out += c
57
- end
58
- out << ''
59
- self + out.collect { |line| "# #{line.strip}".strip }
56
+ def comment(what)
57
+ self + [ '', what.commentize, '' ].flatten.collect { |line| "# #{line.strip}".strip }
60
58
  end
61
59
 
62
60
  # Create a ScriptAlias, checking to make sure the filesystem path exists.
63
61
  def script_alias(uri, path)
64
62
  directory? path
65
- self << %{ScriptAlias #{quoteize(uri, path) * ' '}}
63
+ self << %{ScriptAlias #{[ uri, path ].quoteize * ' '}}
66
64
  end
67
65
 
68
66
  alias :script_alias! :script_alias
@@ -86,7 +84,7 @@ module Apache
86
84
  # Alias a URL to a directory in the filesystem.
87
85
  # Used to get around reserved Ruby keyword.
88
86
  def apache_alias(*opts)
89
- self << "Alias #{quoteize(*opts) * " "}"
87
+ self << "Alias #{opts.quoteize * " "}"
90
88
  end
91
89
 
92
90
  # Set multiple headers to be delivered for a particular section
@@ -96,27 +94,21 @@ module Apache
96
94
  # Header set "Content-dispoaition" "attachment" env=only-for-downloads
97
95
  def set_header(hash)
98
96
  hash.each do |key, value|
99
- output = "Header set #{quoteize(key)}"
100
- case value
101
- when String, Symbol
102
- output += " #{quoteize(value)}"
103
- when Array
104
- output += " #{quoteize(value.first)} #{value.last}"
105
- end
106
- self << output
97
+ self << "Header set #{key.quoteize} #{value.headerize}"
107
98
  end
108
99
  end
109
100
 
110
101
  def server_name(*opts)
111
102
  if first = opts.shift
112
- self << "ServerName #{quoteize(first)}"
113
- opts.each { |o| server_alias o } if !opts.empty?
103
+ self << "ServerName #{first.quoteize}"
104
+ opts.each { |name| server_alias name } if !opts.empty?
114
105
  end
115
106
  end
116
107
 
117
108
  def document_root(*opts)
118
- directory? opts.first
119
- self << "DocumentRoot #{quoteize(opts.first)}"
109
+ dir = opts.first
110
+ directory? dir
111
+ self << "DocumentRoot #{dir.quoteize}"
120
112
  end
121
113
  alias :document_root! :document_root
122
114
  end
@@ -1,11 +1,7 @@
1
- require 'apache/quoteize'
2
-
3
1
  module Apache
4
2
  # Create lists of modules to load in the Apache 2.2 style (with LoadModule only)
5
3
  class Modules
6
4
  class << self
7
- include Apache::Quoteize
8
-
9
5
  attr_accessor :modules
10
6
 
11
7
  # Reset the list of modules to output
@@ -27,7 +23,7 @@ module Apache
27
23
  def build(*modules, &block)
28
24
  reset!
29
25
 
30
- modules.each { |m| self.send(m) }
26
+ modules.each { |mod| self.send(mod) }
31
27
  self.instance_eval(&block) if block
32
28
 
33
29
  [ '' ] + @modules + [ '' ]
@@ -37,7 +33,7 @@ module Apache
37
33
  def method_missing(method, *args)
38
34
  module_name = "#{method}_module"
39
35
  module_path = args[0] || "modules/mod_#{method}.so"
40
- @modules << [ 'LoadModule', *quoteize(module_name, module_path) ] * " "
36
+ @modules << [ 'LoadModule', *[ module_name, module_path ].quoteize ] * " "
41
37
  end
42
38
  end
43
39
  end
@@ -21,7 +21,7 @@ module Apache
21
21
  #
22
22
  # allow_from '127.0.0.1' #=> Allow from "127.0.0.1"
23
23
  def allow_from(*where)
24
- self << "Allow from #{quoteize(*where) * " "}"
24
+ self << "Allow from #{where.quoteize * " "}"
25
25
  end
26
26
 
27
27
  # Specify default access order
@@ -58,26 +58,18 @@ module Apache
58
58
  # basic_authentication "My other secret", '/my.users', :user => [ :john ]
59
59
  def basic_authentication(zone, users_file, requires = {})
60
60
  exist? users_file
61
- auth_type :basic
62
- auth_name zone
61
+ authentication_basics(zone, requires)
63
62
  auth_user_file users_file
64
- requires.each do |type, values|
65
- apache_require type, *values
66
- end
67
63
  end
68
64
 
69
65
  alias :basic_authentication! :basic_authentication
70
66
 
71
67
  # Set up LDAP authentication
72
68
  def ldap_authentication(zone, url, requires = {})
73
- auth_type :basic
74
- auth_name zone
69
+ authentication_basics(zone, requires)
75
70
  auth_basic_provider :ldap
76
71
  authz_ldap_authoritative :on
77
72
  auth_ldap_url url
78
- requires.each do |type, values|
79
- apache_require type, *values
80
- end
81
73
  end
82
74
 
83
75
  alias :ldap_authentication! :ldap_authentication
@@ -87,5 +79,14 @@ module Apache
87
79
  def apache_require(*opts)
88
80
  self << "Require #{opts.compact * " "}"
89
81
  end
82
+
83
+ private
84
+ def authentication_basics(zone, requires)
85
+ auth_type :basic
86
+ auth_name zone
87
+ requires.each do |type, values|
88
+ apache_require type, *values
89
+ end
90
+ end
90
91
  end
91
92
  end
@@ -9,7 +9,7 @@ module Apache
9
9
  # RewriteEngine on
10
10
  # RewriteLogLevel 1
11
11
  def enable_rewrite_engine(options = {})
12
- self << ''
12
+ blank_line!
13
13
  rewrite_engine! :on
14
14
  options.each do |option, value|
15
15
  case option
@@ -17,7 +17,7 @@ module Apache
17
17
  rewrite_log_level! value
18
18
  end
19
19
  end
20
- self << ''
20
+ blank_line!
21
21
  end
22
22
 
23
23
  # Pass the block to RewriteManager.build
@@ -27,17 +27,19 @@ module Apache
27
27
  end
28
28
 
29
29
  def rewrite(*opt, &block)
30
- raise "You probably want rewrites #{quoteize(*opt) * " "} do" if block
30
+ raise "You probably want rewrites #{opt.quoteize * " "} do" if block
31
31
  end
32
32
 
33
33
  # Create a permanent Redirect
34
34
  #
35
35
  # r301 '/here', '/there' #=> Redirect permanent "/here" "/there"
36
36
  def r301(*opt)
37
- if opt.first && !opt.first.kind_of?(::String)
38
- raise "First parameter should be a String. Did you mean to wrap this in a rewrites block? #{opt.first}"
37
+ if first = opt.first
38
+ if !first.kind_of?(::String)
39
+ raise "First parameter should be a String. Did you mean to wrap this in a rewrites block? #{first}"
40
+ end
39
41
  end
40
- self << "Redirect permanent #{quoteize(*opt) * " "}"
42
+ self << "Redirect permanent #{opt.quoteize * " "}"
41
43
  end
42
44
  end
43
45
 
@@ -49,30 +51,34 @@ module Apache
49
51
  # Reset the current list of rewrites
50
52
  def reset!
51
53
  @rewrites = []
54
+ @any_tests = false
55
+ @needs_tests = false
52
56
  end
53
57
 
54
58
  # Build rewritable things from the provided block
55
59
  def build(*opt, &block)
56
60
  reset!
57
61
 
58
- @any_tests = false
59
- @needs_tests = false
60
62
  self.instance_eval(&block)
61
63
 
62
- name = opt.first || (@rewrites.empty? ? 'unnamed block' : "#{@rewrites.first.from} => #{@rewrites.first.to}")
64
+ name = block_name(opt.first)
63
65
 
64
- if !@any_tests && !@rewrites.empty?
65
- puts " [#{"rewrite".foreground(:blue)}] no tests found for #{name}"
66
- end
66
+ show_messages! name
67
67
 
68
- if @needs_tests
69
- puts " [#{"rewrite".foreground(:blue)}] #{name} needs more tests"
70
- end
68
+ [ "# #{name}", @rewrites.collect(&:to_a) ].flatten
69
+ end
71
70
 
72
- output = @rewrites.collect(&:to_a).flatten
73
- output.unshift("# #{name}") if opt.first
71
+ def block_name(first)
72
+ first_rewrite = @rewrites.first
74
73
 
75
- output
74
+ first || (@rewrites.empty? ? 'unnamed block' : "#{first_rewrite.from} => #{first_rewrite.to}")
75
+ end
76
+
77
+ def show_messages!(name)
78
+ blue = "rewrite".foreground(:blue)
79
+
80
+ puts " [#{blue}] no tests found for #{name}" if !@any_tests && !@rewrites.empty?
81
+ puts " [#{blue}] #{name} needs more tests" if @needs_tests
76
82
  end
77
83
 
78
84
  # Commit the latest rewritable thing to the list of rewrites
@@ -122,19 +128,18 @@ module Apache
122
128
  def rewrite_test(from, to, opts = {})
123
129
  @any_tests = true
124
130
  orig_from = from.dup
125
- @rewrites.each do |r|
126
- pre_from = from.dup
127
- if r.match?(from, opts)
128
- from = r.test(from, opts)
129
- from = pre_from if (r.to == '-')
130
- from = :http_forbidden if (r.forbidden?)
131
- break if r.stop_if_match?
131
+ @rewrites.each do |rewrite|
132
+ if rewrite.match?(from, opts)
133
+ from = rewrite.from_tester(from, opts)
134
+ break if rewrite.stop_if_match?
132
135
  end
133
136
  end
134
137
 
135
138
  if from != to
136
- puts " [#{"rewrite".foreground(:blue)}] #{orig_from} >> #{to} failed!"
137
- puts " [#{"rewrite".foreground(:blue)}] Result: #{from}"
139
+ [ "#{orig_from} >> #{to} failed!", "Result: #{from}"
140
+ ].each do |line|
141
+ puts " [#{"rewrite".foreground(:blue)}] #{line}"
142
+ end
138
143
  end
139
144
  end
140
145
 
@@ -170,30 +175,16 @@ module Apache
170
175
  module RegularExpressionMatcher
171
176
  # Test this rewritable thing
172
177
  def test(from, opts = {})
173
- from = from.gsub(@from, @to.gsub(/\$([0-9])/) { |match| '\\' + $1 })
174
- replace_placeholders(from, opts)
178
+ from.gsub(@from, @to.gsub(/\$([0-9])/) { |match| '\\' + $1 }).replace_placeholderize(opts)
175
179
  end
176
180
 
177
181
  def match?(from, opts = {})
178
- replace_placeholders(from, opts)[@from]
179
- end
180
-
181
- # Replace the placeholders in this rewritable thing
182
- def replace_placeholders(string, opts)
183
- opts.each do |opt, value|
184
- case value
185
- when String
186
- string = string.gsub('%{' + opt.to_s.upcase + '}', value)
187
- end
188
- end
189
- string.gsub(%r{%\{[^\}]+\}}, '')
182
+ from.replace_placeholderize(opts)[@from]
190
183
  end
191
184
  end
192
185
 
193
186
  # A matchable thing to be extended
194
187
  class MatchableThing
195
- include Apache::Quoteize
196
-
197
188
  attr_reader :from, :to
198
189
 
199
190
  # The Apache directive tag for this thing
@@ -205,12 +196,14 @@ module Apache
205
196
  end
206
197
 
207
198
  def rule(from, to)
199
+ raise "from must be a Regexp" if (!from.kind_of?(Regexp) && require_regexp?)
200
+
208
201
  @from = from
209
202
  @to = to
210
203
  end
211
204
 
212
205
  def to_s
213
- "#{tag} #{[quoteize(@from), quoteize(@to)].compact.flatten * " "}"
206
+ "#{tag} #{[@from, @to].quoteize.compact.flatten * " "}"
214
207
  end
215
208
 
216
209
  def to_a
@@ -219,6 +212,14 @@ module Apache
219
212
 
220
213
  def stop_if_match?; false; end
221
214
  def forbidden?; false; end
215
+ def require_regexp?; false; end
216
+
217
+ def from_tester(from, opts)
218
+ from = test(from, opts)
219
+ from = @from if (@to == '-')
220
+ from = :http_forbidden if (forbidden?)
221
+ from
222
+ end
222
223
  end
223
224
 
224
225
  # A RewriteRule definition
@@ -237,33 +238,23 @@ module Apache
237
238
  # Define the rule, passing in additional options
238
239
  #
239
240
  # rule %r{^/here}, '/there', { :last => true, :preserve_query_string => true }
241
+ #
242
+ # Options for the options hash are:
243
+ # * :last => true #=> [L]
244
+ # * :forbidden => true #=> [F]
245
+ # * :no_escape => true #=> [NE]
246
+ # * :redirect => true #=> [R]
247
+ # * :redirect => 302 #=> [R=302]
248
+ # * :pass_through => true #=> [PT]
249
+ # * :preserve_query_string => true #=> [QSA]
250
+ # * :query_string_append => true #=> [QSA]
251
+ # * :env => 'what' #=> [E=what]
240
252
  def rule(from, to, options = {})
241
253
  super(from, to)
242
254
 
243
- raise "from must be a Regexp" if !from.kind_of?(Regexp)
244
-
245
255
  @input_options = options
246
256
 
247
- options = options.collect do |key, value|
248
- case key
249
- when :last
250
- 'L'
251
- when :forbidden
252
- 'F'
253
- when :no_escape
254
- 'NE'
255
- when :redirect
256
- (value == true) ? 'R' : "R=#{value}"
257
- when :pass_through
258
- 'PT'
259
- when :preserve_query_string, :query_string_append
260
- 'QSA'
261
- when :env
262
- "E=#{value}"
263
- end
264
- end.compact.sort
265
-
266
- @options = !options.empty? ? "[#{options * ','}]" : nil
257
+ @options = options.rewrite_rule_optionify.rewrite_option_listify
267
258
  end
268
259
 
269
260
  # Add a RewriteCondition to this RewriteRule
@@ -274,37 +265,43 @@ module Apache
274
265
  @conditions << rewrite_cond
275
266
  end
276
267
 
268
+ def initial_blank!
269
+ @conditions.empty? ? nil : ''
270
+ end
271
+
277
272
  def to_s
278
- "#{tag} #{[quoteize(@from.source), quoteize(@to), @options].compact.flatten * " "}"
273
+ "#{tag} #{[@from.source.quoteize, @to.quoteize, @options].compact.flatten * " "}"
279
274
  end
280
275
 
281
276
  def to_a
282
- [ ('' if !@conditions.empty?), @conditions.collect(&:to_s), super ].flatten
277
+ [ initial_blank!, @conditions.collect(&:to_s), super ].flatten
283
278
  end
284
279
 
285
280
  # Test this RewriteRule, ensuring the RewriteConds also match
286
281
  def test(from, opts = {})
282
+ ensure_opts!(opts)
287
283
  opts[:request_uri] = from
288
284
  result = from
289
285
 
290
286
  result = super(from, opts) if match?(from, opts)
291
287
 
292
- replace_placeholders(result, opts)
288
+ result.replace_placeholderize(opts)
289
+ end
290
+
291
+ def ensure_opts!(opts)
292
+ raise "Options must be a hash" if !opts
293
+ raise "Options must be a hash" if !opts.kind_of? ::Hash
293
294
  end
294
295
 
295
296
  def match?(from, opts = {})
297
+ ensure_opts!(opts)
296
298
  opts[:request_uri] = from
297
- ok = true
298
299
 
299
- @conditions.each do |c|
300
- ok = false if !c.test(from, opts)
300
+ @conditions.each do |cond|
301
+ return false if !cond.test(from, opts)
301
302
  end
302
303
 
303
- if ok
304
- super(from, opts)
305
- else
306
- false
307
- end
304
+ super(from, opts)
308
305
  end
309
306
 
310
307
  def stop_if_match?
@@ -314,25 +311,32 @@ module Apache
314
311
  def forbidden?
315
312
  @input_options[:forbidden]
316
313
  end
314
+
315
+ def require_regexp?; true; end
317
316
  end
318
317
 
319
318
  # A permanent RedirectMatch
320
319
  class RedirectMatchPermanent < MatchableThing
321
320
  include RegularExpressionMatcher
322
321
 
322
+ # The Apache directive for this object.
323
323
  def tag; 'RedirectMatch permanent'; end
324
324
 
325
+ # Define a RedirectMatch rule.
325
326
  def rule(from, to)
326
327
  super(from, to)
327
328
 
328
329
  raise "from must be a Regexp" if !from.kind_of?(Regexp)
329
330
  end
330
331
 
332
+ # Convert this tag to a String.
331
333
  def to_s
332
- "#{tag} #{[quoteize(@from.source), quoteize(@to)].compact.flatten * " "}"
334
+ "#{tag} #{[@from.source, @to].quoteize.compact.flatten * " "}"
333
335
  end
334
336
 
337
+ # Stop rewrite testing if this object matches.
335
338
  def stop_if_match; true; end
339
+ def require_regexp?; true; end
336
340
  end
337
341
 
338
342
  # A RewriteCond
@@ -345,48 +349,47 @@ module Apache
345
349
  #
346
350
  # rule "%{REQUEST_FILENAME}", "^/here", :case_insensitive #=>
347
351
  # RewriteCond "%{REQUEST_FILENAME}" "^/here" [NC]
352
+ #
353
+ # Additional parameters can include the following:
354
+ # * :or #=> [OR]
355
+ # * :case_insensitive #=> [NC]
356
+ # * :no_vary #=> [NV]
348
357
  def rule(from, to, *opts)
349
358
  super(from, to)
350
359
 
351
- options = opts.collect do |opt|
352
- case opt
353
- when :or
354
- 'OR'
355
- when :case_insensitive
356
- 'NC'
357
- when :no_vary
358
- 'NV'
359
- end
360
- end
361
-
362
- @options = (!options.empty?) ? "[#{options * ','}]" : nil
360
+ @options = opts.rewrite_cond_optionify.rewrite_option_listify
363
361
  end
364
362
 
365
363
  alias :cond :rule
366
364
 
365
+ # Create a new RewriteCond
367
366
  def initialize
368
367
  super
369
368
  @options = nil
370
369
  end
371
370
 
371
+ # Convert this tag to a String.
372
372
  def to_s
373
- "#{tag} #{[quoteize(@from), quoteize(@to), @options].compact.flatten * " "}"
373
+ "#{tag} #{[@from.quoteize, @to.quoteize, @options].compact.flatten * " "}"
374
+ end
375
+
376
+ def inverse_result?
377
+ @to[0..0] == '!'
378
+ end
379
+
380
+ def actual_to
381
+ to = @to
382
+ to = to[1..-1] if inverse_result?
383
+ to
374
384
  end
375
385
 
376
386
  # Test this RewriteCond
377
387
  def test(from, opts = {})
378
388
  super(from, opts)
379
- source = replace_placeholders(@from, opts)
389
+ source = @from.replace_placeholderize(opts)
380
390
 
381
- to = @to
382
- reverse = false
383
-
384
- if @to[0..0] == '!'
385
- reverse = true
386
- to = @to[1..-1]
387
- end
391
+ to = actual_to
388
392
 
389
- result = false
390
393
  case to
391
394
  when '-f'
392
395
  result = opts[:files].include?(source) if opts[:files]
@@ -394,7 +397,7 @@ module Apache
394
397
  result = source[Regexp.new(to)]
395
398
  end
396
399
 
397
- reverse ? !result : result
400
+ inverse_result? ? !result : result
398
401
  end
399
402
  end
400
403
  end
data/lib/apache/ssl.rb CHANGED
@@ -1,18 +1,17 @@
1
1
  module Apache
2
2
  module SSL
3
3
  def enable_ssl_engine(options = {})
4
- self << ""
5
- self << "SSLEngine on"
4
+ self + [ '', "SSLEngine on" ]
6
5
  options.each do |key, value|
7
- value = quoteize(value).first
6
+ value = value.quoteize
8
7
  case key
9
8
  when :certificate_file, :certificate_key_file
10
- self << "SSL#{apachify(key)} #{value}"
9
+ self << "SSL#{key.apachify} #{value}"
11
10
  when :ca_certificate_file
12
11
  self << "SSLCACertificateFile #{value}"
13
12
  end
14
13
  end
15
- self << ""
14
+ blank_line!
16
15
  end
17
16
  end
18
17
  end
metadata CHANGED
@@ -5,7 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- version: "0.2"
8
+ - 2
9
+ - 1
10
+ version: 0.2.2.1
9
11
  platform: ruby
10
12
  authors:
11
13
  - John Bintz
@@ -13,7 +15,7 @@ autorequire:
13
15
  bindir: bin
14
16
  cert_chain: []
15
17
 
16
- date: 2010-05-12 00:00:00 -04:00
18
+ date: 2010-05-19 00:00:00 -04:00
17
19
  default_executable:
18
20
  dependencies:
19
21
  - !ruby/object:Gem::Dependency
@@ -39,6 +41,7 @@ extra_rdoc_files:
39
41
  - README.rdoc
40
42
  - bin/apache-configurator
41
43
  - lib/apache.rb
44
+ - lib/apache/apachify.rb
42
45
  - lib/apache/config.rb
43
46
  - lib/apache/directory.rb
44
47
  - lib/apache/logging.rb
@@ -47,7 +50,6 @@ extra_rdoc_files:
47
50
  - lib/apache/mpm_prefork.rb
48
51
  - lib/apache/performance.rb
49
52
  - lib/apache/permissions.rb
50
- - lib/apache/quoteize.rb
51
53
  - lib/apache/rake/create.rb
52
54
  - lib/apache/rewrites.rb
53
55
  - lib/apache/ssl.rb
@@ -55,8 +57,11 @@ files:
55
57
  - CHANGELOG
56
58
  - README.rdoc
57
59
  - Rakefile
60
+ - apache-config-generator.gemspec
58
61
  - bin/apache-configurator
62
+ - config/config.reek
59
63
  - lib/apache.rb
64
+ - lib/apache/apachify.rb
60
65
  - lib/apache/config.rb
61
66
  - lib/apache/directory.rb
62
67
  - lib/apache/logging.rb
@@ -65,14 +70,12 @@ files:
65
70
  - lib/apache/mpm_prefork.rb
66
71
  - lib/apache/performance.rb
67
72
  - lib/apache/permissions.rb
68
- - lib/apache/quoteize.rb
69
73
  - lib/apache/rake/create.rb
70
74
  - lib/apache/rewrites.rb
71
75
  - lib/apache/ssl.rb
72
76
  - skel/Rakefile
73
77
  - skel/config.yml
74
78
  - Manifest
75
- - apache-config-generator.gemspec
76
79
  has_rdoc: true
77
80
  homepage: ""
78
81
  licenses: []
@@ -1,16 +0,0 @@
1
- module Apache
2
- # Add quotes around parameters as needed
3
- module Quoteize
4
- # Add quotes around most parameters, and don't add quotes around Symbols
5
- def quoteize(*args)
6
- args.collect do |arg|
7
- case arg
8
- when Symbol
9
- arg.to_s.gsub('_', ' ')
10
- else
11
- %{"#{arg}"}
12
- end
13
- end
14
- end
15
- end
16
- end