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