summaryrefslogtreecommitdiff
path: root/riscos/save_complete.c
blob: 0738f2240b9bbd55f52aef253b09806f3ec1932c (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
/*
 * 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 John M Bell <jmb202@ecs.soton.ac.uk>
 */

#include <assert.h>
#include <string.h>
#include <unixlib/local.h> /* for __riscosify */

#include "libxml/HTMLtree.h"

#include "oslib/osfile.h"
#include "netsurf/utils/config.h"
#include "netsurf/content/content.h"
#include "netsurf/css/css.h"
#include "netsurf/render/form.h"
#include "netsurf/render/layout.h"
#include "netsurf/riscos/gui.h"
#include "netsurf/riscos/save_complete.h"
#include "netsurf/utils/log.h"
#include "netsurf/utils/url.h"
#include "netsurf/utils/utils.h"

#ifdef WITH_SAVE_COMPLETE

/** \todo URL rewriting for @import rules
 *        Objects used by embedded html pages
 *        GUI
 */

struct url_entry {
        char *url;   /**< Fully qualified URL, as per url_join output */
        char *par;   /**< Base URL of parent object */
        int  ptr;    /**< Pointer to object's location in memory */
        struct url_entry *next; /**< Next entry in list */
};

static void save_imported_sheets(struct content *c, char *p, char* fn,
                          struct url_entry *imports);
/*static char *leafname(const char *url);
static int rewrite_stylesheet_urls(const char* sheet, int isize, char* buffer,
                            int osize, struct url_entry *head);*/
static int rewrite_document_urls(xmlDoc *doc, struct url_entry *head, char *fname);
static int rewrite_urls(xmlNode *n, struct url_entry *head, char *fname);
static void rewrite_url(xmlNode *n, struct url_entry *head, char *fname,
                        const char *attr);

/* this is temporary. */
const char * const SAVE_PATH = "<NetSurf$Dir>.savetest.";
const char * const OBJ_DIR = "_files";

/** \todo this will probably want to take a filename */
void save_complete(struct content *c) {

	char *fname = 0, *spath;
	unsigned int i;
	struct url_entry urls = {0, 0, 0, 0}; /* sentinel at head */
	struct url_entry *object;
	xmlDoc *toSave;

	if (c->type != CONTENT_HTML)
		return;

	fname = "test";  /*get_filename(c->data.html.base_url);*/

	spath = xcalloc(strlen(SAVE_PATH)+strlen(OBJ_DIR)+strlen(fname)+50,
			sizeof(char));

	sprintf(spath, "%s%s%s", SAVE_PATH, fname, OBJ_DIR);
	xosfile_create_dir(spath, 77);

        /* save stylesheets, ignoring the base sheet and <style> elements */
        for (i = 2; i != c->data.html.stylesheet_count; i++) {
		struct content *css = c->data.html.stylesheet_content[i];
		struct url_entry imports = {0, 0, 0, 0};
		//char *source;
		//int source_len;

                if (!css)
                        continue;

                save_imported_sheets(css, spath, fname, &imports);

                /*source = xcalloc(css->source_size+100, sizeof(char));
                source_len = rewrite_stylesheet_urls(css->source_data,
                                                     css->source_size,
                                                     source,
                                                     css->source_size+100,
                                                     &imports);*/

                sprintf(spath, "%s%s%s.%p", SAVE_PATH, fname, OBJ_DIR, css);
                /*if (source_len > 0) {
                xosfile_save_stamped(spath, 0xf79, source,
                                     source + source_len);
		}
		xfree(source);*/
		xosfile_save_stamped(spath, 0xf79, css->source_data,
		                     css->source_data + css->source_size);

		/* Now add the url of this sheet to the list
		 * of objects imported by the parent page
		 */
		object = xcalloc(1, sizeof(struct url_entry));
		object->url = css->url;
		object->par = url_normalize(c->data.html.base_url);
		object->ptr = (int)css;
		object->next = urls.next;
		urls.next = object;
        }

	/* save objects */
	for (i = 0; i != c->data.html.object_count; i++) {
		struct content *obj = c->data.html.object[i].content;

		/* skip difficult content types */
		if (!obj || obj->type >= CONTENT_PLUGIN) {
			continue;
		}

		sprintf(spath, "%s%s%s.%p", SAVE_PATH, fname, OBJ_DIR, c->data.html.object[i].content);

		xosfile_save_stamped(spath,
				ro_content_filetype(obj),
				obj->source_data,
				obj->source_data + obj->source_size);

		/* Add to list, as for stylesheets */
		object = xcalloc(1, sizeof(struct url_entry));
		object->url = obj->url;
		object->par = url_normalize(c->data.html.base_url);
		object->ptr = (int)obj;
		object->next = urls.next;
		urls.next = object;
	}

	/* make a copy of the document tree */
	toSave = xmlCopyDoc(c->data.html.document, 1);

	if (!toSave) {
	        xfree(spath);
	        return;
	}

	/* rewrite all urls we know about */
       	if (rewrite_document_urls(toSave, &urls, fname) == 0) {
       	        xfree(spath);
       	        xmlFreeDoc(toSave);
       	        return;
       	}

	/* save the html file out last of all */
	sprintf(spath, "%s%s", SAVE_PATH, fname);
	htmlSaveFile(spath, toSave);
	xosfile_set_type(spath, 0xfaf);

        xmlFreeDoc(toSave);
	xfree(spath);
	//xfree(fname);
}

void save_imported_sheets(struct content *c, char *p, char *fn,
                          struct url_entry *imports)
{
        unsigned int j;
        //struct url_entry *this;
        //char *source;
        //int source_len;

        for (j = 0; j != c->data.css.import_count; j++) {
		struct content *css = c->data.css.import_content[j];
	        struct url_entry imp = {0, 0, 0, 0};

                if (!css)
                        continue;

                save_imported_sheets(css, p, fn, &imp);

                /*source = xcalloc(css->source_size+100, sizeof(char));
                source_len = rewrite_stylesheet_urls(css->source_data,
                                                     css->source_size,
                                                     source,
                                                     css->source_size+100,
                                                     &imp);*/

                sprintf(p, "%s%s%s.%p", SAVE_PATH, fn, OBJ_DIR, css);
                /*if (source_len > 0) {
                xosfile_save_stamped(p, 0xf79, source, source + source_len);
                }
                xfree(source);*/
                xosfile_save_stamped(p, 0xf79, css->source_data,
                                     css->source_data + css->source_size);

		/* now add the url of this sheet to the list of
		 * sheets imported by the parent sheet.
		 */
		/*this = xcalloc(1, sizeof(struct url_entry));
		this->url = css->url;
		this->par = url_normalize(c->url);
		this->ptr = (int)css;
		this->next = imports->next;
		imports->next = this;*/
        }
}

#if 0

char *leafname(const char *url)
{
        char *res, *temp;

        /* the input URL is that produced by url_join,
         * therefore, we can assume that this will work.
         */
        temp = strrchr(url, '/');

        if ((temp - url) == (int)(strlen(url)-1)) { /* root dir */
                res = xstrdup("index/html");
                return res;
        }

        temp += 1;
        res = xcalloc(strlen(temp), sizeof(char));
        if (__riscosify_std(temp, 0, res, strlen(temp), 0)) {
                return res;
        }

        return NULL;
}

/**
 * Rewrite stylesheet @import rules to use relative urls.
 *
 * @param sheet The source of the stylesheet
 * @param isize The size of the input buffer
 * @param buffer The buffer into which to write the modified sheet
 * @param osize The size of the output buffer
 * @param head Pointer to the head of the list containing imported urls
 * @return The length of the output buffer, or 0 on error.
 */
int rewrite_stylesheet_urls(const char *sheet, int isize, char *buffer,
                            int osize, struct url_entry *head)
{
        struct url_entry *item, *next;
        char *rule, *input = sheet, *temp, *end;
        int out_size = 0, out = 0;

        assert(head);

        while (input < sheet+isize) {
                /* find next occurrence of @import in input buffer */
                rule = strstr(input, "@import");

                if (!rule) {
                        if (out_size + ((sheet+isize)-input) > osize) {
                                /* not enough space in buffer -> exit */
                                return 0;
                        }
                        memcpy(buffer, input, ((sheet+isize)-input));
                        out_size += ((sheet+isize)-input);
                        break;
                }

                /* find end of this rule */
                end = strchr(rule, ';');
                if (!end) { /* rule not closed - give up */
                        if (out_size + ((sheet+isize)-input) > osize) {
                                /* not enough space in buffer -> exit */
                                return 0;
                        }
                        memcpy(buffer, input, ((sheet+isize)-input));
                        out_size += ((sheet+isize)-input);
                        break;
                }

                /* skip until after first set of double quotes */
                temp = strchr(rule, '"');
                if (!temp) { /* no quotes - try parentheses */
                        temp = strchr(rule, '(');
                        if (!temp) { /* no parentheses - give up */
                                if (out_size + (rule-input+1) > osize) {
                                /* not enough space in buffer -> exit */
                                        return 0;
                                }
                                memcpy(buffer, input, (rule-input+1));
                                buffer += rule-input+1;
                                out_size += rule-input+1;
                                input = rule + 1;
                                continue;
                        }
                }
                /* check we haven't gone past the end */
                if (temp > end && *temp == '(') { /* tested both */
                        if (out_size + (end-input+1) > osize) {
                               /* not enough space in buffer -> exit */
                                return 0;
                        }
                        memcpy(buffer, input, (end-input+1));
                        buffer += end-input+1;
                        out_size += end-input+1;
                        input = end + 1;
                        continue;
                }
                else if (temp > end) { /* not done parentheses yet */
                        temp = strchr(rule, '(');
                        if (!temp) { /* no parentheses - give up */
                                if (out_size + (rule-input+1) > osize) {
                                /* not enough space in buffer -> exit */
                                        return 0;
                                }
                                memcpy(buffer, input, (rule-input+1));
                                buffer += rule-input+1;
                                out_size += rule-input+1;
                                input = rule + 1;
                                continue;
                        }
                }
                if (temp > end) {
                        if (out_size + (end-input+1) > osize) {
                               /* not enough space in buffer -> exit */
                                return 0;
                        }
                        memcpy(buffer, input, (end-input+1));
                        buffer += end-input+1;
                        out_size += end-input+1;
                        input = end + 1;
                        continue;
                }
                rule = temp + 1;

                /* pointer to end of url */
                temp = strchr(rule, '"');
                if (!temp) {
                        temp = strchr(rule, ')');
                        if (!temp) {
                                if (out_size + (rule-input+1) > osize) {
                                /* not enough space in buffer -> exit */
                                        return 0;
                                }
                                memcpy(buffer, input, (rule-input+1));
                                buffer += rule-input+1;
                                out_size += rule-input+1;
                                input = rule + 1;
                                continue;
                        }
                }
                /* check we haven't gone past the end */
                if (temp > end && *temp == ')') { /* tested both */
                        if (out_size + (end-input+1) > osize) {
                               /* not enough space in buffer -> exit */
                                return 0;
                        }
                        memcpy(buffer, input, (end-input+1));
                        buffer += end-input+1;
                        out_size += end-input+1;
                        input = end + 1;
                        continue;
                }
                else if (temp > end) { /* not done parentheses yet */
                        temp = strchr(rule, ')');
                        if (!temp) { /* no parentheses - give up */
                                if (out_size + (rule-input+1) > osize) {
                                /* not enough space in buffer -> exit */
                                        return 0;
                                }
                                memcpy(buffer, input, (rule-input+1));
                                buffer += rule-input+1;
                                out_size += rule-input+1;
                                input = rule + 1;
                                continue;
                        }
                }
                if (temp > end) {
                        if (out_size + (end-input+1) > osize) {
                               /* not enough space in buffer -> exit */
                                return 0;
                        }
                        memcpy(buffer, input, (end-input+1));
                        buffer += end-input+1;
                        out_size += end-input+1;
                        input = end + 1;
                        continue;
                }
                end = temp;

                /* copy input up to @import rule to output buffer */
                if (out_size + (rule-input) > osize) {
                        /* not enough space in buffer -> exit */
                        return 0;
                }
                memcpy(buffer, input, (out = (rule-input)));
                input = rule;
                buffer += out;
                out_size += out;

                /* copy url into temporary buffer */
                temp = xcalloc((end-rule), sizeof(char));
                strncpy(temp, rule, (end-rule));

                /* iterate over list, looking for url */
                /** \todo make url detection more accurate */
                for (item=head; item->next; item=item->next) {

                        if (strstr(item->next->url, temp) != 0) {
                                /* url found -> rewrite it */
                                int len = 12;
                                char *url = xcalloc(len, sizeof(char));
                                sprintf(url, "./0x%x", item->next->ptr);
                                if (out_size + len > osize) {
                                        return 0;
                                }
                                memcpy(buffer, url, len);
                                xfree(url);
                                out = len;
                                break;
                        }
                }

                if (item->next == 0) {
                        /* url not found -> write temp to buffer */
                        if ((int)(out_size + strlen(temp)) > osize) {
                                return 0;
                        }
                        memcpy(buffer, temp, strlen(temp));
                        out = strlen(temp);
                }

                /* free url */
                xfree(temp);

                input = end;
                buffer += out;
                out_size += out;
        }

        /* free list */
        for (item = head; item->next; item = item->next) {

                next = item->next;
                item->next = item->next->next;
                xfree(next->par);
                xfree(next);

                if (item->next == 0) {
                        break;
                }
        }

        return out_size;
}

#endif

/**
 * Rewrite URLs in a HTML document to be relative
 *
 * @param doc The root of the document tree
 * @param head The head of the list of known URLs
 * @param fname The name of the file to save as
 * @return 0 on error. >0 otherwise
 */
int rewrite_document_urls(xmlDoc *doc, struct url_entry *head, char *fname)
{
        xmlNode *html;
        struct url_entry *item, *next;

        /* find the html element */
        for (html = doc->children;
             html!=0 && html->type != XML_ELEMENT_NODE;
             html = html->next)
                ;
        if (html == 0 || strcmp((const char*)html->name, "html") != 0) {
                return 0;
        }

	rewrite_urls(html, head, fname);

	/* free list */
        for (item = head; item->next; item = item->next) {

                next = item->next;
                item->next = item->next->next;
                xfree(next->par);
                xfree(next);

                if (item->next == 0) {
                        break;
                }
        }

        return 1;
}

/**
 * Traverse tree, rewriting URLs as we go.
 *
 * @param n The root of the tree
 * @param head The head of the list of known URLs
 * @param fname The name of the file to save as
 * @return 0 on error. >0 otherwise
 */
int rewrite_urls(xmlNode *n, struct url_entry *head, char *fname)
{
        xmlNode *this;

        /**
         * We only need to consider the following cases:
         *
         * Attribute:      Elements:
         *
         * 1)   data         <object>
         * 2)   href         <a> <area> <link> <base>
         * 3)   src          <script> <input> <frame> <iframe> <img>
         */
        if (n->type == XML_ELEMENT_NODE) {
                LOG(("%s", n->name));
                /* 1 */
                if (strcmp(n->name, "object") == 0) {
                      rewrite_url(n, head, fname, "data");
                }
                /* 2 */
                else if (strcmp(n->name, "a") == 0 ||
                         strcmp(n->name, "area") == 0 ||
                         strcmp(n->name, "link") == 0 ||
                         strcmp(n->name, "base") == 0) {
                      rewrite_url(n, head, fname, "href");
                }
                /* 3 */
                else if (strcmp(n->name, "frame") == 0 ||
                         strcmp(n->name, "iframe") == 0 ||
                         strcmp(n->name, "input") == 0 ||
                         strcmp(n->name, "img") == 0 ||
                         strcmp(n->name, "script") == 0) {
                      rewrite_url(n, head, fname, "src");
                }
        }
        else {
                return 0;
        }

        /* now recurse */
	for (this = n->children; this != 0; this = this->next) {
                rewrite_urls(this, head, fname);
	}

        return 1;
}

/**
 * Rewrite an URL in a HTML document.
 *
 * @param n The node to modify
 * @param head The head of the list of known URLs
 * @param fname The name of the file to save as
 * @param attr The html attribute to modify
 */
void rewrite_url(xmlNode *n, struct url_entry *head, char *fname,
                 const char *attr)
{
        char *url, *data, *rel;
        struct url_entry *item;
        int len = strlen(fname) + strlen(OBJ_DIR) + 13;

        data = xmlGetProp(n, (const xmlChar*)attr);

        if (!data) return;

        url = url_join(data, head->next->par);
        if (!url) {
                xmlFree(data);
                return;
        }

        for (item=head; item->next; item=item->next) {

                if (strcmp(url, item->next->url) == 0) { /* found a match */
                        rel = xcalloc(len, sizeof(char));
                        sprintf(rel, "./%s%s/0x%x", fname, OBJ_DIR,
                                                           item->next->ptr);
                        xmlSetProp(n, (const xmlChar*)attr,
                                      (const xmlChar*) rel);
                        xfree(rel);
                        break;
                }
        }

        if (item->next == 0) { /* no match found */
                xmlSetProp(n, (const xmlChar*)attr, (const xmlChar*)url);
        }

        xfree(url);
        xmlFree(data);
}

#endif