form.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ;; This file is part of scheme-GNUnet
  2. ;; Copyright (C) 2021 GNUnet e.V.
  3. ;;
  4. ;; scheme-GNUnet is free software: you can redistribute it and/or modify it
  5. ;; under the terms of the GNU Affero General Public License as published
  6. ;; by the Free Software Foundation, either version 3 of the License,
  7. ;; or (at your option) any later version.
  8. ;;
  9. ;; GNUnet is distributed in the hope that it will be useful, but
  10. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Affero General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Affero General Public License
  15. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;
  17. ;; SPDX-License-Identifier: AGPL-3.0-or-later
  18. ;; TODO: look into integrating this into Guile proper.
  19. (define-module (web form)
  20. #:use-module (rnrs bytevectors)
  21. #:use-module (ice-9 match)
  22. #:use-module (ice-9 binary-ports)
  23. #:use-module (ice-9 textual-ports)
  24. #:use-module (ice-9 control)
  25. #:use-module (ice-9 string-fun)
  26. #:export (urlencoded->alist))
  27. ;; application/x-www-form-urlencoded, documented in 8.2.1.
  28. ;; of RFC 1866
  29. ;; 8.2.1 ‘[...] space characters are replaced by #\+ [...]’
  30. ;;
  31. ;; Presumably only #\ is meant here and not the non-breaking space (NBSP),
  32. ;; otherwise NBSP could not be distinguished from the regular space character
  33. ;; #\ .
  34. ;;
  35. ;; 8.2.1 ‘[...] [non-alphanumeric] characters are replaced by %HH [...]’.
  36. ;;
  37. ;; Presumably with ‘non-alphanumeric’, ‘non-alphanumeric or non-ASCII’
  38. ;; is meant here, otherwise the validity of application/x-www-form-urlencoded
  39. ;; data could depend on the Unicode standard used.
  40. ;;
  41. ;; In practice, Firefox doesn't escape - and _, so include those as well
  42. ;; for compatibility.
  43. ;; TODO: isn't a-zA-Z0-9 problematic under some locales?
  44. (define encoded-pat "^(\\+|[a-zA-Z0-9_-]|%[0-9A-F][0-9A-F])*$")
  45. (define encoded-regex (make-regexp encoded-pat))
  46. (define (try-utf8->string bv)
  47. "Like utf8->string, but return #false instead of raising an error if
  48. @var{bv} is not valid UTF-8."
  49. (catch 'decoding-error
  50. ;; RFC 1866 doesn't specify the character encoding, so assume UTF-8.
  51. (lambda () (utf8->string bv))
  52. (lambda _ #false)))
  53. (define (urlencoded-string->alist string)
  54. (let/ec return
  55. (let ()
  56. (define (oops)
  57. (return #false))
  58. (when (string-null? string)
  59. (return '()))
  60. (define fields (string-split string #\&))
  61. (define (unescape string)
  62. ;; Validate the syntax of STRING ...
  63. (unless (regexp-exec encoded-regex string)
  64. (oops))
  65. ;; ... replace #\+ with #\ ...
  66. (define string-with-space (string-replace-substring string "+" " "))
  67. (define bv
  68. (call-with-output-bytevector
  69. (lambda (port)
  70. ;; ... and undo % escapes.
  71. (define (search remainder)
  72. (define next-% (string-index remainder #\%))
  73. (if next-%
  74. (begin
  75. (put-string port (substring remainder 0 next-%))
  76. (undo-% (substring remainder next-%)))
  77. (put-string port remainder)))
  78. (define (undo-% remainder)
  79. (define octet
  80. (string->number (substring remainder 1 3) 16))
  81. ;; 8.2.1 ‘[...] [non-alphanumeric] characters are replaced by
  82. ;; %HH [...]’.
  83. ;;
  84. ;; The syntax of application/x-www-form-urlencoded is given in
  85. ;; terms of how to encode the fields, and alphanumeric characters
  86. ;; are not included there, thus alphanumeric characters are
  87. ;; forbidden.
  88. (when (or (<= (char->integer #\a) octet (char->integer #\z))
  89. (<= (char->integer #\A) octet (char->integer #\Z))
  90. (<= (char->integer #\0) octet (char->integer #\9)))
  91. (oops))
  92. (put-u8 port octet)
  93. (search (substring remainder 3)))
  94. (search string-with-space))))
  95. ;; RFC 1866 doesn't specify the character encoding, so assume UTF-8.
  96. ;; The resulting bytevector could be bogus UTF-8, so catch
  97. ;; 'decoding-error'.
  98. (or (try-utf8->string bv)
  99. (oops)))
  100. (define (decode-field field)
  101. (match (string-split field #\=)
  102. ((escaped-field-name escaped-field-value)
  103. (cons (unescape escaped-field-name) (unescape escaped-field-value)))
  104. (_ (oops))))
  105. (map decode-field fields))))
  106. (define (urlencoded->alist body)
  107. "Decode body, a bytevector holding a application/x-www-form-urlencoded,
  108. to an association list of string-valued key-value pairs. Return #false
  109. if the bytevector could not be parsed."
  110. (and=> (try-utf8->string body) urlencoded-string->alist))