Commit 71b04188 authored by Nala Ginrut's avatar Nala Ginrut

merge wip-sql-mapping

parent ee01daf2
......@@ -60,5 +60,7 @@ cscope.files
\ No newline at end of file
# -*-shell-script-*-
# Note: `ARTANIS_VERSION' is defined in `' using `git-version-gen'.
* cookies won't work if specifed path/domain
This diff is collapsed.
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
This diff is collapsed.
TOP := $(shell pwd)
LIBS := @GUILE_LIBS@ -shared
CC := @CC@
OBJ := obj
MOD := artanis
MOD_OBJ := $(OBJ)/artanis
BIN_OBJ := $(OBJ)/bin
TEMP_LIB_PATH := $(shell pwd)/$(OBJ)
MOD_COMPILED_PATH := $(shell guile -c "(display %compile-fallback-path)")
MOD_PATH := $(shell guile -c "(display (car %load-path))")
ARTANIS_ETC := etc/artanis
PREFIX := @prefix@
INSTALL := install -c
V := @
mod-sfile := $(shell find $(MOD) -name *.scm)
mod-ofile := $(mod-sfile:.scm=.scm.go)
mod-ofile := $(subst $(MOD)/,$(MOD_OBJ)/,$(mod-ofile))
ALL_TARGETS := version \
$(MOD_OBJ)/%.scm.go: $(MOD)/%.scm
@echo + guilec $<
@mkdir -p $(@D)
$(V)$(GUILEC) -L $(TOP) -o $@ $<
-rm -f $@
$(V)$(GUILE) -c '(call-with-output-file "$@" (lambda (port) (format port "(define-module (artanis version) #:export (artanis-version)) (define artanis-version \"Artanis-~a\")" "$(VERSION)")))'
.PHONY: clean modules install distclean distclean-mk bin version test
-rm -fr $(OBJ)
-rm -fr *.log
-rm -rf autom4te.cache
-rm -rf $(MOD)/version.scm
-rm -rf $(MOD_TMP_PATH)
-rm -rf config.{h,log}
-rm -rf configure
install: $(ALL_TARGETS)
cp -frdp $(OBJ)/*.so $(PREFIX)/lib
cp -frdp $(MOD) $(MOD_PATH)
cp -frdp $(MOD_OBJ)/* $(MOD_TARGET_PATH)
chmod a+w $(MOD_TARGET_PATH)/* -R
$(INSTALL) -m 755 $(OBJ)/bin/* $(PREFIX)/bin
cp -frdp $(ARTANIS_ETC) /etc
distclean: distclean-mk clean clean-config
-rm -f Makefile
modules: $(mod-ofile)
version: $(MOD)/version.scm
@echo $(PREFIX)
@echo $(CC)
@echo $(CFLAGS)
@echo $(GUILE)
@echo $(VERSION)
@echo $(LIBS)
......@@ -2,38 +2,37 @@ Artanis
Artanis aims to be a very lightweight web framework for Scheme.
The philosophy of Artanis would be very radical to try cutting-edge things.
So you are under your own risk to use it. However, it may bring cool experiences
when you play it.
TODO: Move all the APIs to docs page.
## Features:
* very lightweight: the core artanis.scm almost 300 lines, easy to hack
and learn for newbies.
* a relative complete web-server implementation, include error page
handler, support all the HTTP methods (you have to specify your own handler)
* 10K concurrent performance for the server, takes advantage of the
Guile inner server. It's enough for your own site/blog.
* sinatra like style route, that's why it names "artanis" ;-)
* Database support (now using guile-dbi), mysql/sqlite/postgresql. But it's
easy to port to other database binding.
* session support
* HTML template of SXML (very easy to use for Lisper)
* Very lightweight: easy to hack and learn for newbies.
* Support Json/CSV/XML/SXML.
* A complete web-server implementation, include error page handler.
* Aim to be high concurrency performance of the server in the future.
* sinatra-like style route, that's why it names "artanis" ;-)
* Database support(with guile-dbi): mysql/sqlite/postgresql.
* Easy and nice web cache control.
* Efficient HTML template parsing.
First, you need Guile-2.x:
Or just install it with your apt-get/zypper/yum...
I suggest you install Guile-2.0.9 which is the best release so far.
And Artanis will be developped based on 2.0.9 till its first official release version.
Guile-2.0.11 is recommended. It's OK if you try Guile-2.2(the master branch).
You need guile-dbi to handle database:
You need guile-dbi to handle database, please use the highest release:
And you need specified dbd to control the database, you have three choices:
And you need dbd to control the specified database, you have three choices:
* guile-dbd-mysql
* guile-dbd-postgresql
* guile-dbd-sqlite3
All the database operations are in (artanis db).
NOTE: For our example/blog.scm, you need guile-dbd-mysql
All the packages above is easy to install:
......@@ -53,6 +52,7 @@ It's very easy to use:
(init-server) ; make sure alway put it in the main script head.
(use-modules (artanis artanis)) ; use (artanis artanis) module is enough for all things!
(define my-var #f) ; a global var for later use
;; get means GET method in HTTP protocol, and you may use:
......@@ -97,9 +97,23 @@ But you may specify it like this:
## Work with Nginx/Apache
You may try Artanis+Nginx with so-called reverse proxy.
The approach is very easy, enable reverse proxy in Nginx, and
redirect it to localhost:3000 or other port you specified.
(The details tutorial is working in progress...)
You may add these line to your /etc/nginx/nginx.conf:
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Then restart you Nginx:
sudo service nginx restart
And run artanis:
(run #:port 1234)
## APIs docs
``` scheme
This file is about the security issues of Artanis, include some advices or conventions.
* To avoid SQL-injection, please consider the following advices:
1. DO NOT use any escapable chars in the binding field, say, whitespaces:
e.g: /conn/lei mu/
Please use underscore: /conn/lei_mu
In cause you forget to avoid to use uri-decode, see Rule 2 below.
2. DO NOT use uri-decode on binding field:
e.g: /fucked/lei%22;select%20*%20from%20Persons%20where%20Lastname=%22ada
If you use (:conn rc (uri-decode (params rc "name"))),
then you're fucked...
Fortunatly, guile-dbd-mysql-2.0.4 doesn't enable CLIENT_MULTI_STATEMENTS
in default, which could avoid the injection mentioned above.
BUT I suggest you do not rely on this lucky point, please follow the
conventions I written here.
It's still possibly to be SQL-injected, say, passwd checking:
And if someone is stupid enough to use uri-decode on
`username' because of using whitespaces in `username',
it's fucked! list_my_info is expected to list the info of user specified
in `username', but now, it'll show all the users' info include passwd.
* DO NOT use ur-decode when the result will be fed to DB.
You are allowed to use uri-decode when the result will be send to client.
* The response may return redundant http headers if users didn't make it
properly. Artanis is not going to check the redundant headers, it's the users'
duty to do it.
* theme (based on template)
* make own favicon.ico (now use opensuse ico instead)
* Fix SSQL bugs
* implement expand-conds for ssql
* write test-cases for ssql
* add cookie's validation
* rewrite cookie->header-string with call-with-output-string
* implement memcached-session swap algorithm
* implement tpl cache
* implement allowed-method?
* implement tpl cache, not read tpl file every time rendering
* design a generic session framework with optional store method: memcached/file+RCU/Berkerley DB...
* sql-mapping syntax should support multi-sql code, but should deal with SQL-injection
* upload progress json feedback (need new server-core)
* Implement co-operative server with delimited-continuation
** green-thread server
This may need these stuffs:
1. non-blocking;
2. scheduling based on delimited-continuations;
3. guile-dbi needs non-blocking too;
4. enhanced connection pool;
5. epoll/kqueue wrapper.
Some ideas:
** Format a patch to guile-dbi for supporting non-blocking. However, now that SQL-query can be blocked, we have to face the connection-pool problem.
In current implementation, all green-threads works for the same CPU share the same connection from the pool. That's bad, since one of the thread
can be scheduled-out in non-blocking mode, so the next scheduled-in thread should get a clean connection from the pool.
** Maybe there'd be a connection pool for each CPU, and a waiting queue to hold the EWOULDBLOCK dbi-connection. The connection could be recycled when
each scheduled green-thread was over.
* Use sendfile on static files, this may need new server-core.
* Add FAM(File Alteration Monitor) based reload-on-the-fly mechanism to debug mode. `inotify' would be fine enough.
** Never use it in productive environment.
** Could be named 'hotfix-on-debug', and #:hotfix-on-debug should checkout if there's guile-inotify.
* In MySQL, delete huge data in product environment will cause halt. There're two ways:
1. limit 1000 and loop
2. use store procedure
* Security stratage:
1. uri-decode should only be used in response processing, never request processing!
2. Any string which will be sent into DBI shouldn't be uri-decode in advanced!
\ No newline at end of file
This diff is collapsed.
;; -*- indent-tabs-mode:nil; coding: utf-8 -*-
;; Copyright (C) 2014
;; "Mu Lei" known as "NalaGinrut" <>
;; Artanis 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.
;; Artanis is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; 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, see <>.
;; ------------------------------------------------------------------------
;; NOTE: Never use (artanis cache) directly! Please use #:cache handler!!
(define-module (artanis cache)
#:use-module (artanis utils)
#:use-module (artanis config)
#:use-module (artanis route)
#:use-module (artanis page)
#:use-module (ice-9 match)
#:use-module (web request)
#:use-module (web response)
#:export (->maxage
;; Cache-Control
;; 1. The Cache-Control header is the most important header to set as it
;; effectively 'switches on' caching in the browser.
;; 2. With this header in place, and set with a value that enables caching,
;; the browser will cache the file for as long as specified.
;; 3. Without this header the browser will re-request the file on each
;; subsequent request.
;; 4. `public' resources can be cached not only by the end-user’s browser
;; but also by any intermediate proxies that may be serving many other
;; users as well.
;; 5. `private' resources are bypassed by intermediate proxies and can only
;; be cached by the end-client.
;; 6. `max-age' sets a timespan for how long to cache the resource (in seconds).
;; e.g: Cache-Control:public, max-age=31536000
;; -----------------------------------------------------------------------------
;; Expires:
;; 1. When accompanying the Cache-Control header, Expires simply sets a date
;; from which the cached resource should no longer be considered valid.
;; From this date forward the browser will request a fresh copy of the
;; resource. Until then, the browsers local cached copy will be used.
;; e.g: Cache-Control:public
;; Expires: Mon, 25 Jun 2012 21:31:12 GMT
;; 2. If both Expires and max-age are set max-age will take precedence.
;; Conditional Cache
;; A. Time based
;; 1. Server return Last-Modified to enable conditional cache on client side;
;; e.g: Cache-Control:public, max-age=31536000
;; Last-Modified: Mon, 03 Jan 2011 17:45:57 GMT
;; 2. Client should send If-Modified-Since, then server can decide how to cache.
;; e.g: If-Modified-Since: Mon, 03 Jan 2011 17:45:57 GMT
;; 3. In Artanis, we use If-Modified-Since for static page expire checking.
;; B. Content based
;; 1. ETag works in a similar way that its value is a digest of the resources
;; contents (Artanis use MD5 to compute hash for ETag).
;; e.g: Cache-Control:public, max-age=31536000
;; ETag: "15f0fff99ed5aae4edffdd6496d7131f"
;; 2. ETag is useful when for when the last modified date is difficult to determine.
;; 3. On subsequent browser requests the If-None-Match request header is sent
;; with the ETag value of the last requested version of the resource.
;; e.g: If-None-Match: "15f0fff99ed5aae4edffdd6496d7131f"
;; 4. As with the If-Modified-Since header, if the current version has the same
;; ETag value, indicating its value is the same as the browser’s cached copy,
;; then an HTTP status of 304 is returned.
;; 5. In Artanis, ETag for static file is Time based, for dynamic content, content
;; based.
;; Use Cases
;; A. Static page
;; Nothing to say.
;; B. Dynamic page
;; 1. The developer must assess how heavily it can be cached and what the
;; implications might be of serving stale content to the user.
;; 2. Some contents are slowly changed, e.g: RSS
;; 3. Some contents are frequently changed, e.g: Json of twitter timeline.
;; 4. In Artanis, if you enable cache for a dynamic page, the benifits are
;; saving the bandwidth, and accelerate the response of loading page.
;; The server will render the page anyway, but won't send it if the
;; cache hits.
;; C. Cache prevention
;; 1. Cache-Control header can specify no-cache and no-store which informs the
;; browser to not cache the resources under any circumstances.
;; 2. Both values are required as IE uses no-cache, and Firefox uses no-store.
;; e.g: Cache-Control:no-cache, no-store
;; D. Private content
;; 1. The content can be considered sensitive and subject to security measures.
;; 2. Also need to consider the impact of having intermediary caches, such as
;; web proxies. If in doubt, a safe option is not cache these items at all.
;; 3. Ask for resources to only be cached privately.
;; (i.e only within the end-user’s browser cache).
;; e.g: Cache-Control:private, max-age=31536000
(define (emit-HTTP-304)
(response-emit "" #:status 304))
(define (cacheable-request? request)
(and (memq (request-method request) '(GET HEAD))
(not (request-authorization request))
;; We don't cache these conditional requests; just
;; if-modified-since and if-none-match.
;; TODO: provide all the cache features
(not (request-if-match request))
(not (request-if-range request))
(not (request-if-unmodified-since request))))
(define *lookaside-table* (make-hash-table))
(define-syntax-rule (get-from-tlb path)
(hash-ref *lookaside-table* path))
(define-syntax-rule (store-to-tlb! path hash)
(hash-set! *lookaside-table* path hash))
(define-syntax-rule (cache-to-tlb! rc hash)
(store-to-tlb! (rc-path rc) hash))
(define (try-to-cache-dynamic-content rc body etag opts)
(define (->cc o)
(match o
;; public cache with default max-age
(format #f "public,max-age=~a" (get-conf '(cache maxage))))
(('public . maxage)
(let ((m (if (null? maxage) (get-conf '(cache maxage)) (car maxage))))
(format #f "public,max-age=~a" m)))
(('private . maxage)
(let ((m (if (null? maxage) (get-conf '(cache maxage)) (car maxage))))
(format #f "private,max-age=~a" m)))
(else (throw 'artanis-err "->cc: Invalid opts!" o))))
(cache-to-tlb! rc etag) ; cache the hash the TLB
(response-emit body #:headers `((ETag . ,etag)
(Cache-Control . ,(->cc opts)))))
(define (generate-ETag filename)
((file-exists? filename)
(let ((st (stat filename)))
;; NOTE: ETag must be around with double-quote explicitly!
(format #f "\"~X-~X-~X\""
(stat:ino st) (stat:mtime st) (stat:size st))))
(else '())))
(define-syntax-rule (->headers rc)
(request-headers (rc-req rc)))
(define-syntax-rule (->If-None-Match rc)
(assoc-ref (->headers rc) 'if-none-match))
(define (If-None-Match-hit? rc etag)
(define (-> e)
;; NOTE: The if-none-match sent from Chromium dropped double-quote,
;; so we have to add it here for comparing. It's reasonable with
;; RFC2616:
;; Dunno about other clients...we just stick to the RFC.
(string-concatenate (list "\"" (caar e) "\"")))
(and=> (->If-None-Match rc)
(lambda (e) (string=? (-> e) etag))))
;; ETag for dynamic content is content based
(define (try-to-cache-body rc body . opts)
(define (gen-etag-for-dynamic-content b)
;; NOTE: ETag must be around with double-quote explicitly!
(string-concatenate (list "\"" (string->md5 b) "\"")))
(define-syntax-rule (get-proper-hash)
(or (get-from-tlb (rc-path rc)) ; get hash from TLB
(gen-etag-for-dynamic-content body))) ; or generate new hash
((cacheable-request? (rc-req rc))
;; NOTE: In Artanis, dynamic page is content based caching, so we don't checkout
;; If-Modified-Since header.
(let ((etag (get-proper-hash)))
(if (If-None-Match-hit? rc etag)
(try-to-cache-dynamic-content rc body etag opts))))
(else body)))
(define-syntax-rule (emit-static-file-with-cache file etag status max-age)
(let ((cc (format #f "~a,max-age=~a" status max-age)))
`((Cache-Control . ,cc)
,@(if (null? etag) '() `((ETag . ,etag)))))))
(define-syntax-rule (emit-static-file-without-cache file)
(let ((headers `((Cache-Control . "no-cache,no-store"))))
(emit-response-with-file file headers)))
;; NOTE: the ETag of static file is time based, not content based
(define (try-to-cache-static-file rc file status max-age)
((cacheable-request? (rc-req rc))
;; TODO: checkout last-modified for expires
(let ((etag (generate-ETag file)))
(if (If-None-Match-hit? rc etag)
(emit-HTTP-304) ; cache hit
(emit-static-file-with-cache file etag status max-age))))
(else (emit-static-file-without-cache file))))
(define-syntax-rule (->maxage maxage)
(match maxage
((? integer? m) m)
(((? integer? m)) m)
(() (get-conf '(cache maxage)))
(else (throw 'artanis-err "->maxage: Invalid maxage!" maxage))))
;; -*- indent-tabs-mode:nil; coding: utf-8 -*-
;; Copyright (C) 2013
;; Copyright (C) 2013,2014