2002-06-08 11:07:01 -04:00
|
|
|
; some useful utilities
|
|
|
|
|
2002-08-27 05:03:22 -04:00
|
|
|
;;; This file is part of the Scheme Untergrund Networking package.
|
|
|
|
|
|
|
|
;;; Copyright (c) 2002 by Andreas Bernauer.
|
|
|
|
;;; For copyright information, see the file COPYING which comes with
|
|
|
|
;;; the distribution.
|
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
(define (host-name-or-ip addr)
|
|
|
|
(with-fatal-error-handler
|
|
|
|
(lambda (condition more)
|
|
|
|
(call-with-values
|
|
|
|
(lambda () (socket-address->internet-address addr))
|
|
|
|
(lambda (ip port)
|
|
|
|
(format-internet-host-address ip))))
|
|
|
|
(host-info:name (host-info addr))))
|
|
|
|
|
|
|
|
(define (on-interrupt interrupt thunk)
|
|
|
|
(let lp ((event (most-recent-sigevent)))
|
|
|
|
(let ((next (next-sigevent event interrupt)))
|
|
|
|
(thunk)
|
|
|
|
(lp next))))
|
2002-08-24 12:43:26 -04:00
|
|
|
|
|
|
|
(define (socket-address->string socket-address . with-port?)
|
|
|
|
(let ((with-port? (:optional with-port? #t)))
|
|
|
|
(receive (host-address service-port)
|
|
|
|
(socket-address->internet-address socket-address)
|
|
|
|
(if with-port?
|
|
|
|
(format #f "~A:~A"
|
|
|
|
(format-internet-host-address host-address)
|
|
|
|
(format-port service-port))
|
|
|
|
(format #f "~A"
|
|
|
|
(format-internet-host-address host-address))))))
|
|
|
|
|
2002-08-26 10:49:17 -04:00
|
|
|
|
2002-08-26 12:36:25 -04:00
|
|
|
;;; Assemble a filename from ROOT and the elts of PATH-LIST.
|
|
|
|
;;; If the assembled filename contains a .. subdirectory, return #f,
|
|
|
|
;;; otw return the filename.
|
|
|
|
|
|
|
|
(define dotdot-check
|
|
|
|
(let ((dotdot-re (make-regexp "(^|/)\\.\\.($|/)"))) ; Matches a .. subdir.
|
|
|
|
(lambda (root path-list)
|
|
|
|
(let ((fname (if (null? path-list) root ; Bogus hack.
|
|
|
|
(string-append (file-name-as-directory root)
|
|
|
|
(string-join path-list "/")))))
|
|
|
|
(and (not (regexp-exec dotdot-re fname)) ; Check for .. subdir.
|
|
|
|
fname)))))
|
|
|
|
|
|
|
|
;;; Timeout on network writes?
|
|
|
|
|
|
|
|
(define (copy-inport->outport in out . maybe-buffer-size)
|
|
|
|
(let* ((buffer-size (:optional maybe-buffer-size 1024))
|
|
|
|
(buf (make-string buffer-size)))
|
|
|
|
(let loop ()
|
|
|
|
(cond ((read-string! buf in) => (lambda (nchars)
|
|
|
|
(write-string buf out 0 nchars)
|
|
|
|
(loop)))))
|
|
|
|
(force-output out)))
|
|
|
|
|
|
|
|
(define (dump fd)
|
|
|
|
(copy-inport->outport fd (current-output-port)))
|
|
|
|
|
2002-12-29 14:09:37 -05:00
|
|
|
(define (with-lock lock thunk)
|
|
|
|
(dynamic-wind
|
|
|
|
(lambda ()
|
2004-03-16 02:30:14 -05:00
|
|
|
(obtain-lock lock))
|
2002-12-29 14:09:37 -05:00
|
|
|
thunk
|
|
|
|
(lambda ()
|
|
|
|
(release-lock lock))))
|
2003-02-19 12:05:16 -05:00
|
|
|
|
|
|
|
;; Get Header from (RFC822 like) header alist
|
|
|
|
(define (get-header headers tag)
|
|
|
|
(cond ((assq tag headers) => cdr)
|
2004-03-16 02:30:14 -05:00
|
|
|
(else #f)))
|
2004-08-14 17:58:11 -04:00
|
|
|
|
|
|
|
;; GET-NUMERIC-FIELD-VALUE
|
|
|
|
;; generalized function to get a field value of the form 1*DIGIT
|
|
|
|
;; req is a request record, field-name a symbol
|
|
|
|
|
|
|
|
;; check wether a header-field with name field-name is contained in req;
|
|
|
|
;; if not, return #f,
|
2004-08-15 07:49:15 -04:00
|
|
|
;; else, take the first such header field and check wether its field-content conforms to
|
2004-08-14 17:58:11 -04:00
|
|
|
;; field-content = *LWS 1*DIGIT *LWS
|
|
|
|
;; if so, return digit as a number
|
|
|
|
|
|
|
|
(define (get-numeric-field-value req field-name)
|
|
|
|
(let
|
2004-08-15 07:49:15 -04:00
|
|
|
;;try to get first "field-name" header
|
2004-08-14 17:58:11 -04:00
|
|
|
((field-content (get-header (request-headers req) field-name)))
|
2004-08-15 07:49:15 -04:00
|
|
|
(if field-content ;; request contained "field-name" header
|
|
|
|
(let ;;see * below
|
2004-08-15 08:02:36 -04:00
|
|
|
((field-value (string->number (string-trim-both field-content char-set:blank)))) ;;char-set:blank = Space + Tab = LWS from RFC2616 after folding
|
2004-08-15 07:49:15 -04:00
|
|
|
(if (and (integer? field-value) (>= field-value 0)) ;;yes, field value contained only digits, and at least one digit.
|
2004-08-14 17:58:11 -04:00
|
|
|
field-value
|
2004-08-15 07:49:15 -04:00
|
|
|
(http-error
|
|
|
|
(status-code bad-request) req
|
|
|
|
(format #f
|
|
|
|
"~A header contained only whitespace, or characters other than digits, or whitespace between digits"
|
|
|
|
field-name))))
|
2004-08-14 17:58:11 -04:00
|
|
|
#f)))
|
|
|
|
|
|
|
|
|
|
|
|
;;* RFC 2616, 4.2: The field-content does not include any leading or
|
|
|
|
;;trailing LWS: linear white space occurring before the first
|
|
|
|
;;non-whitespace character of the field-value or after the last
|
|
|
|
;;non-whitespace character of the field-value. Such leading or
|
|
|
|
;;trailing LWS MAY be removed without changing the semantics of the
|
|
|
|
;;field value.
|
2004-08-15 07:49:15 -04:00
|
|
|
;;(probably read-rfc822-headers in rfc822.scm should do the job of skipping leading and trailing whitespace?)
|
2004-08-14 17:58:11 -04:00
|
|
|
|
|
|
|
;;get request's message-body length from Content-length: header or
|
|
|
|
;;throw http-error if no such header
|
|
|
|
(define (get-body-length-from-content-length req)
|
2004-08-15 07:49:15 -04:00
|
|
|
(let
|
|
|
|
;;try to get field value of first Content-length header (RFC 2616 allows only one Content-length: header)
|
|
|
|
((maybe-length (get-numeric-field-value req 'content-length)))
|
2004-08-14 17:58:11 -04:00
|
|
|
(or maybe-length
|
|
|
|
(http-error (status-code bad-request) req "No Content-Length header in request"))))
|
|
|
|
|