uclient-http: allow .header_done() callback to reset the connection
[project/uclient.git] / uclient-utils.c
1 /*
2  * uclient - ustream based protocol client library
3  *
4  * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <ctype.h>
22
23 #include <libubox/md5.h>
24 #include <libubox/utils.h>
25
26 #include "uclient-utils.h"
27
28 static const char *b64 =
29 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
30
31 void base64_encode(const void *inbuf, unsigned int len, void *outbuf)
32 {
33         unsigned char *out = outbuf;
34         const uint8_t *in = inbuf;
35         unsigned int i;
36         int pad = len % 3;
37
38         for (i = 0; i < len - pad; i += 3) {
39                 uint32_t in3 = (in[0] << 16) | (in[1] << 8) | in[2];
40                 int k;
41
42                 for (k = 3; k >= 0; k--) {
43                         out[k] = b64[in3 & 0x3f];
44                         in3 >>= 6;
45                 }
46                 in += 3;
47                 out += 4;
48         }
49
50         if (pad) {
51                 uint32_t in2 = in[0] << (16 - 6);
52
53                 out[3] = '=';
54
55                 if (pad > 1) {
56                         in2 |= in[1] << (8 - 6);
57                         out[2] = b64[in2 & 0x3f];
58                 } else {
59                         out[2] = '=';
60                 }
61
62                 in2 >>= 6;
63                 out[1] = b64[in2 & 0x3f];
64                 in2 >>= 6;
65                 out[0] = b64[in2 & 0x3f];
66
67                 out += 4;
68         }
69
70         *out = '\0';
71 }
72
73 int uclient_urldecode(const char *in, char *out, bool decode_plus)
74 {
75         static char dec[3];
76         int ret = 0;
77         char c;
78
79         while ((c = *(in++))) {
80                 if (c == '%') {
81                         if (!isxdigit(in[0]) || !isxdigit(in[1]))
82                                 return -1;
83
84                         dec[0] = in[0];
85                         dec[1] = in[1];
86                         c = strtol(dec, NULL, 16);
87                         in += 2;
88                 } else if (decode_plus && c == '+') {
89                         c = ' ';
90                 }
91
92                 *(out++) = c;
93                 ret++;
94         }
95
96         *out = 0;
97         return ret;
98 }
99
100 static char hex_digit(char val)
101 {
102         val += val > 9 ? 'a' - 10 : '0';
103         return val;
104 }
105
106 void bin_to_hex(char *dest, const void *buf, int len)
107 {
108         const uint8_t *data = buf;
109         int i;
110
111         for (i = 0; i < len; i++) {
112                 *(dest++) = hex_digit(data[i] >> 4);
113                 *(dest++) = hex_digit(data[i] & 0xf);
114         }
115         *dest = 0;
116 }
117
118 static void http_create_hash(char *dest, const char * const * str, int n_str)
119 {
120         uint32_t hash[4];
121         md5_ctx_t md5;
122         int i;
123
124         md5_begin(&md5);
125         for (i = 0; i < n_str; i++) {
126                 if (i)
127                         md5_hash(":", 1, &md5);
128                 md5_hash(str[i], strlen(str[i]), &md5);
129         }
130         md5_end(hash, &md5);
131         bin_to_hex(dest, &hash, sizeof(hash));
132 }
133
134 void http_digest_calculate_auth_hash(char *dest, const char *user, const char *realm, const char *password)
135 {
136         const char *hash_str[] = {
137                 user,
138                 realm,
139                 password
140         };
141
142         http_create_hash(dest, hash_str, ARRAY_SIZE(hash_str));
143 }
144
145 void http_digest_calculate_response(char *dest, const struct http_digest_data *data)
146 {
147         const char *h_a2_strings[] = {
148                 data->method,
149                 data->uri,
150         };
151         const char *resp_strings[] = {
152                 data->auth_hash,
153                 data->nonce,
154                 data->nc,
155                 data->cnonce,
156                 data->qop,
157                 dest, /* initialized to H(A2) first */
158         };
159
160         http_create_hash(dest, h_a2_strings, ARRAY_SIZE(h_a2_strings));
161         http_create_hash(dest, resp_strings, ARRAY_SIZE(resp_strings));
162 }
163
164 char *uclient_get_url_filename(const char *url, const char *default_name)
165 {
166         const char *str;
167         int len = strcspn(url, ";&");
168
169         while (len > 0 && url[len - 1] == '/')
170                 len--;
171
172         for (str = url + len - 1; str >= url; str--) {
173                 if (*str == '/')
174                         break;
175         }
176
177         str++;
178         len -= str - url;
179
180         if (len > 0)
181                 return strncpy(calloc(1, len + 1), str, len);
182
183         return strdup(default_name);
184 }