fetcher: add missing "continue" when handling FETCH_CALLBACK
[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 <utils/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 continue;
115 case FETCH_END:
116 break;
117 }
118 break;
119 }
120 va_end(args);
121 if (!good)
122 { /* fetcher does not support supplied options, try another */
123 fetcher->destroy(fetcher);
124 continue;
125 }
126
127 status = fetcher->fetch(fetcher, url, userdata);
128 fetcher->destroy(fetcher);
129 /* try another fetcher only if this one does not support that URL */
130 if (status == NOT_SUPPORTED)
131 {
132 continue;
133 }
134 capable = TRUE;
135 break;
136 }
137 enumerator->destroy(enumerator);
138 this->lock->unlock(this->lock);
139 if (!capable)
140 {
141 DBG1(DBG_LIB, "unable to fetch from %s, no capable fetcher found", url);
142 }
143 return status;
144 }
145
146 METHOD(fetcher_manager_t, add_fetcher, void,
147 private_fetcher_manager_t *this, fetcher_constructor_t create, char *url)
148 {
149 entry_t *entry;
150
151 INIT(entry,
152 .url = strdup(url),
153 .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 METHOD(fetcher_manager_t, remove_fetcher, void,
161 private_fetcher_manager_t *this, fetcher_constructor_t create)
162 {
163 enumerator_t *enumerator;
164 entry_t *entry;
165
166 this->lock->write_lock(this->lock);
167 enumerator = this->fetchers->create_enumerator(this->fetchers);
168 while (enumerator->enumerate(enumerator, &entry))
169 {
170 if (entry->create == create)
171 {
172 this->fetchers->remove_at(this->fetchers, enumerator);
173 entry_destroy(entry);
174 }
175 }
176 enumerator->destroy(enumerator);
177 this->lock->unlock(this->lock);
178 }
179
180 METHOD(fetcher_manager_t, destroy, void,
181 private_fetcher_manager_t *this)
182 {
183 this->fetchers->destroy_function(this->fetchers, (void*)entry_destroy);
184 this->lock->destroy(this->lock);
185 free(this);
186 }
187
188 /*
189 * see header file
190 */
191 fetcher_manager_t *fetcher_manager_create()
192 {
193 private_fetcher_manager_t *this;
194
195 INIT(this,
196 .public = {
197 .fetch = _fetch,
198 .add_fetcher = _add_fetcher,
199 .remove_fetcher = _remove_fetcher,
200 .destroy = _destroy,
201 },
202 .fetchers = linked_list_create(),
203 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
204 );
205
206 return &this->public;
207 }