2010-09-10 01:45:59 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2010 Vincent Sanders <vince@netsurf-browser.org>
|
|
|
|
*
|
|
|
|
* This file is part of NetSurf.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2014-10-16 12:48:09 +04:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
*
|
|
|
|
* file scheme URL handling. Based on the data fetcher by Rob Kendrick
|
|
|
|
*/
|
2010-09-10 01:45:59 +04:00
|
|
|
|
2014-05-07 19:14:18 +04:00
|
|
|
#include "utils/config.h"
|
|
|
|
|
2010-09-10 01:45:59 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2012-08-13 20:00:43 +04:00
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
|
2011-09-27 01:07:19 +04:00
|
|
|
#include <libwapcaplet/libwapcaplet.h>
|
|
|
|
|
2016-04-19 13:21:06 +03:00
|
|
|
#include "utils/dirent.h"
|
2014-01-24 23:58:51 +04:00
|
|
|
#include "utils/corestrings.h"
|
2013-05-26 01:46:27 +04:00
|
|
|
#include "utils/nsoption.h"
|
2012-10-06 17:07:34 +04:00
|
|
|
#include "utils/errors.h"
|
2010-09-10 01:45:59 +04:00
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/messages.h"
|
|
|
|
#include "utils/utils.h"
|
|
|
|
#include "utils/ring.h"
|
2014-05-07 19:14:18 +04:00
|
|
|
#include "utils/file.h"
|
2014-10-16 12:48:09 +04:00
|
|
|
#include "desktop/gui_fetch.h"
|
|
|
|
#include "desktop/gui_internal.h"
|
2014-05-07 19:14:18 +04:00
|
|
|
|
|
|
|
#include "content/dirlist.h"
|
|
|
|
#include "content/fetch.h"
|
2014-06-19 21:27:24 +04:00
|
|
|
#include "content/fetchers.h"
|
2014-05-07 19:14:18 +04:00
|
|
|
#include "content/urldb.h"
|
|
|
|
#include "content/fetchers/file.h"
|
2010-09-10 01:45:59 +04:00
|
|
|
|
|
|
|
/* Maximum size of read buffer */
|
|
|
|
#define FETCH_FILE_MAX_BUF_SIZE (1024 * 1024)
|
|
|
|
|
|
|
|
/** Context for a fetch */
|
|
|
|
struct fetch_file_context {
|
|
|
|
struct fetch_file_context *r_next, *r_prev;
|
|
|
|
|
|
|
|
struct fetch *fetchh; /**< Handle for this fetch */
|
|
|
|
|
|
|
|
bool aborted; /**< Flag indicating fetch has been aborted */
|
|
|
|
bool locked; /**< Flag indicating entry is already entered */
|
|
|
|
|
2011-09-29 19:31:54 +04:00
|
|
|
nsurl *url; /**< The full url the fetch refers to */
|
2010-09-10 01:45:59 +04:00
|
|
|
char *path; /**< The actual path to be used with open() */
|
2010-09-16 00:32:14 +04:00
|
|
|
|
|
|
|
time_t file_etag; /**< Request etag for file (previous st.m_time) */
|
2010-09-10 01:45:59 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct fetch_file_context *ring = NULL;
|
|
|
|
|
|
|
|
/** issue fetch callbacks with locking */
|
2011-11-09 01:51:42 +04:00
|
|
|
static inline bool fetch_file_send_callback(const fetch_msg *msg,
|
|
|
|
struct fetch_file_context *ctx)
|
2010-09-10 01:45:59 +04:00
|
|
|
{
|
|
|
|
ctx->locked = true;
|
2011-11-09 01:51:42 +04:00
|
|
|
fetch_send_callback(msg, ctx->fetchh);
|
2010-09-10 01:45:59 +04:00
|
|
|
ctx->locked = false;
|
|
|
|
|
|
|
|
return ctx->aborted;
|
|
|
|
}
|
|
|
|
|
2010-09-13 22:34:11 +04:00
|
|
|
static bool fetch_file_send_header(struct fetch_file_context *ctx,
|
|
|
|
const char *fmt, ...)
|
2010-09-10 01:45:59 +04:00
|
|
|
{
|
2011-11-09 01:51:42 +04:00
|
|
|
fetch_msg msg;
|
2010-09-10 01:45:59 +04:00
|
|
|
char header[64];
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
|
|
|
vsnprintf(header, sizeof header, fmt, ap);
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.type = FETCH_HEADER;
|
|
|
|
msg.data.header_or_data.buf = (const uint8_t *) header;
|
|
|
|
msg.data.header_or_data.len = strlen(header);
|
|
|
|
fetch_file_send_callback(&msg, ctx);
|
2010-09-10 01:45:59 +04:00
|
|
|
|
|
|
|
return ctx->aborted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** callback to initialise the file fetcher. */
|
2011-09-27 01:07:19 +04:00
|
|
|
static bool fetch_file_initialise(lwc_string *scheme)
|
2010-09-10 01:45:59 +04:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** callback to initialise the file fetcher. */
|
2011-09-27 01:07:19 +04:00
|
|
|
static void fetch_file_finalise(lwc_string *scheme)
|
2010-09-10 01:45:59 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-11-27 18:14:36 +04:00
|
|
|
static bool fetch_file_can_fetch(const nsurl *url)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-10 01:45:59 +04:00
|
|
|
/** callback to set up a file fetch context. */
|
|
|
|
static void *
|
|
|
|
fetch_file_setup(struct fetch *fetchh,
|
2011-09-29 19:31:54 +04:00
|
|
|
nsurl *url,
|
2010-09-10 01:45:59 +04:00
|
|
|
bool only_2xx,
|
2013-01-05 03:13:23 +04:00
|
|
|
bool downgrade_tls,
|
2010-09-10 01:45:59 +04:00
|
|
|
const char *post_urlenc,
|
|
|
|
const struct fetch_multipart_data *post_multipart,
|
|
|
|
const char **headers)
|
|
|
|
{
|
|
|
|
struct fetch_file_context *ctx;
|
2010-09-16 00:32:14 +04:00
|
|
|
int i;
|
2014-05-27 02:43:36 +04:00
|
|
|
nserror ret;
|
2010-09-10 01:45:59 +04:00
|
|
|
|
|
|
|
ctx = calloc(1, sizeof(*ctx));
|
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2014-05-27 02:43:36 +04:00
|
|
|
ret = guit->file->nsurl_to_path(url, &ctx->path);
|
|
|
|
if (ret != NSERROR_OK) {
|
2010-09-10 01:45:59 +04:00
|
|
|
free(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-29 19:31:54 +04:00
|
|
|
ctx->url = nsurl_ref(url);
|
2010-09-10 01:45:59 +04:00
|
|
|
|
2010-09-16 00:32:14 +04:00
|
|
|
/* Scan request headers looking for If-None-Match */
|
|
|
|
for (i = 0; headers[i] != NULL; i++) {
|
|
|
|
if (strncasecmp(headers[i], "If-None-Match:",
|
|
|
|
SLEN("If-None-Match:")) == 0) {
|
|
|
|
/* If-None-Match: "12345678" */
|
|
|
|
const char *d = headers[i] + SLEN("If-None-Match:");
|
|
|
|
|
|
|
|
/* Scan to first digit, if any */
|
|
|
|
while (*d != '\0' && (*d < '0' || '9' < *d))
|
|
|
|
d++;
|
|
|
|
|
|
|
|
/* Convert to time_t */
|
|
|
|
if (*d != '\0')
|
|
|
|
ctx->file_etag = atoi(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-10 01:45:59 +04:00
|
|
|
ctx->fetchh = fetchh;
|
|
|
|
|
|
|
|
RING_INSERT(ring, ctx);
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** callback to free a file fetch */
|
|
|
|
static void fetch_file_free(void *ctx)
|
|
|
|
{
|
|
|
|
struct fetch_file_context *c = ctx;
|
2011-09-29 19:31:54 +04:00
|
|
|
nsurl_unref(c->url);
|
2010-09-10 01:45:59 +04:00
|
|
|
free(c->path);
|
|
|
|
RING_REMOVE(ring, c);
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** callback to start a file fetch */
|
|
|
|
static bool fetch_file_start(void *ctx)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** callback to abort a file fetch */
|
|
|
|
static void fetch_file_abort(void *ctx)
|
|
|
|
{
|
|
|
|
struct fetch_file_context *c = ctx;
|
|
|
|
|
|
|
|
/* To avoid the poll loop having to deal with the fetch context
|
|
|
|
* disappearing from under it, we simply flag the abort here.
|
|
|
|
* The poll loop itself will perform the appropriate cleanup.
|
|
|
|
*/
|
|
|
|
c->aborted = true;
|
|
|
|
}
|
|
|
|
|
2010-09-13 22:34:11 +04:00
|
|
|
static int fetch_file_errno_to_http_code(int error_no)
|
|
|
|
{
|
|
|
|
switch (error_no) {
|
|
|
|
case ENAMETOOLONG:
|
|
|
|
return 400;
|
|
|
|
case EACCES:
|
|
|
|
return 403;
|
|
|
|
case ENOENT:
|
|
|
|
return 404;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 500;
|
|
|
|
}
|
|
|
|
|
2010-09-10 01:45:59 +04:00
|
|
|
static void fetch_file_process_error(struct fetch_file_context *ctx, int code)
|
|
|
|
{
|
2011-11-09 01:51:42 +04:00
|
|
|
fetch_msg msg;
|
2010-09-10 01:45:59 +04:00
|
|
|
char buffer[1024];
|
|
|
|
const char *title;
|
|
|
|
char key[8];
|
|
|
|
|
|
|
|
/* content is going to return error code */
|
|
|
|
fetch_set_http_code(ctx->fetchh, code);
|
|
|
|
|
|
|
|
/* content type */
|
|
|
|
if (fetch_file_send_header(ctx, "Content-Type: text/html"))
|
|
|
|
goto fetch_file_process_error_aborted;
|
|
|
|
|
|
|
|
snprintf(key, sizeof key, "HTTP%03d", code);
|
|
|
|
title = messages_get(key);
|
|
|
|
|
2010-09-13 22:34:11 +04:00
|
|
|
snprintf(buffer, sizeof buffer, "<html><head><title>%s</title></head>"
|
|
|
|
"<body><h1>%s</h1>"
|
|
|
|
"<p>Error %d while fetching file %s</p></body></html>",
|
2011-09-29 19:31:54 +04:00
|
|
|
title, title, code, nsurl_access(ctx->url));
|
2010-09-10 01:45:59 +04:00
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.type = FETCH_DATA;
|
|
|
|
msg.data.header_or_data.buf = (const uint8_t *) buffer;
|
|
|
|
msg.data.header_or_data.len = strlen(buffer);
|
|
|
|
if (fetch_file_send_callback(&msg, ctx))
|
2010-09-10 01:45:59 +04:00
|
|
|
goto fetch_file_process_error_aborted;
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.type = FETCH_FINISHED;
|
|
|
|
fetch_file_send_callback(&msg, ctx);
|
2010-09-10 01:45:59 +04:00
|
|
|
|
|
|
|
fetch_file_process_error_aborted:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Process object as a regular file */
|
|
|
|
static void fetch_file_process_plain(struct fetch_file_context *ctx,
|
2010-09-11 22:47:36 +04:00
|
|
|
struct stat *fdstat)
|
2010-09-10 01:45:59 +04:00
|
|
|
{
|
2012-08-13 20:00:43 +04:00
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
fetch_msg msg;
|
2012-09-20 01:50:28 +04:00
|
|
|
char *buf = NULL;
|
2012-08-13 20:00:43 +04:00
|
|
|
size_t buf_size;
|
|
|
|
|
|
|
|
int fd; /**< The file descriptor of the object */
|
|
|
|
|
|
|
|
/* Check if we can just return not modified */
|
|
|
|
if (ctx->file_etag != 0 && ctx->file_etag == fdstat->st_mtime) {
|
|
|
|
fetch_set_http_code(ctx->fetchh, 304);
|
|
|
|
msg.type = FETCH_NOTMODIFIED;
|
|
|
|
fetch_file_send_callback(&msg, ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(ctx->path, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
/* process errors as appropriate */
|
|
|
|
fetch_file_process_error(ctx,
|
|
|
|
fetch_file_errno_to_http_code(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set buffer size */
|
|
|
|
buf_size = fdstat->st_size;
|
|
|
|
|
|
|
|
/* allocate the buffer storage */
|
2012-09-20 01:50:28 +04:00
|
|
|
if (buf_size > 0) {
|
|
|
|
buf = mmap(NULL, buf_size, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
|
if (buf == MAP_FAILED) {
|
|
|
|
msg.type = FETCH_ERROR;
|
|
|
|
msg.data.error = "Unable to map memory for file data buffer";
|
|
|
|
fetch_file_send_callback(&msg, ctx);
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-13 20:00:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* fetch is going to be successful */
|
|
|
|
fetch_set_http_code(ctx->fetchh, 200);
|
|
|
|
|
|
|
|
/* Any callback can result in the fetch being aborted.
|
|
|
|
* Therefore, we _must_ check for this after _every_ call to
|
|
|
|
* fetch_file_send_callback().
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* content type */
|
|
|
|
if (fetch_file_send_header(ctx, "Content-Type: %s",
|
2016-03-18 00:55:12 +03:00
|
|
|
guit->fetch->filetype(ctx->path))) {
|
2012-08-13 20:00:43 +04:00
|
|
|
goto fetch_file_process_aborted;
|
2016-03-18 00:55:12 +03:00
|
|
|
}
|
2012-08-13 20:00:43 +04:00
|
|
|
|
|
|
|
/* content length */
|
2016-03-18 00:55:12 +03:00
|
|
|
if (fetch_file_send_header(ctx, "Content-Length: %" PRIsizet,
|
|
|
|
fdstat->st_size)) {
|
2012-08-13 20:00:43 +04:00
|
|
|
goto fetch_file_process_aborted;
|
2016-03-18 00:55:12 +03:00
|
|
|
}
|
2012-08-13 20:00:43 +04:00
|
|
|
|
|
|
|
/* create etag */
|
|
|
|
if (fetch_file_send_header(ctx, "ETag: \"%10" PRId64 "\"",
|
2016-03-18 00:55:12 +03:00
|
|
|
(int64_t) fdstat->st_mtime)) {
|
2012-08-13 20:00:43 +04:00
|
|
|
goto fetch_file_process_aborted;
|
2016-03-18 00:55:12 +03:00
|
|
|
}
|
2012-08-13 20:00:43 +04:00
|
|
|
|
|
|
|
msg.type = FETCH_DATA;
|
|
|
|
msg.data.header_or_data.buf = (const uint8_t *) buf;
|
|
|
|
msg.data.header_or_data.len = buf_size;
|
|
|
|
fetch_file_send_callback(&msg, ctx);
|
|
|
|
|
|
|
|
if (ctx->aborted == false) {
|
|
|
|
msg.type = FETCH_FINISHED;
|
|
|
|
fetch_file_send_callback(&msg, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
fetch_file_process_aborted:
|
|
|
|
|
2012-09-20 01:50:28 +04:00
|
|
|
if (buf != NULL)
|
|
|
|
munmap(buf, buf_size);
|
2012-08-13 20:00:43 +04:00
|
|
|
close(fd);
|
|
|
|
#else
|
2011-11-09 01:51:42 +04:00
|
|
|
fetch_msg msg;
|
2010-09-10 01:45:59 +04:00
|
|
|
char *buf;
|
|
|
|
size_t buf_size;
|
|
|
|
|
|
|
|
ssize_t tot_read = 0;
|
|
|
|
ssize_t res;
|
|
|
|
|
2012-08-13 20:00:43 +04:00
|
|
|
FILE *infile;
|
2010-09-16 00:32:14 +04:00
|
|
|
|
|
|
|
/* Check if we can just return not modified */
|
|
|
|
if (ctx->file_etag != 0 && ctx->file_etag == fdstat->st_mtime) {
|
|
|
|
fetch_set_http_code(ctx->fetchh, 304);
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.type = FETCH_NOTMODIFIED;
|
|
|
|
fetch_file_send_callback(&msg, ctx);
|
2010-09-16 00:32:14 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-13 20:00:43 +04:00
|
|
|
infile = fopen(ctx->path, "rb");
|
|
|
|
if (infile == NULL) {
|
2010-09-11 22:47:36 +04:00
|
|
|
/* process errors as appropriate */
|
2010-09-13 22:34:11 +04:00
|
|
|
fetch_file_process_error(ctx,
|
|
|
|
fetch_file_errno_to_http_code(errno));
|
2010-09-11 22:47:36 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-10 01:45:59 +04:00
|
|
|
/* set buffer size */
|
|
|
|
buf_size = fdstat->st_size;
|
|
|
|
if (buf_size > FETCH_FILE_MAX_BUF_SIZE)
|
|
|
|
buf_size = FETCH_FILE_MAX_BUF_SIZE;
|
|
|
|
|
|
|
|
/* allocate the buffer storage */
|
|
|
|
buf = malloc(buf_size);
|
|
|
|
if (buf == NULL) {
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.type = FETCH_ERROR;
|
|
|
|
msg.data.error =
|
|
|
|
"Unable to allocate memory for file data buffer";
|
|
|
|
fetch_file_send_callback(&msg, ctx);
|
2012-08-13 20:00:43 +04:00
|
|
|
fclose(infile);
|
2010-09-10 01:45:59 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fetch is going to be successful */
|
|
|
|
fetch_set_http_code(ctx->fetchh, 200);
|
|
|
|
|
|
|
|
/* Any callback can result in the fetch being aborted.
|
|
|
|
* Therefore, we _must_ check for this after _every_ call to
|
|
|
|
* fetch_file_send_callback().
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* content type */
|
2010-09-13 22:34:11 +04:00
|
|
|
if (fetch_file_send_header(ctx, "Content-Type: %s",
|
2016-03-18 00:55:12 +03:00
|
|
|
guit->fetch->filetype(ctx->path))) {
|
2010-09-10 01:45:59 +04:00
|
|
|
goto fetch_file_process_aborted;
|
2016-03-18 00:55:12 +03:00
|
|
|
}
|
2010-09-10 01:45:59 +04:00
|
|
|
|
|
|
|
/* content length */
|
2016-03-18 00:55:12 +03:00
|
|
|
if (fetch_file_send_header(ctx, "Content-Length: %" PRIsizet,
|
|
|
|
fdstat->st_size)) {
|
2010-09-10 01:45:59 +04:00
|
|
|
goto fetch_file_process_aborted;
|
2016-03-18 00:55:12 +03:00
|
|
|
}
|
2010-09-10 01:45:59 +04:00
|
|
|
|
|
|
|
/* create etag */
|
2010-09-13 22:34:11 +04:00
|
|
|
if (fetch_file_send_header(ctx, "ETag: \"%10" PRId64 "\"",
|
2016-03-18 00:55:12 +03:00
|
|
|
(int64_t) fdstat->st_mtime)) {
|
2010-09-10 01:45:59 +04:00
|
|
|
goto fetch_file_process_aborted;
|
2016-03-18 00:55:12 +03:00
|
|
|
}
|
2010-09-10 01:45:59 +04:00
|
|
|
|
|
|
|
/* main data loop */
|
2011-09-08 12:25:48 +04:00
|
|
|
while (tot_read < fdstat->st_size) {
|
2012-08-13 20:00:43 +04:00
|
|
|
res = fread(buf, 1, buf_size, infile);
|
2010-09-10 01:45:59 +04:00
|
|
|
if (res == 0) {
|
2012-08-13 20:00:43 +04:00
|
|
|
if (feof(infile)) {
|
|
|
|
msg.type = FETCH_ERROR;
|
|
|
|
msg.data.error = "Unexpected EOF reading file";
|
|
|
|
fetch_file_send_callback(&msg, ctx);
|
|
|
|
goto fetch_file_process_aborted;
|
|
|
|
} else {
|
|
|
|
msg.type = FETCH_ERROR;
|
|
|
|
msg.data.error = "Error reading file";
|
|
|
|
fetch_file_send_callback(&msg, ctx);
|
|
|
|
goto fetch_file_process_aborted;
|
|
|
|
}
|
2010-09-10 01:45:59 +04:00
|
|
|
}
|
|
|
|
tot_read += res;
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.type = FETCH_DATA;
|
|
|
|
msg.data.header_or_data.buf = (const uint8_t *) buf;
|
|
|
|
msg.data.header_or_data.len = res;
|
|
|
|
if (fetch_file_send_callback(&msg, ctx))
|
2010-09-10 01:45:59 +04:00
|
|
|
break;
|
2011-09-08 12:25:48 +04:00
|
|
|
}
|
2010-09-10 01:45:59 +04:00
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
if (ctx->aborted == false) {
|
|
|
|
msg.type = FETCH_FINISHED;
|
|
|
|
fetch_file_send_callback(&msg, ctx);
|
|
|
|
}
|
2010-09-10 01:45:59 +04:00
|
|
|
|
|
|
|
fetch_file_process_aborted:
|
|
|
|
|
2012-08-13 20:00:43 +04:00
|
|
|
fclose(infile);
|
2010-09-10 01:45:59 +04:00
|
|
|
free(buf);
|
2012-08-13 20:00:43 +04:00
|
|
|
#endif
|
2010-09-10 01:45:59 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *gen_nice_title(char *path)
|
|
|
|
{
|
|
|
|
char *nice_path, *cnv, *tmp;
|
|
|
|
char *title;
|
|
|
|
int title_length;
|
|
|
|
|
|
|
|
/* Convert path for display */
|
|
|
|
nice_path = malloc(strlen(path) * SLEN("&") + 1);
|
|
|
|
if (nice_path == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Escape special HTML characters */
|
|
|
|
for (cnv = nice_path, tmp = path; *tmp != '\0'; tmp++) {
|
|
|
|
if (*tmp == '<') {
|
|
|
|
*cnv++ = '&';
|
|
|
|
*cnv++ = 'l';
|
|
|
|
*cnv++ = 't';
|
|
|
|
*cnv++ = ';';
|
|
|
|
} else if (*tmp == '>') {
|
|
|
|
*cnv++ = '&';
|
|
|
|
*cnv++ = 'g';
|
|
|
|
*cnv++ = 't';
|
|
|
|
*cnv++ = ';';
|
|
|
|
} else if (*tmp == '&') {
|
|
|
|
*cnv++ = '&';
|
|
|
|
*cnv++ = 'a';
|
|
|
|
*cnv++ = 'm';
|
|
|
|
*cnv++ = 'p';
|
|
|
|
*cnv++ = ';';
|
|
|
|
} else {
|
|
|
|
*cnv++ = *tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*cnv = '\0';
|
|
|
|
|
|
|
|
/* Construct a localised title string */
|
|
|
|
title_length = (cnv - nice_path) + strlen(messages_get("FileIndex"));
|
|
|
|
title = malloc(title_length + 1);
|
|
|
|
|
|
|
|
if (title == NULL) {
|
|
|
|
free(nice_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set title to localised "Index of <nice_path>" */
|
|
|
|
snprintf(title, title_length, messages_get("FileIndex"), nice_path);
|
|
|
|
|
|
|
|
free(nice_path);
|
|
|
|
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
|
2014-05-07 19:14:18 +04:00
|
|
|
/**
|
2014-11-08 15:35:11 +03:00
|
|
|
* Generate an output row of the directory listing.
|
2014-05-07 19:14:18 +04:00
|
|
|
*
|
2014-11-08 15:35:11 +03:00
|
|
|
* \param ctx The file fetching context.
|
|
|
|
* \param ent current directory entry.
|
|
|
|
* \param even is the row an even row.
|
|
|
|
* \param buffer The output buffer.
|
|
|
|
* \param buffer_len The space available in the output buffer.
|
|
|
|
* \return NSERROR_OK or error code on faliure.
|
2014-05-07 19:14:18 +04:00
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
process_dir_ent(struct fetch_file_context *ctx,
|
|
|
|
struct dirent *ent,
|
|
|
|
bool even,
|
|
|
|
char *buffer,
|
|
|
|
size_t buffer_len)
|
|
|
|
{
|
|
|
|
nserror ret;
|
|
|
|
char *urlpath = NULL; /* buffer for leaf entry path */
|
|
|
|
struct stat ent_stat; /* stat result of leaf entry */
|
|
|
|
char datebuf[64]; /* buffer for date text */
|
|
|
|
char timebuf[64]; /* buffer for time text */
|
2014-05-27 02:43:36 +04:00
|
|
|
nsurl *url;
|
2014-05-07 19:14:18 +04:00
|
|
|
|
|
|
|
/* skip hidden files */
|
|
|
|
if (ent->d_name[0] == '.') {
|
|
|
|
return NSERROR_BAD_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = netsurf_mkpath(&urlpath, NULL, 2, ctx->path, ent->d_name);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat(urlpath, &ent_stat) != 0) {
|
|
|
|
ent_stat.st_mode = 0;
|
|
|
|
datebuf[0] = 0;
|
|
|
|
timebuf[0] = 0;
|
|
|
|
} else {
|
|
|
|
/* Get date in output format */
|
|
|
|
if (strftime((char *)&datebuf, sizeof datebuf, "%a %d %b %Y",
|
|
|
|
localtime(&ent_stat.st_mtime)) == 0) {
|
|
|
|
datebuf[0] = '-';
|
|
|
|
datebuf[1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get time in output format */
|
|
|
|
if (strftime((char *)&timebuf, sizeof timebuf, "%H:%M",
|
|
|
|
localtime(&ent_stat.st_mtime)) == 0) {
|
|
|
|
timebuf[0] = '-';
|
|
|
|
timebuf[1] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 02:43:36 +04:00
|
|
|
ret = guit->file->path_to_nsurl(urlpath, &url);
|
|
|
|
if (ret != NSERROR_OK) {
|
2014-05-07 19:14:18 +04:00
|
|
|
free(urlpath);
|
2014-05-27 02:43:36 +04:00
|
|
|
return ret;
|
2014-05-07 19:14:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISREG(ent_stat.st_mode)) {
|
|
|
|
/* regular file */
|
|
|
|
dirlist_generate_row(even,
|
|
|
|
false,
|
2014-05-27 02:43:36 +04:00
|
|
|
url,
|
2014-05-07 19:14:18 +04:00
|
|
|
ent->d_name,
|
|
|
|
guit->fetch->filetype(urlpath),
|
|
|
|
ent_stat.st_size,
|
|
|
|
datebuf, timebuf,
|
|
|
|
buffer, buffer_len);
|
|
|
|
} else if (S_ISDIR(ent_stat.st_mode)) {
|
|
|
|
/* directory */
|
|
|
|
dirlist_generate_row(even,
|
|
|
|
true,
|
2014-05-27 02:43:36 +04:00
|
|
|
url,
|
2014-05-07 19:14:18 +04:00
|
|
|
ent->d_name,
|
|
|
|
messages_get("FileDirectory"),
|
|
|
|
-1,
|
|
|
|
datebuf, timebuf,
|
|
|
|
buffer, buffer_len);
|
|
|
|
} else {
|
|
|
|
/* something else */
|
|
|
|
dirlist_generate_row(even,
|
|
|
|
false,
|
2014-05-27 02:43:36 +04:00
|
|
|
url,
|
2014-05-07 19:14:18 +04:00
|
|
|
ent->d_name,
|
|
|
|
"",
|
|
|
|
-1,
|
|
|
|
datebuf, timebuf,
|
|
|
|
buffer, buffer_len);
|
|
|
|
}
|
|
|
|
|
2014-05-27 02:43:36 +04:00
|
|
|
nsurl_unref(url);
|
2014-05-07 19:14:18 +04:00
|
|
|
free(urlpath);
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2010-09-10 01:45:59 +04:00
|
|
|
|
|
|
|
static void fetch_file_process_dir(struct fetch_file_context *ctx,
|
2010-09-11 22:47:36 +04:00
|
|
|
struct stat *fdstat)
|
2010-09-10 01:45:59 +04:00
|
|
|
{
|
2011-11-09 01:51:42 +04:00
|
|
|
fetch_msg msg;
|
2010-09-10 01:45:59 +04:00
|
|
|
char buffer[1024]; /* Output buffer */
|
|
|
|
bool even = false; /* formatting flag */
|
|
|
|
char *title; /* pretty printed title */
|
2012-10-06 17:07:34 +04:00
|
|
|
nserror err; /* result from url routines */
|
|
|
|
nsurl *up; /* url of parent */
|
2010-09-10 01:45:59 +04:00
|
|
|
|
2013-11-11 14:58:55 +04:00
|
|
|
struct dirent **listing = NULL; /* directory entry listing */
|
2013-11-11 02:02:12 +04:00
|
|
|
int i; /* directory entry index */
|
|
|
|
int n; /* number of directory entries */
|
2010-09-10 01:45:59 +04:00
|
|
|
|
2013-11-15 01:01:51 +04:00
|
|
|
n = scandir(ctx->path, &listing, 0, dir_sort_alpha);
|
2013-11-11 02:02:12 +04:00
|
|
|
if (n < 0) {
|
2010-09-13 22:34:11 +04:00
|
|
|
fetch_file_process_error(ctx,
|
|
|
|
fetch_file_errno_to_http_code(errno));
|
2010-09-10 01:45:59 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fetch is going to be successful */
|
|
|
|
fetch_set_http_code(ctx->fetchh, 200);
|
|
|
|
|
2010-09-16 00:32:14 +04:00
|
|
|
/* force no-cache */
|
|
|
|
if (fetch_file_send_header(ctx, "Cache-Control: no-cache"))
|
|
|
|
goto fetch_file_process_dir_aborted;
|
|
|
|
|
2010-09-10 01:45:59 +04:00
|
|
|
/* content type */
|
|
|
|
if (fetch_file_send_header(ctx, "Content-Type: text/html"))
|
|
|
|
goto fetch_file_process_dir_aborted;
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.type = FETCH_DATA;
|
|
|
|
msg.data.header_or_data.buf = (const uint8_t *) buffer;
|
|
|
|
|
2010-09-10 01:45:59 +04:00
|
|
|
/* directory listing top */
|
|
|
|
dirlist_generate_top(buffer, sizeof buffer);
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.data.header_or_data.len = strlen(buffer);
|
|
|
|
if (fetch_file_send_callback(&msg, ctx))
|
2010-09-10 01:45:59 +04:00
|
|
|
goto fetch_file_process_dir_aborted;
|
|
|
|
|
|
|
|
/* directory listing title */
|
|
|
|
title = gen_nice_title(ctx->path);
|
|
|
|
dirlist_generate_title(title, buffer, sizeof buffer);
|
|
|
|
free(title);
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.data.header_or_data.len = strlen(buffer);
|
|
|
|
if (fetch_file_send_callback(&msg, ctx))
|
2010-09-10 01:45:59 +04:00
|
|
|
goto fetch_file_process_dir_aborted;
|
|
|
|
|
|
|
|
/* Print parent directory link */
|
2012-10-06 17:07:34 +04:00
|
|
|
err = nsurl_parent(ctx->url, &up);
|
|
|
|
if (err == NSERROR_OK) {
|
|
|
|
if (nsurl_compare(ctx->url, up, NSURL_COMPLETE) == false) {
|
|
|
|
/* different URL; have parent */
|
|
|
|
dirlist_generate_parent_link(nsurl_access(up),
|
|
|
|
buffer, sizeof buffer);
|
2010-09-10 01:45:59 +04:00
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.data.header_or_data.len = strlen(buffer);
|
|
|
|
fetch_file_send_callback(&msg, ctx);
|
2010-09-10 01:45:59 +04:00
|
|
|
}
|
2012-10-06 17:07:34 +04:00
|
|
|
nsurl_unref(up);
|
2010-09-10 01:45:59 +04:00
|
|
|
|
|
|
|
if (ctx->aborted)
|
|
|
|
goto fetch_file_process_dir_aborted;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* directory list headings */
|
|
|
|
dirlist_generate_headings(buffer, sizeof buffer);
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.data.header_or_data.len = strlen(buffer);
|
|
|
|
if (fetch_file_send_callback(&msg, ctx))
|
2010-09-10 01:45:59 +04:00
|
|
|
goto fetch_file_process_dir_aborted;
|
|
|
|
|
2013-11-11 02:02:12 +04:00
|
|
|
for (i = 0; i < n; i++) {
|
2010-09-10 01:45:59 +04:00
|
|
|
|
2014-05-07 19:14:18 +04:00
|
|
|
err = process_dir_ent(ctx, listing[i], even, buffer,
|
|
|
|
sizeof(buffer));
|
2010-09-10 01:45:59 +04:00
|
|
|
|
2014-05-07 19:14:18 +04:00
|
|
|
if (err == NSERROR_OK) {
|
|
|
|
msg.data.header_or_data.len = strlen(buffer);
|
|
|
|
if (fetch_file_send_callback(&msg, ctx))
|
|
|
|
goto fetch_file_process_dir_aborted;
|
2010-09-10 01:45:59 +04:00
|
|
|
|
2014-05-07 19:14:18 +04:00
|
|
|
even = !even;
|
2010-09-10 01:45:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* directory listing bottom */
|
|
|
|
dirlist_generate_bottom(buffer, sizeof buffer);
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.data.header_or_data.len = strlen(buffer);
|
|
|
|
if (fetch_file_send_callback(&msg, ctx))
|
2010-09-10 01:45:59 +04:00
|
|
|
goto fetch_file_process_dir_aborted;
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.type = FETCH_FINISHED;
|
|
|
|
fetch_file_send_callback(&msg, ctx);
|
2010-09-10 01:45:59 +04:00
|
|
|
|
|
|
|
fetch_file_process_dir_aborted:
|
|
|
|
|
2013-11-11 02:02:12 +04:00
|
|
|
if (listing != NULL) {
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
free(listing[i]);
|
|
|
|
}
|
|
|
|
free(listing);
|
|
|
|
}
|
2010-09-10 01:45:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* process a file fetch */
|
|
|
|
static void fetch_file_process(struct fetch_file_context *ctx)
|
|
|
|
{
|
|
|
|
struct stat fdstat; /**< The objects stat */
|
|
|
|
|
2010-09-11 22:47:36 +04:00
|
|
|
if (stat(ctx->path, &fdstat) != 0) {
|
2010-09-10 01:45:59 +04:00
|
|
|
/* process errors as appropriate */
|
2010-09-13 22:34:11 +04:00
|
|
|
fetch_file_process_error(ctx,
|
|
|
|
fetch_file_errno_to_http_code(errno));
|
2010-09-10 01:45:59 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(fdstat.st_mode)) {
|
|
|
|
/* directory listing */
|
2010-09-11 22:47:36 +04:00
|
|
|
fetch_file_process_dir(ctx, &fdstat);
|
2010-09-10 01:45:59 +04:00
|
|
|
return;
|
|
|
|
} else if (S_ISREG(fdstat.st_mode)) {
|
|
|
|
/* regular file */
|
2010-09-11 22:47:36 +04:00
|
|
|
fetch_file_process_plain(ctx, &fdstat);
|
2010-09-10 01:45:59 +04:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
/* unhandled type of file */
|
|
|
|
fetch_file_process_error(ctx, 501);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** callback to poll for additional file fetch contents */
|
2011-09-27 01:07:19 +04:00
|
|
|
static void fetch_file_poll(lwc_string *scheme)
|
2010-09-10 01:45:59 +04:00
|
|
|
{
|
|
|
|
struct fetch_file_context *c, *next;
|
|
|
|
|
|
|
|
if (ring == NULL) return;
|
|
|
|
|
|
|
|
/* Iterate over ring, processing each pending fetch */
|
|
|
|
c = ring;
|
|
|
|
do {
|
|
|
|
/* Ignore fetches that have been flagged as locked.
|
|
|
|
* This allows safe re-entrant calls to this function.
|
|
|
|
* Re-entrancy can occur if, as a result of a callback,
|
|
|
|
* the interested party causes fetch_poll() to be called
|
|
|
|
* again.
|
|
|
|
*/
|
|
|
|
if (c->locked == true) {
|
2011-02-24 01:41:17 +03:00
|
|
|
next = c->r_next;
|
2010-09-10 01:45:59 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only process non-aborted fetches */
|
2010-09-16 00:32:14 +04:00
|
|
|
if (c->aborted == false) {
|
2010-09-10 01:45:59 +04:00
|
|
|
/* file fetches can be processed in one go */
|
|
|
|
fetch_file_process(c);
|
|
|
|
}
|
|
|
|
|
2011-02-24 01:41:17 +03:00
|
|
|
/* Compute next fetch item at the last possible moment as
|
|
|
|
* processing this item may have added to the ring.
|
|
|
|
*/
|
|
|
|
next = c->r_next;
|
2010-09-10 01:45:59 +04:00
|
|
|
|
|
|
|
fetch_remove_from_queues(c->fetchh);
|
|
|
|
fetch_free(c->fetchh);
|
|
|
|
|
|
|
|
/* Advance to next ring entry, exiting if we've reached
|
|
|
|
* the start of the ring or the ring has become empty
|
|
|
|
*/
|
|
|
|
} while ( (c = next) != ring && ring != NULL);
|
|
|
|
}
|
|
|
|
|
2014-06-19 21:27:24 +04:00
|
|
|
nserror fetch_file_register(void)
|
2010-09-10 01:45:59 +04:00
|
|
|
{
|
2014-01-24 23:58:51 +04:00
|
|
|
lwc_string *scheme = lwc_string_ref(corestring_lwc_file);
|
2014-06-19 21:27:24 +04:00
|
|
|
const struct fetcher_operation_table fetcher_ops = {
|
|
|
|
.initialise = fetch_file_initialise,
|
|
|
|
.acceptable = fetch_file_can_fetch,
|
|
|
|
.setup = fetch_file_setup,
|
|
|
|
.start = fetch_file_start,
|
|
|
|
.abort = fetch_file_abort,
|
|
|
|
.free = fetch_file_free,
|
|
|
|
.poll = fetch_file_poll,
|
|
|
|
.finalise = fetch_file_finalise
|
|
|
|
};
|
|
|
|
|
|
|
|
return fetcher_add(scheme, &fetcher_ops);
|
2010-09-10 01:45:59 +04:00
|
|
|
}
|