rack-conditional_get 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/rack/conditional_get/version.rb +5 -0
- data/lib/rack/conditional_get.rb +90 -0
- data/lib/rack-conditional_get.rb +3 -0
- data/spec/lib/rack/conditional_get/version_spec.rb +7 -0
- data/spec/lib/rack/conditional_get_spec.rb +36 -0
- data/spec/spec_helper.rb +6 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e43394b566495b4bcd183a9541d63b6f8e0d4520
|
4
|
+
data.tar.gz: 501c66fc4e6180b3ce7b58fc319db07b056e000a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 57ab4e5495636a9f77b16becde50e28d2e159d10a62cf2147b2c182c92ddea08a8199be6a76d793e16997d82404d735c03fad949b60ac9de83f4098ee8e7151f
|
7
|
+
data.tar.gz: 55f972698c82008e91e2efaf0f6fcd1ff220d1c8bb44cfe55107292330c64cc43bcea2e606f516887be2b5a6357265cd15ad88a1329b6fb1b38cb3d9e6db81d9
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module Rack
|
2
|
+
# Middleware that enables conditional GET using If-None-Match and
|
3
|
+
# If-Modified-Since. The application should set either or both of the
|
4
|
+
# Last-Modified or Etag response headers according to RFC 2616. When
|
5
|
+
# either of the conditions is met, the response body is set to be zero
|
6
|
+
# length and the response status is set to 304 Not Modified.
|
7
|
+
#
|
8
|
+
# Applications that defer response body generation until the body's each
|
9
|
+
# message is received will avoid response body generation completely when
|
10
|
+
# a conditional GET matches.
|
11
|
+
#
|
12
|
+
# Adapted from Michael Klishin's Merb implementation:
|
13
|
+
# https://github.com/wycats/merb/blob/master/merb-core/lib/merb-core/rack/middleware/conditional_get.rb
|
14
|
+
module ConditionalGet
|
15
|
+
require_relative "conditional_get/version"
|
16
|
+
VERB_KEY = "REQUEST_METHOD"
|
17
|
+
MODIFIED_SINCE_KEY = "HTTP_IF_MODIFIED_SINCE"
|
18
|
+
NONE_MATCH_KEY = "HTTP_IF_NONE_MATCH"
|
19
|
+
ETAG_KEY = "ETag"
|
20
|
+
LAST_MODIFIED_KEY = "Last-Modified"
|
21
|
+
|
22
|
+
def initialize(stack)
|
23
|
+
@stack = stack
|
24
|
+
end
|
25
|
+
|
26
|
+
def call(env)
|
27
|
+
@env = env
|
28
|
+
@status, @headers, @body = @app.call(environment)
|
29
|
+
if (get? || head?) && (@status == 200 && fresh?)
|
30
|
+
@status = 304
|
31
|
+
headers.delete("Content-Type")
|
32
|
+
headers.delete("Content-Length")
|
33
|
+
body = Rack::BodyProxy.new([]) do
|
34
|
+
@body.close if @body.respond_to?(:close)
|
35
|
+
end
|
36
|
+
[@status, @headers, body]
|
37
|
+
else
|
38
|
+
@app.call(env)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private def fresh?
|
43
|
+
modified_since || etag_matches?
|
44
|
+
end
|
45
|
+
|
46
|
+
private def env
|
47
|
+
@env
|
48
|
+
end
|
49
|
+
|
50
|
+
private def headers
|
51
|
+
@headers
|
52
|
+
end
|
53
|
+
|
54
|
+
private def none_match
|
55
|
+
@env[NONE_MATCH_KEY]
|
56
|
+
end
|
57
|
+
|
58
|
+
private def etag
|
59
|
+
headers[ETAG_KEY]
|
60
|
+
end
|
61
|
+
|
62
|
+
private def etag_matches?
|
63
|
+
etag && none_match && etag == none_match
|
64
|
+
end
|
65
|
+
|
66
|
+
private def modified_since
|
67
|
+
@env[MODIFIED_SINCE_KEY]
|
68
|
+
end
|
69
|
+
|
70
|
+
private def last_modified
|
71
|
+
headers[LAST_MODIFIED_KEY]
|
72
|
+
end
|
73
|
+
|
74
|
+
private def modified_since?
|
75
|
+
last_modified && modified_since && modified_since >= last_modified
|
76
|
+
end
|
77
|
+
|
78
|
+
private def get?
|
79
|
+
verb == "GET"
|
80
|
+
end
|
81
|
+
|
82
|
+
private def get?
|
83
|
+
verb == "HEAD"
|
84
|
+
end
|
85
|
+
|
86
|
+
private def verb
|
87
|
+
env[VERB_KEY]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Rack::ConditionalGet do
|
4
|
+
let(:application) { instance_double("Application") }
|
5
|
+
let(:middleware) { described_class.new(application) }
|
6
|
+
let(:verb) { "GET" }
|
7
|
+
let(:status) { 200 }
|
8
|
+
let(:headers) do
|
9
|
+
{
|
10
|
+
"REQUEST_METHOD" => verb,
|
11
|
+
"Content-Type" => "text/plain",
|
12
|
+
"Content-Length" => "0"
|
13
|
+
}
|
14
|
+
end
|
15
|
+
let(:body) { "" }
|
16
|
+
|
17
|
+
before(:each) do
|
18
|
+
allow(application).to receive(:call).and_return([status, headers, body])
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#call" do
|
22
|
+
context "with If-Modified-Since set" do
|
23
|
+
let(:modified_sense) { Time.now }
|
24
|
+
it "returns 304"
|
25
|
+
it "removes Content-Type"
|
26
|
+
it "removes Content-Length"
|
27
|
+
end
|
28
|
+
|
29
|
+
context "with If-None-Match set" do
|
30
|
+
let(:none_match) { Time.now }
|
31
|
+
it "returns 304"
|
32
|
+
it "removes Content-Type"
|
33
|
+
it "removes Content-Length"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-conditional_get
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kurtis Rainbolt-Greene
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.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: '10.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.9'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.9'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-doc
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.6'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.6'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: codeclimate-test-reporter
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.4'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.4'
|
97
|
+
description: Middleware that catches Last-Modified and Etag requests
|
98
|
+
email:
|
99
|
+
- me@kurtisrainboltgreene.name
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- lib/rack-conditional_get.rb
|
105
|
+
- lib/rack/conditional_get.rb
|
106
|
+
- lib/rack/conditional_get/version.rb
|
107
|
+
- spec/lib/rack/conditional_get/version_spec.rb
|
108
|
+
- spec/lib/rack/conditional_get_spec.rb
|
109
|
+
- spec/spec_helper.rb
|
110
|
+
homepage: http://krainboltgreene.github.io/rack/conditional_get
|
111
|
+
licenses:
|
112
|
+
- MIT
|
113
|
+
metadata: {}
|
114
|
+
post_install_message:
|
115
|
+
rdoc_options: []
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 2.2.2
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: Middleware that catches Last-Modified and Etag requests
|
134
|
+
test_files:
|
135
|
+
- spec/lib/rack/conditional_get/version_spec.rb
|
136
|
+
- spec/lib/rack/conditional_get_spec.rb
|
137
|
+
- spec/spec_helper.rb
|
138
|
+
has_rdoc:
|