| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- <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 <stdio.h>
- 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 <stdio.h>
- 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 <stdlib.h>
-
- 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("<HTML>\n");
- printf("<BODY bgcolor='EFEFEF'>\n");
- printf("<form action='/' enctype='multipart/form-data'>\n");
- printf("<input type=file name=image><BR>\n");
- printf("</form>\n");
- if(strlen(ClientInfo->MultiPart("image").data)) {
- printf("%s<BR><img src='/?img=%s.jpg'>\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("</BODY>\n");
- printf("</HTML>\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 <stdio.h>
- #include <string.h>
- 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("<BODY>\n");
- printf("<font color='FF0000'>Access denied</font>\n");
- printf("</BODY>\n");
- return;
- }
- printf("Content-type: text/html\r\n\r\n");
- printf("<BODY bgcolor='EFEFEF'>\n");
- printf("You entered in your area\n");
- printf("</BODY></HTML>\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 <stdio.h>
- #include <math.h>
- #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("<center>Generated a circle (click inside the image)<BR>\n");
- printf("Pressed x=%s,y=%s<BR>\n",ClientInfo->Query("x"),ClientInfo->Query("y"));
- printf("<form><input type=image border=0 src='/gif?img=circle&x=%s&y=%s'></form></CENTER>\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 <stdio.h>
- 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("<form method='POST'>\r\n");
- printf("<input type='text' name='user' value='%s'>\r\n<BR>",ClientInfo->Cookie("username"));
- printf("<input type='submit' name='send' value=' GO! '>\r\n<BR>");
- printf("</form>\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 <stdio.h>
- void checkbox() {
- int i=0;
- char *txt[]={"one","two","three","four","five"};
- printf("Content-type: text/html\r\n\r\n");
- printf("<form method='QUERY'>\r\n");
-
- for(i=0;i<5;i++) {
- printf("<input type='checkbox' name='number' value='%s'>\r\n<BR>",txt[i]);
- };
- printf("<input type='submit' name='send' value=' SEND '>\r\n<BR>");
- printf("</form>\r\n");
-
- printf("You have choosen <font color='FF0000'>%d</font> numbers: \r\n",ClientInfo->Query("#number"));
- for(i=0;i<ClientInfo->Query("#number");i++) {
- printf("<b>%s</b>,\r\n\r\n",ClientInfo->Query("number"));
- };
- printf("...<BR>\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 <stdio.h>
- void confexample() {
- printf("Content-type: text/html\r\n\r\n");
- printf("<PRE>");
- web_client_addfile(server.conffile); // add help.cfg file to output
- printf("</PRE>");
- printf("ClientInfo->Conf(\"PERSONAL_CONF\",\"PORT\")=%s<BR>\n",ClientInfo->Conf("PERSONAL_CONF","PORT"));
- printf("ClientInfo->Conf(\"PERSONAL_CONF\",\"IP\")=%s<BR>\n",ClientInfo->Conf("PERSONAL_CONF","IP"));
- printf("ClientInfo->Conf(\"LIBWEBSERVER\",\"PORT\")=%s<BR>\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>
|