commit 105683d

default  ·  2023-03-01 07:09:56 +0000 UTC
parent fd47743
Moved srv_archive() to data.c.
2 files changed,  +96, -94
M data.c
M snac.c
M data.c
+96, -0
  1@@ -1748,3 +1748,99 @@ void purge_all(void)
  2 
  3     purge_server();
  4 }
  5+
  6+
  7+/** archive **/
  8+
  9+void srv_archive(const char *direction, xs_dict *req,
 10+                 const char *payload, int p_size,
 11+                 int status, xs_dict *headers,
 12+                 const char *body, int b_size)
 13+/* archives a connection */
 14+{
 15+    /* obsessive archiving */
 16+    xs *date = tid(0);
 17+    xs *dir  = xs_fmt("%s/archive/%s_%s", srv_basedir, date, direction);
 18+    FILE *f;
 19+
 20+    if (mkdirx(dir) != -1) {
 21+        xs *meta_fn = xs_fmt("%s/_META", dir);
 22+
 23+        if ((f = fopen(meta_fn, "w")) != NULL) {
 24+            xs *j1 = xs_json_dumps_pp(req, 4);
 25+            xs *j2 = xs_json_dumps_pp(headers, 4);
 26+
 27+            fprintf(f, "dir: %s\n", direction);
 28+            fprintf(f, "req: %s\n", j1);
 29+            fprintf(f, "p_size: %d\n", p_size);
 30+            fprintf(f, "status: %d\n", status);
 31+            fprintf(f, "response: %s\n", j2);
 32+            fprintf(f, "b_size: %d\n", b_size);
 33+            fclose(f);
 34+        }
 35+
 36+        if (p_size && payload) {
 37+            xs *payload_fn = NULL;
 38+            xs *payload_fn_raw = NULL;
 39+            char *v = xs_dict_get(req, "content-type");
 40+
 41+            if (v && xs_str_in(v, "json") != -1) {
 42+                payload_fn = xs_fmt("%s/payload.json", dir);
 43+
 44+                if ((f = fopen(payload_fn, "w")) != NULL) {
 45+                    xs *v1 = xs_json_loads(payload);
 46+                    xs *j1 = NULL;
 47+
 48+                    if (v1 != NULL)
 49+                        j1 = xs_json_dumps_pp(v1, 4);
 50+
 51+                    if (j1 != NULL)
 52+                        fwrite(j1, strlen(j1), 1, f);
 53+                    else
 54+                        fwrite(payload, p_size, 1, f);
 55+
 56+                    fclose(f);
 57+                }
 58+            }
 59+
 60+            payload_fn_raw = xs_fmt("%s/payload", dir);
 61+
 62+            if ((f = fopen(payload_fn_raw, "w")) != NULL) {
 63+                fwrite(payload, p_size, 1, f);
 64+                fclose(f);
 65+            }
 66+        }
 67+
 68+        if (b_size && body) {
 69+            xs *body_fn = NULL;
 70+            char *v = xs_dict_get(headers, "content-type");
 71+
 72+            if (v && xs_str_in(v, "json") != -1) {
 73+                body_fn = xs_fmt("%s/body.json", dir);
 74+
 75+                if ((f = fopen(body_fn, "w")) != NULL) {
 76+                    xs *v1 = xs_json_loads(body);
 77+                    xs *j1 = NULL;
 78+
 79+                    if (v1 != NULL)
 80+                        j1 = xs_json_dumps_pp(v1, 4);
 81+
 82+                    if (j1 != NULL)
 83+                        fwrite(j1, strlen(j1), 1, f);
 84+                    else
 85+                        fwrite(body, b_size, 1, f);
 86+
 87+                    fclose(f);
 88+                }
 89+            }
 90+            else {
 91+                body_fn = xs_fmt("%s/body", dir);
 92+
 93+                if ((f = fopen(body_fn, "w")) != NULL) {
 94+                    fwrite(body, b_size, 1, f);
 95+                    fclose(f);
 96+                }
 97+            }
 98+        }
 99+    }
100+}
M snac.c
+0, -94
 1@@ -148,97 +148,3 @@ int check_password(const char *uid, const char *passwd, const char *hash)
 2 
 3     return ret;
 4 }
 5-
 6-
 7-void srv_archive(const char *direction, xs_dict *req,
 8-                 const char *payload, int p_size,
 9-                 int status, xs_dict *headers,
10-                 const char *body, int b_size)
11-/* archives a connection */
12-{
13-    /* obsessive archiving */
14-    xs *date = tid(0);
15-    xs *dir  = xs_fmt("%s/archive/%s_%s", srv_basedir, date, direction);
16-    FILE *f;
17-
18-    if (mkdirx(dir) != -1) {
19-        xs *meta_fn = xs_fmt("%s/_META", dir);
20-
21-        if ((f = fopen(meta_fn, "w")) != NULL) {
22-            xs *j1 = xs_json_dumps_pp(req, 4);
23-            xs *j2 = xs_json_dumps_pp(headers, 4);
24-
25-            fprintf(f, "dir: %s\n", direction);
26-            fprintf(f, "req: %s\n", j1);
27-            fprintf(f, "p_size: %d\n", p_size);
28-            fprintf(f, "status: %d\n", status);
29-            fprintf(f, "response: %s\n", j2);
30-            fprintf(f, "b_size: %d\n", b_size);
31-            fclose(f);
32-        }
33-
34-        if (p_size && payload) {
35-            xs *payload_fn = NULL;
36-            xs *payload_fn_raw = NULL;
37-            char *v = xs_dict_get(req, "content-type");
38-
39-            if (v && xs_str_in(v, "json") != -1) {
40-                payload_fn = xs_fmt("%s/payload.json", dir);
41-
42-                if ((f = fopen(payload_fn, "w")) != NULL) {
43-                    xs *v1 = xs_json_loads(payload);
44-                    xs *j1 = NULL;
45-
46-                    if (v1 != NULL)
47-                        j1 = xs_json_dumps_pp(v1, 4);
48-
49-                    if (j1 != NULL)
50-                        fwrite(j1, strlen(j1), 1, f);
51-                    else
52-                        fwrite(payload, p_size, 1, f);
53-
54-                    fclose(f);
55-                }
56-            }
57-
58-            payload_fn_raw = xs_fmt("%s/payload", dir);
59-
60-            if ((f = fopen(payload_fn_raw, "w")) != NULL) {
61-                fwrite(payload, p_size, 1, f);
62-                fclose(f);
63-            }
64-        }
65-
66-        if (b_size && body) {
67-            xs *body_fn = NULL;
68-            char *v = xs_dict_get(headers, "content-type");
69-
70-            if (v && xs_str_in(v, "json") != -1) {
71-                body_fn = xs_fmt("%s/body.json", dir);
72-
73-                if ((f = fopen(body_fn, "w")) != NULL) {
74-                    xs *v1 = xs_json_loads(body);
75-                    xs *j1 = NULL;
76-
77-                    if (v1 != NULL)
78-                        j1 = xs_json_dumps_pp(v1, 4);
79-
80-                    if (j1 != NULL)
81-                        fwrite(j1, strlen(j1), 1, f);
82-                    else
83-                        fwrite(body, b_size, 1, f);
84-
85-                    fclose(f);
86-                }
87-            }
88-            else {
89-                body_fn = xs_fmt("%s/body", dir);
90-
91-                if ((f = fopen(body_fn, "w")) != NULL) {
92-                    fwrite(body, b_size, 1, f);
93-                    fclose(f);
94-                }
95-            }
96-        }
97-    }
98-}