2005-09-15 09:58:31 -04:00
|
|
|
/* Internal "file" protocol implementation */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
2021-12-22 10:12:09 -05:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h> /* OS/2 needs this after sys/types.h */
|
|
|
|
#endif
|
2005-09-15 09:58:31 -04:00
|
|
|
#include <sys/stat.h> /* OS/2 needs this after sys/types.h */
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h> /* OS/2 needs this after sys/types.h */
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "cache/cache.h"
|
|
|
|
#include "config/options.h"
|
|
|
|
#include "encoding/encoding.h"
|
2021-08-08 15:25:08 -04:00
|
|
|
#include "intl/libintl.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "main/module.h"
|
|
|
|
#include "network/connection.h"
|
2010-07-24 12:09:16 -04:00
|
|
|
#include "network/socket.h"
|
2006-07-02 03:01:14 -04:00
|
|
|
#include "osdep/osdep.h"
|
2006-01-29 19:25:18 -05:00
|
|
|
#include "protocol/common.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "protocol/file/cgi.h"
|
|
|
|
#include "protocol/file/file.h"
|
2010-07-24 12:09:16 -04:00
|
|
|
#include "protocol/http/http.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "protocol/uri.h"
|
|
|
|
#include "util/conv.h"
|
|
|
|
#include "util/file.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
|
|
|
|
|
bug 764: Initialize the right member of union option_value
INIT_OPTION used to initialize union option_value at compile time by
casting the default value to LIST_OF(struct option) *, which is the
type of the first member. On sparc64 and other big-endian systems
where sizeof(int) < sizeof(struct list_head *), this tended to leave
option->value.number as zero, thus messing up OPT_INT and OPT_BOOL
at least. OPT_LONG however tended to work right.
This would be easy to fix with C99 designated initializers,
but doc/hacking.txt says ELinks must be kept C89 compatible.
Another solution would be to make register_options() read the
value from option->value.tree (the first member), cast it back
to the right type, and write it to the appropriate member;
but that would still require somewhat dubious conversions
between integers, data pointers, and function pointers.
So here's a rather more invasive solution. Add struct option_init,
which is somewhat similar to struct option but has non-overlapping
members for different types of values, to ensure nothing is lost
in compile-time conversions. Move unsigned char *path from struct
option_info to struct option_init, and replace struct option_info
with a union that contains struct option_init and struct option.
Now, this union can be initialized with no portability problems,
and register_options() then moves the values from struct option_init
to their final places in struct option.
In my x86 ELinks build with plenty of options configured in, this
change bloated the text section by 340 bytes but compressed the data
section by 2784 bytes, presumably because union option_info is a
pointer smaller than struct option_info was.
(cherry picked from elinks-0.12 commit e5f6592ee20780a61f70feeb1f9e17631b9c5835)
Conflicts:
src/protocol/fsp/fsp.c: All options had been removed in 0.13.GIT.
src/protocol/smb/smb2.c: Ditto.
2009-08-15 15:39:07 -04:00
|
|
|
static union option_info file_options[] = {
|
2022-01-19 16:49:13 -05:00
|
|
|
INIT_OPT_TREE(C_("protocol"), N_("Local files"),
|
|
|
|
C_("file"), OPT_ZERO,
|
2005-09-15 09:58:31 -04:00
|
|
|
N_("Options specific to local browsing.")),
|
|
|
|
|
2022-01-19 16:49:13 -05:00
|
|
|
INIT_OPT_BOOL(C_("protocol.file"), N_("Allow reading special files"),
|
|
|
|
C_("allow_special_files"), OPT_ZERO, 0,
|
Rewrap lines in option documentation.
Documentation strings of most options used to contain a "\n" at the
end of each source line. When the option manager displayed these
strings, it treated each "\n" as a hard newline. On 80x24 terminals
however, the option description window has only 60 columes available
for the text (with the default setup.h), and the hard newlines were
further apart, so the option manager wrapped the text a second time,
resulting in rather ugly output where long lones are interleaved with
short ones. This could also cause the text to take up too much
vertical space and not fit in the window.
Replace most of those hard newlines with spaces so that the option
manager (or perhaps BFU) will take care of the wrapping. At the same
time, rewrap the strings in source code so that the source lines are
at most 79 columns wide.
In some options though, there is a list of possible values and their
meanings. In those lists, if the description of one value does not
fit in one line, then continuation lines should be indented. The
option manager and BFU are not currently able to do that. So, keep
the hard newlines in those lists, but rewrap them to 60 columns so
that they are less likely to require further wrapping at runtime.
2009-03-07 13:48:38 -05:00
|
|
|
N_("Whether to allow reading from non-regular files. "
|
|
|
|
"Note this can be dangerous; reading /dev/urandom or "
|
2005-09-15 09:58:31 -04:00
|
|
|
"/dev/zero can ruin your day!")),
|
|
|
|
|
2022-01-19 16:49:13 -05:00
|
|
|
INIT_OPT_BOOL(C_("protocol.file"), N_("Show hidden files in directory listing"),
|
|
|
|
C_("show_hidden_files"), OPT_ZERO, 1,
|
Rewrap lines in option documentation.
Documentation strings of most options used to contain a "\n" at the
end of each source line. When the option manager displayed these
strings, it treated each "\n" as a hard newline. On 80x24 terminals
however, the option description window has only 60 columes available
for the text (with the default setup.h), and the hard newlines were
further apart, so the option manager wrapped the text a second time,
resulting in rather ugly output where long lones are interleaved with
short ones. This could also cause the text to take up too much
vertical space and not fit in the window.
Replace most of those hard newlines with spaces so that the option
manager (or perhaps BFU) will take care of the wrapping. At the same
time, rewrap the strings in source code so that the source lines are
at most 79 columns wide.
In some options though, there is a list of possible values and their
meanings. In those lists, if the description of one value does not
fit in one line, then continuation lines should be indented. The
option manager and BFU are not currently able to do that. So, keep
the hard newlines in those lists, but rewrap them to 60 columns so
that they are less likely to require further wrapping at runtime.
2009-03-07 13:48:38 -05:00
|
|
|
N_("When set to false, files with name starting with a dot "
|
|
|
|
"will be hidden in local directory listings.")),
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2022-01-19 16:49:13 -05:00
|
|
|
INIT_OPT_BOOL(C_("protocol.file"), N_("Try encoding extensions"),
|
|
|
|
C_("try_encoding_extensions"), OPT_ZERO, 1,
|
Rewrap lines in option documentation.
Documentation strings of most options used to contain a "\n" at the
end of each source line. When the option manager displayed these
strings, it treated each "\n" as a hard newline. On 80x24 terminals
however, the option description window has only 60 columes available
for the text (with the default setup.h), and the hard newlines were
further apart, so the option manager wrapped the text a second time,
resulting in rather ugly output where long lones are interleaved with
short ones. This could also cause the text to take up too much
vertical space and not fit in the window.
Replace most of those hard newlines with spaces so that the option
manager (or perhaps BFU) will take care of the wrapping. At the same
time, rewrap the strings in source code so that the source lines are
at most 79 columns wide.
In some options though, there is a list of possible values and their
meanings. In those lists, if the description of one value does not
fit in one line, then continuation lines should be indented. The
option manager and BFU are not currently able to do that. So, keep
the hard newlines in those lists, but rewrap them to 60 columns so
that they are less likely to require further wrapping at runtime.
2009-03-07 13:48:38 -05:00
|
|
|
N_("When set, if we can't open a file named 'filename', "
|
|
|
|
"we'll try to open 'filename' with some encoding extension "
|
|
|
|
"appended (ie. 'filename.gz'); it depends on the supported "
|
|
|
|
"encodings.")),
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
NULL_OPTION_INFO,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct module file_protocol_module = struct_module(
|
|
|
|
/* name: */ N_("File"),
|
|
|
|
/* options: */ file_options,
|
|
|
|
/* hooks: */ NULL,
|
|
|
|
/* submodules: */ NULL,
|
|
|
|
/* data: */ NULL,
|
|
|
|
/* init: */ NULL,
|
|
|
|
/* done: */ NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
/* Directory listing */
|
|
|
|
|
|
|
|
/* Based on the @entry attributes and file-/dir-/linkname is added to the @data
|
2007-03-20 04:07:51 -04:00
|
|
|
* fragment. All the strings are in the system charset. */
|
2005-09-15 09:58:31 -04:00
|
|
|
static inline void
|
2019-04-21 06:27:40 -04:00
|
|
|
add_dir_entry(struct directory_entry *entry, struct string *page,
|
2021-01-02 10:20:27 -05:00
|
|
|
int pathlen, char *dircolor)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *lnk = NULL;
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string html_encoded_name;
|
|
|
|
struct string uri_encoded_name;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (!init_string(&html_encoded_name)) return;
|
|
|
|
if (!init_string(&uri_encoded_name)) {
|
|
|
|
done_string(&html_encoded_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
encode_uri_string(&uri_encoded_name, entry->name + pathlen, -1, 1);
|
|
|
|
add_html_to_string(&html_encoded_name, entry->name + pathlen,
|
|
|
|
strlen(entry->name) - pathlen);
|
|
|
|
|
|
|
|
/* add_to_string(&fragment, &fragmentlen, " "); */
|
|
|
|
add_html_to_string(page, entry->attrib, strlen(entry->attrib));
|
|
|
|
add_to_string(page, "<a href=\"");
|
|
|
|
add_string_to_string(page, &uri_encoded_name);
|
|
|
|
|
|
|
|
if (entry->attrib[0] == 'd') {
|
2007-07-17 17:24:09 -04:00
|
|
|
add_char_to_string(page, '/');
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
#ifdef FS_UNIX_SOFTLINKS
|
|
|
|
} else if (entry->attrib[0] == 'l') {
|
|
|
|
struct stat st;
|
2021-01-02 10:20:27 -05:00
|
|
|
char buf[MAX_STR_LEN];
|
2005-09-15 09:58:31 -04:00
|
|
|
int readlen = readlink(entry->name, buf, MAX_STR_LEN);
|
|
|
|
|
|
|
|
if (readlen > 0 && readlen != MAX_STR_LEN) {
|
|
|
|
buf[readlen] = '\0';
|
2021-01-02 10:20:27 -05:00
|
|
|
lnk = straconcat(" -> ", buf, (char *) NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!stat(entry->name, &st) && S_ISDIR(st.st_mode))
|
|
|
|
add_char_to_string(page, '/');
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
add_to_string(page, "\">");
|
|
|
|
|
|
|
|
if (entry->attrib[0] == 'd' && *dircolor) {
|
|
|
|
/* The <b> is for the case when use_document_colors is off. */
|
2007-03-11 07:01:50 -04:00
|
|
|
string_concat(page, "<font color=\"", dircolor, "\"><b>",
|
2021-01-02 10:20:27 -05:00
|
|
|
(char *) NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
add_string_to_string(page, &html_encoded_name);
|
|
|
|
done_string(&uri_encoded_name);
|
|
|
|
done_string(&html_encoded_name);
|
|
|
|
|
|
|
|
if (entry->attrib[0] == 'd' && *dircolor) {
|
|
|
|
add_to_string(page, "</b></font>");
|
|
|
|
}
|
|
|
|
|
|
|
|
add_to_string(page, "</a>");
|
|
|
|
if (lnk) {
|
|
|
|
add_html_to_string(page, lnk, strlen(lnk));
|
|
|
|
mem_free(lnk);
|
|
|
|
}
|
|
|
|
|
|
|
|
add_char_to_string(page, '\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First information such as permissions is gathered for each directory entry.
|
|
|
|
* Finally the sorted entries are added to the @data->fragment one by one. */
|
|
|
|
static inline void
|
2021-01-02 10:20:27 -05:00
|
|
|
add_dir_entries(struct directory_entry *entries, char *dirpath,
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *page)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char dircolor[8];
|
2005-09-15 09:58:31 -04:00
|
|
|
int dirpathlen = strlen(dirpath);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Setup @dircolor so it's easy to check if we should color dirs. */
|
2007-08-28 12:41:18 -04:00
|
|
|
if (get_opt_bool("document.browse.links.color_dirs", NULL)) {
|
|
|
|
color_to_string(get_opt_color("document.colors.dirs", NULL),
|
2021-01-02 10:20:27 -05:00
|
|
|
(char *) &dircolor);
|
2005-09-15 09:58:31 -04:00
|
|
|
} else {
|
|
|
|
dircolor[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; entries[i].name; i++) {
|
|
|
|
add_dir_entry(&entries[i], page, dirpathlen, dircolor);
|
|
|
|
mem_free(entries[i].attrib);
|
|
|
|
mem_free(entries[i].name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We may have allocated space for entries but added none. */
|
|
|
|
mem_free_if(entries);
|
|
|
|
}
|
|
|
|
|
2008-01-26 23:09:18 -05:00
|
|
|
/* Generates an HTML page listing the content of @directory with the path
|
2005-09-15 09:58:31 -04:00
|
|
|
* @dirpath. */
|
|
|
|
/* Returns a connection state. S_OK if all is well. */
|
2008-08-03 08:24:26 -04:00
|
|
|
static inline struct connection_state
|
2021-01-02 10:20:27 -05:00
|
|
|
list_directory(struct connection *conn, char *dirpath,
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *page)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2007-08-28 12:41:18 -04:00
|
|
|
int show_hidden_files = get_opt_bool("protocol.file.show_hidden_files",
|
|
|
|
NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
struct directory_entry *entries;
|
2008-08-03 08:24:26 -04:00
|
|
|
struct connection_state state;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
entries = get_directory_entries(dirpath, show_hidden_files);
|
|
|
|
if (!entries) {
|
2008-08-03 08:24:26 -04:00
|
|
|
if (errno) return connection_state_for_errno(errno);
|
|
|
|
return connection_state(S_OUT_OF_MEM);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
2006-01-29 19:25:18 -05:00
|
|
|
state = init_directory_listing(page, conn->uri);
|
2008-08-03 08:24:26 -04:00
|
|
|
if (!is_in_state(state, S_OK))
|
|
|
|
return connection_state(S_OUT_OF_MEM);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
add_dir_entries(entries, dirpath, page);
|
2006-01-29 19:25:18 -05:00
|
|
|
|
2006-07-31 07:24:39 -04:00
|
|
|
if (!add_to_string(page, "</pre>\n<hr/>\n</body>\n</html>\n")) {
|
2006-01-29 19:25:18 -05:00
|
|
|
done_string(page);
|
2008-08-03 08:24:26 -04:00
|
|
|
return connection_state(S_OUT_OF_MEM);
|
2006-01-29 19:25:18 -05:00
|
|
|
}
|
|
|
|
|
2008-08-03 08:24:26 -04:00
|
|
|
return connection_state(S_OK);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
2010-07-31 15:48:48 -04:00
|
|
|
static void
|
|
|
|
check_if_closed(struct socket *socket, struct read_buffer *rb)
|
|
|
|
{
|
|
|
|
if (socket->state == SOCKET_CLOSED) {
|
2022-01-26 11:35:46 -05:00
|
|
|
abort_connection((struct connection *)socket->conn, connection_state(S_OK));
|
2010-07-31 15:48:48 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
http_got_header(socket, rb);
|
|
|
|
}
|
|
|
|
|
2010-07-24 12:09:16 -04:00
|
|
|
static void
|
|
|
|
read_from_stdin(struct connection *conn)
|
|
|
|
{
|
|
|
|
struct read_buffer *rb = alloc_read_buffer(conn->socket);
|
|
|
|
|
|
|
|
if (!rb) return;
|
|
|
|
|
|
|
|
memcpy(rb->data, "HTTP/1.0 200 OK\r\n\r\n", 19);
|
|
|
|
rb->length = 19;
|
|
|
|
rb->freespace -= 19;
|
|
|
|
|
|
|
|
conn->unrestartable = 1;
|
|
|
|
|
|
|
|
conn->socket->state = SOCKET_END_ONCLOSE;
|
|
|
|
read_from_socket(conn->socket, rb, connection_state(S_SENT),
|
2010-07-31 15:48:48 -04:00
|
|
|
check_if_closed);
|
2010-07-24 12:09:16 -04:00
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* To reduce redundant error handling code [calls to abort_connection()]
|
|
|
|
* most of the function is build around conditions that will assign the error
|
|
|
|
* code to @state if anything goes wrong. The rest of the function will then just
|
|
|
|
* do the necessary cleanups. If all works out we end up with @state being S_OK
|
|
|
|
* resulting in a cache entry being created with the fragment data generated by
|
|
|
|
* either reading the file content or listing a directory. */
|
|
|
|
void
|
|
|
|
file_protocol_handler(struct connection *connection)
|
|
|
|
{
|
2022-02-21 11:47:48 -05:00
|
|
|
const char *redirect_location = NULL;
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string page, name;
|
2008-08-03 08:24:26 -04:00
|
|
|
struct connection_state state;
|
2007-03-20 04:07:51 -04:00
|
|
|
int set_dir_content_type = 0;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (get_cmd_opt_bool("anonymous")) {
|
|
|
|
if (strcmp(connection->uri->string, "file:///dev/stdin")
|
|
|
|
|| isatty(STDIN_FILENO)) {
|
2008-08-03 08:24:26 -04:00
|
|
|
abort_connection(connection,
|
|
|
|
connection_state(S_FILE_ANONYMOUS));
|
2005-09-15 09:58:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_CGI
|
|
|
|
if (!execute_cgi(connection)) return;
|
|
|
|
#endif /* CONFIG_CGI */
|
|
|
|
|
2010-07-24 12:09:16 -04:00
|
|
|
/* Treat /dev/stdin in special way */
|
|
|
|
if (!strcmp(connection->uri->string, "file:///dev/stdin")) {
|
2012-05-25 10:13:25 -04:00
|
|
|
int fd = open("/dev/stdin", O_RDONLY);
|
2010-07-24 12:09:16 -04:00
|
|
|
|
|
|
|
if (fd == -1) {
|
|
|
|
abort_connection(connection, connection_state(-errno));
|
|
|
|
return;
|
|
|
|
}
|
2012-05-25 10:13:25 -04:00
|
|
|
set_nonblocking_fd(fd);
|
2010-07-24 12:09:16 -04:00
|
|
|
if (!init_http_connection_info(connection, 1, 0, 1)) {
|
|
|
|
abort_connection(connection, connection_state(S_OUT_OF_MEM));
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
connection->socket->fd = fd;
|
|
|
|
connection->data_socket->fd = -1;
|
|
|
|
read_from_stdin(connection);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
/* This function works on already simplified file-scheme URI pre-chewed
|
|
|
|
* by transform_file_url(). By now, the function contains no hostname
|
|
|
|
* part anymore, possibly relative path is converted to an absolute one
|
|
|
|
* and uri->data is just the final path to file/dir we should try to
|
|
|
|
* show. */
|
|
|
|
|
|
|
|
if (!init_string(&name)
|
|
|
|
|| !add_uri_to_string(&name, connection->uri, URI_PATH)) {
|
|
|
|
done_string(&name);
|
2008-08-03 08:24:26 -04:00
|
|
|
abort_connection(connection, connection_state(S_OUT_OF_MEM));
|
2005-09-15 09:58:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
decode_uri_string(&name);
|
|
|
|
|
2007-07-17 17:25:07 -04:00
|
|
|
/* In Win32, file_is_dir seems to always return 0 if the name
|
|
|
|
* ends with a directory separator. */
|
|
|
|
if ((name.length > 0 && dir_sep(name.source[name.length - 1]))
|
|
|
|
|| file_is_dir(name.source)) {
|
2005-09-15 09:58:31 -04:00
|
|
|
/* In order for global history and directory listing to
|
|
|
|
* function properly the directory url must end with a
|
|
|
|
* directory separator. */
|
|
|
|
if (name.source[0] && !dir_sep(name.source[name.length - 1])) {
|
2006-07-02 03:01:14 -04:00
|
|
|
redirect_location = STRING_DIR_SEP;
|
2008-08-03 08:24:26 -04:00
|
|
|
state = connection_state(S_OK);
|
2005-09-15 09:58:31 -04:00
|
|
|
} else {
|
2006-01-29 19:25:18 -05:00
|
|
|
state = list_directory(connection, name.source, &page);
|
2007-03-20 04:07:51 -04:00
|
|
|
set_dir_content_type = 1;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
state = read_encoded_file(&name, &page);
|
|
|
|
/* FIXME: If state is now S_ENCODE_ERROR we should try loading
|
|
|
|
* the file undecoded. --jonas */
|
|
|
|
}
|
|
|
|
|
|
|
|
done_string(&name);
|
|
|
|
|
2008-08-03 08:24:26 -04:00
|
|
|
if (is_in_state(state, S_OK)) {
|
2005-09-15 09:58:31 -04:00
|
|
|
struct cache_entry *cached;
|
|
|
|
|
|
|
|
/* Try to add fragment data to the connection cache if either
|
|
|
|
* file reading or directory listing worked out ok. */
|
|
|
|
cached = connection->cached = get_cache_entry(connection->uri);
|
|
|
|
if (!connection->cached) {
|
|
|
|
if (!redirect_location) done_string(&page);
|
2008-08-03 08:24:26 -04:00
|
|
|
state = connection_state(S_OUT_OF_MEM);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
} else if (redirect_location) {
|
|
|
|
if (!redirect_cache(cached, redirect_location, 1, 0))
|
2008-08-03 08:24:26 -04:00
|
|
|
state = connection_state(S_OUT_OF_MEM);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
add_fragment(cached, 0, page.source, page.length);
|
2006-02-08 15:40:08 -05:00
|
|
|
connection->from += page.length;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2007-03-20 04:07:51 -04:00
|
|
|
if (!cached->head && set_dir_content_type) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *head;
|
2007-03-20 04:07:51 -04:00
|
|
|
|
|
|
|
/* If the system charset somehow
|
|
|
|
* changes after the directory listing
|
|
|
|
* has been generated, it should be
|
|
|
|
* parsed with the original charset. */
|
|
|
|
head = straconcat("\r\nContent-Type: text/html; charset=",
|
|
|
|
get_cp_mime_name(get_cp_index("System")),
|
2021-01-02 10:20:27 -05:00
|
|
|
"\r\n", (char *) NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* Not so gracefully handle failed memory
|
|
|
|
* allocation. */
|
2007-03-20 04:07:51 -04:00
|
|
|
if (!head)
|
2008-08-03 08:24:26 -04:00
|
|
|
state = connection_state(S_OUT_OF_MEM);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2007-03-20 04:07:51 -04:00
|
|
|
/* Setup directory listing for viewing. */
|
|
|
|
mem_free_set(&cached->head, head);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
done_string(&page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abort_connection(connection, state);
|
|
|
|
}
|