move-to-automatic-protocol-list-scheme.patch
[profile/ivi/libwebsockets.git] / test-server / test-server.c
1 /*
2  * libwebsockets-test-server - libwebsockets test implementation
3  * 
4  * Copyright (C) 2010 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <string.h>
27
28 #include "../lib/libwebsockets.h"
29
30 #define LOCAL_RESOURCE_PATH "/usr/share/libwebsockets-test-server"
31 static int port = 7681;
32 static int use_ssl = 0;
33
34 /* this protocol server (always the first one) just knows how to do HTTP */
35
36 static int callback_http(struct libwebsocket * wsi,
37                 enum libwebsocket_callback_reasons reason, void * user,
38                                                            void *in, size_t len)
39 {
40         switch (reason) {
41         case LWS_CALLBACK_HTTP:
42                 fprintf(stderr, "serving HTTP URI %s\n", in);
43                 
44                 if (in && strcmp(in, "/favicon.ico") == 0) {
45                         if (libwebsockets_serve_http_file(wsi,
46                              LOCAL_RESOURCE_PATH"/favicon.ico", "image/x-icon"))
47                                 fprintf(stderr, "Failed to send favicon\n");
48                         break;
49                 }
50                 
51                 /* send the script... when it runs it'll start websockets */
52
53                 if (libwebsockets_serve_http_file(wsi,
54                                   LOCAL_RESOURCE_PATH"/test.html", "text/html"))
55                         fprintf(stderr, "Failed to send HTTP file\n");
56                 break;
57
58         default:
59                 break;
60         }
61
62         return 0;
63 }
64
65 /* dumb_increment protocol */
66
67 struct per_session_data__dumb_increment {
68         int number;
69 };
70
71 static int
72 callback_dumb_increment(struct libwebsocket * wsi,
73                         enum libwebsocket_callback_reasons reason,
74                         void * user, void *in, size_t len)
75 {
76         int n;
77         char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 +
78                                                   LWS_SEND_BUFFER_POST_PADDING];
79         unsigned char *p = (unsigned char *)&buf[LWS_SEND_BUFFER_PRE_PADDING];
80         struct per_session_data__dumb_increment * pss = user;
81         
82         switch (reason) {
83
84         case LWS_CALLBACK_ESTABLISHED:
85                 pss->number = 0;
86                 break;
87
88         case LWS_CALLBACK_SEND: 
89                 n = sprintf(p, "%d", pss->number++);
90                 n = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
91                 if (n < 0) {
92                         fprintf(stderr, "ERROR writing to socket");
93                         return 1;
94                 }
95                 break;
96
97         case LWS_CALLBACK_RECEIVE:
98                 if (len < 6)
99                         break;
100                 if (strcmp(in, "reset\n") == 0)
101                         pss->number = 0;
102                 break;
103
104         default:
105                 break;
106         }
107
108         return 0;
109 }
110
111
112 /* list of supported protocols and callbacks */
113
114 static const struct libwebsocket_protocols protocols[] = {
115         {
116                 .name = "http-only",
117                 .callback = callback_http,
118                 .per_session_data_size = 0,
119         },
120         {
121                 .name = "dumb-increment-protocol",
122                 .callback = callback_dumb_increment,
123                 .per_session_data_size =
124                                 sizeof(struct per_session_data__dumb_increment),
125         },
126         {  /* end of list */
127                 .callback = NULL
128         }
129 };
130
131 static struct option options[] = {
132         { "help",       no_argument, NULL, 'h' },
133         { "port",       required_argument, NULL, 'p' },
134         { "ssl",        no_argument, NULL, 's' },
135         { NULL, 0, 0, 0 }
136 };
137
138 int main(int argc, char **argv)
139 {
140         int n = 0;
141         const char * cert_path =
142                             LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem";
143         const char * key_path =
144                         LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem";
145
146         fprintf(stderr, "libwebsockets test server\n"
147                         "(C) Copyright 2010 Andy Green <andy@warmcat.com> "
148                                                     "licensed under LGPL2.1\n");
149         
150         while (n >= 0) {
151                 n = getopt_long(argc, argv, "hp:", options, NULL);
152                 if (n < 0)
153                         continue;
154                 switch (n) {
155                 case 's':
156                         use_ssl = 1;
157                         break;
158                 case 'p':
159                         port = atoi(optarg);
160                         break;
161                 case 'h':
162                         fprintf(stderr, "Usage: test-server "
163                                              "[--port=<p>] [--ssl]\n");
164                         exit(1);
165                 }
166         }
167
168         if (!use_ssl)
169                 cert_path = key_path = NULL;
170         
171         if (libwebsocket_create_server(port, protocols,
172                                        cert_path, key_path, -1, -1) < 0) {
173                 fprintf(stderr, "libwebsocket init failed\n");
174                 return -1;
175         }
176         
177         /* just sit there until killed */
178                 
179         while (1)
180                 sleep(10);
181
182         return 0;
183 }