Well, if you're looking for a very simple http server, here's one I wrote for an embedded system. Note, it's not stupid-proof (not much error handling), neither performance oriented (uses fork), but it's only a few lines of C code with no library dependency at all. It handles GET and POST http requests. You can statically link it with php if you like.
Code: Select all
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#define __USE_GNU
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include "libs.h" //split(separatorlist,string,index)
#define METHOD_GET 1
#define METHOD_POST 2
char http_method=0;
char *http_req=NULL;
char *http_requesturi=NULL;
char *http_param=NULL;
char *http_module=NULL;
char *http_language=NULL;
char *http_remote=NULL;
int module_argc=0;
char **module_argv=NULL;
extern char *strndup (__const char *__string, size_t __n);
void http_request();
/**************************************************************************
parses http header information
sets http_language and http_param and others
**************************************************************************/
int parse_header(char *header)
{
char *ptr=NULL,*ptr2=NULL;
unsigned int i,j,postlen=0;
if(!strncmp(header,"GET",3)) { http_method=METHOD_GET; header+=4; }
else if(!strncmp(header,"POST",4)) { http_method=METHOD_POST; header+=5; }
else return -1;
while(header[0]==' ')header++;
j=0;while(header[j]!=' '&&header[j]!='\r'&&header[j]!='\n'&&header[j]!=0)j++;
http_requesturi=(char*)strndup(header,j);
ptr2=++header;
//get the browser default (first) language
i=0;while(header[i]!='\r'&&header[i]!='\n'&&header[i]!=0)i++;
for(;i<strlen(header);i++){
if(!strncasecmp(&header[i],"accept-language",15)){
i+=16; while(header[i]==' '||header[i]=='\r'||header[i]=='\n')i++;
if(header[i]!='\n'&&header[i]!='\r'&&header[i]!=0){
j=i; while(header[i]!=';' && header[i]!='\r' && header[i]!=',' && header[i]!='\n' && header[i]!=0)i++;
http_language=(char*)strndup(&header[j],i-j);
}//if got language
} else //if accept language header
if(!strncasecmp(&header[i],"content-length",14)){
i+=15; while(header[i]==' '||header[i]=='\r'||header[i]=='\n')i++;
sscanf(&header[i],"%d",&postlen);
} //if content-length
}//for
if(http_language==NULL) http_language="en";
while(*header!='?' && *header!=' ' && *header!=0) header++;
http_module=split("/? ",ptr2,1);
if(*header=='?') header++;
ptr=header; while(*ptr!='\n' && *ptr!='\r' && *ptr!=0) ptr++;
while(*ptr!=' ') ptr--;
http_param=malloc(ptr-header+1);
memset(http_param,'\0',ptr-header+1);
memcpy(http_param,header,ptr-header);
//if it's a post, we have more arguments waiting
if(http_method==METHOD_POST && postlen>0){
int len=strlen(http_param);
http_param=realloc(http_param,len+1+postlen+1);
if(len!=0) { http_param[len+1]=0; http_param[len]='&'; len++; }
memset(http_param+len,'\0',postlen+1);
i=fread(http_param+len,postlen,1,stdin);
}
return 0;
}
/**************************************************************************
add a key=value pair to arguments
**************************************************************************/
void add_argument(char *name,char *value)
{
if(value==NULL) return;
module_argc++;
module_argv=realloc(module_argv,module_argc*sizeof(char*));
if(name!=NULL){
module_argv[module_argc-1]=malloc(strlen(name)+strlen(value)+2);
sprintf(module_argv[module_argc-1],"%s=%s",name,value);
} else {
module_argv[module_argc-1]=malloc(strlen(value)+1);
module_argv[module_argc-1]=(char*)strndup(value,strlen(value));
}
}
/**************************************************************************
parse url encoded arguments
**************************************************************************/
void parse_arguments(char *param)
{
int i=0,c=0,size=0;
if(param==NULL || param[0]==0) return;
module_argc++;
module_argv=realloc(module_argv,module_argc*sizeof(char*));
module_argv[module_argc-1]=NULL; size=0;
while(param[i]!=0){
if(param[i]=='&'){
i++;
if(!strncasecmp(¶m[i],"amp;",4)) {
i+=4;
module_argv[module_argc-1]=realloc(module_argv[module_argc-1],size+2);
module_argv[module_argc-1][size]='&';
module_argv[module_argc-1][size+1]=0;
size++;
} else {
module_argc++;
module_argv=realloc(module_argv,module_argc*sizeof(char*));
module_argv[module_argc-1]=NULL;
size=0;
}
}
else if(param[i]=='%'){
i++;
sscanf(¶m[i],"%02x",&c);
i+=2;
module_argv[module_argc-1]=realloc(module_argv[module_argc-1],size+2);
module_argv[module_argc-1][size]=(char)c;
module_argv[module_argc-1][size+1]=0;
size++;
}
else if(param[i]!=0){
module_argv[module_argc-1]=realloc(module_argv[module_argc-1],size+2);
module_argv[module_argc-1][size]=param[i]=='+'?' ':param[i];
module_argv[module_argc-1][size+1]=0;
size++;
i++;
}
}//while
}
/**************************************************************************
main function, a mini httpd
**************************************************************************/
void mini_httpd(char *http_listenon)
{
struct sockaddr_in server,connected;
socklen_t length=sizeof server;
fd_set rdfs;
struct hostent *hent;
int port=80;
int webserver_sock,conn_sock=1,val=1;
char *buf;
if(httpd_listenon==NULL) httpd_listenon="0.0.0.0:80";
//get listen parameters
memset(&server,0,length);
buf=split(":",httpd_listenon,1);
if(buf!=NULL) port=atoi(buf);
if(port<1) port=80;
server.sin_port=htons(port);
buf=split(":",httpd_listenon,0);
if(buf==NULL||strcmp(buf,"0.0.0.0")==0){
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
} else {
if((hent=gethostbyname(buf))==(struct hostent *)NULL) {
sys_log('C',"HTTPD: Unable to locate %s", buf);
}
if(hent->h_length>sizeof(hent->h_addr)) {
sys_log('C', "HTTPD: Buffer overflow in gethostbyname()");
}
server.sin_family=hent->h_addrtype;
server.sin_addr.s_addr=((struct in_addr *)(hent->h_addr))->s_addr;
}
free(buf); buf=NULL;
if((webserver_sock=socket(AF_INET,SOCK_STREAM,0))<0){
sys_log('C',"HTTPD: unable to create socket");
}
(void) fcntl( conn_sock, F_SETFD, 1 );
setsockopt(webserver_sock,SOL_SOCKET,SO_REUSEADDR,&val,sizeof(val));
if( bind(webserver_sock,(struct sockaddr *) &server,length)==-1 ||
getsockname(webserver_sock,(struct sockaddr *) &server,&length)==-1 ||
listen(webserver_sock,16)<0){
sys_log('C',"HTTPD: couldn't listen on %s:%d",inet_ntoa(server.sin_addr),port);
}
sys_log('N',"HTTPD: listen on %s",httpd_listenon);
//let's start accepting http connections
while(1){
FD_ZERO(&rdfs);
FD_SET(webserver_sock, &rdfs);
select(webserver_sock+1, &rdfs, NULL, NULL, NULL);
if(FD_ISSET(webserver_sock, &rdfs)) {
conn_sock=accept(webserver_sock,(struct sockaddr *)&connected,&length);
//start a child to handle connection
if(fork()==0){
http_remote=inet_ntoa(connected.sin_addr);
//redirect stdin/stdout to conn_sock
dup2(conn_sock,0);
dup2(conn_sock,1);
close(webserver_sock);
sys_log('D',"HTTPD: connected via minihttpd (%s)",http_remote);
//ok we have a connection now let's read the HTTP request
http_req=realloc(http_req,1);
http_req[0]=0;
while(1){
http_req=realloc(http_req,strlen(http_req)+1025);
memset(http_req+strlen(http_req),0,1025);
//read the HTTP header
if(fgets(&http_req[strlen(http_req)],1024,stdin)!=NULL){
//two empty line means end of header
if(strlen(http_req)>=4 && (
!strncmp(http_req+strlen(http_req)-2,"\n\n",2) ||
!strncmp(http_req+strlen(http_req)-4,"\n\r\n\r",4) ||
!strncmp(http_req+strlen(http_req)-4,"\r\n\r\n",4)
)) break;
} else sys_log('E',"HTTPD: request read problem");
}
//handle the request
parse_header(http_req);
http_request();
//close channel
close(conn_sock);
exit(0);
} else close(conn_sock); //end fork
} //IF input ready
} //endless loop
exit(0);
}
/**************************************************************************
handle a http request, do it as you like
**************************************************************************/
void http_request()
{
//here you can call php and pass variables to it
//just like apache mod_php do.
//http response header
printf("HTTP/1.1 200 OK\r\n");
//parse request params into array
module_argc=0;
module_argv=NULL;
parse_arguments(http_param);
printf("<html><body>called as: %s<br>Arguments:<br>",http_requesturi);
for(i=0;i<module_argc;i++){ printf("%s<br>",module_argv[i]); }
printf("</body></html>");
fflush(stdout);
}