Permalink
Browse files

Make the mime types mappings configurable

Now, it possible to customize the global mime types mappings and to overload
it for each virtual server. It can be done using following directives in the
global part or the server part of the configuration:

* default_type: Defines the default mime type to be used where Yaws cannot
  determine it by its mime types mappings (default: text/plain).
  In the server part, this directive overloads the global one.

* default_charset: Defines the default charset to be added when a response
  content-type is text/* (default: none). In the server part, this directive
  overloads the global one.

* mime_types_file: Overrides the default mime.types file included with Yaws
  (default: ${PREFIX}/lib/yaws/priv/mime.types). In the server part, this
  directive overloads the global one but mappings defined in this file will
  not overload those defined by add_types directives in the global part.

* add_types: Specifies one or more mappings between mime types and file
  extensions. More than one extension can be assigned to a mime type. If a
  mapping is defined in the global part and redefined in a server part using
  this directive, then the later is used. Else the global one is kept.

* add_charsets: Specifies one or more mappings between charsets and file
  extensions. More than one extension can be assigned to a charset. If a
  mapping is defined in the global part and redefined in a server part using
  this directive, then the later is used. Else the global one is kept.

Here is an example:

  default_type = text/html

  <server localhost>
      port = 8000
      listen = 0.0.0.0
      docroot = /var/www
      # nothing is overloaded in the vhost
  </server>

  <server localhost>
      port = 8001
      listen = 0.0.0.0
      docroot = /var/www

      # overload global configuration:
      default_type    = text/plain
      mime_types_file = /etc/mime.types
      add_types       = <text/xhtml, yaws> <application/x-test, tst test>
      default_charset = UTF-8
      add_charsets    = <ISO-8859-1, php html yaws> <US-ASCII, tst>
  </server>

During Yaws compilation, a default module 'mime_types' is created using the
default mime.types file. Then, when yaws starts up, this module is
re-generated, re-compiled and loaded dynamically. The new module replaces the
default one but the .beam file is unchanged. So if one of these steps failed,
we fall back on the default module.
  • Loading branch information...
1 parent a5515f2 commit 89a64ef481a653d3aaed3ce24ee05ccc0ed22b5d @capflam capflam committed Jul 19, 2012
Showing with 2,341 additions and 1,647 deletions.
  1. +3 −3 .gitignore
  2. +70 −1 doc/yaws.tex
  3. +15 −5 include/yaws.hrl
  4. +84 −5 man/yaws.conf.5
  5. +17 −2 man/yaws_api.5
  6. +1,337 −79 priv/mime.types
  7. +5 −5 scripts/rebar-pre-script
  8. +8 −6 src/Makefile
  9. +0 −1,357 src/mime.types
  10. +234 −125 src/mime_type_c.erl
  11. +6 −2 src/yaws.erl
  12. +6 −5 src/yaws_api.erl
  13. +245 −17 src/yaws_config.erl
  14. +27 −33 src/yaws_server.erl
  15. +1 −1 test/Makefile
  16. +128 −0 test/conf/mimetypes.conf
  17. +1 −1 test/support/include.mk.in
  18. +33 −0 test/t7/Makefile
  19. +118 −0 test/t7/app_test.erl
  20. +3 −0 test/t7/mime.types
View
@@ -16,8 +16,8 @@ config.status
ebin/yaws.app
include.mk
priv/epam
+priv/charset.def
scripts/yaws.conf
-src/charset.def
src/mime_types.erl
src/yaws_configure.hrl
src/yaws_generated.erl
@@ -26,8 +26,8 @@ test/ibrowse.tar.gz
test/support/include.mk
test/support/include.sh
test/t1/localhost:8000/
-test/t[123456]/logs/
-test/t[123456]/yaws.conf
+test/t[1234567]/logs/
+test/t[1234567]/yaws.conf
test/t4/www2/8388608.bin
www/yaws.pdf
www/yaws.ps
View
@@ -1547,7 +1547,7 @@ \section{Stream content}
{streamcontent_from_pid, MimeType, Pid}
\end{verbatim}
-\noindent to tell \Yaws\ we wish to deliver data of mime type
+\noindent to tell \Yaws\ we wish to deliver data of MIME type
\verb+MimeType+ to the client from process \verb+Pid+. In this case,
\Yaws\ will prepare the socket for delivery from \verb+Pid+ and then
send one of the following messages to \verb+Pid+:
@@ -2319,6 +2319,50 @@ \section{Global Part}
\item \verb+x_forwarded_for_log_proxy_whitelist = ListOfUpstreamProxyServerIps+ ---
\textit{This target is deprecated and will be ignored}.
+\item \verb+default_type = MimeType+ ---
+ Defines the default MIME type to be used where \Yaws\ cannot
+ determine it by its MIME types mappings. Default is
+ \textit{text/plain}.
+
+\item \verb+default_charset = Charset+ ---
+ Defines the default charset to be added when a response
+ content-type is \textit{text/*}. By default, no charset is added.
+
+\item \verb+mime_types_file = File+ ---
+ Overloads the default \textit{mime.types} file included with
+ \Yaws{}. This file must use the following format:
+\begin{verbatim}
+ # Lines beginning with a '#' or a whitespace are ignored
+ # blank lines are also ignored
+ <MIME type> <space separated file extensions>
+\end{verbatim}
+ The default file is located at
+ \textit{\${PREFIX}/lib/yaws/priv/mime.types}. You should not edit
+ this file because it may be replaced when you upgrade your server.
+
+\item \verb+add_types = ListOfTypes+ ---
+ Specifies one or more mappings between MIME types and file
+ extensions. More than one extension can be assigned to a MIME
+ type. \textit{ListOfTypes} is defined as follows:
+\begin{verbatim}
+ add_types = <MimeType1, Ext> <MimeType2, Ext1 Ext2 ...> ...
+\end{verbatim}
+ The mappings defined using this directive will overload all other
+ definitions. If a file extension is defined several times, only
+ the last one is kept. Multiple \textit{add\_types} directives may
+ be used.
+
+\item \verb+add_charsets = ListOfCharsets+ ---
+ Specifies one or more mappings between charsets and file
+ extensions. More than one extension can be assigned to a
+ charset. \textit{ListOfCharsets} is defined as follows:
+\begin{verbatim}
+ add_charsets = <Charset1, Ext> <Charset2, Ext1 Ext2 ...> ...
+\end{verbatim}
+ The mappings defined using this directive will overload all other
+ definitions. If a file extension is defined several times, only
+ the last one is kept. Multiple \textit{add\_charsets} directives
+ may be used.
\end{itemize}
@@ -2859,6 +2903,31 @@ \section{Server Part}
Use this directive is same as:
\verb+php_handler = <fcgi, HostPortSpec>+.
+\item \verb+default_type = MimeType+ ---
+ Overloads the global \verb+default_type+ value for this virtual
+ server.
+
+\item \verb+default_charset = Charset+ ---
+ Overloads the global \verb+default_charset+ value for this virtual
+ server.
+
+\item \verb+mime_types_file = File+ ---
+ Overrides the global \verb+mime_type_file+ value for this virtual
+ server. Mappings defined in \textit{File} will not overload those
+ defined by \verb+add_types+ directives in the global part.
+
+\item \verb+add_types = ListOfTypes+ ---
+ Overloads the global \verb+add_types+ values for this virtual
+ server. If a mapping is defined in the global part and redefined
+ in a server part using this directive, then it is replaced. Else
+ it is kept.
+
+\item \verb+add_charsets = ListOfCharsets+ ---
+ Overloads the global \verb+add_charsets+ values for this virtual
+ server. If a mapping is defined in the global part and redefined
+ in a server part using this directive, then it is replaced. Else
+ it is kept.
+
\item \verb+<ssl> .... </ssl>+
This begins and ends an SSL configuration for this server. It's
possible to virthost several SSL servers on the same IP given
View
@@ -97,10 +97,10 @@
soap_srv_mods = [],
ysession_mod = yaws_session_server, % storage module for ysession
- acceptor_pool_size = 8 % size of acceptor proc pool
- }).
-
+ acceptor_pool_size = 8, % size of acceptor proc pool
+ mime_types_info
+ }).
-record(ssl, {
keyfile,
@@ -241,7 +241,8 @@
fcgi_app_server, % FastCGI application server {host,port}
php_handler = {cgi, "/usr/bin/php-cgi"},
shaper,
- deflate_options
+ deflate_options,
+ mime_types_info % if undefined, global config is used
}).
@@ -289,6 +290,16 @@
}).
+%% Internal record used to set information about mime-types
+-record(mime_types_info, {
+ mime_types_file, % an absolute filename path
+ types = [], % a map between mime-types and extensions
+ charsets = [], % a map between charsets and extensions
+ default_type = "text/plain",
+ default_charset
+ }).
+
+
%% this internal record is used and returned by the URL path parser
-record(urltype, {
type, % error | yaws | regular | directory | forbidden |
@@ -349,7 +360,6 @@
}).
-
%% as read by application:get_env()
-record(env, {
debug,
View
@@ -246,6 +246,60 @@ Load all config file in specified directory.
\fBx_forwarded_for_log_proxy_whitelist = ListOfUpstreamProxyServerIps\fR
\fBthis target is deprecated and will be ignored.\fR
+.TP
+\fBdefault_type = MimeType\fR
+Defines the default MIME type to be used where Yaws cannot determine it by its
+MIME types mappings. Default is \fItext/plain\fR.
+
+.TP
+\fBdefault_charset = Charset\fR
+Defines the default charset to be added when a response content-type is
+\fItext/*\fR. By default, no charset is added.
+
+.TP
+\fBmime_types_file = File\fR
+Overrides the default \fImime.types\fR file included with Yaws. This file must
+use the following format:
+
+.nf
+ # Lines beginning with a '#' or a whitespace are ignored
+ # blank lines are also ignored
+ <MIME type> <space separated file extensions>
+.fi
+
+The default file is located at \fI${PREFIX}/lib/yaws/priv/mime.types\fR. You
+should not edit this file because it may be replaced when you upgrade your
+server.
+
+.TP
+\fBadd_types = ListOfTypes\fR
+Specifies one or more mappings between MIME types and file extensions. More than
+one extension can be assigned to a MIME type. \fIListOfTypes\fR is defined as
+follows:
+
+.nf
+ add_types = <MimeType1, Ext> <MimeType2, Ext1 Ext2 ...> ...
+.fi
+
+The mappings defined using this directive will overload all other
+definitions. If a file extension is defined several times, only the last one is
+kept. Multiple \fBadd_types\fR directives may be used.
+
+.TP
+\fBadd_charsets = ListOfCharsets\fR
+Specifies one or more mappings between charsets and file extensions. More than
+one extension can be assigned to a charset. \fIListOfCharsets\fR is defined as
+follows:
+
+.nf
+ add_charsets = <Charset1, Ext> <Charset2, Ext1 Ext2 ...> ...
+.fi
+
+The mappings defined using this directive will overload all other
+definitions. If a file extension is defined several times, only the last one is
+kept. Multiple \fBadd_charsets\fR directives may be used.
+
+
.SH SERVER PART
Yaws can virthost several web servers on the same IP address as well as several
web servers on different IP addresses. This includes SSL servers.
@@ -476,8 +530,8 @@ is \fIfalse\fR.
\fBmime_types = ListOfTypes | defaults | all\fR
.RS 12
-Restricts the deflate compression to particular mime types. The special value
-\fIall\fR enable it for all types (It is a synonym of `*/*'). Mime types into
+Restricts the deflate compression to particular MIME types. The special value
+\fIall\fR enable it for all types (It is a synonym of `*/*'). MIME types into
\fIListOfTypes\fR must have the form `type/subtype' or `type/*' (indicating all
subtypes of that type). Here is an example:
@@ -486,10 +540,10 @@ subtypes of that type). Here is an example:
mime_types = application/xml application/xhtml+xml application/rss+xml
.fi
-By default, following mime types are compressed (if
+By default, following MIME types are compressed (if
\fBdeflate\fR is set to true): \fItext/*, application/rtf, application/msword,
application/pdf, application/x-dvi, application/javascript,
-application/x-javascript\fR. Multiple mime_types directive can be used.
+application/x-javascript\fR. Multiple \fBmime_types\fR directive can be used.
.RE
.HP
@@ -634,7 +688,7 @@ The function must return, \fI{content,MimeType,Cont}\fR or \fI{html, Str}\fR or
\fBexpires = ListOfExpires\fR
Controls the setting of the \fIExpires\fR HTTP header and the \fImax-age\fR
directive of the \fICache-Control\fR HTTP header in server responses for
-specific mime types. The expiration date can set to be relative to either the
+specific MIME types. The expiration date can set to be relative to either the
time the source file was last modified, or to the time of the client
access. ListOfExpires is defined as follows:
@@ -802,6 +856,31 @@ instead.\fR
.br
Use this directive is same as: php_handler = <fcgi, Host:Port>.
+.TP
+\fBdefault_type = MimeType\fR
+Overloads the global \fBdefault_type\fR value for this virtual server.
+.TP
+\fBdefault_charset = Charset\fR
+Overloads the global \fBdefault_charset\fR value for this virtual server.
+
+.TP
+\fBmime_types_file = File\fR
+Overloads the global \fBmime_type_file\fR value for this virtual
+server. Mappings defined in \fIFile\fR will not overload those defined by
+\fBadd_types\fR directives in the global part.
+
+.TP
+\fBadd_types = ListOfTypes\fR
+Overloads the global \fBadd_types\fR values for this virtual server. If a
+mapping is defined in the global part and redefined in a server part using this
+directive, then it is replaced. Else it is kept.
+
+.TP
+\fBadd_charsets = ListOfCharsets\fR
+Overloads the global \fBadd_charsets\fR values for this virtual server. If a
+mapping is defined in the global part and redefined in a server part using this
+directive, then it is replaced. Else it is kept.
+
.TP
\fB<ssl> ... </ssl>\fR
This begins and ends an SSL configuration for this server. It's possible to
View
@@ -239,9 +239,24 @@ from a stream of data. It returns:
The function handles multilines as defined in e.g. SMTP or HTTP
+.TP
+\fBmime_type(Scope, FileName)\fR
+Returns the MIME type as defined by the extension of \fIFileName\fR. \fIScope\fR
+can have following values:
+
+.RS 12
+\fBglobal\fR - returns the result obtained from the global context.
+.br
+\fB#sconf{} | {ServerName, Port}\fR - returns the result obtained from the
+virtual server's context. If no MIME type is found in this scope, it falls back
+on the global one.
+.RE
+.HP
+
.TP
\fBmime_type(FileName)\fR
-Returns the mime type as defined by the extension of FileName
+Tries to determine the right \fIScope\fR before calling mime_type/2.
+
.TP
\fBstream_chunk_deliver(YawsPid, Data)\fR
@@ -644,7 +659,7 @@ The code will be inserted in the page.
.TP
\fB{ehtml|exhtml, Term}\fR
This will transform the erlang term Term into a
-stream of HTML content. The exhtml variant transforms into
+stream of HTML content. The exhtml variant transforms into
strict XHTML code. The basic syntax of Term
is
Oops, something went wrong.

0 comments on commit 89a64ef

Please sign in to comment.