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 dl.meetei 1.1.2.5 _webRoot = ConfigManager::getHomedPath(
88 _configManager->getCurrentValue(PROPERTY_WEB_ROOT));
89
|
90 lawrence.luo 1.1.2.1 _indexFile = _configManager->getCurrentValue(PROPERTY_INDEX_FILE);
|
91 dl.meetei 1.1.2.5
92 String mimeTypesFile = ConfigManager::getHomedPath(
93 _configManager->getCurrentValue(
94 PROPERTY_MIMETYPES_FILE));
|
95 lawrence.luo 1.1.2.1
|
96 dl.meetei 1.1.2.3 PEG_TRACE((TRC_WEBSERVER, Tracer::LEVEL4,
|
97 lawrence.luo 1.1.2.1 "WebConfig::_loadConfig() - "
98 "ConfigManager returned\nwebRoot='%s',\nindexFile='%s',\n"
99 "mimeTypesFile='%s'",
100 (const char*)_webRoot.getCString(),
101 (const char*)_indexFile.getCString(),
102 (const char*)mimeTypesFile.getCString()));
103
104 // load mime types map
105 _loadMimeTypes(mimeTypesFile);
106
107 PEG_METHOD_EXIT();
108 }
109
110 void WebConfig::_loadMimeTypes(String& mimeTypesFile)
111 {
112 PEG_METHOD_ENTER(TRC_WEBSERVER,
113 "WebConfig::_loadMimeTypes(String& mimeTypesFile)");
114
115 // ensure an empty list
116 _mimeTypes.clear();
117
118 lawrence.luo 1.1.2.1
119
120 const char delimiter = '=';
121 ifstream fileStream(mimeTypesFile.getCString());
122
123 // mimetypes file exists ?
|
124 dl.meetei 1.1.2.4 if (!fileStream || !fileStream.is_open())
|
125 lawrence.luo 1.1.2.1 {// no, use defaults and abort
126 // set defaults
127 _mimeTypes.insert("html", "text/html");
128 _mimeTypes.insert("htm", "text/html");
129 _mimeTypes.insert("css", "text/css");
130 _mimeTypes.insert("txt", "text/plain");
131 _mimeTypes.insert("js", "text/javascript");
132 _mimeTypes.insert("png", "image/png");
133 _mimeTypes.insert("gif", "image/gif");
134 _mimeTypes.insert("jpg", "image/jpeg");
135 // write trace
136 PEG_TRACE_CSTRING(TRC_WEBSERVER, Tracer::LEVEL4,
137 "WebConfig::_loadMimeTypes() - "
138 "Failed to read mime-types file, using defaults.");
139 // trace
140 String defaultMimeTypes = "";
141 for (MimeTypes::Iterator i = _mimeTypes.start(); i; i++)
142 {
143 defaultMimeTypes.append(i.key() + ":" + i.value() + "\n");
144 }
145 defaultMimeTypes.append("\n");
146 lawrence.luo 1.1.2.1 PEG_TRACE((TRC_WEBSERVER, Tracer::LEVEL4,
147 "WebConfig::_loadMimeTypes() - "
148 "Failed to read mime-types file, using defaults."
149 " Default MIME-types are: \n%s",
150 (const char*)defaultMimeTypes.getCString()));
151 return;
152 }
153 PEG_TRACE((TRC_WEBSERVER, Tracer::LEVEL4,
154 "WebConfig::_loadMimeTypes() - "
155 "Reading mime-types file '%s'",
156 (const char*)mimeTypesFile.getCString()));
157
158 String line;
159 while (!fileStream.eof())
160 {
161 // read line
162 GetLine(fileStream, line);
163 // is it a comment line ?
164 if (!line.compare("#", line, 1))
165 {
166 continue;
167 lawrence.luo 1.1.2.1 }
168 // find delimiter
169 Uint32 delimPos = line.find(delimiter);
170 if (PEG_NOT_FOUND == delimPos)
171 {// delimiter not found
172 continue;
173 }
174 // extract key/value
175 String key = line.subString(0, delimPos);
176 String value = line.subString(delimPos+1);
177 // apply config value
178 _mimeTypes.insert(key, value);
179 }
180
181 String definedMimeTypes = "";
182 for (MimeTypes::Iterator i = _mimeTypes.start(); i; i++)
183 {
184 definedMimeTypes.append(i.key() + ":" + i.value() + "\n");
185 }
186 definedMimeTypes.append("\n");
187
188 lawrence.luo 1.1.2.1 PEG_TRACE((TRC_WEBSERVER, Tracer::LEVEL4,
189 "WebConfig::_loadMimeTypes() - "
190 "Loaded MIME-types are: \n%s",
191 (const char*)definedMimeTypes.getCString()));
192
193 PEG_METHOD_EXIT();
194 }
195
196 /* END */
197
198 PEGASUS_NAMESPACE_END
|