-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttp.c
More file actions
219 lines (182 loc) · 3.8 KB
/
Copy pathhttp.c
File metadata and controls
219 lines (182 loc) · 3.8 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
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include "types.h"
#include "http.h"
#include "sound_file.h"
struct sound_file_info *http_file_open(const char *URL)
{
// fixme!
return 0;
}
int http_file_close(struct sound_file_info *file)
{
// fixme!
return 0;
}
int get_http_head(int fd, char *buff, size_t buff_size)
{
int ret;
int size;
size = 4;
ret = recv(fd, buff, size, 0);
if (ret < 0) {
perror("recv");
return -1;
}
while (memcmp(buff + size - 4, "\r\n\r\n", 4) && size < buff_size)
{
ret = recv(fd, buff + size, 1, 0);
size++;
}
buff[size] = '\0';
return size;
}
int read_timeouts(int fd, int sec)
{
fd_set rfds;
struct timeval timeout;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
timeout.tv_sec = sec;
timeout.tv_usec = 0;
return select(fd + 1, &rfds, NULL, NULL, &timeout);
}
void *download(void *arg)
{
struct download_arg *p;
long used = 0;
int len;
int ret;
int reconn = 0;
char httpHeadBuff[HTTP_HEAD_LEN];
char request[RQU_LEN];
int down_fd;
struct sockaddr_in server;
down_fd = socket(AF_INET, SOCK_STREAM, 0);
if (down_fd < 0) {
perror("socket");
goto L1;
}
p = arg;
server.sin_family = AF_INET;
server.sin_port = htons(SERVER_PORT);
inet_aton(p->ip, &server.sin_addr);
ret = connect(down_fd, (struct sockaddr *)&server, sizeof(server));
if (ret < 0) {
perror("connect()");
goto L1;
}
len = sprintf(request,
"GET %s HTTP/1.0\r\nRange:bytes=%ld-%ld\r\n\r\n",
p->file->url,
(long)p->offset,
(long)p->offset + p->size - 1);
ret = send(down_fd, request, len, 0);
if (ret < 0) {
perror("send");
goto L1;
}
ret = get_http_head(down_fd, httpHeadBuff, HTTP_HEAD_LEN);
if (ret < 0) {
printf("get_http_head()");
goto L1;
}
while (p->size > 0) {
ret = read_timeouts(down_fd, SEC);
if (ret < 0) {
perror("select");
goto L1;
}
if (ret == 0) {
server.sin_family = AF_INET;
server.sin_port = htons(SERVER_PORT);
inet_aton(p->ip, &server.sin_addr);
ret = connect(down_fd, (struct sockaddr *)&server,
sizeof(server));
if (ret < 0) {
perror("connect");
continue;
}
len = sprintf(request,
"GET %s HTTP/1.0\r\nrange: bytes=%ld-%ld\r\n\r\n",
p->file->url,
(long)used + p->offset + 1,
(long)p->offset + p->size - 1);
ret = send(down_fd, request, len, 0);
if (ret < 0) {
perror("send");
goto L1;
}
reconn = 1;
continue;
}
if (reconn == 1) {
get_http_head(down_fd, httpHeadBuff, HTTP_HEAD_LEN);
reconn = 0;
continue;
}
ret = recv(down_fd, p->buff + used, p->size, 0);
if (ret < 0) {
perror("recv_http_data()");
goto L1;
}
p->size -= ret;
used += ret;
if (ret == 0)
break;
}
p->ret_size = used;
L1:
close(down_fd);
return 0;
}
int http_file_load(struct sound_file_info *file, u8 *buff, size_t size)
{
int i;
int ret;
int block_size;
int sum;
pthread_t pth_id[THREAD_NUM];
char ip[INET_ADDRSTRLEN];
const char *p;
int total_ret_size;
struct download_arg arg[THREAD_NUM];
i = 0;
p = file->url + 7;
while (*p != '/')
ip[i++] = *p++;
ip[i] = '\0';
sum = 0;
block_size = size / THREAD_NUM;
for (i = 0; i < THREAD_NUM; i++) {
if (i == THREAD_NUM - 1)
arg[i].size = size - sum;
else
arg[i].size = block_size;
arg[i].ip = ip;
arg[i].file = file;
arg[i].buff = buff + block_size * i;
arg[i].offset = i * block_size + file->offset;
sum += block_size;
ret = pthread_create(&pth_id[i], NULL, download, &arg[i]);
if (ret < 0) {
perror("pthread_create()");
return ret;
}
}
for (i = 0; i < THREAD_NUM; i++) {
ret = pthread_join(pth_id[i], NULL);
if (ret < 0) {
perror("pthread_join()");
return ret;
}
total_ret_size += arg[i].ret_size;
}
return total_ret_size;
}