http.scm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2021, 2022 Maxime Devos <maximedevos@telenet.be>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix tests http)
  20. #:use-module (ice-9 threads)
  21. #:use-module (web server)
  22. #:use-module (web server http)
  23. #:use-module (web request)
  24. #:use-module (web response)
  25. #:use-module (web uri)
  26. #:use-module (srfi srfi-11)
  27. #:use-module (srfi srfi-39)
  28. #:use-module (srfi srfi-71) ; extended 'let'
  29. #:use-module (ice-9 match)
  30. #:export (with-http-server
  31. with-http-server*
  32. call-with-http-server
  33. call-with-http-server*
  34. %http-server-port
  35. %local-url
  36. call-with-unreachable-http-server
  37. with-unreachable-http-server))
  38. ;;; Commentary:
  39. ;;;
  40. ;;; Code to spawn a Web server for testing purposes.
  41. ;;;
  42. ;;; Code:
  43. ;; TCP port allocated to the stub HTTP server.
  44. (define-syntax-parameter %http-server-port
  45. (lambda (stx)
  46. (syntax-violation '%http-server-port
  47. "%http-server-port used outside bind-http-port" stx)))
  48. (define-syntax-rule (bind-http-port port body ...)
  49. "Bind %HTTP-SERVER-PORT to PORT."
  50. (syntax-parameterize ((%http-server-port (identifier-syntax port)))
  51. body ...))
  52. (define (open-http-server-socket)
  53. "Return a listening socket for the web server and the port
  54. that the socket was bound to."
  55. (catch 'system-error
  56. (lambda ()
  57. (let ((sock (socket PF_INET SOCK_STREAM 0)))
  58. (setsockopt sock SOL_SOCKET SO_REUSEADDR 1)
  59. (bind sock (make-socket-address AF_INET INADDR_LOOPBACK 0))
  60. (values sock (sockaddr:port (getsockname sock)))))
  61. (lambda args
  62. (let ((err (system-error-errno args)))
  63. (format (current-error-port)
  64. "warning: cannot run Web server for tests: ~a~%"
  65. (strerror err))
  66. (values #f #f)))))
  67. (define (make-%local-url %http-server-port)
  68. ;; To preserve the procedure name in backtraces, this is not written as
  69. ;; a lambda or macro.
  70. (define* (%local-url #:optional (resource "/foo/bar"))
  71. "The URL to the resource named RESOURCE on the HTTP server in hu
  72. By default, '/foo/bar' is used.'"
  73. ;; URL to use for 'home-page' tests.
  74. (format #f "http://localhost:~a~a" %http-server-port resource))
  75. %local-url)
  76. (define-syntax %local-url
  77. (identifier-syntax (make-%local-url %http-server-port)))
  78. (define* (call-with-http-server* handle proc #:key
  79. (last-response? (const #false)))
  80. "Call PROC with the port of a fresh HTTP server running and responding to
  81. HTTP requests with HANDLE (see (guile)Web Server). HANDLE is additionally
  82. passed the port as first argument.
  83. The server will quit after THUNK returns. It will also quit if LAST-RESPONSE?
  84. returns true."
  85. (define (http-write server client response body)
  86. "Write RESPONSE."
  87. (let* ((response (write-response response client))
  88. (port (response-port response)))
  89. (cond
  90. ((not body)) ;pass
  91. (else
  92. (write-response-body response body)))
  93. (close-port port)
  94. (when (last-response?)
  95. (throw 'quit))
  96. (values)))
  97. ;; Mutex and condition variable to synchronize with the HTTP server.
  98. (define %http-server-lock (make-mutex))
  99. (define %http-server-ready (make-condition-variable))
  100. (define %http-real-server-port #f)
  101. (define (http-open . args)
  102. "Start listening for HTTP requests and signal %HTTP-SERVER-READY."
  103. (with-mutex %http-server-lock
  104. (let ((result (apply (@@ (web server http) http-open) args)))
  105. (signal-condition-variable %http-server-ready)
  106. result)))
  107. (define-server-impl stub-http-server
  108. ;; Stripped-down version of Guile's built-in HTTP server.
  109. http-open
  110. (@@ (web server http) http-read)
  111. http-write
  112. (@@ (web server http) http-close))
  113. (define (server-body)
  114. (let-values (((socket port) (open-http-server-socket)))
  115. (set! %http-real-server-port port)
  116. (catch 'quit
  117. (lambda ()
  118. (run-server (lambda arguments (apply handle port arguments))
  119. stub-http-server `(#:socket ,socket)))
  120. (lambda _
  121. (close-port socket)))))
  122. (with-mutex %http-server-lock
  123. (let ((server (make-thread server-body)))
  124. (wait-condition-variable %http-server-ready %http-server-lock)
  125. ;; Normally SERVER exits automatically once it has received a request.
  126. (let-values ((results (proc %http-real-server-port)))
  127. ;; exit the server thread
  128. (system-async-mark (lambda () (throw 'quit)) server)
  129. (apply values results)))))
  130. (define* (call-with-http-server responses+data proc)
  131. "Call PROC with the port of an HTTP server running and returning
  132. RESPONSES+DATA on HTTP requests. Each element of RESPONSES+DATA must be a
  133. triple containing an URI path (including the query, if any), response and a
  134. string.
  135. The requests to the HTTP server must match the URL path from the triples in
  136. RESPONSES+DATA, in-order.
  137. The argument RESPONSES+DATA is not a list but a procedure accepting the port
  138. number of the HTTP server returning the list of responses.
  139. The server will exit after the last response or when THUNK returns, whichever
  140. happens the earliest."
  141. (define (sanitize-response+data response+data)
  142. (match response+data
  143. (((? string? uri) (? response? response) data)
  144. (list uri response data))
  145. (((? string? uri) (? integer? code) data)
  146. (list uri
  147. (build-response #:code code
  148. #:reason-phrase "Such is life")
  149. data))))
  150. (define (responses port)
  151. (map sanitize-response+data (responses+data port)))
  152. (define (handle port request body)
  153. (match (responses port)
  154. (((uri response data) rest ...)
  155. (unless (string=? uri (uri->string (request-uri request)))
  156. (error "this URI should not be contacted!"
  157. (request-uri request)))
  158. (set! responses (const rest))
  159. (values response data))))
  160. (call-with-http-server* handle proc
  161. #:last-response?
  162. (lambda () (null? (responses 'unused)))))
  163. (define-syntax with-http-server
  164. (syntax-rules ()
  165. ((_ responses+data body ...)
  166. (call-with-http-server (lambda (port) (bind-http-port port responses+data))
  167. (lambda (port) (bind-http-port port body ...))))))
  168. (define-syntax with-http-server*
  169. (syntax-rules ()
  170. ((_ handle body ...)
  171. (call-with-http-server* (lambda (port . arguments)
  172. (bind-http-port port (apply handle arguments)))
  173. (lambda (port) (bind-http-port port body ...))))))
  174. (define (call-with-unreachable-http-server proc)
  175. "Call PROC with the port of a HTTP server that refuses
  176. all connections."
  177. ;; As long as 'listen' is not actually called on the ‘listening’
  178. ;; socket, connections will be refused.
  179. (let ((socket port (open-http-server-socket)))
  180. (call-with-port socket (lambda (_) (proc port)))))
  181. (define-syntax with-unreachable-http-server
  182. (syntax-rules ()
  183. "Run BODY ... with a HTTP server that refuses all connections.
  184. The URL of this HTTP server can be found with '%local-url' in the lexical
  185. environment of BODY ..."
  186. ((_ body ...)
  187. (call-with-unreachable-http-server
  188. (lambda (port) (bind-http-port port body ...))))))
  189. ;;; http.scm ends here