bug 764: Initialize the right member of union option_value
[elinks.git] / src / protocol / file / file.c
blob46a3eac25958ecba45896eb1d64f6f8b451e565a
1 /* Internal "file" protocol implementation */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h> /* OS/2 needs this after sys/types.h */
13 #ifdef HAVE_FCNTL_H
14 #include <fcntl.h> /* OS/2 needs this after sys/types.h */
15 #endif
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
20 #include "elinks.h"
22 #include "cache/cache.h"
23 #include "config/options.h"
24 #include "encoding/encoding.h"
25 #include "intl/gettext/libintl.h"
26 #include "main/module.h"
27 #include "network/connection.h"
28 #include "osdep/osdep.h"
29 #include "protocol/common.h"
30 #include "protocol/file/cgi.h"
31 #include "protocol/file/file.h"
32 #include "protocol/uri.h"
33 #include "util/conv.h"
34 #include "util/file.h"
35 #include "util/memory.h"
36 #include "util/string.h"
39 static union option_info file_options[] = {
40 INIT_OPT_TREE("protocol", N_("Local files"),
41 "file", 0,
42 N_("Options specific to local browsing.")),
44 INIT_OPT_BOOL("protocol.file", N_("Allow reading special files"),
45 "allow_special_files", 0, 0,
46 N_("Whether to allow reading from non-regular files. "
47 "Note this can be dangerous; reading /dev/urandom or "
48 "/dev/zero can ruin your day!")),
50 INIT_OPT_BOOL("protocol.file", N_("Show hidden files in directory listing"),
51 "show_hidden_files", 0, 1,
52 N_("When set to false, files with name starting with a dot "
53 "will be hidden in local directory listings.")),
55 INIT_OPT_BOOL("protocol.file", N_("Try encoding extensions"),
56 "try_encoding_extensions", 0, 1,
57 N_("When set, if we can't open a file named 'filename', "
58 "we'll try to open 'filename' with some encoding extension "
59 "appended (ie. 'filename.gz'); it depends on the supported "
60 "encodings.")),
62 NULL_OPTION_INFO,
65 struct module file_protocol_module = struct_module(
66 /* name: */ N_("File"),
67 /* options: */ file_options,
68 /* hooks: */ NULL,
69 /* submodules: */ NULL,
70 /* data: */ NULL,
71 /* init: */ NULL,
72 /* done: */ NULL
76 /* Directory listing */
78 /* Based on the @entry attributes and file-/dir-/linkname is added to the @data
79 * fragment. All the strings are in the system charset. */
80 static inline void
81 add_dir_entry(struct directory_entry *entry, struct string *page,
82 int pathlen, unsigned char *dircolor)
84 unsigned char *lnk = NULL;
85 struct string html_encoded_name;
86 struct string uri_encoded_name;
88 if (!init_string(&html_encoded_name)) return;
89 if (!init_string(&uri_encoded_name)) {
90 done_string(&html_encoded_name);
91 return;
94 encode_uri_string(&uri_encoded_name, entry->name + pathlen, -1, 1);
95 add_html_to_string(&html_encoded_name, entry->name + pathlen,
96 strlen(entry->name) - pathlen);
98 /* add_to_string(&fragment, &fragmentlen, " "); */
99 add_html_to_string(page, entry->attrib, strlen(entry->attrib));
100 add_to_string(page, "<a href=\"");
101 add_string_to_string(page, &uri_encoded_name);
103 if (entry->attrib[0] == 'd') {
104 add_char_to_string(page, '/');
106 #ifdef FS_UNIX_SOFTLINKS
107 } else if (entry->attrib[0] == 'l') {
108 struct stat st;
109 unsigned char buf[MAX_STR_LEN];
110 int readlen = readlink(entry->name, buf, MAX_STR_LEN);
112 if (readlen > 0 && readlen != MAX_STR_LEN) {
113 buf[readlen] = '\0';
114 lnk = straconcat(" -> ", buf, (unsigned char *) NULL);
117 if (!stat(entry->name, &st) && S_ISDIR(st.st_mode))
118 add_char_to_string(page, '/');
119 #endif
122 add_to_string(page, "\">");
124 if (entry->attrib[0] == 'd' && *dircolor) {
125 /* The <b> is for the case when use_document_colors is off. */
126 string_concat(page, "<font color=\"", dircolor, "\"><b>",
127 (unsigned char *) NULL);
130 add_string_to_string(page, &html_encoded_name);
131 done_string(&uri_encoded_name);
132 done_string(&html_encoded_name);
134 if (entry->attrib[0] == 'd' && *dircolor) {
135 add_to_string(page, "</b></font>");
138 add_to_string(page, "</a>");
139 if (lnk) {
140 add_html_to_string(page, lnk, strlen(lnk));
141 mem_free(lnk);
144 add_char_to_string(page, '\n');
147 /* First information such as permissions is gathered for each directory entry.
148 * Finally the sorted entries are added to the @data->fragment one by one. */
149 static inline void
150 add_dir_entries(struct directory_entry *entries, unsigned char *dirpath,
151 struct string *page)
153 unsigned char dircolor[8];
154 int dirpathlen = strlen(dirpath);
155 int i;
157 /* Setup @dircolor so it's easy to check if we should color dirs. */
158 if (get_opt_bool("document.browse.links.color_dirs")) {
159 color_to_string(get_opt_color("document.colors.dirs"),
160 (unsigned char *) &dircolor);
161 } else {
162 dircolor[0] = 0;
165 for (i = 0; entries[i].name; i++) {
166 add_dir_entry(&entries[i], page, dirpathlen, dircolor);
167 mem_free(entries[i].attrib);
168 mem_free(entries[i].name);
171 /* We may have allocated space for entries but added none. */
172 mem_free_if(entries);
175 /* Generates an HTML page listing the content of @directory with the path
176 * @dirpath. */
177 /* Returns a connection state. S_OK if all is well. */
178 static inline struct connection_state
179 list_directory(struct connection *conn, unsigned char *dirpath,
180 struct string *page)
182 int show_hidden_files = get_opt_bool("protocol.file.show_hidden_files");
183 struct directory_entry *entries;
184 struct connection_state state;
186 errno = 0;
187 entries = get_directory_entries(dirpath, show_hidden_files);
188 if (!entries) {
189 if (errno) return connection_state_for_errno(errno);
190 return connection_state(S_OUT_OF_MEM);
193 state = init_directory_listing(page, conn->uri);
194 if (!is_in_state(state, S_OK))
195 return connection_state(S_OUT_OF_MEM);
197 add_dir_entries(entries, dirpath, page);
199 if (!add_to_string(page, "</pre>\n<hr/>\n</body>\n</html>\n")) {
200 done_string(page);
201 return connection_state(S_OUT_OF_MEM);
204 return connection_state(S_OK);
208 /* To reduce redundant error handling code [calls to abort_connection()]
209 * most of the function is build around conditions that will assign the error
210 * code to @state if anything goes wrong. The rest of the function will then just
211 * do the necessary cleanups. If all works out we end up with @state being S_OK
212 * resulting in a cache entry being created with the fragment data generated by
213 * either reading the file content or listing a directory. */
214 void
215 file_protocol_handler(struct connection *connection)
217 unsigned char *redirect_location = NULL;
218 struct string page, name;
219 struct connection_state state;
220 int set_dir_content_type = 0;
222 if (get_cmd_opt_bool("anonymous")) {
223 if (strcmp(connection->uri->string, "file:///dev/stdin")
224 || isatty(STDIN_FILENO)) {
225 abort_connection(connection,
226 connection_state(S_FILE_ANONYMOUS));
227 return;
231 #ifdef CONFIG_CGI
232 if (!execute_cgi(connection)) return;
233 #endif /* CONFIG_CGI */
235 /* This function works on already simplified file-scheme URI pre-chewed
236 * by transform_file_url(). By now, the function contains no hostname
237 * part anymore, possibly relative path is converted to an absolute one
238 * and uri->data is just the final path to file/dir we should try to
239 * show. */
241 if (!init_string(&name)
242 || !add_uri_to_string(&name, connection->uri, URI_PATH)) {
243 done_string(&name);
244 abort_connection(connection, connection_state(S_OUT_OF_MEM));
245 return;
248 decode_uri_string(&name);
250 /* In Win32, file_is_dir seems to always return 0 if the name
251 * ends with a directory separator. */
252 if ((name.length > 0 && dir_sep(name.source[name.length - 1]))
253 || file_is_dir(name.source)) {
254 /* In order for global history and directory listing to
255 * function properly the directory url must end with a
256 * directory separator. */
257 if (name.source[0] && !dir_sep(name.source[name.length - 1])) {
258 redirect_location = STRING_DIR_SEP;
259 state = connection_state(S_OK);
260 } else {
261 state = list_directory(connection, name.source, &page);
262 set_dir_content_type = 1;
265 } else {
266 state = read_encoded_file(&name, &page);
267 /* FIXME: If state is now S_ENCODE_ERROR we should try loading
268 * the file undecoded. --jonas */
271 done_string(&name);
273 if (is_in_state(state, S_OK)) {
274 struct cache_entry *cached;
276 /* Try to add fragment data to the connection cache if either
277 * file reading or directory listing worked out ok. */
278 cached = connection->cached = get_cache_entry(connection->uri);
279 if (!connection->cached) {
280 if (!redirect_location) done_string(&page);
281 state = connection_state(S_OUT_OF_MEM);
283 } else if (redirect_location) {
284 if (!redirect_cache(cached, redirect_location, 1, 0))
285 state = connection_state(S_OUT_OF_MEM);
287 } else {
288 add_fragment(cached, 0, page.source, page.length);
289 connection->from += page.length;
291 if (!cached->head && set_dir_content_type) {
292 unsigned char *head;
294 /* If the system charset somehow
295 * changes after the directory listing
296 * has been generated, it should be
297 * parsed with the original charset. */
298 head = straconcat("\r\nContent-Type: text/html; charset=",
299 get_cp_mime_name(get_cp_index("System")),
300 "\r\n", (unsigned char *) NULL);
302 /* Not so gracefully handle failed memory
303 * allocation. */
304 if (!head)
305 state = connection_state(S_OUT_OF_MEM);
307 /* Setup directory listing for viewing. */
308 mem_free_set(&cached->head, head);
311 done_string(&page);
315 abort_connection(connection, state);