Hello World
#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
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
};
}
logfile
#include "web_server.h"
#include <stdio.h>
void logfile() {
printf("Content-type: text/plain\r\n\r\n");
web_client_addfile("help.log"); // 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
};
};
Image Uploader
#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);
};
};
Authentication
user: "username", pass: "password"
#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"
"\r\nWWW-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
};
};
Gif generator
#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;
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_outputgif(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
};
};
Cookies
#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
};
}
Checkbox
#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
};
}