56e049c56e37b3c73471fad599136b9eaf4cf9a6
[project/uhttpd.git] / plugin.c
1 /*
2  * uhttpd - Tiny single-threaded httpd - Main header
3  *
4  *   Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5  *   Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  */
19
20 #include <dlfcn.h>
21 #include "uhttpd.h"
22 #include "plugin.h"
23
24 static LIST_HEAD(plugins);
25
26 static const struct uhttpd_ops ops = {
27         .dispatch_add = uh_dispatch_add,
28         .path_match = uh_path_match,
29         .create_process = uh_create_process,
30         .get_process_vars = uh_get_process_vars,
31         .http_header = uh_http_header,
32         .client_error = uh_client_error,
33         .request_done = uh_request_done,
34         .chunk_write = uh_chunk_write,
35         .urlencode = uh_urlencode,
36         .urldecode = uh_urldecode,
37 };
38
39 int uh_plugin_init(const char *name)
40 {
41         struct uhttpd_plugin *p;
42         const char *sym;
43         void *dlh;
44
45         dlh = dlopen(name, RTLD_LAZY | RTLD_LOCAL);
46         if (!dlh) {
47                 fprintf(stderr, "Could not open plugin %s: %s\n", name, dlerror());
48                 return -ENOENT;
49         }
50
51         sym = "uhttpd_plugin";
52         p = dlsym(dlh, sym);
53         if (!p) {
54                 fprintf(stderr, "Could not find symbol '%s' in plugin '%s'\n", sym, name);
55                 return -ENOENT;
56         }
57
58         list_add(&p->list, &plugins);
59         return p->init(&ops, &conf);
60 }
61
62 void uh_plugin_post_init(void)
63 {
64         struct uhttpd_plugin *p;
65
66         list_for_each_entry(p, &plugins, list)
67                 p->post_init();
68 }