[Mod-auth-commit] mod_vhost_dbi/src mod_vhost_dbi.c,1.1,1.2
Brought to you by:
firechipmunk,
honx
From: Paul Q. <fir...@us...> - 2004-05-10 06:51:35
|
Update of /cvsroot/mod-auth/mod_vhost_dbi/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7595 Modified Files: mod_vhost_dbi.c Log Message: begin porting to mod_dbi_pool support Index: mod_vhost_dbi.c =================================================================== RCS file: /cvsroot/mod-auth/mod_vhost_dbi/src/mod_vhost_dbi.c,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- mod_vhost_dbi.c 4 Mar 2004 08:12:13 -0000 1.1 +++ mod_vhost_dbi.c 10 May 2004 06:51:27 -0000 1.2 @@ -29,10 +29,15 @@ #include "http_protocol.h" #include "http_request.h" +// DBI Pooling Module +#include "mod_dbi_pool.h" + +// libdbi Headers #include <dbi/dbi.h> // include php support. #define HAVE_PHP_SUPPORT +// TODO: use configure.in for this #ifdef HAVE_PHP_SUPPORT #include <stdlib.h> @@ -45,7 +50,9 @@ #endif typedef struct { - + int disabled; + conn_id *id; + char *query; } vhost_dbi_config_rec; static void *vhost_dbi_create_server_config(apr_pool_t * p, server_rec * s) @@ -53,54 +60,183 @@ vhost_dbi_config_rec *vdbir = (vhost_dbi_config_rec *) apr_pcalloc(p, sizeof(vhost_dbi_config_rec)); + vdbir->disabled = 0; + vdbir->id = NULL; + return vdbir; } -static void *vhost_dbi_merge_server_config(apr_pool_t * p, void *parentv, - void *childv) +#define HOSTNAME_MAGIC_CHAR '.' + +/** + * "foo.home.com" -> "'foo.home.com','*.home.com','*.com','*'" + * + */ +const char *expand_hostname(request_rec * r, dbi_conn * dbi, + const char *hostname) { - vhost_dbi_config_rec *parent = (vhost_dbi_config_rec *) parentv; - vhost_dbi_config_rec *child = (vhost_dbi_config_rec *) childv; - vhost_dbi_config_rec *conf = - (vhost_dbi_config_rec *) apr_pcalloc(p, sizeof(vhost_dbi_config_rec)); + char tmp[MAX_STRING_LEN]; + const char *s, *e; + char *p; - if (child->fl == 1) { - conf->fl = child->fl; + tmp[0] = '\0'; + int written = 0; - conf->dbi_database = - (child->dbi_database ? child->dbi_database : parent->dbi_database); - conf->dbi_info_table = - (child->dbi_info_table ? child->dbi_info_table : parent-> - dbi_info_table); - conf->dbi_alias_table = - (child->dbi_alias_table ? child->dbi_alias_table : parent-> - dbi_alias_table); - conf->dbi_host_field = - (child->dbi_host_field ? child->dbi_host_field : parent-> - dbi_host_field); - conf->dbi_path_field = - (child->dbi_path_field ? child->dbi_path_field : parent-> - dbi_path_field); - conf->dbi_host = - (child->dbi_host ? child->dbi_host : parent->dbi_host); - conf->dbi_port = - (child->dbi_port ? child->dbi_port : parent->dbi_port); - conf->dbi_username = - (child->dbi_username ? child->dbi_username : parent->dbi_username); - conf->dbi_password = - (child->dbi_password ? child->dbi_password : parent->dbi_password); + if (!(s = ap_strchr_c(hostname, HOSTNAME_MAGIC_CHAR))) { + p = strdup(hostname); + dbi_driver_quote_string(dbi_conn_get_driver(dbi), &p); + strcat(tmp, p); + free(p); + return apr_pstrdup(r->pool, tmp); + } - conf->default_host = - (child->default_host ? child->default_host : parent->default_host); - conf->dbi_driver = - (child->dbi_driver ? child->dbi_driver : parent->dbi_driver); + ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, r->pool, + "[mod_vhost_dbi] Hostname to expand: %s", hostname); - } + do { + written += (s - hostname); + // MAX_STRING_LEN-1 so we still have room for a null at the end + if (written >= MAX_STRING_LEN - 1) { + ap_log_perror(APLOG_MARK, APLOG_ERR, 0, r->pool, + "[mod_vhost_dbi] Populated Hostname String would exceed %d bytes", + MAX_STRING_LEN); + return NULL; + } + strncat(tmp, hostname, s - hostname); + + } while ((s = ap_strchr_c(hostname, HOSTNAME_MAGIC_CHAR))); + + strcat(tmp, querystring); + written += strlen(querystring); + ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, r->pool, + "[mod_authn_dbi.c] Populated result: \"%s\" / %d chars written", + apr_pstrdup(r->pool, tmp), written); + + return apr_pstrdup(r->pool, tmp); +} + +#define QUERYSTRING_MAGIC_CHAR '&' +#define QUERYSTRING_LEFT_DELIM_CHAR '{' +#define QUERYSTRING_RIGHT_DELIM_CHAR '}' + +#define EMPTY_VAR "" /* do NOT set this to NULL! */ + +const char *populate_querystring(request_rec * r, dbi_conn * dbi, + const char *querystring) +{ + char tmp[MAX_STRING_LEN]; /* 8 KByte should be enough for everyone :) */ + const char *s, *e; + char *p; + + tmp[0] = '\0'; + int written = 0; + + if (!(s = ap_strchr_c(querystring, QUERYSTRING_MAGIC_CHAR))) + return querystring; + + ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, r->pool, + "[mod_vhost_dbi] Querystring to populate: %s", querystring); + do { + written += (s - querystring); + // MAX_STRING_LEN-1 so we still have room for a null at the end + if (written >= MAX_STRING_LEN - 1) { + ap_log_perror(APLOG_MARK, APLOG_ERR, 0, r->pool, + "[mod_vhost_dbi] Populated string would exceed %d bytes", + MAX_STRING_LEN); + return NULL; + } + strncat(tmp, querystring, s - querystring); + + if ((s[1] == QUERYSTRING_LEFT_DELIM_CHAR) + && (e = ap_strchr_c(s, QUERYSTRING_RIGHT_DELIM_CHAR))) { + const char *e2 = e; + char *var; + + p = NULL; + querystring = e + 1; + e = NULL; + var = apr_pstrndup(r->pool, s + 2, e2 - (s + 2)); + + if (!strcasecmp(var, "RequestHostname")) { + e = (r->hostname ? r->hostname : EMPTY_VAR); + } + else if (!strcasecmp(var, "RemoteIP")) { + e = (r->connection->remote_ip ? r->connection-> + remote_ip : EMPTY_VAR); + } + + /* + * Everything but the variable values representing fieldnames and tables gets + * * escaped according to the selected driver + */ + + if (e != NULL) { + p = strdup(e); + dbi_driver_quote_string(dbi_conn_get_driver(dbi_res->conn), + &p); + } + + if (!strcasecmp(var, "ExpandHostname")) { + e = (r->hostname ? expand_hostname(r, dbi, + r->hostname) : EMPTY_VAR); + } + + if (e == NULL) { + ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, r->pool, + "[mod_vhost_dbi] Unknown variable: %s", var); + return NULL; + } + + if (p == NULL) { + p = strdup(e); + } + written += strlen(p); + if (written >= MAX_STRING_LEN - 1) { + ap_log_perror(APLOG_MARK, APLOG_ERR, 0, r->pool, + "[mod_vhost_dbi] Populated string would exceed %d bytes", + MAX_STRING_LEN); + free(p); + return NULL; + } + strcat(tmp, p); + free(p); + + } + else { + ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, r->pool, + "[mod_vhost_dbi] Invalid querystring"); + return NULL; + + }; + + } while ((s = ap_strchr_c(querystring, QUERYSTRING_MAGIC_CHAR))); + + strcat(tmp, querystring); + written += strlen(querystring); + ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, r->pool, + "[mod_authn_dbi.c] Populated result: \"%s\" / %d chars written", + apr_pstrdup(r->pool, tmp), written); + + return apr_pstrdup(r->pool, tmp); +} + +static const char *set_switch(cmd_parms * cmd, void *config, const char *value) +{ + apr_ssize_t pos = (apr_ssize_t) cmd->info; - return conf; } static const command_rec vhost_dbi_commands[] = { + AP_INIT_TAKE1("VhostDbiConnName", + set_switch, + (void *)0, + RSRC_CONF, + "name of the dbi_pool connection to use"), + AP_INIT_TAKE1("VhostDbiQuery", + set_switch, + (void *)0, + RSRC_CONF, + "name of the dbi_pool connection to use"), AP_INIT_FLAG("VhostDbiEnabled", set_switch, (void *)1, @@ -111,7 +247,7 @@ -static char *get_path(request_rec * r, const char *host, char **env, +static char *find_path(request_rec * r, const char *host, char **env, vhost_dbi_config_rec * vdbir) { dbi_result *res; @@ -122,14 +258,11 @@ int dbi_err; const char *dbi_err_str; - - if (dbi == NULL) { - // XXXXX: use mod_dbi_pool - } + dbi = dbipool_open(vdbir->conn_name); if (dbi == NULL) { ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r->server, - "vhost_dbi: get_path: DBI connection was not acquired!"); + "vhost_dbi: find_path: DBI connection was not acquired!"); return NULL; } @@ -137,14 +270,17 @@ strcpy(rstr, host); dbi_driver_quote_string(dbi_conn_get_driver(dbi), &rstr); - // XXXXX: Custom SQL Query Here + + // TODO: Custom SQL Query Here qstr = apr_psprintf(r->pool, "SELECT vdbi_info.root_path, vdbi_info.php_path " "FROM vdbi_info, vdbi_alias " "WHERE vdbi_info.idx = vdbi_alias.owner_idx AND " "vdbi_alias.hostname=%s LIMIT 0,1", rstr); + res = dbi_conn_query(dbi, qstr); + if (res == NULL) { dbi_err = dbi_conn_error(dbi, &dbi_err_str); ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r->server, @@ -152,8 +288,7 @@ dbi_err_str, qstr); free(rstr); dbi_result_free(res); - dbi_conn_close(dbi); - dbi_shutdown(); + dbipool_close(vdbir->conn_name, dbi); return NULL; } @@ -165,8 +300,7 @@ "vhost_dbi: get_path: no results for %s", host); free(rstr); dbi_result_free(res); - dbi_conn_close(dbi); - dbi_shutdown(); + dbipool_close(vdbir->conn_name, dbi); return NULL; default: ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r->server, @@ -174,8 +308,7 @@ host); free(rstr); dbi_result_free(res); - dbi_conn_close(dbi); - dbi_shutdown(); + dbipool_close(vdbir->conn_name, dbi); return NULL; } @@ -186,8 +319,7 @@ dbi_err, dbi_err_str, qstr); free(rstr); dbi_result_free(res); - dbi_conn_close(dbi); - dbi_shutdown(); + dbipool_close(vdbir->conn_name, dbi); return NULL; } @@ -197,8 +329,8 @@ free(rstr); dbi_result_free(res); - dbi_conn_close(dbi); - dbi_shutdown(); + dbipool_close(vdbir->conn_name, dbi); + ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "get_path: %s from SQL: %s", path, qstr); @@ -208,6 +340,7 @@ static int vhost_dbi_init_handler(apr_pool_t * pconf, apr_pool_t * plog, apr_pool_t * ptemp, server_rec * s) { + ap_add_version_component(pconf, "mod_vhost_dbi/0.1.0"); return OK; @@ -224,7 +357,7 @@ char *ptr; int i; - if (!(vdbir->fl == 1)) { + if (!(vdbir->disabled == 1)) { ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "vhost_dbi_translate_name: declined http://%s%s module is not configured for this server", apr_table_get(r->headers_in, "Host"), r->uri); @@ -246,7 +379,9 @@ if (ptr = strchr(host, ':')) { *ptr = '\0'; } - path = get_path(r, host, &env, vdbir); + + // TODO: Cache this Result + path = find_path(r, host, &env, vdbir); if (!path) { ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, r->server, |