-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathclient.c
337 lines (278 loc) · 10.5 KB
/
client.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
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
/*
* Copyright (c) Calin Crisan
* This file is part of streamEye.
*
* streamEye 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 3 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "streameye.h"
#include "common.h"
#include "auth.h"
const char *RESPONSE_BASIC_AUTH_HEADER_TEMPLATE =
"HTTP/1.1 401 Not Authorized\r\n"
"Server: streamEye/%s\r\n"
"Connection: close\r\n"
"WWW-Authenticate: Basic realm=\"%s\"\r\n";
const char *RESPONSE_OK_HEADER_TEMPLATE =
"HTTP/1.1 200 OK\r\n"
"Server: streamEye/%s\r\n"
"Connection: close\r\n"
"Max-Age: 0\r\n"
"Expires: 0\r\n"
"Cache-Control: no-cache, private\r\n"
"Pragma: no-cache\r\n"
"Content-Type: multipart/x-mixed-replace; boundary=" BOUNDARY_SEPARATOR "\r\n";
const char *MULTIPART_HEADER =
"\r\n" BOUNDARY_SEPARATOR "\r\n"
"Content-Type: image/jpeg\r\n"
"Content-Length: ";
static int read_request(client_t *client);
static int write_to_client(client_t *client, char *buf, int size);
static int write_response_ok_header(client_t *client);
static int write_response_auth_basic_header(client_t *client);
static int write_multipart_header(client_t *client, int jpeg_size);
/* client handling */
int read_request(client_t *client) {
char buf[REQ_BUF_LEN];
char *line_end, *header_mid;
char *header_name, *header_value;
char *auth_mode, *auth_basic_hash;
char *strtok_ptr;
int size, found, offs = 0;
memset(buf, 0, REQ_BUF_LEN);
while (running) {
if (offs >= REQ_BUF_LEN) {
ERROR_CLIENT(client, "request header too large");
return -1;
}
size = read(client->stream_fd, buf + offs, REQ_BUF_LEN - offs);
if (size < 0) {
if (errno == EAGAIN) {
ERROR_CLIENT(client, "timeout reading from client");
return -1;
}
else if (errno == EINTR) {
break;
}
else {
ERRNO_CLIENT(client, "read() failed");
return -1;
}
}
else if (size == 0) {
ERROR_CLIENT(client, "connection closed");
return -1;
}
offs += size;
line_end = strstr(buf, "\r\n\r\n");
if (line_end) {
/* two new lines end the request */
line_end[4] = 0;
break;
}
}
DEBUG_CLIENT(client, "received request header");
offs = 0;
while (running && (line_end = strstr(buf + offs, "\r\n"))) {
if (offs == 0) { /* first request line */
found = sscanf(buf, "%9s %1023s %9s", client->method, client->uri, client->http_ver);
if (found != 3) {
ERROR_CLIENT(client, "invalid request line");
return -1;
}
DEBUG_CLIENT(client, "%s %s %s", client->method, client->uri, client->http_ver);
}
else { /* subsequent line, request header */
header_mid = strstr(buf + offs, ": ");
if (header_mid && header_mid < line_end) {
header_name = strndup(buf + offs, header_mid - buf - offs);
header_mid += 1; /* skip ":" */
while (header_mid < line_end && *header_mid == ' ') {
header_mid++; /* skip header value leading spaces */
}
header_value = strndup(header_mid, line_end - header_mid);
if (!strcmp(header_name, "Authorization")) {
auth_mode = strtok_r(header_value, " ", &strtok_ptr);
if (!strcmp(auth_mode, "Basic")) {
DEBUG_CLIENT(client, "authorization header: Basic");
auth_basic_hash = strtok_r(NULL, " ", &strtok_ptr);
if (auth_basic_hash) {
client->auth_basic_hash = strdup(auth_basic_hash);
}
else {
ERROR_CLIENT(client, "missing authorization hash");
}
}
else {
ERROR_CLIENT(client, "unknown authorization header: %s", auth_mode);
}
}
else {
DEBUG_CLIENT(client, "header: %s: %s", header_name, header_value);
}
free(header_name);
free(header_value);
}
}
offs = line_end - buf + 2;
}
DEBUG_CLIENT(client, "request read");
return 0;
}
int write_to_client(client_t *client, char *buf, int size) {
int written = write(client->stream_fd, buf, size);
if (written < 0) {
if (errno == EPIPE || errno == EINTR) {
return 0;
}
else {
ERRNO_CLIENT(client, "write() failed");
return -1;
}
}
else if (written < size) {
ERROR_CLIENT(client, "not all data could be written");
return -1;
}
return written;
}
int write_response_ok_header(client_t *client) {
char *data = malloc(strlen(RESPONSE_OK_HEADER_TEMPLATE) + 16);
sprintf(data, RESPONSE_OK_HEADER_TEMPLATE, STREAM_EYE_VERSION);
int r = write_to_client(client, data, strlen(data));
free(data);
return r;
}
int write_response_auth_basic_header(client_t *client) {
char *realm = get_auth_realm();
char *data = malloc(strlen(RESPONSE_BASIC_AUTH_HEADER_TEMPLATE) + 16 + strlen(realm));
sprintf(data, RESPONSE_BASIC_AUTH_HEADER_TEMPLATE, STREAM_EYE_VERSION, realm);
int r = write_to_client(client, data, strlen(data));
free(data);
return r;
}
int write_multipart_header(client_t *client, int jpeg_size) {
static int multipart_header_len = 0;
if (!multipart_header_len) {
multipart_header_len = strlen(MULTIPART_HEADER);
}
int written = write_to_client(client, (char *) MULTIPART_HEADER, multipart_header_len);
if (written <= 0) {
return written;
}
char size_str[16];
snprintf(size_str, 16, "%d\r\n\r\n", jpeg_size);
return write_to_client(client, size_str, strlen(size_str));
}
void handle_client(client_t *client) {
DEBUG_CLIENT(client, "reading client request");
int result = read_request(client);
if (result < 0) {
ERROR_CLIENT(client, "failed to read client request");
cleanup_client(client);
return;
}
if (get_auth_mode() == AUTH_BASIC) {
if (!client->auth_basic_hash || strcmp(client->auth_basic_hash, get_auth_basic_hash())) {
if (client->auth_basic_hash) {
ERROR_CLIENT(client, "authentication error");
}
else {
DEBUG_CLIENT(client, "authentication required");
}
result = write_response_auth_basic_header(client);
if (result < 0) {
ERROR_CLIENT(client, "failed to write response header");
}
cleanup_client(client);
return;
}
else {
DEBUG_CLIENT(client, "authentication successful");
}
}
DEBUG_CLIENT(client, "writing response header");
result = write_response_ok_header(client);
if (result < 0) {
ERROR_CLIENT(client, "failed to write response header");
cleanup_client(client);
return;
}
client->last_frame_time = get_now();
while (running) {
if (pthread_mutex_lock(&jpeg_mutex)) {
ERROR_CLIENT(client, "pthread_mutex_lock() failed");
break;
}
while (!client->jpeg_ready) {
if (pthread_cond_wait(&jpeg_cond, &jpeg_mutex)) {
ERROR_CLIENT(client, "pthread_mutex_wait() failed");
pthread_mutex_unlock(&jpeg_mutex);
break;
}
}
/* copy the jpeg buffer into the client's temporary buffer,
* but first make sure there's enough space */
if (jpeg_size > client->jpeg_tmp_buf_max_size) {
DEBUG_CLIENT(client, "temporary buffer increased to %d bytes", jpeg_size);
client->jpeg_tmp_buf_max_size = jpeg_size;
client->jpeg_tmp_buf = realloc(client->jpeg_tmp_buf, client->jpeg_tmp_buf_max_size);
}
client->jpeg_tmp_buf_size = jpeg_size;
memcpy(client->jpeg_tmp_buf, jpeg_buf, client->jpeg_tmp_buf_size);
if (pthread_mutex_unlock(&jpeg_mutex)) {
ERROR_CLIENT(client, "pthread_mutex_unlock() failed");
}
double now = get_now();
client->frame_int = client->frame_int * 0.7 + (now - client->last_frame_time) * 0.3;
client->last_frame_time = now;
DEBUG_CLIENT(client, "current fps: %.01lf", 1 / client->frame_int);
/* clear the ready flag for this client */
client->jpeg_ready = 0;
if (!running) {
break; /* speeds up the shut down procedure a bit */
}
DEBUG_CLIENT(client, "writing multipart header");
result = write_multipart_header(client, client->jpeg_tmp_buf_size);
if (result < 0) {
ERROR_CLIENT(client, "failed to write multipart header");
break;
}
else if (result == 0) {
INFO_CLIENT(client, "connection closed");
break;
}
DEBUG_CLIENT(client, "writing jpeg data (%d bytes)", client->jpeg_tmp_buf_size);
result = write_to_client(client, client->jpeg_tmp_buf, client->jpeg_tmp_buf_size);
if (result < 0) {
ERROR_CLIENT(client, "failed to write jpeg data");
break;
}
else if (result == 0) {
INFO_CLIENT(client, "connection closed");
break;
}
}
cleanup_client(client);
}