2011-01-06 00:02:22 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2010 Ole Loots <ole@monochrom.net>
|
|
|
|
*
|
|
|
|
* 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 <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-04-24 00:09:24 +04:00
|
|
|
#include <assert.h>
|
2011-01-06 00:02:22 +03:00
|
|
|
#include <curl/curl.h>
|
2011-01-22 19:27:26 +03:00
|
|
|
#include <windom.h>
|
2011-01-06 00:02:22 +03:00
|
|
|
|
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/url.h"
|
2012-11-19 02:22:43 +04:00
|
|
|
#include "atari/gemtk/gemtk.h"
|
2011-01-06 00:02:22 +03:00
|
|
|
#include "atari/findfile.h"
|
2011-01-22 19:27:26 +03:00
|
|
|
#include "atari/gui.h"
|
|
|
|
#include "atari/misc.h"
|
2011-01-31 01:17:15 +03:00
|
|
|
#include "atari/osspec.h"
|
|
|
|
|
2011-12-07 01:06:41 +04:00
|
|
|
char * local_file_to_url( const char * filename )
|
|
|
|
{
|
|
|
|
#define BACKSLASH 0x5C
|
|
|
|
char * url;
|
|
|
|
if( strlen(filename) <= 2){
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2012-10-19 02:07:37 +04:00
|
|
|
char * fname_local = malloc( strlen(filename)+1 );
|
2011-12-07 01:06:41 +04:00
|
|
|
char * start = (char*)fname_local;
|
|
|
|
strcpy( start, filename );
|
|
|
|
|
|
|
|
/* if path points to unified filesystem, skip that info: */
|
|
|
|
if( fname_local[1] == ':' && fname_local[0] == 'U' ){
|
|
|
|
start = &fname_local[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we got something like "C:\folder\file.txt", handle that: */
|
|
|
|
if( start[1] == ':' ){
|
|
|
|
start[1] = (char)tolower(start[0]);
|
|
|
|
start++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip leading slash, already included in file scheme: */
|
|
|
|
if( start[0] == (char)BACKSLASH || start[0] == '/' ){
|
|
|
|
start++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert backslashes: */
|
2011-12-16 02:56:46 +04:00
|
|
|
for( unsigned int i=0; i<strlen(start); i++ ){
|
2011-12-07 01:06:41 +04:00
|
|
|
if( start[i] == BACKSLASH ){
|
|
|
|
start[i] = '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: make file path absolute if it isn't yet.
|
|
|
|
url = malloc( strlen(start) + FILE_SCHEME_PREFIX_LEN + 1);
|
|
|
|
strcpy( url, FILE_SCHEME_PREFIX );
|
|
|
|
strcat( url, start );
|
2012-10-19 02:07:37 +04:00
|
|
|
|
|
|
|
free(fname_local);
|
|
|
|
|
2011-12-07 01:06:41 +04:00
|
|
|
return( url );
|
|
|
|
#undef BACKSLASH
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert an local path to an URL, memory for URL is allocated. */
|
2011-04-24 00:09:24 +04:00
|
|
|
char *path_to_url(const char *path_in)
|
2011-01-06 00:02:22 +03:00
|
|
|
{
|
2011-12-07 01:06:41 +04:00
|
|
|
#define BACKSLASH 0x5C
|
2011-04-24 00:09:24 +04:00
|
|
|
char * path_ptr=NULL;
|
|
|
|
char * path;
|
|
|
|
LOG(("path2url in: %s\n", path_in));
|
|
|
|
|
|
|
|
if (*path_in == '/') {
|
|
|
|
path_in++; /* file: path is are already absolute */
|
|
|
|
path = (char*)path_in;
|
|
|
|
} else {
|
|
|
|
path = path_ptr = (char*)malloc(PATH_MAX+1);
|
|
|
|
gemdos_realpath(path_in, path);
|
2011-12-07 01:06:41 +04:00
|
|
|
|
|
|
|
if( *path == '/' || *path == BACKSLASH ) {
|
2011-04-24 00:09:24 +04:00
|
|
|
path++;
|
|
|
|
}
|
|
|
|
if( sys_type() != SYS_MINT ){
|
|
|
|
if( path[1] == ':' ) {
|
|
|
|
path[1] = path[0];
|
|
|
|
path++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-06 00:02:22 +03:00
|
|
|
int urllen = strlen(path) + FILE_SCHEME_PREFIX_LEN + 1;
|
|
|
|
char *url = malloc(urllen);
|
|
|
|
|
|
|
|
snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, path);
|
|
|
|
|
2011-04-11 01:49:27 +04:00
|
|
|
int i=0;
|
|
|
|
while( url[i] != 0 ){
|
2011-12-07 01:06:41 +04:00
|
|
|
if( url[i] == BACKSLASH ){
|
2011-04-11 01:49:27 +04:00
|
|
|
url[i] = '/';
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2011-12-07 01:06:41 +04:00
|
|
|
if( path_ptr )
|
2011-04-24 00:09:24 +04:00
|
|
|
free( path_ptr );
|
|
|
|
LOG(("path2url out: %s\n", url));
|
2011-01-06 00:02:22 +03:00
|
|
|
return url;
|
2011-12-07 01:06:41 +04:00
|
|
|
#undef BACKSLASH
|
2011-01-06 00:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *url_to_path(const char *url)
|
|
|
|
{
|
|
|
|
char *url_path = curl_unescape(url, 0);
|
|
|
|
char *path;
|
2011-04-24 00:09:24 +04:00
|
|
|
char abspath[PATH_MAX+1];
|
|
|
|
LOG(( "url2path in: %s\n", url ));
|
2011-04-11 01:49:27 +04:00
|
|
|
/* printf( "url2path in: %s\n", url_path ); */
|
2011-01-06 00:02:22 +03:00
|
|
|
/* return the absolute path including leading / */
|
2011-04-11 01:49:27 +04:00
|
|
|
/* todo: better check for filesystem? */
|
2011-03-01 22:27:34 +03:00
|
|
|
if( sys_type() & SYS_MINT ) {
|
2011-04-24 00:09:24 +04:00
|
|
|
/* it's ok to have relative paths with mint, just strip proto: */
|
|
|
|
path = strdup(url_path + (FILE_SCHEME_PREFIX_LEN -1));
|
2011-01-22 19:27:26 +03:00
|
|
|
} else {
|
2011-02-26 02:24:02 +03:00
|
|
|
/* do not include / within url_path */
|
2011-04-24 00:09:24 +04:00
|
|
|
char * tmp = url_path + (FILE_SCHEME_PREFIX_LEN-1);
|
|
|
|
gemdos_realpath( tmp, (char*)&abspath );
|
|
|
|
path = strdup( (char*)&abspath );
|
2011-01-22 19:27:26 +03:00
|
|
|
}
|
2011-01-06 00:02:22 +03:00
|
|
|
curl_free(url_path);
|
2011-04-24 00:09:24 +04:00
|
|
|
LOG(( "url2path out: %s\n", path ));
|
2011-01-06 00:02:22 +03:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2011-01-11 02:51:52 +03:00
|
|
|
|
2011-01-06 00:02:22 +03:00
|
|
|
/**
|
|
|
|
* Locate a shared resource file by searching known places in order.
|
|
|
|
*
|
|
|
|
* \param buf buffer to write to. must be at least PATH_MAX chars
|
|
|
|
* \param filename file to look for
|
|
|
|
* \param def default to return if file not found
|
|
|
|
* \return buf
|
|
|
|
*
|
|
|
|
* Search order is: ./, NETSURF_GEM_RESPATH, ./$HOME/.netsurf/, $NETSURFRES/ (where NETSURFRES is an
|
2011-12-07 01:06:41 +04:00
|
|
|
* environment variable),
|
2011-01-06 00:02:22 +03:00
|
|
|
*/
|
|
|
|
#ifndef NETSURF_GEM_RESPATH
|
2011-12-07 01:06:41 +04:00
|
|
|
#define NETSURF_GEM_RESPATH "./res/"
|
2011-01-06 00:02:22 +03:00
|
|
|
#endif
|
2011-01-22 19:27:26 +03:00
|
|
|
|
2011-01-06 00:02:22 +03:00
|
|
|
char * atari_find_resource(char *buf, const char *filename, const char *def)
|
|
|
|
{
|
|
|
|
char *cdir = NULL;
|
|
|
|
char t[PATH_MAX];
|
|
|
|
LOG(("%s (def: %s)", filename, def ));
|
|
|
|
strcpy(t, NETSURF_GEM_RESPATH);
|
|
|
|
strcat(t, filename);
|
|
|
|
LOG(("checking %s", (char*)&t));
|
2011-04-24 00:09:24 +04:00
|
|
|
if (gemdos_realpath(t, buf) != NULL) {
|
2011-01-06 00:02:22 +03:00
|
|
|
if (access(buf, R_OK) == 0) {
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strcpy(t, "./");
|
|
|
|
strcat(t, filename);
|
|
|
|
LOG(("checking %s", (char*)&t));
|
2011-04-24 00:09:24 +04:00
|
|
|
if (gemdos_realpath(t, buf) != NULL) {
|
2011-01-06 00:02:22 +03:00
|
|
|
if (access(buf, R_OK) == 0) {
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cdir = getenv("HOME");
|
|
|
|
if (cdir != NULL) {
|
|
|
|
strcpy(t, cdir);
|
|
|
|
strcat(t, "/.netsurf/");
|
|
|
|
strcat(t, filename);
|
|
|
|
LOG(("checking %s", (char*)&t));
|
2011-04-24 00:09:24 +04:00
|
|
|
if (gemdos_realpath(t, buf) != NULL) {
|
2011-01-06 00:02:22 +03:00
|
|
|
if (access(buf, R_OK) == 0)
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cdir = getenv("NETSURFRES");
|
|
|
|
if (cdir != NULL) {
|
2011-04-24 00:09:24 +04:00
|
|
|
if (gemdos_realpath(cdir, buf) != NULL) {
|
2011-01-06 00:02:22 +03:00
|
|
|
strcat(buf, "/");
|
|
|
|
strcat(buf, filename);
|
|
|
|
LOG(("checking %s", (char*)&t));
|
|
|
|
if (access(buf, R_OK) == 0)
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (def[0] == '~') {
|
|
|
|
snprintf(t, PATH_MAX, "%s%s", getenv("HOME"), def + 1);
|
|
|
|
LOG(("checking %s", (char*)&t));
|
2011-04-24 00:09:24 +04:00
|
|
|
if (gemdos_realpath(t, buf) == NULL) {
|
2011-01-06 00:02:22 +03:00
|
|
|
strcpy(buf, t);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(("checking %s", (char*)def));
|
2011-04-24 00:09:24 +04:00
|
|
|
if (gemdos_realpath(def, buf) == NULL) {
|
2011-01-06 00:02:22 +03:00
|
|
|
strcpy(buf, def);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local Variables:
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|