1 /* Internal "file" protocol implementation */
11 #include <sys/types.h>
12 #include <sys/stat.h> /* OS/2 needs this after sys/types.h */
14 #include <fcntl.h> /* OS/2 needs this after sys/types.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"),
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 "
65 struct module file_protocol_module
= struct_module(
66 /* name: */ N_("File"),
67 /* options: */ file_options
,
69 /* submodules: */ 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. */
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
);
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') {
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
) {
114 lnk
= straconcat(" -> ", buf
, (unsigned char *) NULL
);
117 if (!stat(entry
->name
, &st
) && S_ISDIR(st
.st_mode
))
118 add_char_to_string(page
, '/');
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>");
140 add_html_to_string(page
, lnk
, strlen(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. */
150 add_dir_entries(struct directory_entry
*entries
, unsigned char *dirpath
,
153 unsigned char dircolor
[8];
154 int dirpathlen
= strlen(dirpath
);
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
);
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
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
,
182 int show_hidden_files
= get_opt_bool("protocol.file.show_hidden_files");
183 struct directory_entry
*entries
;
184 struct connection_state state
;
187 entries
= get_directory_entries(dirpath
, show_hidden_files
);
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")) {
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. */
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
));
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
241 if (!init_string(&name
)
242 || !add_uri_to_string(&name
, connection
->uri
, URI_PATH
)) {
244 abort_connection(connection
, connection_state(S_OUT_OF_MEM
));
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
);
261 state
= list_directory(connection
, name
.source
, &page
);
262 set_dir_content_type
= 1;
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 */
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
);
288 add_fragment(cached
, 0, page
.source
, page
.length
);
289 connection
->from
+= page
.length
;
291 if (!cached
->head
&& set_dir_content_type
) {
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
305 state
= connection_state(S_OUT_OF_MEM
);
307 /* Setup directory listing for viewing. */
308 mem_free_set(&cached
->head
, head
);
315 abort_connection(connection
, state
);