summaryrefslogtreecommitdiff
path: root/content/fetch.c
blob: b42e48ea73ed7ca152bf10a82e8f16c1a0b1bf8c (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2004 James Bursa <bursa@users.sourceforge.net>
 * Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
 */

/** \file
 * Fetching of data from a URL (implementation).
 *
 * This implementation uses libcurl's 'multi' interface.
 *
 * Active fetches are held in the linked list fetch_list. There may be at most
 * one fetch in progress from each host. Any further fetches are queued until
 * the previous one ends.
 *
 * Invariant: only the fetch at the head of each queue is in progress, ie.
 *        queue_prev == 0  <=>  curl_handle != 0
 *   and  queue_prev != 0  <=>  curl_handle == 0.
 */

#include <assert.h>
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#ifdef riscos
#include <unixlib/local.h>
#endif
#include "curl/curl.h"
#include "netsurf/utils/config.h"
#include "netsurf/content/fetch.h"
#include "netsurf/desktop/options.h"
#ifdef WITH_AUTH
#include "netsurf/desktop/401login.h"
#endif
#ifdef WITH_POST
#include "netsurf/render/form.h"
#endif
#include "netsurf/utils/log.h"
#include "netsurf/utils/messages.h"
#include "netsurf/utils/url.h"
#include "netsurf/utils/utils.h"


bool fetch_active;	/**< Fetches in progress, please call fetch_poll(). */

/** Information for a single fetch. */
struct fetch {
	CURL * curl_handle;	/**< cURL handle if being fetched, or 0. */
	void (*callback)(fetch_msg msg, void *p, char *data, unsigned long size);
				/**< Callback function. */
	bool had_headers;	/**< Headers have been processed. */
	int locked;		/**< Lock count. */
	bool aborting;		/**< Abort requested in callback. */
	bool stopped;		/**< Download stopped on purpose. */
	bool only_2xx;		/**< Only HTTP 2xx responses acceptable. */
	bool cookies;		/**< Send & accept cookies. */
	char *url;		/**< URL. */
	char *referer;		/**< URL for Referer header. */
	void *p;		/**< Private data for callback. */
	struct curl_slist *headers;	/**< List of request headers. */
	char *host;		/**< Host part of URL. */
	char *location;		/**< Response Location header, or 0. */
	unsigned long content_length;	/**< Response Content-Length, or 0. */
	char *realm;            /**< HTTP Auth Realm */
	char *post_urlenc;	/**< Url encoded POST string, or 0. */
	struct curl_httppost *post_multipart;	/**< Multipart post data, or 0. */
	struct fetch *queue_prev;	/**< Previous fetch for this host. */
	struct fetch *queue_next;	/**< Next fetch for this host. */
	struct fetch *prev;	/**< Previous active fetch in ::fetch_list. */
	struct fetch *next;	/**< Next active fetch in ::fetch_list. */
};

static const char * const user_agent = "NetSurf";
static CURLM *curl_multi;		/**< Global cURL multi handle. */
/** Curl handle with default options set; not used for transfers. */
static CURL *fetch_blank_curl;
static struct fetch *fetch_list = 0;	/**< List of active fetches. */
static char fetch_error_buffer[CURL_ERROR_SIZE]; /**< Error buffer for cURL. */

static CURLcode fetch_set_options(struct fetch *f);
static void fetch_free(struct fetch *f);
static void fetch_done(CURL *curl_handle, CURLcode result);
static size_t fetch_curl_data(void * data, size_t size, size_t nmemb, struct fetch *f);
static size_t fetch_curl_header(char * data, size_t size, size_t nmemb, struct fetch *f);
static bool fetch_process_headers(struct fetch *f);
#ifdef WITH_POST
static struct curl_httppost *fetch_post_convert(struct form_successful_control *control);
#endif

#ifdef riscos
static char * ca_bundle;	/**< SSL certificate bundle filename. */
extern const char * const NETSURF_DIR;
#endif


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

void fetch_init(void)
{
	CURLcode code;

	code = curl_global_init(CURL_GLOBAL_ALL);
	if (code != CURLE_OK)
		die("Failed to initialise the fetch module "
				"(curl_global_init failed).");

	curl_multi = curl_multi_init();
	if (!curl_multi)
		die("Failed to initialise the fetch module "
				"(curl_multi_init failed).");

#ifdef riscos
	ca_bundle = malloc(strlen(NETSURF_DIR) + 40);
	if (!ca_bundle)
		die("NoMemory");
	sprintf(ca_bundle, "%s.Resources.ca-bundle", NETSURF_DIR);
#endif

	/* Create a curl easy handle with the options that are common to all
	   fetches. */
	fetch_blank_curl = curl_easy_init();
	if (!fetch_blank_curl)
		die("Failed to initialise the fetch module "
				"(curl_easy_init failed).");

#define SETOPT(option, value) \
	code = curl_easy_setopt(fetch_blank_curl, option, value);	\
	if (code != CURLE_OK)						\
		goto curl_easy_setopt_failed;

	SETOPT(CURLOPT_VERBOSE, 1);
	SETOPT(CURLOPT_ERRORBUFFER, fetch_error_buffer);
	SETOPT(CURLOPT_WRITEFUNCTION, fetch_curl_data);
	SETOPT(CURLOPT_HEADERFUNCTION, fetch_curl_header);
	SETOPT(CURLOPT_USERAGENT, user_agent);
	SETOPT(CURLOPT_ENCODING, "gzip");
	SETOPT(CURLOPT_LOW_SPEED_LIMIT, 1L);
	SETOPT(CURLOPT_LOW_SPEED_TIME, 60L);
	SETOPT(CURLOPT_NOSIGNAL, 1L);
	SETOPT(CURLOPT_CONNECTTIMEOUT, 60L);
#ifdef riscos
	SETOPT(CURLOPT_CAINFO, ca_bundle);
#endif

	if (!option_ssl_verify_certificates) {
        	/* disable verification of SSL certificates.
	         * security? we've heard of it...
	         */
        	SETOPT(CURLOPT_SSL_VERIFYPEER, 0L);
	        SETOPT(CURLOPT_SSL_VERIFYHOST, 0L);
	}

	return;

curl_easy_setopt_failed:
	die("Failed to initialise the fetch module "
			"(curl_easy_setopt failed).");
}


/**
 * Clean up for quit.
 *
 * Must be called before exiting.
 */

void fetch_quit(void)
{
	CURLMcode codem;

	curl_easy_cleanup(fetch_blank_curl);

	codem = curl_multi_cleanup(curl_multi);
	if (codem != CURLM_OK)
		LOG(("curl_multi_cleanup failed: ignoring"));

	curl_global_cleanup();
}


/**
 * 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 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.
 */

struct fetch * fetch_start(char *url, char *referer,
		void (*callback)(fetch_msg msg, void *p, char *data,
				unsigned long size),
		void *p, bool only_2xx, char *post_urlenc,
		struct form_successful_control *post_multipart, bool cookies)
{
	char *host;
	struct fetch *fetch;
	struct fetch *host_fetch;
	CURLcode code;
	CURLMcode codem;
	struct curl_slist *slist;

	fetch = malloc(sizeof (*fetch));
	if (!fetch)
		return 0;

	host = url_host(url);

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

	/* construct a new fetch structure */
	fetch->curl_handle = 0;
	fetch->callback = callback;
	fetch->had_headers = false;
	fetch->locked = 0;
	fetch->aborting = false;
	fetch->stopped = false;
	fetch->only_2xx = only_2xx;
	fetch->cookies = cookies;
	fetch->url = strdup(url);
	fetch->referer = 0;
	if (referer)
		fetch->referer = strdup(referer);
	fetch->p = p;
	fetch->headers = 0;
	fetch->host = host;
	fetch->location = 0;
	fetch->content_length = 0;
	fetch->realm = 0;
	fetch->post_urlenc = 0;
	fetch->post_multipart = 0;
	if (post_urlenc)
		fetch->post_urlenc = strdup(post_urlenc);
	else if (post_multipart)
		fetch->post_multipart = fetch_post_convert(post_multipart);
	fetch->queue_prev = 0;
	fetch->queue_next = 0;
	fetch->prev = 0;
	fetch->next = 0;

	if (!fetch->url || (referer && !fetch->referer) ||
			(post_urlenc && !fetch->post_urlenc) ||
			(post_multipart && !fetch->post_multipart))
		goto failed;

#define APPEND(list, value) \
	slist = curl_slist_append(list, value);		\
	if (!slist)					\
		goto failed;				\
	list = slist;

	/* remove curl default headers */
	APPEND(fetch->headers, "Accept:");
	APPEND(fetch->headers, "Pragma:");
	if (option_accept_language) {
		char s[80];
		snprintf(s, sizeof s, "Accept-Language: %s, *;q=0.1",
				option_accept_language);
		s[sizeof s - 1] = 0;
		APPEND(fetch->headers, s);
	}

	/* look for a fetch from the same host */
	if (host) {
		for (host_fetch = fetch_list;
				host_fetch && (host_fetch->host == 0 ||
				strcasecmp(host_fetch->host, host) != 0);
				host_fetch = host_fetch->next)
			;
		if (host_fetch) {
			/* fetch from this host in progress:
			   queue the new fetch */
			LOG(("queueing"));
			fetch->curl_handle = 0;
			/* queue at end */
			for (; host_fetch->queue_next;
					host_fetch = host_fetch->queue_next)
				;
			fetch->queue_prev = host_fetch;
			host_fetch->queue_next = fetch;
			return fetch;
		}
	}

	fetch->next = fetch_list;
	if (fetch_list != 0)
		fetch_list->prev = fetch;
	fetch_list = fetch;
	fetch_active = true;

	/* create the curl easy handle */
	fetch->curl_handle = curl_easy_duphandle(fetch_blank_curl);
	if (!fetch->curl_handle)
		goto failed;

	code = fetch_set_options(fetch);
	if (code != CURLE_OK)
		goto failed;

	/* add to the global curl multi handle */
	codem = curl_multi_add_handle(curl_multi, fetch->curl_handle);
	assert(codem == CURLM_OK || codem == CURLM_CALL_MULTI_PERFORM);

	return fetch;

failed:
	free(host);
	free(fetch->url);
	free(fetch->referer);
	free(fetch->post_urlenc);
	if (fetch->post_multipart)
		curl_formfree(fetch->post_multipart);
	curl_slist_free_all(fetch->headers);
	free(fetch);
	return 0;
}


/**
 * Set options specific for a fetch.
 */

CURLcode fetch_set_options(struct fetch *f)
{
	CURLcode code;
	struct login *li;

#undef SETOPT
#define SETOPT(option, value) \
	code = curl_easy_setopt(f->curl_handle, option, value);	\
	if (code != CURLE_OK)					\
		return code;

	SETOPT(CURLOPT_URL, f->url);
	SETOPT(CURLOPT_PRIVATE, f);
	SETOPT(CURLOPT_WRITEDATA, f);
	SETOPT(CURLOPT_WRITEHEADER, f);
	SETOPT(CURLOPT_REFERER, f->referer);
	SETOPT(CURLOPT_HTTPHEADER, f->headers);
	if (f->post_urlenc) {
		SETOPT(CURLOPT_POSTFIELDS, f->post_urlenc);
	} else if (f->post_multipart) {
		SETOPT(CURLOPT_HTTPPOST, f->post_multipart);
	} else {
		SETOPT(CURLOPT_HTTPGET, 1L);
	}
	if (f->cookies) {
		SETOPT(CURLOPT_COOKIEFILE, messages_get("cookiefile"));
		SETOPT(CURLOPT_COOKIEJAR, messages_get("cookiejar"));
	} else {
		SETOPT(CURLOPT_COOKIEFILE, 0);
		SETOPT(CURLOPT_COOKIEJAR, 0);
	}
	if ((li = login_list_get(f->url))) {
		SETOPT(CURLOPT_HTTPAUTH, CURLAUTH_ANY);
		SETOPT(CURLOPT_USERPWD, li->logindetails);
	} else {
		SETOPT(CURLOPT_USERPWD, 0);
	}
	if (option_http_proxy && option_http_proxy_host) {
		SETOPT(CURLOPT_PROXY, option_http_proxy_host);
		SETOPT(CURLOPT_PROXYPORT, (long) option_http_proxy_port);
	}

	return CURLE_OK;
}


/**
 * Stop a fetch.
 */

void fetch_abort(struct fetch *f)
{
	CURLcode code;
	CURLMcode codem;
	struct fetch *fetch;
	struct fetch *next_fetch;

	assert(f != 0);
	LOG(("fetch %p, url '%s'", f, f->url));

	if (f->locked) {
		LOG(("locked: will abort later"));
		f->aborting = true;
		return;
	}

	/* remove from list of fetches */
	if (f->prev == 0)
		fetch_list = f->next;
	else
		f->prev->next = f->next;
	if (f->next != 0)
		f->next->prev = f->prev;

	/* remove from curl multi handle */
	if (f->curl_handle) {
		codem = curl_multi_remove_handle(curl_multi, f->curl_handle);
		assert(codem == CURLM_OK);
	}

	if (f->curl_handle && f->queue_next) {
		/* start a queued fetch for this host, reusing the handle */
		fetch = f->queue_next;

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

		fetch->curl_handle = f->curl_handle;
		f->curl_handle = 0;
		code = fetch_set_options(fetch);
		if (code == CURLE_OK)
			/* add to the global curl multi handle */
			codem = curl_multi_add_handle(curl_multi,
					fetch->curl_handle);

		if (code == CURLE_OK && (codem == CURLM_OK ||
				codem == CURLM_CALL_MULTI_PERFORM)) {
			/* add to list of fetches */
			fetch->prev = 0;
			fetch->next = fetch_list;
			if (fetch_list != 0)
				fetch_list->prev = fetch;
			fetch_list = fetch;
			fetch->queue_prev = 0;
		} else {
			/* destroy all queued fetches for this host */
			do {
				fetch->locked++;
				fetch->callback(FETCH_ERROR, fetch->p,
						(char*)messages_get("FetchError"), 0);
				fetch->locked--;
				next_fetch = fetch->queue_next;
				fetch_free(fetch);
				fetch = next_fetch;
			} while (fetch);
		}

	} else {
		if (f->queue_prev)
			f->queue_prev->queue_next = f->queue_next;
		if (f->queue_next)
			f->queue_next->queue_prev = f->queue_prev;
	}

	fetch_free(f);
}


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

void fetch_free(struct fetch *f)
{
	if (f->curl_handle)
		curl_easy_cleanup(f->curl_handle);
	free(f->url);
	free(f->host);
	free(f->referer);
	free(f->location);
	free(f->realm);
	if (f->headers)
		curl_slist_free_all(f->headers);
	free(f->post_urlenc);
	if (f->post_multipart)
		curl_formfree(f->post_multipart);
	free(f);
}


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

void fetch_poll(void)
{
	int running, queue;
	CURLMcode codem;
	CURLMsg *curl_msg;

	/* do any possible work on the current fetches */
	do {
		codem = curl_multi_perform(curl_multi, &running);
		assert(codem == CURLM_OK || codem == CURLM_CALL_MULTI_PERFORM);
	} while (codem == CURLM_CALL_MULTI_PERFORM);

	/* process curl results */
	curl_msg = curl_multi_info_read(curl_multi, &queue);
	while (curl_msg) {
		switch (curl_msg->msg) {
			case CURLMSG_DONE:
				fetch_done(curl_msg->easy_handle,
						curl_msg->data.result);
				break;
			default:
				break;
		}
		curl_msg = curl_multi_info_read(curl_multi, &queue);
	}

	if (!fetch_list)
		fetch_active = false;
}


/**
 * Handle a completed fetch (CURLMSG_DONE from curl_multi_info_read()).
 *
 * \param  curl_handle  curl easy handle of fetch
 */

void fetch_done(CURL *curl_handle, CURLcode result)
{
	bool finished = false;
	bool error = false;
	struct fetch *f;
	void *p;
	void (*callback)(fetch_msg msg, void *p, char *data,
			unsigned long size);
	CURLcode code;

	/* find the structure associated with this fetch */
	code = curl_easy_getinfo(curl_handle, CURLINFO_PRIVATE, &f);
	assert(code == CURLE_OK);

	callback = f->callback;
	p = f->p;

	if (result == CURLE_OK) {
		/* fetch completed normally */
		if (!f->had_headers && fetch_process_headers(f))
			; /* redirect with no body or similar */
		else
			finished = true;
	} else if (result == CURLE_WRITE_ERROR && f->stopped)
		/* CURLE_WRITE_ERROR occurs when fetch_curl_data
		 * returns 0, which we use to abort intentionally */
		;
	else
		error = true;

	/* clean up fetch */
	fetch_abort(f);

	/* postponed until after abort so that queue fetches are started */
	if (finished)
		callback(FETCH_FINISHED, p, 0, 0);
	else if (error)
		callback(FETCH_ERROR, p, fetch_error_buffer, 0);
}


/**
 * Callback function for cURL.
 */

size_t fetch_curl_data(void * data, size_t size, size_t nmemb, struct fetch *f)
{
	f->locked++;

	LOG(("fetch %p, size %u", f, size * nmemb));

	if (!f->had_headers && fetch_process_headers(f)) {
		f->locked--;
		f->stopped = true;
		return 0;
	}

	/* send data to the caller */
	LOG(("FETCH_DATA"));
	f->callback(FETCH_DATA, f->p, data, size * nmemb);
	if (f->aborting) {
		f->locked--;
		f->stopped = true;
		return 0;
	}

	f->locked--;
	return size * nmemb;
}


/**
 * Callback function for headers.
 */

size_t fetch_curl_header(char * data, size_t size, size_t nmemb, struct fetch *f)
{
	int i;
	size *= nmemb;
	if (12 < size && strncasecmp(data, "Location:", 9) == 0) {
		/* extract Location header */
		free(f->location);
		f->location = malloc(size);
		if (!f->location) {
			LOG(("malloc failed"));
			return size;
		}
		for (i = 9; i < (int)size && (data[i] == ' ' || data[i] == '\t'); i++)
			/* */;
		strncpy(f->location, data + i, size - i);
		f->location[size - i] = '\0';
		for (i = size - i - 1; i >= 0 &&
				(f->location[i] == ' ' ||
				f->location[i] == '\t' ||
				f->location[i] == '\r' ||
				f->location[i] == '\n'); i--)
			f->location[i] = '\0';
	} else if (15 < size && strncasecmp(data, "Content-Length:", 15) == 0) {
		/* extract Content-Length header */
		for (i = 15; i < (int)size && (data[i] == ' ' || data[i] == '\t'); i++)
			/* */;
		if ('0' <= data[i] && data[i] <= '9')
			f->content_length = atol(data + i);
#ifdef WITH_AUTH
	} else if (16 < size && strncasecmp(data, "WWW-Authenticate", 16) == 0) {
		/* extract the first Realm from WWW-Authenticate header */
		free(f->realm);
		f->realm = malloc(size);
		if (!f->realm) {
			LOG(("malloc failed"));
			return size;
		}
		for (i = 16; i < (int)size && data[i] != '='; i++)
			/* */;
		while (i < (int)size && data[++i] == '"')
			/* */;
		strncpy(f->realm, data + i, size - i);
		f->realm[size - i] = '\0';
		for (i = size - i - 1; i >= 0 &&
				(f->realm[i] == ' ' ||
				f->realm[i] == '"' ||
				f->realm[i] == '\t' ||
				f->realm[i] == '\r' ||
				f->realm[i] == '\n'); --i)
			f->realm[i] = '\0';
#endif
	}
	return size;
}


/**
 * Find the status code and content type and inform the caller.
 *
 * Return true if the fetch is being aborted.
 */

bool fetch_process_headers(struct fetch *f)
{
	long http_code;
	const char *type;
	CURLcode code;

	f->locked++;
	f->had_headers = true;

	code = curl_easy_getinfo(f->curl_handle, CURLINFO_HTTP_CODE, &http_code);
	assert(code == CURLE_OK);
	LOG(("HTTP status code %li", http_code));

	/* handle HTTP redirects (3xx response codes) */
	if (300 <= http_code && http_code < 400 && f->location != 0) {
		LOG(("FETCH_REDIRECT, '%s'", f->location));
		f->callback(FETCH_REDIRECT, f->p, f->location, 0);
		f->locked--;
		return true;
	}

        /* handle HTTP 401 (Authentication errors) */
#ifdef WITH_AUTH
	if (http_code == 401) {
		f->callback(FETCH_AUTH, f->p, f->realm,0);
		f->locked--;
		return true;
	}
#endif

	/* handle HTTP errors (non 2xx response codes) */
	if (f->only_2xx && strncmp(f->url, "http", 4) == 0 &&
			(http_code < 200 || 299 < http_code)) {
		f->callback(FETCH_ERROR, f->p, (char*)messages_get("Not2xx"), 0);
		f->locked--;
		return true;
	}

	/* find MIME type from headers or filetype for local files */
	code = curl_easy_getinfo(f->curl_handle, CURLINFO_CONTENT_TYPE, &type);
	assert(code == CURLE_OK);

	if (type == 0) {
		type = "text/html";
		if (strncmp(f->url, "file:///", 8) == 0) {
			char *url_path;
			url_path = curl_unescape(f->url + 8, (int) strlen(f->url) - 8);
			type = fetch_filetype(url_path);
			curl_free(url_path);
		} else if (strncmp(f->url, "file:/", 6) == 0) {
			char *url_path;
			url_path = curl_unescape(f->url + 6, (int) strlen(f->url) - 6);
			type = fetch_filetype(url_path);
			curl_free(url_path);
		}
	}

	LOG(("FETCH_TYPE, '%s'", type));
	f->callback(FETCH_TYPE, f->p, (char*)type, f->content_length);
	f->locked--;
	if (f->aborting)
		return true;

	return false;
}


/**
 * Convert a list of struct ::form_successful_control to a list of
 * struct curl_httppost for libcurl.
 */
#ifdef WITH_POST
struct curl_httppost *fetch_post_convert(struct form_successful_control *control)
{
	struct curl_httppost *post = 0, *last = 0;
	char *mimetype = 0;
	char *leafname = 0, *temp = 0;

	for (; control; control = control->next) {
		if (control->file) {
			mimetype = fetch_mimetype(control->value);
#ifdef riscos
			temp = strrchr(control->value, '.');
			if (!temp)
				temp = control->value; /* already leafname */
			else
				temp += 1;
			leafname = calloc(strlen(temp)+5, sizeof(char));
			if (!leafname) {
				LOG(("calloc failed"));
				free(mimetype);
				continue;
			}
			__unixify_std(temp, leafname, strlen(temp), 0xfff);
#else
			leafname = strrchr(control->value, '/') ;
			if (!leafname)
				leafname = control->value;
			else
				leafname += 1;
#endif
			curl_formadd(&post, &last,
					CURLFORM_COPYNAME, control->name,
					CURLFORM_FILE, control->value,
					CURLFORM_FILENAME, leafname,
					CURLFORM_CONTENTTYPE,
					(mimetype != 0 ? mimetype : "text/plain"),
					CURLFORM_END);
#ifdef riscos
			free(leafname);
#endif
			free(mimetype);
		}
		else {
			curl_formadd(&post, &last,
					CURLFORM_COPYNAME, control->name,
					CURLFORM_COPYCONTENTS, control->value,
					CURLFORM_END);
		}
	}

	return post;
}
#endif


/**
 * 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 char *url)
{
	unsigned int i;
	const char *semi;
	unsigned int len;
	curl_version_info_data *data;

	semi = strchr(url, ':');
	if (!semi)
		return false;
	len = semi - url;

	data = curl_version_info(CURLVERSION_NOW);

	for (i = 0; data->protocols[i]; i++)
		if (strlen(data->protocols[i]) == len &&
				strncasecmp(url, data->protocols[i], len) == 0)
			return true;

	return false;
}


/**
 * testing framework
 */

#ifdef TEST
#include <unistd.h>

struct test {char *url; struct fetch *f;};

void callback(fetch_msg msg, struct test *t, char *data, unsigned long size)
{
	printf("%s: ", t->url);
	switch (msg) {
		case FETCH_TYPE:
			printf("FETCH_TYPE '%s'", data);
			break;
		case FETCH_DATA:
			printf("FETCH_DATA %lu", size);
			break;
		case FETCH_FINISHED:
			printf("FETCH_FINISHED");
			break;
		case FETCH_ERROR:
			printf("FETCH_ERROR '%s'", data);
			break;
		default:
			assert(0);
	}
	printf("\n");
}

struct test test[] = {
	{"http://127.0.0.1/", 0},
	{"http://netsurf.strcprstskrzkrk.co.uk/", 0},
	{"http://www.oxfordstudent.com/", 0},
	{"http://www.google.co.uk/", 0},
	{"http://news.bbc.co.uk/", 0},
	{"http://doesnt.exist/", 0},
	{"blah://blah", 0},
};

int main(void)
{
	int i;
	fetch_init();
	for (i = 0; i != sizeof(test) / sizeof(test[0]); i++)
		test[i].f = fetch_start(test[i].url, 0, callback, &test[i]);
	while (1) {
		fetch_poll();
		sleep(1);
	}
	return 0;
}
#endif