1
0
Fork 0
tekuti/tekuti/request.scm

159 lines
6.3 KiB
Scheme
Raw Normal View History

2008-02-17 01:17:01 +01:00
;; Tekuti
;; Copyright (C) 2008, 2010, 2011 Andy Wingo <wingo at pobox dot com>
2008-02-17 01:17:01 +01:00
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 3 of
;; the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, contact:
;;
;; Free Software Foundation Voice: +1-617-542-5942
;; 59 Temple Place - Suite 330 Fax: +1-617-542-2652
;; Boston, MA 02111-1307, USA gnu@gnu.org
;;; Commentary:
;;
;; This is the main script that will launch tekuti.
;;
;;; Code:
(define-module (tekuti request)
#:use-module ((srfi srfi-1) #:select (find-tail))
#:use-module (tekuti match-bind)
2008-02-17 01:17:01 +01:00
#:use-module (tekuti util)
#:use-module (web uri)
#:use-module (web request)
#:use-module (rnrs bytevectors)
#:use-module (ice-9 binary-ports)
#:use-module (ice-9 rdelim)
2008-02-17 01:17:01 +01:00
#:use-module (tekuti config)
2008-02-23 23:58:55 +01:00
#:use-module (tekuti base64)
#:export (request-relative-path
request-relative-path-str
request-query-ref
request-path-case
request-authenticated?
request-form-data))
2008-02-19 08:58:16 +01:00
(define* (parse-www-form-urlencoded str #:optional (charset "utf-8"))
comment deletion, post editing. * tekuti/comment.scm (make-new-comment): Refactor to decouple comment.scm from post.scm. * tekuti/git.scm (run): Generalize so it can run any executable. (git-ls-tree, git-ls-subdirs): Check for a #f treeish, return directly in that case, avoiding tracebacks in the forked kid. (git-mktree): Return #f if the tree is empty. (git-rev-list): New command, something like git log. (patch-blob): New function; unused, though. Perhaps I should just delete it. (git-commit-reverse-operations): New function, constructs munge-tree operations to revert a commit. (git-revert): New operator, reverts a commit. Can't use git's revert because it requires a working tree. (munge-tree): Rewrite to unify the various commands, and process the commands in order. Makes a bit more garbage, but is much more understandable. * tekuti/page-helpers.scm (post-editing-form): Add a preview. (admin-post-url): New helper. (redirect): New helper. (admin-post-redirect): New helper. * tekuti/page.scm (page-admin): Show recent changes in the sidebar. (page-admin-new-post): Redirect to the new post. (page-new-comment): Rework for make-new-comment changes, and redirect to the post. Would be nice to redirect to the comment though. (page-admin-changes): New page, lists recent changes. (page-admin-change): New page, shows a change, allowing it to be undone. (page-admin-revert-change): New page, actually reverts a change. Thus we have comment deletion. * tekuti/post.scm (munge-post): New helper, factored out of make-new-post. (parse-post-data): Another new helper. Needs to do more validation, though. (modify-post): Post editing, yay! * tekuti/request.scm (parse-www-form-urlencoded): Factored out of request-form-data. (*request-initializers*): Initialize 'query to have the parsed query string data. * tekuti/url.scm (url:path-part, url:query-part): New somewhat hacky functions. url.scm needs some love. (url:path-split): Split on only the "path part" of the URL. * tekuti/util.scm (foldn): New export. Probably a bad idea. (match-case): New macro, not used though. (string-split/trimming): New util. * tekuti/web.scm (choose-handler): Update the set of pages.
2008-03-07 13:09:47 +01:00
(map
(lambda (piece)
(let ((equals (string-index piece #\=)))
(if equals
2011-01-11 16:17:58 +01:00
(cons (uri-decode (substring piece 0 equals) #:encoding charset)
(uri-decode (substring piece (1+ equals)) #:encoding charset))
(cons (uri-decode piece #:encoding charset) ""))))
comment deletion, post editing. * tekuti/comment.scm (make-new-comment): Refactor to decouple comment.scm from post.scm. * tekuti/git.scm (run): Generalize so it can run any executable. (git-ls-tree, git-ls-subdirs): Check for a #f treeish, return directly in that case, avoiding tracebacks in the forked kid. (git-mktree): Return #f if the tree is empty. (git-rev-list): New command, something like git log. (patch-blob): New function; unused, though. Perhaps I should just delete it. (git-commit-reverse-operations): New function, constructs munge-tree operations to revert a commit. (git-revert): New operator, reverts a commit. Can't use git's revert because it requires a working tree. (munge-tree): Rewrite to unify the various commands, and process the commands in order. Makes a bit more garbage, but is much more understandable. * tekuti/page-helpers.scm (post-editing-form): Add a preview. (admin-post-url): New helper. (redirect): New helper. (admin-post-redirect): New helper. * tekuti/page.scm (page-admin): Show recent changes in the sidebar. (page-admin-new-post): Redirect to the new post. (page-new-comment): Rework for make-new-comment changes, and redirect to the post. Would be nice to redirect to the comment though. (page-admin-changes): New page, lists recent changes. (page-admin-change): New page, shows a change, allowing it to be undone. (page-admin-revert-change): New page, actually reverts a change. Thus we have comment deletion. * tekuti/post.scm (munge-post): New helper, factored out of make-new-post. (parse-post-data): Another new helper. Needs to do more validation, though. (modify-post): Post editing, yay! * tekuti/request.scm (parse-www-form-urlencoded): Factored out of request-form-data. (*request-initializers*): Initialize 'query to have the parsed query string data. * tekuti/url.scm (url:path-part, url:query-part): New somewhat hacky functions. url.scm needs some love. (url:path-split): Split on only the "path part" of the URL. * tekuti/util.scm (foldn): New export. Probably a bad idea. (match-case): New macro, not used though. (string-split/trimming): New util. * tekuti/web.scm (choose-handler): Update the set of pages.
2008-03-07 13:09:47 +01:00
(string-split str #\&)))
(define (request-relative-path r)
(let ((base *private-path-base*)
(path (split-and-decode-uri-path (uri-path (request-uri r)))))
(let ((tail (list-head-match base path (length base))))
(or tail
(error "unexpected path" path base)))))
2008-02-17 01:17:01 +01:00
(define (request-relative-path-str r)
(encode-and-join-uri-path (request-relative-path r)))
2008-02-17 01:17:01 +01:00
(define (request-query-ref r param default)
(let ((q (uri-query (request-uri r))))
2008-02-17 01:17:01 +01:00
(cond
((and q (assoc param (parse-www-form-urlencoded q))) => cdr)
2008-02-17 01:17:01 +01:00
(else default))))
(define (decode-string bv charset)
(if (string-ci=? charset "utf-8")
(utf8->string bv)
(let ((p (open-bytevector-input-port bv)))
(set-port-encoding! p charset)
(read-delimited "" p))))
(define (request-form-data request body)
(if (bytevector? body)
;; Since valid application/x-www-form-urlencoded content only has
;; ascii characters, treat the incoming data as ascii (well,
;; latin-1), then use the charset when percent-decoding the
;; content.
(request-form-data request (decode-string body "iso-8859-1"))
(if (or (not body) (string-null? body))
'()
(let* ((content-type (request-content-type request))
(charset (or (assoc-ref (cdr content-type) "charset")
"utf-8")))
(cond
2011-01-11 16:17:58 +01:00
((equal? (car content-type) 'application/x-www-form-urlencoded)
(parse-www-form-urlencoded body charset))
(else
(error "bad content-type" content-type)))))))
2008-02-23 23:58:55 +01:00
;; danger here, regarding the optional alternate clauses...
(define (request-authenticated? request)
(let ((auth (request-authorization request)))
(and auth
2011-01-11 16:17:58 +01:00
(match-bind "^([^:]*):(.*)$"
(utf8->string (base64-decode (cdr auth)))
(_ user pass)
(and (equal? user *admin-user*)
(equal? pass *admin-pass*))
#f))))
2008-02-23 23:58:55 +01:00
(define-syntax path-proc-case
(lambda (stx)
2008-02-17 01:17:01 +01:00
(define (optional-argument? arg)
(eqv? (string-ref arg (- (string-length arg) 1)) #\?))
2008-02-19 08:58:16 +01:00
(define (required-argument? arg)
(eqv? (string-ref arg (- (string-length arg) 1)) #\!))
2008-02-19 08:58:16 +01:00
(define (output-argument? arg)
(or (optional-argument? arg) (required-argument? arg)))
2008-02-17 01:17:01 +01:00
(define (process-clause clause)
(syntax-case clause (else)
((else expr ...) clause)
(((p ...) proc)
(let ((pat (map (lambda (p)
(symbol->string (syntax->datum p)))
#'(p ...))))
(cond
((find-tail output-argument? pat)
=> (lambda (tail)
(let* ((req (find-tail required-argument? tail))
(opt (find-tail optional-argument? tail))
(npat (length pat))
(ntail (length tail))
(nopt (if opt (length opt) 0))
(nreq (if req (- (length req) nopt) 0)))
#`((let ((pathtail (list-head-match '#,pat
path-var
(- #,npat #,ntail))))
;;(pk pat npat ntail req opt nopt nreq path pathtail)
(if (and pathtail (>= (length pathtail) #,nreq)
(<= (length pathtail) (+ #,nreq #,nopt)))
(append
pathtail
(make-list (- (+ #,nreq #,nopt) (length pathtail)) #f))
#f))
=> (lambda (outargs)
(lambda args
(apply proc (append args outargs))))))))
(else
#`((equal? path-var '#,pat) proc)))))))
(syntax-case stx ()
((_ path clause ...)
(with-syntax (((cond-clause ...) (map process-clause #'(clause ...))))
#'(let ((path-var path))
(cond cond-clause ...)))))))
2008-02-17 01:17:01 +01:00
(define-syntax request-path-case
(syntax-rules ()
((_ request clause ...)
(let ((r request))
(path-proc-case
(cons (symbol->string (request-method r)) (request-relative-path r))
clause ...)))))