/[hydra]/hydra/src/virthost.c
ViewVC logotype

Annotation of /hydra/src/virthost.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Tue Sep 24 17:12:47 2002 UTC (21 years, 6 months ago) by nmav
Branch: MAIN
File MIME type: text/plain
Added some support for host based virtual hosting.

1 nmav 1.1 /*
2     * Boa, an http server
3     * Copyright (C) 1995 Paul Phillips <paulp@go2net.com>
4     * Some changes Copyright (C) 1996 Larry Doolittle <ldoolitt@boa.org>
5     * Some changes Copyright (C) 1996 Russ Nelson <nelson@crynwr.com>
6     * Some changes Copyright (C) 1996-2002 Jon Nelson <jnelson@boa.org>
7     * Portions Copyright (C) 2002 Nikos Mavroyanopoulos <nmav@gnutls.org>
8     *
9     * This program is free software; you can redistribute it and/or modify
10     * it under the terms of the GNU General Public License as published by
11     * the Free Software Foundation; either version 1, or (at your option)
12     * any later version.
13     *
14     * This program is distributed in the hope that it will be useful,
15     * but WITHOUT ANY WARRANTY; without even the implied warranty of
16     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     * GNU General Public License for more details.
18     *
19     * You should have received a copy of the GNU General Public License
20     * along with this program; if not, write to the Free Software
21     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22     *
23     */
24    
25     /* $Id: virthost.c,v 1.2 2002/09/22 09:07:57 nmav Exp $ */
26    
27     #include "boa.h"
28    
29     static virthost *virthost_hashtable[ALIAS_HASHTABLE_SIZE];
30    
31     /*
32     * Name: get_host_hash_value
33     *
34     * Here we simply use the algorithm used in get_alias_hash_value();
35     */
36    
37     int get_host_hash_value(const char *file)
38     {
39     unsigned int hash = 0;
40     unsigned int index = 0;
41     unsigned char c;
42    
43     hash = file[index++];
44     while ((c = file[index++]) && c != '/')
45     hash += (unsigned int) c;
46    
47     return hash % VIRTHOST_HASHTABLE_SIZE;
48     }
49    
50     /*
51     * Name: add_virthost
52     *
53     * Description: add a virtual host to the virthost hash table.
54     */
55    
56     void add_virthost(const char *host, const char *ip, const char* document_root,
57     const char* user_dir)
58     {
59     int hash;
60     virthost *old, *new;
61     int hostlen, iplen, document_root_len, user_dir_len = 0;
62    
63     /* sanity checking */
64     if (host == NULL || ip == NULL || document_root == NULL) {
65     DIE("NULL values sent to add_virthost");
66     }
67    
68     iplen = strlen(ip);
69    
70     if (iplen > NI_MAXHOST) {
71     DIE("IP in virthost is tooooo long");
72     }
73     hostlen = strlen(host);
74     document_root_len = strlen( document_root);
75     if (user_dir) user_dir_len = strlen( user_dir);
76    
77     if (iplen == 0 || hostlen == 0 || document_root_len == 0) {
78     DIE("empty values sent to add_virthost");
79     }
80    
81     hash = get_host_hash_value( host);
82    
83     old = virthost_hashtable[hash];
84    
85     if (old) {
86     while (old->next) {
87     if (!strcmp( host, old->host)) /* don't add twice */
88     return;
89     old = old->next;
90     }
91     }
92    
93     new = (virthost *) malloc(sizeof (virthost));
94     if (!new) {
95     DIE("out of memory adding virthost to hash");
96     }
97    
98     if (old)
99     old->next = new;
100     else
101     virthost_hashtable[hash] = new;
102    
103     new->host = strdup( host);
104     if (!new->host) {
105     DIE("failed strdup");
106     }
107     new->host_len = hostlen;
108    
109     if (user_dir && user_dir_len > 0) {
110     new->user_dir = strdup(user_dir);
111     new->user_dir_len = user_dir_len;
112     } else {
113     new->user_dir = NULL;
114     new->user_dir_len = 0;
115     }
116    
117     if (iplen == 0 || !strchr( ip, '*')) { /* if the IP part is '*' then
118     * we don't bind this virthost to a
119     * specific ip */
120     new->ip = strdup( ip);
121     if (!new->ip) {
122     DIE("failed strdup");
123     }
124     new->ip_len = iplen;
125     } else {
126     new->ip = NULL;
127     new->ip_len = 0;
128     }
129    
130     /* check for "here" */
131     if (document_root[0] == '.' && document_root[1] == '/') {
132     new->document_root = normalize_path( (char*)(document_root+2));
133     if (!new->document_root) {
134     /* superfluous - normalize_path checks for NULL return values. */
135     DIE("normalize_path returned NULL");
136     }
137     document_root_len = strlen(new->document_root);
138     } else {
139     new->document_root = strdup(document_root);
140     if (!new->document_root) {
141     DIE("strdup of document_root failed");
142     }
143     }
144     new->document_root_len = document_root_len;
145    
146     new->next = NULL;
147     }
148    
149     /*
150     * Name: find_virthost
151     *
152     * Description: Locates host in the virthost hashtable if it exists.
153     *
154     * Returns:
155     *
156     * virthost structure or NULL if not found
157     */
158    
159     virthost *find_virthost(const char *host, int hostlen)
160     {
161     virthost *current;
162     int hash;
163    
164     /* Find Hostname, IP, document root */
165    
166     if (hostlen == 0)
167     hostlen = strlen(host);
168    
169     hash = get_host_hash_value( host);
170    
171     current = virthost_hashtable[hash];
172     while (current) {
173     #ifdef FASCIST_LOGGING
174     fprintf(stderr,
175     "%s:%d - comparing \"%s\" (request) to \"%s\" (virthost): ",
176     __FILE__, __LINE__, host, current->host);
177     #endif
178     /* current->host_len must always be:
179     * equal to the host
180     */
181     if (current->host_len == hostlen &&
182     !memcmp(host, current->host, current->host_len)) {
183     #ifdef FASCIST_LOGGING
184     fprintf(stderr, "Got it!\n");
185     #endif
186     return current;
187     }
188     #ifdef FASCIST_LOGGING
189     else
190     fprintf(stderr, "Don't Got it!\n");
191     #endif
192     current = current->next;
193     }
194     return current;
195     }
196    
197    
198    
199     /*
200     * Empties the virthost hashtable, deallocating any allocated memory.
201     */
202    
203     void dump_virthost(void)
204     {
205     int i;
206     virthost *temp;
207    
208     for (i = 0; i < VIRTHOST_HASHTABLE_SIZE; ++i) { /* these limits OK? */
209     if (virthost_hashtable[i]) {
210     temp = virthost_hashtable[i];
211     while (temp) {
212     virthost *temp_next;
213    
214     if (temp->host)
215     free(temp->host);
216     if (temp->ip)
217     free(temp->ip);
218     if (temp->document_root)
219     free(temp->document_root);
220     temp_next = temp->next;
221     free(temp);
222     temp = temp_next;
223     }
224     virthost_hashtable[i] = NULL;
225     }
226     }
227     }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26