(file) Return to WebConfig.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / WebServer

  1 lawrence.luo 1.1.2.1 //%LICENSE////////////////////////////////////////////////////////////////
  2                      //
  3                      // Licensed to The Open Group (TOG) under one or more contributor license
  4                      // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  5                      // this work for additional information regarding copyright ownership.
  6                      // Each contributor licenses this file to you under the OpenPegasus Open
  7                      // Source License; you may not use this file except in compliance with the
  8                      // License.
  9                      //
 10                      // Permission is hereby granted, free of charge, to any person obtaining a
 11                      // copy of this software and associated documentation files (the "Software"),
 12                      // to deal in the Software without restriction, including without limitation
 13                      // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 14                      // and/or sell copies of the Software, and to permit persons to whom the
 15                      // Software is furnished to do so, subject to the following conditions:
 16                      //
 17                      // The above copyright notice and this permission notice shall be included
 18                      // in all copies or substantial portions of the Software.
 19                      //
 20                      // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21                      // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22 lawrence.luo 1.1.2.1 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 23                      // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 24                      // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 25                      // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 26                      // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27                      //
 28                      //////////////////////////////////////////////////////////////////////////
 29                      //
 30                      //%/////////////////////////////////////////////////////////////////////////////
 31                      
 32                      
 33                      #include <fstream>
 34                      #include <iostream>
 35                      
 36                      #include <Pegasus/Common/FileSystem.h>
 37                      #include <Pegasus/Common/HashTable.h>
 38                      #include <Pegasus/Common/Tracer.h>
 39                      #include <Pegasus/Config/ConfigManager.h>
 40                      #include <Pegasus/WebServer/WebConfig.h>
 41                      
 42                      
 43 lawrence.luo 1.1.2.1 PEGASUS_USING_STD;
 44                      PEGASUS_NAMESPACE_BEGIN
 45                      
 46                      
 47                      const String WebConfig::PROPERTY_WEB_ROOT = "webRoot";
 48                      const String WebConfig::PROPERTY_INDEX_FILE = "indexFile";
 49                      const String WebConfig::PROPERTY_MIMETYPES_FILE = "mimeTypesFile";
 50                      
 51                      
 52                      WebConfig::WebConfig()
 53                      {
 54                          _loadConfig();
 55                      }
 56                      
 57                      WebConfig::~WebConfig()
 58                      {
 59                      }
 60                      
 61 dl.meetei    1.1.2.3 String WebConfig::getWebRoot() const
 62 lawrence.luo 1.1.2.1 {
 63                          return _webRoot;
 64                      }
 65                      
 66 dl.meetei    1.1.2.3 String WebConfig::getIndexFile() const
 67 lawrence.luo 1.1.2.1 {
 68                          return _indexFile;
 69                      }
 70                      
 71 dl.meetei    1.1.2.3 WebConfig::MimeTypes WebConfig::getMimeTypes() const
 72 lawrence.luo 1.1.2.1 {
 73                          return _mimeTypes;
 74                      }
 75                      
 76                      void WebConfig::reload()
 77                      {
 78                          _loadConfig();
 79                      }
 80                      
 81                      void WebConfig::_loadConfig()
 82                      {
 83                          PEG_METHOD_ENTER(TRC_WEBSERVER, "WebConfig::_loadConfig()");
 84                      
 85                          ConfigManager* _configManager = ConfigManager::getInstance();
 86                          // get values from current config
 87                          _webRoot = _configManager->getCurrentValue(PROPERTY_WEB_ROOT);
 88                          _indexFile = _configManager->getCurrentValue(PROPERTY_INDEX_FILE);
 89                          String mimeTypesFile = _configManager->getCurrentValue(
 90                                                                   PROPERTY_MIMETYPES_FILE);
 91                      
 92 dl.meetei    1.1.2.3     PEG_TRACE((TRC_WEBSERVER, Tracer::LEVEL4,
 93 lawrence.luo 1.1.2.1             "WebConfig::_loadConfig() - "
 94                                      "ConfigManager returned\nwebRoot='%s',\nindexFile='%s',\n"
 95                                      "mimeTypesFile='%s'",
 96                                      (const char*)_webRoot.getCString(),
 97                                      (const char*)_indexFile.getCString(),
 98                                      (const char*)mimeTypesFile.getCString()));
 99                      
100                          // load mime types map
101                          _loadMimeTypes(mimeTypesFile);
102                      
103                          PEG_METHOD_EXIT();
104                      }
105                      
106                      void WebConfig::_loadMimeTypes(String& mimeTypesFile)
107                      {
108                          PEG_METHOD_ENTER(TRC_WEBSERVER,
109                                  "WebConfig::_loadMimeTypes(String& mimeTypesFile)");
110                      
111                          // ensure an empty list
112                          _mimeTypes.clear();
113                      
114 lawrence.luo 1.1.2.1 
115                      
116                          const char delimiter = '=';
117                          ifstream fileStream(mimeTypesFile.getCString());
118                      
119                          // mimetypes file exists ?
120                          if (!fileStream
121                                  || !fileStream.is_open())
122                          {// no, use defaults and abort
123                              // set defaults
124                              _mimeTypes.insert("html", "text/html");
125                              _mimeTypes.insert("htm", "text/html");
126                              _mimeTypes.insert("css", "text/css");
127                              _mimeTypes.insert("txt", "text/plain");
128                              _mimeTypes.insert("js", "text/javascript");
129                              _mimeTypes.insert("png", "image/png");
130                              _mimeTypes.insert("gif", "image/gif");
131                              _mimeTypes.insert("jpg", "image/jpeg");
132                              // write trace
133                              PEG_TRACE_CSTRING(TRC_WEBSERVER, Tracer::LEVEL4,
134                                         "WebConfig::_loadMimeTypes() - "
135 lawrence.luo 1.1.2.1                        "Failed to read mime-types file, using defaults.");
136                              // trace
137                              String defaultMimeTypes = "";
138                              for (MimeTypes::Iterator i = _mimeTypes.start(); i; i++)
139                              {
140                                  defaultMimeTypes.append(i.key() + ":" + i.value() + "\n");
141                              }
142                              defaultMimeTypes.append("\n");
143                              PEG_TRACE((TRC_WEBSERVER, Tracer::LEVEL4,
144                                       "WebConfig::_loadMimeTypes() - "
145                                       "Failed to read mime-types file, using defaults."
146                                       " Default MIME-types are: \n%s",
147                                       (const char*)defaultMimeTypes.getCString()));
148                              return;
149                          }
150                          PEG_TRACE((TRC_WEBSERVER, Tracer::LEVEL4,
151                                             "WebConfig::_loadMimeTypes() - "
152                                                 "Reading mime-types file '%s'",
153                                             (const char*)mimeTypesFile.getCString()));
154                      
155                          String line;
156 lawrence.luo 1.1.2.1     while (!fileStream.eof())
157                          {
158                              // read line
159                              GetLine(fileStream, line);
160                              // is it a comment line ?
161                              if (!line.compare("#", line, 1))
162                              {
163                                  continue;
164                              }
165                              // find delimiter
166                              Uint32 delimPos = line.find(delimiter);
167                              if (PEG_NOT_FOUND == delimPos)
168                              {// delimiter not found
169                                  continue;
170                              }
171                              // extract key/value
172                              String key = line.subString(0, delimPos);
173                              String value = line.subString(delimPos+1);
174                              // apply config value
175                              _mimeTypes.insert(key, value);
176                          }
177 lawrence.luo 1.1.2.1 
178                          String definedMimeTypes = "";
179                          for (MimeTypes::Iterator i = _mimeTypes.start(); i; i++)
180                          {
181                              definedMimeTypes.append(i.key() + ":" + i.value() + "\n");
182                          }
183                          definedMimeTypes.append("\n");
184                      
185                          PEG_TRACE((TRC_WEBSERVER, Tracer::LEVEL4,
186                                  "WebConfig::_loadMimeTypes() - "
187                                      "Loaded MIME-types are: \n%s",
188                                  (const char*)definedMimeTypes.getCString()));
189                      
190                          PEG_METHOD_EXIT();
191                      }
192                      
193                      /* END */
194                      
195                      PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2