apache-vhosts-parser 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,89 @@
1
+ module ApacheVhostsParser
2
+
3
+ class Config
4
+ attr_reader :vhosts
5
+
6
+
7
+ def initialize tag
8
+ @tag = tag
9
+
10
+ @vhosts = @tag[:children].keep_if do |child|
11
+ child[:name] == 'virtualhost'
12
+ end.each do |vhost|
13
+
14
+ vhost[:addresses] = vhost[:arguments].map do |hp|
15
+ host, port = hp.split(':')
16
+ {
17
+ host: host,
18
+ port: port ? port.to_i : nil
19
+ }
20
+ end
21
+
22
+ (vhost[:children] || []).each do |nested|
23
+ if nested[:type] == 'directive'
24
+ case nested[:name]
25
+ when 'servername'
26
+ vhost[:server_name] = nested[:arguments].first
27
+ when 'documentroot'
28
+ vhost[:document_root] = nested[:arguments].first
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ def urlFor dir
36
+ vhosts.each do |h|
37
+ if h[:document_root] == dir
38
+ return h[:server_name]
39
+ end
40
+ end
41
+ return nil
42
+ end
43
+ end
44
+
45
+ def self.parseString str
46
+ vhosts = []
47
+
48
+ tag = {
49
+ children: []
50
+ }
51
+
52
+ str.split(/\n+/).map(&:strip).each do |line|
53
+ if m = line.match(/<\s*(\w+)(.*?)>$/)
54
+ opener, rest = m[1], m[2].to_s.strip.split(/\s+/)
55
+ new_tag = {
56
+ name: opener.downcase,
57
+ type: 'tag',
58
+ original_name: opener,
59
+ arguments: rest,
60
+ parent: tag,
61
+ children: []
62
+ }
63
+ tag[:children] << new_tag
64
+ tag = new_tag
65
+ elsif closer = line[/<\/\s*(\w+)>$/, 1]
66
+ if closer.downcase == tag[:name]
67
+ tag = tag[:parent]
68
+ else
69
+ throw "Mismatched closing tag: #{closer} for #{tag[:original_name]}"
70
+ end
71
+ else
72
+ directive, *arguments = line.split(/\s+/)
73
+ tag[:children] << {
74
+ name: directive.downcase,
75
+ arguments: arguments,
76
+ type: 'directive',
77
+ original_name: directive,
78
+ parent: tag
79
+ }
80
+ end
81
+ end
82
+
83
+ return Config.new(tag)
84
+ end
85
+
86
+ def self.parseDirectory dir='/etc/apache2/sites-enabled/'
87
+ parseString Dir.glob(File.join(dir, '*')).map(&File.method(:read)).join "\n"
88
+ end
89
+ end
@@ -0,0 +1,96 @@
1
+ require 'apache-vhosts-parser'
2
+
3
+ describe 'Parser' do
4
+ it 'should find one virtualhost' do
5
+ str = <<-eos
6
+ <VirtualHost>
7
+ ServerName localhost
8
+ </VirtualHost>
9
+ eos
10
+
11
+ conf = ApacheVhostsParser.parseString(str)
12
+ expect(conf.vhosts.count).to be 1
13
+ end
14
+
15
+ it 'should find one virtualhost with *:80 as addresses' do
16
+ str = <<-eos
17
+ <VirtualHost *:80>
18
+ ServerName localhost
19
+ </VirtualHost>
20
+ eos
21
+
22
+ res = ApacheVhostsParser.parseString(str)
23
+ expect(res.vhosts.count).to be 1
24
+ expect(res.vhosts.first[:addresses]).to eq [{host: '*', port: 80}]
25
+ end
26
+
27
+ it 'should find one virtualhost with *:80 and 127.0.0.1:8080 as addresses' do
28
+ str = <<-eos
29
+ <VirtualHost *:80 127.0.0.1:8080 1.2.3.4>
30
+ ServerName localhost
31
+ </VirtualHost>
32
+ eos
33
+
34
+ res = ApacheVhostsParser.parseString(str)
35
+ expect(res.vhosts.count).to be 1
36
+ expect(res.vhosts.first[:addresses]).to eq [
37
+ {host: '*', port: 80},
38
+ {host: '127.0.0.1', port: 8080},
39
+ {host: '1.2.3.4', port: nil}
40
+ ]
41
+ end
42
+
43
+ it 'should find one virtualhost with servername localhost' do
44
+ str = <<-eos
45
+ <VirtualHost>
46
+ ServerName localhost
47
+ </VirtualHost>
48
+ eos
49
+
50
+ res = ApacheVhostsParser.parseString(str)
51
+ expect(res.vhosts.count).to be 1
52
+ expect(res.vhosts.first[:server_name]).to eq 'localhost'
53
+ end
54
+
55
+ it 'should find 2 virtualhosts' do
56
+ str = <<-eos
57
+ <VirtualHost>
58
+ ServerName localhost
59
+ </VirtualHost>
60
+
61
+ <VirtualHost>
62
+ ServerName example.com
63
+ </VirtualHost>
64
+ eos
65
+
66
+ res = ApacheVhostsParser.parseString(str)
67
+ expect(res.vhosts.count).to be 2
68
+ expect(res.vhosts.first[:server_name]).to eq 'localhost'
69
+ expect(res.vhosts.last[:server_name]).to eq 'example.com'
70
+ end
71
+
72
+ it 'should find one virtualhost with document root /var/www/example.com' do
73
+ str = <<-eos
74
+ <VirtualHost>
75
+ DocumentRoot /var/www/example.com
76
+ ServerName localhost
77
+ </VirtualHost>
78
+ eos
79
+
80
+ res = ApacheVhostsParser.parseString(str)
81
+ expect(res.vhosts.count).to be 1
82
+ expect(res.vhosts.first[:document_root]).to eq '/var/www/example.com'
83
+ end
84
+
85
+ it 'should find the URL for /var/www/example.com to be example.com' do
86
+ str = <<-eos
87
+ <VirtualHost>
88
+ DocumentRoot /var/www/example.com
89
+ ServerName example.com
90
+ </VirtualHost>
91
+ eos
92
+
93
+ res = ApacheVhostsParser.parseString(str)
94
+ expect(res.urlFor '/var/www/example.com').to eq 'example.com'
95
+ end
96
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apache-vhosts-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - François-Marie de Jouvencel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-05-19 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Parses apache2 vhosts. Have a look at the spec files to see how the API
15
+ works!
16
+ email: fm.de.jouvencel@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/apache-vhosts-parser.rb
22
+ - spec/apache_vhosts_parser_spec.rb
23
+ homepage: https://github.com/djfm/apache-vhosts-parser
24
+ licenses:
25
+ - OSL
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.23
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Sometimes you need some info from an Apache vhosts file or directory. This
48
+ gem helps.
49
+ test_files: []