summaryrefslogtreecommitdiff
path: root/beos/beos_filetype.cpp
blob: f06d63364208ba99d882875db3b702d03dd048af (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 Fran├žois Revol <mmu_man@users.sourceforge.net>
 * Copyright 2007 Rob Kendrick <rjek@netsurf-browser.org>
 * Copyright 2007 Vincent Sanders <vince@debian.org>
 *
 * 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/>.
 */

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <Mime.h>
#include <NodeInfo.h>
#include <String.h>

extern "C" {
#include "content/fetch.h"
#include "utils/log.h"
#include "utils/hashtable.h"
#include "utils/utils.h"
}

#include "beos/beos_filetype.h"

#warning REMOVEME
static struct hash_table *mime_hash = NULL;

void beos_fetch_filetype_init(const char *mimefile)
{
	BMessage mimes;
	status_t err;

	err = BMimeType::GetInstalledTypes(&mimes);
	if (err < B_OK) {
		warn_user("Mime", strerror(err));
		return;
	}

	mime_hash = hash_create(117);

	// just in case
	hash_add(mime_hash, "css", "text/css");
	hash_add(mime_hash, "htm", "text/html");
	hash_add(mime_hash, "html", "text/html");
	hash_add(mime_hash, "jpg", "image/jpeg");
	hash_add(mime_hash, "jpeg", "image/jpeg");
	hash_add(mime_hash, "gif", "image/gif");
	hash_add(mime_hash, "png", "image/png");
	hash_add(mime_hash, "jng", "image/jng");


	BString type;
	int i, j;
	//mimes.PrintToStream();
	for (i = 0; mimes.FindString("types", i, &type) >= B_OK; i++) {
		BMimeType mime(type.String());
		if (!mime.IsValid())
			continue;
		BMessage extensions;
		if (mime.GetFileExtensions(&extensions) < B_OK)
			continue;
		BString ext;
		for (j = 0; extensions.FindString("extentions", i, &ext) >= B_OK; i++) {
			hash_add(mime_hash, ext.String(), type.String());
		}
	}

#warning WRITEME
#if 0
	struct stat statbuf;
	FILE *fh = NULL;

	/* first, check to see if /etc/mime.types in preference */

	if ((stat("/etc/mime.types", &statbuf) == 0) &&
			S_ISREG(statbuf.st_mode)) {
		mimefile = "/etc/mime.types";

	}

	fh = fopen(mimefile, "r");

	/* Some OSes (mentioning no Solarises) have a worthlessly tiny
	 * /etc/mime.types that don't include essential things, so we
	 * pre-seed our hash with the essentials.  These will get
	 * over-ridden if they are mentioned in the mime.types file.
	 */

	hash_add(mime_hash, "css", "text/css");
	hash_add(mime_hash, "htm", "text/html");
	hash_add(mime_hash, "html", "text/html");
	hash_add(mime_hash, "jpg", "image/jpeg");
	hash_add(mime_hash, "jpeg", "image/jpeg");
	hash_add(mime_hash, "gif", "image/gif");
	hash_add(mime_hash, "png", "image/png");
	hash_add(mime_hash, "jng", "image/jng");

	if (fh == NULL) {
		LOG(("Unable to open a mime.types file, so using a minimal one for you."));
		return;
	}

	while (!feof(fh)) {
		char line[256], *ptr, *type, *ext;
		fgets(line, 256, fh);
		if (!feof(fh) && line[0] != '#') {
			ptr = line;

			/* search for the first non-whitespace character */
			while (isspace(*ptr))
				ptr++;

			/* is this line empty other than leading whitespace? */
			if (*ptr == '\n' || *ptr == '\0')
				continue;

			type = ptr;

			/* search for the first non-whitespace char or NUL or
			 * NL */
			while (*ptr && (!isspace(*ptr)) && *ptr != '\n')
				ptr++;

			if (*ptr == '\0' || *ptr == '\n') {
				/* this mimetype has no extensions - read next
				 * line.
				 */
				continue;
			}

			*ptr++ = '\0';

			/* search for the first non-whitespace character which
			 * will be the first filename extenion */
			while (isspace(*ptr))
				ptr++;

			while(true) {
				ext = ptr;

				/* search for the first whitespace char or
				 * NUL or NL which is the end of the ext.
				 */
				while (*ptr && (!isspace(*ptr)) &&
					*ptr != '\n')
					ptr++;

				if (*ptr == '\0' || *ptr == '\n') {
					/* special case for last extension on
					 * the line
					 */
					*ptr = '\0';
					hash_add(mime_hash, ext, type);
					break;
				}

				*ptr++ = '\0';
				hash_add(mime_hash, ext, type);

				/* search for the first non-whitespace char or
				 * NUL or NL, to find start of next ext.
				 */
				while (*ptr && (isspace(*ptr)) && *ptr != '\n')
					ptr++;
			}
		}
	}

	fclose(fh);
#endif
}

void beos_fetch_filetype_fin(void)
{
	hash_destroy(mime_hash);
}

const char *fetch_filetype(const char *unix_path)
{
	struct stat statbuf;
	status_t err;

	stat(unix_path, &statbuf);
	if (S_ISDIR(statbuf.st_mode))
		return "application/x-netsurf-directory";

	if (strchr(unix_path, '.') == NULL) {
		/* no extension anywhere! */
		return "text/plain";
	}

	// force some types
	const char *ext;
	ext = strrchr(unix_path, '.');
	if (!strcmp(ext, ".css"))
		return "text/css";
	if (!strcmp(ext, ".html"))
		return "text/html";
	if (!strcmp(ext, ".htm"))
		return "text/html";

	BNode node(unix_path);
	if (node.InitCheck() < B_OK) {
		warn_user("Mime", strerror(err));
		return "text/plain";
	}

	BNodeInfo info(&node);
	if (info.InitCheck() < B_OK) {
		warn_user("Mime", strerror(err));
		return "text/plain";
	}

	// NOT THREADSAFE
	static char type[B_MIME_TYPE_LENGTH];
	if (info.GetType(type) < B_OK) {
		// it might not have been sniffed yet...
		update_mime_info(unix_path, false, true, false);
		// try again
		if (info.GetType(type) < B_OK) {
			warn_user("Mime", strerror(err));
			return "text/plain";
		}
	}

	return type;

#warning WRITEME
#if 0
	struct stat statbuf;
	char *ext;
	const char *ptr;
	char *lowerchar;
	const char *type;

	stat(unix_path, &statbuf);
	if (S_ISDIR(statbuf.st_mode))
		return "application/x-netsurf-directory";

	if (strchr(unix_path, '.') == NULL) {
		/* no extension anywhere! */
		return "text/plain";
	}

	ptr = unix_path + strlen(unix_path);
	while (*ptr != '.' && *ptr != '/')
		ptr--;

	if (*ptr != '.')
		return "text/plain";

	ext = strdup(ptr + 1);	/* skip the . */

	/* the hash table only contains lower-case versions - make sure this
	 * copy is lower case too.
	 */
	lowerchar = ext;
	while(*lowerchar) {
		*lowerchar = tolower(*lowerchar);
		lowerchar++;
	}

	type = hash_get(mime_hash, ext);
	free(ext);

	return type != NULL ? type : "text/plain";
#endif
	return NULL;
}

char *fetch_mimetype(const char *unix_path)
{
	return strdup(fetch_filetype(unix_path));
}

#ifdef TEST_RIG

int main(int argc, char *argv[])
{
	unsigned int c1, *c2;
	const char *key;

	beos_fetch_filetype_init("./mime.types");

	c1 = 0; c2 = 0;

	while ( (key = hash_iterate(mime_hash, &c1, &c2)) != NULL) {
		printf("%s ", key);
	}

	printf("\n");

	if (argc > 1) {
		printf("%s maps to %s\n", argv[1], fetch_filetype(argv[1]));
	}

	beos_fetch_filetype_fin();
}

#endif