Moved data structures to new collections subfolder
[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
16 #include "fetcher_manager.h"
17
18 #include <debug.h>
19 #include <threading/rwlock.h>
20 #include <collections/linked_list.h>
21
22 typedef struct private_fetcher_manager_t private_fetcher_manager_t;
23
24 /**
25 * private data of fetcher_manager
26 */
27 struct private_fetcher_manager_t {
28
29 /**
30 * public functions
31 */
32 fetcher_manager_t public;
33
34 /**
35 * list of registered fetchers, as entry_t
36 */
37 linked_list_t *fetchers;
38
39 /**
40 * read write lock to list
41 */
42 rwlock_t *lock;
43 };
44
45 typedef struct {
46 /** assocaited fetcher construction function */
47 fetcher_constructor_t create;
48 /** URL this fetcher support */
49 char *url;
50 } entry_t;
51
52 /**
53 * destroy an entry_t
54 */
55 static void entry_destroy(entry_t *entry)
56 {
57 free(entry->url);
58 free(entry);
59 }
60
61 METHOD(fetcher_manager_t, fetch, status_t,
62 private_fetcher_manager_t *this, char *url, void *userdata, ...)
63 {
64 enumerator_t *enumerator;
65 status_t status = NOT_SUPPORTED;
66 entry_t *entry;
67 bool capable = FALSE;
68
69 this->lock->read_lock(this->lock);
70 enumerator = this->fetchers->create_enumerator(this->fetchers);
71 while (enumerator->enumerate(enumerator, &entry))
72 {
73 fetcher_option_t opt;
74 fetcher_t *fetcher;
75 bool good = TRUE;
76 va_list args;
77
78 /* check URL support of fetcher */
79 if (strncasecmp(entry->url, url, strlen(entry->url)))
80 {
81 continue;
82 }
83 /* create fetcher instance and set options */
84 fetcher = entry->create();
85 if (!fetcher)
86 {
87 continue;
88 }
89 va_start(args, userdata);
90 while (good)
91 {
92 opt = va_arg(args, int);
93 switch (opt)
94 {
95 case FETCH_REQUEST_DATA:
96 good = fetcher->set_option(fetcher, opt,
97 va_arg(args, chunk_t));
98 continue;
99 case FETCH_REQUEST_TYPE:
100 case FETCH_REQUEST_HEADER:
101 good = fetcher->set_option(fetcher, opt,
102 va_arg(args, char*));
103 continue;
104 case FETCH_HTTP_VERSION_1_0:
105 good = fetcher->set_option(fetcher, opt);
106 continue;
107 case FETCH_TIMEOUT:
108 good = fetcher->set_option(fetcher, opt,
109 va_arg(args, u_int));
110 continue;
111 case FETCH_CALLBACK:
112 good = fetcher->set_option(fetcher, opt,
113 va_arg(args, fetcher_callback_t));
114 case FETCH_END:
115 break;
116 }
117 break;
118 }
119 va_end(args);
120 if (!good)
121 { /* fetcher does not support supplied options, try another */
122 fetcher->destroy(fetcher);
123 continue;
124 }
125
126 status = fetcher->fetch(fetcher, url, userdata);
127 fetcher->destroy(fetcher);
128 /* try another fetcher only if this one does not support that URL */
129 if (status == NOT_SUPPORTED)
130 {
131 continue;
132 }
133 capable = TRUE;
134 break;
135 }
136 enumerator->destroy(enumerator);
137 this->lock->unlock(this->lock);
138 if (!capable)
139 {
140 DBG1(DBG_LIB, "unable to fetch from %s, no capable fetcher found", url);
141 }
142 return status;
143 }
144
145 METHOD(fetcher_manager_t, add_fetcher, void,
146 private_fetcher_manager_t *this, fetcher_constructor_t create, char *url)
147 {
148 entry_t *entry;
149
150 INIT(entry,
151 .url = strdup(url),
152 .create = create,
153 );
154 this->lock->write_lock(this->lock);
155 this->fetchers->insert_last(this->fetchers, entry);
156 this->lock->unlock(this->lock);
157 }
158
159 METHOD(fetcher_manager_t, remove_fetcher, void,
160 private_fetcher_manager_t *this, fetcher_constructor_t create)
161 {
162 enumerator_t *enumerator;
163 entry_t *entry;
164
165 this->lock->write_lock(this->lock);
166 enumerator = this->fetchers->create_enumerator(this->fetchers);
167 while (enumerator->enumerate(enumerator, &entry))
168 {
169 if (entry->create == create)
170 {
171 this->fetchers->remove_at(this->fetchers, enumerator);
172 entry_destroy(entry);
173 }
174 }
175 enumerator->destroy(enumerator);
176 this->lock->unlock(this->lock);
177 }
178
179 METHOD(fetcher_manager_t, destroy, void,
180 private_fetcher_manager_t *this)
181 {
182 this->fetchers->destroy_function(this->fetchers, (void*)entry_destroy);
183 this->lock->destroy(this->lock);
184 free(this);
185 }
186
187 /*
188 * see header file
189 */
190 fetcher_manager_t *fetcher_manager_create()
191 {
192 private_fetcher_manager_t *this;
193
194 INIT(this,
195 .public = {
196 .fetch = _fetch,
197 .add_fetcher = _add_fetcher,
198 .remove_fetcher = _remove_fetcher,
199 .destroy = _destroy,
200 },
201 .fetchers = linked_list_create(),
202 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
203 );
204
205 return &this->public;
206 }
207