diff options
Diffstat (limited to 'snac.c')
| -rw-r--r-- | snac.c | 117 |
1 files changed, 102 insertions, 15 deletions
| @@ -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 | ||
| 20 | d_char *srv_basedir = NULL; | 20 | d_char *srv_basedir = NULL; |
| 21 | d_char *srv_config = NULL; | 21 | d_char *srv_config = NULL; |
| @@ -55,6 +55,20 @@ d_char *tid(int offset) | |||
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | 57 | ||
| 58 | int 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 | |||
| 58 | void srv_debug(int level, d_char *str) | 72 | void 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 | ||
| 76 | int 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 | |||
| 90 | void snac_debug(snac *snac, int level, d_char *str) | 90 | void 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 | |||
| 139 | void 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 | ||