2011-05-07 00:40:09 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2011 John-Mark Bell <jmb@netsurf-browser.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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* Content factory (implementation)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "content/content.h"
|
|
|
|
#include "content/content_factory.h"
|
|
|
|
#include "content/content_protected.h"
|
|
|
|
#include "content/llcache.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entry in list of content handlers
|
|
|
|
*/
|
|
|
|
typedef struct content_handler_entry {
|
|
|
|
/** Next entry */
|
|
|
|
struct content_handler_entry *next;
|
|
|
|
|
|
|
|
/** MIME type handled by handler */
|
|
|
|
lwc_string *mime_type;
|
|
|
|
/** Content handler object */
|
|
|
|
const content_handler *handler;
|
|
|
|
} content_handler_entry;
|
|
|
|
|
|
|
|
static content_handler_entry *content_handlers;
|
|
|
|
|
2011-05-17 01:16:44 +04:00
|
|
|
/**
|
|
|
|
* Clean up after the content factory
|
|
|
|
*/
|
|
|
|
void content_factory_fini(void)
|
|
|
|
{
|
|
|
|
content_handler_entry *victim;
|
|
|
|
|
|
|
|
while (content_handlers != NULL) {
|
|
|
|
victim = content_handlers;
|
|
|
|
|
|
|
|
content_handlers = content_handlers->next;
|
|
|
|
|
2011-09-16 02:47:50 +04:00
|
|
|
if (victim->handler->fini != NULL)
|
|
|
|
victim->handler->fini();
|
|
|
|
|
2011-05-17 01:16:44 +04:00
|
|
|
lwc_string_unref(victim->mime_type);
|
|
|
|
|
|
|
|
free(victim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
/**
|
|
|
|
* Register a handler with the content factory
|
|
|
|
*
|
|
|
|
* \param mime_type MIME type to handle
|
|
|
|
* \param handler Content handler for MIME type
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*
|
|
|
|
* \note Latest registration for a MIME type wins
|
|
|
|
*/
|
2011-09-16 02:31:16 +04:00
|
|
|
nserror content_factory_register_handler(const char *mime_type,
|
2011-05-07 00:40:09 +04:00
|
|
|
const content_handler *handler)
|
|
|
|
{
|
2011-09-16 02:31:16 +04:00
|
|
|
lwc_string *imime_type;
|
|
|
|
lwc_error lerror;
|
2011-05-07 00:40:09 +04:00
|
|
|
content_handler_entry *entry;
|
|
|
|
bool match;
|
|
|
|
|
2011-09-16 02:31:16 +04:00
|
|
|
lerror = lwc_intern_string(mime_type, strlen(mime_type), &imime_type);
|
|
|
|
if (lerror != lwc_error_ok)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
for (entry = content_handlers; entry != NULL; entry = entry->next) {
|
2011-09-16 02:31:16 +04:00
|
|
|
if (lwc_string_caseless_isequal(imime_type, entry->mime_type,
|
2011-05-07 00:40:09 +04:00
|
|
|
&match) == lwc_error_ok && match)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry == NULL) {
|
|
|
|
entry = malloc(sizeof(content_handler_entry));
|
|
|
|
if (entry == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
|
|
|
entry->next = content_handlers;
|
|
|
|
content_handlers = entry;
|
|
|
|
|
2011-09-16 02:31:16 +04:00
|
|
|
entry->mime_type = imime_type;
|
|
|
|
} else {
|
|
|
|
lwc_string_unref(imime_type);
|
2011-05-07 00:40:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
entry->handler = handler;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a handler for a MIME type.
|
|
|
|
*
|
|
|
|
* \param mime_type MIME type to search for
|
|
|
|
* \return Associated handler, or NULL if none
|
|
|
|
*/
|
|
|
|
static const content_handler *content_lookup(lwc_string *mime_type)
|
|
|
|
{
|
|
|
|
content_handler_entry *entry;
|
|
|
|
bool match;
|
|
|
|
|
|
|
|
for (entry = content_handlers; entry != NULL; entry = entry->next) {
|
|
|
|
if (lwc_string_caseless_isequal(mime_type, entry->mime_type,
|
|
|
|
&match) == lwc_error_ok && match)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry != NULL)
|
|
|
|
return entry->handler;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the generic content type for a MIME type
|
|
|
|
*
|
|
|
|
* \param mime_type MIME type to consider
|
|
|
|
* \return Generic content type
|
|
|
|
*/
|
2011-07-08 12:38:17 +04:00
|
|
|
content_type content_factory_type_from_mime_type(lwc_string *mime_type)
|
2011-05-07 00:40:09 +04:00
|
|
|
{
|
|
|
|
const content_handler *handler;
|
|
|
|
content_type type = CONTENT_NONE;
|
|
|
|
|
2011-07-08 12:38:17 +04:00
|
|
|
handler = content_lookup(mime_type);
|
2011-05-07 00:40:09 +04:00
|
|
|
if (handler != NULL) {
|
2011-09-03 13:27:42 +04:00
|
|
|
type = handler->type();
|
2011-05-07 00:40:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a content object
|
|
|
|
*
|
|
|
|
* \param llcache Underlying source data handle
|
|
|
|
* \param fallback_charset Character set to fall back to if none specified
|
|
|
|
* \param quirks Quirkiness of containing document
|
2011-09-04 10:28:09 +04:00
|
|
|
* \param effective_type Effective MIME type of content
|
2011-05-07 00:40:09 +04:00
|
|
|
* \return Pointer to content object, or NULL on failure
|
|
|
|
*/
|
|
|
|
struct content *content_factory_create_content(llcache_handle *llcache,
|
2011-09-04 10:28:09 +04:00
|
|
|
const char *fallback_charset, bool quirks,
|
|
|
|
lwc_string *effective_type)
|
2011-05-07 00:40:09 +04:00
|
|
|
{
|
|
|
|
struct content *c;
|
|
|
|
const char *content_type_header;
|
|
|
|
const content_handler *handler;
|
2011-09-04 10:28:09 +04:00
|
|
|
http_content_type *ct = NULL;
|
2011-05-07 00:40:09 +04:00
|
|
|
nserror error;
|
|
|
|
|
2011-09-04 10:28:09 +04:00
|
|
|
handler = content_lookup(effective_type);
|
|
|
|
if (handler == NULL)
|
2011-05-07 00:40:09 +04:00
|
|
|
return NULL;
|
|
|
|
|
2011-09-04 10:28:09 +04:00
|
|
|
assert(handler->create != NULL);
|
|
|
|
|
|
|
|
/* Use the parameters from the declared Content-Type header */
|
|
|
|
content_type_header =
|
|
|
|
llcache_handle_get_header(llcache, "Content-Type");
|
|
|
|
if (content_type_header != NULL) {
|
|
|
|
/* We don't care if this fails */
|
|
|
|
http_parse_content_type(content_type_header, &ct);
|
2011-05-07 00:40:09 +04:00
|
|
|
}
|
|
|
|
|
2011-09-04 10:28:09 +04:00
|
|
|
error = handler->create(handler, effective_type,
|
|
|
|
ct != NULL ? ct->parameters : NULL,
|
|
|
|
llcache, fallback_charset, quirks,
|
|
|
|
&c);
|
2011-05-07 00:40:09 +04:00
|
|
|
|
2011-09-04 10:28:09 +04:00
|
|
|
if (ct != NULL)
|
2011-07-08 12:38:17 +04:00
|
|
|
http_content_type_destroy(ct);
|
2011-05-07 00:40:09 +04:00
|
|
|
|
2011-09-04 10:28:09 +04:00
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return NULL;
|
2011-05-07 00:40:09 +04:00
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|