aboutsummaryrefslogtreecommitdiff
path: root/doc/examples.html
blob: 5cd8bbfecceaf34949d9cecbaab9935241f1f7f9 (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
<center><H3><font color='007700'>libwebserver examples</font></H3></center><BR><BR>

<!-- Johannes E. Schindelin -->
<center><A name=helloworld><B><font color='000077'><a href=/hello><H3>Hello World</H3></a></font></B></center>
<small> <B>used functions:</B><BR>
<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
<a href='/?help=functions#web_server_addhandler'>web_server_addhandler</a><BR>
<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
</small><BR>

This example starts the server with one handler for all requests pointing to hello_world()
that prints the content-type with the end of the header "\r\n\r\n" and one simple printf with Hello world<BR><BR>

<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
<CODE><PRE>
#include "web_server.h"
#include &lt;stdio.h&gt;


void hello_world() {
	printf("Content-type: text/plain\r\n\r\n");
	printf("Hello, World!\r\n");
}

int main(int argc,char** argv) {
        struct web_server server; // server handler
        if(!web_server_init(&server,80,"help.log",0)) { // initialize and start the server at port 80, logging to help.log
                fprintf(stderr,"can't open listen socket\n");
		return 1;
        };

        web_server_addhandler(&server,"* *",hello_world,0); // add handler for all requests
        while(1) {
                web_server_run(&server);   // run server
        };
}
</PRE></CODE>
</TD></TR></TABLE>
<HR><BR>

<center><A name=logfile><B><font color='000077'><a href='/log'><H3>logfile</H3></a></font></B></center>
<small> <B>used functions:</B><BR>
<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
<a href='/?help=functions#web_server_addhandler'>web_server_addhandler</a><BR>
<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
<a href='/?help=functions#web_client_addfile'>web_client_addfile</a><BR>
</small><BR>

This example uses the function <a href='/?help=functions#web_client_addfile'>web_client_addfile</a> to send a file to client
<BR><BR>
<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
<CODE><PRE>
#include "web_server.h"
#include &lt;stdio.h&gt;

void logfile() {
        printf("Content-type: text/plain\r\n\r\n");
        web_client_addfile(server.logfile); // add help.log file to output
        printf("End of log\n");
};
        

main() {
        struct web_server server; // server handler
        if(!web_server_init(&server,82,"help.log",0)) { // initializate
                fprintf(stderr,"can't open listen socket\n");
        };

        web_server_addhandler(&server,"* /log",logfile,0); // add handler for http://host/log requests
        while(1) {
                web_server_run(&server);   // run server
        };
};
</PRE></CODE>
</TD></TR></TABLE>
<HR><BR>


<center><A name=imageup><B><font color='000077'><a href='/image'><H3>Image Uploader</H3></a></font></B></center>
<small> <B>used functions:</B><BR>
<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
<a href='/?help=functions#web_server_addhandler'>web_server_addhandler</a><BR>
<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
<a href='/?help=functions#clientinfo'>ClientInfo</a><BR>
</small><BR>


This example uses the struct <a href='/?help=functions#clientinfo'>ClientInfo</a> for fetching the input from the client
using the Query("img") to send the image <BR>
and multipart for fetching the uploaded file<BR>


<BR><BR>
<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
<CODE><PRE>
#include "web_server.h"
                
#include &lt;stdlib.h&gt;
        
struct image {
	char *data;
	size_t size;
} image={NULL,0};

void imageout() {
	if(strlen(ClientInfo->Query("img"))) {
		if(image.data!=NULL) {
			printf("Content-type: image/jpeg\r\n\r\n");
			fwrite(image.data,image.size,1,stdout);
		};
		return;
	};
	printf("Content-type: text/html\r\n\r\n");
	printf("&lt;HTML&gt;\n");
	printf("&lt;BODY bgcolor='EFEFEF'&gt;\n");        
	printf("&lt;form action='/' enctype='multipart/form-data'&gt;\n");
	printf("&lt;input type=file name=image&gt;&lt;BR&gt;\n");
	printf("&lt;/form&gt;\n");
	if(strlen(ClientInfo->MultiPart("image").data)) {
		printf("%s&lt;BR&gt;&lt;img src='/?img=%s.jpg'&gt;\n",ClientInfo->MultiPart("image").filename,ClientInfo->MultiPart("image").filename);
		free(image.data);
		image.data=malloc(ClientInfo->MultiPart("image").size+1);
		memcpy(image.data,ClientInfo->MultiPart("image").data,ClientInfo->MultiPart("image").size);
		image.size=ClientInfo->MultiPart("image").size;
	}else {
		free(image.data);
		image.data=NULL;
	};
	printf("&lt;/BODY&gt;\n");
	printf("&lt;/HTML&gt;\n");
};



main() {
	struct web_server server;
	if(!web_server_init(&server,80,"teste.log",0)) {
		fprintf(stderr,"can't open listen socket\n");
	};
	web_server_addhandler(&server,"* /",imageout,0);
	while(1) {
		web_server_run(&server);
	};
};
</PRE></CODE>
</TD></TR></TABLE>
<HR><BR>


<center><A name=auth><B><font color='000077'><a href='/auth'><H3>Authentication</H3></a> </font></B></center>
<small> <B>used functions:</B><BR>
<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
<a href='/?help=functions#web_server_addhandler'>web_server_addhandler</a><BR>
<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
<a href='/?help=functions#web_client_HTTPdirective'>web_client_HTTPdirective</a><BR>
</small><BR>

Here we're using the <a href='/?help=functions#web_client_HTTPdirective'>web_client_HTTPdirective</a> to set up the server response

<BR><BR>
user: "username", pass: "password"
<BR>
<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
<CODE><PRE>
#include "web_server.h"
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;


void urlauthenticate() {
        if(!strlen(ClientInfo->user) || !strlen(ClientInfo->pass) &&
            strcmp(ClientInfo->user,"username") || strcmp(ClientInfo->pass,"password")) { // you can read things from a auth file
                web_client_HTTPdirective("HTTP/1.1 401 Authorization Required");
                printf("WWW-Authenticate: Basic realm=\"This site info\"\r\n");
                printf("Content-type: text/html\r\n\r\n");
                printf("&lt;BODY&gt;\n");
                printf("&lt;font color='FF0000'&gt;Access denied&lt;/font&gt;\n");
                printf("&lt;/BODY&gt;\n");
                return;
        }
        printf("Content-type: text/html\r\n\r\n");         
        printf("&lt;BODY bgcolor='EFEFEF'&gt;\n");        
        printf("You entered in your area\n");
        printf("&lt;/BODY&gt;&lt;/HTML&gt;\n");
};


main() {
        struct web_server server; // server handler
        if(!web_server_init(&server,83,"help.log",0)) { // initialize
                fprintf(stderr,"can't open listen socket\n");
        };

        web_server_addhandler(&server,"* /auth",urlauthenticate,0);
        while(1) {
                web_server_run(&server);   // run server
        };
};
</PRE></CODE>
</TD></TR></TABLE>
<HR><BR>


<CENTER><A name=ssl><B><font color='000077'><H3>openssl for (https)</H3></font></B></CENTER>
<small> <B>used functions:</B><BR>
<a href='/?help=functions#web_server_HTTPdirective'>web_server_useSSLcert</a><BR>
<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
</small><BR>

Here we setup a server and we use the <a href='/?help=functions#web_server_useSSLcert'>web_server_useSSLcert</a> to use specific certificate file
and we start the server with the flag WS_USESSL for secure connections (libwebserver compiled w/ openssl)<BR><BR>

See also the packetmounter example in the example directory. 
<BR>
<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
<CODE><PRE>

  
#include "web_server.h"

int main()
        struct web_server serverssl;
        web_server_useSSLcert(&serverssl,"foo-cert.pem"); // Certificate file    
        if(!web_server_init(&serverssl,443,"help.log",WS_USESSL)) {
                fprintf(stderr,"Cannot open port\n");
        };
        while(1) {
                web_server_run(&serverssl);
        };
};
</PRE></CODE>
</TD></TR></TABLE>
All the rest is the same as without SSL. 
<HR><BR>


<center><A name=outgif><B><font color='000077'><a href='/gif'><H3>Gif generator</H3></a></font></B></center>
<small> <B>used functions;</B><BR>
<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
<a href='/?help=functions#web_server_addhandler'>web_server_addhandler</a><BR>
<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
<a href='/?help=functions#web_client_gifsetpalette'>web_client_gifsetpalette</a><BR>
<a href='/?help=functions#web_client_gifoutput'>web_client_gifoutput</a><BR>
<a href='/?help=functions#clientinfo'>ClientInfo</a><BR>
</small><BR>

This example draws an circle at x,y requested by client, and outputs with function <a href='/?help=functions#web_client_gifoutput'>web_client_gifoutput</a>


<BR><BR>
<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
<CODE><PRE>
#include "web_server.h"
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;


#define GIFSIDE 320
char gifdata[GIFSIDE*GIFSIDE];
void outgif() {
	float i;
	int x,y,xc,yc;
	int color;
	web_client_gifsetpalette("EGA");
	if(*ClientInfo->Query("img")!=0) {
		printf("Content-type: image/gif\r\n\r\n");
		if(!strcmp(ClientInfo->Query("img"),"circle")) {
			xc=atoi(ClientInfo->Query("x"))%GIFSIDE;
			yc=atoi(ClientInfo->Query("y"))%GIFSIDE;
			color=(rand()%15)+1;
			for(i=0;i<6.28;i+=0.01) {
				x=(int)(GIFSIDE+(xc+cos(i)*10))%GIFSIDE;
				y=(int)(GIFSIDE+(yc+sin(i)*10))%GIFSIDE;
				gifdata[x+(y*GIFSIDE)]=color;
			};
		};
		web_client_gifoutput(gifdata,GIFSIDE,GIFSIDE);
	};
	printf("&lt;center&gt;Generated a circle (click inside the image)&lt;BR&gt;\n");
	printf("Pressed x=%s,y=%s&lt;BR&gt;\n",ClientInfo->Query("x"),ClientInfo->Query("y"));
	printf("&lt;form&gt;&lt;input type=image border=0 src='/gif?img=circle&x=%s&y=%s'&gt;&lt;/form&gt;&lt;/CENTER&gt;\n",ClientInfo->Query("x"),ClientInfo->Query("y"));
};


main() {
	struct web_server server; // server handler
	memset(gifdata,0,GIFSIDE*GIFSIDE);
	if(!web_server_init(&server,83,"help.log",0)) { // initialize
		fprintf(stderr,"can't open listen socket\n");
	};

	web_server_addhandler(&server,"* /gif",outgif,0);
	while(1) {
		web_server_run(&server);   // run server
	};
};
</PRE></CODE>
</TD></TR></TABLE>
<HR><BR>



<CENTER><A name=cookie><B><font color='000077'><a href=/cookie><H3>Cookies</H3></a></font></B></CENTER>
<small> <B>used functions;</B><BR>
<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
<a href='/?help=functions#web_server_addhandler'>web_server_addhandler</a><BR>
<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
<a href='/?help=functions#clientinfo'>ClientInfo</a><BR>
<a href='/?help=functions#web_client_setcookie'>web_client_setcookie</a><BR>
</small><BR>

This example fetchs an client input and set's an cookie for 15 minutes "+15M" using function <a href='/?help=functions#web_client_setcookie'>web_client_setcookie</a>

<BR><BR>
<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
<CODE><PRE>
#include "web_server.h"
#include &lt;stdio.h&gt;


void cookie() {
	if(strlen(ClientInfo->Post("user")))
		web_client_setcookie("username",ClientInfo->Post("user"),"+15M");
	printf("Content-type: text/html\r\n\r\n");
	printf("&lt;form method='POST'&gt;\r\n");
	printf("&lt;input type='text' name='user' value='%s'&gt;\r\n&lt;BR&gt;",ClientInfo->Cookie("username"));
	printf("&lt;input type='submit' name='send' value=' GO! '&gt;\r\n&lt;BR&gt;");
	printf("&lt;/form&gt;\r\n");
}

int main(int argc,char** argv) {
        struct web_server server; // server handler
        if(!web_server_init(&server,80,"help.log",0)) { // initialize
                fprintf(stderr,"can't open listen socket\n");
		return 1;
        };

        web_server_addhandler(&server,"* /*",cookie,0); // add handler for all requests
        while(1) {
                web_server_run(&server);   // run server
        };
}

</PRE></CODE>
</TD></TR></TABLE>
<HR><BR>

<center><A name=checkbox><B><font color='000077'><a href=/checkbox><H3>Checkbox</H3></a></font></B></center>
<small> <B>used functions;</B><BR>
<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
<a href='/?help=functions#web_server_addhandler'>web_server_addhandler</a><BR>
<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
<a href='/?help=functions#clientinfo'>ClientInfo</a><BR>
</small><BR>

This example uses a especific case from <a href='/?help=functions#clientinfo'>ClientInfo</a> query and post, using the '#' as prefix of varname returning the number of occurences


<BR><BR>
<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
<CODE><PRE>

#include "web_server.h"
#include &lt;stdio.h&gt;


void checkbox() {
	int i=0;
	char *txt[]={"one","two","three","four","five"};
	printf("Content-type: text/html\r\n\r\n");
	printf("&lt;form method='QUERY'&gt;\r\n");
	
	for(i=0;i<5;i++) {	
		printf("&lt;input type='checkbox' name='number' value='%s'&gt;\r\n&lt;BR&gt;",txt[i]);	
	};
	printf("&lt;input type='submit' name='send' value=' SEND '&gt;\r\n&lt;BR&gt;");
	printf("&lt;/form&gt;\r\n");
	
	printf("You have choosen &lt;font color='FF0000'&gt;%d&lt;/font&gt; numbers: \r\n",ClientInfo->Query("#number"));
	for(i=0;i&lt;ClientInfo->Query("#number");i++) {	
		printf("&lt;b>%s&lt;/b&gt;,\r\n\r\n",ClientInfo->Query("number"));
	};
	printf("...&lt;BR&gt;\r\n\r\n");
	
}
int main(int argc,char** argv) {
        struct web_server server; // server handler
        if(!web_server_init(&server,80,"help.log",0)) { // initialize
                fprintf(stderr,"can't open listen socket\n");
		return 1;
        };

        web_server_addhandler(&server,"* /*",checkbox,0); // add handler for all requests
        while(1) {
                web_server_run(&server);   // run server
        };
}

</PRE></CODE>
</TD></TR></TABLE>
<HR><BR>

<center><A name=confexample><B><font color='000077'><a href=/confexample><H3>Config example</H3></a></font></B></center>
<small> <B>used functions;</B><BR>
<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
<a href='/?help=functions#web_server_addhandler'>web_server_addhandler</a><BR>
<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
<a href='/?help=functions#web_client_addfile'>web_client_addfile</a><BR>
<a href='/?help=functions#clientinfo'>ClientInfo</a><BR>
</small><BR>


<BR><BR>
<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
<CODE><PRE>
#include "web_server.h"
#include &lt;stdio.h&gt;


void confexample() {
	printf("Content-type: text/html\r\n\r\n");
	printf("&lt;PRE&gt;");
	web_client_addfile(server.conffile); // add help.cfg file to output	
	printf("&lt;/PRE&gt;");
	printf("ClientInfo->Conf(\"PERSONAL_CONF\",\"PORT\")=%s&lt;BR&gt;\n",ClientInfo->Conf("PERSONAL_CONF","PORT"));
	printf("ClientInfo->Conf(\"PERSONAL_CONF\",\"IP\")=%s&lt;BR&gt;\n",ClientInfo->Conf("PERSONAL_CONF","IP"));
	printf("ClientInfo->Conf(\"LIBWEBSERVER\",\"PORT\")=%s&lt;BR&gt;\n",ClientInfo->Conf("LIBWEBSERVER","PORT"));
	
}

int main(int argc,char** argv) {
        struct web_server server; // server handler
        if(!web_server_init(&server,80,"help.cfg",WS_USEEXTCONF)) { // initialize
                fprintf(stderr,"can't open listen socket\n");
		return 1;
        };

        web_server_addhandler(&server,"* *",confexample,0); // add handler for all requests
        while(1) {
                web_server_run(&server);   // run server
        };
}
</PRE></CODE>
</TD></TR></TABLE>