myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myserver-commit] [2929] Added the possibility to specify the MIME type


From: Giuseppe Scrivano
Subject: [myserver-commit] [2929] Added the possibility to specify the MIME type using a regular expression on the file name .
Date: Sun, 02 Nov 2008 12:28:59 +0000

Revision: 2929
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2929
Author:   gscrivano
Date:     2008-11-02 12:28:58 +0000 (Sun, 02 Nov 2008)

Log Message:
-----------
Added the possibility to specify the MIME type using a regular expression on 
the file name.

Modified Paths:
--------------
    trunk/myserver/documentation/mime_types.texi
    trunk/myserver/include/base/regex/myserver_regex.h
    trunk/myserver/include/conf/mime/mime_manager.h
    trunk/myserver/src/conf/mime/mime_manager.cpp
    trunk/myserver/src/http_handler/cgi/cgi.cpp
    trunk/myserver/src/protocol/http/http.cpp

Modified: trunk/myserver/documentation/mime_types.texi
===================================================================
--- trunk/myserver/documentation/mime_types.texi        2008-11-01 19:47:52 UTC 
(rev 2928)
+++ trunk/myserver/documentation/mime_types.texi        2008-11-02 12:28:58 UTC 
(rev 2929)
@@ -73,3 +73,18 @@
 @item ISAPI
 Run the ISAPI script.  
 @end enumerate
+
+It is possible to match the file name against a regular expression
+instead of specifying only its extension.
+
+It is done using the @code{PATH} directive in the following way:
+
address@hidden
+<?xml version ="1.0"?>
+<MIME mime="text/html" handler="SEND" param="">
+<PATH regex="/foo/bar/.*" />
+</MIME>
address@hidden example
+
+The @code{PATH} regular expressions are checked before the file
+extension, in the order they are specified in the configuration file.

Modified: trunk/myserver/include/base/regex/myserver_regex.h
===================================================================
--- trunk/myserver/include/base/regex/myserver_regex.h  2008-11-01 19:47:52 UTC 
(rev 2928)
+++ trunk/myserver/include/base/regex/myserver_regex.h  2008-11-02 12:28:58 UTC 
(rev 2929)
@@ -49,8 +49,8 @@
 using namespace std;
 
 /*!
-*This class is used to manage regular expressions in MyServer.
-*/
+ *This class is used to manage regular expressions in MyServer.
+ */
 class Regex
 {
 public:

Modified: trunk/myserver/include/conf/mime/mime_manager.h
===================================================================
--- trunk/myserver/include/conf/mime/mime_manager.h     2008-11-01 19:47:52 UTC 
(rev 2928)
+++ trunk/myserver/include/conf/mime/mime_manager.h     2008-11-02 12:28:58 UTC 
(rev 2929)
@@ -23,6 +23,7 @@
 #include <include/base/hash_map/hash_map.h>
 #include <include/base/sync/mutex.h>
 #include <include/base/xml/xml_parser.h>
+#include <include/base/regex/myserver_regex.h>
 
 
 #ifdef WIN32
@@ -46,6 +47,12 @@
 
 using namespace std;
 
+struct PathRegex
+{
+  Regex *regex;
+  int record;
+};
+
 struct MimeRecord
 {
        list<string> filters;
@@ -54,6 +61,8 @@
        string cmdName;
        string cgiManager;
   bool selfExecuted;
+  list<Regex*> pathRegex;
+
        MimeRecord ();
        MimeRecord (MimeRecord&);
        int addFilter (const char*, bool acceptDuplicate = true);
@@ -88,7 +97,8 @@
 private:
   bool loaded;
   HashMap<string, int> extIndex;
-  vector <MimeRecord*> records;
+  vector<MimeRecord*> records;
+  list<PathRegex*> pathRegex;
 
        u_long numMimeTypesLoaded;
        string filename;

Modified: trunk/myserver/src/conf/mime/mime_manager.cpp
===================================================================
--- trunk/myserver/src/conf/mime/mime_manager.cpp       2008-11-01 19:47:52 UTC 
(rev 2928)
+++ trunk/myserver/src/conf/mime/mime_manager.cpp       2008-11-02 12:28:58 UTC 
(rev 2929)
@@ -107,6 +107,15 @@
   mimeType.assign (""); 
   cmdName.assign ("");
   cgiManager.assign ("");
+
+  for (list<Regex*>::iterator it = pathRegex.begin (); 
+       it != pathRegex.end (); 
+       it++)
+  {
+    delete *it;
+  }
+
+  pathRegex.clear ();
 }  
 
 /*!
@@ -143,6 +152,7 @@
       rc->cgiManager.assign ((const char*)attrs->children->content);
   }
 
+
   for ( ;lcur; lcur = lcur->next)
   {
     if (lcur->name && !xmlStrcmp(lcur->name, (const xmlChar *)"EXTENSION"))
@@ -158,6 +168,23 @@
       }
     }
 
+    if (lcur->name && !xmlStrcmp(lcur->name, (const xmlChar *)"PATH"))
+    {
+      for (attrs = lcur->properties; attrs; attrs = attrs->next)
+      {
+        if (!xmlStrcmp (attrs->name, (const xmlChar *)"regex") && 
+            attrs->children && attrs->children->content)
+        {
+          Regex *r = new Regex;
+
+          if (r->compile ((const char*)attrs->children->content, 0))
+            return NULL;
+
+          rc->pathRegex.push_back (r);
+        }
+      }
+    }
+
     if (lcur->name && !xmlStrcmp(lcur->name, (const xmlChar *)"FILTER"))
     {
       for (attrs = lcur->properties; attrs; attrs = attrs->next)
@@ -277,7 +304,18 @@
 
     extIndex.put (ext, position);
   }
-  
+
+  for (list<Regex*>::iterator it = mr->pathRegex.begin (); 
+       it != mr->pathRegex.end (); 
+       it++)
+  {
+    PathRegex *pr = new PathRegex;
+    pr->regex = *it;
+    pr->record = position;
+
+    pathRegex.push_back (pr);
+  }
+
   return position;
 }
 
@@ -298,6 +336,14 @@
     i++;
   }
 
+  for (list<PathRegex*>::iterator it = pathRegex.begin (); 
+       it != pathRegex.end (); 
+       it++)
+  {
+    delete *it;
+  }
+
+  pathRegex.clear ();
   records.clear ();
 
   extIndex.clear ();
@@ -307,21 +353,43 @@
 }
 
 /*!
- *Get a pointer to a MIME record by its extension.
+ *Get the MIME type to use on the specified file.
  */
-MimeRecord *MimeManager::getMIME (const char *ext)
+MimeRecord *MimeManager::getMIME (const char *filename)
 {
-  string str (ext);
-  return getMIME (str);
+  string str (filename);
+  return getMIME (filename);
 }
 
 /*!
- *Get a pointer to an existing record passing its extension.
+ *Get the MIME type to use on the specified file.
  */
-MimeRecord *MimeManager::getMIME (string const &ext)
+MimeRecord *MimeManager::getMIME (string const &filename)
 {
-  u_long pos = extIndex.get (ext.c_str ());
+  string ext;
+  u_long pos = 0;
+  
 
+  for (list<PathRegex*>::iterator it = pathRegex.begin (); 
+       it != pathRegex.end (); 
+       it++)
+  {
+    PathRegex *pr = *it;
+    regmatch_t pm;
+
+    if (pr->regex->exec(filename.c_str (), 1, &pm, 0) == 0)
+    {
+      pos = pr->record;
+      break;
+    }
+  }
+ 
+  if (pos == 0)
+  {
+    FilesUtility::getFileExt (ext, filename);
+    pos = extIndex.get (ext.c_str ());
+  }
+
   if (pos)
     return records [pos];
 

Modified: trunk/myserver/src/http_handler/cgi/cgi.cpp
===================================================================
--- trunk/myserver/src/http_handler/cgi/cgi.cpp 2008-11-01 19:47:52 UTC (rev 
2928)
+++ trunk/myserver/src/http_handler/cgi/cgi.cpp 2008-11-02 12:28:58 UTC (rev 
2929)
@@ -90,8 +90,10 @@
     return td->http->sendAuth();
 
   td->scriptPath.assign (scriptpath);
+
+  if (!FilesUtility::fileExists (scriptpath))
+    return td->http->raiseHTTPError(404);
   
-  
   int subString = cgipath[0] == '"';
   /* Do not modify the text between " and ".  */
   for(i = 1; i < len; i++)
@@ -178,7 +180,7 @@
   }
   else
   {
-    if(!FilesUtility::fileExists(tmpCgiPath.c_str()))
+    if (!FilesUtility::fileExists (tmpCgiPath.c_str ()))
     {
       if(tmpCgiPath.length() > 0)
       {
@@ -198,7 +200,6 @@
       td->scriptDir.assign("");
       chain.clearAllFilters(); 
       return td->http->raiseHTTPError(500);
-
     }
 
     spi.arg.assign(moreArg);

Modified: trunk/myserver/src/protocol/http/http.cpp
===================================================================
--- trunk/myserver/src/protocol/http/http.cpp   2008-11-01 19:47:52 UTC (rev 
2928)
+++ trunk/myserver/src/protocol/http/http.cpp   2008-11-02 12:28:58 UTC (rev 
2929)
@@ -1761,16 +1761,14 @@
  *Returns the MIME type passing its extension.
  *Returns zero if the file is registered.
  */
-MimeRecord* Http::getMIME(string &filename)
+MimeRecord* Http::getMIME (string &filename)
 {
-  string ext;
-  FilesUtility::getFileExt(ext, filename);
-
-  if(staticHttp.allowVhostMime && td->connection->host->isMIME() )
+  if(staticHttp.allowVhostMime && td->connection->host->isMIME () )
   {
-    return td->connection->host->getMIME()->getMIME(ext);
+    return td->connection->host->getMIME ()->getMIME (filename);
   }
-  return Server::getInstance()->getMimeManager()->getMIME(ext);
+
+  return Server::getInstance ()->getMimeManager ()->getMIME (filename);
 }
 
 /*!






reply via email to

[Prev in Thread] Current Thread [Next in Thread]