examples.html 15 KB

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