diff options
Diffstat (limited to 'data.c')
-rw-r--r-- | data.c | 96 |
1 files changed, 96 insertions, 0 deletions
@@ -1748,3 +1748,99 @@ void purge_all(void) purge_server(); } + + +/** archive **/ + +void srv_archive(const char *direction, xs_dict *req, + const char *payload, int p_size, + int status, xs_dict *headers, + const char *body, int b_size) +/* archives a connection */ +{ + /* obsessive archiving */ + xs *date = tid(0); + xs *dir = xs_fmt("%s/archive/%s_%s", srv_basedir, date, direction); + FILE *f; + + if (mkdirx(dir) != -1) { + xs *meta_fn = xs_fmt("%s/_META", dir); + + if ((f = fopen(meta_fn, "w")) != NULL) { + xs *j1 = xs_json_dumps_pp(req, 4); + xs *j2 = xs_json_dumps_pp(headers, 4); + + fprintf(f, "dir: %s\n", direction); + fprintf(f, "req: %s\n", j1); + fprintf(f, "p_size: %d\n", p_size); + fprintf(f, "status: %d\n", status); + fprintf(f, "response: %s\n", j2); + fprintf(f, "b_size: %d\n", b_size); + fclose(f); + } + + if (p_size && payload) { + xs *payload_fn = NULL; + xs *payload_fn_raw = NULL; + char *v = xs_dict_get(req, "content-type"); + + if (v && xs_str_in(v, "json") != -1) { + payload_fn = xs_fmt("%s/payload.json", dir); + + if ((f = fopen(payload_fn, "w")) != NULL) { + xs *v1 = xs_json_loads(payload); + xs *j1 = NULL; + + if (v1 != NULL) + j1 = xs_json_dumps_pp(v1, 4); + + if (j1 != NULL) + fwrite(j1, strlen(j1), 1, f); + else + fwrite(payload, p_size, 1, f); + + fclose(f); + } + } + + payload_fn_raw = xs_fmt("%s/payload", dir); + + if ((f = fopen(payload_fn_raw, "w")) != NULL) { + fwrite(payload, p_size, 1, f); + fclose(f); + } + } + + if (b_size && body) { + xs *body_fn = NULL; + char *v = xs_dict_get(headers, "content-type"); + + if (v && xs_str_in(v, "json") != -1) { + body_fn = xs_fmt("%s/body.json", dir); + + if ((f = fopen(body_fn, "w")) != NULL) { + xs *v1 = xs_json_loads(body); + xs *j1 = NULL; + + if (v1 != NULL) + j1 = xs_json_dumps_pp(v1, 4); + + if (j1 != NULL) + fwrite(j1, strlen(j1), 1, f); + else + fwrite(body, b_size, 1, f); + + fclose(f); + } + } + else { + body_fn = xs_fmt("%s/body", dir); + + if ((f = fopen(body_fn, "w")) != NULL) { + fwrite(body, b_size, 1, f); + fclose(f); + } + } + } + } +} |