Changeset 480

Show
Ignore:
Timestamp:
07/28/2005 08:55:55 PM (3 years ago)
Author:
jan
Message:

added support for host and port

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/lighttpd-1.3.x/src/mod_mysql_vhost.c

    r1 r480  
    2929 */ 
    3030 
     31#ifdef HAVE_MYSQL 
    3132typedef struct { 
    32 #ifdef HAVE_MYSQL 
    3333        MYSQL   *mysql; 
    34 #endif 
     34         
    3535        buffer  *mydb; 
    3636        buffer  *myuser; 
    3737        buffer  *mypass; 
    3838        buffer  *mysock; 
     39         
     40        buffer  *hostname; 
     41        unsigned short port; 
    3942         
    4043        buffer  *mysql_pre; 
     
    8891                for (i = 0; i < srv->config_context->used; i++) { 
    8992                        plugin_config *s = p->config_storage[i]; 
    90 #ifdef HAVE_MYSQL 
     93                         
    9194                        mysql_close(s->mysql); 
    92 #endif 
     95                         
    9396                        buffer_free(s->mydb); 
    9497                        buffer_free(s->myuser); 
     
    166169 
    167170        config_values_t cv[] = { 
    168                 { "mysql-vhost.db",     NULL, T_CONFIG_STRING,  T_CONFIG_SCOPE_SERVER },         
     171                { "mysql-vhost.db",     NULL, T_CONFIG_STRING,  T_CONFIG_SCOPE_SERVER }, 
    169172                { "mysql-vhost.user",   NULL, T_CONFIG_STRING,  T_CONFIG_SCOPE_SERVER }, 
    170                 { "mysql-vhost.pass",   NULL, T_CONFIG_STRING,  T_CONFIG_SCOPE_SERVER },         
    171                 { "mysql-vhost.sock",   NULL, T_CONFIG_STRING,  T_CONFIG_SCOPE_SERVER },         
    172                 { "mysql-vhost.sql",    NULL, T_CONFIG_STRING,  T_CONFIG_SCOPE_SERVER },         
     173                { "mysql-vhost.pass",   NULL, T_CONFIG_STRING,  T_CONFIG_SCOPE_SERVER }, 
     174                { "mysql-vhost.sock",   NULL, T_CONFIG_STRING,  T_CONFIG_SCOPE_SERVER }, 
     175                { "mysql-vhost.sql",    NULL, T_CONFIG_STRING,  T_CONFIG_SCOPE_SERVER }, 
     176                { "mysql-vhost.hostname", NULL, T_CONFIG_STRING,T_CONFIG_SCOPE_SERVER }, 
     177                { "mysql-vhost.port",   NULL, T_CONFIG_SHORT,   T_CONFIG_SCOPE_SERVER }, 
    173178                { NULL,                 NULL, T_CONFIG_UNSET,   T_CONFIG_SCOPE_UNSET } 
    174179        }; 
     
    186191                s->mypass = buffer_init(); 
    187192                s->mysock = buffer_init(); 
     193                s->hostname = buffer_init(); 
     194                s->port   = 0;               /* default port for mysql */ 
    188195                sel = buffer_init(); 
    189 #ifdef HAVE_MYSQL 
    190196                s->mysql = NULL; 
    191 #endif 
    192197                 
    193198                s->mysql_pre = buffer_init(); 
     
    199204                cv[3].destination = s->mysock; 
    200205                cv[4].destination = sel; 
     206                cv[5].destination = s->hostname; 
     207                cv[6].destination = &(s->port); 
    201208                 
    202209                p->config_storage[i] = s; 
     
    217224                } 
    218225                 
     226                /* required: 
     227                 * - username 
     228                 * - database  
     229                 *  
     230                 * optional: 
     231                 * - password, default: empty 
     232                 * - socket, default: mysql default 
     233                 * - hostname, if set overrides socket 
     234                 * - port, default: 3306 
     235                 */ 
     236                 
    219237                /* all have to be set */ 
    220238                if (!(buffer_is_empty(s->myuser) || 
    221                       buffer_is_empty(s->mypass) || 
    222                       buffer_is_empty(s->mydb) || 
    223                       buffer_is_empty(s->mysock))) { 
    224 #ifdef HAVE_MYSQL 
     239                      buffer_is_empty(s->mydb))) { 
     240 
    225241                        int fd; 
    226242                 
     
    230246                                return HANDLER_ERROR; 
    231247                        } 
     248#define FOO(x) (s->x->used ? s->x->ptr : NULL) 
    232249                         
    233                         if (!mysql_real_connect(s->mysql, NULL, s->myuser->ptr, s->mypass->ptr,  
    234                                                 s->mydb->ptr, 0, s->mysock->ptr, 0)) { 
     250                        if (!mysql_real_connect(s->mysql, FOO(hostname), FOO(myuser), FOO(mypass),  
     251                                                FOO(mydb), s->port, FOO(mysock), 0)) { 
    235252                                log_error_write(srv, __FILE__, __LINE__, "s", mysql_error(s->mysql)); 
    236253                                 
    237254                                return HANDLER_ERROR; 
    238255                        } 
    239                  
     256#undef FOO 
    240257                        /* set close_on_exec for mysql the hard way */ 
    241258                        /* Note: this only works as it is done during startup, */ 
     
    245262                                fcntl(fd-1, F_SETFD, FD_CLOEXEC);  
    246263                        } 
    247 #endif 
    248264                } 
    249265        } 
     
    280296                } 
    281297                 
    282 #ifdef HAVE_MYSQL 
    283298                if (s->mysql) { 
    284299                        PATCH(mysql); 
    285300                } 
    286 #endif 
    287301        } 
    288302         
     
    298312        PATCH(mysql_pre); 
    299313        PATCH(mysql_post); 
    300 #ifdef HAVE_MYSQL 
    301314        PATCH(mysql); 
    302 #endif 
    303315         
    304316        return 0; 
     
    309321/* handle document root request */ 
    310322CONNECTION_FUNC(mod_mysql_vhost_handle_docroot) { 
    311 #ifdef HAVE_MYSQL 
    312323        plugin_data *p = p_d; 
    313324        plugin_connection_data *c; 
     
    400411        con->http_status = 500; /* Internal Error */ 
    401412        return HANDLER_FINISHED; 
    402 #else 
    403         UNUSED(srv); 
    404         UNUSED(con); 
    405         UNUSED(p_d); 
    406          
    407         return HANDLER_ERROR; 
    408 #endif 
    409413} 
    410414 
     
    423427        return 0; 
    424428} 
    425  
     429#else 
     430/* we don't have mysql support, this plugin does nothing */ 
     431int mod_mysql_vhost_plugin_init(plugin *p) { 
     432        p->version     = LIGHTTPD_VERSION_ID; 
     433        p->name                         = buffer_init_string("mysql_vhost"); 
     434 
     435        return 0; 
     436} 
     437#endif