debug-sxml.scm 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. ;;; Commentary:
  2. ;;; A simple debugging server that responds to all requests with a
  3. ;;; table containing the headers given in the request.
  4. ;;;
  5. ;;; As a novelty, this server uses a little micro-framework to build up
  6. ;;; the response as SXML. Instead of a string, the `respond' helper
  7. ;;; returns a procedure for the body, which allows the `(web server)'
  8. ;;; machinery to collect the output as a bytevector in the desired
  9. ;;; encoding, instead of building an intermediate output string.
  10. ;;;
  11. ;;; In the future this will also allow for chunked transfer-encoding,
  12. ;;; for HTTP/1.1 clients.
  13. ;;; Code:
  14. (use-modules (web server)
  15. (web request)
  16. (web response)
  17. (sxml simple))
  18. (define html5-doctype "<!DOCTYPE html>\n")
  19. (define default-title "Hello hello!")
  20. (define* (templatize #:key (title "No title") (body '((p "No body"))))
  21. `(html (head (title ,title))
  22. (body ,@body)))
  23. (define* (respond #:optional body #:key
  24. (status 200)
  25. (title default-title)
  26. (doctype html5-doctype)
  27. (content-type-params '((charset . "utf-8")))
  28. (content-type 'text/html)
  29. (extra-headers '())
  30. (sxml (and body (templatize #:title title #:body body))))
  31. (values (build-response
  32. #:code status
  33. #:headers `((content-type . (,content-type ,@content-type-params))
  34. ,@extra-headers))
  35. (lambda (port)
  36. (if sxml
  37. (begin
  38. (if doctype (display doctype port))
  39. (sxml->xml sxml port))))))
  40. (define (debug-page request body)
  41. (respond `((h1 "hello world!")
  42. (table
  43. (tr (th "header") (th "value"))
  44. ,@(map (lambda (pair)
  45. `(tr (td (tt ,(with-output-to-string
  46. (lambda () (display (car pair))))))
  47. (td (tt ,(with-output-to-string
  48. (lambda ()
  49. (write (cdr pair))))))))
  50. (request-headers request))))))
  51. (run-server debug-page)