Created
April 1, 2011 21:48
-
-
Save fxn/898931 to your computer and use it in GitHub Desktop.
demonstrates early fetch of an asset in the HEAD, and the 1K threshold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Html | |
def initialize(env) | |
@server_port = env['SERVER_PORT'] | |
end | |
def first_chunk_prefix | |
<<EOS | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Test 1K Buffer</title> | |
<link href="http://localhost:#{@server_port}/css" media="screen" rel="stylesheet" type="text/css" /> | |
<!-- | |
EOS | |
end | |
def first_chunk_postfix | |
"-->" | |
end | |
def padding | |
1024 - (first_chunk_prefix.length + first_chunk_postfix.length) | |
end | |
def first_chunk | |
first_chunk_prefix + (" " * padding) + first_chunk_postfix | |
end | |
def second_chunk | |
"</head><body><p>Done</p></body></html>" | |
end | |
def each | |
yield first_chunk | |
5.downto(1) {|n| puts n; sleep 1} | |
yield second_chunk | |
end | |
end | |
class Css | |
def each | |
puts "I WAS REQUESTED" | |
yield "body { background-color: black; }" | |
end | |
end | |
map '/' do | |
run lambda { |env| [200, {'Content-Type' => 'text/html'}, Html.new(env)] } | |
end | |
map '/css' do | |
run lambda { |env| [200, {'Content-Type' => 'text/css'}, Css.new] } | |
end | |
map '/favicon.ico' do | |
run lambda { |env| [404, {'Content-Type' => 'image/vnd.microsoft.icon'}, []] } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment