diff --git a/amiga/misc.c b/amiga/misc.c index f492fcf8a..fb4b86ff4 100755 --- a/amiga/misc.c +++ b/amiga/misc.c @@ -76,9 +76,9 @@ char *url_to_path(const char *url) char *path_to_url(const char *path) { - char *r = malloc(strlen(path) + 8 + 1); + char *r = malloc(strlen(path) + FILE_SCHEME_PREFIX_LEN + 1); - strcpy(r, "file:///"); + strcpy(r, FILE_SCHEME_PREFIX); strcat(r, path); return r; diff --git a/beos/beos_gui.cpp b/beos/beos_gui.cpp index d506cdfe6..57f81877a 100644 --- a/beos/beos_gui.cpp +++ b/beos/beos_gui.cpp @@ -1151,9 +1151,9 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len, char *path_to_url(const char *path) { - char *r = (char *)malloc(strlen(path) + 7 + 1); + char *r = (char *)malloc(strlen(path) + FILE_SCHEME_PREFIX_LEN + 1); - strcpy(r, "file://"); + strcpy(r, FILE_SCHEME_PREFIX); strcat(r, path); return r; @@ -1161,7 +1161,7 @@ char *path_to_url(const char *path) char *url_to_path(const char *url) { - return strdup(url + 5); + return strdup(url + FILE_SCHEME_PREFIX_LEN); } bool cookies_update(const char *domain, const struct cookie_data *data) diff --git a/content/fetchers/fetch_curl.c b/content/fetchers/fetch_curl.c index 9ac3ad7b3..e5ff6bf71 100644 --- a/content/fetchers/fetch_curl.c +++ b/content/fetchers/fetch_curl.c @@ -1098,7 +1098,6 @@ size_t fetch_curl_header(char *data, size_t size, size_t nmemb, #undef SKIP_ST } - /** * Find the status code and content type and inform the caller. * @@ -1154,10 +1153,12 @@ bool fetch_curl_process_headers(struct curl_fetch_info *f) } /* find MIME type from filetype for local files */ - if (strncmp(f->url, "file:///", 8) == 0) { + if (strncmp(f->url, FILE_SCHEME_PREFIX, FILE_SCHEME_PREFIX_LEN) == 0) { struct stat s; - char *url_path = curl_unescape(f->url + 7, - (int) strlen(f->url + 7)); + char *url_path = curl_unescape(f->url + FILE_SCHEME_PREFIX_LEN, + (int) strlen(f->url + FILE_SCHEME_PREFIX_LEN)); + + LOG(("Obtaining mime type for file %s", url_path)); if (url_path != NULL && stat(url_path, &s) == 0) { /* file: URL and file exists */ diff --git a/framebuffer/findfile.c b/framebuffer/findfile.c index 29b82588d..64d96fcb4 100644 --- a/framebuffer/findfile.c +++ b/framebuffer/findfile.c @@ -24,14 +24,15 @@ #include #include "utils/log.h" +#include "utils/url.h" #include "framebuffer/findfile.h" char *path_to_url(const char *path) { - char *r = malloc(strlen(path) + 7 + 1); + char *r = malloc(strlen(path) + FILE_SCHEME_PREFIX_LEN + 1); - strcpy(r, "file://"); + strcpy(r, FILE_SCHEME_PREFIX); strcat(r, path); return r; diff --git a/framebuffer/findfile.h b/framebuffer/findfile.h index 87b9a95e4..85a2f7074 100644 --- a/framebuffer/findfile.h +++ b/framebuffer/findfile.h @@ -19,8 +19,6 @@ #ifndef NETSURF_FB_FINDFILE_H #define NETSURF_FB_FINDFILE_H -char *path_to_url(const char *path); - extern char *fb_find_resource(char *buf, const char *filename, const char *def); #endif /* NETSURF_FB_FINDFILE_H */ diff --git a/framebuffer/gui.c b/framebuffer/gui.c index c537c705b..62ca77131 100644 --- a/framebuffer/gui.c +++ b/framebuffer/gui.c @@ -38,6 +38,7 @@ #include "desktop/netsurf.h" #include "desktop/options.h" #include "utils/log.h" +#include "utils/url.h" #include "utils/messages.h" #include "utils/utils.h" #include "desktop/textinput.h" diff --git a/gtk/gtk_gui.c b/gtk/gtk_gui.c index ba2052083..c7c535fc5 100644 --- a/gtk/gtk_gui.c +++ b/gtk/gtk_gui.c @@ -97,8 +97,6 @@ static struct browser_window *select_menu_bw; static struct form_control *select_menu_control; static void nsgtk_init_glade(void); -static char *nsgtk_find_resource(char *buf, const char *filename, - const char *def); static void nsgtk_check_homedir(void); static void *nsgtk_hubbub_realloc(void *ptr, size_t len, void *pw); static bool nsgtk_throbber_init(int framec); @@ -114,6 +112,71 @@ static void nsgtk_PDF_no_pass(GtkButton *w, gpointer data); #define THROBBER_FRAMES 9 +/** + * Locate a shared resource file by searching known places in order. + * + * \param buf buffer to write to. must be at least PATH_MAX chars. May be NULL and routine will allocate string which must be freed by caller. + * \param filename file to look for + * \param def default to return if file not found + * \return buf + * + * Search order is: ~/.netsurf/, $NETSURFRES/ (where NETSURFRES is an + * environment variable), and finally the path specified by the define + * GTK_RESPATH. + */ +static char * +nsgtk_find_resource(char *buf, const char *filename, const char *def) +{ + char *cdir = getenv("HOME"); + char t[PATH_MAX]; + + if (buf == NULL) { + buf = malloc(PATH_MAX); + if (buf == NULL) + return NULL; + } + + if (cdir != NULL) { + strcpy(t, cdir); + strcat(t, "/.netsurf/"); + strcat(t, filename); + if (realpath(t, buf) != NULL) { + if (access(buf, R_OK) == 0) + return buf; + } + } + + cdir = getenv("NETSURFRES"); + + if (cdir != NULL) { + if (realpath(cdir, buf) != NULL) { + strcat(buf, "/"); + strcat(buf, filename); + if (access(buf, R_OK) == 0) + return buf; + } + } + + strcpy(t, GTK_RESPATH); + strcat(t, filename); + if (realpath(t, buf) != NULL) { + if (access(buf, R_OK) == 0) + return buf; + } + + if (def[0] == '~') { + snprintf(t, PATH_MAX, "%s%s", getenv("HOME"), def + 1); + if (realpath(t, buf) == NULL) { + strcpy(buf, t); + } + } else { + if (realpath(def, buf) == NULL) { + strcpy(buf, def); + } + } + + return buf; +} /** @@ -421,70 +484,6 @@ void gui_quit(void) } -/** - * Locate a shared resource file by searching known places in order. - * - * \param buf buffer to write to. must be at least PATH_MAX chars. May be NULL and routine will allocate string which must be freed by caller. - * \param filename file to look for - * \param def default to return if file not found - * \return buf - * - * Search order is: ~/.netsurf/, $NETSURFRES/ (where NETSURFRES is an - * environment variable), and finally the path specified by the define - * GTK_RESPATH. - */ -char *nsgtk_find_resource(char *buf, const char *filename, const char *def) -{ - char *cdir = getenv("HOME"); - char t[PATH_MAX]; - - if (buf == NULL) { - buf = malloc(PATH_MAX); - if (buf == NULL) - return NULL; - } - - if (cdir != NULL) { - strcpy(t, cdir); - strcat(t, "/.netsurf/"); - strcat(t, filename); - if (realpath(t, buf) != NULL) { - if (access(buf, R_OK) == 0) - return buf; - } - } - - cdir = getenv("NETSURFRES"); - - if (cdir != NULL) { - if (realpath(cdir, buf) != NULL) { - strcat(buf, "/"); - strcat(buf, filename); - if (access(buf, R_OK) == 0) - return buf; - } - } - - strcpy(t, GTK_RESPATH); - strcat(t, filename); - if (realpath(t, buf) != NULL) { - if (access(buf, R_OK) == 0) - return buf; - } - - if (def[0] == '~') { - snprintf(t, PATH_MAX, "%s%s", getenv("HOME"), def + 1); - if (realpath(t, buf) == NULL) { - strcpy(buf, t); - } - } else { - if (realpath(def, buf) == NULL) { - strcpy(buf, def); - } - } - - return buf; -} /** @@ -729,9 +728,9 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len, char *path_to_url(const char *path) { - char *r = malloc(strlen(path) + SLEN("file://") + 1); + char *r = malloc(strlen(path) + FILE_SCHEME_PREFIX_LEN + 1); - strcpy(r, "file://"); + strcpy(r, FILE_SCHEME_PREFIX); strcat(r, path); return r; @@ -740,7 +739,7 @@ char *path_to_url(const char *path) char *url_to_path(const char *url) { - return strdup(url + 5); + return strdup(url + FILE_SCHEME_PREFIX_LEN); } diff --git a/gtk/gtk_scaffolding.c b/gtk/gtk_scaffolding.c index 381711377..bfb48104d 100644 --- a/gtk/gtk_scaffolding.c +++ b/gtk/gtk_scaffolding.c @@ -390,9 +390,9 @@ void nsgtk_openfile_open(const char *filename) { struct browser_window *bw = gui_window_get_browser_window( current_model->top_level); - char url[strlen(filename) + SLEN("file://") + 1]; + char url[strlen(filename) + FILE_SCHEME_PREFIX_LEN + 1]; - sprintf(url, "file://%s", filename); + sprintf(url, FILE_SCHEME_PREFIX"%s", filename); browser_window_go(bw, url, 0, true); diff --git a/riscos/gui.c b/riscos/gui.c index 007466f55..24723a87c 100644 --- a/riscos/gui.c +++ b/riscos/gui.c @@ -2074,10 +2074,10 @@ char *path_to_url(const char *path) return NULL; } - memcpy(url, "file://", SLEN("file://")); + memcpy(url, FILE_SCHEME_PREFIX, FILE_SCHEME_PREFIX_LEN); if (__unixify(buffer, __RISCOSIFY_NO_REVERSE_SUFFIX, - url + SLEN("file://"), - 1 - spare + 10 - SLEN("file://"), + url + FILE_SCHEME_PREFIX_LEN, + 1 - spare + 10 - FILE_SCHEME_PREFIX_LEN, 0) == NULL) { LOG(("__unixify failed: %s", buffer)); free(buffer); @@ -2087,7 +2087,7 @@ char *path_to_url(const char *path) free(buffer); buffer = NULL; /* We don't want '/' to be escaped. */ - url_err = url_escape(url, SLEN("file://"), false, "/", &escurl); + 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)); @@ -2110,7 +2110,7 @@ char *url_to_path(const char *url) char *temp_name, *r; char *filename; - if (strncmp(url, "file:///", 8)) + if (strncmp(url, FILE_SCHEME_PREFIX, FILE_SCHEME_PREFIX_LEN)) return NULL; temp_name = curl_unescape(url + 7, strlen(url) - 7); diff --git a/utils/url.h b/utils/url.h index f83c626f6..e0e18fb17 100644 --- a/utils/url.h +++ b/utils/url.h @@ -24,6 +24,10 @@ #ifndef _NETSURF_UTILS_URL_H_ #define _NETSURF_UTILS_URL_H_ +/* file url prefix */ +#define FILE_SCHEME_PREFIX "file:///" +#define FILE_SCHEME_PREFIX_LEN 8 + typedef enum { URL_FUNC_OK, /**< No error */ URL_FUNC_NOMEM, /**< Insufficient memory */ diff --git a/windows/findfile.c b/windows/findfile.c index f47e49d03..c3e613d3a 100644 --- a/windows/findfile.c +++ b/windows/findfile.c @@ -24,6 +24,7 @@ #include #include +#include "utils/url.h" #include "utils/log.h" #include "utils/utils.h" @@ -37,12 +38,19 @@ static char *realpath(const char *path, char *resolved_path) char *path_to_url(const char *path) { - char *r = malloc(strlen(path) + 7 + 1); + char *url = malloc(strlen(path) + FILE_SCHEME_PREFIX_LEN + 1); + char *sidx; - strcpy(r, "file://"); - strcat(r, path); + strcpy(url, FILE_SCHEME_PREFIX); + strcat(url, path); - return r; + sidx = strrchr(url, '\\'); + while (sidx != NULL) { + *sidx = '/'; + sidx = strrchr(url, '\\'); + } + + return url; } /** diff --git a/windows/findfile.h b/windows/findfile.h index f9b6746da..fcc2da94d 100644 --- a/windows/findfile.h +++ b/windows/findfile.h @@ -21,8 +21,6 @@ #define NETSURF_WINDOWS_RESPATH "C:" -char *path_to_url(const char *path); - extern char *nsws_find_resource(char *buf, const char *filename, const char *def); diff --git a/windows/gui.c b/windows/gui.c index c4fc442e9..ab36d92dd 100644 --- a/windows/gui.c +++ b/windows/gui.c @@ -44,6 +44,7 @@ #include "desktop/selection.h" #include "desktop/textinput.h" #include "render/html.h" +#include "utils/url.h" #include "utils/log.h" #include "utils/messages.h" #include "utils/utils.h"