aboutsummaryrefslogtreecommitdiff
path: root/doc/examples.html
diff options
context:
space:
mode:
Diffstat (limited to 'doc/examples.html')
-rw-r--r--doc/examples.html445
1 files changed, 445 insertions, 0 deletions
diff --git a/doc/examples.html b/doc/examples.html
new file mode 100644
index 00000000..5cd8bbfe
--- /dev/null
+++ b/doc/examples.html
@@ -0,0 +1,445 @@
1<center><H3><font color='007700'>libwebserver examples</font></H3></center><BR><BR>
2
3<!-- Johannes E. Schindelin -->
4<center><A name=helloworld><B><font color='000077'><a href=/hello><H3>Hello World</H3></a></font></B></center>
5<small> <B>used functions:</B><BR>
6<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
7<a href='/?help=functions#web_server_addhandler'>web_server_addhandler</a><BR>
8<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
9</small><BR>
10
11This example starts the server with one handler for all requests pointing to hello_world()
12that prints the content-type with the end of the header "\r\n\r\n" and one simple printf with Hello world<BR><BR>
13
14<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
15<CODE><PRE>
16#include "web_server.h"
17#include &lt;stdio.h&gt;
18
19
20void hello_world() {
21 printf("Content-type: text/plain\r\n\r\n");
22 printf("Hello, World!\r\n");
23}
24
25int main(int argc,char** argv) {
26 struct web_server server; // server handler
27 if(!web_server_init(&server,80,"help.log",0)) { // initialize and start the server at port 80, logging to help.log
28 fprintf(stderr,"can't open listen socket\n");
29 return 1;
30 };
31
32 web_server_addhandler(&server,"* *",hello_world,0); // add handler for all requests
33 while(1) {
34 web_server_run(&server); // run server
35 };
36}
37</PRE></CODE>
38</TD></TR></TABLE>
39<HR><BR>
40
41<center><A name=logfile><B><font color='000077'><a href='/log'><H3>logfile</H3></a></font></B></center>
42<small> <B>used functions:</B><BR>
43<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
44<a href='/?help=functions#web_server_addhandler'>web_server_addhandler</a><BR>
45<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
46<a href='/?help=functions#web_client_addfile'>web_client_addfile</a><BR>
47</small><BR>
48
49This example uses the function <a href='/?help=functions#web_client_addfile'>web_client_addfile</a> to send a file to client
50<BR><BR>
51<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
52<CODE><PRE>
53#include "web_server.h"
54#include &lt;stdio.h&gt;
55
56void logfile() {
57 printf("Content-type: text/plain\r\n\r\n");
58 web_client_addfile(server.logfile); // add help.log file to output
59 printf("End of log\n");
60};
61
62
63main() {
64 struct web_server server; // server handler
65 if(!web_server_init(&server,82,"help.log",0)) { // initializate
66 fprintf(stderr,"can't open listen socket\n");
67 };
68
69 web_server_addhandler(&server,"* /log",logfile,0); // add handler for http://host/log requests
70 while(1) {
71 web_server_run(&server); // run server
72 };
73};
74</PRE></CODE>
75</TD></TR></TABLE>
76<HR><BR>
77
78
79<center><A name=imageup><B><font color='000077'><a href='/image'><H3>Image Uploader</H3></a></font></B></center>
80<small> <B>used functions:</B><BR>
81<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
82<a href='/?help=functions#web_server_addhandler'>web_server_addhandler</a><BR>
83<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
84<a href='/?help=functions#clientinfo'>ClientInfo</a><BR>
85</small><BR>
86
87
88This example uses the struct <a href='/?help=functions#clientinfo'>ClientInfo</a> for fetching the input from the client
89using the Query("img") to send the image <BR>
90and multipart for fetching the uploaded file<BR>
91
92
93<BR><BR>
94<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
95<CODE><PRE>
96#include "web_server.h"
97
98#include &lt;stdlib.h&gt;
99
100struct image {
101 char *data;
102 size_t size;
103} image={NULL,0};
104
105void imageout() {
106 if(strlen(ClientInfo->Query("img"))) {
107 if(image.data!=NULL) {
108 printf("Content-type: image/jpeg\r\n\r\n");
109 fwrite(image.data,image.size,1,stdout);
110 };
111 return;
112 };
113 printf("Content-type: text/html\r\n\r\n");
114 printf("&lt;HTML&gt;\n");
115 printf("&lt;BODY bgcolor='EFEFEF'&gt;\n");
116 printf("&lt;form action='/' enctype='multipart/form-data'&gt;\n");
117 printf("&lt;input type=file name=image&gt;&lt;BR&gt;\n");
118 printf("&lt;/form&gt;\n");
119 if(strlen(ClientInfo->MultiPart("image").data)) {
120 printf("%s&lt;BR&gt;&lt;img src='/?img=%s.jpg'&gt;\n",ClientInfo->MultiPart("image").filename,ClientInfo->MultiPart("image").filename);
121 free(image.data);
122 image.data=malloc(ClientInfo->MultiPart("image").size+1);
123 memcpy(image.data,ClientInfo->MultiPart("image").data,ClientInfo->MultiPart("image").size);
124 image.size=ClientInfo->MultiPart("image").size;
125 }else {
126 free(image.data);
127 image.data=NULL;
128 };
129 printf("&lt;/BODY&gt;\n");
130 printf("&lt;/HTML&gt;\n");
131};
132
133
134
135main() {
136 struct web_server server;
137 if(!web_server_init(&server,80,"teste.log",0)) {
138 fprintf(stderr,"can't open listen socket\n");
139 };
140 web_server_addhandler(&server,"* /",imageout,0);
141 while(1) {
142 web_server_run(&server);
143 };
144};
145</PRE></CODE>
146</TD></TR></TABLE>
147<HR><BR>
148
149
150<center><A name=auth><B><font color='000077'><a href='/auth'><H3>Authentication</H3></a> </font></B></center>
151<small> <B>used functions:</B><BR>
152<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
153<a href='/?help=functions#web_server_addhandler'>web_server_addhandler</a><BR>
154<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
155<a href='/?help=functions#web_client_HTTPdirective'>web_client_HTTPdirective</a><BR>
156</small><BR>
157
158Here we're using the <a href='/?help=functions#web_client_HTTPdirective'>web_client_HTTPdirective</a> to set up the server response
159
160<BR><BR>
161user: "username", pass: "password"
162<BR>
163<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
164<CODE><PRE>
165#include "web_server.h"
166#include &lt;stdio.h&gt;
167#include &lt;string.h&gt;
168
169
170void urlauthenticate() {
171 if(!strlen(ClientInfo->user) || !strlen(ClientInfo->pass) &&
172 strcmp(ClientInfo->user,"username") || strcmp(ClientInfo->pass,"password")) { // you can read things from a auth file
173 web_client_HTTPdirective("HTTP/1.1 401 Authorization Required");
174 printf("WWW-Authenticate: Basic realm=\"This site info\"\r\n");
175 printf("Content-type: text/html\r\n\r\n");
176 printf("&lt;BODY&gt;\n");
177 printf("&lt;font color='FF0000'&gt;Access denied&lt;/font&gt;\n");
178 printf("&lt;/BODY&gt;\n");
179 return;
180 }
181 printf("Content-type: text/html\r\n\r\n");
182 printf("&lt;BODY bgcolor='EFEFEF'&gt;\n");
183 printf("You entered in your area\n");
184 printf("&lt;/BODY&gt;&lt;/HTML&gt;\n");
185};
186
187
188main() {
189 struct web_server server; // server handler
190 if(!web_server_init(&server,83,"help.log",0)) { // initialize
191 fprintf(stderr,"can't open listen socket\n");
192 };
193
194 web_server_addhandler(&server,"* /auth",urlauthenticate,0);
195 while(1) {
196 web_server_run(&server); // run server
197 };
198};
199</PRE></CODE>
200</TD></TR></TABLE>
201<HR><BR>
202
203
204<CENTER><A name=ssl><B><font color='000077'><H3>openssl for (https)</H3></font></B></CENTER>
205<small> <B>used functions:</B><BR>
206<a href='/?help=functions#web_server_HTTPdirective'>web_server_useSSLcert</a><BR>
207<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
208<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
209</small><BR>
210
211Here we setup a server and we use the <a href='/?help=functions#web_server_useSSLcert'>web_server_useSSLcert</a> to use specific certificate file
212and we start the server with the flag WS_USESSL for secure connections (libwebserver compiled w/ openssl)<BR><BR>
213
214See also the packetmounter example in the example directory.
215<BR>
216<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
217<CODE><PRE>
218
219
220#include "web_server.h"
221
222int main()
223 struct web_server serverssl;
224 web_server_useSSLcert(&serverssl,"foo-cert.pem"); // Certificate file
225 if(!web_server_init(&serverssl,443,"help.log",WS_USESSL)) {
226 fprintf(stderr,"Cannot open port\n");
227 };
228 while(1) {
229 web_server_run(&serverssl);
230 };
231};
232</PRE></CODE>
233</TD></TR></TABLE>
234All the rest is the same as without SSL.
235<HR><BR>
236
237
238<center><A name=outgif><B><font color='000077'><a href='/gif'><H3>Gif generator</H3></a></font></B></center>
239<small> <B>used functions;</B><BR>
240<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
241<a href='/?help=functions#web_server_addhandler'>web_server_addhandler</a><BR>
242<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
243<a href='/?help=functions#web_client_gifsetpalette'>web_client_gifsetpalette</a><BR>
244<a href='/?help=functions#web_client_gifoutput'>web_client_gifoutput</a><BR>
245<a href='/?help=functions#clientinfo'>ClientInfo</a><BR>
246</small><BR>
247
248This 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>
249
250
251<BR><BR>
252<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
253<CODE><PRE>
254#include "web_server.h"
255#include &lt;stdio.h&gt;
256#include &lt;math.h&gt;
257
258
259#define GIFSIDE 320
260char gifdata[GIFSIDE*GIFSIDE];
261void outgif() {
262 float i;
263 int x,y,xc,yc;
264 int color;
265 web_client_gifsetpalette("EGA");
266 if(*ClientInfo->Query("img")!=0) {
267 printf("Content-type: image/gif\r\n\r\n");
268 if(!strcmp(ClientInfo->Query("img"),"circle")) {
269 xc=atoi(ClientInfo->Query("x"))%GIFSIDE;
270 yc=atoi(ClientInfo->Query("y"))%GIFSIDE;
271 color=(rand()%15)+1;
272 for(i=0;i<6.28;i+=0.01) {
273 x=(int)(GIFSIDE+(xc+cos(i)*10))%GIFSIDE;
274 y=(int)(GIFSIDE+(yc+sin(i)*10))%GIFSIDE;
275 gifdata[x+(y*GIFSIDE)]=color;
276 };
277 };
278 web_client_gifoutput(gifdata,GIFSIDE,GIFSIDE);
279 };
280 printf("&lt;center&gt;Generated a circle (click inside the image)&lt;BR&gt;\n");
281 printf("Pressed x=%s,y=%s&lt;BR&gt;\n",ClientInfo->Query("x"),ClientInfo->Query("y"));
282 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"));
283};
284
285
286main() {
287 struct web_server server; // server handler
288 memset(gifdata,0,GIFSIDE*GIFSIDE);
289 if(!web_server_init(&server,83,"help.log",0)) { // initialize
290 fprintf(stderr,"can't open listen socket\n");
291 };
292
293 web_server_addhandler(&server,"* /gif",outgif,0);
294 while(1) {
295 web_server_run(&server); // run server
296 };
297};
298</PRE></CODE>
299</TD></TR></TABLE>
300<HR><BR>
301
302
303
304<CENTER><A name=cookie><B><font color='000077'><a href=/cookie><H3>Cookies</H3></a></font></B></CENTER>
305<small> <B>used functions;</B><BR>
306<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
307<a href='/?help=functions#web_server_addhandler'>web_server_addhandler</a><BR>
308<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
309<a href='/?help=functions#clientinfo'>ClientInfo</a><BR>
310<a href='/?help=functions#web_client_setcookie'>web_client_setcookie</a><BR>
311</small><BR>
312
313This 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>
314
315<BR><BR>
316<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
317<CODE><PRE>
318#include "web_server.h"
319#include &lt;stdio.h&gt;
320
321
322void cookie() {
323 if(strlen(ClientInfo->Post("user")))
324 web_client_setcookie("username",ClientInfo->Post("user"),"+15M");
325 printf("Content-type: text/html\r\n\r\n");
326 printf("&lt;form method='POST'&gt;\r\n");
327 printf("&lt;input type='text' name='user' value='%s'&gt;\r\n&lt;BR&gt;",ClientInfo->Cookie("username"));
328 printf("&lt;input type='submit' name='send' value=' GO! '&gt;\r\n&lt;BR&gt;");
329 printf("&lt;/form&gt;\r\n");
330}
331
332int main(int argc,char** argv) {
333 struct web_server server; // server handler
334 if(!web_server_init(&server,80,"help.log",0)) { // initialize
335 fprintf(stderr,"can't open listen socket\n");
336 return 1;
337 };
338
339 web_server_addhandler(&server,"* /*",cookie,0); // add handler for all requests
340 while(1) {
341 web_server_run(&server); // run server
342 };
343}
344
345</PRE></CODE>
346</TD></TR></TABLE>
347<HR><BR>
348
349<center><A name=checkbox><B><font color='000077'><a href=/checkbox><H3>Checkbox</H3></a></font></B></center>
350<small> <B>used functions;</B><BR>
351<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
352<a href='/?help=functions#web_server_addhandler'>web_server_addhandler</a><BR>
353<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
354<a href='/?help=functions#clientinfo'>ClientInfo</a><BR>
355</small><BR>
356
357This 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
358
359
360<BR><BR>
361<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
362<CODE><PRE>
363
364#include "web_server.h"
365#include &lt;stdio.h&gt;
366
367
368void checkbox() {
369 int i=0;
370 char *txt[]={"one","two","three","four","five"};
371 printf("Content-type: text/html\r\n\r\n");
372 printf("&lt;form method='QUERY'&gt;\r\n");
373
374 for(i=0;i<5;i++) {
375 printf("&lt;input type='checkbox' name='number' value='%s'&gt;\r\n&lt;BR&gt;",txt[i]);
376 };
377 printf("&lt;input type='submit' name='send' value=' SEND '&gt;\r\n&lt;BR&gt;");
378 printf("&lt;/form&gt;\r\n");
379
380 printf("You have choosen &lt;font color='FF0000'&gt;%d&lt;/font&gt; numbers: \r\n",ClientInfo->Query("#number"));
381 for(i=0;i&lt;ClientInfo->Query("#number");i++) {
382 printf("&lt;b>%s&lt;/b&gt;,\r\n\r\n",ClientInfo->Query("number"));
383 };
384 printf("...&lt;BR&gt;\r\n\r\n");
385
386}
387int main(int argc,char** argv) {
388 struct web_server server; // server handler
389 if(!web_server_init(&server,80,"help.log",0)) { // initialize
390 fprintf(stderr,"can't open listen socket\n");
391 return 1;
392 };
393
394 web_server_addhandler(&server,"* /*",checkbox,0); // add handler for all requests
395 while(1) {
396 web_server_run(&server); // run server
397 };
398}
399
400</PRE></CODE>
401</TD></TR></TABLE>
402<HR><BR>
403
404<center><A name=confexample><B><font color='000077'><a href=/confexample><H3>Config example</H3></a></font></B></center>
405<small> <B>used functions;</B><BR>
406<a href='/?help=functions#web_server_init'>web_server_init</a><BR>
407<a href='/?help=functions#web_server_addhandler'>web_server_addhandler</a><BR>
408<a href='/?help=functions#web_server_run'>web_server_run</a><BR>
409<a href='/?help=functions#web_client_addfile'>web_client_addfile</a><BR>
410<a href='/?help=functions#clientinfo'>ClientInfo</a><BR>
411</small><BR>
412
413
414<BR><BR>
415<TABLE width='100%' bgcolor='CFCFCF' border=0><TR><TD>
416<CODE><PRE>
417#include "web_server.h"
418#include &lt;stdio.h&gt;
419
420
421void confexample() {
422 printf("Content-type: text/html\r\n\r\n");
423 printf("&lt;PRE&gt;");
424 web_client_addfile(server.conffile); // add help.cfg file to output
425 printf("&lt;/PRE&gt;");
426 printf("ClientInfo->Conf(\"PERSONAL_CONF\",\"PORT\")=%s&lt;BR&gt;\n",ClientInfo->Conf("PERSONAL_CONF","PORT"));
427 printf("ClientInfo->Conf(\"PERSONAL_CONF\",\"IP\")=%s&lt;BR&gt;\n",ClientInfo->Conf("PERSONAL_CONF","IP"));
428 printf("ClientInfo->Conf(\"LIBWEBSERVER\",\"PORT\")=%s&lt;BR&gt;\n",ClientInfo->Conf("LIBWEBSERVER","PORT"));
429
430}
431
432int main(int argc,char** argv) {
433 struct web_server server; // server handler
434 if(!web_server_init(&server,80,"help.cfg",WS_USEEXTCONF)) { // initialize
435 fprintf(stderr,"can't open listen socket\n");
436 return 1;
437 };
438
439 web_server_addhandler(&server,"* *",confexample,0); // add handler for all requests
440 while(1) {
441 web_server_run(&server); // run server
442 };
443}
444</PRE></CODE>
445</TD></TR></TABLE> \ No newline at end of file