00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 module mango.servlet.ServletContext;
00037
00038 private import mango.log.Logger;
00039
00040 private import mango.utils.Text;
00041
00042 private import mango.io.FileStyle,
00043 mango.io.FileConduit;
00044
00045 private import mango.servlet.Servlet;
00046
00047 private import mango.http.utils.Dictionary;
00048
00049
00050
00051
00052
00053
00054
00055
00056 class ServletContext
00057 {
00058 private char[] name,
00059 basePath;
00060 private MutableDictionary attributes,
00061 configuration;
00062
00063 private static Logger logger;
00064
00065 private static ServletException irp;
00066
00067 private static MutableDictionary mimeMap;
00068
00069 private static const char[] ServerIdentity = "Mango.Servlet/Beta 9";
00070
00071
00072
00073 private struct MimeMap
00074 {
00075 char[] ext,
00076 mime;
00077 }
00078
00079
00080 private static const MimeMap list[] =
00081 [
00082 {"a", "application/octet-stream"},
00083 {"ai", "application/postscript"},
00084 {"aif", "audio/x-aiff"},
00085 {"aifc", "audio/x-aiff"},
00086 {"aiff", "audio/x-aiff"},
00087 {"arc", "application/octet-stream"},
00088 {"au", "audio/basic"},
00089 {"avi", "application/x-troff-msvideo"},
00090 {"bcpio", "application/x-bcpio"},
00091 {"bin", "application/octet-stream"},
00092 {"bmp", "image/bmp"},
00093 {"c", "text/plain"},
00094 {"c++", "text/plain"},
00095 {"cc", "text/plain"},
00096 {"cdf", "application/x-netcdf"},
00097 {"cpio", "application/x-cpio"},
00098 {"d", "text/plain"},
00099 {"djv", "image/x-djvu"},
00100 {"djvu", "image/x-djvu"},
00101 {"dump", "application/octet-stream"},
00102 {"dvi", "application/x-dvi"},
00103 {"eps", "application/postscript"},
00104 {"etx", "text/x-setext"},
00105 {"exe", "application/octet-stream"},
00106 {"gif", "image/gif"},
00107 {"gtar", "application/x-gtar"},
00108 {"gz", "application/octet-stream"},
00109 {"h", "text/plain"},
00110 {"hdf", "application/x-hdf"},
00111 {"hqx", "application/octet-stream"},
00112 {"htm", "text/html"},
00113 {"html", "text/html"},
00114 {"iw4", "image/x-iw44"},
00115 {"iw44", "image/x-iw44"},
00116 {"ief", "image/ief"},
00117 {"java", "text/plain"},
00118 {"jfif", "image/jpeg"},
00119 {"jfif-tbnl", "image/jpeg"},
00120 {"jpe", "image/jpeg"},
00121 {"jpeg", "image/jpeg"},
00122 {"jpg", "image/jpeg"},
00123 {"latex", "application/x-latex"},
00124 {"man", "application/x-troff-man"},
00125 {"me", "application/x-troff-me"},
00126 {"mime", "message/rfc822"},
00127 {"mov", "video/quicktime"},
00128 {"movie", "video/x-sgi-movie"},
00129 {"mpe", "video/mpeg"},
00130 {"mpeg", "video/mpeg"},
00131 {"mpg", "video/mpeg"},
00132 {"ms", "application/x-troff-ms"},
00133 {"mv", "video/x-sgi-movie"},
00134 {"nc", "application/x-netcdf"},
00135 {"o", "application/octet-stream"},
00136 {"oda", "application/oda"},
00137 {"pbm", "image/x-portable-bitmap"},
00138 {"pdf", "application/pdf"},
00139 {"pgm", "image/x-portable-graymap"},
00140 {"pl", "text/plain"},
00141 {"png", "image/png"},
00142 {"pnm", "image/x-portable-anymap"},
00143 {"ppm", "image/x-portable-pixmap"},
00144 {"ps", "application/postscript"},
00145 {"qt", "video/quicktime"},
00146 {"ras", "image/x-cmu-rast"},
00147 {"rgb", "image/x-rgb"},
00148 {"roff", "application/x-troff"},
00149 {"rtf", "application/rtf"},
00150 {"rtx", "application/rtf"},
00151 {"saveme", "application/octet-stream"},
00152 {"sh", "application/x-shar"},
00153 {"shar", "application/x-shar"},
00154 {"snd", "audio/basic"},
00155 {"src", "application/x-wais-source"},
00156 {"sv4cpio", "application/x-sv4cpio"},
00157 {"sv4crc", "application/x-sv4crc"},
00158 {"t", "application/x-troff"},
00159 {"tar", "application/x-tar"},
00160 {"tex", "application/x-tex"},
00161 {"texi", "application/x-texinfo"},
00162 {"texinfo", "application/x-texinfo"},
00163 {"text", "text/plain"},
00164 {"tif", "image/tiff"},
00165 {"tiff", "image/tiff"},
00166 {"tr", "application/x-troff"},
00167 {"tsv", "text/tab-separated-values"},
00168 {"txt", "text/plain"},
00169 {"ustar", "application/x-ustar"},
00170 {"uu", "application/octet-stream"},
00171 {"wav", "audio/x-wav"},
00172 {"wsrc", "application/x-wais-source"},
00173 {"xbm", "image/x-xbitmap"},
00174 {"xpm", "image/x-xpixmap"},
00175 {"xwd", "image/x-xwindowdump"},
00176 {"z", "application/octet-stream"},
00177 {"zip", "application/zip"},
00178 ];
00179
00180
00181
00182
00183
00184
00185
00186 static this ()
00187 {
00188 irp = new ServletException ("Invalid resource path");
00189
00190 mimeMap = new MutableDictionary;
00191
00192
00193 foreach (MimeMap mm; list)
00194 mimeMap.put (mm.ext, mm.mime);
00195 mimeMap.optimize();
00196
00197
00198 logger = Logger.getLogger ("mango.servlet.Context");
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208 this (char[] name)
00209 {
00210 this (name, ".");
00211 }
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 this (char[] name, char[] basePath)
00222 in {
00223 assert (name !== null);
00224 assert (basePath !== null);
00225 }
00226 body
00227 {
00228 this.name = name;
00229 this.basePath = basePath;
00230
00231 if (name.length > 0)
00232 if (name[0] != '/' || name[name.length-1] == '/')
00233 throw new ServletException ("Invalid context specification");
00234
00235 if (basePath.length > 0)
00236 if (basePath[basePath.length-1] == '/')
00237 throw new ServletException ("Invalid path specification");
00238
00239 attributes = new MutableDictionary;
00240 configuration = new MutableDictionary;
00241 }
00242
00243
00244
00245
00246
00247
00248
00249 char[] getName ()
00250 {
00251 return name;
00252 }
00253
00254
00255
00256
00257
00258
00259
00260 synchronized Dictionary getAttributes ()
00261 {
00262 return attributes;
00263 }
00264
00265
00266
00267
00268
00269
00270
00271 synchronized Dictionary getConfiguration ()
00272 {
00273 return configuration;
00274 }
00275
00276
00277
00278
00279
00280
00281
00282
00283 protected synchronized ServletContext setAttributes (MutableDictionary other)
00284 {
00285 attributes = other;
00286 return this;
00287 }
00288
00289
00290
00291
00292
00293
00294
00295
00296 protected synchronized ServletContext setConfiguration (MutableDictionary other)
00297 {
00298 configuration = other;
00299 return this;
00300 }
00301
00302
00303
00304
00305
00306
00307
00308 int getMajorVersion ()
00309 {
00310 return 1;
00311 }
00312
00313
00314
00315
00316
00317
00318
00319 int getMinorVersion ()
00320 {
00321 return 0;
00322 }
00323
00324
00325
00326
00327
00328
00329
00330
00331 char[] getMimeType (char[] ext)
00332 {
00333 return mimeMap.get (ext);
00334 }
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 FileConduit getResourceAsFile (char[] path)
00347 {
00348 checkPath (path);
00349 return new FileConduit (basePath~path, FileStyle.ReadExisting);
00350 }
00351
00352
00353
00354
00355
00356
00357
00358 ServletContext log (char[] msg)
00359 {
00360 logger.info (msg);
00361 return this;
00362 }
00363
00364
00365
00366
00367
00368
00369
00370 ServletContext log (char[] msg, Object error)
00371 {
00372 logger.error (msg ~ error.toString());
00373 return this;
00374 }
00375
00376
00377
00378
00379
00380
00381
00382 char[] getServerInfo ()
00383 {
00384 return ServerIdentity;
00385 }
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395 ServletContext checkPath (char[] path)
00396 {
00397 if (path.length == 0)
00398 throw irp;
00399
00400 char c = path [path.length-1];
00401 if (Text.indexOf (path, "..") >= 0 ||
00402 c == '/' ||
00403 c == '\\' ||
00404 c == '.')
00405 throw irp;
00406
00407 return this;
00408 }
00409 }