summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--httpd.c34
-rw-r--r--snac.c117
-rw-r--r--snac.h3
3 files changed, 106 insertions, 48 deletions
diff --git a/httpd.c b/httpd.c
index 2c36364..bf72d5a 100644
--- a/httpd.c
+++ b/httpd.c
@@ -169,39 +169,7 @@ void httpd_connection(int rs)
169 169
170 fclose(f); 170 fclose(f);
171 171
172 /* obsessive archiving */ 172 srv_archive("RECV", req, payload, p_size, status, headers, body, b_size);
173 {
174 xs *date = xs_local_time("%Y%m%d%H%M%S");
175 xs *dir = xs_fmt("%s/archive/%s", srv_basedir, date);
176
177 if (mkdir(dir, 0755) != -1) {
178 xs *meta_fn = xs_fmt("%s/meta", dir);
179 xs *payload_fn = xs_fmt("%s/payload", dir);
180 xs *body_fn = xs_fmt("%s/body", dir);
181
182 if ((f = fopen(meta_fn, "w")) != NULL) {
183 xs *j1 = xs_json_dumps_pp(req, 4);
184 xs *j2 = xs_json_dumps_pp(headers, 4);
185
186 fprintf(f, "req: %s\n", j1);
187 fprintf(f, "p_size: %d\n", p_size);
188 fprintf(f, "status: %d\n", status);
189 fprintf(f, "response: %s\n", j2);
190 fprintf(f, "b_size: %d\n", b_size);
191 fclose(f);
192 }
193
194 if (p_size && payload && (f = fopen(payload_fn, "w")) != NULL) {
195 fwrite(payload, p_size, 1, f);
196 fclose(f);
197 }
198
199 if (b_size && body && (f = fopen(body_fn, "w")) != NULL) {
200 fwrite(body, p_size, 1, f);
201 fclose(f);
202 }
203 }
204 }
205 173
206 free(body); 174 free(body);
207} 175}
diff --git a/snac.c b/snac.c
index ed09c84..ba50a79 100644
--- a/snac.c
+++ b/snac.c
@@ -15,7 +15,7 @@
15#include "snac.h" 15#include "snac.h"
16 16
17#include <sys/time.h> 17#include <sys/time.h>
18 18#include <sys/stat.h>
19 19
20d_char *srv_basedir = NULL; 20d_char *srv_basedir = NULL;
21d_char *srv_config = NULL; 21d_char *srv_config = NULL;
@@ -55,6 +55,20 @@ d_char *tid(int offset)
55} 55}
56 56
57 57
58int validate_uid(char *uid)
59/* returns if uid is a valid identifier */
60{
61 while (*uid) {
62 if (!(isalnum(*uid) || *uid == '_'))
63 return 0;
64
65 uid++;
66 }
67
68 return 1;
69}
70
71
58void srv_debug(int level, d_char *str) 72void srv_debug(int level, d_char *str)
59/* logs a debug message */ 73/* logs a debug message */
60{ 74{
@@ -73,20 +87,6 @@ void srv_debug(int level, d_char *str)
73} 87}
74 88
75 89
76int validate_uid(char *uid)
77/* returns if uid is a valid identifier */
78{
79 while (*uid) {
80 if (!(isalnum(*uid) || *uid == '_'))
81 return 0;
82
83 uid++;
84 }
85
86 return 1;
87}
88
89
90void snac_debug(snac *snac, int level, d_char *str) 90void snac_debug(snac *snac, int level, d_char *str)
91/* prints a user debugging information */ 91/* prints a user debugging information */
92{ 92{
@@ -134,3 +134,90 @@ int check_password(char *uid, char *passwd, char *hash)
134 134
135 return ret; 135 return ret;
136} 136}
137
138
139void srv_archive(char *direction, char *req, char *payload, int p_size,
140 int status, char *headers, char *body, int b_size)
141/* archives a connection */
142{
143 /* obsessive archiving */
144 xs *date = xs_local_time("%Y%m%d%H%M%S");
145 xs *dir = xs_fmt("%s/archive/%s", srv_basedir, date);
146 FILE *f;
147
148 if (mkdir(dir, 0755) != -1) {
149 xs *meta_fn = xs_fmt("%s/_META", dir);
150
151 if ((f = fopen(meta_fn, "w")) != NULL) {
152 xs *j1 = xs_json_dumps_pp(req, 4);
153 xs *j2 = xs_json_dumps_pp(headers, 4);
154
155 fprintf(f, "dir: %s\n", direction);
156 fprintf(f, "req: %s\n", j1);
157 fprintf(f, "p_size: %d\n", p_size);
158 fprintf(f, "status: %d\n", status);
159 fprintf(f, "response: %s\n", j2);
160 fprintf(f, "b_size: %d\n", b_size);
161 fclose(f);
162 }
163
164 if (p_size && payload) {
165 xs *payload_fn;
166 char *h = xs_dict_get(req, "headers");
167 char *v = xs_dict_get(h, "content-type");
168
169 if (v && xs_str_in(v, "json") != -1) {
170 payload_fn = xs_fmt("%s/payload.json", dir);
171
172 if ((f = fopen(payload_fn, "w")) != NULL) {
173 xs *v1 = xs_json_loads(payload);
174 xs *j1 = xs_json_dumps_pp(v1, 4);
175
176 if (j1 != NULL)
177 fwrite(j1, strlen(j1), 1, f);
178 else
179 fwrite(payload, p_size, 1, f);
180
181 fclose(f);
182 }
183 }
184 else {
185 payload_fn = xs_fmt("%s/payload", dir);
186
187 if ((f = fopen(payload_fn, "w")) != NULL) {
188 fwrite(payload, p_size, 1, f);
189 fclose(f);
190 }
191 }
192 }
193
194 if (b_size && body) {
195 xs *body_fn;
196 char *v = xs_dict_get(headers, "content-type");
197
198 if (v && xs_str_in(v, "json") != -1) {
199 body_fn = xs_fmt("%s/body.json", dir);
200
201 if ((f = fopen(body_fn, "w")) != NULL) {
202 xs *v1 = xs_json_loads(payload);
203 xs *j1 = xs_json_dumps_pp(v1, 4);
204
205 if (j1 != NULL)
206 fwrite(j1, strlen(j1), 1, f);
207 else
208 fwrite(body, b_size, 1, f);
209
210 fclose(f);
211 }
212 }
213 else {
214 body_fn = xs_fmt("%s/body", dir);
215
216 if ((f = fopen(body_fn, "w")) != NULL) {
217 fwrite(body, b_size, 1, f);
218 fclose(f);
219 }
220 }
221 }
222 }
223} \ No newline at end of file
diff --git a/snac.h b/snac.h
index b76d7e9..c91186f 100644
--- a/snac.h
+++ b/snac.h
@@ -41,6 +41,9 @@ int validate_uid(char *uid);
41d_char *hash_password(char *uid, char *passwd, char *nonce); 41d_char *hash_password(char *uid, char *passwd, char *nonce);
42int check_password(char *uid, char *passwd, char *hash); 42int check_password(char *uid, char *passwd, char *hash);
43 43
44void srv_archive(char *direction, char *req, char *payload, int p_size,
45 int status, char *headers, char *body, int b_size);
46
44float mtime(char *fn); 47float mtime(char *fn);
45 48
46int follower_add(snac *snac, char *actor, char *msg); 49int follower_add(snac *snac, char *actor, char *msg);