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