~ubuntu-branches/ubuntu/natty/lighttpd/natty

« back to all changes in this revision

Viewing changes to src/mod_alias.c

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2006-12-08 14:40:42 UTC
  • mto: (6.1.1 lenny)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20061208144042-ehr7h8c6xmijqipw
Tags: upstream-1.4.13
ImportĀ upstreamĀ versionĀ 1.4.13

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
typedef struct {
18
18
        PLUGIN_DATA;
19
 
        
 
19
 
20
20
        plugin_config **config_storage;
21
 
        
22
 
        plugin_config conf; 
 
21
 
 
22
        plugin_config conf;
23
23
} plugin_data;
24
24
 
25
25
/* init the plugin data */
26
26
INIT_FUNC(mod_alias_init) {
27
27
        plugin_data *p;
28
 
        
 
28
 
29
29
        p = calloc(1, sizeof(*p));
30
 
        
31
 
        
32
 
        
 
30
 
 
31
 
 
32
 
33
33
        return p;
34
34
}
35
35
 
36
36
/* detroy the plugin data */
37
37
FREE_FUNC(mod_alias_free) {
38
38
        plugin_data *p = p_d;
39
 
        
 
39
 
40
40
        if (!p) return HANDLER_GO_ON;
41
 
        
 
41
 
42
42
        if (p->config_storage) {
43
43
                size_t i;
44
 
                
 
44
 
45
45
                for (i = 0; i < srv->config_context->used; i++) {
46
46
                        plugin_config *s = p->config_storage[i];
47
 
                        
 
47
 
48
48
                        array_free(s->alias);
49
 
                        
 
49
 
50
50
                        free(s);
51
51
                }
52
52
                free(p->config_storage);
53
53
        }
54
 
        
 
54
 
55
55
        free(p);
56
 
        
 
56
 
57
57
        return HANDLER_GO_ON;
58
58
}
59
59
 
62
62
SETDEFAULTS_FUNC(mod_alias_set_defaults) {
63
63
        plugin_data *p = p_d;
64
64
        size_t i = 0;
65
 
        
66
 
        config_values_t cv[] = { 
 
65
 
 
66
        config_values_t cv[] = {
67
67
                { "alias.url",                  NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION },       /* 0 */
68
68
                { NULL,                         NULL, T_CONFIG_UNSET,  T_CONFIG_SCOPE_UNSET }
69
69
        };
70
 
        
 
70
 
71
71
        if (!p) return HANDLER_ERROR;
72
 
        
 
72
 
73
73
        p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
74
 
        
 
74
 
75
75
        for (i = 0; i < srv->config_context->used; i++) {
76
76
                plugin_config *s;
77
 
                
 
77
 
78
78
                s = calloc(1, sizeof(plugin_config));
79
 
                s->alias = array_init();        
 
79
                s->alias = array_init();
80
80
                cv[0].destination = s->alias;
81
 
                
 
81
 
82
82
                p->config_storage[i] = s;
83
 
                
 
83
 
84
84
                if (0 != config_insert_values_global(srv, ((data_config *)srv->config_context->data[i])->value, cv)) {
85
85
                        return HANDLER_ERROR;
86
86
                }
110
110
                        }
111
111
                }
112
112
        }
113
 
        
 
113
 
114
114
        return HANDLER_GO_ON;
115
115
}
116
116
 
119
119
static int mod_alias_patch_connection(server *srv, connection *con, plugin_data *p) {
120
120
        size_t i, j;
121
121
        plugin_config *s = p->config_storage[0];
122
 
        
 
122
 
123
123
        PATCH(alias);
124
 
        
 
124
 
125
125
        /* skip the first, the global context */
126
126
        for (i = 1; i < srv->config_context->used; i++) {
127
127
                data_config *dc = (data_config *)srv->config_context->data[i];
128
128
                s = p->config_storage[i];
129
 
                
 
129
 
130
130
                /* condition didn't match */
131
131
                if (!config_check_cond(srv, con, dc)) continue;
132
 
                
 
132
 
133
133
                /* merge config */
134
134
                for (j = 0; j < dc->value->used; j++) {
135
135
                        data_unset *du = dc->value->data[j];
136
 
                        
 
136
 
137
137
                        if (buffer_is_equal_string(du->key, CONST_STR_LEN("alias.url"))) {
138
138
                                PATCH(alias);
139
139
                        }
140
140
                }
141
141
        }
142
 
        
 
142
 
143
143
        return 0;
144
144
}
145
145
#undef PATCH
149
149
        int uri_len, basedir_len;
150
150
        char *uri_ptr;
151
151
        size_t k;
152
 
        
 
152
 
153
153
        if (con->physical.path->used == 0) return HANDLER_GO_ON;
154
 
        
 
154
 
155
155
        mod_alias_patch_connection(srv, con, p);
156
 
        
 
156
 
157
157
        /* not to include the tailing slash */
158
158
        basedir_len = (con->physical.basedir->used - 1) - 1;
159
159
        uri_len = con->physical.path->used - 1 - basedir_len;
160
160
        uri_ptr = con->physical.path->ptr + basedir_len;
161
 
        
 
161
 
162
162
        for (k = 0; k < p->conf.alias->used; k++) {
163
163
                data_string *ds = (data_string *)p->conf.alias->data[k];
164
164
                int alias_len = ds->key->used - 1;
165
 
                
 
165
 
166
166
                if (alias_len > uri_len) continue;
167
167
                if (ds->key->used == 0) continue;
168
 
                
 
168
 
169
169
                if (0 == (con->conf.force_lowercase_filenames ?
170
170
                                        strncasecmp(uri_ptr, ds->key->ptr, alias_len) :
171
171
                                        strncmp(uri_ptr, ds->key->ptr, alias_len))) {
172
172
                        /* matched */
173
 
                        
 
173
 
174
174
                        buffer_copy_string_buffer(con->physical.basedir, ds->value);
175
175
                        buffer_copy_string_buffer(srv->tmp_buf, ds->value);
176
176
                        buffer_append_string(srv->tmp_buf, uri_ptr + alias_len);
177
177
                        buffer_copy_string_buffer(con->physical.path, srv->tmp_buf);
178
 
                        
 
178
 
179
179
                        return HANDLER_GO_ON;
180
180
                }
181
181
        }
182
 
        
 
182
 
183
183
        /* not found */
184
184
        return HANDLER_GO_ON;
185
185
}
189
189
int mod_alias_plugin_init(plugin *p) {
190
190
        p->version     = LIGHTTPD_VERSION_ID;
191
191
        p->name        = buffer_init_string("alias");
192
 
        
 
192
 
193
193
        p->init           = mod_alias_init;
194
194
        p->handle_physical= mod_alias_physical_handler;
195
195
        p->set_defaults   = mod_alias_set_defaults;
196
196
        p->cleanup        = mod_alias_free;
197
 
        
 
197
 
198
198
        p->data        = NULL;
199
 
        
 
199
 
200
200
        return 0;
201
201
}