diff options
| -rw-r--r-- | xs_httpd.h | 39 | ||||
| -rw-r--r-- | xs_openssl.h | 58 | ||||
| -rw-r--r-- | xs_version.h | 2 |
3 files changed, 91 insertions, 8 deletions
| @@ -4,15 +4,15 @@ | |||
| 4 | 4 | ||
| 5 | #define _XS_HTTPD_H | 5 | #define _XS_HTTPD_H |
| 6 | 6 | ||
| 7 | xs_str *xs_url_dec(char *str); | 7 | xs_str *xs_url_dec(const char *str); |
| 8 | xs_dict *xs_url_vars(char *str); | 8 | xs_dict *xs_url_vars(const char *str); |
| 9 | xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size); | 9 | xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size); |
| 10 | void xs_httpd_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b_size); | 10 | void xs_httpd_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b_size); |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | #ifdef XS_IMPLEMENTATION | 13 | #ifdef XS_IMPLEMENTATION |
| 14 | 14 | ||
| 15 | xs_str *xs_url_dec(char *str) | 15 | xs_str *xs_url_dec(const char *str) |
| 16 | /* decodes an URL */ | 16 | /* decodes an URL */ |
| 17 | { | 17 | { |
| 18 | xs_str *s = xs_str_new(NULL); | 18 | xs_str *s = xs_str_new(NULL); |
| @@ -41,7 +41,7 @@ xs_str *xs_url_dec(char *str) | |||
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | 43 | ||
| 44 | xs_dict *xs_url_vars(char *str) | 44 | xs_dict *xs_url_vars(const char *str) |
| 45 | /* parse url variables */ | 45 | /* parse url variables */ |
| 46 | { | 46 | { |
| 47 | xs_dict *vars; | 47 | xs_dict *vars; |
| @@ -59,9 +59,34 @@ xs_dict *xs_url_vars(char *str) | |||
| 59 | while (xs_list_iter(&l, &v)) { | 59 | while (xs_list_iter(&l, &v)) { |
| 60 | xs *kv = xs_split_n(v, "=", 2); | 60 | xs *kv = xs_split_n(v, "=", 2); |
| 61 | 61 | ||
| 62 | if (xs_list_len(kv) == 2) | 62 | if (xs_list_len(kv) == 2) { |
| 63 | vars = xs_dict_append(vars, | 63 | const char *key = xs_list_get(kv, 0); |
| 64 | xs_list_get(kv, 0), xs_list_get(kv, 1)); | 64 | const char *pv = xs_dict_get(vars, key); |
| 65 | |||
| 66 | if (!xs_is_null(pv)) { | ||
| 67 | /* there is a previous value: convert to a list and append */ | ||
| 68 | xs *vlist = NULL; | ||
| 69 | if (xs_type(pv) == XSTYPE_LIST) | ||
| 70 | vlist = xs_dup(pv); | ||
| 71 | else { | ||
| 72 | vlist = xs_list_new(); | ||
| 73 | vlist = xs_list_append(vlist, pv); | ||
| 74 | } | ||
| 75 | |||
| 76 | vlist = xs_list_append(vlist, xs_list_get(kv, 1)); | ||
| 77 | vars = xs_dict_set(vars, key, vlist); | ||
| 78 | } | ||
| 79 | else { | ||
| 80 | /* ends with []? force to always be a list */ | ||
| 81 | if (xs_endswith(key, "[]")) { | ||
| 82 | xs *vlist = xs_list_new(); | ||
| 83 | vlist = xs_list_append(vlist, xs_list_get(kv, 1)); | ||
| 84 | vars = xs_dict_append(vars, key, vlist); | ||
| 85 | } | ||
| 86 | else | ||
| 87 | vars = xs_dict_append(vars, key, xs_list_get(kv, 1)); | ||
| 88 | } | ||
| 89 | } | ||
| 65 | } | 90 | } |
| 66 | } | 91 | } |
| 67 | 92 | ||
diff --git a/xs_openssl.h b/xs_openssl.h index eef57ed..4a86046 100644 --- a/xs_openssl.h +++ b/xs_openssl.h | |||
| @@ -22,6 +22,64 @@ int xs_evp_verify(const char *pubkey, const char *mem, int size, const char *b64 | |||
| 22 | #include "openssl/pem.h" | 22 | #include "openssl/pem.h" |
| 23 | #include "openssl/evp.h" | 23 | #include "openssl/evp.h" |
| 24 | 24 | ||
| 25 | #if 0 | ||
| 26 | xs_str *xs_base64_enc(const xs_val *data, int sz) | ||
| 27 | /* encodes data to base64 */ | ||
| 28 | { | ||
| 29 | BIO *mem, *b64; | ||
| 30 | BUF_MEM *bptr; | ||
| 31 | |||
| 32 | b64 = BIO_new(BIO_f_base64()); | ||
| 33 | mem = BIO_new(BIO_s_mem()); | ||
| 34 | b64 = BIO_push(b64, mem); | ||
| 35 | |||
| 36 | BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); | ||
| 37 | |||
| 38 | BIO_write(b64, data, sz); | ||
| 39 | BIO_flush(b64); | ||
| 40 | BIO_get_mem_ptr(b64, &bptr); | ||
| 41 | |||
| 42 | int n = bptr->length; | ||
| 43 | xs_str *s = xs_realloc(NULL, _xs_blk_size(n + 1)); | ||
| 44 | |||
| 45 | memcpy(s, bptr->data, n); | ||
| 46 | s[n] = '\0'; | ||
| 47 | |||
| 48 | BIO_free_all(b64); | ||
| 49 | |||
| 50 | return s; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | xs_val *xs_base64_dec(const xs_str *data, int *size) | ||
| 55 | /* decodes data from base64 */ | ||
| 56 | { | ||
| 57 | BIO *b64, *mem; | ||
| 58 | |||
| 59 | *size = strlen(data); | ||
| 60 | |||
| 61 | b64 = BIO_new(BIO_f_base64()); | ||
| 62 | mem = BIO_new_mem_buf(data, *size); | ||
| 63 | b64 = BIO_push(b64, mem); | ||
| 64 | |||
| 65 | BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); | ||
| 66 | |||
| 67 | /* alloc a very big buffer */ | ||
| 68 | xs_str *s = xs_realloc(NULL, *size); | ||
| 69 | |||
| 70 | *size = BIO_read(b64, s, *size); | ||
| 71 | |||
| 72 | /* adjust to current size */ | ||
| 73 | s = xs_realloc(s, _xs_blk_size(*size + 1)); | ||
| 74 | s[*size] = '\0'; | ||
| 75 | |||
| 76 | BIO_free_all(mem); | ||
| 77 | |||
| 78 | return s; | ||
| 79 | } | ||
| 80 | #endif | ||
| 81 | |||
| 82 | |||
| 25 | xs_str *_xs_digest(const xs_val *input, int size, const char *digest, int as_hex) | 83 | xs_str *_xs_digest(const xs_val *input, int size, const char *digest, int as_hex) |
| 26 | /* generic function for generating and encoding digests */ | 84 | /* generic function for generating and encoding digests */ |
| 27 | { | 85 | { |
diff --git a/xs_version.h b/xs_version.h index d2b2069..4c842e3 100644 --- a/xs_version.h +++ b/xs_version.h | |||
| @@ -1 +1 @@ | |||
| /* 01bea7d4a0e631c0406b358dda9cc9409362c003 */ | /* 333e84c76cd0e51f9f98a36df2eb3bf81e0d2608 */ | ||