-/* $OpenBSD: extern.h,v 1.55 2021/03/19 13:56:10 claudio Exp $ */
+/* $OpenBSD: extern.h,v 1.56 2021/03/25 12:18:45 claudio Exp $ */
/*
* Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
*
RTYPE_GBR,
};
+enum http_result {
+ HTTP_FAILED, /* anything else */
+ HTTP_OK, /* 200 OK */
+ HTTP_NOT_MOD, /* 304 Not Modified */
+};
+
/*
* An entity (MFT, ROA, certificate, etc.) that needs to be downloaded
* and parsed.
-/* $OpenBSD: http.c,v 1.8 2021/03/18 16:15:19 tb Exp $ */
+/* $OpenBSD: http.c,v 1.9 2021/03/25 12:18:45 claudio Exp $ */
/*
* Copyright (c) 2020 Nils Fisher <nils_fisher@hotmail.com>
* Copyright (c) 2020 Claudio Jeker <claudio@openbsd.com>
}
static void
-http_done(struct http_connection *conn, int ok)
+http_done(struct http_connection *conn, enum http_result res)
{
struct ibuf *b;
if ((b = ibuf_dynamic(64, UINT_MAX)) == NULL)
err(1, NULL);
io_simple_buffer(b, &conn->id, sizeof(conn->id));
- io_simple_buffer(b, &ok, sizeof(ok));
-#if 0 /* TODO: cache last_modified */
+ io_simple_buffer(b, &res, sizeof(res));
io_str_buffer(b, conn->last_modified);
-#endif
ibuf_close(&msgq, b);
}
http_fail(size_t id)
{
struct ibuf *b;
- int ok = 0;
+ enum http_result res = HTTP_FAILED;
if ((b = ibuf_dynamic(8, UINT_MAX)) == NULL)
err(1, NULL);
io_simple_buffer(b, &id, sizeof(id));
- io_simple_buffer(b, &ok, sizeof(ok));
+ io_simple_buffer(b, &res, sizeof(res));
+ io_str_buffer(b, NULL);
ibuf_close(&msgq, b);
}
{
char *host, *port, *path;
- warnx("redirect to %s", http_info(uri));
+ logx("redirect to %s", http_info(uri));
if (http_parse_uri(uri, &host, &port, &path) == -1) {
free(uri);
case 302:
case 303:
case 307:
+ case 308:
if (conn->redirect_loop++ > 10) {
warnx("%s: Too many redirections requested",
http_info(conn->url));
}
/* FALLTHROUGH */
case 200:
- case 206:
case 304:
conn->status = status;
break;
http_isredirect(struct http_connection *conn)
{
if ((conn->status >= 301 && conn->status <= 303) ||
- conn->status == 307)
+ conn->status == 307 || conn->status == 308)
return 1;
return 0;
}
conn->chunksz = chunksize;
if (conn->chunksz == 0) {
- http_done(conn, 1);
+ http_done(conn, HTTP_OK);
return 0;
}
memmove(conn->buf, conn->buf + s, conn->bufpos);
if (conn->bytes == conn->filesize) {
- http_done(conn, 1);
+ http_done(conn, HTTP_OK);
return 0;
}
conn->state = STATE_RESPONSE_HEADER;
return WANT_POLLIN;
case STATE_RESPONSE_HEADER:
- if (conn->status == 200)
+ if (conn->status == 200) {
conn->state = STATE_RESPONSE_DATA;
- else {
- http_done(conn, 0);
+ } else {
+ if (conn->status == 304)
+ http_done(conn, HTTP_NOT_MOD);
+ else
+ http_done(conn, HTTP_FAILED);
return http_close(conn);
}
return WANT_POLLIN;
-/* $OpenBSD: main.c,v 1.122 2021/03/19 13:56:10 claudio Exp $ */
+/* $OpenBSD: main.c,v 1.123 2021/03/25 12:18:45 claudio Exp $ */
/*
* Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
*
}
static int
-http_done(struct repo *rp, int ok)
+http_done(struct repo *rp, enum http_result res)
{
if (rp->repouri == NULL) {
/* Move downloaded TA file into place, or unlink on failure. */
- if (ok) {
+ if (res == HTTP_OK) {
char *file;
file = ta_filename(rp, 0);
rp->temp = NULL;
}
- if (!ok && rp->uriidx < REPO_MAX_URI - 1 &&
+ if (res == HTTP_OK)
+ logx("%s: loaded from network", rp->local);
+ else if (rp->uriidx < REPO_MAX_URI - 1 &&
rp->uris[rp->uriidx + 1] != NULL) {
logx("%s: load from network failed, retry", rp->local);
rp->uriidx++;
repo_fetch(rp);
return 0;
- }
-
- if (ok)
- logx("%s: loaded from network", rp->local);
- else
+ } else
logx("%s: load from network failed, "
"fallback to cache", rp->local);
int
main(int argc, char *argv[])
{
- int rc = 1, c, st, proc, rsync, http,
+ int rc = 1, c, st, proc, rsync, http, ok,
fl = SOCK_STREAM | SOCK_CLOEXEC;
size_t i, id, outsz = 0, talsz = 0;
pid_t procpid, rsyncpid, httppid;
*/
if ((pfd[0].revents & POLLIN)) {
- int ok;
io_simple_read(rsync, &id, sizeof(id));
io_simple_read(rsync, &ok, sizeof(ok));
rp = repo_find(id);
}
if ((pfd[2].revents & POLLIN)) {
- int ok;
+ enum http_result res;
+ char *last_mod;
+
io_simple_read(http, &id, sizeof(id));
- io_simple_read(http, &ok, sizeof(ok));
+ io_simple_read(http, &res, sizeof(res));
+ io_str_read(http, &last_mod);
rp = repo_find(id);
if (rp == NULL)
errx(1, "unknown repository id: %zu", id);
assert(!rp->loaded);
- if (http_done(rp, ok)) {
+ if (http_done(rp, res)) {
rp->loaded = 1;
stats.repos++;
entityq_flush(rp);
}
+ free(last_mod);
}
/*