myserver-commit
[Top][All Lists]
Advanced

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

[myserver-commit] [2902] Added tests for the `MimeManager' class and upd


From: Giuseppe Scrivano
Subject: [myserver-commit] [2902] Added tests for the `MimeManager' class and updated documentation.
Date: Sat, 25 Oct 2008 10:21:15 +0000

Revision: 2902
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2902
Author:   gscrivano
Date:     2008-10-25 10:21:13 +0000 (Sat, 25 Oct 2008)

Log Message:
-----------
Added tests for the `MimeManager' class and updated documentation.

Modified Paths:
--------------
    trunk/myserver/documentation/mime_types.texi
    trunk/myserver/include/conf/mime/mime_manager.h
    trunk/myserver/src/conf/mime/mime_manager.cpp
    trunk/myserver/tests/Makefile.am

Added Paths:
-----------
    trunk/myserver/tests/test_mime_manager.cpp

Modified: trunk/myserver/documentation/mime_types.texi
===================================================================
--- trunk/myserver/documentation/mime_types.texi        2008-10-24 20:59:20 UTC 
(rev 2901)
+++ trunk/myserver/documentation/mime_types.texi        2008-10-25 10:21:13 UTC 
(rev 2902)
@@ -3,74 +3,47 @@
 @file{MIMEtypes.xml}. The following is an example of this file: 
 
 @example
-<MIMETYPES>
+<?xml version ="1.0"?>
+<MIME mime="text/html" handler="SEND" param="">
+<EXTENSION value="html"/>
+</MIME>
 
-  <MIMETYPE defaultAction="DENY">
-    <EXT>htm</EXT>
-    <MIME>text/html</MIME>
-    <CMD>SEND</CMD>
-    <ACTION name="Host" value="localhost">ALLOW</ACTION>
-    <FILTER>gzip</FILTER>
-    <MANAGER>NONE</MANAGER>
-  </MIMETYPE>
-
-</MIMETYPES>
+<MIME mime="text/html" handler="RUNCGI" param="/usr/bin/perl">
+<FILTER value="gzip"/>
+<EXTENSION value="pl"/>
+</MIME>
 @end example
 
 @itemize
 Each block describes a different MIME type.
 
address@hidden EXT
-is the type of file
address@hidden mime
+the MIME attribute specifies the MIME name.
 
address@hidden MIME
-is the Mime type.
address@hidden extension
+the MIME type will be used by any file with this extension.
 
address@hidden CMD
-is the way to send the file to the client.
address@hidden itemize
address@hidden filter
+this directive registers a filter to use with MIME type.
 
address@hidden @code{<ACTION name="Host" value="localhost">ALLOW</ACTION>}
-will allow access to the desired MIME Type if the Host is
-"localhost".
-In the line @code{<MIMETYPE defaultAction="DENY">} we declare that 
-the MIME type will DENY access from any host we don't set in the
-proper ACTION tag (so, it will have to exist in the
-configuration). Also, the property defaultAction is optional (when
-it's omitted, it's set to "ALLOW"). In the above example, HTML files
-will be accessed only through the hostname "localhost".
address@hidden handler
+specifies the way the file is handled by the server.
 
-The tag @code{<ACTION>} is optional, unless you specify "DENY" for
-your defaultAction in your MIME Type.
address@hidden param
+specifies additional params for the handler.
 
address@hidden<FILTER>} defines a filter for that MIME type. The data will
-pass through the filter before being sent to the user. In the example
-above, the data would be sent gzipped to the user. It means Gzip works
-as a filter in MyServer. you can define Gzip to automatically run for
-every file based in its size, setting the @code{<GZIP_THRESHOLD>}, in
address@hidden file. For obvious reasons, data will be sent
-gzipped only if the client supports gzip - otherwise he'd receive a
-lot of weird characters instead of the site requested.
address@hidden itemize
 
-In general, filters can also be applied to dynamic data - like CGI,
-ISAPI, FastCGI - but they need to be filters that don't modify the
-contents of the page to be shown to the user.
address@hidden<FILTER>} defines a filter for the MIME type.  The data will
+pass through the filter before being sent to the user.  
+In the example above, the data would be sent gzipped to the user.
+It is possible to specify multiple filters for the same MIME type,
+they will be applied in the specified order.
 
address@hidden<MANAGER>} completes the @code{<CMD>} command specifying, if
-necessary for the command, an external application to use.
-For example using the RUNCGI command, it will be necessary to specify
-an external process to execute the CGI command, MANAGER under
-GNU/Linux will be something like this:
+The same MIME type can have several extensions, you will need to
+specify a different @code{<EXTENSION>} element for each extension.
 
address@hidden
-<MANAGER>/usr/bin/php-cgi</MANAGER>
-or under Windows:
-<MANAGER>c:\php\php-cgi.exe</MANAGER>
address@hidden example
-
-
-The following values for sending the file to the client are valid in
-the @code{<CMD>} section:
+These are valid HTTP handlers:
 @enumerate
 @item SEND
 Send the file statically as is.  This command is used for files

Modified: trunk/myserver/include/conf/mime/mime_manager.h
===================================================================
--- trunk/myserver/include/conf/mime/mime_manager.h     2008-10-24 20:59:20 UTC 
(rev 2901)
+++ trunk/myserver/include/conf/mime/mime_manager.h     2008-10-25 10:21:13 UTC 
(rev 2902)
@@ -49,46 +49,47 @@
 struct MimeRecord
 {
        list<string> filters;
-       string extension;
+       list<string> extensions;
        string mimeType;
        string cmdName;
        string cgiManager;
-       MimeRecord();
-       MimeRecord(MimeRecord&);
-       int addFilter(const char*, bool acceptDuplicate = true);
-       ~MimeRecord();
-       void clear();
+       MimeRecord ();
+       MimeRecord (MimeRecord&);
+       int addFilter (const char*, bool acceptDuplicate = true);
+       ~MimeRecord ();
+       void clear ();
 };
 
 class MimeManager
 {
 public:
-       MimeManager();
-  ~MimeManager();
-       u_long getNumMIMELoaded();
+       MimeManager ();
+  ~MimeManager ();
+       u_long getNumMIMELoaded ();
 
-       int loadXML(const char *filename);
-       int loadXML(string &filename)
-    {return loadXML(filename.c_str());}
+  u_long loadXML (XmlParser* parser);
+       u_long loadXML (const char *filename);
+       u_long loadXML (string &filename)
+    {return loadXML (filename.c_str ());}
 
-       MimeRecord* getMIME(const char* ext);
-  MimeRecord* getMIME(string const &ext);
+       MimeRecord* getMIME (const char *ext);
+  MimeRecord* getMIME (string const &ext);
 
-  bool isLoaded();
-       void clean();
+  bool isLoaded ();
+       void clean ();
+       int addRecord (MimeRecord *record);
 
 protected:
   MimeRecord *readRecord (xmlNodePtr node);
-       const char *getFilename();
-       int addRecord(MimeRecord *record);
-       void removeAllRecords();
+       const char *getFilename ();
+       void clearRecords ();
 private:
   bool loaded;
   HashMap<string, int> extIndex;
   vector <MimeRecord*> records;
 
        u_long numMimeTypesLoaded;
-       string *filename;
+       string filename;
 };
 
 #endif 

Modified: trunk/myserver/src/conf/mime/mime_manager.cpp
===================================================================
--- trunk/myserver/src/conf/mime/mime_manager.cpp       2008-10-24 20:59:20 UTC 
(rev 2901)
+++ trunk/myserver/src/conf/mime/mime_manager.cpp       2008-10-25 10:21:13 UTC 
(rev 2902)
@@ -31,28 +31,28 @@
 
 using namespace std;
 
-MimeRecord::MimeRecord()
+MimeRecord::MimeRecord ()
 {
-  filters.clear();
-  extension.assign(""); 
-  mimeType.assign("");
-  cgiManager.assign("");
-  cmdName.assign("");
+  filters.clear ();
+  extensions.clear ();
+  mimeType.assign ("");
+  cgiManager.assign ("");
+  cmdName.assign ("");
 }
 
 /*!
  *Destroy the object.
  */
-MimeRecord::~MimeRecord()
+MimeRecord::~MimeRecord ()
 {
-  clear();
+  clear ();
 }
 
 /*!
  *Add a filter to the list of filters to apply on this MIME type.
  *Return zero if the filters was not added.
  */
-int MimeRecord::addFilter(const char* n, bool acceptDuplicate) 
+int MimeRecord::addFilter (const char* n, bool acceptDuplicate) 
 {
   if(!acceptDuplicate)
   {
@@ -70,42 +70,49 @@
 /*!
  *Copy constructor.
  */
-MimeRecord::MimeRecord(MimeRecord& m)
+MimeRecord::MimeRecord (MimeRecord& m)
 {
-  list<string>::iterator i = m.filters.begin();
+  list<string>::iterator i = m.filters.begin ();
 
-  filters.clear(); 
+  filters.clear (); 
 
-  for( ; i != m.filters.end(); i++)
+  for( ; i != m.filters.end (); i++)
   {
-    filters.push_back(*i);
+    filters.push_back (*i);
   }
-  extension.assign(m.extension); 
-  mimeType.assign(m.mimeType);
-  cmdName.assign(m.cmdName);
-  cgiManager.assign(m.cgiManager);
+
+  i = m.extensions.begin ();
+
+  extensions.clear (); 
+
+  for( ; i != m.extensions.end (); i++)
+  {
+    filters.push_back (*i);
+  }
+
+  mimeType.assign (m.mimeType);
+  cmdName.assign (m.cmdName);
+  cgiManager.assign (m.cgiManager);
 } 
 
 /*!
   *Clear the used memory.
   */
-void MimeRecord::clear()
+void MimeRecord::clear ()
 {
-  filters.clear();
-  extension.assign(""); 
-  mimeType.assign(""); 
-  cmdName.assign("");
-  cgiManager.assign("");
+  filters.clear ();
+  extensions.clear ();
+  mimeType.assign (""); 
+  cmdName.assign ("");
+  cgiManager.assign ("");
 }  
 
 /*!
  *Get the name of the file opened by the class.
  */
-const char *MimeManager::getFilename()
+const char *MimeManager::getFilename ()
 {
-  if(filename == 0)
-    return "";
-  return filename->c_str();
+  return filename.c_str();
 }
 
 /*!
@@ -133,22 +140,31 @@
       rc->cgiManager.assign ((const char*)attrs->children->content);
   }
 
-  for( ;lcur; lcur = lcur->next)
+  for ( ;lcur; lcur = lcur->next)
   {
-    if(lcur->name && !xmlStrcmp(lcur->name, (const xmlChar *)"EXTENSION"))
+    if (lcur->name && !xmlStrcmp(lcur->name, (const xmlChar *)"EXTENSION"))
     {
       for (attrs = lcur->properties; attrs; attrs = attrs->next)
       {
         if (!xmlStrcmp (attrs->name, (const xmlChar *)"value") && 
             attrs->children && attrs->children->content)
-          rc->extension.assign ((const char*)attrs->children->content);
+        {
+          string ext ((const char*)attrs->children->content);
+          rc->extensions.push_back (ext);
+        }
       }
     }
 
-    if(lcur->name && !xmlStrcmp(lcur->name, (const xmlChar *)"FILTER"))
+    if (lcur->name && !xmlStrcmp(lcur->name, (const xmlChar *)"FILTER"))
     {
-      if(lcur->children->content)
-        rc->addFilter((const char*)lcur->children->content);
+      for (attrs = lcur->properties; attrs; attrs = attrs->next)
+      {
+        if (!xmlStrcmp (attrs->name, (const xmlChar *)"value") && 
+            attrs->children && attrs->children->content)
+        {
+          rc->addFilter ((const char*)attrs->children->content);
+        }
+      }
     }
   }
 
@@ -159,31 +175,37 @@
  *Load the MIME types from a XML file. Returns the number of
  *MIME types loaded successfully.
  */
-int MimeManager::loadXML(const char *fn)
+u_long MimeManager::loadXML (const char *fn)
 {
   XmlParser parser;
-  xmlNodePtr node;
-  xmlDocPtr doc;
+  u_long ret = 0;
 
-  if(!fn)
-    return -1;
-
-  if(filename)
-    delete filename;
-
-  filename = new string(fn);
-
   if(parser.open (fn))
   {
     return -1;
   }
 
-  removeAllRecords();
+  filename.assign (fn);
 
-  /* The first record is not used to store information.  */
-  records.push_back (NULL);
+  ret = loadXML (&parser);
+  
+  parser.close ();
+  
+  return ret;
+}
 
-  doc = parser.getDoc();
+/*!
+ *Load the MIME types from a XML parser object. Returns the number 
+ *of MIME types loaded successfully.
+ */
+u_long MimeManager::loadXML (XmlParser* parser)
+{
+  xmlNodePtr node;
+  xmlDocPtr doc;
+
+  clearRecords ();
+
+  doc = parser->getDoc();
   node = doc->children->children;
 
   for(; node; node = node->next )
@@ -193,7 +215,6 @@
     if (rc)
       addRecord (rc);
   }
-  parser.close();
   
   /*! Store the loaded status. */
   loaded = true;
@@ -204,7 +225,7 @@
 /*!
  *Destroy the object.
  */
-MimeManager::~MimeManager()
+MimeManager::~MimeManager ()
 {
   clean();
 }
@@ -212,24 +233,21 @@
 /*!
  *Clean the memory allocated by the structure.
  */
-void MimeManager::clean()
+void MimeManager::clean ()
 {
   if(loaded)
   {
     loaded = false;
-    if(filename) 
-      delete filename;
-    filename = 0;
-    removeAllRecords();
+    filename.assign ("");
+    clearRecords ();
   }
 }
 
 /*!
  *Constructor of the class.
  */
-MimeManager::MimeManager()
+MimeManager::MimeManager ()
 {
-  filename = 0;
   loaded = false;
 }
 
@@ -241,21 +259,26 @@
 {
   u_long position = records.size ();
 
+  records.push_back (mr);
+
+  for (list<string>::iterator it = mr->extensions.begin (); it != 
mr->extensions.end (); it++)
+  {
+    string &ext = *it;
+
 #ifdef MIME_LOWER_CASE
-  transform (mr->extension.begin(), mr->extension.end(), 
mr->extension.begin(), ::tolower);
+    transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
 #endif
-  
-  records.push_back (mr);
 
-  extIndex.put (mr->extension, position);
+    extIndex.put (ext, position);
+  }
   
   return position;
 }
 
 /*!
- *Remove all records from the linked list.
+ *Remove all the stored records.
  */
-void MimeManager::removeAllRecords ()
+void MimeManager::clearRecords ()
 {
   vector <MimeRecord*>::iterator i = records.begin ();
 
@@ -272,6 +295,9 @@
   records.clear ();
 
   extIndex.clear ();
+
+  /* The first record is not used to store information.  */
+  records.push_back (NULL);
 }
 
 /*!

Modified: trunk/myserver/tests/Makefile.am
===================================================================
--- trunk/myserver/tests/Makefile.am    2008-10-24 20:59:20 UTC (rev 2901)
+++ trunk/myserver/tests/Makefile.am    2008-10-25 10:21:13 UTC (rev 2902)
@@ -2,5 +2,5 @@
 #
 
 bin_PROGRAMS = tests_suite
-tests_suite_SOURCES = main.cpp test_connection.cpp test_ftp.cpp 
test_log_manager.cpp test_mutex.cpp test_security_domain.cpp test_validator.cpp 
test_auth_domain.cpp test_connections_scheduler.cpp test_gzip.cpp 
test_log_stream_factory.cpp test_pipe.cpp test_security_manager.cpp 
test_validator_factory.cpp test_base64.cpp test_file_stream.cpp 
test_hashmap.cpp test_md5.cpp test_recursive_mutex.cpp test_semaphore.cpp 
test_xml.cpp test_cached_file_buffer.cpp test_file_stream_creator.cpp 
test_homedir.cpp test_mem_buff.cpp test_regex.cpp 
test_socket_stream_creator.cpp test_cached_file.cpp test_files_utility.cpp 
test_http_request.cpp test_http_req_security_domain.cpp test_mem_stream.cpp 
test_safetime.cpp test_thread.cpp test_cached_file_factory.cpp 
test_filter_chain.cpp test_http_response.cpp test_multicast.cpp 
test_security_cache.cpp test_security_token.cpp test_utility.cpp  
test_xml_validator.cpp
+tests_suite_SOURCES = main.cpp test_connection.cpp test_ftp.cpp 
test_log_manager.cpp test_mime_manager.cpp test_mutex.cpp 
test_security_domain.cpp test_validator.cpp test_auth_domain.cpp 
test_connections_scheduler.cpp test_gzip.cpp test_log_stream_factory.cpp 
test_pipe.cpp test_security_manager.cpp test_validator_factory.cpp 
test_base64.cpp test_file_stream.cpp test_hashmap.cpp test_md5.cpp 
test_recursive_mutex.cpp test_semaphore.cpp test_xml.cpp 
test_cached_file_buffer.cpp test_file_stream_creator.cpp test_homedir.cpp 
test_mem_buff.cpp test_regex.cpp test_socket_stream_creator.cpp 
test_cached_file.cpp test_files_utility.cpp test_http_request.cpp 
test_http_req_security_domain.cpp test_mem_stream.cpp test_safetime.cpp 
test_thread.cpp test_cached_file_factory.cpp test_filter_chain.cpp 
test_http_response.cpp test_multicast.cpp test_security_cache.cpp 
test_security_token.cpp test_utility.cpp  test_xml_validator.cpp
 tests_suite_LDADD = ../src/libmyserver.a $(CPPUNIT_LDFLAGS) $(PTHREAD_LIB) 
$(IDN_LIB) $(XNET_LIB) $(EVENT_LIB) $(DL_LIB) $(OPENSSL_LIB) $(ZLIB_LIB) 
$(XML_LIBS) $(LDFLAGS)

Added: trunk/myserver/tests/test_mime_manager.cpp
===================================================================
--- trunk/myserver/tests/test_mime_manager.cpp                          (rev 0)
+++ trunk/myserver/tests/test_mime_manager.cpp  2008-10-25 10:21:13 UTC (rev 
2902)
@@ -0,0 +1,102 @@
+/*
+ MyServer
+ Copyright (C) 2008 Free Software Foundation, Inc.
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+ 
+ This program is distributed in the hope that it will be useful, 
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+ 
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <ctype.h>
+
+#include <cppunit/CompilerOutputter.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <include/conf/mime/mime_manager.h>
+
+#include <string.h>
+
+#include <iostream>
+using namespace std;
+
+
+class TestMimeManager : public CppUnit::TestFixture
+{
+  CPPUNIT_TEST_SUITE ( TestMimeManager );
+  CPPUNIT_TEST ( testLoadXML );
+  CPPUNIT_TEST_SUITE_END ();
+
+  MimeManager *mm;
+public:
+  void setUp ()
+  {
+    mm = new MimeManager;
+  }
+
+  void tearDown ()
+  {
+    delete mm;
+  }
+
+  void testLoadXML ()
+  {
+    XmlParser *parser = getTestParser ();
+
+    CPPUNIT_ASSERT_EQUAL (mm->isLoaded(), false);
+
+    CPPUNIT_ASSERT_EQUAL (mm->loadXML (parser), 2ul);
+    CPPUNIT_ASSERT_EQUAL (mm->getNumMIMELoaded(), 2ul);
+
+    CPPUNIT_ASSERT_EQUAL (mm->isLoaded(), true);
+    
+    delete parser;
+  }
+
+  void testAddRecord ()
+  {
+    XmlParser *parser = getTestParser ();
+    MimeRecord mr;
+    mm->loadXML (parser);
+
+    CPPUNIT_ASSERT_EQUAL (mm->getNumMIMELoaded(), 2ul);
+
+    mm->addRecord (&mr);
+
+    CPPUNIT_ASSERT_EQUAL (mm->getNumMIMELoaded(), 3ul);
+    
+    delete parser;
+  }
+
+private:
+
+  //Returns a XML object with 2 MIME.
+  XmlParser *getTestParser ()
+  {
+    MemBuf mb;
+    XmlParser *parser = new XmlParser ();
+
+    const char *buffer = "<?xml version=\"1.0\"?>\n<MIMES>\n\
+       <MIME mime=\"text/html\" handler=\"SEND\" param=\"\">\n                 
\
+       <EXTENSION value=\"htm\"/>\n<EXTENSION value=\"html\"/>\n\
+       </MIME>\n<MIME mime=\"text/html\" handler=\"SEND\" param=\"\">\n\
+       <EXTENSION value=\"txt\"/>\n</MIME>\n</MIMES>\n";
+
+    mb.addBuffer (buffer, strlen (buffer));
+    parser->openMemBuf(mb);
+
+    return parser;
+  }
+};
+
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestMimeManager );






reply via email to

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