Arc Forumnew | comments | leaders | submitlogin
9 points by brett 6173 days ago | link | parent

you don't really need callcc as much as just storing proc closures

here's a ruby version a bit closer to the original. there's a bunch of support and then process roughly corresponds to said above

  #!/usr/bin/env ruby
  require 'rubygems'
  require 'mongrel'

  class FooHandler < Mongrel::HttpHandler
  
    def initialize
      @fnids = {}
      @c = 0
    end
  
    def new_fnid(proc)
      @c += 1
      @fnids[@c] = proc
      @c
    end
  
    def query_params(request)
      request.class.query_parse(request.params['QUERY_STRING'])
    end
  
    def pr(response, html)    
      response.start do |head,out|
        head["Content-Type"] = "text/html"
        out << html
      end
    end
  
    def w_link(response, link_text, &block)
      c = new_fnid(block)
      pr(response, "<a href='?fnid=#{c}'>#{link_text}</a>")
    end
  
    def aform(response, form_html, &block)
      c = new_fnid(block)
      pr(response, "<form><input type='hidden' name='fnid' value='#{c}'>#{form_html}</form>")
    end
  
    def process(request, response)
      if (fnid = query_params(request)['fnid'])
        @fnids[fnid.to_i].call(request, response)
      else
        aform(response, "<input name='foo'><input type='submit'>") do |req1, resp1|
          w_link(resp1, "click here") do |req2, resp2|
            pr(resp2, "you said: " + query_params(req1)['foo'])
          end
        end
      end
    end
  end

  Mongrel::Configurator.new :port => 8080 do
    listener {uri "/", :handler => FooHandler.new}
    trap("INT") {stop}
    run
  end.join


3 points by s3graham 6173 days ago | link

Neat, thanks. (Not quite "right" since I can change &foo=myinput on page 2, but I'm guessing that could easily be fixed with an extra closure somewhere).

-----