Debian Codegraph @

doc/src/manual/cowboy_static.asciidoc

raw
1 = cowboy_static(3)
2
3 == Name
4
5 cowboy_static - Static file handler
6
7 == Description
8
9 The module `cowboy_static` implements file serving capabilities
10 using the REST semantics provided by `cowboy_rest`.
11
12 The static file handler is a pre-written handler coming with
13 Cowboy. To serve files, use it in your routes.
14
15 == Options
16
17 [source,erlang]
18 ----
19 opts() :: {priv_file, App, Path}
20 | {priv_file, App, Path, Extra}
21 | {file, Path}
22 | {file, Path, Extra}
23 | {priv_dir, App, Path}
24 | {priv_dir, App, Path, Extra}
25 | {dir, Path}
26 | {dir, Path, Extra}
27
28 App :: atom()
29 Path :: binary() | string()
30 Extra :: [Charset | Etag | Mimetypes]
31
32 Charset :: {charset, module(), function()}
33 | {charset, binary()}
34
35 Etag :: {etag, module(), function()}
36 | {etag, false}
37
38 Mimetypes :: {mimetypes, module(), function()}
39 | {mimetypes, binary() | ParsedMime}
40
41 ParsedMime :: {Type :: binary(), SubType :: binary(), Params}
42 Params :: [{Key :: binary(), Value :: binary()}]
43 ----
44
45 Static handler configuration.
46
47 priv_file::
48
49 Send a file.
50 +
51 The path is relative to the given application's private
52 directory.
53
54 file::
55
56 Send a file.
57 +
58 The path is either absolute or relative to the Erlang node's
59 current directory.
60
61 priv_dir::
62
63 Recursively serve files from a directory.
64 +
65 The path is relative to the given application's private
66 directory.
67
68 dir::
69
70 Recursively serve files from a directory.
71 +
72 The path is either absolute or relative to the Erlang node's
73 current directory.
74
75 NOTE: The static handler considers that the local filesystem
76 is trusted. Be careful when writing data on disk to files
77 or directories served by the static handler.
78
79 NOTE: The static handler follows symlinks and symlinks may
80 point to an entirely different location than configured.
81 For example, when configured with `dir`, the static handler
82 may serve files from outside of this directory if the
83 directory contains symlinks.
84
85 The extra options allow you to define how the etag should be
86 calculated and how the MIME type of files should be detected.
87
88 By default the static handler will not send a charset with
89 the response. You can provide a specific charset that will
90 be used for all files using the text media type, or provide
91 a module and function that will be called when needed:
92
93 [source,erlang]
94 ----
95 detect_charset(Path :: binary()) -> Charset :: binary()
96 ----
97
98 A charset must always be returned even if it doesn't make
99 sense considering the media type of the file. A good default
100 is `<<"utf-8">>`.
101
102 By default the static handler will generate an etag based
103 on the size and modification time of the file. You may disable
104 the etag entirely with `{etag, false}` or provide a module
105 and function that will be called when needed:
106
107 [source,erlang]
108 ----
109 generate_etag(Path, Size, Mtime) -> {strong | weak, binary()}
110
111 Path :: binary()
112 Size :: non_neg_integer()
113 Mtime :: file:date_time()
114 ----
115
116 By default the static handler will detect Web-related MIME types
117 by looking at the file extension. You can provide a specific
118 MIME type that will always be used, or a module and function that
119 will be called when needed:
120
121 [source,erlang]
122 ----
123 detect_mimetype(Path) -> ParsedMime
124
125 Path :: binary()
126 ParsedMime :: {Type :: binary(), SubType :: binary(), Params}
127 Params :: [{Key :: binary(), Value :: binary()}]
128 ----
129
130 // @todo Case sensitivity of parsed mime content?
131
132 Cowboy comes with two such functions; the default function
133 `cow_mimetypes:web/1`, and a second function generated from
134 the Apache 'mime.types' file, `cow_mimetypes:all/1`.
135
136 The MIME type function should return
137 `{<<"application">>, <<"octet-stream">>, []}`
138 when it fails to detect a file's MIME type.
139
140 == Changelog
141
142 * *2.11*: Support for range requests was added in 2.6 and
143 is now considered stable.
144 * *2.6*: The `charset` extra option was added.
145 * *1.0*: Handler introduced.
146
147 == Examples
148
149 .Custom etag function
150 [source,erlang]
151 ----
152 generate_etag(Path, Size, Mtime) ->
153 {strong, integer_to_binary(
154 erlang:phash2({Path, Size, Mtime}, 16#ffffffff))}.
155 ----
156
157 .Custom MIME type function
158 [source,erlang]
159 ----
160 always_octet_stream(_Path) ->
161 case filename:extension(Path) of
162 <<".erl">> -> {<<"text">>, <<"plain">>, []};
163 _ -> {<<"application">>, <<"octet-stream">>, []}
164 end.
165 ----
166
167 == See also
168
169 link:man:cowboy(7)[cowboy(7)],
170 link:man:cowboy_router(3)[cowboy_router(3)]