-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpfsclient.c
299 lines (250 loc) · 7.58 KB
/
httpfsclient.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright (C) 2002 John Todd Larason <[email protected]>
*
* Parts based on ReplayPC 0.3 by Matthew T. Linehan and others
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "httpfsclient.h"
#include "sleep.h"
#include "crypt.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#define ARGBUFSIZE 2048
static int make_httpfs_url(char * dst, size_t size,
const char * address, const char * command,
va_list args)
{
char * d, * tag, * value, *argp;
int argno = 0;
size_t l, argl;
char argbuf[ARGBUFSIZE]; /* XXX bad, should fix this */
(void) size;
l = strlen(address) + strlen(command) + strlen("http:///httpfs-?");
if (l >= size) {
fprintf(stderr, "Error: make_httpfs_url: address + command too long for buffer\n");
return -1;
}
d = dst;
d += sprintf(d, "http://%s/httpfs-%s?", address, command);
argp = argbuf;
argl = 0;
while ((tag = va_arg(args, char *)) != NULL) {
value = va_arg(args, char *);
if (value == NULL) {
continue;
}
if (argno)
argl++;
argl += strlen(tag)+1+strlen(value);
if (argl >= sizeof(argbuf)) {
fprintf(stderr, "Error: make_httpfs_url: with arg %s, argbuf too small\n", tag);
return -1;
}
if (argno)
*argp++ = '&';
argp += sprintf(argp, "%s=%s", tag, value);
argno++;
}
if (replaytv_version.major >= 5 ||
(replaytv_version.major == 4 && replaytv_version.minor >= 3)) {
unsigned char ctext[ARGBUFSIZE+32] ;
u32 ctextlen;
unsigned int i;
if (l + strlen("__Q_=") + 2*argl + 32 >= size) {
fprintf(stderr, "Error: make_httpfs_url: encrypted args too large for buffer\n");
return -1;
}
strcpy(d, "__Q_=");
d += strlen(d);
rtv_encrypt(argbuf, argl, ctext, sizeof ctext, &ctextlen, 1);
for (i = 0; i < ctextlen; i++)
d += sprintf(d, "%02x", ctext[i]);
} else {
if (l + argl >= size) {
fprintf(stderr, "Error: make_httpfs_url: args too large for buffer\n");
return -1;
}
strcpy(d, argbuf);
}
return 0;
}
static int add_httpfs_headers(struct hc * hc)
{
hc_add_req_header(hc, "Authorization", "Basic Uk5TQmFzaWM6QTd4KjgtUXQ=" );
hc_add_req_header(hc, "User-Agent", "Replay-HTTPFS/1");
hc_add_req_header(hc, "Accept-Encoding", "text/plain");
return 0;
}
static struct hc * make_request(const char * address, const char * command,
va_list ap)
{
struct hc * hc = NULL;
char url[URLSIZE];
int http_status;
if (make_httpfs_url(url, sizeof url, address, command, ap) < 0)
goto exit;
hc = hc_start_request(url);
if (!hc) {
perror("Error: make_request(): hc_start_request()");
goto exit;
}
if (add_httpfs_headers(hc)) {
goto exit;
}
hc_send_request(hc);
http_status = hc_get_status(hc);
if (http_status/100 != 2) {
fprintf(stderr, "Error: http status %d\n", http_status);
goto exit;
}
return hc;
exit:
if (hc)
hc_free(hc);
return NULL;
}
unsigned long hfs_do_simple(char ** presult, const char * address,
const char * command, ...)
{
va_list ap;
struct hc * hc;
char * tmp, * e;
unsigned long rtv_status;
va_start(ap, command);
hc = make_request(address, command, ap);
va_end(ap);
if (!hc)
return -1;
tmp = hc_read_all(hc);
hc_free(hc);
e = strchr(tmp, '\n');
if (e) {
*presult = strdup(e+1);
rtv_status = strtoul(tmp, NULL, 10);
free(tmp);
return rtv_status;
} else if (hc_get_status(hc) == 204) {
*presult = NULL;
free(tmp);
return 0;
} else {
fprintf(stderr, "Error: end of httpfs status line not found\n");
return -1;
}
}
struct hfs_data
{
void (*fn)(unsigned char *, size_t, void *);
void * v;
unsigned long status;
u16 msec_delay;
u8 firsttime;
};
/* XXX should this free the buf, or make a new one on the first block
* and let our caller free them if it wants? */
static void hfs_callback(unsigned char * buf, size_t len, void * vd)
{
struct hfs_data * data = vd;
unsigned char * buf_data_start;
if (data->firsttime) {
unsigned char * e;
data->firsttime = 0;
/* First line: error code */
e = strchr(buf, '\n');
if (e)
*e = '\0';
data->status = strtoul(buf, NULL, 16);
e++;
len -= (e - buf);
buf_data_start = e;
} else {
buf_data_start = buf;
}
data->fn(buf_data_start, len, data->v);
free(buf);
if (data->msec_delay)
rtv_sleep(data->msec_delay);
}
unsigned long hfs_do_chunked(void (*fn)(unsigned char *, size_t, void *),
void *v,
const char * address,
u16 msec_delay,
const char * command,
...)
{
struct hfs_data data;
struct hc * hc;
va_list ap;
va_start(ap, command);
hc = make_request(address, command, ap);
va_end(ap);
if (!hc)
return -1;
memset(&data, 0, sizeof data);
data.fn = fn;
data.v = v;
data.firsttime = 1;
data.msec_delay = msec_delay;
hc_read_pieces(hc, hfs_callback, &data);
hc_free(hc);
return data.status;
}
unsigned long hfs_do_post_simple(char ** presult, const char * address,
int (*fn)(unsigned char *, size_t, void *),
void *v,
unsigned long size,
const char * command, ...)
{
char buf[URLSIZE];
va_list ap;
struct hc * hc;
char * tmp, * e;
int http_status;
unsigned long rtv_status;
va_start(ap, command);
if (make_httpfs_url(buf, sizeof buf, address, command, ap) < 0)
return -1;
va_end(ap);
hc = hc_start_request(buf);
if (!hc) {
perror("Error: hfs_do_simple(): hc_start_request()");
return -1;
}
sprintf(buf, "%lu", size);
if (add_httpfs_headers(hc) != 0)
return -1;
hc_add_req_header(hc, "Content-Length", buf);
hc_post_request(hc, fn, v);
http_status = hc_get_status(hc);
if (http_status/100 != 2) {
fprintf(stderr, "Error: http status %d\n", http_status);
hc_free(hc);
return http_status;
}
tmp = hc_read_all(hc);
hc_free(hc);
e = strchr(tmp, '\n');
if (e) {
*presult = strdup(e+1);
rtv_status = strtoul(tmp, NULL, 10);
free(tmp);
return rtv_status;
} else if (http_status == 204) {
*presult = NULL;
free(tmp);
return 0;
} else {
fprintf(stderr, "Error: end of httpfs status line not found\n");
return -1;
}
}