Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 152 lines (123 sloc) 3.985 kb
21abc5b @klacke Initial revision
klacke authored
1 %%%----------------------------------------------------------------------
2 %%% File : yaws_ls.erl
3 %%% Author : Claes Wikstrom <klacke@hyber.org>
4 %%% Purpose :
5 %%% Created : 5 Feb 2002 by Claes Wikstrom <klacke@hyber.org>
6 %%%----------------------------------------------------------------------
7
8 -module(yaws_ls).
9 -author('klacke@hyber.org').
10
11 -compile(export_all).
3c06a9e @klacke rearr of includefiles
klacke authored
12
13 -include_lib("yaws/include/yaws.hrl").
14 -include_lib("yaws/include/yaws_api.hrl").
15 -include("yaws_debug.hrl").
16
21abc5b @klacke Initial revision
klacke authored
17 -include_lib("kernel/include/file.hrl").
18
19
20
38fb85e @klacke ""
klacke authored
21 list_directory(CliSock, List, DirName, Req, GC, SC) ->
21abc5b @klacke Initial revision
klacke authored
22 ?Debug("List=~p", [List]),
23 L = lists:zf(
24 fun(F) ->
25 File = SC#sconf.docroot ++ DirName ++ [$/|F],
26 FI = file:read_file_info(File),
27 file_entry(FI, DirName, F)
28 end, [".." | List]),
29 Body = [doc_head(DirName),
30 list_head(),
31 L,
22bbba4 Fixed the indentation of directory listings.
Tobbe Tornquist authored
32 "\n</pre><pre>",
33 inline_readme(SC,DirName,List),
34 "</pre>\n<hr>\n",
21abc5b @klacke Initial revision
klacke authored
35 yaws:address(GC, SC),
36 "</body>\n</html>\n"],
3b9e4a1 @klacke new ret vals from out/1
klacke authored
37 B = list_to_binary(Body),
38
39 yaws_server:make_date_and_server_headers(),
40 yaws_server:make_connection_close(true),
41 yaws_server:make_content_length(size(B)),
42 yaws_server:make_content_type("text/html"),
43
44 yaws_server:close_if_HEAD(
45 Req,
46 fun() ->
47 yaws_server:deliver_accumulated(#dcc{}, CliSock, GC, SC),
48 yaws_server:do_tcp_close(CliSock, SC),
49 throw({ok, 1})
50 end),
51 yaws_server:accumulate_content(B),
52 yaws_server:deliver_accumulated(#dcc{}, CliSock, GC, SC),
21abc5b @klacke Initial revision
klacke authored
53 done.
54
22bbba4 Fixed the indentation of directory listings.
Tobbe Tornquist authored
55 inline_readme(SC,DirName,L) ->
56 F = fun("README", _Acc) ->
57 File = SC#sconf.docroot ++ DirName ++ [$/ | "README"],
58 {ok,Bin} = file:read_file(File),
59 binary_to_list(Bin);
60 (_, Acc) ->
61 Acc
62 end,
63 lists:foldl(F,[],L).
21abc5b @klacke Initial revision
klacke authored
64
3b9e4a1 @klacke new ret vals from out/1
klacke authored
65
21abc5b @klacke Initial revision
klacke authored
66 doc_head(DirName) ->
2129adc @klacke ""
klacke authored
67 ?F("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\"> \n"
68 "<html> \n"
69 " <head> \n"
70 " <title>Index of ~s </title> \n"
71 " </head> \n"
72 " <body>\n"
73 " <h1>Index of ~s </h1>\n", [DirName, DirName]).
21abc5b @klacke Initial revision
klacke authored
74
75 list_head() ->
2129adc @klacke ""
klacke authored
76 " <pre><img SRC=\"/icons/blank.gif\" ALT=\" \"> "
77 "<a HREF=\"?N=D\">Name</a> "
22bbba4 Fixed the indentation of directory listings.
Tobbe Tornquist authored
78 "<a HREF=\"?M=A\">Last modified</a> "
79 " <a HREF=\"?S=A\">Size</a> "
2129adc @klacke ""
klacke authored
80 "<a HREF=\"?D=A\">Description</a> \n"
81 "<hr> \n".
21abc5b @klacke Initial revision
klacke authored
82
83
a7ac905 @klacke introduced opaque data in sconf
klacke authored
84 file_entry({ok, FI}, _DirName, Name) ->
21abc5b @klacke Initial revision
klacke authored
85 ?Debug("file_entry(~p) ", [Name]),
86 Ext = filename:extension(Name),
87 {Gif, Alt} = list_gif(FI#file_info.type, Ext),
22bbba4 Fixed the indentation of directory listings.
Tobbe Tornquist authored
88 {Trim,TrimLen} = trim(Name, 22),
89 Entry = ?F("<img SRC=~p ALT=~p> <a HREF=~p title=\"~w bytes\">~s</a> ~s~s ~8.s~n",
21abc5b @klacke Initial revision
klacke authored
90 ["/icons/" ++ Gif,
22bbba4 Fixed the indentation of directory listings.
Tobbe Tornquist authored
91 Alt,
aef9169 @klacke ""
klacke authored
92 Name,
22bbba4 Fixed the indentation of directory listings.
Tobbe Tornquist authored
93 FI#file_info.size,
21abc5b @klacke Initial revision
klacke authored
94 Trim,
22bbba4 Fixed the indentation of directory listings.
Tobbe Tornquist authored
95 lists:duplicate(22 - TrimLen, $\s),
21abc5b @klacke Initial revision
klacke authored
96 datestr(FI),
97 sizestr(FI)]),
98 ?Debug("Entry:~p", [Entry]),
99 {true, Entry};
2129adc @klacke ""
klacke authored
100 file_entry(_Err, _, _Name) ->
101 ?Debug("no entry for ~p: ~p", [_Name, _Err]),
21abc5b @klacke Initial revision
klacke authored
102 false.
103
104
22bbba4 Fixed the indentation of directory listings.
Tobbe Tornquist authored
105 %%% Compensate for '&gt' which really is just one character.
106 trim(L,N) ->
107 case trim(L,N,[]) of
108 {truncated,R} -> {R,length(R)-2};
109 R -> {R,length(R)}
110 end.
21abc5b @klacke Initial revision
klacke authored
111
22bbba4 Fixed the indentation of directory listings.
Tobbe Tornquist authored
112 trim([_H|_T], 4, Acc) ->
113 {truncated,lists:reverse(Acc) ++ "...&gt"};
114 trim([H|T], I, Acc) ->
115 trim(T, I-1, [H|Acc]);
116 trim([], _, Acc) ->
117 lists:reverse(Acc).
21abc5b @klacke Initial revision
klacke authored
118
119
120 datestr(FI) ->
121 yaws:time_to_string(FI#file_info.mtime, []).
22bbba4 Fixed the indentation of directory listings.
Tobbe Tornquist authored
122
21abc5b @klacke Initial revision
klacke authored
123 sizestr(FI) when FI#file_info.size > 1000000 ->
22bbba4 Fixed the indentation of directory listings.
Tobbe Tornquist authored
124 ?F("~.1fM", [FI#file_info.size / 1000000]);
21abc5b @klacke Initial revision
klacke authored
125 sizestr(FI) when FI#file_info.size > 1000 ->
22bbba4 Fixed the indentation of directory listings.
Tobbe Tornquist authored
126 ?F("~wk", [trunc(FI#file_info.size / 1000)]);
21abc5b @klacke Initial revision
klacke authored
127 sizestr(FI) ->
22bbba4 Fixed the indentation of directory listings.
Tobbe Tornquist authored
128 ?F("1k", []). % As apache does it...
21abc5b @klacke Initial revision
klacke authored
129
130
131 list_gif(directory, ".") ->
132 {"back.gif", "[DIR]"};
133 list_gif(regular, ".txt") ->
134 {"text.gif", "[TXT]"};
135 list_gif(regular, ".c") ->
136 {"c.gif", "[ ]"};
137 list_gif(regular, ".dvi") ->
138 {"dvi.gif", "[ ]"};
139 list_gif(regular, ".pdf") ->
140 {"pdf.gif", "[ ]"};
141 list_gif(regular, _) ->
142 {"layout.gif", "[ ]"};
143 list_gif(directory, _) ->
cd812ff @klacke ""
klacke authored
144 {"dir.gif", "[DIR]"};
145 list_gif(_, _) ->
146 {"unknown.gif", "[OTH]"}.
147
21abc5b @klacke Initial revision
klacke authored
148
149
150
151
Something went wrong with that request. Please try again.