db.c
changeset 13 23a242d7b7fa
parent 10 d07309450285
child 14 51eb85ae4de4
equal deleted inserted replaced
12:191b3c25974a 13:23a242d7b7fa
    69 		else {
    69 		else {
    70 			return( SQLITE_ERROR );
    70 			return( SQLITE_ERROR );
    71 		}
    71 		}
    72 	}
    72 	}
    73 
    73 
       
    74 	/* initialize prepared statements */
       
    75 	if ( prepare_statements() != 0 ) return SQLITE_ERROR;
       
    76 
    74 	return( SQLITE_OK );
    77 	return( SQLITE_OK );
    75 }
    78 }
    76 
    79 
    77 
    80 
    78 /*
    81 /*
   142 
   145 
   143 	sqlite3_finalize( stmt );
   146 	sqlite3_finalize( stmt );
   144 	return( version );
   147 	return( version );
   145 }
   148 }
   146 
   149 
       
   150 
       
   151 /*
       
   152  * Initialize the DB statements, returning 0 on success.
       
   153  *
       
   154  */
       
   155 unsigned short int
       
   156 prepare_statements( void )
       
   157 {
       
   158 	unsigned short int rv = 0;
       
   159 
       
   160 	rv = rv + sqlite3_prepare_v2( v.db, DBSQL_GET_REWRITE_RULE, -1, &v.db_stmt.get_rewrite_rule, NULL );
       
   161 	if ( rv != 0 )
       
   162 		debug( 2, LOC, "Error preparing DB statement \"%s\": %s\n",
       
   163 				DBSQL_GET_REWRITE_RULE, sqlite3_errmsg(v.db) );
       
   164 
       
   165 	rv = rv + sqlite3_prepare_v2( v.db, DBSQL_MATCH_REQUEST, -1, &v.db_stmt.match_request, NULL );
       
   166 	if ( rv != 0 )
       
   167 		debug( 2, LOC, "Error preparing DB statement \"%s\": %s\n",
       
   168 				DBSQL_MATCH_REQUEST, sqlite3_errmsg(v.db) );
       
   169 
       
   170 	return( rv );
       
   171 }
       
   172 
       
   173 
       
   174 /*
       
   175  * Initialize and return a pointer to a new rewrite object.
       
   176  *
       
   177  */
       
   178 rewrite *
       
   179 init_rewrite( void )
       
   180 {
       
   181 	rewrite *p_rewrite = NULL;
       
   182 	if ( (p_rewrite = malloc( sizeof(rewrite) )) == NULL ) {
       
   183 		debug( 5, LOC, "Unable to allocate memory for rewrite struct: %s\n", strerror(errno) );
       
   184 		return( NULL );
       
   185 	}
       
   186 
       
   187 	p_rewrite->scheme  = NULL;
       
   188 	p_rewrite->host    = NULL;
       
   189 	p_rewrite->path    = NULL;
       
   190 	p_rewrite->port    = 0;
       
   191 	p_rewrite->redir   = 0;
       
   192 
       
   193 	return( p_rewrite );
       
   194 }
       
   195 
       
   196 
       
   197 #define COPY_REWRITE_ROW( INDEX ) copy_string_token( \
       
   198 			(char *)sqlite3_column_text( v.db_stmt.get_rewrite_rule, INDEX ),\
       
   199 			sqlite3_column_bytes( v.db_stmt.get_rewrite_rule, INDEX ))
       
   200 /*
       
   201  * Given a request struct pointer, try and find the best matching
       
   202  * rewrite rule, returning a pointer to a rewrite struct.
       
   203  *
       
   204  */
       
   205 rewrite *
       
   206 prepare_rewrite( request *p_request )
       
   207 {
       
   208 	if ( p_request == NULL ) return( NULL );
       
   209 
       
   210 	unsigned short int rewrite_id = 0;
       
   211 	rewrite *p_rewrite = init_rewrite();
       
   212 
       
   213 	sqlite3_bind_text( v.db_stmt.match_request, 3, p_request->tld,       -1, SQLITE_STATIC );
       
   214 	sqlite3_bind_text( v.db_stmt.match_request, 1, p_request->scheme,    -1, SQLITE_STATIC );
       
   215 	sqlite3_bind_text( v.db_stmt.match_request, 2, p_request->host,      -1, SQLITE_STATIC );
       
   216 	sqlite3_bind_text( v.db_stmt.match_request, 3, p_request->tld,       -1, SQLITE_STATIC );
       
   217 	sqlite3_bind_text( v.db_stmt.match_request, 4, p_request->path,      -1, SQLITE_STATIC );
       
   218 	sqlite3_bind_int(  v.db_stmt.match_request, 5, p_request->port );
       
   219 	/*
       
   220 	sqlite3_bind_text( v.db_stmt.match_request, 6, NULL, -1, SQLITE_STATIC );
       
   221 	sqlite3_bind_text( v.db_stmt.match_request, 6, p_request->client_ip, -1, SQLITE_STATIC );
       
   222 	*/
       
   223 	sqlite3_bind_text( v.db_stmt.match_request, 7, p_request->user,      -1, SQLITE_STATIC );
       
   224 	sqlite3_bind_text( v.db_stmt.match_request, 8, p_request->method,    -1, SQLITE_STATIC );
       
   225 
       
   226 	switch ( sqlite3_step( v.db_stmt.match_request )) {
       
   227 		case SQLITE_ROW:
       
   228 			rewrite_id = sqlite3_column_int( v.db_stmt.match_request, 0 );
       
   229 			break;
       
   230 
       
   231 		case SQLITE_DONE:
       
   232 			break;
       
   233 
       
   234 		default:
       
   235 			return( NULL );
       
   236 	}
       
   237 
       
   238 	/* FIXME: CHECK for rewrite_rule being NULL on successful match, emit warning, continue */
       
   239 
       
   240 	/* return early if we didn't get a matching request */
       
   241 	if ( rewrite_id == 0 ) return( NULL );
       
   242 
       
   243 	/* pull the rewrite data, populate the struct.  only one
       
   244 	 * row should ever be returned for this. */
       
   245 	sqlite3_bind_int( v.db_stmt.get_rewrite_rule, 1, rewrite_id );
       
   246 	switch ( sqlite3_step( v.db_stmt.get_rewrite_rule )) {
       
   247 		case SQLITE_ROW:
       
   248 			p_rewrite->scheme = COPY_REWRITE_ROW( 1 );
       
   249 			p_rewrite->host   = COPY_REWRITE_ROW( 2 );
       
   250 			p_rewrite->path   = COPY_REWRITE_ROW( 3 );
       
   251 			p_rewrite->port   = sqlite3_column_int( v.db_stmt.get_rewrite_rule, 4 );
       
   252 			p_rewrite->redir  = sqlite3_column_int( v.db_stmt.get_rewrite_rule, 5 );
       
   253 			break;
       
   254 
       
   255 		case SQLITE_DONE:
       
   256 			break;
       
   257 
       
   258 		default:
       
   259 			return( NULL );
       
   260 	}
       
   261 
       
   262 	return( p_rewrite );
       
   263 }
       
   264 
       
   265 
       
   266 /*
       
   267  * Release memory used by the rewrite struct and
       
   268  * reset prepared statements.
       
   269  *
       
   270  */
       
   271 void
       
   272 finish_rewrite( rewrite *p_rewrite )
       
   273 {
       
   274 	sqlite3_reset( v.db_stmt.get_rewrite_rule );
       
   275 	sqlite3_reset( v.db_stmt.match_request );
       
   276 	sqlite3_clear_bindings( v.db_stmt.get_rewrite_rule );
       
   277 	sqlite3_clear_bindings( v.db_stmt.match_request );
       
   278 
       
   279 	if ( p_rewrite == NULL ) return;
       
   280 
       
   281 	free( p_rewrite->scheme );
       
   282 	free( p_rewrite->host );
       
   283 	free( p_rewrite->path );
       
   284 
       
   285 	free( p_rewrite ), p_rewrite = NULL;
       
   286 
       
   287 	return;
       
   288 }
       
   289