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