terça-feira, 23 de fevereiro de 2010

Arc challenge em awful

Há dois anos, Paul Graham propôs o seguinte desafio (arc challenge):

Write a program that causes the url said (e.g. http://localhost:port/said) to produce a page with an input field and a submit button. When the submit button is pressed, that should produce a second page with a single link saying "click here." When that is clicked it should lead to a third page that says "you said: ..." where ... is whatever the user typed in the original input field. The third page must only show what the user actually typed. I.e. the value entered in the input field must not be passed in the url, or it would be possible to change the behavior of the final page by editing the url.


Abaixo está uma implementação para o arc challenge em awful:

(use awful html-utils spiffy-request-vars)

(define-session-page "said"
(lambda ()
(with-request-vars $ (said)
(cond (said
($session-set! 'said said)
(link "said" "click here"))
(($session 'said)
=> (lambda (said)
(++ "You said: " said)))
(else (form (++ (text-input 'said)
(submit-input))
action: "said"
method: 'post))))))


Para testar o programa, basta executar:

$ awful arc-challenge.scm


E acessar http://localhost:8080/said

Nenhum comentário: