others-code.scm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #|
  2. (define-module (web decode))
  3. (use-modules (ice-9 match))
  4. (use-modules (rnrs bytevectors))
  5. (use-modules (srfi srfi-1))
  6. (use-modules (srfi srfi-26))
  7. (use-modules (web uri))
  8. ;;;
  9. ;;; decode
  10. ;;;
  11. (define (acons-list k v alist)
  12. "Add V to K to alist as list"
  13. (let ((value (assoc-ref alist k)))
  14. (if value
  15. (let ((alist (alist-delete k alist)))
  16. (acons k (cons v value) alist))
  17. (acons k (list v) alist))))
  18. (define (list->alist lst)
  19. "Build a alist of list based on a list of key and values.
  20. Multiple values can be associated with the same key"
  21. (let next ((lst lst)
  22. (out '()))
  23. (if (null? lst)
  24. out
  25. (next (cdr lst) (acons-list (caar lst) (cdar lst) out)))))
  26. (define-public (decode bv)
  27. "Convert BV querystring or form data to an alist"
  28. (define string (utf8->string bv))
  29. (define pairs (map (cut string-split <> #\=)
  30. ;; semi-colon and amp can be used as pair separator
  31. (append-map (cut string-split <> #\;)
  32. (string-split string #\&))))
  33. (list->alist (map (match-lambda
  34. ((key value)
  35. (cons (uri-decode key) (uri-decode value)))) pairs)))
  36. |#
  37. #|
  38. (define (serve-file request body)
  39. (let* ((path (request-path-components request))
  40. (file-path (public-file-path path)))
  41. (if (and file-path (file-exists? file-path))
  42. (values '((content-type . (text/plain)))
  43. (open-input-file file-path))
  44. (not-found request))))
  45. (define (file-server)
  46. (run-server serve-file))
  47. |#
  48. ;; "Here is the piece of code that handles files for the curious:"
  49. #;(let ((file-path (public-file-path path)))
  50. (if (file-exists? file-path)
  51. (let* ((mime-type (mime-type-ref file-path))
  52. (mime-type-symbol (mime-type-symbol mime-type)))
  53. (if (text-mime-type? mime-type)
  54. (values
  55. `((content-type . (,mime-type-symbol)))
  56. (lambda (out-port)
  57. (call-with-input-file file-path
  58. (lambda (in-port)
  59. (display (read-delimited "" in-port)
  60. out-port)))))
  61. (values
  62. `((content-type . (,mime-type-symbol)))
  63. (call-with-input-file file-path
  64. (lambda (in-port)
  65. (get-bytevector-all in-port))))))
  66. (not-found request)))
  67. #|
  68. (define-module (glider mime-types)
  69. :export (mime-type-ref text-mime-type? mime-type-symbol))
  70. (define *mime-types* (make-hash-table 31))
  71. (hash-set! *mime-types* "css" '("text" . "css"))
  72. (hash-set! *mime-types* "txt" '("text" . "plain"))
  73. (hash-set! *mime-types* "png" '("image" . "png"))
  74. (hash-set! *mime-types* "jpg" '("image" . "jpeg"))
  75. (hash-set! *mime-types* "jpeg" '("image" . "jpeg"))
  76. (hash-set! *mime-types* "gif" '("image" . "gif"))
  77. (define (mime-type-ref file-name)
  78. (let* ((dot-position (string-rindex file-name #\.))
  79. (extension (and dot-position
  80. (string-copy file-name (+ dot-position 1))))
  81. (mime-type (and dot-position
  82. (hash-ref *mime-types* extension))))
  83. (if mime-type mime-type '("application" . "octet-stream"))))
  84. (define (mime-type-symbol mime-type)
  85. (string->symbol (string-append (car mime-type) "/" (cdr mime-type))))
  86. (define (text-mime-type? mime-type)
  87. (if (equal? (car mime-type) "text") #t #f))
  88. |#