Repository navigation
Expand file tree
/
Copy pathhttpfsclient.c
More file actions
401 lines (333 loc) · 10.1 KB
/
Copy pathhttpfsclient.c
File metadata and controls
401 lines (333 loc) · 10.1 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#ifndef __GNUC__
#pragma code_seg( "RTV_TEXT" )
#pragma data_seg( "RTV_DATA" )
#pragma bss_seg( "RTV_BSS" )
#pragma const_seg( "RTV_RD" )
#endif
/*
* Copyright (C) 2002 John Todd Larason <jtl@molehill.org>
*
* 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>
#include <errno.h>
int hfs_output_errors = 1;
#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) {
//if (hfs_output_errors)
//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)) {
//if (hfs_output_errors)
//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) {
//if (hfs_output_errors)
// 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, (char*)ctext, sizeof ctext, &ctextlen, 1);
for (i = 0; i < ctextlen; i++)
d += sprintf(d, "%02x", ctext[i]);
} else {
if (l + argl >= size) {
//if (hfs_output_errors)
//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) {
if (hfs_output_errors)
perror("Error: make_request(): hc_start_request()");
if (errno == 0) {
// fprintf(stderr, "Check your clock.\n"
// "The system clock must be within 40 seconds of the Replay's.");
}
goto exit;
}
if (add_httpfs_headers(hc)) {
goto exit;
}
hc_send_request(hc);
http_status = hc_get_status(hc);
if (http_status/100 != 2) {
//if (hfs_output_errors)
//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;
int http_status;
unsigned long rtv_status;
va_start(ap, command);
hc = make_request(address, command, ap);
va_end(ap);
if (!hc)
return -1;
http_status = hc_get_status(hc);
if (http_status/100 != 2) {
//if (hfs_output_errors)
// fprintf(stderr, "Error: http status %d\n", http_status);
hc_free(hc);
return http_status;
}
tmp = (char*)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 {
//if (hfs_output_errors)
// 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;
char * buffer;
size_t bufLen;
};
/* 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 = (unsigned char*)strchr((char*)buf, '\n');
if (e)
*e = '\0';
data->status = strtoul((char*)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);
errno = 0;
hc = hc_start_request(buf);
if (!hc) {
if (hfs_output_errors)
perror("Error: hfs_do_simple(): hc_start_request()");
if (errno == 0) {
//fprintf(stderr, "Check your clock.\n"
// "The system clock must be within 40 seconds of the Replay's.");
}
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) {
//if (hfs_output_errors)
// fprintf(stderr, "Error: http status %d\n", http_status);
hc_free(hc);
return http_status;
}
tmp = (char*)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 {
//if (hfs_output_errors)
//fprintf(stderr, "Error: end of httpfs status line not found\n");
return -1;
}
}
static void hfs_callback2(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 = (unsigned char*)strchr((char*)buf, '\n');
if (e)
*e = '\0';
data->status = strtoul((char*)buf, NULL, 16);
e++;
len -= (e - buf);
buf_data_start = e;
} else {
buf_data_start = buf;
}
/* append to buffer, this may be called multiple times */
memcpy(data->buffer+data->bufLen, buf_data_start, len);
data->bufLen += len;
free(buf);
}
struct hc * hfs_do_chunked_open(const char * address, const char * command, ...)
{
struct hc * hc;
va_list ap;
va_start(ap, command);
hc = make_request(address, command, ap);
va_end(ap);
return hc;
}
size_t hfs_do_chunked_read(struct hc * hc, char * buf, size_t pos, size_t len)
{
struct hfs_data data;
memset(&data, 0, sizeof data);
data.buffer = buf;
if (!pos)
data.firsttime = 1;
hc_read_pieces_len(hc, hfs_callback2, &data, len);
return data.bufLen;
}
void hfs_do_chunked_close(struct hc * hc)
{
hc_free(hc);
}