move path_to_url and url_to_path to fetch operation table

This commit is contained in:
Vincent Sanders 2014-01-25 23:00:22 +00:00
parent 46b8fbaeac
commit 8ce0a10670
32 changed files with 613 additions and 512 deletions

View File

@ -5189,6 +5189,8 @@ static struct gui_fetch_table amiga_fetch_table = {
.filename_from_path = filename_from_path,
.path_add_part = path_add_part,
.filetype = fetch_filetype,
.path_to_url = path_to_url,
.url_to_path = url_to_path,
.get_resource_url = gui_get_resource_url,
};

View File

@ -18,6 +18,10 @@
#ifndef AMIGA_MISC_H
#define AMIGA_MISC_H
char *translate_escape_chars(const char *s);
int32 ami_warn_user_multi(const char *body, const char *opt1, const char *opt2, struct Window *win);
char *url_to_path(const char *url);
char *path_to_url(const char *path);
#endif

View File

@ -21,6 +21,9 @@
#define NS_ATARI_FINDFILE_H
extern char *atari_find_resource(char *buf, const char *filename, const char *def);
char * local_file_to_url( const char * filename );
char *local_file_to_url(const char *filename);
char *path_to_url(const char *path_in);
char *url_to_path(const char *url);
#endif /* NETSURF_ATARI_FINDFILE_H */

View File

@ -1077,6 +1077,8 @@ static struct gui_fetch_table atari_fetch_table = {
.filename_from_path = filename_from_path,
.path_add_part = path_add_part,
.filetype = fetch_filetype,
.path_to_url = path_to_url,
.url_to_path = url_to_path,
.get_resource_url = gui_get_resource_url,
.mimetype = fetch_mimetype,

View File

@ -786,6 +786,17 @@ static void gui_quit(void)
fetch_rsrc_unregister();
}
static char *url_to_path(const char *url)
{
char *url_path = curl_unescape(url, 0);
char *path;
/* return the absolute path including leading / */
path = strdup(url_path + (FILE_SCHEME_PREFIX_LEN - 1));
curl_free(url_path);
return path;
}
/**
* Send the source of a content to a text editor.
@ -983,7 +994,7 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
return UTF8_CONVERT_OK;
}
char *path_to_url(const char *path)
static char *path_to_url(const char *path)
{
int urllen = strlen(path) + FILE_SCHEME_PREFIX_LEN + 1;
char *url = (char *)malloc(urllen);
@ -1001,17 +1012,6 @@ char *path_to_url(const char *path)
return url;
}
char *url_to_path(const char *url)
{
char *url_path = curl_unescape(url, 0);
char *path;
/* return the absolute path including leading / */
path = strdup(url_path + (FILE_SCHEME_PREFIX_LEN - 1));
curl_free(url_path);
return path;
}
static void *myrealloc(void *ptr, size_t len, void *pw)
{
@ -1071,6 +1071,8 @@ static struct gui_fetch_table beos_fetch_table = {
filename_from_path,
path_add_part,
fetch_filetype,
path_to_url,
url_to_path,
gui_get_resource_url,
NULL //fetch_mimetype
};

View File

@ -19,6 +19,7 @@
#import "cocoa/BrowserViewController.h"
#import "cocoa/BrowserView.h"
#import "cocoa/BrowserWindowController.h"
#import "cocoa/fetch.h"
#import "desktop/browser_private.h"
#import "desktop/local_history.h"

View File

@ -95,7 +95,6 @@ S_COCOA := \
schedule.m \
selection.m \
thumbnail.m \
url.m \
utf8.m \
utils.m \
ArrowBox.m \

View File

@ -25,7 +25,6 @@
#import "desktop/gui.h"
#import "content/urldb.h"
#import "content/fetch.h"
#import "css/utils.h"
#import "desktop/gui.h"
#import "desktop/local_history.h"

View File

@ -16,4 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const char *fetch_filetype(const char *unix_path);
extern struct gui_fetch_table *cocoa_fetch_table;
char *url_to_path(const char *url);

View File

@ -19,7 +19,7 @@
#import <Cocoa/Cocoa.h>
#import "utils/log.h"
#import "content/fetch.h"
#import "desktop/gui.h"
#import "cocoa/fetch.h"
@ -42,7 +42,7 @@ static const struct mimemap_s {
};
const char *fetch_filetype(const char *unix_path)
static const char *fetch_filetype(const char *unix_path)
{
NSString *uti;
NSString *mimeType = nil;
@ -93,3 +93,50 @@ const char *fetch_filetype(const char *unix_path)
return cocoafiletype;
}
char *url_to_path(const char *url)
{
NSURL *nsurl = [NSURL URLWithString: [NSString stringWithUTF8String: url]];
return strdup([[nsurl path] UTF8String]);
}
static char *path_to_url(const char *path)
{
return strdup( [[[NSURL fileURLWithPath: [NSString stringWithUTF8String: path]]
absoluteString] UTF8String] );
}
static char *filename_from_path(char *path)
{
return strdup( [[[NSString stringWithUTF8String: path] lastPathComponent] UTF8String] );
}
static bool path_add_part(char *path, int length, const char *newpart)
{
NSString *newPath = [[NSString stringWithUTF8String: path] stringByAppendingPathComponent: [NSString stringWithUTF8String: newpart]];
strncpy( path, [newPath UTF8String], length );
return true;
}
static nsurl *gui_get_resource_url(const char *path)
{
nsurl *url = NULL;
NSString *nspath = [[NSBundle mainBundle] pathForResource: [NSString stringWithUTF8String: path] ofType: @""];
if (nspath == nil) return NULL;
nsurl_create([[[NSURL fileURLWithPath: nspath] absoluteString] UTF8String], &url);
return url;
}
static struct gui_fetch_table fetch_table = {
.filename_from_path = filename_from_path,
.path_add_part = path_add_part,
.filetype = fetch_filetype,
.path_to_url = path_to_url,
.url_to_path = url_to_path,
.get_resource_url = gui_get_resource_url,
};
struct gui_fetch_table *cocoa_fetch_table = &fetch_table;

View File

@ -20,7 +20,6 @@
extern struct gui_window_table *cocoa_window_table;
extern struct gui_clipboard_table *cocoa_clipboard_table;
extern struct gui_fetch_table *cocoa_fetch_table;
extern struct gui_browser_table *cocoa_browser_table;
extern NSString * const kCookiesFileOption;

View File

@ -46,15 +46,6 @@ NSString * const kAlwaysCloseMultipleTabs = @"AlwaysCloseMultipleTabs";
#define UNIMPL() NSLog( @"Function '%s' unimplemented", __func__ )
static nsurl *gui_get_resource_url(const char *path)
{
nsurl *url = NULL;
NSString *nspath = [[NSBundle mainBundle] pathForResource: [NSString stringWithUTF8String: path] ofType: @""];
if (nspath == nil) return NULL;
nsurl_create([[[NSURL fileURLWithPath: nspath] absoluteString] UTF8String], &url);
return url;
}
static void gui_poll(bool active)
{
cocoa_autorelease();
@ -273,20 +264,6 @@ static void gui_cert_verify(nsurl *url, const struct ssl_cert_info *certs,
cb( false, cbpw );
}
static char *filename_from_path(char *path)
{
return strdup( [[[NSString stringWithUTF8String: path] lastPathComponent] UTF8String] );
}
static bool path_add_part(char *path, int length, const char *newpart)
{
NSString *newPath = [[NSString stringWithUTF8String: path] stringByAppendingPathComponent: [NSString stringWithUTF8String: newpart]];
strncpy( path, [newPath UTF8String], length );
return true;
}
static struct gui_window_table window_table = {
.create = gui_window_create,
@ -312,15 +289,6 @@ static struct gui_window_table window_table = {
struct gui_window_table *cocoa_window_table = &window_table;
static struct gui_fetch_table fetch_table = {
.filename_from_path = filename_from_path,
.path_add_part = path_add_part,
.filetype = fetch_filetype,
.get_resource_url = gui_get_resource_url,
};
struct gui_fetch_table *cocoa_fetch_table = &fetch_table;
static struct gui_browser_table browser_table = {
.poll = gui_poll,

View File

@ -142,7 +142,7 @@ fetch_file_setup(struct fetch *fetchh,
if (ctx == NULL)
return NULL;
ctx->path = url_to_path(nsurl_access(url));
ctx->path = guit->fetch->url_to_path(nsurl_access(url));
if (ctx->path == NULL) {
free(ctx);
return NULL;
@ -600,7 +600,7 @@ static void fetch_file_process_dir(struct fetch_file_context *ctx,
}
}
if((path = path_to_url(urlpath)) == NULL)
if((path = guit->fetch->path_to_url(urlpath)) == NULL)
continue;
if (S_ISREG(ent_stat.st_mode)) {

View File

@ -319,6 +319,22 @@ struct gui_fetch_table {
*/
const char *(*filetype)(const char *unix_path);
/**
* Convert a pathname to a file: URL.
*
* \param path pathname
* \return URL, allocated on heap, or NULL on failure
*/
char *(*path_to_url)(const char *path);
/**
* Convert a file: URL to a pathname.
*
* \param url a file: URL
* \return pathname, allocated on heap, or NULL on failure
*/
char *(*url_to_path)(const char *url);
/* Optional entries */

View File

@ -317,6 +317,12 @@ static nserror verify_fetch_register(struct gui_fetch_table *gft)
if (gft->filetype == NULL) {
return NSERROR_BAD_PARAMETER;
}
if (gft->path_to_url == NULL) {
return NSERROR_BAD_PARAMETER;
}
if (gft->url_to_path == NULL) {
return NSERROR_BAD_PARAMETER;
}
/* fill in the optional entries with defaults */

View File

@ -236,7 +236,7 @@ void search_web_retrieve_ico(bool localdefault)
if (localdefault) {
if (search_default_ico_location == NULL)
return;
url = path_to_url(search_default_ico_location);
url = guit->fetch->path_to_url(search_default_ico_location);
} else {
url = search_web_ico_name();
}

View File

@ -68,7 +68,13 @@ static bool path_add_part(char *path, int length, const char *newpart)
return true;
}
char *path_to_url(const char *path)
/**
* Convert a pathname to a file: URL.
*
* \param path pathname
* \return URL, allocated on heap, or NULL on failure
*/
static char *path_to_url(const char *path)
{
int urllen;
char *url;
@ -88,8 +94,13 @@ char *path_to_url(const char *path)
return url;
}
char *url_to_path(const char *url)
/**
* Convert a file: URL to a pathname.
*
* \param url a file: URL
* \return pathname, allocated on heap, or NULL on failure
*/
static char *url_to_path(const char *url)
{
char *path;
char *respath;
@ -178,6 +189,8 @@ static struct gui_fetch_table fetch_table = {
.filename_from_path = filename_from_path,
.path_add_part = path_add_part,
.filetype = fetch_filetype,
.path_to_url = path_to_url,
.url_to_path = url_to_path,
.get_resource_url = get_resource_url,
.mimetype = fetch_mimetype,

View File

@ -108,7 +108,7 @@ $(eval $(foreach V,$(filter GTK_IMAGE_%,$(.VARIABLES)),$(call convert_image,$($(
# S_GTK are sources purely for the GTK build
S_GTK := font_pango.c bitmap.c gui.c schedule.c thumbnail.c plotters.c \
treeview.c scaffolding.c gdk.c completion.c login.c throbber.c \
selection.c history.c window.c filetype.c download.c menu.c \
selection.c history.c window.c fetch.c download.c menu.c \
print.c search.c tabs.c theme.c toolbar.c gettext.c \
compat.c cookies.c hotlist.c \
$(addprefix dialogs/,preferences.c about.c source.c)

View File

@ -23,13 +23,6 @@
#include <unistd.h>
#include <gtk/gtk.h>
#include "gtk/compat.h"
#include "gtk/dialogs/source.h"
#include "gtk/dialogs/about.h"
#include "gtk/window.h"
#include "gtk/gui.h"
#include "gtk/print.h"
#include "gtk/selection.h"
#include "desktop/browser_private.h"
#include "desktop/netsurf.h"
#include "desktop/print.h"
@ -42,9 +35,17 @@
#include "render/font.h"
#include "content/content.h"
#include "content/content_type.h"
#include "utils/log.h"
#include "gtk/compat.h"
#include "gtk/dialogs/source.h"
#include "gtk/dialogs/about.h"
#include "gtk/window.h"
#include "gtk/gui.h"
#include "gtk/print.h"
#include "gtk/selection.h"
#include "gtk/fetch.h"
struct nsgtk_source_window {
gchar *url;
char *data;

View File

@ -1,6 +1,5 @@
/*
* Copyright 2007 Rob Kendrick <rjek@netsurf-browser.org>
* Copyright 2007 Vincent Sanders <vince@debian.org>
* Copyright 2014 vincent Sanders <vince@netsurf-browser.org>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
@ -18,18 +17,18 @@
*/
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "gtk/filetype.h"
#include "content/fetch.h"
#include "utils/log.h"
#include "utils/hashtable.h"
#include "utils/url.h"
#include "utils/log.h"
#include "utils/filepath.h"
#include "desktop/gui.h"
#include "gtk/gui.h"
#include "gtk/fetch.h"
static struct hash_table *mime_hash = NULL;
@ -43,9 +42,8 @@ void gtk_fetch_filetype_init(const char *mimefile)
/* first, check to see if /etc/mime.types in preference */
if ((stat("/etc/mime.types", &statbuf) == 0) &&
S_ISREG(statbuf.st_mode)) {
S_ISREG(statbuf.st_mode)) {
mimefile = "/etc/mime.types";
}
fh = fopen(mimefile, "r");
@ -75,25 +73,30 @@ void gtk_fetch_filetype_init(const char *mimefile)
while (!feof(fh)) {
char line[256], *ptr, *type, *ext;
if (fgets(line, 256, fh) == NULL)
break;
break;
if (!feof(fh) && line[0] != '#') {
ptr = line;
/* search for the first non-whitespace character */
while (isspace(*ptr))
while (isspace(*ptr)) {
ptr++;
}
/* is this line empty other than leading whitespace? */
if (*ptr == '\n' || *ptr == '\0')
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')
while (*ptr && (!isspace(*ptr)) && *ptr != '\n') {
ptr++;
}
if (*ptr == '\0' || *ptr == '\n') {
/* this mimetype has no extensions - read next
@ -106,8 +109,9 @@ void gtk_fetch_filetype_init(const char *mimefile)
/* search for the first non-whitespace character which
* will be the first filename extenion */
while (isspace(*ptr))
while (isspace(*ptr)) {
ptr++;
}
while(true) {
ext = ptr;
@ -115,9 +119,11 @@ void gtk_fetch_filetype_init(const char *mimefile)
/* search for the first whitespace char or
* NUL or NL which is the end of the ext.
*/
while (*ptr && (!isspace(*ptr)) &&
*ptr != '\n')
while (*ptr &&
(!isspace(*ptr)) &&
*ptr != '\n') {
ptr++;
}
if (*ptr == '\0' || *ptr == '\n') {
/* special case for last extension on
@ -134,8 +140,11 @@ void gtk_fetch_filetype_init(const char *mimefile)
/* search for the first non-whitespace char or
* NUL or NL, to find start of next ext.
*/
while (*ptr && (isspace(*ptr)) && *ptr != '\n')
while (*ptr &&
(isspace(*ptr)) &&
*ptr != '\n') {
ptr++;
}
}
}
}
@ -162,8 +171,9 @@ const char *fetch_filetype(const char *unix_path)
return "text/plain";
}
if (S_ISDIR(statbuf.st_mode))
if (S_ISDIR(statbuf.st_mode)) {
return "application/x-netsurf-directory";
}
l = strlen(unix_path);
@ -184,11 +194,13 @@ const char *fetch_filetype(const char *unix_path)
}
ptr = unix_path + strlen(unix_path);
while (*ptr != '.' && *ptr != '/')
while (*ptr != '.' && *ptr != '/') {
ptr--;
}
if (*ptr != '.')
if (*ptr != '.') {
return "text/plain";
}
ext = strdup(ptr + 1); /* skip the . */
@ -196,7 +208,7 @@ const char *fetch_filetype(const char *unix_path)
* copy is lower case too.
*/
lowerchar = ext;
while(*lowerchar) {
while (*lowerchar) {
*lowerchar = tolower(*lowerchar);
lowerchar++;
}
@ -204,31 +216,132 @@ const char *fetch_filetype(const char *unix_path)
type = hash_get(mime_hash, ext);
free(ext);
return type != NULL ? type : "text/plain";
if (type == NULL) {
type = "text/plain";
}
return type;
}
#ifdef TEST_RIG
int main(int argc, char *argv[])
/**
* Return the filename part of a full path
*
* \param path full path and filename
* \return filename (will be freed with free())
*/
static char *filename_from_path(char *path)
{
unsigned int c1, *c2;
const char *key;
char *leafname;
gtk_fetch_filetype_init("./mime.types");
c1 = 0; c2 = 0;
while ( (key = hash_iterate(mime_hash, &c1, &c2)) != NULL) {
printf("%s ", key);
leafname = strrchr(path, '/');
if (!leafname) {
leafname = path;
} else {
leafname += 1;
}
printf("\n");
if (argc > 1) {
printf("%s maps to %s\n", argv[1], fetch_filetype(argv[1]));
}
gtk_fetch_filetype_fin();
return strdup(leafname);
}
#endif
/**
* Add a path component/filename to an existing path
*
* \param path buffer containing path + free space
* \param length length of buffer "path"
* \param newpart string containing path component to add to path
* \return true on success
*/
static bool path_add_part(char *path, int length, const char *newpart)
{
if (path[strlen(path) - 1] != '/') {
strncat(path, "/", length);
}
strncat(path, newpart, length);
return true;
}
char *path_to_url(const char *path)
{
int urllen;
char *url;
if (path == NULL) {
return NULL;
}
urllen = strlen(path) + FILE_SCHEME_PREFIX_LEN + 1;
url = malloc(urllen);
if (url == NULL) {
return NULL;
}
if (*path == '/') {
path++; /* file: paths are already absolute */
}
snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, path);
return url;
}
static char *url_to_path(const char *url)
{
char *path;
char *respath;
url_func_result res; /* result from url routines */
res = url_path(url, &path);
if (res != URL_FUNC_OK) {
return NULL;
}
res = url_unescape(path, &respath);
free(path);
if (res != URL_FUNC_OK) {
return NULL;
}
return respath;
}
static nsurl *gui_get_resource_url(const char *path)
{
char buf[PATH_MAX];
char *raw;
nsurl *url = NULL;
/* default.css -> gtkdefault.css */
if (strcmp(path, "default.css") == 0) {
path = "gtkdefault.css";
}
/* favicon.ico -> favicon.png */
if (strcmp(path, "favicon.ico") == 0) {
path = "favicon.png";
}
raw = path_to_url(filepath_sfind(respaths, buf, path));
if (raw != NULL) {
nsurl_create(raw, &url);
free(raw);
}
return url;
}
static struct gui_fetch_table fetch_table = {
.filename_from_path = filename_from_path,
.path_add_part = path_add_part,
.filetype = fetch_filetype,
.path_to_url = path_to_url,
.url_to_path = url_to_path,
.get_resource_url = gui_get_resource_url,
};
struct gui_fetch_table *nsgtk_fetch_table = &fetch_table;

View File

@ -1,6 +1,5 @@
/*
* Copyright 2007 Rob Kendrick <rjek@netsurf-browser.org>
* Copyright 2007 Vincent Sanders <vince@debian.org>
* Copyright 2014 Vincent Sanders <vince@netsurf-browser.org>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
@ -17,6 +16,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef NETSURF_GTK_FETCH_H
#define NETSURF_GTK_FETCH_H
struct gui_fetch_table *nsgtk_fetch_table;
void gtk_fetch_filetype_init(const char *mimefile);
void gtk_fetch_filetype_fin(void);
const char *fetch_filetype(const char *unix_path);
char *path_to_url(const char *path);
#endif

120
gtk/gui.c
View File

@ -60,7 +60,7 @@
#include "gtk/completion.h"
#include "gtk/cookies.h"
#include "gtk/download.h"
#include "gtk/filetype.h"
#include "gtk/fetch.h"
#include "gtk/gui.h"
#include "gtk/history.h"
#include "gtk/hotlist.h"
@ -109,7 +109,7 @@ static void nsgtk_PDF_no_pass(GtkButton *w, gpointer data);
#define THROBBER_FRAMES 9
static char **respaths; /** resource search path vector */
char **respaths; /** resource search path vector */
/** Create an array of valid paths to search for resources.
*
@ -324,28 +324,6 @@ static void check_options(char **respath)
}
static nsurl *gui_get_resource_url(const char *path)
{
char buf[PATH_MAX];
char *raw;
nsurl *url = NULL;
/* default.css -> gtkdefault.css */
if (strcmp(path, "default.css") == 0)
path = "gtkdefault.css";
/* favicon.ico -> favicon.png */
if (strcmp(path, "favicon.ico") == 0)
path = "favicon.png";
raw = path_to_url(filepath_sfind(respaths, buf, path));
if (raw != NULL) {
nsurl_create(raw, &url);
free(raw);
}
return url;
}
/**
@ -842,51 +820,6 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
}
char *path_to_url(const char *path)
{
int urllen;
char *url;
if (path == NULL) {
return NULL;
}
urllen = strlen(path) + FILE_SCHEME_PREFIX_LEN + 1;
url = malloc(urllen);
if (url == NULL) {
return NULL;
}
if (*path == '/') {
path++; /* file: paths are already absolute */
}
snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, path);
return url;
}
char *url_to_path(const char *url)
{
char *path;
char *respath;
url_func_result res; /* result from url routines */
res = url_path(url, &path);
if (res != URL_FUNC_OK) {
return NULL;
}
res = url_unescape(path, &respath);
free(path);
if (res != URL_FUNC_OK) {
return NULL;
}
return respath;
}
#ifdef WITH_PDF_EXPORT
@ -1089,59 +1022,12 @@ uint32_t gtk_gui_gdkkey_to_nskey(GdkEventKey *key)
}
}
/**
* Return the filename part of a full path
*
* \param path full path and filename
* \return filename (will be freed with free())
*/
static char *filename_from_path(char *path)
{
char *leafname;
leafname = strrchr(path, '/');
if (!leafname)
leafname = path;
else
leafname += 1;
return strdup(leafname);
}
/**
* Add a path component/filename to an existing path
*
* \param path buffer containing path + free space
* \param length length of buffer "path"
* \param newpart string containing path component to add to path
* \return true on success
*/
static bool path_add_part(char *path, int length, const char *newpart)
{
if(path[strlen(path) - 1] != '/')
strncat(path, "/", length);
strncat(path, newpart, length);
return true;
}
static struct gui_clipboard_table nsgtk_clipboard_table = {
.get = gui_get_clipboard,
.set = gui_set_clipboard,
};
static struct gui_fetch_table nsgtk_fetch_table = {
.filename_from_path = filename_from_path,
.path_add_part = path_add_part,
.filetype = fetch_filetype,
.get_resource_url = gui_get_resource_url,
};
static struct gui_browser_table nsgtk_browser_table = {
.poll = gui_poll,
@ -1166,7 +1052,7 @@ int main(int argc, char** argv)
.window = nsgtk_window_table,
.clipboard = &nsgtk_clipboard_table,
.download = nsgtk_download_table,
.fetch = &nsgtk_fetch_table,
.fetch = nsgtk_fetch_table,
};
/* check home directory is available */

View File

@ -59,6 +59,8 @@ extern char *themelist_file_location;
extern GdkPixbuf *favicon_pixbuf; /* favicon default pixbuf */
extern char **respaths; /** resource search path vector */
uint32_t gtk_gui_gdkkey_to_nskey(GdkEventKey *);
extern void gui_401login_open(nsurl *url, const char *realm,

View File

@ -68,7 +68,7 @@ endif
# S_MONKEY are sources purely for the MONKEY build
S_MONKEY := main.c utils.c filetype.c schedule.c \
bitmap.c plot.c browser.c download.c thumbnail.c \
401login.c cert.c font.c poll.c dispatch.c
401login.c cert.c font.c poll.c dispatch.c fetch.c
S_MONKEY := $(addprefix monkey/,$(S_MONKEY))

145
monkey/fetch.c Normal file
View File

@ -0,0 +1,145 @@
/*
* Copyright 2014 Vincent Sanders <vince@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/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <limits.h>
#include "desktop/gui.h"
#include "utils/url.h"
#include "utils/nsurl.h"
#include "utils/filepath.h"
#include "monkey/filetype.h"
#include "monkey/fetch.h"
extern char **respaths;
static char *path_to_url(const char *path)
{
int urllen;
char *url;
if (path == NULL) {
return NULL;
}
urllen = strlen(path) + FILE_SCHEME_PREFIX_LEN + 1;
url = malloc(urllen);
if (url == NULL) {
return NULL;
}
if (*path == '/') {
path++; /* file: paths are already absolute */
}
snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, path);
return url;
}
static char *url_to_path(const char *url)
{
char *path;
char *respath;
url_func_result res; /* result from url routines */
res = url_path(url, &path);
if (res != URL_FUNC_OK) {
return NULL;
}
res = url_unescape(path, &respath);
free(path);
if (res != URL_FUNC_OK) {
return NULL;
}
return respath;
}
/**
* Return the filename part of a full path
*
* \param path full path and filename
* \return filename (will be freed with free())
*/
static char *filename_from_path(char *path)
{
char *leafname;
leafname = strrchr(path, '/');
if (!leafname)
leafname = path;
else
leafname += 1;
return strdup(leafname);
}
/**
* Add a path component/filename to an existing path
*
* \param path buffer containing path + free space
* \param length length of buffer "path"
* \param newpart string containing path component to add to path
* \return true on success
*/
static bool path_add_part(char *path, int length, const char *newpart)
{
if(path[strlen(path) - 1] != '/')
strncat(path, "/", length);
strncat(path, newpart, length);
return true;
}
static nsurl *gui_get_resource_url(const char *path)
{
char buf[PATH_MAX];
char *raw;
nsurl *url = NULL;
raw = path_to_url(filepath_sfind(respaths, buf, path));
if (raw != NULL) {
nsurl_create(raw, &url);
free(raw);
}
return url;
}
static struct gui_fetch_table fetch_table = {
.filename_from_path = filename_from_path,
.path_add_part = path_add_part,
.filetype = monkey_fetch_filetype,
.path_to_url = path_to_url,
.url_to_path = url_to_path,
.get_resource_url = gui_get_resource_url,
};
struct gui_fetch_table *monkey_fetch_table = &fetch_table;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2011 Sven Weidauer <sven.weidauer@gmail.com>
* Copyright 2014 Vincent Sanders <vince@netsurf-browser.org>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
@ -16,19 +16,4 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#import <Cocoa/Cocoa.h>
#import "utils/url.h"
char *url_to_path(const char *url)
{
NSURL *nsurl = [NSURL URLWithString: [NSString stringWithUTF8String: url]];
return strdup([[nsurl path] UTF8String]);
}
char *path_to_url(const char *path)
{
return strdup( [[[NSURL fileURLWithPath: [NSString stringWithUTF8String: path]]
absoluteString] UTF8String] );
}
struct gui_fetch_table *monkey_fetch_table;

View File

@ -20,13 +20,14 @@
#include <stdio.h>
#include <stdlib.h>
#include "monkey/filetype.h"
#include "utils/nsoption.h"
#include "monkey/poll.h"
#include "monkey/dispatch.h"
#include "monkey/browser.h"
#include "monkey/cert.h"
#include "monkey/401login.h"
#include "monkey/filetype.h"
#include "monkey/fetch.h"
#include "content/urldb.h"
#include "content/fetchers/resource.h"
@ -36,49 +37,34 @@
#include "utils/filepath.h"
#include "utils/url.h"
static char **respaths; /** resource search path vector */
char **respaths; /** resource search path vector */
/* Stolen from gtk/gui.c */
static char **
nsmonkey_init_resource(const char *resource_path)
{
const gchar * const *langv;
char **pathv; /* resource path string vector */
char **respath; /* resource paths vector */
const gchar * const *langv;
char **pathv; /* resource path string vector */
char **respath; /* resource paths vector */
pathv = filepath_path_to_strvec(resource_path);
pathv = filepath_path_to_strvec(resource_path);
langv = g_get_language_names();
langv = g_get_language_names();
respath = filepath_generate(pathv, langv);
respath = filepath_generate(pathv, langv);
filepath_free_strvec(pathv);
filepath_free_strvec(pathv);
return respath;
return respath;
}
static void monkey_quit(void)
{
urldb_save_cookies(nsoption_charp(cookie_jar));
urldb_save(nsoption_charp(url_file));
free(nsoption_charp(cookie_file));
free(nsoption_charp(cookie_jar));
monkey_fetch_filetype_fin();
}
static nsurl *gui_get_resource_url(const char *path)
{
char buf[PATH_MAX];
char *raw;
nsurl *url = NULL;
raw = path_to_url(filepath_sfind(respaths, buf, path));
if (raw != NULL) {
nsurl_create(raw, &url);
free(raw);
}
return url;
urldb_save_cookies(nsoption_charp(cookie_jar));
urldb_save(nsoption_charp(url_file));
free(nsoption_charp(cookie_file));
free(nsoption_charp(cookie_jar));
monkey_fetch_filetype_fin();
}
static void
@ -115,53 +101,6 @@ static bool nslog_stream_configure(FILE *fptr)
return true;
}
/**
* Return the filename part of a full path
*
* \param path full path and filename
* \return filename (will be freed with free())
*/
static char *filename_from_path(char *path)
{
char *leafname;
leafname = strrchr(path, '/');
if (!leafname)
leafname = path;
else
leafname += 1;
return strdup(leafname);
}
/**
* Add a path component/filename to an existing path
*
* \param path buffer containing path + free space
* \param length length of buffer "path"
* \param newpart string containing path component to add to path
* \return true on success
*/
static bool path_add_part(char *path, int length, const char *newpart)
{
if(path[strlen(path) - 1] != '/')
strncat(path, "/", length);
strncat(path, newpart, length);
return true;
}
static struct gui_fetch_table monkey_fetch_table = {
.filename_from_path = filename_from_path,
.path_add_part = path_add_part,
.filetype = monkey_fetch_filetype,
.get_resource_url = gui_get_resource_url,
};
static struct gui_browser_table monkey_browser_table = {
.poll = monkey_poll,
@ -182,7 +121,7 @@ main(int argc, char **argv)
.browser = &monkey_browser_table,
.window = monkey_window_table,
.download = monkey_download_table,
.fetch = &monkey_fetch_table,
.fetch = monkey_fetch_table,
};
/* Unbuffer stdin/out/err */

View File

@ -25,50 +25,6 @@
#include "utils/url.h"
#include "utils/utf8.h"
char *path_to_url(const char *path)
{
int urllen;
char *url;
if (path == NULL) {
return NULL;
}
urllen = strlen(path) + FILE_SCHEME_PREFIX_LEN + 1;
url = malloc(urllen);
if (url == NULL) {
return NULL;
}
if (*path == '/') {
path++; /* file: paths are already absolute */
}
snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, path);
return url;
}
char *url_to_path(const char *url)
{
char *path;
char *respath;
url_func_result res; /* result from url routines */
res = url_path(url, &path);
if (res != URL_FUNC_OK) {
return NULL;
}
res = url_unescape(path, &respath);
free(path);
if (res != URL_FUNC_OK) {
return NULL;
}
return respath;
}
void warn_user(const char *warning, const char *detail)

View File

@ -719,6 +719,135 @@ void ro_gui_check_resolvers(void)
}
}
/**
* Convert a RISC OS pathname to a file: URL.
*
* \param path RISC OS pathname
* \return URL, allocated on heap, or 0 on failure
*/
static char *path_to_url(const char *path)
{
int spare;
char *canonical_path; /* canonicalised RISC OS path */
char *unix_path; /* unix path */
char *escurl;
os_error *error;
url_func_result url_err;
int urllen;
char *url; /* resulting url */
/* calculate the canonical risc os path */
error = xosfscontrol_canonicalise_path(path, 0, 0, 0, 0, &spare);
if (error) {
LOG(("xosfscontrol_canonicalise_path failed: 0x%x: %s",
error->errnum, error->errmess));
warn_user("PathToURL", error->errmess);
return NULL;
}
canonical_path = malloc(1 - spare);
if (canonical_path == NULL) {
LOG(("malloc failed"));
warn_user("NoMemory", 0);
free(canonical_path);
return NULL;
}
error = xosfscontrol_canonicalise_path(path, canonical_path, 0, 0, 1 - spare, 0);
if (error) {
LOG(("xosfscontrol_canonicalise_path failed: 0x%x: %s",
error->errnum, error->errmess));
warn_user("PathToURL", error->errmess);
free(canonical_path);
return NULL;
}
/* create a unix path from teh cananocal risc os one */
unix_path = __unixify(canonical_path, __RISCOSIFY_NO_REVERSE_SUFFIX, NULL, 0, 0);
if (unix_path == NULL) {
LOG(("__unixify failed: %s", canonical_path));
free(canonical_path);
return NULL;
}
free(canonical_path);
/* convert the unix path into a url */
urllen = strlen(unix_path) + FILE_SCHEME_PREFIX_LEN + 1;
url = malloc(urllen);
if (url == NULL) {
LOG(("Unable to allocate url"));
free(unix_path);
return NULL;
}
if (*unix_path == '/') {
snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, unix_path + 1);
} else {
snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, unix_path);
}
free(unix_path);
/* We don't want '/' to be escaped. */
url_err = url_escape(url, FILE_SCHEME_PREFIX_LEN, false, "/", &escurl);
free(url); url = NULL;
if (url_err != URL_FUNC_OK) {
LOG(("url_escape failed: %s", url));
return NULL;
}
return escurl;
}
/**
* Convert a file: URL to a RISC OS pathname.
*
* \param url a file: URL
* \return RISC OS pathname, allocated on heap, or 0 on failure
*/
static char *url_to_path(const char *url)
{
char *path;
char *filename;
char *respath;
url_func_result res; /* result from url routines */
char *r;
res = url_path(url, &path);
if (res != URL_FUNC_OK) {
warn_user("NoMemory", 0);
return NULL;
}
res = url_unescape(path, &respath);
free(path);
if (res != URL_FUNC_OK) {
return NULL;
}
/* RISC OS path should not be more than 100 characters longer */
filename = malloc(strlen(respath) + 100);
if (!filename) {
free(respath);
warn_user("NoMemory", 0);
return NULL;
}
r = __riscosify(respath, 0, __RISCOSIFY_NO_SUFFIX,
filename, strlen(respath) + 100, 0);
free(respath);
if (r == 0) {
free(filename);
LOG(("__riscosify failed"));
return NULL;
}
return filename;
}
/**
* Last-minute gui init, after all other modules have initialised.
@ -838,7 +967,6 @@ static bool nslog_stream_configure(FILE *fptr)
}
/**
* Close down the gui (RISC OS).
*/
@ -1873,135 +2001,6 @@ void ro_msg_window_info(wimp_message *message)
}
/**
* Convert a RISC OS pathname to a file: URL.
*
* \param path RISC OS pathname
* \return URL, allocated on heap, or 0 on failure
*/
char *path_to_url(const char *path)
{
int spare;
char *canonical_path; /* canonicalised RISC OS path */
char *unix_path; /* unix path */
char *escurl;
os_error *error;
url_func_result url_err;
int urllen;
char *url; /* resulting url */
/* calculate the canonical risc os path */
error = xosfscontrol_canonicalise_path(path, 0, 0, 0, 0, &spare);
if (error) {
LOG(("xosfscontrol_canonicalise_path failed: 0x%x: %s",
error->errnum, error->errmess));
warn_user("PathToURL", error->errmess);
return NULL;
}
canonical_path = malloc(1 - spare);
if (canonical_path == NULL) {
LOG(("malloc failed"));
warn_user("NoMemory", 0);
free(canonical_path);
return NULL;
}
error = xosfscontrol_canonicalise_path(path, canonical_path, 0, 0, 1 - spare, 0);
if (error) {
LOG(("xosfscontrol_canonicalise_path failed: 0x%x: %s",
error->errnum, error->errmess));
warn_user("PathToURL", error->errmess);
free(canonical_path);
return NULL;
}
/* create a unix path from teh cananocal risc os one */
unix_path = __unixify(canonical_path, __RISCOSIFY_NO_REVERSE_SUFFIX, NULL, 0, 0);
if (unix_path == NULL) {
LOG(("__unixify failed: %s", canonical_path));
free(canonical_path);
return NULL;
}
free(canonical_path);
/* convert the unix path into a url */
urllen = strlen(unix_path) + FILE_SCHEME_PREFIX_LEN + 1;
url = malloc(urllen);
if (url == NULL) {
LOG(("Unable to allocate url"));
free(unix_path);
return NULL;
}
if (*unix_path == '/') {
snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, unix_path + 1);
} else {
snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, unix_path);
}
free(unix_path);
/* We don't want '/' to be escaped. */
url_err = url_escape(url, FILE_SCHEME_PREFIX_LEN, false, "/", &escurl);
free(url); url = NULL;
if (url_err != URL_FUNC_OK) {
LOG(("url_escape failed: %s", url));
return NULL;
}
return escurl;
}
/**
* Convert a file: URL to a RISC OS pathname.
*
* \param url a file: URL
* \return RISC OS pathname, allocated on heap, or 0 on failure
*/
char *url_to_path(const char *url)
{
char *path;
char *filename;
char *respath;
url_func_result res; /* result from url routines */
char *r;
res = url_path(url, &path);
if (res != URL_FUNC_OK) {
warn_user("NoMemory", 0);
return NULL;
}
res = url_unescape(path, &respath);
free(path);
if (res != URL_FUNC_OK) {
return NULL;
}
/* RISC OS path should not be more than 100 characters longer */
filename = malloc(strlen(respath) + 100);
if (!filename) {
free(respath);
warn_user("NoMemory", 0);
return NULL;
}
r = __riscosify(respath, 0, __RISCOSIFY_NO_SUFFIX,
filename, strlen(respath) + 100, 0);
free(respath);
if (r == 0) {
free(filename);
LOG(("__riscosify failed"));
return NULL;
}
return filename;
}
/**
@ -2357,6 +2356,8 @@ static struct gui_fetch_table riscos_fetch_table = {
.filename_from_path = filename_from_path,
.path_add_part = path_add_part,
.filetype = fetch_filetype,
.path_to_url = path_to_url,
.url_to_path = url_to_path,
.get_resource_url = gui_get_resource_url,
.mimetype = fetch_mimetype,

View File

@ -58,7 +58,4 @@ url_func_result url_escape(const char *unescaped, size_t toskip,
url_func_result url_unescape(const char *str, char **result);
url_func_result url_path(const char *url, char **result);
char *path_to_url(const char *path);
char *url_to_path(const char *url);
#endif

View File

@ -23,5 +23,8 @@ extern char *nsws_find_resource(char *buf, const char *filename, const char *def
char **nsws_init_resource(const char *resource_path);
char *path_to_url(const char *path);
char *url_to_path(const char *url);
#endif /* _NETSURF_WINDOWS_FINDFILE_H_ */

View File

@ -1882,6 +1882,8 @@ static struct gui_fetch_table fetch_table = {
.filename_from_path = filename_from_path,
.path_add_part = path_add_part,
.filetype = fetch_filetype,
.path_to_url = path_to_url,
.url_to_path = url_to_path,
.mimetype = fetch_mimetype,
};