summaryrefslogtreecommitdiff
path: root/content/fetch.h
blob: 09b7c8d44f33efcbd5504b2db4fe302550e10ad7 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * Copyright 2003 James Bursa <bursa@users.sourceforge.net>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf 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; version 2 of the License.
 *
 * NetSurf 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file
 * Fetching of data from a URL (interface).
 */

#ifndef _NETSURF_DESKTOP_FETCH_H_
#define _NETSURF_DESKTOP_FETCH_H_

#include <stdbool.h>

#include <libwapcaplet/libwapcaplet.h>

#include "utils/config.h"
#include "utils/nsurl.h"

struct content;
struct fetch;
struct ssl_cert_info;

typedef enum {
	FETCH_PROGRESS,
	FETCH_HEADER,
	FETCH_DATA,
	FETCH_FINISHED,
	FETCH_ERROR,
	FETCH_REDIRECT,
	FETCH_NOTMODIFIED,
	FETCH_AUTH,
	FETCH_CERT_ERR,
	FETCH_SSL_ERR
} fetch_msg_type;

typedef struct fetch_msg {
	fetch_msg_type type;

	union {
		const char *progress;

		struct {
			const uint8_t *buf;
			size_t len;
		} header_or_data;

		const char *error;

		/** \todo Use nsurl */
		const char *redirect;

		struct {
			const char *realm;
		} auth;

		struct {
			const struct ssl_cert_info *certs;
			size_t num_certs;
		} cert_err;
	} data;
} fetch_msg;

/** Fetch POST multipart data */
struct fetch_multipart_data {
	bool file;			/**< Item is a file */
	char *name;			/**< Name of item */
	char *value;			/**< Item value */
	char *rawfile;			/**< Raw filename if file is true */

	struct fetch_multipart_data *next;	/**< Next in linked list */
};

struct ssl_cert_info {
	long version;		/**< Certificate version */
	char not_before[32];	/**< Valid from date */
	char not_after[32];	/**< Valid to date */
	int sig_type;		/**< Signature type */
	long serial;		/**< Serial number */
	char issuer[256];	/**< Issuer details */
	char subject[256];	/**< Subject details */
	int cert_type;		/**< Certificate type */
};

extern bool fetch_active;

typedef void (*fetch_callback)(const fetch_msg *msg, void *p);

/** @todo these calls should be in a file_table in gui_factory */
const char *fetch_filetype(const char *unix_path);
char *fetch_mimetype(const char *ro_path);


/**
 * Initialise the fetcher.
 *
 * @return NSERROR_OK or error code
 */
nserror fetch_init(void);

/**
 * Start fetching data for the given URL.
 *
 * The function returns immediately. The fetch may be queued for later
 * processing.
 *
 * A pointer to an opaque struct fetch is returned, which can be passed to
 * fetch_abort() to abort the fetch at any time. Returns NULL if memory is
 * exhausted (or some other fatal error occurred).
 *
 * The caller must supply a callback function which is called when anything
 * interesting happens. The callback function is first called with msg
 * FETCH_HEADER, with the header in data, then one or more times
 * with FETCH_DATA with some data for the url, and finally with
 * FETCH_FINISHED. Alternatively, FETCH_ERROR indicates an error occurred:
 * data contains an error message. FETCH_REDIRECT may replace the FETCH_HEADER,
 * FETCH_DATA, FETCH_FINISHED sequence if the server sends a replacement URL.
 *
 */
struct fetch *fetch_start(nsurl *url, nsurl *referer,
			  fetch_callback callback,
			  void *p, bool only_2xx, const char *post_urlenc,
			  const struct fetch_multipart_data *post_multipart,
			  bool verifiable, bool downgrade_tls,
			  const char *headers[]);

/**
 * Abort a fetch.
 */
void fetch_abort(struct fetch *f);

/**
 * Do some work on current fetches.
 *
 * Must be called regularly to make progress on fetches.
 */
void fetch_poll(void);

/**
 * Clean up for quit.
 *
 * Must be called before exiting.
 */
void fetch_quit(void);

/**
 * Check if a URL's scheme can be fetched.
 *
 * \param  url  URL to check
 * \return  true if the scheme is supported
 */
bool fetch_can_fetch(const nsurl *url);

/**
 * Change the callback function for a fetch.
 */
void fetch_change_callback(struct fetch *fetch,
			   fetch_callback callback,
			   void *p);

/**
 * Get the HTTP response code.
 */
long fetch_http_code(struct fetch *fetch);

/**
 * Determine if a fetch was verifiable
 *
 * \param fetch  Fetch to consider
 * \return Verifiable status of fetch
 */
bool fetch_get_verifiable(struct fetch *fetch);

/**
 * Free a linked list of fetch_multipart_data.
 *
 * \param list Pointer to head of list to free
 */
void fetch_multipart_data_destroy(struct fetch_multipart_data *list);

/**
 * Clone a linked list of fetch_multipart_data.
 *
 * \param list  List to clone
 * \return Pointer to head of cloned list, or NULL on failure
 */
struct fetch_multipart_data *fetch_multipart_data_clone(const struct fetch_multipart_data *list);

/**
 * send message to fetch
 */
void fetch_send_callback(const fetch_msg *msg, struct fetch *fetch);

/**
 * remove a queued fetch
 */
void fetch_remove_from_queues(struct fetch *fetch);

/**
 * Free a fetch structure and associated resources.
 */
void fetch_free(struct fetch *f);

/**
 * set the http code of a fetch
 */
void fetch_set_http_code(struct fetch *fetch, long http_code);

/**
 * get the referer from the fetch
 */
const char *fetch_get_referer_to_send(struct fetch *fetch);

/**
 * set cookie data on a fetch
 */
void fetch_set_cookie(struct fetch *fetch, const char *data);


/* API for fetchers themselves */

typedef bool (*fetcher_initialise)(lwc_string *scheme);
typedef bool (*fetcher_can_fetch)(const nsurl *url);
typedef void *(*fetcher_setup_fetch)(struct fetch *parent_fetch, nsurl *url,
		bool only_2xx, bool downgrade_tls, const char *post_urlenc,
		const struct fetch_multipart_data *post_multipart,
		const char **headers);
typedef bool (*fetcher_start_fetch)(void *fetch);
typedef void (*fetcher_abort_fetch)(void *fetch);
typedef void (*fetcher_free_fetch)(void *fetch);
typedef void (*fetcher_poll_fetcher)(lwc_string *scheme);
typedef void (*fetcher_finalise)(lwc_string *scheme);

/** Register a fetcher for a scheme
 *
 * \param scheme	scheme fetcher is for (caller relinquishes ownership)
 * \param initialiser	fetcher initialiser
 * \param can_fetch     fetcher can fetch function
 * \param setup_fetch	fetcher fetch setup function
 * \param start_fetch	fetcher fetch start function
 * \param abort_fetch	fetcher fetch abort function
 * \param free_fetch	fetcher fetch free function
 * \param poll_fetcher	fetcher poll function
 * \param finaliser	fetcher finaliser
 * \return true iff success
 */
bool fetch_add_fetcher(lwc_string *scheme,
		       fetcher_initialise initialiser,
		       fetcher_can_fetch can_fetch,
		       fetcher_setup_fetch setup_fetch,
		       fetcher_start_fetch start_fetch,
		       fetcher_abort_fetch abort_fetch,
		       fetcher_free_fetch free_fetch,
		       fetcher_poll_fetcher poll_fetcher,
		       fetcher_finalise finaliser);

#endif