1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

init_directory_listing: Link to original URLs. Decode for display only.

HTML-escape all strings that are not intended to contain markup.
This commit is contained in:
Kalle Olavi Niemitalo 2007-03-18 11:04:02 +02:00 committed by Kalle Olavi Niemitalo
parent b48a2d660d
commit 3ee04479d4

View File

@ -47,12 +47,14 @@ enum connection_state
init_directory_listing(struct string *page, struct uri *uri) init_directory_listing(struct string *page, struct uri *uri)
{ {
struct string dirpath = NULL_STRING; struct string dirpath = NULL_STRING;
struct string decoded = NULL_STRING;
struct string location = NULL_STRING; struct string location = NULL_STRING;
unsigned char *info; unsigned char *info;
int local = (uri->protocol == PROTOCOL_FILE); int local = (uri->protocol == PROTOCOL_FILE);
if (!init_string(page) if (!init_string(page)
|| !init_string(&dirpath) || !init_string(&dirpath)
|| !init_string(&decoded)
|| !init_string(&location) || !init_string(&location)
|| !add_uri_to_string(&dirpath, uri, URI_DATA) || !add_uri_to_string(&dirpath, uri, URI_DATA)
|| !add_uri_to_string(&location, uri, URI_DIR_LOCATION)) || !add_uri_to_string(&location, uri, URI_DIR_LOCATION))
@ -63,8 +65,12 @@ init_directory_listing(struct string *page, struct uri *uri)
&& !add_char_to_string(&dirpath, local ? CHAR_DIR_SEP : '/')) && !add_char_to_string(&dirpath, local ? CHAR_DIR_SEP : '/'))
goto out_of_memory; goto out_of_memory;
/* Decode uri for displaying. */ /* Decode uri for displaying. Do not use
decode_uri_string(&dirpath); * add_string_to_string, because it for some reason returns
* NULL if the second string is empty. */
if (!add_bytes_to_string(&decoded, dirpath.source, dirpath.length))
goto out_of_memory;
decode_uri_string(&decoded);
if (!local && !add_char_to_string(&location, '/')) if (!local && !add_char_to_string(&location, '/'))
goto out_of_memory; goto out_of_memory;
@ -72,23 +78,15 @@ init_directory_listing(struct string *page, struct uri *uri)
if (!add_to_string(page, "<html>\n<head><title>")) if (!add_to_string(page, "<html>\n<head><title>"))
goto out_of_memory; goto out_of_memory;
if (!local && !add_string_to_string(page, &location)) if (!local && !add_html_to_string(page, location.source, location.length))
goto out_of_memory; goto out_of_memory;
if (!add_html_to_string(page, dirpath.source, dirpath.length) if (!add_html_to_string(page, decoded.source, decoded.length)
|| !add_to_string(page, "</title>\n<base href=\"") || !add_to_string(page, "</title>\n<base href=\"")
|| !add_string_to_string(page, &location)) || !add_html_to_string(page, location.source, location.length)
|| !add_html_to_string(page, dirpath.source, dirpath.length))
goto out_of_memory; goto out_of_memory;
#ifdef CONFIG_OS_WIN32
/* Stupid. ':' and '\\' are encoded and base href with them
* doesn't "work". */
if (local)
encode_win32_uri_string(page, dirpath.source, dirpath.length);
else
#endif
encode_uri_string(page, dirpath.source, dirpath.length, 0);
if (!add_to_string(page, "\" />\n</head>\n<body>\n<h2>")) if (!add_to_string(page, "\" />\n</head>\n<body>\n<h2>"))
goto out_of_memory; goto out_of_memory;
@ -122,20 +120,24 @@ init_directory_listing(struct string *page, struct uri *uri)
/* Make the directory path with links to each subdir. */ /* Make the directory path with links to each subdir. */
{ {
unsigned char *slash = dirpath.source; const unsigned char *slash = dirpath.source;
unsigned char *pslash = slash; const unsigned char *pslash = slash;
unsigned char sep = local ? CHAR_DIR_SEP : '/'; const unsigned char sep = local ? CHAR_DIR_SEP : '/';
while ((slash = strchr(slash, sep)) != NULL) {
done_string(&decoded);
if (!init_string(&decoded)
|| !add_bytes_to_string(&decoded, pslash, slash - pslash))
goto out_of_memory;
decode_uri_string(&decoded);
while ((slash = strchr(slash, sep))) {
/* FIXME: htmlesc? At least we should escape quotes. --pasky */
if (!add_to_string(page, "<a href=\"") if (!add_to_string(page, "<a href=\"")
|| !add_string_to_string(page, &location) || !add_html_to_string(page, location.source, location.length)
|| !add_bytes_to_string(page, dirpath.source, slash - dirpath.source) || !add_html_to_string(page, dirpath.source, slash + 1 - dirpath.source)
|| !add_char_to_string(page, sep)
|| !add_to_string(page, "\">") || !add_to_string(page, "\">")
|| !add_html_to_string(page, pslash, slash - pslash) || !add_html_to_string(page, decoded.source, decoded.length)
|| !add_to_string(page, "</a>") || !add_to_string(page, "</a>")
|| !add_char_to_string(page, sep)) || !add_html_to_string(page, &sep, 1))
goto out_of_memory; goto out_of_memory;
pslash = ++slash; pslash = ++slash;
@ -148,6 +150,7 @@ out_of_memory:
} }
done_string(&dirpath); done_string(&dirpath);
done_string(&decoded);
done_string(&location); done_string(&location);
return page->length > 0 ? S_OK : S_OUT_OF_MEM; return page->length > 0 ? S_OK : S_OUT_OF_MEM;