/collects/web-server/web-config-unit.rkt

http://github.com/gmarceau/PLT · Racket · 140 lines · 124 code · 7 blank · 9 comment · 10 complexity · 4077be9e28be91d170d04847d8cf6c14 MD5 · raw file

  1. #lang racket/base
  2. (require mzlib/unit
  3. racket/contract)
  4. (require web-server/private/util
  5. web-server/private/cache-table
  6. web-server/configuration/configuration-table-structs
  7. web-server/configuration/configuration-table
  8. web-server/configuration/namespace
  9. web-server/configuration/responders
  10. web-server/web-config-sig)
  11. (provide/contract
  12. [configuration-table->web-config@
  13. (->* (path-string?)
  14. (#:port (or/c false/c number?)
  15. #:listen-ip (or/c false/c string?)
  16. #:make-servlet-namespace make-servlet-namespace/c)
  17. (unit/c (import) (export web-config^)))]
  18. [configuration-table-sexpr->web-config@
  19. (->* (configuration-table-sexpr?)
  20. (#:web-server-root path-string?
  21. #:port (or/c false/c number?)
  22. #:listen-ip (or/c false/c string?)
  23. #:make-servlet-namespace make-servlet-namespace/c)
  24. (unit/c (import) (export web-config^)))])
  25. ; configuration-table->web-config@ : path -> configuration
  26. (define (configuration-table->web-config@
  27. table-file-name
  28. #:port [port #f]
  29. #:listen-ip [listen-ip #f]
  30. #:make-servlet-namespace [make-servlet-namespace (make-make-servlet-namespace)])
  31. (configuration-table-sexpr->web-config@
  32. (call-with-input-file table-file-name read)
  33. #:web-server-root (directory-part table-file-name)
  34. #:port port
  35. #:listen-ip listen-ip
  36. #:make-servlet-namespace make-servlet-namespace))
  37. ; configuration-table-sexpr->web-config@ : string? sexp -> configuration
  38. (define (configuration-table-sexpr->web-config@
  39. sexpr
  40. #:web-server-root [web-server-root (directory-part default-configuration-table-path)]
  41. #:port [port #f]
  42. #:listen-ip [listen-ip #f]
  43. #:make-servlet-namespace [make-servlet-namespace (make-make-servlet-namespace)])
  44. (complete-configuration
  45. web-server-root
  46. (sexpr->configuration-table sexpr)
  47. #:port port
  48. #:listen-ip listen-ip
  49. #:make-servlet-namespace make-servlet-namespace))
  50. ; : str configuration-table -> configuration
  51. (define (complete-configuration
  52. base table
  53. #:port [port #f]
  54. #:listen-ip [listen-ip #f]
  55. #:make-servlet-namespace [make-servlet-namespace (make-make-servlet-namespace)])
  56. (define default-host
  57. (apply-default-functions-to-host-table
  58. base (configuration-table-default-host table)))
  59. (define expanded-virtual-host-table
  60. (map (lambda (x)
  61. (list (regexp (string-append (car x) "(:[0-9]*)?"))
  62. (apply-default-functions-to-host-table base (cdr x))))
  63. (configuration-table-virtual-hosts table)))
  64. (build-configuration
  65. table
  66. (gen-virtual-hosts expanded-virtual-host-table default-host)
  67. #:port port
  68. #:listen-ip listen-ip
  69. #:make-servlet-namespace make-servlet-namespace))
  70. ; : configuration-table host-table -> configuration
  71. (define (build-configuration
  72. table the-virtual-hosts
  73. #:port [port #f]
  74. #:listen-ip [listen-ip #f]
  75. #:make-servlet-namespace [make-servlet-namespace (make-make-servlet-namespace)])
  76. (define the-port (or port (configuration-table-port table)))
  77. (define the-listen-ip (or listen-ip #f))
  78. (define the-make-servlet-namespace make-servlet-namespace)
  79. (unit
  80. (import)
  81. (export web-config^)
  82. (define port the-port)
  83. (define max-waiting (configuration-table-max-waiting table))
  84. (define listen-ip the-listen-ip)
  85. (define initial-connection-timeout (configuration-table-initial-connection-timeout table))
  86. (define virtual-hosts the-virtual-hosts)
  87. (define make-servlet-namespace the-make-servlet-namespace)))
  88. ; apply-default-functions-to-host-table : str host-table -> host
  89. ;; Greg P: web-server-root is the directory-part of the path to the configuration-table (I don't think I like this.)
  90. (define (apply-default-functions-to-host-table web-server-root host-table)
  91. (let ([paths (expand-paths web-server-root (host-table-paths host-table))])
  92. (make-host
  93. (host-table-indices host-table)
  94. (host-table-log-format host-table) (paths-log paths)
  95. (paths-passwords paths)
  96. (let ([m (host-table-messages host-table)]
  97. [conf (paths-conf paths)])
  98. (make-responders
  99. servlet-error-responder
  100. servlet-loading-responder
  101. (gen-authentication-responder (build-path-unless-absolute conf (messages-authentication m)))
  102. (gen-servlets-refreshed (build-path-unless-absolute conf (messages-servlets-refreshed m)))
  103. (gen-passwords-refreshed (build-path-unless-absolute conf (messages-passwords-refreshed m)))
  104. (gen-file-not-found-responder (build-path-unless-absolute conf (messages-file-not-found m)))
  105. (gen-protocol-responder (build-path-unless-absolute conf (messages-protocol m)))
  106. (gen-collect-garbage-responder (build-path-unless-absolute conf (messages-collect-garbage m)))))
  107. (host-table-timeouts host-table)
  108. paths)))
  109. ; expand-paths : str paths -> paths
  110. (define (expand-paths web-server-root paths)
  111. (let ([build-path-unless-absolute
  112. (lambda (b p)
  113. (if p
  114. (build-path-unless-absolute b p)
  115. #f))])
  116. (let* ([host-base (build-path-unless-absolute web-server-root (paths-host-base paths))]
  117. [htdocs-base (build-path-unless-absolute host-base (paths-htdocs paths))])
  118. (make-paths (build-path-unless-absolute host-base (paths-conf paths))
  119. host-base
  120. (build-path-unless-absolute host-base (paths-log paths))
  121. htdocs-base
  122. (build-path-unless-absolute htdocs-base (paths-servlet paths))
  123. (build-path-unless-absolute host-base (paths-mime-types paths))
  124. (build-path-unless-absolute host-base (paths-passwords paths))))))
  125. ; gen-virtual-hosts : (listof (list regexp host)) host ->
  126. ; str -> host-configuration
  127. (define (gen-virtual-hosts expanded-virtual-host-table default-host)
  128. (lambda (host-name-possibly-followed-by-a-collon-and-a-port-number)
  129. (or (ormap (lambda (x)
  130. (and (regexp-match (car x) host-name-possibly-followed-by-a-collon-and-a-port-number)
  131. (cadr x)))
  132. expanded-virtual-host-table)
  133. default-host)))