aboutsummaryrefslogtreecommitdiff
path: root/src/examples/spdy_event_loop.c
blob: 3b6f9c828971bc0bba7742faca4f3168ec97b760 (plain) (blame)
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
    This file is part of libmicrospdy
    Copyright (C) 2012 Andrey Uzunov

    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 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/>.
*/

/**
 * @file event_loop.c
 * @brief  shows how to use the daemon. THIS IS MAINLY A TEST AND DEBUG
 * 		 PROGRAM
 * @author Andrey Uzunov
 */
 
#include "platform.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include "microspdy.h"
#include <sys/time.h>
#include <time.h>
#ifndef MINGW
#include <arpa/inet.h>
#endif
//#include "../framinglayer/structures.h"
//#include "../applicationlayer/alstructures.h"

static int run = 1;

static int run2 = 1;

	
static uint64_t loops;

static time_t start;


static void
new_session_callback (void *cls,
						struct SPDY_Session * session)
{
	char ipstr[1024];
		
	struct sockaddr *addr;
	socklen_t addr_len = SPDY_get_remote_addr(session, &addr);	
	
	if(!addr_len)
	{
		printf("SPDY_get_remote_addr");
		abort();
	}
	
	if(AF_INET == addr->sa_family)
	{
		struct sockaddr_in * addr4 = (struct sockaddr_in *) addr;
		if(NULL == inet_ntop(AF_INET, &(addr4->sin_addr), ipstr, sizeof(ipstr)))
		{
			printf("inet_ntop");
			abort();
		}
		printf("New connection from: %s:%i\n", ipstr, ntohs(addr4->sin_port));
		
	}
	else if(AF_INET6 == addr->sa_family)
	{
		struct sockaddr_in6 * addr6 = (struct sockaddr_in6 *) addr;
		if(NULL == inet_ntop(AF_INET6, &(addr6->sin6_addr), ipstr, sizeof(ipstr)))
		{
			printf("inet_ntop");
			abort();
		}
		printf("New connection from: %s:%i\n", ipstr, ntohs(addr6->sin6_port));
		
	}
}


static void
session_closed_handler (void *cls,
						struct SPDY_Session * session,
						int by_client)
{
	//printf("session_closed_handler called\n");
	
	if(SPDY_YES != by_client)
	{
		//killchild(child,"wrong by_client");
		printf("session closed by server\n");
	}
	else
	{
		printf("session closed by client\n");
	}
	
	//session_closed_called = 1;
}


static void
response_done_callback(void *cls,
						struct SPDY_Response *response,
						struct SPDY_Request *request,
						enum SPDY_RESPONSE_RESULT status,
						bool streamopened)
{
	(void)streamopened;
	if(strcmp(cls, "/close (daemon1)") == 0)
		run = 0;
	else {
		if(strcmp(cls, "/close (daemon2)") == 0) run2 = 0;
		loops = 0;
		start = time(NULL);
	}
	if(SPDY_RESPONSE_RESULT_SUCCESS != status)
	{
		printf("not sent frame cause %i", status);
	}
	printf("answer for %s was sent\n", (char*)cls);
	//printf("raw sent headers %s\n", (char *)(response->headers)+8);
	
	SPDY_destroy_request(request);
	SPDY_destroy_response(response);
	free(cls);
}


static int
print_headers (void *cls,
                           const char *name, const char *value)
{
	(void)cls;
	printf("%s: %s\n",name,value);
	return SPDY_YES;
}
 
/*       
void
new_request_cb (void *cls,
						struct SPDY_Request * request,
						uint8_t priority,
                        const char *method,
                        const char *path,
                        const char *version,
                        const char *host,
                        const char *scheme,
						struct SPDY_NameValue * headers)
{
	(void)cls;
	(void)request;
	printf("Priority: %i\nHTTP headers, scheme: %s\n\n%s %s %s\nHost: %s\n", priority,scheme,method,path,version,host);
	SPDY_name_value_iterate(headers, &print_headers, NULL);
}
*/


static int
append_headers_to_data (void *cls,
                           const char *name, const char * const *value, int num_values)
{
	char **data = cls;
	void *tofree = *data;
	int i;
	
	if(num_values)
	for(i=0;i<num_values;++i)
	{
	asprintf(data,"%s%s: %s\n", *data,name,value[i]);
	}
	else
	asprintf(data,"%s%s: \n", *data,name);
	
	free(tofree);
	return SPDY_YES;
}  


static void
standard_request_handler(void *cls,
						struct SPDY_Request * request,
						uint8_t priority,
                        const char *method,
                        const char *path,
                        const char *version,
                        const char *host,
                        const char *scheme,
						struct SPDY_NameValue * headers,
            bool more)
{
	char *html;
	char *data;
	struct SPDY_Response *response=NULL;
	
	printf("received request for '%s %s %s'\n", method, path, version);
	if(strcmp(path,"/main.css")==0)
	{
		if(NULL != cls)
			asprintf(&html,"body{color:green;}");
		else
			asprintf(&html,"body{color:red;}");
				
		//struct SPDY_NameValue *headers=SPDY_name_value_create();
		//SPDY_name_value_add(headers,"content-type","text/css");
		
		response = SPDY_build_response(200,NULL,SPDY_HTTP_VERSION_1_1,NULL,html,strlen(html));
		free(html);
	}
	else
	{
		asprintf(&data,"Priority: %i\nHTTP headers, scheme: %s\n\n%s %s %s\nHost: %s\n", priority,scheme,method,path,version,host);
		
		SPDY_name_value_iterate(headers, &append_headers_to_data, &data);
		
		if(strcmp(path,"/close")==0)
		{
			asprintf(&html,"<html>"
		"<body><b>Closing now!<br>This is an answer to the following "
		"request:</b><br><br><pre>%s</pre></body></html>",data);
		}
		else
		{
			asprintf(&html,"<html><link href=\"main.css\" rel=\"stylesheet\" type=\"text/css\" />"
		"<body><b>This is an answer to the following "
		"request:</b><br><br><pre>%s</pre></body></html>",data);
		}
		
		free(data);
		
		response = SPDY_build_response(200,NULL,SPDY_HTTP_VERSION_1_1,NULL,html,strlen(html));
		free(html);
	}
	
	if(NULL==response){
		fprintf(stdout,"no response obj\n");
		abort();
	}
	
	char *pathcls;
	asprintf(&pathcls, "%s (daemon%i)",path,NULL==cls ? 1 : 2);
	if(SPDY_queue_response(request,response,true,false,&response_done_callback,pathcls)!=SPDY_YES)
	{
		fprintf(stdout,"queue\n");
		abort();
	}
}


static int
new_post_data_cb (void * cls,
					 struct SPDY_Request *request,
					 const void * buf,
					 size_t size,
					 bool more)
{
	printf("DATA:\n===============================\n");
  write(0, buf, size);
	printf("\n===============================\n");
  return SPDY_YES;
}


static void 
sig_handler(int signo)
{
    printf("received signal\n");
}


int
main (int argc, char *const *argv)
{	
	if(argc != 2) return 1;
	
	#ifndef MINGW
	if (signal(SIGPIPE, sig_handler) == SIG_ERR)
		printf("\ncan't catch SIGPIPE\n");
	#endif
	
	SPDY_init();
	
	struct sockaddr_in addr4;
	struct in_addr inaddr4;
	inaddr4.s_addr = htonl(INADDR_ANY);
	addr4.sin_family = AF_INET;
	addr4.sin_addr = inaddr4;
	addr4.sin_port = htons(atoi(argv[1]));
	
	struct SPDY_Daemon *daemon = SPDY_start_daemon(atoi(argv[1]),
	 DATA_DIR "cert-and-key.pem",
	 DATA_DIR "cert-and-key.pem",
	&new_session_callback,&session_closed_handler,&standard_request_handler,&new_post_data_cb,NULL,
	SPDY_DAEMON_OPTION_SESSION_TIMEOUT, 10,
	//SPDY_DAEMON_OPTION_SOCK_ADDR,  (struct sockaddr *)&addr4,
	SPDY_DAEMON_OPTION_END);
	
	if(NULL==daemon){
		printf("no daemon\n");
		return 1;
	}
	
	struct sockaddr_in6 addr6;
	addr6.sin6_family = AF_INET6;
	addr6.sin6_addr = in6addr_any;
	addr6.sin6_port = htons(atoi(argv[1]) + 1);
	
	struct SPDY_Daemon *daemon2 = SPDY_start_daemon(atoi(argv[1]) + 1,
	 DATA_DIR "cert-and-key.pem",
	 DATA_DIR "cert-and-key.pem",
	&new_session_callback,NULL,&standard_request_handler,&new_post_data_cb,&main,
	//SPDY_DAEMON_OPTION_SESSION_TIMEOUT, 0,
	//SPDY_DAEMON_OPTION_SOCK_ADDR,  (struct sockaddr *)&addr6,
	//SPDY_DAEMON_OPTION_FLAGS, SPDY_DAEMON_FLAG_ONLY_IPV6,
	SPDY_DAEMON_OPTION_END);
	
	if(NULL==daemon2){
		printf("no daemon\n");
		return 1;
	}
	
	do
	{
	unsigned long long timeoutlong=0;
	struct timeval timeout;
	volatile int rc; /* select() return code */ 
	volatile int ret;

	fd_set read_fd_set;
	fd_set write_fd_set;
	fd_set except_fd_set;
	int maxfd = -1;

	if(run && daemon != NULL)
	{
		loops++;
		FD_ZERO(&read_fd_set);
		FD_ZERO(&write_fd_set);
		FD_ZERO(&except_fd_set);

		ret = SPDY_get_timeout(daemon, &timeoutlong);
		if(SPDY_NO == ret || timeoutlong > 1000)
		{
			timeout.tv_sec = 1;
      timeout.tv_usec = 0;
		}
		else
		{
			timeout.tv_sec = timeoutlong / 1000;
			timeout.tv_usec = (timeoutlong % 1000) * 1000;
		}
    
		printf("ret=%i; timeoutlong=%i; sec=%i; usec=%i\n", ret, timeoutlong, timeout.tv_sec, timeout.tv_usec);
		//raise(SIGINT);

		/* get file descriptors from the transfers */ 
		maxfd = SPDY_get_fdset (daemon,
		&read_fd_set,
		&write_fd_set, 
		&except_fd_set);

//struct timeval ts1,ts2;
    //gettimeofday(&ts1, NULL);
		rc = select(maxfd+1, &read_fd_set, &write_fd_set, &except_fd_set, &timeout);
    //gettimeofday(&ts2, NULL);
    printf("rc %i\n",rc);
   // printf("time for select %i\n",ts2.tv_usec - ts1.tv_usec);
   // printf("%i %i %i %i\n",ts1.tv_sec, ts1.tv_usec,ts2.tv_sec, ts2.tv_usec);

		switch(rc) {
			case -1:
				/* select error */ 
				break;
			case 0:

				break;
			default:
				SPDY_run(daemon);

			break;
		}
	}
	else if(daemon != NULL){
	
	printf("%i loops in %i secs\n", loops, time(NULL) - start);
		SPDY_stop_daemon(daemon);
		daemon=NULL;
	}

	if(run2)
	{
		FD_ZERO(&read_fd_set);
		FD_ZERO(&write_fd_set);
		FD_ZERO(&except_fd_set);

		ret = SPDY_get_timeout(daemon2, &timeoutlong);
		//printf("tout %i\n",timeoutlong);
		if(SPDY_NO == ret || timeoutlong > 1)
		{ 
			//do sth else
			//sleep(1);

			//try new connection
			timeout.tv_sec = 1;
			timeout.tv_usec = 0;
		}
		else
		{
			timeout.tv_sec = timeoutlong;
			timeout.tv_usec = 0;//(timeoutlong % 1000) * 1000;
		}

		//printf("ret=%i; timeoutlong=%i; sec=%i; usec=%i\n", ret, timeoutlong, timeout.tv_sec, timeout.tv_usec);
		//raise(SIGINT);

		/* get file descriptors from the transfers */ 
		maxfd = SPDY_get_fdset (daemon2,
		&read_fd_set,
		&write_fd_set, 
		&except_fd_set);

		rc = select(maxfd+1, &read_fd_set, &write_fd_set, &except_fd_set, &timeout);

		switch(rc) {
			case -1:
				/* select error */ 
				break;
			case 0:

				break;
			default:
				SPDY_run(daemon2);

				break;
		}
	}
	else if(daemon2 != NULL){
		SPDY_stop_daemon(daemon2);
		daemon2=NULL;
	}
	}
	while(run || run2);

	if(daemon != NULL){
		SPDY_stop_daemon(daemon);
	}
	if(daemon2 != NULL){
		SPDY_stop_daemon(daemon2);
	}
	
	SPDY_deinit();
	
	return 0;
}