local_pac 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,7 @@
1
1
  module LocalPac
2
2
  class FileServer < Sinatra::Base
3
+ use Rack::Deflater
4
+
3
5
  configure do
4
6
  mime_type :proxy_pac_file, 'application/x-ns-proxy-autoconfig'
5
7
  end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module LocalPac
3
+ class JavaScriptCompressor
4
+ private
5
+
6
+ attr_reader :compressor
7
+
8
+ public
9
+
10
+ def initialize(compressor = Uglifier.new)
11
+ @compressor = compressor
12
+ end
13
+
14
+ def prepare(file)
15
+ file.compressed_content = compressor.compile(file.content)
16
+ end
17
+ end
18
+ end
@@ -13,6 +13,14 @@ module LocalPac
13
13
  nil
14
14
  end
15
15
 
16
+ def compressed_content=(val)
17
+ nil
18
+ end
19
+
20
+ def prepare(val)
21
+ nil
22
+ end
23
+
16
24
  def nil?
17
25
  true
18
26
  end
@@ -2,6 +2,7 @@
2
2
  module LocalPac
3
3
  class PacFile
4
4
  attr_reader :path
5
+ attr_accessor :compressed_content
5
6
 
6
7
  def initialize(path)
7
8
  @path = path
@@ -18,5 +19,9 @@ module LocalPac
18
19
  def nil?
19
20
  false
20
21
  end
22
+
23
+ def prepare(handler)
24
+ handler.prepare(self)
25
+ end
21
26
  end
22
27
  end
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+ module LocalPac
3
+ class PacFileValidator
4
+ def valid?(file)
5
+ PAC.source(file.content)
6
+
7
+ true
8
+ rescue
9
+ false
10
+ end
11
+ end
12
+ end
@@ -3,22 +3,27 @@ module LocalPac
3
3
 
4
4
  private
5
5
 
6
- attr_reader :cache, :null_file, :paths, :creator
6
+ attr_reader :cache, :null_file, :paths, :creator, :content_handler, :validator
7
7
 
8
8
  public
9
9
 
10
10
  def initialize(paths = default_paths, creator = PacFile)
11
- @paths = Array(paths)
12
- @creator = creator
13
- @cache = ActiveSupport::Cache::MemoryStore.new
14
- @null_file = proc { NullPacFile.new }
11
+ @paths = Array(paths)
12
+ @creator = creator
13
+ @cache = ActiveSupport::Cache::MemoryStore.new
14
+ @null_file = proc { NullPacFile.new }
15
+ @content_handler = JavaScriptCompressor.new
16
+ @validator = PacFileValidator.new
15
17
  end
16
18
 
17
19
  def find(name)
18
20
  name = File.basename(name, '.pac').to_sym
19
21
 
20
22
  cache.fetch(name) do
21
- pac_files.find(null_file) { |f| f.name == name }
23
+ file = valid_pac_files.find(null_file) { |f| f.name == name }
24
+ file.prepare(content_handler)
25
+
26
+ file
22
27
  end
23
28
  end
24
29
 
@@ -30,6 +35,15 @@ module LocalPac
30
35
  end
31
36
  end
32
37
 
38
+ def valid_pac_files
39
+ files = []
40
+ pac_files.each do |f|
41
+ files << f if validator.valid?(f)
42
+ end
43
+
44
+ files
45
+ end
46
+
33
47
  def default_paths
34
48
  [
35
49
  File.expand_path(File.join(ENV['HOME'], '.config', 'pacfiles')),
@@ -1,4 +1,4 @@
1
1
  #main LocalPac
2
2
  module LocalPac
3
- VERSION = '0.0.6'
3
+ VERSION = '0.0.7'
4
4
  end
data/lib/local_pac.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'sinatra'
2
2
  require 'fileutils'
3
3
  require 'logger'
4
+ require 'uglifier'
5
+ require 'pac'
4
6
 
5
7
  require 'active_support/cache'
6
8
 
@@ -10,5 +12,7 @@ require 'local_pac/file_server'
10
12
  require 'local_pac/pac_manager'
11
13
  require 'local_pac/pac_file'
12
14
  require 'local_pac/null_pac_file'
15
+ require 'local_pac/java_script_compressor'
16
+ require 'local_pac/pac_file_validator'
13
17
 
14
18
  module LocalPac; end
data/local_pac.gemspec CHANGED
@@ -23,4 +23,7 @@ EOS
23
23
  spec.add_runtime_dependency 'sinatra'
24
24
  spec.add_runtime_dependency 'thor'
25
25
  spec.add_runtime_dependency 'activesupport'
26
+ spec.add_runtime_dependency 'uglifier'
27
+ spec.add_runtime_dependency 'therubyracer'
28
+ spec.add_runtime_dependency 'pac'
26
29
  end
@@ -2,8 +2,15 @@ require 'spec_helper'
2
2
 
3
3
  describe 'Fetch proxy pac' do
4
4
  context '/v1/pac' do
5
+ let(:valid_pac_file) do <<-EOS.strip_heredoc
6
+ function FindProxyForURL(url, host) {
7
+ return "DIRECT"
8
+ }
9
+ EOS
10
+ end
11
+
5
12
  it 'finds an existing proxy pac' do
6
- create_file '.config/pacfiles/file1.pac', 'asdf'
13
+ create_file '.config/pacfiles/file1.pac', valid_pac_file
7
14
 
8
15
  env = {
9
16
  'HOME' => working_directory,
@@ -11,7 +18,7 @@ describe 'Fetch proxy pac' do
11
18
 
12
19
  with_environment env, clear: true do
13
20
  response = get('/v1/pac/file1.pac')
14
- expect(response.body).to eq('asdf')
21
+ expect(response.body).to eq(valid_pac_file)
15
22
  end
16
23
  end
17
24
 
@@ -19,5 +26,19 @@ describe 'Fetch proxy pac' do
19
26
  response = get('/v1/pac/does_not_exist.pac')
20
27
  expect(response.body).to include('does_not_exist.pac')
21
28
  end
29
+
30
+ it 'compresses data for transit if requested' do
31
+ ['deflate','gzip', 'deflate,gzip','gzip,deflate'].each do|compression_method|
32
+ response = get('/v1/pac/does_not_exist.pac', {}, { 'HTTP_ACCEPT_ENCODING' => compression_method })
33
+ expect(response.headers['Content-Encoding']).to be
34
+ end
35
+ end
36
+
37
+ it 'does not compresses data for transit if not requested' do
38
+ ['deflate','gzip', 'deflate,gzip','gzip,deflate'].each do|compression_method|
39
+ response = get('/v1/pac/does_not_exist.pac')
40
+ expect(response.headers['Content-Encoding']).not_to be
41
+ end
42
+ end
22
43
  end
23
44
  end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe JavaScriptCompressor do
5
+ context '#prepare' do
6
+ it 'compresses the javascript content' do
7
+ javascript = <<-EOS.strip_heredoc
8
+ function FindProxyForURL(url, host) {
9
+
10
+ dnsDomainIs(host, 'example.org');
11
+ shExpMatch(url, "*.local")
12
+ isInNet("127.0.0.1", "172.16.0.0", "255.240.0.0")
13
+ myIpAddress()
14
+ dnsResolve(host)
15
+ isPlainHostName(host)
16
+ localHostOrDomainIs(host, "www.google.com")
17
+ isResolvable(host)
18
+ dnsDomainLevels(host)
19
+ weekdayRange("MON", "FRI")
20
+ dateRange("JAN", "MAR")
21
+ timeRange(8, 18)
22
+ alert('asdf')
23
+
24
+ }
25
+ EOS
26
+
27
+ compressed_javascript = <<-EOS.strip_heredoc.chomp
28
+ function FindProxyForURL(e,a){dnsDomainIs(a,"example.org"),shExpMatch(e,"*.local"),isInNet("127.0.0.1","172.16.0.0","255.240.0.0"),myIpAddress(),dnsResolve(a),isPlainHostName(a),localHostOrDomainIs(a,"www.google.com"),isResolvable(a),dnsDomainLevels(a),weekdayRange("MON","FRI"),dateRange("JAN","MAR"),timeRange(8,18),alert("asdf")}
29
+ EOS
30
+
31
+ file = double('file')
32
+ allow(file).to receive(:content).and_return(javascript)
33
+ expect(file).to receive(:compressed_content=).with(compressed_javascript)
34
+
35
+ compressor = JavaScriptCompressor.new
36
+ compressor.prepare(file)
37
+ end
38
+ end
39
+ end
@@ -29,4 +29,14 @@ describe NullPacFile do
29
29
  expect(file.nil?).to be_true
30
30
  end
31
31
  end
32
+
33
+ context '#compressed_content' do
34
+ it 'uses an external handler to compress content' do
35
+ handler = double('handler')
36
+
37
+ file = create_file('file1.pac', 'content')
38
+ file = NullPacFile.new
39
+ file.prepare(handler)
40
+ end
41
+ end
32
42
  end
@@ -30,4 +30,15 @@ describe PacFile do
30
30
  expect(file.nil?).to be_false
31
31
  end
32
32
  end
33
+
34
+ context '#compressed_content' do
35
+ it 'uses an external handler to compress content' do
36
+ handler = double('handler')
37
+ expect(handler).to receive(:prepare)
38
+
39
+ file = create_file('file1.pac', 'content')
40
+ file = PacFile.new(file)
41
+ file.prepare(handler)
42
+ end
43
+ end
33
44
  end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe PacFileValidator do
4
+ context '#valid?' do
5
+ let(:valid_pac_file) do <<-EOS.strip_heredoc
6
+ function FindProxyForURL(url, host) {
7
+ return "DIRECT"
8
+ }
9
+ EOS
10
+ end
11
+
12
+ let(:invalid_pac_file) do <<-EOS.strip_heredoc
13
+ function FindProxyForURL(url, host) {
14
+ EOS
15
+ end
16
+
17
+ it 'returns true if file is valid' do
18
+ file = double('PacFile')
19
+ expect(file).to receive(:content).and_return(valid_pac_file)
20
+ validator = PacFileValidator.new
21
+ expect(validator.valid?(file)).to be_true
22
+ end
23
+
24
+ it 'returns true if file is invalid' do
25
+ file = double('PacFile')
26
+ expect(file).to receive(:content).and_return(invalid_pac_file)
27
+ validator = PacFileValidator.new
28
+ expect(validator.valid?(file)).to be_false
29
+ end
30
+ end
31
+ end
@@ -3,9 +3,16 @@ require 'spec_helper'
3
3
 
4
4
  describe PacManager do
5
5
  context '#find' do
6
+ let(:valid_pac_file) do <<-EOS.strip_heredoc
7
+ function FindProxyForURL(url, host) {
8
+ return "DIRECT"
9
+ }
10
+ EOS
11
+ end
12
+
6
13
  it 'finds the file in given path' do
7
14
  directory = create_directory 'pac_files'
8
- file_path = create_file 'pac_files/file1.pac'
15
+ file_path = create_file 'pac_files/file1.pac', valid_pac_file
9
16
  create_file 'pac_files/file2.pac'
10
17
 
11
18
  manager = PacManager.new(directory)
@@ -19,9 +26,9 @@ describe PacManager do
19
26
  expect(file.nil?).to be_true
20
27
  end
21
28
 
22
- it 'caches files', :focus do
29
+ it 'caches files' do
23
30
  directory = create_directory 'pac_files'
24
- file_path = create_file 'pac_files/file1.pac'
31
+ file_path = create_file 'pac_files/file1.pac', valid_pac_file
25
32
 
26
33
  manager = PacManager.new(directory)
27
34
  file = manager.find('file1')
@@ -32,5 +39,14 @@ describe PacManager do
32
39
  file = manager.find('file1')
33
40
  expect(file.path).to eq(file_path)
34
41
  end
42
+
43
+ it 'ignores invalid pac files' do
44
+ directory = create_directory 'pac_files'
45
+ create_file 'pac_files/file1.pac', 'asdfasdf'
46
+
47
+ manager = PacManager.new(directory)
48
+ file = manager.find('file1.pac')
49
+ expect(file.nil?).to be_true
50
+ end
35
51
  end
36
52
  end
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require 'active_support/core_ext/string/strip'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: local_pac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -59,6 +59,54 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: uglifier
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: therubyracer
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: pac
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
62
110
  description: ! 'This gem helps you to serve proxy.pacs locallly
63
111
 
64
112
  '
@@ -82,9 +130,11 @@ files:
82
130
  - files/proxy.pac
83
131
  - lib/local_pac.rb
84
132
  - lib/local_pac/file_server.rb
133
+ - lib/local_pac/java_script_compressor.rb
85
134
  - lib/local_pac/logger.rb
86
135
  - lib/local_pac/null_pac_file.rb
87
136
  - lib/local_pac/pac_file.rb
137
+ - lib/local_pac/pac_file_validator.rb
88
138
  - lib/local_pac/pac_manager.rb
89
139
  - lib/local_pac/spec_helper.rb
90
140
  - lib/local_pac/version.rb
@@ -95,14 +145,17 @@ files:
95
145
  - share/systemd/local_pac.socket
96
146
  - share/systemd/local_pac@.service
97
147
  - spec/features/fetch_proxy_pac_spec.rb
148
+ - spec/java_script_compressor_spec.rb
98
149
  - spec/null_pac_file_spec.rb
99
150
  - spec/pac_file_spec.rb
151
+ - spec/pac_file_validator_spec.rb
100
152
  - spec/pac_manager_spec.rb
101
153
  - spec/spec_helper.rb
102
154
  - spec/support/environment.rb
103
155
  - spec/support/filesystem.rb
104
156
  - spec/support/rack_test.rb
105
157
  - spec/support/rspec.rb
158
+ - spec/support/string.rb
106
159
  homepage: https://github.com/dg-vrnetze/local_pac
107
160
  licenses:
108
161
  - MIT
@@ -132,12 +185,15 @@ test_files:
132
185
  - features/fetch_proxy_pac.feature
133
186
  - features/support/env.rb
134
187
  - spec/features/fetch_proxy_pac_spec.rb
188
+ - spec/java_script_compressor_spec.rb
135
189
  - spec/null_pac_file_spec.rb
136
190
  - spec/pac_file_spec.rb
191
+ - spec/pac_file_validator_spec.rb
137
192
  - spec/pac_manager_spec.rb
138
193
  - spec/spec_helper.rb
139
194
  - spec/support/environment.rb
140
195
  - spec/support/filesystem.rb
141
196
  - spec/support/rack_test.rb
142
197
  - spec/support/rspec.rb
198
+ - spec/support/string.rb
143
199
  has_rdoc: