#!/usr/bin/env ruby # Copyright (C) 2004 Hiroyuki KUROSAKI # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # require 'socket' class ICalRetriever def initialize(port, filename) @port = port @filename = filename end def start $stderr.puts "Press Control-C to exit." Signal.trap(:INT) do $stderr.puts "Bye." exit 0 end server = TCPServer.new('0.0.0.0', @port) loop do Thread.fork(server.accept) do |soc| begin content_length = 0 request_method = '' while buf = soc.gets if request_method.empty? then if /^(\w+)/ =~ buf then request_method = $1 end end if /^Content-Length: (\d+)/ =~ buf then content_length = $1.to_i end if /^\r?$/ =~ buf then break end $stderr.puts buf end if request_method == 'PUT' && content_length > 0 then $stderr.puts "*** put ***" File.open(@filename, "w") do |f| f.write(soc.read(content_length)) end $stderr.puts "*** done ***" elsif request_method == 'DELETE' then $stderr.puts "*** delete ***\n*** done ***" end soc.write("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n") ensure soc.close unless sock.closed? end end end end end if $0 == __FILE__ then if ARGV.size != 2 then puts "Usage: ruby icalretriever.rb port filename" exit 1 end retriever = ICalRetreiver.new(ARGV[0].to_i, ARGV[1]) retriever.start end