Author: Lars Hjemli <hjemli@gmail.com>
Add support for ETag in 'plain' view When downloading a blob identified by its path, the client might want to know if the blob has been modified since a previous download of the same path. To this end, an ETag containing the blob SHA1 seems to be ideal. Todo: add support for HEAD requests... Suggested-by: Owen Taylor <otaylor@redhat.com> Signed-off-by: Lars Hjemli <hjemli@gmail.com>
cgit.c | 1 + cgit.h | 1 + ui-plain.c | 1 + ui-shared.c | 2 ++
diff --git a/cgit.c b/cgit.c index 64d95f9a3a7bb56197988e6730c4576173811db0..411e42132be546f3b8a9fc2c86b3f2f1dd04e04e 100644 --- a/cgit.c +++ b/cgit.c @@ -206,6 +206,7 @@ ctx->page.filename = NULL; ctx->page.size = 0; ctx->page.modified = time(NULL); ctx->page.expires = ctx->page.modified; + ctx->page.etag = NULL; } struct refmatch { diff --git a/cgit.h b/cgit.h index 5f7af51a68e0a773cac9c2758df51d5f0af90ff6..2bfbe7b0007ae65939cf972bd9cd3eb3093fef48 100644 --- a/cgit.h +++ b/cgit.h @@ -180,6 +180,7 @@ size_t size; char *mimetype; char *charset; char *filename; + char *etag; char *title; }; diff --git a/ui-plain.c b/ui-plain.c index 5addd9e686539ca41f264394ecf6836aa739af4e..f73cd14e8e56e3b080fe73a647c5ff91ed2f58ed 100644 --- a/ui-plain.c +++ b/ui-plain.c @@ -34,6 +34,7 @@ } ctx.page.mimetype = "text/plain"; ctx.page.filename = fmt("%s", path); ctx.page.size = size; + ctx.page.etag = sha1_to_hex(sha1); cgit_print_http_headers(&ctx); html_raw(buf, size); match = 1; diff --git a/ui-shared.c b/ui-shared.c index de77bbfadf86dd08e794145395ca1e20e1f63e9c..86a7d29cbc7263c2614229ed7909790eacb884a8 100644 --- a/ui-shared.c +++ b/ui-shared.c @@ -468,6 +468,8 @@ htmlf("Content-Disposition: inline; filename=\"%s\"\n", ctx->page.filename); htmlf("Last-Modified: %s\n", http_date(ctx->page.modified)); htmlf("Expires: %s\n", http_date(ctx->page.expires)); + if (ctx->page.etag) + htmlf("ETag: \"%s\"\n", ctx->page.etag); html("\n"); }