1 |
/* |
2 |
* Hydra, an http server |
3 |
* Copyright (C) 2002 Nikos Mavroyanopoulos <nmav@gnutls.org> |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
6 |
* it under the terms of the GNU General Public License as published by |
7 |
* the Free Software Foundation; either version 1, or (at your option) |
8 |
* any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 |
* |
19 |
*/ |
20 |
|
21 |
/* $Id: action_cgi.c,v 1.10 2003-02-18 18:35:27 nmav Exp $ */ |
22 |
|
23 |
/* This file includes support for dynamically loaded HIC modules |
24 |
* All modules added to module_table[] will be dlopen()ed at startup. |
25 |
* |
26 |
* Also hic symbols will be resolved. |
27 |
*/ |
28 |
|
29 |
#include "boa.h" |
30 |
|
31 |
|
32 |
static action_module_st* module_hashtable[MODULE_HASHTABLE_SIZE]; |
33 |
|
34 |
|
35 |
/* add_cgi_action |
36 |
* |
37 |
* Like add_hic_module() but associates the file type with a |
38 |
* specific action (executable to run with) |
39 |
*/ |
40 |
|
41 |
void add_cgi_action(const char *action, const char* file_type) |
42 |
{ |
43 |
int hash; |
44 |
action_module_st* old, *start; |
45 |
|
46 |
/* sanity checking */ |
47 |
if (action == NULL || file_type == NULL) { |
48 |
DIE("NULL values sent to add_cgi_action"); |
49 |
} |
50 |
|
51 |
hash = get_cgi_module_hash_value( file_type); |
52 |
start = old = module_hashtable[hash]; |
53 |
|
54 |
if ( old != NULL) { |
55 |
/* find next empty */ |
56 |
do { |
57 |
hash = (hash + 1) % MODULE_HASHTABLE_SIZE; |
58 |
|
59 |
old = module_hashtable[hash]; |
60 |
|
61 |
if (start == old) { |
62 |
DIE("Module hashtable is full."); |
63 |
} |
64 |
|
65 |
} while( old != NULL); |
66 |
} |
67 |
|
68 |
/* old was found, and is empty. */ |
69 |
|
70 |
old = malloc( sizeof(action_module_st)); |
71 |
if (old==NULL) { |
72 |
DIE("malloc() failed."); |
73 |
} |
74 |
|
75 |
old->sym_prefix = NULL; |
76 |
|
77 |
old->content_type = strdup( file_type); |
78 |
if (old->content_type == NULL) { |
79 |
DIE("strdup() failed."); |
80 |
} |
81 |
|
82 |
old->content_type_len = strlen( file_type); |
83 |
|
84 |
old->action = strdup( action); |
85 |
if ( old->action == NULL) { |
86 |
DIE("strdup() failed."); |
87 |
} |
88 |
|
89 |
module_hashtable[hash] = old; |
90 |
|
91 |
return; |
92 |
} |
93 |
|
94 |
/* |
95 |
* Name: find_cgi_action_appr_module |
96 |
* |
97 |
* Description: Locates the appropriate HIC module for the given file. |
98 |
* Actually ones needs this to get the dlsymed() functions. |
99 |
* |
100 |
* Returns: |
101 |
* |
102 |
* a pointer to a hic_module_st structure or NULL if not found |
103 |
*/ |
104 |
|
105 |
action_module_st *find_cgi_action_appr_module(const char *content_type, int content_type_len) |
106 |
{ |
107 |
int i, hash; |
108 |
|
109 |
if (content_type == NULL) return NULL; |
110 |
if (content_type_len == 0) content_type_len = strlen( content_type); |
111 |
|
112 |
hash = get_cgi_module_hash_value( content_type); |
113 |
for (i=hash;i<MODULE_HASHTABLE_SIZE;i++) { |
114 |
if (module_hashtable[i] == NULL) break; |
115 |
|
116 |
if ( content_type_len != module_hashtable[i]->content_type_len) continue; |
117 |
|
118 |
if (memcmp( content_type, module_hashtable[i]->content_type, |
119 |
content_type_len) == 0) { |
120 |
/* FOUND! */ |
121 |
return module_hashtable[i]; |
122 |
} |
123 |
} |
124 |
|
125 |
return NULL; |
126 |
|
127 |
} |
128 |
|
129 |
|
130 |
/* |
131 |
* Empties the hic modules table, deallocating any allocated memory. |
132 |
*/ |
133 |
|
134 |
void dump_cgi_action_modules(void) |
135 |
{ |
136 |
int i; |
137 |
|
138 |
for (i = 0; i < MODULE_HASHTABLE_SIZE; ++i) { /* these limits OK? */ |
139 |
if (!module_hashtable[i]) continue; |
140 |
|
141 |
free( module_hashtable[i]->action); |
142 |
|
143 |
free( module_hashtable[i]); |
144 |
module_hashtable[i] = NULL; |
145 |
} |
146 |
} |