trikko / serverino

Small and ready-to-go http server, in D
https://trikko.github.io/serverino/
MIT License
47 stars 2 forks source link

Expires header for cache control #12

Closed p0nce closed 4 months ago

p0nce commented 4 months ago

What would be the equivalent of: cgi.setResponseExpiresRelative(24 * 60 * 60);

This sets a Expires header 24h later in order to use HTTP cache.

trikko commented 4 months ago

That should be set as any other header. But it seems there's no easy/fast way to format a date in D as required by RFC 7231 Sun, 06 Nov 1994 08:49:37 GMT. Is there?

In another part of serverino I format it in this way:

            string[] mm = ["", "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
            string[] dd = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];

            SysTime gmt = c._expire.toUTC();

            string data = format("%s, %s %s %s %s:%s:%s GMT",
               dd[gmt.dayOfWeek], gmt.day, mm[gmt.month], gmt.year,
               gmt.hour, gmt.minute, gmt.second
            );

Should I add a toXXXString(SysTime d) so you can write (Clock.currTime() + 1.days).toXXXString()?

to7231String/Date toHTTPString/Date?

p0nce commented 4 months ago

In an ideal world I would just say:

output.expiresHeader(Duration duration)

and it would do it relatively. (with a nice identifier of course, not especially expiresHeader)

trikko commented 4 months ago

Yes I know but I don't like the idea to wrap every single header. I have to keep the whole thing light.

I know it's an exception, but then exceptions became a (bad) pratice...

trikko commented 4 months ago

(anyway cache is not always relative, f.e. "sorry, the website will be online from Jan 1st")

p0nce commented 4 months ago

Ah so basically a case of "I haven't found the snippet of code to do that", it doesn't need to be a code change. I felt the same for MIME type but ultimately yours to decide.

trikko commented 4 months ago

I don't follow you. What do you mean?

trikko commented 4 months ago

Ok I have an idea. RFC7231 says that every header must use that format for dates. So I will overload the addHeader() function adding: addHeader(string, SysTime) and addHeader(string, Duration)

So you can write:

output.addHeader("expire", 3.days);

It should be a good fix, isn't it?

trikko commented 4 months ago

Hey @p0nce are you still testing serverino? Just to know if wait for more fixes (hope not) or make a new release...

p0nce commented 4 months ago

I'm using it successfully for this website, I'm not in need of anything in particular :) Very nice lib

trikko commented 4 months ago

🤩

Nice! I think you'd like the Severino/Parserino integration for this kind of websites.