rack-varnish-esi 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a00a01acc17dceb937143491e5935ca5f733ce5e
4
+ data.tar.gz: 6fca9d2effea264c73af087b51c6f39432467a17
5
+ SHA512:
6
+ metadata.gz: d3257838b5b959daacb5221475bc2d4097ebf8e1e80837ca2220da007f0c12ffcbb8ebae0bd75e1adb3ab732fcc695ae7b52bcc035424519d97875f73f34951e
7
+ data.tar.gz: 73b93a7e86c546a20e4f7491e02a042ecd66d203472b164ab0ea292effd5e1345dee22c545f0399889d8988c024412bafa6af47635e6efe1d4aeccc911d4de3a
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ *.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-varnish-esi.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Thomas Marek
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # rack-varnish-esi
2
+
3
+ rack-varnish-esi is a Varnish ESI middleware implementation for Rack which is as close as possible to Varnish's own ESI implementation.
4
+
5
+ __Note:__ This gem should only be used in development.
6
+
7
+
8
+ ## Installation
9
+
10
+ ### With Gemfile
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'rack-varnish-esi'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ ### Without Gemfile
23
+
24
+ $ gem install rack-varnish-esi
25
+
26
+
27
+ ## Usage
28
+
29
+ ### rackup
30
+
31
+ ```ruby
32
+ use Rack::VarnishEsi, options
33
+ run Application.new
34
+ ```
35
+
36
+ ### Rails: config/environments/development.rb
37
+
38
+ ```ruby
39
+ config.middleware.insert 0, Rack::VarnishEsi, options
40
+ ```
41
+
42
+ ### Options
43
+
44
+ ```ruby
45
+ options = {
46
+ :locations => {
47
+ /\/esi\/*/ => "http://my-esi-host.com", # Resolves /esi/* to http://my-esi-host.com/esi/*
48
+ "/banners/header.html" => "http://localhost:8080", # Resolves /banners/header.html to http://localhost:8080/banners/header.html
49
+ }
50
+ }
51
+ ```
52
+
53
+
54
+ ## Test environment
55
+
56
+ $ rake test
57
+
58
+ ### Update varnish fixtures
59
+
60
+ Start varnish in one terminal:
61
+
62
+ $ test/fixtures/run_varnish.sh
63
+
64
+ Start static server in another terminal:
65
+
66
+ $ rackup test/static_server/config.ru
67
+
68
+ And then update varnish fixtures:
69
+
70
+ $ wget http://localhost:6081 -O test/fixtures/varnish_output.html
71
+
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it ( https://github.com/tmak/rack-varnish-esi/fork )
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ end
7
+
8
+ task :default => :test
9
+
@@ -0,0 +1 @@
1
+ require 'rack/varnish_esi'
@@ -0,0 +1,23 @@
1
+ require 'rack/varnish_esi_version'
2
+ require 'rack/varnish_esi_processor'
3
+
4
+ module Rack
5
+ class VarnishEsi
6
+ def initialize(app, options={})
7
+ @app = app
8
+ @processor = Rack::VarnishEsiProcessor.new(options[:locations] || {})
9
+ end
10
+
11
+ def call(env)
12
+ status, headers, body = @app.call(env)
13
+
14
+ if headers['Content-Type'] =~ /text\/html/
15
+ body.close if body.respond_to?(:close)
16
+ body = @processor.process(body)
17
+ headers['Content-Length'] = (body.reduce(0) {|sum, part| sum + part.bytesize}).to_s
18
+ end
19
+
20
+ [status, headers, body]
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,52 @@
1
+ require 'net/http'
2
+
3
+ module Rack
4
+ class VarnishEsiProcessor
5
+ def initialize(locations)
6
+ @locations = locations
7
+ end
8
+
9
+ def process(body)
10
+ output = []
11
+
12
+ body.each do |part|
13
+ part = process_remove(part)
14
+ part = process_comment(part)
15
+ output << process_include(part)
16
+ end
17
+
18
+ output
19
+ end
20
+
21
+ protected
22
+
23
+ def process_remove(part)
24
+ part.gsub(/<esi:remove>.*<\/esi:remove>/m, '')
25
+ end
26
+
27
+ def process_comment(part)
28
+ part.gsub(/<!--esi(.*)-->/m, '\1')
29
+ end
30
+
31
+ def process_include(part)
32
+ part.gsub(/<esi:include src="([^"]+)"(?: +alt="([^"]+)")?\/?>/) do
33
+ fetch($1)
34
+ end
35
+ end
36
+
37
+ def fetch(src)
38
+ location = src.gsub(/https?:\/\/[^\/]+/, '')
39
+ location = "/#{location}" unless location.start_with?('/')
40
+
41
+ @locations.each do |pattern, host|
42
+ if (pattern.is_a?(String) && location == pattern) || (pattern.is_a?(Regexp) && location =~ pattern)
43
+ return get("#{host}#{location}")
44
+ end
45
+ end
46
+ end
47
+
48
+ def get(uri)
49
+ Net::HTTP.get(URI(uri))
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class VarnishEsi
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/varnish_esi_version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-varnish-esi"
8
+ spec.version = Rack::VarnishEsi::VERSION
9
+ spec.authors = ["Thomas Marek"]
10
+ spec.email = ["thomas_marek@gmx.net"]
11
+ spec.summary = %q{Varnish ESI middleware implementation for Rack.}
12
+ spec.description = %q{<<-EOF
13
+ rack-varnish-esi is a Varnish ESI middleware implementation for Rack which is as close as possible to Varnish's own ESI implementation.
14
+ Note: This gem should only be used in development.
15
+ EOF}
16
+ spec.homepage = "https://github.com/tmak/rack-varnish-esi"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0")
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "rack"
25
+
26
+ spec.add_development_dependency "bundler"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "minitest"
29
+ end
@@ -0,0 +1,10 @@
1
+ #!/bin/bash
2
+
3
+ CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
4
+
5
+ sudo varnishd \
6
+ -F \
7
+ -a :6081 \
8
+ -T localhost:6082 \
9
+ -f ${CURRENT_DIR}/varnish.vcl \
10
+ -s malloc,32m
@@ -0,0 +1,15 @@
1
+ vcl 4.0;
2
+
3
+ backend default {
4
+ .host = "127.0.0.1";
5
+ .port = "9292";
6
+ }
7
+
8
+
9
+ sub vcl_recv {
10
+ return (pass);
11
+ }
12
+
13
+ sub vcl_backend_response {
14
+ set beresp.do_esi = true;
15
+ }
@@ -0,0 +1,20 @@
1
+ <html>
2
+ <head>
3
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8">
4
+ </head>
5
+ <body>
6
+ XML notation: Marek<br>
7
+ HTML notation: Thomas<br>
8
+ Recursive: <span>Thomas Marek</span><br>
9
+ Unicode: €10<br>
10
+ Alternative option: File not found: /unknown
11
+ <br>
12
+ Different host: Thomas<br>
13
+ No leading slash: Thomas<br>
14
+
15
+
16
+ Commented out
17
+ Thomas
18
+
19
+ </body>
20
+ </html>
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'rack/varnish_esi'
3
+
4
+ require 'minitest/autorun'
@@ -0,0 +1,19 @@
1
+ require 'rack'
2
+
3
+ module StaticServer
4
+ class App
5
+ def initialize(extra_middleware=nil, extra_middleware_options={})
6
+ @static = Rack::Static.new(self, :urls => [""], :root => File.expand_path('../public', __FILE__), :index => "index.html")
7
+
8
+ if extra_middleware
9
+ @middleware = extra_middleware.new(@static, extra_middleware_options)
10
+ else
11
+ @middleware = @static
12
+ end
13
+ end
14
+
15
+ def call(env)
16
+ @middleware.call(env)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ require File.expand_path('../app', __FILE__)
2
+
3
+ run StaticServer::App.new
@@ -0,0 +1 @@
1
+ Thomas
@@ -0,0 +1,22 @@
1
+ <html>
2
+ <head>
3
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8">
4
+ </head>
5
+ <body>
6
+ XML notation: <esi:include src="/last_name.txt"/><br>
7
+ HTML notation: <esi:include src="/first_name.txt"><br>
8
+ Recursive: <esi:include src="/subdir/full_name.html"/><br>
9
+ Unicode: <esi:include src="/unicode.txt"/><br>
10
+ Alternative option: <esi:include src="/unknown" alt="/first_name.txt"/><br>
11
+ Different host: <esi:include src="http://www.google.com/first_name.txt"/><br>
12
+ No leading slash: <esi:include src="first_name.txt"/><br>
13
+ <esi:remove>
14
+ Removed
15
+ <esi:include src="/first_name.txt"/>
16
+ </esi:remove>
17
+ <!--esi
18
+ Commented out
19
+ <esi:include src="/first_name.txt"/>
20
+ -->
21
+ </body>
22
+ </html>
@@ -0,0 +1 @@
1
+ Marek
@@ -0,0 +1 @@
1
+ <span><esi:include src="/first_name.txt"/> <esi:include src="/last_name.txt"/></span>
@@ -0,0 +1 @@
1
+ €10
@@ -0,0 +1,46 @@
1
+ require 'minitest_helper'
2
+ require 'net/http'
3
+ require 'rack'
4
+ require 'webrick'
5
+ require File.expand_path('../static_server/app', __FILE__)
6
+
7
+ class TestRackVarnishEsi < Minitest::Test
8
+ def test_that_it_has_a_version_number
9
+ refute_nil ::Rack::VarnishEsi::VERSION
10
+ end
11
+
12
+ def test_that_it_has_the_same_behavior_as_varnish
13
+ http_server_thread = start_http_server_with_varnish_esi_middleware(
14
+ :locations => {/.*/ => 'http://localhost:8080'},
15
+ )
16
+
17
+ begin
18
+ assert_equal File.read(File.expand_path('../fixtures/varnish_output.html', __FILE__)), Net::HTTP.get(URI('http://localhost:8080')).force_encoding('UTF-8')
19
+ ensure
20
+ http_server_thread.exit
21
+ end
22
+ end
23
+
24
+ protected
25
+
26
+ def start_http_server_with_varnish_esi_middleware(varnish_esi_options)
27
+ http_server_log_file = File.open(File.expand_path('../http_server.log', __FILE__), 'a')
28
+
29
+ http_server_options = {
30
+ :Port => 8080,
31
+ :Logger => WEBrick::Log.new(http_server_log_file),
32
+ :AccessLog => [
33
+ [ http_server_log_file, WEBrick::AccessLog::COMMON_LOG_FORMAT ],
34
+ [ http_server_log_file, WEBrick::AccessLog::REFERER_LOG_FORMAT ]
35
+ ]
36
+ }
37
+
38
+ http_server_thread = Thread.new do
39
+ Rack::Handler::WEBrick.run(StaticServer::App.new(::Rack::VarnishEsi, varnish_esi_options), http_server_options)
40
+ end
41
+
42
+ sleep(0.5)
43
+
44
+ http_server_thread
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-varnish-esi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Marek
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: |-
70
+ <<-EOF
71
+ rack-varnish-esi is a Varnish ESI middleware implementation for Rack which is as close as possible to Varnish's own ESI implementation.
72
+ Note: This gem should only be used in development.
73
+ EOF
74
+ email:
75
+ - thomas_marek@gmx.net
76
+ executables: []
77
+ extensions: []
78
+ extra_rdoc_files: []
79
+ files:
80
+ - ".gitignore"
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - lib/rack-varnish-esi.rb
86
+ - lib/rack/varnish_esi.rb
87
+ - lib/rack/varnish_esi_processor.rb
88
+ - lib/rack/varnish_esi_version.rb
89
+ - rack-varnish-esi.gemspec
90
+ - test/fixtures/run_varnish.sh
91
+ - test/fixtures/varnish.vcl
92
+ - test/fixtures/varnish_output.html
93
+ - test/minitest_helper.rb
94
+ - test/static_server/app.rb
95
+ - test/static_server/config.ru
96
+ - test/static_server/public/first_name.txt
97
+ - test/static_server/public/index.html
98
+ - test/static_server/public/last_name.txt
99
+ - test/static_server/public/subdir/full_name.html
100
+ - test/static_server/public/unicode.txt
101
+ - test/test_rack_varnish_esi.rb
102
+ homepage: https://github.com/tmak/rack-varnish-esi
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.2.2
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Varnish ESI middleware implementation for Rack.
126
+ test_files:
127
+ - test/fixtures/run_varnish.sh
128
+ - test/fixtures/varnish.vcl
129
+ - test/fixtures/varnish_output.html
130
+ - test/minitest_helper.rb
131
+ - test/static_server/app.rb
132
+ - test/static_server/config.ru
133
+ - test/static_server/public/first_name.txt
134
+ - test/static_server/public/index.html
135
+ - test/static_server/public/last_name.txt
136
+ - test/static_server/public/subdir/full_name.html
137
+ - test/static_server/public/unicode.txt
138
+ - test/test_rack_varnish_esi.rb