Skip to content

Commit

Permalink
Properly terminate connection after a 304 (cached) response.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wil Bierbaum & Brian Takita authored and Wil Bierbaum & Brian Takita committed Aug 29, 2008
1 parent 2fe7ce4 commit 19fffd7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
2 changes: 1 addition & 1 deletion vendor/js-test-core/.piston.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
format: 1
handler:
commit: 15f328216186294f2a3b4494449b92d1ac787fbd
commit: b161d7fcd06a93938c748b66ad524f3a29c60b3c
branch: HEAD
lock: false
repository_url: git://github.com/pivotal/js-test-core.git
Expand Down
44 changes: 22 additions & 22 deletions vendor/js-test-core/lib/js_test_core/resources/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,42 @@ class File < ThinRest::Resource
'.jpg' => 'image/jpeg',
'.jpeg' => 'image/jpeg',
'.gif' => 'image/gif',
}
}

property :absolute_path, :relative_path

def get
extension = ::File.extname(absolute_path)
content_type = MIME_TYPES[extension] || 'text/html'
if !rack_request.env["HTTP_IF_MODIFIED_SINCE"].to_s.empty? && Time.parse(rack_request.env["HTTP_IF_MODIFIED_SINCE"]) >= ::File.mtime(absolute_path)
connection.send_head(
304,
'Content-Type' => content_type,
'Last-Modified' => ::File.mtime(absolute_path).rfc822,
'Content-Length' => 0
)
else
connection.send_head(
200,
'Content-Type' => content_type,
'Last-Modified' => ::File.mtime(absolute_path).rfc822,
'Content-Length' => ::File.size(absolute_path)
)
connection.terminate_after_sending do

connection.terminate_after_sending do
if !rack_request.env["HTTP_IF_MODIFIED_SINCE"].to_s.empty? && Time.parse(rack_request.env["HTTP_IF_MODIFIED_SINCE"]) >= ::File.mtime(absolute_path)
connection.send_head(
304,
'Content-Type' => content_type,
'Last-Modified' => ::File.mtime(absolute_path).rfc822,
'Content-Length' => 0
)
else
connection.send_head(
200,
'Content-Type' => content_type,
'Last-Modified' => ::File.mtime(absolute_path).rfc822,
'Content-Length' => ::File.size(absolute_path)
)
::File.open(absolute_path) do |file|
while !file.eof?
connection.send_data(file.read(1024))
end
end
end
end
end

def ==(other)
return false unless other.class == self.class
absolute_path == other.absolute_path && relative_path == other.relative_path
def ==(other)
return false unless other.class == self.class
absolute_path == other.absolute_path && relative_path == other.relative_path
end
end
end
end
end
end
16 changes: 13 additions & 3 deletions vendor/js-test-core/spec/unit/js_test_core/resources/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,23 @@ module Resources
it "returns a 304 response with Content-Length: 0 and Last-Modified: File.mtime" do
path = "#{public_path}/stylesheets/example.css"
mock(connection).send_head(304, 'Content-Type' => "text/css", 'Content-Length' => 0, 'Last-Modified' => ::File.mtime(path).rfc822)

connection.receive_data("GET /stylesheets/example.css HTTP/1.1\r\nHost: _\r\nIf-Modified-Since: #{::File.mtime(path).rfc822}\r\n\r\n")

path_2 = "#{public_path}/javascripts/foo.js"
mock(connection).send_head(304, 'Content-Type' => "text/javascript", 'Content-Length' => 0, 'Last-Modified' => ::File.mtime(path_2).rfc822)
connection.receive_data("GET /javascripts/foo.js HTTP/1.1\r\nHost: _\r\nIf-Modified-Since: #{::File.mtime(path_2).rfc822}\r\n\r\n")
end
end

context "when If-Modified-Since header is > the File's mtime" do
it "returns a 304 response with Content-Length: 0 and Last-Modified: File.mtime" do
path = "#{public_path}/stylesheets/example.css"
mock(connection).send_head(304, 'Content-Type' => "text/css", 'Content-Length' => 0, 'Last-Modified' => ::File.mtime(path).rfc822)

connection.receive_data("GET /stylesheets/example.css HTTP/1.1\r\nHost: _\r\nIf-Modified-Since: #{(::File.mtime(path) + 10).rfc822}\r\n\r\n")

path_2 = "#{public_path}/javascripts/foo.js"
mock(connection).send_head(304, 'Content-Type' => "text/javascript", 'Content-Length' => 0, 'Last-Modified' => ::File.mtime(path_2).rfc822)
connection.receive_data("GET /javascripts/foo.js HTTP/1.1\r\nHost: _\r\nIf-Modified-Since: #{(::File.mtime(path_2) + 10).rfc822}\r\n\r\n")
end
end

Expand All @@ -33,8 +39,12 @@ module Resources
path = "#{public_path}/stylesheets/example.css"
mock(connection).send_head(200, 'Content-Type' => "text/css", 'Content-Length' => ::File.size(path), 'Last-Modified' => ::File.mtime(path).rfc822)
mock(connection).send_data(::File.read(path))

connection.receive_data("GET /stylesheets/example.css HTTP/1.1\r\nHost: _\r\nIf-Modified-Since: #{(::File.mtime(path) - 10).rfc822}\r\n\r\n")

path_2 = "#{public_path}/javascripts/foo.js"
mock(connection).send_head(200, 'Content-Type' => "text/javascript", 'Content-Length' => ::File.size(path_2), 'Last-Modified' => ::File.mtime(path_2).rfc822)
mock(connection).send_data(::File.read(path_2))
connection.receive_data("GET /javascripts/foo.js HTTP/1.1\r\nHost: _\r\nIf-Modified-Since: #{(::File.mtime(path_2) - 10).rfc822}\r\n\r\n")
end
end

Expand Down

0 comments on commit 19fffd7

Please sign in to comment.