ultimatepp/uppsrc/Skylark/Static.icpp
cxl 18b49a8654 .cosmetics
git-svn-id: svn://ultimatepp.org/upp/trunk@5453 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2012-10-23 07:36:40 +00:00

71 lines
1.6 KiB
Text

#include "Skylark.h"
namespace Upp {
String GetFileTimeText(const String& path)
{
return IntStr64(FileGetTime(path).Get());
}
Value CachedStatic(const Vector<Value>& arg, const Renderer *)
{
String file = arg[0];
if(SkylarkApp::Config().caching != 2)
return "/" + SkylarkApp::Config().static_dir + "/" + file;
String path = GetFileOnPath(file, SkylarkApp::Config().path, false);
if(path.GetCount())
return SkylarkApp::Config().static_dir + "/ver" + GetFileTimeText(path) + "/" + file;
return Null;
}
INITBLOCK {
Compiler::Register("CachedStatic", CachedStatic);
};
SKYLARK(ServeStaticPage, "static/**")
{
String file;
for(int i = 0; i < http.GetParamCount(); i++) {
if(i)
file << '/';
file << http[i];
}
if(SkylarkApp::Config().caching == 2 && file.StartsWith("ver")) {
int q = file.Find('/');
if(q >= 0)
file = file.Mid(q + 1);
}
String path = GetFileOnPath(file, SkylarkApp::Config().path, false);
if(!path.GetCount()) {
http.Response(404, "Not found");
return;
}
String ext = ToLower(GetFileExt(file));
String type = "text";
if(ext == ".css")
type = "text/css";
else
if(ext == ".js")
type = "text/javascript";
else
if(ext == ".png" || ext == ".jpg" || ext == ".gif")
type = "image/" + ext.Mid(1);
if(SkylarkApp::Config().caching > 0) {
String tag = GetFileTimeText(path);
http.SetHeader("Cache-Control", "public, max-age=2592000");
http.SetHeader("ETag", tag);
if(http.GetHeader("if-none-match") == tag){
http.Response(304, "Not Modified");
return;
}
}
http.Content(type, LoadFile(path));
}
};