Skip to content
Browse files

[klone] regex-url-to-function supplier (kilt)

  • Loading branch information...
1 parent bd26963 commit 500cf84f913995260835590b909b8572ca877ed7 tat committed Oct 27, 2008
View
2 Makefile
@@ -19,6 +19,6 @@ endif
include $(MAKL_DIR)/mk/subdir.mk
# deps
-webapp site src: build/libu
+webapp site src: build/libu
contrib: src
View
7 Makefile.dist
@@ -1,4 +1,4 @@
-# $Id: Makefile.dist,v 1.34 2008/10/21 12:52:54 tat Exp $
+# $Id: Makefile.dist,v 1.35 2008/10/27 21:28:03 tat Exp $
include common.mk
@@ -25,7 +25,6 @@ DISTFILES += contrib/iocat/test.sh contrib/iocat/Makefile
DISTFILES += $(wildcard contrib/iowc/*.c) contrib/iowc/Makefile
# klone sources and headers
-DISTFILES += site/Makefile
DISTFILES += $(wildcard klone/*.h) klone/Makefile
DISTFILES += $(wildcard webapp/*.[ch]) webapp/Makefile webapp/Makefile-webapp
DISTFILES += src/Makefile src/tools/Makefile
@@ -43,6 +42,10 @@ DISTFILES += $(wildcard src/libutils/*.[ch]) src/libutils/Makefile
DISTFILES += $(wildcard src/libhook/*.[ch]) src/libhook/Makefile
DISTFILES += $(wildcard src/tools/klone/*.[ch]) src/tools/klone/Makefile
+# site/ dir
+DISTFILES += site/Makefile
+DISTFILES += $(wildcard site/*.[ch])
+
# configuration samples (etc)
DISTFILES += $(wildcard etc/*.conf) etc/psk.db
View
2 VERSION
@@ -1 +1 @@
-2.2.0alpha3
+2.2.0alpha6
View
10 configure
@@ -1,6 +1,6 @@
#!/bin/sh
#
-# $Id: configure,v 1.93 2008/10/21 12:44:40 tat Exp $
+# $Id: configure,v 1.94 2008/10/27 21:28:03 tat Exp $
export makl_conf_h=klone_conf.h
export MAKL_DIR=`pwd`/makl
@@ -34,6 +34,8 @@ makl_args_def "enable_sup_fs" "" "" "enables filesystem web access"
makl_args_def "enable_fs" "" "" "--enable_sup_fs alias"
makl_args_def "enable_sup_cgi" "" "" "enables external CGIs"
makl_args_def "enable_cgi" "" "" "--enable_sup_cgi alias"
+makl_args_def "enable_sup_kilt" "" "" "enables kilt module"
+makl_args_def "enable_kilt" "" "" "--enable_sup_kilt alias"
makl_args_def "enable_warns" "" "" "set CFLAGS for extra warnings"
makl_args_def "disable_cxx" "" "" "disable C++ support"
makl_args_def "install_daemon_only" "" "" "install just Klone daemon file"
@@ -47,10 +49,16 @@ __makl_enable_debug () { makl_set_var_mk "KLONE_DEBUG" ;
makl_append_var_mk "CFLAGS" "-g -DDEBUG" ; }
__makl_enable_libu_debug () { makl_set_var_mk "LIBU_DEBUG" ; }
__makl_enable_profile () { makl_append_var_mk "CFLAGS" "-pg -a" ; }
+
__makl_enable_sup_fs () { makl_set_var "ENABLE_SUP_FS" ; }
__makl_enable_fs () { __makl_enable_sup_fs ; }
+
+__makl_enable_sup_kilt () { makl_set_var "ENABLE_SUP_KILT" ; }
+__makl_enable_kilt () { __makl_enable_sup_kilt ; }
+
__makl_enable_sup_cgi () { makl_set_var "ENABLE_SUP_CGI" ; }
__makl_enable_cgi () { __makl_enable_sup_cgi; }
+
__makl_disable_cxx () { makl_unset_var "USE_CXX" ; }
__makl_install_daemon_only ()
{ makl_set_var "INSTALL_DAEMON_ONLY" ; }
View
4 klone/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.15 2008/10/03 10:19:07 tho Exp $
+# $Id: Makefile,v 1.16 2008/10/27 21:28:03 tat Exp $
include common.mk
include ../Makefile.conf
@@ -10,7 +10,7 @@ INCS = addr.h atom.h backend.h broker.h caes.h cgi.h codec.h \
http.h io.h ioprv.h klone.h klog.h klogprv.h md5.h mime_map.h os.h \
page.h parser.h ppc.h ppc_cmd.h request.h response.h rsfilter.h run.h \
server.h ses_prv.h session.h supplier.h timer.h tls.h tlsprv.h \
- translat.h utils.h va.h var.h varprv.h vars.h version.h
+ translat.h utils.h va.h var.h varprv.h vars.h version.h dypage.h
ifdef HAVE_LIBOPENSSL
INCS += ccipher.h
View
45 klone/dypage.h
@@ -0,0 +1,45 @@
+#ifndef _KLONE_DYPAGE_H_
+#define _KLONE_DYPAGE_H_
+#include <klone/request.h>
+#include <klone/response.h>
+#include <klone/session.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct dypage_args_s;
+typedef struct dypage_args_s dypage_args_t;
+
+typedef void (*dypage_fun_t)(dypage_args_t *);
+
+enum { DYPAGE_MAX_PARAMS = 16 };
+
+typedef struct dypage_param_s
+{
+ const char *key, *val;
+} dypage_param_t;
+
+const char *dypage_get_param(dypage_args_t *args, const char *key);
+
+struct dypage_args_s
+{
+ request_t *rq;
+ response_t *rs;
+ session_t *ss;
+ dypage_fun_t fun; /* callback function */
+ void *opaque; /* additional opaque callback argument */
+
+ size_t argc; /* # of argv */
+ const char **argv; /* regex submatches (#0 is the full url) */
+ size_t nparams; /* # of named params */
+ dypage_param_t *params; /* array of named parameters */
+};
+
+int dypage_serve(dypage_args_t *args);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
View
5 klone/emb.h
@@ -5,7 +5,7 @@
* This file is part of KLone, and as such it is subject to the license stated
* in the LICENSE file which you have received as part of this distribution.
*
- * $Id: emb.h,v 1.16 2007/09/15 16:36:12 tat Exp $
+ * $Id: emb.h,v 1.17 2008/10/27 21:28:03 tat Exp $
*/
#ifndef _KLONE_EMB_H_
@@ -18,6 +18,7 @@
#endif /* HAVE_STDINT */
#include <u/libu.h>
#include <klone/klone.h>
+#include <klone/dypage.h>
#ifdef __cplusplus
extern "C" {
@@ -57,7 +58,7 @@ typedef struct embfile_s
typedef struct embpage_s
{
embres_t res; /* any emb resource must start with a embres_t */
- void (*run)(request_t*, response_t*, session_t*); /* page code */
+ dypage_fun_t fun;
} embpage_t;
int emb_init(void);
View
33 klone/kilt.h
@@ -0,0 +1,33 @@
+#ifndef _KLONE_KILT_H_
+#define _KLONE_KILT_H_
+#include <stdlib.h>
+#include <klone/kilt_urls.h>
+#include <klone/dypage.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* url regex to function mapping */
+typedef struct kilt_url_s
+{
+ const char *pattern; /* url pattern */
+ dypage_fun_t fun;
+ dypage_param_t params[DYPAGE_MAX_PARAMS];
+} kilt_url_t;
+
+/* run the .kl1 script whose name is in "script" param */
+void kilt_run_script(dypage_args_t *dp) ;
+
+/* function that displays passed-in arguments */
+void kilt_show_params(dypage_args_t *dp);
+
+/* must be set by the user (helper macros in kilt_urls.h) */
+extern kilt_url_t *kilt_urls;
+extern size_t kilt_nurls;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
View
18 klone/kilt_urls.h
@@ -0,0 +1,18 @@
+#ifndef _KLONE_KILT_URLS_H_
+#define _KLONE_KILT_URLS_H_
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define KILT_URLS(u) size_t kilt_nurls = (sizeof(u)/sizeof(kilt_url_t)); kilt_url_t *kilt_urls = u;
+#define NO_PARAMS { { NULL, NULL } }
+#define PARAMS(...) { __VA_ARGS__ },
+#define P(k,v) {k,v}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
View
3 klone/klone.h
@@ -5,7 +5,7 @@
* This file is part of KLone, and as such it is subject to the license stated
* in the LICENSE file which you have received as part of this distribution.
*
- * $Id: klone.h,v 1.9 2008/10/03 10:19:07 tho Exp $
+ * $Id: klone.h,v 1.10 2008/10/27 21:28:04 tat Exp $
*/
#ifndef _KLONE_H_
@@ -16,6 +16,7 @@
#include <klone/request.h>
#include <klone/response.h>
#include <klone/session.h>
+#include <klone/dypage.h>
#include <klone/io.h>
#include <klone/codecs.h>
#include <klone/utils.h>
View
8 klone/request.h
@@ -5,13 +5,14 @@
* This file is part of KLone, and as such it is subject to the license stated
* in the LICENSE file which you have received as part of this distribution.
*
- * $Id: request.h,v 1.20 2008/06/04 17:48:01 tat Exp $
+ * $Id: request.h,v 1.21 2008/10/27 21:28:04 tat Exp $
*/
#ifndef _KLONE_REQUEST_H_
#define _KLONE_REQUEST_H_
#include <sys/types.h>
+#include <time.h>
#include <u/libu.h>
#include <klone/header.h>
#include <klone/io.h>
@@ -95,6 +96,11 @@ const char *request_get_cookie(request_t *rq, const char *name);
vhost_t *request_get_vhost(request_t *rq);
int request_set_vhost(request_t *rq, vhost_t *vhost);
+/* internal */
+struct supplier_s;
+void request_set_sup_info(request_t *, struct supplier_s*, void *, time_t);
+void request_get_sup_info(request_t *, struct supplier_s **, void **, time_t *);
+
#ifdef __cplusplus
}
#endif
View
6 klone/supplier.h
@@ -5,7 +5,7 @@
* This file is part of KLone, and as such it is subject to the license stated
* in the LICENSE file which you have received as part of this distribution.
*
- * $Id: supplier.h,v 1.7 2007/10/25 20:26:56 tat Exp $
+ * $Id: supplier.h,v 1.8 2008/10/27 21:28:04 tat Exp $
*/
#ifndef _KLONE_SUPPLIER_H_
@@ -25,8 +25,8 @@ typedef struct supplier_s
const char *name; /* descriptive name */
int (*init)(void);
void (*term)(void);
- int (*is_valid_uri)(http_t *h, request_t *rq, const char *buf, size_t len,
- time_t *mtime);
+ int (*is_valid_uri)(http_t *h, request_t *rq, const char *file, size_t len,
+ void **handle, time_t *mtime);
int (*serve)(request_t *, response_t*);
} supplier_t;
View
8 site/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.9 2008/10/18 17:23:32 tat Exp $
+# $Id: Makefile,v 1.10 2008/10/27 21:28:04 tat Exp $
include common.mk
include ../Makefile.conf
@@ -12,7 +12,7 @@ NO_INSTALL = true
endif
CFLAGS += ${WEBAPP_CFLAGS}
-CLEANFILES += *.c *.cc *.a *.o *.d *.kld autogen.*
+CLEANFILES += pg_*.c pg_*.cc pg_*.o pg_*.d pg_*.kld autogen.* register.c libsite.a
# if already set don't change it
KLONE ?= ../src/tools/klone/klone
@@ -28,8 +28,8 @@ include lib.mk
beforedepend: register.c
-register.c:
- @echo "void register_pages(){} void unregister_pages(){}" > $@
+register.c: register-def.c
+ @cp -f $< $@
autogen.dps:
@echo > $@
View
11 site/register-def.c
@@ -0,0 +1,11 @@
+#include <klone_conf.h>
+
+#if ENABLE_SUP_KILT
+#include <klone/kilt.h>
+#include <klone/kilt_urls.h>
+kilt_url_t *kilt_urls = NULL;
+size_t kilt_nurls = 0;
+#endif
+
+void register_pages(void);void unregister_pages(void);
+void register_pages(void){} void unregister_pages(void){}
View
5 src/kloned/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.31 2007/11/09 22:06:26 tat Exp $
+# $Id: Makefile,v 1.32 2008/10/27 21:28:04 tat Exp $
include common.mk
include ../../Makefile.conf
@@ -29,8 +29,7 @@ LDADD += ../libhttp/libhttp.a ../libutils/libutils.a ../libpage/libpage.a \
../libklog/libklog.a ../../webapp/libwebapp.a ../libhttp/libhttp.a \
../libpage/libpage.a ../libutils/libutils.a ../libio/libio.a \
../libtls/libtls.a ../libcodec/libcodec.a ../libpage/libpage.a \
- ../libhook/libhook.a \
- ${WEBAPP_LDADD}
+ ../libhook/libhook.a ${WEBAPP_LDADD}
# add libu flags
CFLAGS += -I../../libu/include
View
3 src/libemb/emb.c
@@ -5,7 +5,7 @@
* This file is part of KLone, and as such it is subject to the license stated
* in the LICENSE file which you have received as part of this distribution.
*
- * $Id: emb.c,v 1.18 2008/10/18 13:04:02 tat Exp $
+ * $Id: emb.c,v 1.19 2008/10/27 21:28:04 tat Exp $
*/
#include <klone/emb.h>
@@ -101,7 +101,6 @@ int emb_lookup(const char *filename, embres_t **pr)
*pr = obj->val;
return 0;
-
err:
/* not found */
return ~0;
View
8 src/libhttp/Makefile
@@ -1,10 +1,10 @@
-# $Id: Makefile,v 1.6 2007/11/09 01:30:45 tat Exp $
+# $Id: Makefile,v 1.7 2008/10/27 21:28:04 tat Exp $
include common.mk
include ../../Makefile.conf
LIB = http
-SRCS = broker.c cgi.c http.c sup_emb.c access.c vhost.c
+SRCS = broker.c cgi.c http.c sup_emb.c access.c vhost.c dypage.c
ifdef ENABLE_SUP_FS
SRCS += sup_fs.c
@@ -16,6 +16,10 @@ SRCS += sup_cgi.c
endif
endif
+ifdef ENABLE_SUP_KILT
+SRCS += sup_kilt.c
+endif
+
ifdef INSTALL_DAEMON_ONLY
NO_INSTALL = true
endif
View
75 src/libhttp/broker.c
@@ -5,7 +5,7 @@
* This file is part of KLone, and as such it is subject to the license stated
* in the LICENSE file which you have received as part of this distribution.
*
- * $Id: broker.c,v 1.20 2008/04/30 13:00:00 tat Exp $
+ * $Id: broker.c,v 1.21 2008/10/27 21:28:04 tat Exp $
*/
#include <u/libu.h>
@@ -18,6 +18,9 @@
enum { MAX_SUP_COUNT = 8 }; /* max number of suppliers */
extern supplier_t sup_emb;
+#ifdef ENABLE_SUP_KILT
+extern supplier_t sup_kilt;
+#endif
#ifdef ENABLE_SUP_CGI
extern supplier_t sup_cgi;
#endif
@@ -35,55 +38,75 @@ int broker_is_valid_uri(broker_t *b, http_t *h, request_t *rq, const char *buf,
{
int i;
time_t mtime;
+ void *handle;
dbg_goto_if (b == NULL, notfound);
dbg_goto_if (buf == NULL, notfound);
for(i = 0; b->sup_list[i]; ++i)
- if(b->sup_list[i]->is_valid_uri(h, rq, buf, len, &mtime))
+ {
+ if(b->sup_list[i]->is_valid_uri(h, rq, buf, len, &handle, &mtime))
+ {
+ request_set_sup_info(rq, b->sup_list[i], handle, mtime);
return 1; /* found */
-
+ }
+ }
notfound:
return 0;
}
int broker_serve(broker_t *b, http_t *h, request_t *rq, response_t *rs)
{
const char *file_name;
- int i;
+ supplier_t *sup;
+ void *handle;
time_t mtime, ims;
+ int i;
dbg_err_if (b == NULL);
dbg_err_if (rq == NULL);
dbg_err_if (rs == NULL);
-
- file_name = request_get_resolved_filename(rq);
- for(i = 0; b->sup_list[i]; ++i)
- {
- if(b->sup_list[i]->is_valid_uri(h, rq, file_name, strlen(file_name),
- &mtime) )
+
+ /* get cached sup info */
+ request_get_sup_info(rq, &sup, &handle, &mtime);
+
+ if(sup == NULL)
+ {
+ file_name = request_get_resolved_filename(rq);
+ for(i = 0; b->sup_list[i]; ++i)
{
- ims = request_get_if_modified_since(rq);
- if(ims && ims >= mtime)
+ if(b->sup_list[i]->is_valid_uri(h, rq, file_name, strlen(file_name),
+ &handle, &mtime) )
{
- response_set_status(rs, HTTP_STATUS_NOT_MODIFIED);
- dbg_err_if(response_print_header(rs));
- } else {
- dbg_err_if(b->sup_list[i]->serve(rq, rs));
-
- /* if the user explicitly set the status from a kl1 return 0 */
- if(response_get_status(rs) >= 400 && b->sup_list[i] != &sup_emb)
- return response_get_status(rs);
+ sup = b->sup_list[i];
+ break;
}
-
- return 0; /* page successfully served */
}
+ dbg_err_if(sup == NULL);
+ }
+
+ ims = request_get_if_modified_since(rq);
+ if(ims && ims >= mtime)
+ {
+ response_set_status(rs, HTTP_STATUS_NOT_MODIFIED);
+ dbg_err_if(response_print_header(rs));
+ } else {
+ dbg_err_if(sup->serve(rq, rs));
+
+ /* if the user explicitly set the status from a kl1 return 0 */
+ if(response_get_status(rs) >= 400 && sup != &sup_emb
+ #ifdef ENABLE_SUP_KILT
+ && sup!= &sup_kilt
+ #endif
+ )
+ return response_get_status(rs);
}
+ return 0; /* page successfully served */
+err:
response_set_status(rs, HTTP_STATUS_NOT_FOUND);
dbg("404, file not found: %s", request_get_filename(rq));
-err:
return HTTP_STATUS_NOT_FOUND; /* page not found */
}
@@ -100,6 +123,12 @@ int broker_create(broker_t **pb)
i = 0;
b->sup_list[i++] = &sup_emb;
+#ifdef ENABLE_SUP_KILT
+ b->sup_list[i++] = &sup_kilt;
+#else
+ warn("Kilt support disabled, use --enable_kilt to enable it");
+#endif
+
#ifdef ENABLE_SUP_CGI
b->sup_list[i++] = &sup_cgi;
#else
View
90 src/libhttp/dypage.c
@@ -0,0 +1,90 @@
+#include <klone/dypage.h>
+#include <klone/session.h>
+#include <klone/ses_prv.h>
+#include <klone/rsfilter.h>
+
+const char *dypage_get_param(dypage_args_t *args, const char *key)
+{
+ int i;
+
+ for(i = 0; i < args->nparams; ++i)
+ if(strcmp(key, args->params[i].key) == 0)
+ return args->params[i].val;
+
+ return "";
+}
+
+int dypage_serve(dypage_args_t *args)
+{
+ http_t *http = NULL;
+ codec_t *filter = NULL;
+ session_opt_t *so;
+ request_t *rq;
+ response_t *rs;
+ session_t *ss;
+ io_t *oio;
+
+ dbg_return_if(args == NULL, ~0);
+
+ dbg_return_if(args->rq == NULL, ~0);
+ dbg_return_if(args->rs == NULL, ~0);
+ dbg_return_if(args->fun == NULL, ~0);
+
+ /* alias */
+ rq = args->rq;
+ rs = args->rs;
+
+ /* output io object */
+ oio = response_io(rs);
+
+ /* get session options */
+ dbg_err_if((http = request_get_http(rq)) == NULL);
+ dbg_err_if((so = http_get_session_opt(http)) == NULL);
+
+ /* parse URL encoded or POSTed data (POST must be read before) */
+ dbg_err_if(request_parse_data(rq));
+
+ /* create/get the session */
+ dbg_err_if(session_create(so, rq, rs, &args->ss));
+
+ /* alias */
+ ss = args->ss;
+
+ /* set some default values */
+ dbg_err_if(response_set_content_type(rs, "text/html"));
+
+ /* by default disable caching */
+ response_disable_caching(rs);
+
+ /* create a response filter (used to automatically print all header fields
+ * when the header buffer fills up) and attach it to the response io */
+ dbg_err_if(response_filter_create(rq, rs, ss, &filter));
+ io_codec_add_tail(oio, filter);
+
+ /* run the page code */
+ args->fun(args);
+
+ /* flush the output buffer */
+ io_flush(oio);
+
+ /* if nothing has been printed by the script then write a dummy byte so
+ * the io_t calls the filter function that, in turn, will print out the
+ * HTTP header (rsfilter will handle it) */
+ if(!response_filter_feeded(filter))
+ io_write(oio, "\n", 1);
+
+ /* save and destroy the session */
+ session_free(ss); args->ss = ss = NULL;
+
+ return 0;
+err:
+ if(args->rs)
+ io_flush(response_io(args->rs));
+ if(args->ss)
+ {
+ session_free(args->ss);
+ args->ss = NULL;
+ }
+ return ~0;
+}
+
View
13 src/libhttp/http.c
@@ -5,7 +5,7 @@
* This file is part of KLone, and as such it is subject to the license stated
* in the LICENSE file which you have received as part of this distribution.
*
- * $Id: http.c,v 1.65 2008/10/13 16:21:04 tat Exp $
+ * $Id: http.c,v 1.66 2008/10/27 21:28:04 tat Exp $
*/
#include "klone_conf.h"
@@ -34,6 +34,7 @@
#include <klone/hookprv.h>
#include <klone/access.h>
#include <klone/vhost.h>
+#include <klone/supplier.h>
#include "http_s.h"
struct http_status_map_s
@@ -232,6 +233,11 @@ static int http_is_valid_uri(request_t *rq, const char *buf, size_t len)
strncpy(uri, buf, len);
uri[len] = 0;
+ /* try the url itself */
+ if(broker_is_valid_uri(h->broker, h, rq, uri, strlen(uri)))
+ return 1;
+
+ /* try the path-resolved url */
dbg_err_if(http_alias_resolv(h, rq, resolved, uri, U_FILENAME_MAX));
return broker_is_valid_uri(h->broker, h, rq, resolved, strlen(resolved));
@@ -468,6 +474,7 @@ static int http_serve(http_t *h, int fd)
struct sockaddr sa;
socklen_t sasz;
char *uri, nuri[URI_MAX];
+ supplier_t *sup;
u_unused_args(al);
@@ -574,7 +581,9 @@ static int http_serve(http_t *h, int fd)
dbg_err_if(http_resolv_request(h, rq));
/* if the uri end with a slash then return an index page */
- if((cstr = request_get_filename(rq)) != NULL && cstr[strlen(cstr)-1] == '/')
+ request_get_sup_info(rq, &sup, NULL, NULL);
+ if(sup == NULL && (cstr = request_get_filename(rq)) != NULL &&
+ cstr[strlen(cstr)-1] == '/')
dbg_if(http_set_index_request(h, rq)); /* set the index page */
/* add default header fields */
View
5 src/libhttp/sup_cgi.c
@@ -5,7 +5,7 @@
* This file is part of KLone, and as such it is subject to the license stated
* in the LICENSE file which you have received as part of this distribution.
*
- * $Id: sup_cgi.c,v 1.11 2008/06/04 17:48:02 tat Exp $
+ * $Id: sup_cgi.c,v 1.12 2008/10/27 21:28:04 tat Exp $
*/
#include "klone_conf.h"
@@ -175,7 +175,7 @@ static int cgi_setenv(cgi_env_t *env, const char *name, const char *value)
}
static int cgi_is_valid_uri(http_t *h, request_t *rq, const char *uri,
- size_t len, time_t *mtime)
+ size_t len, void **handle, time_t *mtime)
{
struct stat st;
char fqn[U_FILENAME_MAX];
@@ -198,6 +198,7 @@ static int cgi_is_valid_uri(http_t *h, request_t *rq, const char *uri,
return 0;
*mtime = st.st_mtime;
+ *handle = NULL;
return 1;
} else
return 0;
View
63 src/libhttp/sup_emb.c
@@ -5,7 +5,7 @@
* This file is part of KLone, and as such it is subject to the license stated
* in the LICENSE file which you have received as part of this distribution.
*
- * $Id: sup_emb.c,v 1.34 2008/10/18 13:04:02 tat Exp $
+ * $Id: sup_emb.c,v 1.35 2008/10/27 21:28:04 tat Exp $
*/
#include "klone_conf.h"
@@ -22,10 +22,11 @@
#include <klone/codecs.h>
#include <klone/ses_prv.h>
#include <klone/rsfilter.h>
+#include <klone/dypage.h>
#include "http_s.h"
static int supemb_is_valid_uri(http_t *h, request_t *rq, const char *uri,
- size_t len, time_t *mtime)
+ size_t len, void **handle, time_t *mtime)
{
embres_t *e;
char filename[U_FILENAME_MAX] = { 0 };
@@ -47,6 +48,8 @@ static int supemb_is_valid_uri(http_t *h, request_t *rq, const char *uri,
else
*mtime = 0; /* dynamic pages cannot be cached */
+ *handle = NULL;
+
return 1;
}
@@ -217,60 +220,18 @@ static int supemb_serve_static(request_t *rq, response_t *rs, embfile_t *e)
static int supemb_serve_dynamic(request_t *rq, response_t *rs, embpage_t *e)
{
- session_t *ss = NULL;
- http_t *http = NULL;
- codec_t *filter = NULL;
- session_opt_t *so;
- io_t *oio;
-
- dbg_return_if (rq == NULL, ~0);
- dbg_return_if (rs == NULL, ~0);
- dbg_return_if (e == NULL, ~0);
-
- /* output io object */
- oio = response_io(rs);
-
- /* get session options */
- dbg_err_if((http = request_get_http(rq)) == NULL);
- dbg_err_if((so = http_get_session_opt(http)) == NULL);
+ dypage_args_t args;
- /* parse URL encoded or POSTed data (POST must be read before) */
- dbg_err_if(request_parse_data(rq));
+ args.rq = rq;
+ args.rs = rs;
+ args.ss = NULL; /* dypage_serve will set it before calling args.fun() */
+ args.fun = e->fun;
+ args.opaque = NULL;
- /* create/get the session */
- dbg_err_if(session_create(so, rq, rs, &ss));
-
- /* set some default values */
- dbg_err_if(response_set_content_type(rs, "text/html"));
-
- /* by default disable caching */
- response_disable_caching(rs);
-
- /* create a response filter (used to automatically print all header fields
- * when the header buffer fills up) and attach it to the response io */
- dbg_err_if(response_filter_create(rq, rs, ss, &filter));
- io_codec_add_tail(oio, filter);
-
- /* run the page code */
- e->run(rq, rs, ss);
-
- /* flush the output buffer */
- io_flush(oio);
-
- /* if nothing has been printed by the script then write a dummy byte so
- * the io_t calls the filter function that, in turn, will print out the
- * HTTP header (rsfilter will handle it) */
- if(!response_filter_feeded(filter))
- io_write(oio, "\n", 1);
-
- /* save and destroy the session */
- session_free(ss);
+ dbg_err_if(dypage_serve(&args));
return 0;
err:
- io_flush(response_io(rs));
- if(ss)
- session_free(ss);
return ~0;
}
View
5 src/libhttp/sup_fs.c
@@ -5,7 +5,7 @@
* This file is part of KLone, and as such it is subject to the license stated
* in the LICENSE file which you have received as part of this distribution.
*
- * $Id: sup_fs.c,v 1.13 2008/04/25 18:59:08 tat Exp $
+ * $Id: sup_fs.c,v 1.14 2008/10/27 21:28:04 tat Exp $
*/
#include "klone_conf.h"
@@ -18,7 +18,7 @@
#include <klone/utils.h>
static int fs_is_valid_uri(http_t *h, request_t *rq, const char *uri,
- size_t len, time_t *mtime)
+ size_t len, void **handle, time_t *mtime)
{
struct stat st;
char fqn[U_FILENAME_MAX];
@@ -39,6 +39,7 @@ static int fs_is_valid_uri(http_t *h, request_t *rq, const char *uri,
if(stat(fqn, &st) == 0 && S_ISREG(st.st_mode))
{
*mtime = st.st_mtime;
+ *handle = NULL;
return 1;
} else
View
217 src/libhttp/sup_kilt.c
@@ -0,0 +1,217 @@
+/*
+ * Copyright (c) 2005, 2006 by KoanLogic s.r.l. <http://www.koanlogic.com>
+ * All rights reserved.
+ *
+ * This file is part of KLone, and as such it is subject to the license stated
+ * in the LICENSE file which you have received as part of this distribution.
+ *
+ * $Id: sup_kilt.c,v 1.1 2008/10/27 21:28:04 tat Exp $
+ */
+
+#include "klone_conf.h"
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <regex.h>
+#include <klone/supplier.h>
+#include <klone/io.h>
+#include <klone/utils.h>
+#include <klone/request.h>
+#include <klone/response.h>
+#include <klone/session.h>
+#include <klone/kilt.h>
+#include <klone/kilt_urls.h>
+#include <klone/emb.h>
+
+typedef struct url_to_ku_s
+{
+ regex_t re;
+ kilt_url_t *ku;
+} url_to_ku_t;
+
+static url_to_ku_t *url_to_ku = NULL;
+
+void kilt_run_script(dypage_args_t *args)
+{
+ embpage_t *e;
+ const char *script;
+
+ script = dypage_get_param(args, "script");
+ crit_err_ifm(script == NULL, "missing 'script' param");
+
+ crit_err_ifm(emb_lookup(script, (embres_t**)&e),
+ "script %s not found", script);
+
+ args->fun = e->fun;
+
+ /* run the page code */
+ e->fun(args);
+
+ return;
+err:
+ response_set_status(args->rs, HTTP_STATUS_NOT_FOUND);
+ return;
+}
+
+void kilt_show_params(dypage_args_t *args)
+{
+ io_t *out = response_io(args->rs);
+ int i;
+
+ io_printf(out, "argc: %u<br>", args->argc);
+ for(i = 0; i < args->argc; ++i)
+ io_printf(out, "argv[%d]: %s<br>", i, args->argv[i]);
+
+ io_printf(out, "<p>");
+
+ io_printf(out, "nparams: %u<br>", args->nparams);
+ for(i = 0; i < args->nparams; ++i)
+ io_printf(out, "param['%s']: %s<br>", args->params[i].key,
+ args->params[i].val);
+
+ return;
+}
+
+static int kilt_is_valid_uri(http_t *h, request_t *rq, const char *uri,
+ size_t len, void **handle, time_t *mtime)
+{
+ url_to_ku_t *utk;
+ char url[U_FILENAME_MAX];
+ int i;
+
+ u_unused_args(h, rq);
+
+ dbg_return_if (uri == NULL, 0);
+ dbg_return_if (mtime == NULL, 0);
+ dbg_return_if (len >= U_FILENAME_MAX, 0);
+ dbg_return_if (url_to_ku == NULL, 0);
+
+ memcpy(url, uri, len);
+ url[len] = 0;
+
+ for(i = 0; i < kilt_nurls; ++i)
+ {
+ utk = &url_to_ku[i];
+
+ if(regexec(&utk->re, url, 0, NULL, 0) == 0)
+ {
+ *handle = (void*)utk;
+ *mtime = 0;
+ return 1; /* found */
+ }
+ }
+
+ return 0;
+}
+
+static int kilt_serve(request_t *rq, response_t *rs)
+{
+ url_to_ku_t *utk;
+ dypage_args_t args;
+ regmatch_t subs[DYPAGE_MAX_PARAMS + 1];
+ const char *file_name;
+ char *argv[DYPAGE_MAX_PARAMS + 1];
+ int i, argc;
+ void *handle;
+
+ /* get cached utk pointer (avoid running all regex's on this url again) */
+ request_get_sup_info(rq, NULL, &handle, NULL);
+ dbg_err_if(handle == NULL);
+
+ utk = handle;
+
+ file_name = request_get_filename(rq);
+ dbg_err_if(file_name == NULL);
+
+ dbg_err_if(regexec(&utk->re, file_name, DYPAGE_MAX_PARAMS, subs, 0));
+
+ for(i = 0, argc = 0; subs[i].rm_so != subs[i].rm_eo; ++i, ++argc)
+ {
+ argv[i] = u_strndup(file_name + subs[i].rm_so,
+ subs[i].rm_eo - subs[i].rm_so);
+ dbg_err_if(argv[i] == NULL);
+ }
+
+ /* set dypage args first */
+ args.rq = rq;
+ args.rs = rs;
+ args.ss = NULL; /* set by dypage_serve */
+ args.fun = utk->ku->fun;
+ args.opaque = NULL;
+
+ /* kilt args */
+
+ /* regex submatches */
+ args.argc = argc;
+ args.argv = argv;
+
+ /* user provided named arguments */
+ args.params = utk->ku->params;
+ for(args.nparams = 0; args.params[args.nparams].key; ++args.nparams)
+ continue;
+
+ /* run the function! */
+ dypage_serve((dypage_args_t*)&args);
+
+ for(i = 0; i < argc; ++i)
+ u_free(argv[i]);
+
+ return 0;
+err:
+ return ~0;
+}
+
+static int kilt_init(void)
+{
+ kilt_url_t *u;
+ url_to_ku_t *utk;
+ int i;
+
+ url_to_ku = u_zalloc( (kilt_nurls + 1) * sizeof(url_to_ku_t));
+ dbg_err_if(url_to_ku == NULL);
+
+ for(u = kilt_urls, i = 0; i < kilt_nurls; ++u, ++i)
+ {
+ utk = &url_to_ku[i];
+
+ /* save the compiled regex in utk.re */
+ dbg_err_if(regcomp(&utk->re, u->pattern, REG_EXTENDED));
+
+ /* save the ptr to the kilt_url object */
+ utk->ku = u;
+
+ dbg_err_if(utk->re.re_nsub > DYPAGE_MAX_PARAMS);
+ }
+
+ return 0;
+err:
+ return ~0;
+}
+
+static void kilt_term(void)
+{
+ url_to_ku_t *utk;
+ int i;
+
+ if(url_to_ku)
+ {
+ for(i = 0; i < kilt_nurls; ++i)
+ {
+ utk = &url_to_ku[i];
+ regfree(&utk->re);
+ }
+ u_free(url_to_ku); url_to_ku = NULL;
+ }
+
+ return;
+}
+
+supplier_t sup_kilt = {
+ "kilt supplier",
+ kilt_init,
+ kilt_term,
+ kilt_is_valid_uri,
+ kilt_serve
+};
+
View
34 src/libpage/request.c
@@ -5,7 +5,7 @@
* This file is part of KLone, and as such it is subject to the license stated
* in the LICENSE file which you have received as part of this distribution.
*
- * $Id: request.c,v 1.61 2008/10/13 16:21:04 tat Exp $
+ * $Id: request.c,v 1.62 2008/10/27 21:28:04 tat Exp $
*/
#include "klone_conf.h"
@@ -24,6 +24,7 @@
#include <klone/vars.h>
#include <klone/timer.h>
#include <klone/vhost.h>
+#include <klone/supplier.h>
struct request_s
{
@@ -55,6 +56,10 @@ struct request_s
size_t post_maxsize; /* max # of POSTed bytes to accepts */
vhost_t *vhost; /* cached vhost pointer */
size_t padding;
+ /* cached supplier info data */
+ supplier_t *si_sup;
+ void *si_handle;
+ time_t si_mtime;
};
typedef struct upload_info_s /* uploaded file info struct */
@@ -1614,6 +1619,33 @@ int request_parse_data(request_t *rq)
return rc;
}
+/* cache data found in broker_is_valid_uri so we don't have to look for it
+ again in broker_serve */
+void request_set_sup_info(request_t *rq, supplier_t *sup, void *handle,
+ time_t mtime)
+{
+ rq->si_sup = sup;
+ rq->si_handle = handle;
+ rq->si_mtime = mtime;
+
+ return;
+}
+
+void request_get_sup_info(request_t *rq, supplier_t **psup, void **phandle,
+ time_t *pmtime)
+{
+ if(psup)
+ *psup = rq->si_sup;
+
+ if(phandle)
+ *phandle = rq->si_handle;
+
+ if(pmtime)
+ *pmtime = rq->si_mtime;
+
+ return;
+}
+
/*
* Parse request object \p rq.
*
View
13 src/libtrans/trans_c.c
@@ -5,7 +5,7 @@
* This file is part of KLone, and as such it is subject to the license stated
* in the LICENSE file which you have received as part of this distribution.
*
- * $Id: trans_c.c,v 1.40 2008/04/10 16:30:58 tat Exp $
+ * $Id: trans_c.c,v 1.41 2008/10/27 21:28:04 tat Exp $
*/
#include "klone_conf.h"
@@ -123,6 +123,7 @@ static void print_header(parser_t *p, lang_c_ctx_t *ctx)
io_printf(p->out, "%s", copyright_hdr);
io_printf(p->out, "#include <klone/emb.h>\n");
+ io_printf(p->out, "#include <klone/dypage.h>\n");
file = ctx->ti->uri + strlen(ctx->ti->uri) - 1;
@@ -221,11 +222,11 @@ static void print_code_blocks(parser_t *p, lang_c_ctx_t *ctx)
io_printf(p->out,
"\n\n"
- "static void exec_page(request_t *rq, response_t *rs, session_t *ses)\n"
+ "static void exec_page(dypage_args_t *args)\n"
"{\n"
- " request = rq; \n"
- " response = rs; \n"
- " session = ses; \n"
+ " request = args->rq; \n"
+ " response = args->rs; \n"
+ " session = args->ss; \n"
" in = request_io(request); \n"
" out = response_io(response); \n"
" u_unused_args(SCRIPT_NAME, request, response, session, in, out); \n"
@@ -288,7 +289,7 @@ static void print_dynamic_page_block(io_t *out, lang_c_ctx_t *ctx)
"{ \n"
" e.res.type = ET_PAGE; \n"
" e.res.filename = \"%s\"; \n"
- " e.run = exec_page; \n"
+ " e.fun = exec_page; \n"
"} \n",
ctx->ti->uri);
}

0 comments on commit 500cf84

Please sign in to comment.
Something went wrong with that request. Please try again.