summaryrefslogtreecommitdiff
path: root/amiga/fetch_file.c
blob: f8d3e777ed96fd67533a7628489f2a800ffddda4 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * Copyright 2008 Chris Young <chris@unsatisfactorysoftware.co.uk>
 *
 * 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 file (implementation).
 */

#include <string.h>
#include "content/fetch.h"
#include "utils/log.h"
#include "utils/url.h"
#include <proto/dos.h>
#include <proto/exec.h>
#include "amiga/object.h"
#include <malloc.h>
#include "content/content.h"
#include <time.h>
#include <proto/utility.h>
#include "utils/messages.h"

static struct MinList *ami_file_fetcher_list;
static UBYTE *ami_file_fetcher_buffer = NULL;

/** Information for a single fetch. */
struct ami_file_fetch_info {
	struct fetch *fetch_handle; /**< The fetch handle we're parented by. */
	BPTR fh;	/** File handle */
	bool only_2xx;		/**< Only HTTP 2xx responses acceptable. */
	char *path;
	char *url;		/**< URL of this fetch. */
	bool aborted;
	bool locked;
	struct nsObject *obj;
	int httpcode;
	int64 len;
	char *mimetype;
	struct cache_data cachedata;
};

static bool ami_fetch_file_initialise(const char *scheme);
static void ami_fetch_file_finalise(const char *scheme);
static void * ami_fetch_file_setup(struct fetch *parent_fetch, const char *url,
		 bool only_2xx, const char *post_urlenc,
		 struct form_successful_control *post_multipart,
		 const char **headers);
static bool ami_fetch_file_start(void *vfetch);
static void ami_fetch_file_abort(void *vf);
static void ami_fetch_file_free(void *f);
static void ami_fetch_file_poll(const char *scheme_ignored);

/**
 * Initialise the fetcher.
 *
 * Must be called once before any other function.
 */

void ami_fetch_file_register(void)
{
	if (!fetch_add_fetcher("file",
				       ami_fetch_file_initialise,
				       ami_fetch_file_setup,
				       ami_fetch_file_start,
				       ami_fetch_file_abort,
				       ami_fetch_file_free,
				       ami_fetch_file_poll,
				       ami_fetch_file_finalise)) {
			LOG(("Unable to register Amiga fetcher for file:"));
	}
}


/**
 * Initialise a cURL fetcher.
 */

bool ami_fetch_file_initialise(const char *scheme)
{
	LOG(("Initialise Amiga fetcher for %s", scheme));
	ami_file_fetcher_list = NewObjList();
	ami_file_fetcher_buffer = AllocVec(1024,MEMF_PRIVATE);

	if(ami_file_fetcher_list && ami_file_fetcher_buffer) return true;
		else return false;
}


/**
 * Finalise a cURL fetcher
 */

void ami_fetch_file_finalise(const char *scheme)
{
	LOG(("Finalise Amiga fetcher %s", scheme));
	FreeObjList(ami_file_fetcher_list);
	FreeVec(ami_file_fetcher_buffer);
}


/**
 * 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 curl_fetch_info is returned, which can be passed to
 * fetch_abort() to abort the fetch at any time. Returns 0 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_TYPE, with the Content-Type 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_TYPE,
 * FETCH_DATA, FETCH_FINISHED sequence if the server sends a replacement URL.
 *
 * Some private data can be passed as the last parameter to fetch_start, and
 * callbacks will contain this.
 */

void * ami_fetch_file_setup(struct fetch *parent_fetch, const char *url,
		 bool only_2xx, const char *post_urlenc,
		 struct form_successful_control *post_multipart,
		 const char **headers)
{
	struct ami_file_fetch_info *fetch;

	fetch = AllocVec(sizeof (*fetch),MEMF_PRIVATE | MEMF_CLEAR);
	if (!fetch)
		return 0;

	fetch->fetch_handle = parent_fetch;

	/* construct a new fetch structure */
	fetch->fh = 0;
	fetch->only_2xx = only_2xx;
//	fetch->url = strdup(url);
	fetch->path = url_to_path(url);

	LOG(("fetch %p, url '%s', path '%s'", fetch, url,fetch->path));

	fetch->obj = AddObject(ami_file_fetcher_list,AMINS_FETCHER);
	fetch->obj->objstruct = fetch;

	return fetch;
}


/**
 * Dispatch a single job
 */
bool ami_fetch_file_start(void *vfetch)
{
	struct ami_file_fetch_info *fetch = (struct ami_file_fetch_info*)vfetch;

	LOG(("ami file fetcher start"));

	fetch->cachedata.req_time = time(NULL);
	fetch->cachedata.res_time = time(NULL);
	fetch->cachedata.date = 0;
	fetch->cachedata.expires = 0;
	fetch->cachedata.age = INVALID_AGE;
	fetch->cachedata.max_age = 0;
	fetch->cachedata.no_cache = true;
	fetch->cachedata.etag = NULL;
	fetch->cachedata.last_modified = 0;

	return true;
}

void ami_fetch_file_abort(void *vf)
{
	struct ami_file_fetch_info *fetch = (struct ami_file_fetch_info*)vf;

	LOG(("ami file fetcher abort"));

	if (fetch->fh) {
		FClose(fetch->fh);
		fetch->fh = 0;
		fetch->aborted = true;
	}
/*
else {
		fetch_remove_from_queues(fetch->fetch_handle);
		fetch_free(fetch->fetch_handle);
	}
*/
}


/**
 * Free a fetch structure and associated resources.
 */

void ami_fetch_file_free(void *vf)
{
	struct ami_file_fetch_info *fetch = (struct ami_file_fetch_info*)vf;
	LOG(("ami file fetcher free %lx",fetch));

	if(fetch->fh) FClose(fetch->fh);
	if(fetch->mimetype) free(fetch->mimetype);
	if(fetch->path) free(fetch->path);

	DelObject(fetch->obj); // delobject frees fetch
}

static void ami_fetch_file_send_callback(fetch_msg msg,
		struct ami_file_fetch_info *fetch, const void *data,
		unsigned long size)
{
	fetch->locked = true;
	LOG(("ami file fetcher callback %ld",msg));
	fetch_send_callback(msg,fetch->fetch_handle,data,size);
	fetch->locked = false;
}

/**
 * Do some work on current fetches.
 *
 * Must be called regularly to make progress on fetches.
 */

void ami_fetch_file_poll(const char *scheme_ignored)
{
	struct nsObject *node;
	struct nsObject *nnode;
	struct ami_file_fetch_info *fetch;
	
	if(IsMinListEmpty(ami_file_fetcher_list)) return;

	node = (struct nsObject *)GetHead((struct List *)ami_file_fetcher_list);

	do
	{
		nnode=(struct nsObject *)GetSucc((struct Node *)node);

		fetch = (struct ami_file_fetch_info *)node->objstruct;
		LOG(("polling %lx",fetch));

		if(fetch->locked) continue;

		if(!fetch->aborted)
		{
			if(fetch->fh)
			{
				ULONG len;

				len = FRead(fetch->fh,ami_file_fetcher_buffer,1,1024);

				LOG(("fetch %lx read %ld",fetch,len));

				ami_fetch_file_send_callback(FETCH_DATA, 
					fetch,ami_file_fetcher_buffer,len);

				if((len<1024) && (!fetch->aborted))
				{
					ami_fetch_file_send_callback(FETCH_FINISHED,
						fetch, &fetch->cachedata, 0);

					fetch->aborted = true;
				}
			}
			else
			{
				fetch->fh = FOpen(fetch->path,MODE_OLDFILE,0);

				if(fetch->fh)
				{
					struct ExamineData *fib;
					if(fib = ExamineObjectTags(EX_FileHandleInput,fetch->fh,TAG_DONE))
					{
						fetch->len = fib->FileSize;
						FreeDosObject(DOS_EXAMINEDATA,fib);
					}

					fetch_set_http_code(fetch->fetch_handle,200);
					fetch->mimetype = fetch_mimetype(fetch->path);
					LOG(("mimetype %s len %ld",fetch->mimetype,fetch->len));

					ami_fetch_file_send_callback(FETCH_TYPE,
						fetch, fetch->mimetype, (ULONG)fetch->len);
				}
				else
				{
					STRPTR errorstring;

					errorstring = ASPrintf("%s %s",messages_get("FileError"),fetch->path);
					fetch_set_http_code(fetch->fetch_handle,404);
					ami_fetch_file_send_callback(FETCH_ERROR, fetch,
						errorstring, 0);
					fetch->aborted = true;
					FreeVec(errorstring);
				}
			}
		}

		if(fetch && fetch->aborted)
		{
			fetch_remove_from_queues(fetch->fetch_handle);
			fetch_free(fetch->fetch_handle);
		}
	}while(node=nnode);
}