pluto and scepclient use the curl and ldap fetcher plugins
[strongswan.git] / src / libstrongswan / fetcher / fetcher_manager.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include "fetcher_manager.h"
19
20 #include <debug.h>
21 #include <utils/mutex.h>
22 #include <utils/linked_list.h>
23
24 typedef struct private_fetcher_manager_t private_fetcher_manager_t;
25
26 /**
27 * private data of fetcher_manager
28 */
29 struct private_fetcher_manager_t {
30
31 /**
32 * public functions
33 */
34 fetcher_manager_t public;
35
36 /**
37 * list of registered fetchers, as entry_t
38 */
39 linked_list_t *fetchers;
40
41 /**
42 * read write lock to list
43 */
44 rwlock_t *lock;
45 };
46
47 typedef struct {
48 /** assocaited fetcher construction function */
49 fetcher_constructor_t create;
50 /** URL this fetcher support */
51 char *url;
52 } entry_t;
53
54 /**
55 * destroy an entry_t
56 */
57 static void entry_destroy(entry_t *entry)
58 {
59 free(entry->url);
60 free(entry);
61 }
62
63 /**
64 * Implementation of fetcher_manager_t.fetch.
65 */
66 static status_t fetch(private_fetcher_manager_t *this,
67 char *url, chunk_t *response, ...)
68 {
69 enumerator_t *enumerator;
70 status_t status = NOT_SUPPORTED;
71 entry_t *entry;
72 bool capable = FALSE;
73
74 this->lock->read_lock(this->lock);
75 enumerator = this->fetchers->create_enumerator(this->fetchers);
76 while (enumerator->enumerate(enumerator, &entry))
77 {
78 fetcher_option_t opt;
79 fetcher_t *fetcher;
80 bool good = TRUE;
81 va_list args;
82
83 /* check URL support of fetcher */
84 if (strncasecmp(entry->url, url, strlen(entry->url)))
85 {
86 continue;
87 }
88 /* create fetcher instance and set options */
89 fetcher = entry->create();
90 if (!fetcher)
91 {
92 continue;
93 }
94 va_start(args, response);
95 while (good)
96 {
97 opt = va_arg(args, fetcher_option_t);
98 switch (opt)
99 {
100 case FETCH_REQUEST_DATA:
101 good = fetcher->set_option(fetcher, opt, va_arg(args, chunk_t));
102 continue;
103 case FETCH_REQUEST_TYPE:
104 case FETCH_REQUEST_HEADER:
105 good = fetcher->set_option(fetcher, opt, va_arg(args, char*));
106 continue;
107 case FETCH_HTTP_VERSION_1_0:
108 good = fetcher->set_option(fetcher, opt);
109 continue;
110 case FETCH_TIMEOUT:
111 good = fetcher->set_option(fetcher, opt, va_arg(args, u_int));
112 continue;
113 case FETCH_END:
114 break;;
115 }
116 break;
117 }
118 va_end(args);
119 if (!good)
120 { /* fetcher does not support supplied options, try another */
121 fetcher->destroy(fetcher);
122 continue;
123 }
124
125 status = fetcher->fetch(fetcher, url, response);
126 fetcher->destroy(fetcher);
127 /* try another fetcher only if this one does not support that URL */
128 if (status == NOT_SUPPORTED)
129 {
130 continue;
131 }
132 capable = TRUE;
133 break;
134 }
135 enumerator->destroy(enumerator);
136 this->lock->unlock(this->lock);
137 if (!capable)
138 {
139 DBG1("unable to fetch from %s, no capable fetcher found", url);
140 }
141 return status;
142 }
143
144 /**
145 * Implementation of fetcher_manager_t.add_fetcher.
146 */
147 static void add_fetcher(private_fetcher_manager_t *this,
148 fetcher_constructor_t create, char *url)
149 {
150 entry_t *entry = malloc_thing(entry_t);
151
152 entry->url = strdup(url);
153 entry->create = create;
154
155 this->lock->write_lock(this->lock);
156 this->fetchers->insert_last(this->fetchers, entry);
157 this->lock->unlock(this->lock);
158 }
159
160 /**
161 * Implementation of fetcher_manager_t.remove_fetcher.
162 */
163 static void remove_fetcher(private_fetcher_manager_t *this,
164 fetcher_constructor_t create)
165 {
166 enumerator_t *enumerator;
167 entry_t *entry;
168
169 this->lock->write_lock(this->lock);
170 enumerator = this->fetchers->create_enumerator(this->fetchers);
171 while (enumerator->enumerate(enumerator, &entry))
172 {
173 if (entry->create == create)
174 {
175 this->fetchers->remove_at(this->fetchers, enumerator);
176 entry_destroy(entry);
177 }
178 }
179 enumerator->destroy(enumerator);
180 this->lock->unlock(this->lock);
181 }
182
183 /**
184 * Implementation of fetcher_manager_t.destroy
185 */
186 static void destroy(private_fetcher_manager_t *this)
187 {
188 this->fetchers->destroy_function(this->fetchers, (void*)entry_destroy);
189 this->lock->destroy(this->lock);
190 free(this);
191 }
192
193 /*
194 * see header file
195 */
196 fetcher_manager_t *fetcher_manager_create()
197 {
198 private_fetcher_manager_t *this = malloc_thing(private_fetcher_manager_t);
199
200 this->public.fetch = (status_t(*)(fetcher_manager_t*, char *url, chunk_t *response, ...))fetch;
201 this->public.add_fetcher = (void(*)(fetcher_manager_t*, fetcher_constructor_t,char*))add_fetcher;
202 this->public.remove_fetcher = (void(*)(fetcher_manager_t*, fetcher_constructor_t))remove_fetcher;
203 this->public.destroy = (void(*)(fetcher_manager_t*))destroy;
204
205 this->fetchers = linked_list_create();
206 this->lock = rwlock_create(RWLOCK_DEFAULT);
207
208 return &this->public;
209 }
210