Snif local files entirely the BeOS way, using the mime db, this fixes taking symlinks to html as plain text.

Make sure the mime db has the needed types just in case they are missing.

svn path=/trunk/netsurf/; revision=5501
This commit is contained in:
François Revel 2008-10-06 23:53:59 +00:00
parent 5e32d1ea12
commit 2ab25ce7db

View File

@ -39,47 +39,49 @@ extern "C" {
#include "beos/beos_filetype.h" #include "beos/beos_filetype.h"
static struct {
const char *type;
const char *ext1;
const char *ext2;
} default_types[] = {
{ "text/plain", "txt", NULL },
{ "text/html", "htm", "html" },
{ "text/css", "css", NULL },
{ "image/gif", "gif", NULL },
{ "image/jpeg", "jpg", "jpeg" },
{ "image/png", "png", NULL },
{ "image/jng", "jng", NULL },
{ NULL, NULL, NULL }
};
void beos_fetch_filetype_init(void) void beos_fetch_filetype_init(void)
{ {
#if 0 BMimeType m;
BMessage mimes;
status_t err; status_t err;
int i;
err = BMimeType::GetInstalledTypes(&mimes); // make sure we have basic mime types in the database
for (i = 0; default_types[i].type; i++) {
if (m.SetTo(default_types[i].type) < B_OK)
continue;
if (m.IsInstalled())
continue;
err = m.Install();
if (err < B_OK) { if (err < B_OK) {
warn_user("Mime", strerror(err)); 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; continue;
BMessage extensions; }
if (mime.GetFileExtensions(&extensions) < B_OK) // the mime db doesn't know about it yet
continue; BMessage extensions(0UL);
BString ext; if (default_types[i].ext1)
for (j = 0; extensions.FindString("extentions", i, &ext) >= B_OK; i++) { extensions.AddString("extensions", default_types[i].ext1);
hash_add(mime_hash, ext.String(), type.String()); if (default_types[i].ext2)
extensions.AddString("extensions", default_types[i].ext2);
err = m.SetFileExtensions(&extensions);
if (err < B_OK) {
warn_user("Mime", strerror(err));
} }
} }
#endif
} }
void beos_fetch_filetype_fin(void) void beos_fetch_filetype_fin(void)
@ -90,48 +92,32 @@ const char *fetch_filetype(const char *unix_path)
{ {
struct stat statbuf; struct stat statbuf;
status_t err; 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 // NOT THREADSAFE
static char type[B_MIME_TYPE_LENGTH]; static char type[B_MIME_TYPE_LENGTH];
if (info.GetType(type) < B_OK) {
// it might not have been sniffed yet... BEntry entry(unix_path, true);
update_mime_info(unix_path, false, true, false); BNode node(&entry);
// try again err = node.InitCheck();
if (info.GetType(type) < B_OK) { if (err < B_OK)
warn_user("Mime", strerror(err)); return "text/plain";
if (node.IsDirectory())
return "application/x-netsurf-directory";
BNodeInfo info(&node);
err = info.InitCheck();
if (err < B_OK)
return "test/plain";
err = info.GetType(type);
if (err < B_OK) {
// not there yet, sniff and retry
err = update_mime_info(unix_path, false, true, false);
if (err < B_OK)
return "text/plain";
err = info.GetType(type);
if (err < B_OK)
return "text/plain"; return "text/plain";
}
} }
return type; return type;