Migrated fetcher_manager to INIT/METHOD macros
[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 <utils/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, chunk_t *response, ...)
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, response);
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, va_arg(args, chunk_t));
97 continue;
98 case FETCH_REQUEST_TYPE:
99 case FETCH_REQUEST_HEADER:
100 good = fetcher->set_option(fetcher, opt, va_arg(args, char*));
101 continue;
102 case FETCH_HTTP_VERSION_1_0:
103 good = fetcher->set_option(fetcher, opt);
104 continue;
105 case FETCH_TIMEOUT:
106 good = fetcher->set_option(fetcher, opt, va_arg(args, u_int));
107 continue;
108 case FETCH_END:
109 break;
110 }
111 break;
112 }
113 va_end(args);
114 if (!good)
115 { /* fetcher does not support supplied options, try another */
116 fetcher->destroy(fetcher);
117 continue;
118 }
119
120 status = fetcher->fetch(fetcher, url, response);
121 fetcher->destroy(fetcher);
122 /* try another fetcher only if this one does not support that URL */
123 if (status == NOT_SUPPORTED)
124 {
125 continue;
126 }
127 capable = TRUE;
128 break;
129 }
130 enumerator->destroy(enumerator);
131 this->lock->unlock(this->lock);
132 if (!capable)
133 {
134 DBG1(DBG_LIB, "unable to fetch from %s, no capable fetcher found", url);
135 }
136 return status;
137 }
138
139 METHOD(fetcher_manager_t, add_fetcher, void,
140 private_fetcher_manager_t *this, fetcher_constructor_t create, char *url)
141 {
142 entry_t *entry;
143
144 INIT(entry,
145 .url = strdup(url),
146 .create = create,
147 );
148 this->lock->write_lock(this->lock);
149 this->fetchers->insert_last(this->fetchers, entry);
150 this->lock->unlock(this->lock);
151 }
152
153 METHOD(fetcher_manager_t, remove_fetcher, void,
154 private_fetcher_manager_t *this, fetcher_constructor_t create)
155 {
156 enumerator_t *enumerator;
157 entry_t *entry;
158
159 this->lock->write_lock(this->lock);
160 enumerator = this->fetchers->create_enumerator(this->fetchers);
161 while (enumerator->enumerate(enumerator, &entry))
162 {
163 if (entry->create == create)
164 {
165 this->fetchers->remove_at(this->fetchers, enumerator);
166 entry_destroy(entry);
167 }
168 }
169 enumerator->destroy(enumerator);
170 this->lock->unlock(this->lock);
171 }
172
173 METHOD(fetcher_manager_t, destroy, void,
174 private_fetcher_manager_t *this)
175 {
176 this->fetchers->destroy_function(this->fetchers, (void*)entry_destroy);
177 this->lock->destroy(this->lock);
178 free(this);
179 }
180
181 /*
182 * see header file
183 */
184 fetcher_manager_t *fetcher_manager_create()
185 {
186 private_fetcher_manager_t *this;
187
188 INIT(this,
189 .public = {
190 .fetch = _fetch,
191 .add_fetcher = _add_fetcher,
192 .remove_fetcher = _remove_fetcher,
193 .destroy = _destroy,
194 },
195 .fetchers = linked_list_create(),
196 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
197 );
198
199 return &this->public;
200 }
201