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: index.c,v 1.2 2002/09/26 00:05:43 nmav Exp $ */ |
22 |
|
23 |
#include "boa.h" |
24 |
|
25 |
typedef struct { |
26 |
char* file; |
27 |
int file_size; |
28 |
} dir_index_st; |
29 |
|
30 |
static dir_index_st* directory_index_table[DIRECTORY_INDEX_TABLE_SIZE]; |
31 |
|
32 |
/* |
33 |
* Name: add_directory_index |
34 |
* |
35 |
* Description: add an index file to the directory index files table. |
36 |
*/ |
37 |
|
38 |
void add_directory_index(const char *index_file) |
39 |
{ |
40 |
dir_index_st *new; |
41 |
int index_file_len; |
42 |
int i; |
43 |
|
44 |
/* sanity checking */ |
45 |
if (index_file == NULL) { |
46 |
DIE("NULL values sent to add_directory_index"); |
47 |
} |
48 |
|
49 |
index_file_len = strlen( index_file); |
50 |
|
51 |
if (index_file_len == 0) { |
52 |
DIE("empty values sent to add_directory_index"); |
53 |
} |
54 |
|
55 |
for (i=0;i<DIRECTORY_INDEX_TABLE_SIZE;i++) { |
56 |
|
57 |
new = directory_index_table[ i]; |
58 |
if (new) { |
59 |
if (!strcmp( index_file, new->file)) /* don't add twice */ |
60 |
return; |
61 |
} else break; /* found an empty position */ |
62 |
} |
63 |
|
64 |
if (new) { |
65 |
DIE("Directory index table is full. Increase DIRECTORY_INDEX_TABLE_SIZE"); |
66 |
} |
67 |
|
68 |
new = malloc( sizeof( dir_index_st)); |
69 |
if (!new) { |
70 |
DIE("out of memory adding directory index"); |
71 |
} |
72 |
|
73 |
new->file = strdup( index_file); |
74 |
if (!new) { |
75 |
DIE("failed strdup"); |
76 |
} |
77 |
|
78 |
new->file_size = index_file_len; |
79 |
|
80 |
directory_index_table[i] = new; |
81 |
|
82 |
} |
83 |
|
84 |
/* |
85 |
* Name: find_and_open_directory_index |
86 |
* |
87 |
* Description: Locates one index file in the directory given. |
88 |
* Also opens the file and returns the data_fd. |
89 |
* |
90 |
* Returns: |
91 |
* |
92 |
* a pointer to the index file or NULL if not found |
93 |
*/ |
94 |
|
95 |
char *find_and_open_directory_index(const char *directory, int directory_len, int* data_fd) |
96 |
{ |
97 |
char pathname_with_index[MAX_PATH_LENGTH + 1]; |
98 |
int total_size, i; |
99 |
|
100 |
*data_fd = -1; |
101 |
|
102 |
if (directory_len == 0) directory_len = strlen( directory); |
103 |
if (directory_len > MAX_PATH_LENGTH) return NULL; |
104 |
|
105 |
memcpy( pathname_with_index, directory, directory_len); |
106 |
|
107 |
|
108 |
for (i=0;i<DIRECTORY_INDEX_TABLE_SIZE;i++) { |
109 |
if ( !directory_index_table[i]) break; |
110 |
|
111 |
total_size = directory_index_table[i]->file_size + directory_len; |
112 |
if ( total_size > MAX_PATH_LENGTH) continue; |
113 |
|
114 |
memcpy( &pathname_with_index[directory_len], directory_index_table[i]->file, |
115 |
directory_index_table[i]->file_size); |
116 |
|
117 |
pathname_with_index[total_size] = 0; |
118 |
|
119 |
*data_fd = open(pathname_with_index, O_RDONLY); |
120 |
|
121 |
/* If we couldn't access the file, then return the |
122 |
* filename as usual, and a data_fd (-1), with the |
123 |
* proper errno. |
124 |
*/ |
125 |
if (*data_fd == -1 && errno != EACCES) continue; |
126 |
|
127 |
/* data_fd > 0 -- found index! */ |
128 |
return directory_index_table[i]->file; |
129 |
} |
130 |
|
131 |
return NULL; |
132 |
|
133 |
} |
134 |
|
135 |
/* |
136 |
* Name: find_default_directory_index |
137 |
* |
138 |
* Description: Returns the first directory index file, in the list |
139 |
* |
140 |
* Returns: |
141 |
* |
142 |
* a pointer to the index file or NULL if not found |
143 |
*/ |
144 |
|
145 |
char *find_default_directory_index() |
146 |
{ |
147 |
if (directory_index_table[0] == NULL) return NULL; |
148 |
return directory_index_table[0]->file; |
149 |
} |
150 |
|
151 |
|
152 |
/* |
153 |
* Empties the virthost hashtable, deallocating any allocated memory. |
154 |
*/ |
155 |
|
156 |
void dump_directory_index(void) |
157 |
{ |
158 |
int i; |
159 |
|
160 |
for (i = 0; i < DIRECTORY_INDEX_TABLE_SIZE; ++i) { /* these limits OK? */ |
161 |
if (directory_index_table[i]) { |
162 |
free( directory_index_table[i]->file); |
163 |
free( directory_index_table[i]); |
164 |
directory_index_table[i] = NULL; |
165 |
} |
166 |
} |
167 |
} |