Skip to content

Instantly share code, notes, and snippets.

@fxn
Created April 16, 2011 14:18
Show Gist options
  • Save fxn/923136 to your computer and use it in GitHub Desktop.
Save fxn/923136 to your computer and use it in GitHub Desktop.
class Img
def each
puts "I WAS REQUESTED"
yield ''
end
end
map '/img' do
run lambda { |env| [200, {'Content-Type' => 'image/png', 'Content-Length' => '0'}, Img.new(env)] }
end
require 'socket'
PREAMBLE = <<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Testing Parallel Asset Fetching</title>
</head>
<body>
<img src="http://localhost:9292/img"/>
HTML
REST = []
1000.times do |n|
REST << "<p>This is paragraph #{n}</p>\n"
end
CONTENT_LENGTH = PREAMBLE.length + REST.inject(0) {|sum, par| sum + par.length}
CRLF = "\015\012"
server = TCPServer.new('localhost', 3000)
loop do
session = server.accept
session.write("HTTP/1.1 200 OK#{CRLF}")
session.write("Server: Assets Fetcher Tester")
session.write("Content-Type: text/html; charset=utf-8#{CRLF}")
session.write("Content-Length: #{CONTENT_LENGTH}#{CRLF}")
session.write(CRLF)
session.write(PREAMBLE)
REST.each do |par|
session.write(par)
sleep 0.01
end
session.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment