From 729a17340a556193d33c150bbc1236eae085916a Mon Sep 17 00:00:00 2001 From: randomdan Date: Tue, 24 Apr 2012 19:27:37 +0000 Subject: Include content length when serving static content --- project2/cgi/cgiAppEngine.cpp | 1 + project2/common/transform.h | 1 + project2/files/presenterCache.cpp | 6 ++++++ 3 files changed, 8 insertions(+) diff --git a/project2/cgi/cgiAppEngine.cpp b/project2/cgi/cgiAppEngine.cpp index c7ee3e6..ea50627 100644 --- a/project2/cgi/cgiAppEngine.cpp +++ b/project2/cgi/cgiAppEngine.cpp @@ -67,6 +67,7 @@ class StaticToCgiResult : public TransformImpl { public: void transform(const StaticContent * sc, CgiResult * cr) const { cr->header->addHeader("Content-Type", Glib::ustring::compose("%1; charset=%2", sc->getContentType(), sc->getEncoding())); + cr->header->addHeader("Content-Length", Glib::ustring::compose("%1", sc->getSizeInBytes())); cr->header->render(cr->stream); sc->writeTo(cr->stream); } diff --git a/project2/common/transform.h b/project2/common/transform.h index 2355195..b7a724a 100644 --- a/project2/common/transform.h +++ b/project2/common/transform.h @@ -81,6 +81,7 @@ class StaticContent { public: virtual Glib::ustring getContentType() const = 0; virtual Glib::ustring getEncoding() const = 0; + virtual size_t getSizeInBytes() const = 0; virtual void writeTo(std::ostream &) const = 0; }; diff --git a/project2/files/presenterCache.cpp b/project2/files/presenterCache.cpp index 8d189b5..8838568 100644 --- a/project2/files/presenterCache.cpp +++ b/project2/files/presenterCache.cpp @@ -77,6 +77,12 @@ class FilePresenterCache : public PresenterCache, public StaticContent, public S buf[safesys(-1, fgetxattr(readcachefd, "user.content-type", buf, sizeof(buf)))] = 0; return buf; } + size_t getSizeInBytes() const + { + struct stat st; + safesys(-1, fstat(readcachefd, &st)); + return st.st_size; + } void writeTo(std::ostream & o) const { safesys(-1, ::flock(readcachefd, LOCK_SH)); -- cgit v1.2.3