Change tstamp() to allow passing time_t

then use this to remove duplicate code in the last-modified-section.
This commit is contained in:
FRIGN 2014-08-11 15:37:45 +02:00
parent 54a95cd229
commit 55912e14bf

28
quark.c
View file

@ -62,7 +62,7 @@ static const char *resentry[] = {
[MODIFIED] = "Last-Modified: %s\r\n" [MODIFIED] = "Last-Modified: %s\r\n"
}; };
static char *tstamp(void); static char *tstamp(time_t t);
static int writedata(const char *buf, size_t buflen); static int writedata(const char *buf, size_t buflen);
static int writetext(const char *buf); static int writetext(const char *buf);
static void atomiclog(int fd, const char *errstr, va_list ap); static void atomiclog(int fd, const char *errstr, va_list ap);
@ -95,10 +95,11 @@ static int fd;
static Request req; static Request req;
char * char *
tstamp(void) { tstamp(time_t t) {
static char res[30]; static char res[30];
time_t t = time(NULL);
if (!t)
t = time(NULL);
strftime(res, sizeof res, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&t)); strftime(res, sizeof res, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&t));
return res; return res;
} }
@ -128,7 +129,7 @@ atomiclog(int fd, const char *errstr, va_list ap) {
/* assemble the message in buf and write it in one pass /* assemble the message in buf and write it in one pass
to avoid interleaved concurrent writes on a shared fd. */ to avoid interleaved concurrent writes on a shared fd. */
n = snprintf(buf, sizeof buf, "%s\t", tstamp()); n = snprintf(buf, sizeof buf, "%s\t", tstamp(0));
n += vsnprintf(buf + n, sizeof buf - n, errstr, ap); n += vsnprintf(buf + n, sizeof buf - n, errstr, ap);
if (n >= sizeof buf) if (n >= sizeof buf)
n = sizeof buf - 1; n = sizeof buf - 1;
@ -194,11 +195,10 @@ responsefile(void) {
char mod[30], *p; char mod[30], *p;
int r, ffd; int r, ffd;
struct stat st; struct stat st;
time_t t;
if ((r = stat(reqbuf, &st)) == -1 || (ffd = open(reqbuf, O_RDONLY)) == -1) { if ((r = stat(reqbuf, &st)) == -1 || (ffd = open(reqbuf, O_RDONLY)) == -1) {
/* file not found */ /* file not found */
if (putresentry(HEADER, HttpNotFound, tstamp()) if (putresentry(HEADER, HttpNotFound, tstamp(0))
|| putresentry(CONTENTTYPE, texthtml)) || putresentry(CONTENTTYPE, texthtml))
return; return;
status = 404; status = 404;
@ -206,9 +206,7 @@ responsefile(void) {
writetext("\r\n<html><body>"HttpNotFound"</body></html>\r\n"); writetext("\r\n<html><body>"HttpNotFound"</body></html>\r\n");
} else { } else {
/* check if modified */ /* check if modified */
t = st.st_mtim.tv_sec; if (!strcmp(reqmod, tstamp(st.st_mtim.tv_sec)) && !putresentry(HEADER, HttpNotModified, tstamp(0))) {
strftime(mod, sizeof mod, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&t));
if (!strcmp(reqmod, mod) && !putresentry(HEADER, HttpNotModified, tstamp())) {
/* not modified, we're done here*/ /* not modified, we're done here*/
status = 304; status = 304;
} else { } else {
@ -222,7 +220,7 @@ responsefile(void) {
} }
} }
/* serve file */ /* serve file */
if (putresentry(HEADER, HttpOk, tstamp()) if (putresentry(HEADER, HttpOk, tstamp(0))
|| putresentry(MODIFIED, mod) || putresentry(MODIFIED, mod)
|| putresentry(CONTENTLEN, st.st_size) || putresentry(CONTENTLEN, st.st_size)
|| putresentry(CONTENTTYPE, mimetype)) || putresentry(CONTENTTYPE, mimetype))
@ -239,7 +237,7 @@ void
responsedirdata(DIR *d) { responsedirdata(DIR *d) {
struct dirent *e; struct dirent *e;
if (putresentry(HEADER, HttpOk, tstamp()) if (putresentry(HEADER, HttpOk, tstamp(0))
|| putresentry(CONTENTTYPE, texthtml)) || putresentry(CONTENTTYPE, texthtml))
return; return;
status = 200; status = 200;
@ -271,7 +269,7 @@ responsedir(void) {
/* add directory terminator if necessary */ /* add directory terminator if necessary */
reqbuf[len] = '/'; reqbuf[len] = '/';
reqbuf[len + 1] = 0; reqbuf[len + 1] = 0;
if (putresentry(HEADER, HttpMoved, tstamp()) if (putresentry(HEADER, HttpMoved, tstamp(0))
|| putresentry(LOCATION, location, reqbuf) || putresentry(LOCATION, location, reqbuf)
|| putresentry(CONTENTTYPE, texthtml)) || putresentry(CONTENTTYPE, texthtml))
return; return;
@ -315,7 +313,7 @@ responsecgi(void) {
if (chdir(cgi_dir) == -1) if (chdir(cgi_dir) == -1)
logerrmsg("error\tchdir to cgi directory %s failed: %s\n", cgi_dir, strerror(errno)); logerrmsg("error\tchdir to cgi directory %s failed: %s\n", cgi_dir, strerror(errno));
if ((cgi = popen(cgi_script, "r"))) { if ((cgi = popen(cgi_script, "r"))) {
if (putresentry(HEADER, HttpOk, tstamp())) if (putresentry(HEADER, HttpOk, tstamp(0)))
return; return;
status = 200; status = 200;
while ((r = fread(resbuf, 1, MAXBUFLEN, cgi)) > 0) { while ((r = fread(resbuf, 1, MAXBUFLEN, cgi)) > 0) {
@ -327,7 +325,7 @@ responsecgi(void) {
pclose(cgi); pclose(cgi);
} else { } else {
logerrmsg("error\t%s requests %s, but cannot run cgi script %s\n", host, cgi_script, reqbuf); logerrmsg("error\t%s requests %s, but cannot run cgi script %s\n", host, cgi_script, reqbuf);
if (putresentry(HEADER, HttpNotFound, tstamp()) if (putresentry(HEADER, HttpNotFound, tstamp(0))
|| putresentry(CONTENTTYPE, texthtml)) || putresentry(CONTENTTYPE, texthtml))
return; return;
status = 404; status = 404;
@ -343,7 +341,7 @@ response(void) {
for (p = reqbuf; *p; p++) for (p = reqbuf; *p; p++)
if (*p == '\\' || (*p == '/' && *(p + 1) == '.')) { /* don't serve bogus or hidden files */ if (*p == '\\' || (*p == '/' && *(p + 1) == '.')) { /* don't serve bogus or hidden files */
if (putresentry(HEADER, HttpUnauthorized, tstamp()) if (putresentry(HEADER, HttpUnauthorized, tstamp(0))
|| putresentry(CONTENTTYPE, texthtml)) || putresentry(CONTENTTYPE, texthtml))
return; return;
status = 401; status = 401;