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