TIdMimeTable.BuildCache problem

Giganews Newsgroups
Subject: TIdMimeTable.BuildCache problem
Posted by:  Farshad Mohajeri (farshad…@fmsoft.net)
Date: Wed, 30 Sep 2009

Hi,

Not sure if this issue has been addressed before.

Problem  happens when you try serving files using AResponseInfo.ServeFile()
in CommandGet() event of TIdHTTPServer.

ServeFile() tries to resolve MIMEType by calling MIMETable.GetFileMIMEType.

If it is the very first call, BuildCache() is called to create a cache for
all MIME types. If there are multiple calls from different threads there can
be multiple calls to BuildCache() method *which is not thread-safe*. This
will lead to some random exceptions and memory errors, and as a result some
files are not served.

To make it more clear, consider your browser is requesting below files:

<link rel=stylesheet href="/css/css1.css" />
<script src="/src1.js"></script>
<script src="/src2.js"></script>
<script src="/src3.js"></script>
<script src="/src4.js"></script>
<script src="/src5.js"></script>

Browser opens a separate thread for each file and those requests are
received as multiple calls to CommandGet() almost simultaneously. This will
lead to multiple calls to BuildCache() from different threads.

My workaround is to make an explicit call to BuildCache() before I activate
my HTTP server.

MyHTTPServer.MIMETable.BuildCache;

Indy version: 10.2.3

Regards,
Farshad

Replies