summaryrefslogtreecommitdiff
path: root/src/libstrongswan/fetcher/fetcher_manager.c
blob: c81de032c817ca0dafcb458b4e83e6c178aaf9ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright (C) 2008 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "fetcher_manager.h"

#include <debug.h>
#include <threading/rwlock.h>
#include <utils/linked_list.h>

typedef struct private_fetcher_manager_t private_fetcher_manager_t;

/**
 * private data of fetcher_manager
 */
struct private_fetcher_manager_t {

	/**
	 * public functions
	 */
	fetcher_manager_t public;

	/**
	 * list of registered fetchers, as entry_t
	 */
	linked_list_t *fetchers;

	/**
	 * read write lock to list
	 */
	rwlock_t *lock;
};

typedef struct {
	/** assocaited fetcher construction function */
	fetcher_constructor_t create;
	/** URL this fetcher support */
	char *url;
} entry_t;

/**
 * destroy an entry_t
 */
static void entry_destroy(entry_t *entry)
{
	free(entry->url);
	free(entry);
}

/**
 * Implementation of fetcher_manager_t.fetch.
 */
static status_t fetch(private_fetcher_manager_t *this,
					  char *url, chunk_t *response, ...)
{
	enumerator_t *enumerator;
	status_t status = NOT_SUPPORTED;
	entry_t *entry;
	bool capable = FALSE;

	this->lock->read_lock(this->lock);
	enumerator = this->fetchers->create_enumerator(this->fetchers);
	while (enumerator->enumerate(enumerator, &entry))
	{
		fetcher_option_t opt;
		fetcher_t *fetcher;
		bool good = TRUE;
		va_list args;

		/* check URL support of fetcher */
		if (strncasecmp(entry->url, url, strlen(entry->url)))
		{
			continue;
		}
		/* create fetcher instance and set options */
		fetcher = entry->create();
		if (!fetcher)
		{
			continue;
		}
		va_start(args, response);
		while (good)
		{
			opt = va_arg(args, fetcher_option_t);
			switch (opt)
			{
				case FETCH_REQUEST_DATA:
					good = fetcher->set_option(fetcher, opt, va_arg(args, chunk_t));
					continue;
				case FETCH_REQUEST_TYPE:
				case FETCH_REQUEST_HEADER:
					good = fetcher->set_option(fetcher, opt, va_arg(args, char*));
					continue;
				case FETCH_HTTP_VERSION_1_0:
					good = fetcher->set_option(fetcher, opt);
					continue;
				case FETCH_TIMEOUT:
					good = fetcher->set_option(fetcher, opt, va_arg(args, u_int));
					continue;
				case FETCH_END:
					break;;
			}
			break;
		}
		va_end(args);
		if (!good)
		{	/* fetcher does not support supplied options, try another */
			fetcher->destroy(fetcher);
			continue;
		}

		status = fetcher->fetch(fetcher, url, response);
		fetcher->destroy(fetcher);
		/* try another fetcher only if this one does not support that URL */
		if (status == NOT_SUPPORTED)
		{
			continue;
		}
		capable = TRUE;
		break;
	}
	enumerator->destroy(enumerator);
	this->lock->unlock(this->lock);
	if (!capable)
	{
		DBG1(DBG_LIB, "unable to fetch from %s, no capable fetcher found", url);
	}
	return status;
}

/**
 * Implementation of fetcher_manager_t.add_fetcher.
 */
static void add_fetcher(private_fetcher_manager_t *this,
						fetcher_constructor_t create, char *url)
{
	entry_t *entry = malloc_thing(entry_t);

	entry->url = strdup(url);
	entry->create = create;

	this->lock->write_lock(this->lock);
	this->fetchers->insert_last(this->fetchers, entry);
	this->lock->unlock(this->lock);
}

/**
 * Implementation of fetcher_manager_t.remove_fetcher.
 */
static void remove_fetcher(private_fetcher_manager_t *this,
						   fetcher_constructor_t create)
{
	enumerator_t *enumerator;
	entry_t *entry;

	this->lock->write_lock(this->lock);
	enumerator = this->fetchers->create_enumerator(this->fetchers);
	while (enumerator->enumerate(enumerator, &entry))
	{
		if (entry->create == create)
		{
			this->fetchers->remove_at(this->fetchers, enumerator);
			entry_destroy(entry);
		}
	}
	enumerator->destroy(enumerator);
	this->lock->unlock(this->lock);
}

/**
 * Implementation of fetcher_manager_t.destroy
 */
static void destroy(private_fetcher_manager_t *this)
{
	this->fetchers->destroy_function(this->fetchers, (void*)entry_destroy);
	this->lock->destroy(this->lock);
	free(this);
}

/*
 * see header file
 */
fetcher_manager_t *fetcher_manager_create()
{
	private_fetcher_manager_t *this = malloc_thing(private_fetcher_manager_t);

	this->public.fetch = (status_t(*)(fetcher_manager_t*, char *url, chunk_t *response, ...))fetch;
	this->public.add_fetcher = (void(*)(fetcher_manager_t*, fetcher_constructor_t,char*))add_fetcher;
	this->public.remove_fetcher = (void(*)(fetcher_manager_t*, fetcher_constructor_t))remove_fetcher;
	this->public.destroy = (void(*)(fetcher_manager_t*))destroy;

	this->fetchers = linked_list_create();
	this->lock = rwlock_create(RWLOCK_TYPE_DEFAULT);

	return &this->public;
}