2006-01-16 17:42:17 -05:00
|
|
|
/* Shared protocol functions */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h> /* FreeBSD needs this before resource.h */
|
|
|
|
#endif
|
|
|
|
#include <sys/types.h> /* FreeBSD needs this before resource.h */
|
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
2006-01-29 19:22:31 -05:00
|
|
|
#include "config/options.h"
|
2006-07-02 10:38:52 -04:00
|
|
|
#include "osdep/osdep.h"
|
2006-01-16 17:42:17 -05:00
|
|
|
#include "protocol/common.h"
|
2006-01-29 19:22:31 -05:00
|
|
|
#include "protocol/protocol.h"
|
|
|
|
#include "protocol/uri.h"
|
|
|
|
#include "util/conv.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "util/string.h"
|
2006-01-16 17:42:17 -05:00
|
|
|
|
|
|
|
|
|
|
|
/* Close all non-terminal file descriptors. */
|
|
|
|
void
|
|
|
|
close_all_non_term_fd(void)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
int max = 1024;
|
|
|
|
#ifdef RLIMIT_NOFILE
|
|
|
|
struct rlimit lim;
|
|
|
|
|
|
|
|
if (!getrlimit(RLIMIT_NOFILE, &lim))
|
|
|
|
max = lim.rlim_max;
|
|
|
|
#endif
|
|
|
|
for (n = 3; n < max; n++)
|
|
|
|
close(n);
|
|
|
|
}
|
2006-01-29 19:22:31 -05:00
|
|
|
|
2008-08-03 08:24:26 -04:00
|
|
|
struct connection_state
|
2006-01-29 19:22:31 -05:00
|
|
|
init_directory_listing(struct string *page, struct uri *uri)
|
|
|
|
{
|
|
|
|
struct string dirpath = NULL_STRING;
|
2007-03-18 05:04:02 -04:00
|
|
|
struct string decoded = NULL_STRING;
|
2006-01-29 19:22:31 -05:00
|
|
|
struct string location = NULL_STRING;
|
|
|
|
unsigned char *info;
|
|
|
|
int local = (uri->protocol == PROTOCOL_FILE);
|
|
|
|
|
|
|
|
if (!init_string(page)
|
|
|
|
|| !init_string(&dirpath)
|
2007-03-18 05:04:02 -04:00
|
|
|
|| !init_string(&decoded)
|
2006-01-29 19:22:31 -05:00
|
|
|
|| !init_string(&location)
|
|
|
|
|| !add_uri_to_string(&dirpath, uri, URI_DATA)
|
|
|
|
|| !add_uri_to_string(&location, uri, URI_DIR_LOCATION))
|
|
|
|
goto out_of_memory;
|
|
|
|
|
|
|
|
if (dirpath.length > 0
|
|
|
|
&& !dir_sep(dirpath.source[dirpath.length - 1])
|
2006-07-02 10:38:52 -04:00
|
|
|
&& !add_char_to_string(&dirpath, local ? CHAR_DIR_SEP : '/'))
|
2006-01-29 19:22:31 -05:00
|
|
|
goto out_of_memory;
|
|
|
|
|
2007-03-18 14:25:18 -04:00
|
|
|
/* Decode uri for displaying. */
|
|
|
|
if (!add_string_to_string(&decoded, &dirpath))
|
2007-03-18 05:04:02 -04:00
|
|
|
goto out_of_memory;
|
|
|
|
decode_uri_string(&decoded);
|
2006-01-29 20:41:09 -05:00
|
|
|
|
2006-01-29 19:22:31 -05:00
|
|
|
if (!local && !add_char_to_string(&location, '/'))
|
|
|
|
goto out_of_memory;
|
|
|
|
|
|
|
|
if (!add_to_string(page, "<html>\n<head><title>"))
|
|
|
|
goto out_of_memory;
|
|
|
|
|
2007-03-18 05:04:02 -04:00
|
|
|
if (!local && !add_html_to_string(page, location.source, location.length))
|
2006-01-29 19:22:31 -05:00
|
|
|
goto out_of_memory;
|
|
|
|
|
2007-03-18 05:04:02 -04:00
|
|
|
if (!add_html_to_string(page, decoded.source, decoded.length)
|
2006-01-29 19:22:31 -05:00
|
|
|
|| !add_to_string(page, "</title>\n<base href=\"")
|
2007-03-18 05:04:02 -04:00
|
|
|
|| !add_html_to_string(page, location.source, location.length)
|
|
|
|
|| !add_html_to_string(page, dirpath.source, dirpath.length))
|
2006-01-29 19:22:31 -05:00
|
|
|
goto out_of_memory;
|
|
|
|
|
|
|
|
if (!add_to_string(page, "\" />\n</head>\n<body>\n<h2>"))
|
|
|
|
goto out_of_memory;
|
|
|
|
|
2006-01-29 20:41:09 -05:00
|
|
|
/* Use module names? */
|
2006-01-29 19:22:31 -05:00
|
|
|
switch (uri->protocol) {
|
|
|
|
case PROTOCOL_FILE:
|
|
|
|
info = "Local";
|
|
|
|
break;
|
2006-01-29 20:41:09 -05:00
|
|
|
case PROTOCOL_FSP:
|
|
|
|
info = "FSP";
|
|
|
|
break;
|
2006-01-29 19:22:31 -05:00
|
|
|
case PROTOCOL_FTP:
|
|
|
|
info = "FTP";
|
|
|
|
break;
|
2006-01-29 20:41:09 -05:00
|
|
|
case PROTOCOL_GOPHER:
|
|
|
|
info = "Gopher";
|
2006-01-29 19:22:31 -05:00
|
|
|
break;
|
2007-01-24 12:40:28 -05:00
|
|
|
case PROTOCOL_SMB:
|
|
|
|
info = "Samba";
|
|
|
|
break;
|
2006-01-29 19:22:31 -05:00
|
|
|
default:
|
|
|
|
info = "?";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!add_to_string(page, info)
|
|
|
|
|| !add_to_string(page, " directory "))
|
|
|
|
goto out_of_memory;
|
|
|
|
|
|
|
|
if (!local && !add_string_to_string(page, &location))
|
|
|
|
goto out_of_memory;
|
|
|
|
|
|
|
|
/* Make the directory path with links to each subdir. */
|
|
|
|
{
|
2007-03-18 05:04:02 -04:00
|
|
|
const unsigned char *slash = dirpath.source;
|
|
|
|
const unsigned char *pslash = slash;
|
|
|
|
const unsigned char sep = local ? CHAR_DIR_SEP : '/';
|
|
|
|
|
2016-04-20 13:43:37 -04:00
|
|
|
while ((slash = strchr((const char *)slash, sep)) != NULL) {
|
2007-03-18 05:04:02 -04:00
|
|
|
done_string(&decoded);
|
|
|
|
if (!init_string(&decoded)
|
|
|
|
|| !add_bytes_to_string(&decoded, pslash, slash - pslash))
|
|
|
|
goto out_of_memory;
|
|
|
|
decode_uri_string(&decoded);
|
2006-01-29 19:22:31 -05:00
|
|
|
|
|
|
|
if (!add_to_string(page, "<a href=\"")
|
2007-03-18 05:04:02 -04:00
|
|
|
|| !add_html_to_string(page, location.source, location.length)
|
|
|
|
|| !add_html_to_string(page, dirpath.source, slash + 1 - dirpath.source)
|
2006-07-02 10:38:52 -04:00
|
|
|
|| !add_to_string(page, "\">")
|
2007-03-18 05:04:02 -04:00
|
|
|
|| !add_html_to_string(page, decoded.source, decoded.length)
|
2006-07-02 10:48:39 -04:00
|
|
|
|| !add_to_string(page, "</a>")
|
2007-03-18 05:04:02 -04:00
|
|
|
|| !add_html_to_string(page, &sep, 1))
|
2006-01-29 19:22:31 -05:00
|
|
|
goto out_of_memory;
|
|
|
|
|
|
|
|
pslash = ++slash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!add_to_string(page, "</h2>\n<pre>")) {
|
|
|
|
out_of_memory:
|
|
|
|
done_string(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
done_string(&dirpath);
|
2007-03-18 05:04:02 -04:00
|
|
|
done_string(&decoded);
|
2006-01-29 19:22:31 -05:00
|
|
|
done_string(&location);
|
|
|
|
|
2008-08-03 08:24:26 -04:00
|
|
|
return page->length > 0
|
|
|
|
? connection_state(S_OK)
|
|
|
|
: connection_state(S_OUT_OF_MEM);
|
2006-01-29 19:22:31 -05:00
|
|
|
}
|