45#include <libwapcaplet/libwapcaplet.h>
66#define FETCH_FILE_MAX_BUF_SIZE (1024 * 1024)
105 len = vsnprintf(header,
sizeof header, fmt, ap);
108 if (len >= (
int)
sizeof(header) || len < 0) {
141 const char *post_urlenc,
143 const char **headers)
149 ctx = calloc(1,
sizeof(*ctx));
162 for (i = 0; headers[i] != NULL; i++) {
163 if (strncasecmp(headers[i],
"If-None-Match:",
164 SLEN(
"If-None-Match:")) != 0) {
169 const char *d = headers[i] +
SLEN(
"If-None-Match:");
172 while (*d !=
'\0' && (*d <
'0' ||
'9' < *d))
180 "Bad If-None-Match value");
247 goto fetch_file_process_error_aborted;
249 snprintf(key,
sizeof key,
"HTTP%03d", code);
255 "<link rel=\"stylesheet\" type=\"text/css\" "
256 "href=\"resource:internal.css\">\n"
258 "<body class=\"ns-even-bg ns-even-fg ns-border\" "
259 "id =\"fetcherror\">\n"
260 "<h1 class=\"ns-border ns-odd-fg-bad\">%s</h1>\n"
261 "<p>%s %d %s %s</p>\n"
262 "</body>\n</html>\n",
271 goto fetch_file_process_error_aborted;
276fetch_file_process_error_aborted:
300 fd = open(ctx->
path, O_RDONLY);
309 buf_size = fdstat->st_size;
313 buf = mmap(NULL, buf_size, PROT_READ, MAP_SHARED, fd, 0);
314 if (buf == MAP_FAILED) {
316 msg.
data.
error =
"Unable to map memory for file data buffer";
334 goto fetch_file_process_aborted;
340 goto fetch_file_process_aborted;
345 (int64_t) fdstat->st_mtime)) {
346 goto fetch_file_process_aborted;
359fetch_file_process_aborted:
362 munmap(buf, buf_size);
369 ssize_t tot_read = 0;
382 infile = fopen(ctx->
path,
"rb");
383 if (infile == NULL) {
391 buf_size = fdstat->st_size;
396 buf = malloc(buf_size);
400 "Unable to allocate memory for file data buffer";
417 goto fetch_file_process_aborted;
423 goto fetch_file_process_aborted;
428 (int64_t) fdstat->st_mtime)) {
429 goto fetch_file_process_aborted;
433 while (tot_read < fdstat->st_size) {
434 res = fread(buf, 1, buf_size, infile);
438 msg.
data.
error =
"Unexpected EOF reading file";
440 goto fetch_file_process_aborted;
445 goto fetch_file_process_aborted;
462fetch_file_process_aborted:
472 char *nice_path, *cnv, *tmp;
477 nice_path = malloc(strlen(
path) *
SLEN(
"&") + 1);
478 if (nice_path == NULL) {
483 for (cnv = nice_path, tmp =
path; *tmp !=
'\0'; tmp++) {
489 }
else if (*tmp ==
'>') {
494 }
else if (*tmp ==
'&') {
507 title_length = (cnv - nice_path) + strlen(
messages_get(
"FileIndex"));
508 title = malloc(title_length + 1);
516 snprintf(title, title_length,
messages_get(
"FileIndex"), nice_path);
541 char *urlpath = NULL;
542 struct stat ent_stat;
548 if (ent->d_name[0] ==
'.') {
557 if (stat(urlpath, &ent_stat) != 0) {
558 ent_stat.st_mode = 0;
565 if (strftime((
char *)&datebuf,
sizeof datebuf,
"%a %d %b %Y",
566 localtime(&ent_stat.st_mtime)) == 0) {
572 if (strftime((
char *)&timebuf,
sizeof timebuf,
"%H:%M",
573 localtime(&ent_stat.st_mtime)) == 0) {
585 if (S_ISREG(ent_stat.st_mode)) {
595 }
else if (S_ISDIR(ent_stat.st_mode)) {
634 const char *s1 = (*d1)->d_name;
635 const char *s2 = (*d2)->d_name;
637 while (*s1 !=
'\0' && *s2 !=
'\0') {
638 if ((*s1 >=
'0' && *s1 <=
'9') &&
639 (*s2 >=
'0' && *s2 <=
'9')) {
641 while (*s1 >=
'0' && *s1 <=
'9') {
642 n1 = n1 * 10 + (*s1) -
'0';
645 while (*s2 >=
'0' && *s2 <=
'9') {
646 n2 = n2 * 10 + (*s2) -
'0';
652 if (*s1 ==
'\0' || *s2 ==
'\0')
655 if (tolower(*s1) != tolower(*s2))
662 return tolower(*s1) - tolower(*s2);
675 struct dirent **listing = NULL;
691 goto fetch_file_process_dir_aborted;
695 goto fetch_file_process_dir_aborted;
704 goto fetch_file_process_dir_aborted;
712 goto fetch_file_process_dir_aborted;
728 goto fetch_file_process_dir_aborted;
736 goto fetch_file_process_dir_aborted;
738 for (i = 0; i < n; i++) {
746 goto fetch_file_process_dir_aborted;
756 goto fetch_file_process_dir_aborted;
761fetch_file_process_dir_aborted:
763 if (listing != NULL) {
764 for (i = 0; i < n; i++) {
777 if (stat(ctx->
path, &fdstat) != 0) {
784 if (S_ISDIR(fdstat.st_mode)) {
788 }
else if (S_ISREG(fdstat.st_mode)) {
805 while (
ring != NULL) {
841 lwc_string *scheme = lwc_string_ref(corestring_lwc_file);
static osspriteop_area * buffer
The buffer characteristics.
void fetch_set_http_code(struct fetch *fetch, long http_code)
set the http code of a fetch
nserror fetcher_add(lwc_string *scheme, const struct fetcher_operation_table *ops)
Register a fetcher for a scheme.
void fetch_send_callback(const fetch_msg *msg, struct fetch *fetch)
send message to fetch
void fetch_free(struct fetch *f)
Free a fetch structure and associated resources.
void fetch_remove_from_queues(struct fetch *fetch)
remove a queued fetch
Fetching of data from a URL (interface).
static void fetch_file_abort(void *ctx)
callback to abort a file fetch
static void fetch_file_process_error(struct fetch_file_context *ctx, int code)
static int fetch_file_errno_to_http_code(int error_no)
static void * fetch_file_setup(struct fetch *fetchh, nsurl *url, bool only_2xx, bool downgrade_tls, const char *post_urlenc, const struct fetch_multipart_data *post_multipart, const char **headers)
callback to set up a file fetch context.
static bool fetch_file_start(void *ctx)
callback to start a file fetch
static void fetch_file_process(struct fetch_file_context *ctx)
static bool fetch_file_initialise(lwc_string *scheme)
callback to initialise the file fetcher.
static void fetch_file_poll(lwc_string *scheme)
callback to poll for additional file fetch contents
static struct fetch_file_context * ring
static void fetch_file_process_dir(struct fetch_file_context *ctx, struct stat *fdstat)
static char * gen_nice_title(char *path)
nserror fetch_file_register(void)
Register file scheme handler.
static bool fetch_file_send_header(struct fetch_file_context *ctx, const char *fmt,...)
static int dir_sort_alpha(const struct dirent **d1, const struct dirent **d2)
Comparison function for sorting directories.
static void fetch_file_free(void *ctx)
callback to free a file fetch
static bool fetch_file_can_fetch(const nsurl *url)
static void fetch_file_finalise(lwc_string *scheme)
callback to initialise the file fetcher.
static void fetch_file_process_plain(struct fetch_file_context *ctx, struct stat *fdstat)
Process object as a regular file.
static nserror process_dir_ent(struct fetch_file_context *ctx, struct dirent *ent, bool even, char *buffer, size_t buffer_len)
Generate an output row of the directory listing.
#define FETCH_FILE_MAX_BUF_SIZE
static bool fetch_file_send_callback(const fetch_msg *msg, struct fetch_file_context *ctx)
issue fetch callbacks with locking
Useful interned string pointers (interface).
directory traversal and entry
bool dirlist_generate_row(bool even, bool directory, nsurl *url, char *name, const char *mimetype, long long size, char *date, char *time, char *buffer, int buffer_length)
Generates the part of an HTML directory listing page that displays a row in the directory contents ta...
bool dirlist_generate_bottom(char *buffer, int buffer_length)
Generates the bottom part of an HTML directory listing page.
bool dirlist_generate_title(const char *title, char *buffer, int buffer_length)
Generates the part of an HTML directory listing page that contains the title.
bool dirlist_generate_parent_link(const char *parent, char *buffer, int buffer_length)
Generates the part of an HTML directory listing page that links to the parent directory.
bool dirlist_generate_top(char *buffer, int buffer_length)
Generates the top part of an HTML directory listing page.
bool dirlist_generate_headings(char *buffer, int buffer_length)
Generates the part of an HTML directory listing page that displays the column headings.
interface to generate HTML content for displaying directory listings.
nserror
Enumeration of error codes.
@ NSERROR_BAD_PARAMETER
Bad Parameter.
Interface for fetchers factory.
struct netsurf_table * guit
The global interface table.
Interface to core interface table.
Interface to platform-specific fetcher operations.
Netsurf additional integer type formatting macros.
#define PRIsizet
c99 standard printf formatting for size_t type
#define NSLOG(catname, level, logmsg, args...)
const char * messages_get(const char *key)
Fast lookup of a message by key from the standard Messages hash.
Localised message support (interface).
NetSurf URL handling (interface).
bool nsurl_compare(const nsurl *url1, const nsurl *url2, nsurl_component parts)
Compare two URLs.
void nsurl_unref(nsurl *url)
Drop a reference to a NetSurf URL object.
const char * nsurl_access(const nsurl *url)
Access a NetSurf URL object as a string.
nsurl * nsurl_ref(nsurl *url)
Increment the reference count to a NetSurf URL object.
struct nsurl nsurl
NetSurf URL object.
nserror nsurl_parent(const nsurl *url, nsurl **new_url)
Create a NetSurf URL object for URL with parent location of an existing URL.
int scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const struct dirent **, const struct dirent **))
#define RING_REMOVE(ring, element)
Remove the given element from the specified ring.
#define RING_INSERT(ring, element)
Insert the given item into the specified ring.
Interface to utility string handling.
nsurl * url
The full url the fetch refers to.
time_t file_etag
Request etag for file (previous st.m_time)
char * path
The actual path to be used with open()
bool locked
Flag indicating entry is already entered.
struct fetch * fetchh
Handle for this fetch.
struct fetch_file_context * r_next
bool aborted
Flag indicating fetch has been aborted.
struct fetch_file_context * r_prev
struct fetch_msg::@118::@119 header_or_data
union fetch_msg::@118 data
Fetch POST multipart data.
Information for a single fetch.
bool(* initialise)(lwc_string *scheme)
The initialiser for the fetcher.
const char *(* filetype)(const char *unix_path)
Determine the MIME type of a local file.
nserror(* nsurl_to_path)(struct nsurl *url, char **path)
Create a path from a nsurl.
nserror(* path_to_nsurl)(const char *path, struct nsurl **url)
Create a nsurl from a path.
struct gui_file_table * file
File table.
struct gui_fetch_table * fetch
Fetcher table.
nserror nsc_snptimet(const char *str, size_t size, time_t *timep)
Parse time in seconds since epoc.
Interface to time operations.
nserror netsurf_mkpath(char **str, size_t *size, size_t nelm,...)
Generate a path from one or more component elemnts.
Default operations table for files.
Interface to a number of general purpose functionality.
#define SLEN(x)
Calculate length of constant C string.
static nserror path(const struct redraw_context *ctx, const plot_style_t *pstyle, const float *p, unsigned int n, const float transform[6])
Plots a path.