#!/usr/local/bin/ruby -w IMAGE_NAME = "/home/ambrus/a/flycgi/leer.jpg"; require "socket"; require "timeout"; print "Content-Type: text/html; charset=ISO-8859-2\n\n"; print %{ On-the-fly server experiment

On-the-fly server experiment with ruby

This CGI script experiments with starting a TCP server connection on the fly to serve an image embedded in the page. I do not recommend this technique for production.

So, here's an image: }; lsock = Socket.new(Socket::PF_INET, Socket::SOCK_STREAM, 0); lsock.listen 1; port = Socket.unpack_sockaddr_in(lsock.getsockname)[0]; imgthread = Thread.new { begin asock = (); timeout(60) { asock, * = lsock.accept; }; imgfile = File.open IMAGE_NAME; imgsize = imgfile.stat.size; asock.print "HTTP/1.1 200 Ok\nContent-type: image/jpeg\n" + "Content-length: " + imgsize.to_s + "\n\n"; while b = imgfile.read(16*1024); asock.print b; end; asock.close; rescue Timeout::Error; end; }; server_name = ENV["SERVER_NAME"] || "localhost"; print %{some dynamic image}; print %{

Goodbye for now. }; $>.flush; imgthread.join; __END__