forked from wxhuang/httpin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hget.c
72 lines (65 loc) · 1.32 KB
/
hget.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
#include "config.h"
#include "httpin.h"
typedef struct _hget_ctx
{
unsigned char* buffer;
unsigned int bufsize;
unsigned int bytes_read;
}hget_ctx;
int hget_cb(void* buf, unsigned int size, void* pctx, unsigned int status)
{
int ret = 0;
unsigned int tmp;
hget_ctx* ctx = pctx;
switch(status)
{
case HTTPIN_HEADER:
break;
case HTTPIN_START:
break;
case HTTPIN_PROGRESS:
tmp = ctx->bufsize - ctx->bytes_read;
if(size > tmp)
{
size = tmp;
ret = -1;
}
memcpy(ctx->buffer + ctx->bytes_read, buf, size);
ctx->bytes_read += size;
break;
case HTTPIN_FINISHED:
ctx->buffer[ctx->bytes_read] = 0;
case HTTPIN_ABORT:
case HTTPIN_ERROR:
default:
break;
}
return ret;
}
int hget(unsigned char* url, unsigned char* buffer, int bufsize)
{
int ret;
char urlbuf[1024];
strcpy(urlbuf, url);
httpin_param param;
memset(¶m, 0, sizeof(param));
param.url = urlbuf;
param.running_flag = 1;
hget_ctx ctx;
ctx.buffer = buffer;
ctx.bufsize = (unsigned int)bufsize;
ctx.bytes_read = 0;
ret = httpin_get(¶m, hget_cb, &ctx);
printf("%s\n", ctx.buffer);
printf("recvsize = %d\n", ctx.bytes_read);
return ret;
}
int main()
{
INT32 ret;
UINT8* buf = malloc(0x40000);
ret = hget("http://gdata.youtube.com/feeds/api/videos", buf, 0x40000);
printf("%d\n", ret);
free(buf);
return 0;
}