Skip to content

Commit

Permalink
xinetd version is integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
aegiryy committed Mar 18, 2011
1 parent f12bc85 commit 878099c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 29 deletions.
8 changes: 6 additions & 2 deletions Makefile
Expand Up @@ -2,16 +2,20 @@ CC=gcc
SRC=source
INC=include
CFLAGS=-I$(INC) -Wall -c
OBJS=server.o static.o resolver.o cgi.o
OBJS=server.o controller.o resolver.o cgi.o
TARGET=server

all: $(OBJS)
$(CC) $(OBJS) -o $(TARGET)

xinetd: $(SRC)/controller.c resolver.o cgi.o
$(CC) $(CFLAGS) -DXINETD $<
$(CC) controller.o resolver.o cgi.o -o $(TARGET)

server.o: $(SRC)/server.c
$(CC) $(CFLAGS) $<

static.o: $(SRC)/static.c
controller.o: $(SRC)/controller.c
$(CC) $(CFLAGS) $<

resolver.o: $(SRC)/resolver.c
Expand Down
14 changes: 13 additions & 1 deletion README
@@ -1 +1,13 @@
it's a project authored by Yuhang GE and me.
HOWTO BUILD:
Actually, there are two versions of this project, one for xinetd and the other for itself.
XINETD:
make xinetd
Individual:
make
You should config it so that it can run via xinetd. For the XINETD version, an extra arg
of root directory should be provided. And when it comes to Individual Version, two extra
args -- port and root directory should be provided.


AUTHOR:
It's a project authored by Yuhang GE and me. And it is released under the GPL v3 License.
2 changes: 1 addition & 1 deletion include/static.h → include/controller.h
@@ -1,6 +1,6 @@
#ifndef _SERVER_H
#define _SERVER_H

int static_serve(char * rootdir);
int serve(char * rootdir);

#endif
2 changes: 1 addition & 1 deletion include/resolver.h
@@ -1,6 +1,6 @@
#ifndef _RESOLVER_H
#define _RESOLVER_H

int resolve(char * req, char * url, char * ext, char * params);
int resolve(char * url, char * ext, char * params);

#endif
26 changes: 24 additions & 2 deletions source/static.c → source/controller.c
Expand Up @@ -7,6 +7,8 @@
#include <ctype.h>
#include <time.h>
#include <sys/stat.h>
#include "resolver.h"
#include "cgi.h"

#define SERVER_NAME "uHTTP"
#define SERVER_URL "https://github.com/aegiryy/uHTTP"
Expand All @@ -23,8 +25,11 @@ static void strdecode(char* to, char* from);
static int hexit(char c);
static void strencode(char* to, size_t tosize, const char* from);


int static_serve(char * rootdir)
#ifdef XINETD
int main(int argc, char * argv[])
#else
int serve(char * rootdir)
#endif
{
char line[10000], method[8], path[1024], protocol[16], idx[20000], location[20000];
char* file;
Expand All @@ -34,13 +39,30 @@ int static_serve(char * rootdir)
FILE* fp;
struct dirent **dl;
int i, n;
int result;
char filepath[1024], ext[8], params[128];
#ifdef XINETD
char * rootdir;
if (argc < 2)
send_error(500, "Internal Error", (char *)0, "Config error - need rootdir specified.");
rootdir = argv[1];
#endif
params[0] = '\0';

if (chdir(rootdir) < 0)
send_error(500, "Internal Error", (char*) 0, "Config error - couldn't chdir().");
if (fgets(line, sizeof(line), stdin) == (char*)0)
send_error(400, "Bad Request", (char*) 0, "No request found.");
if (sscanf(line, "%[^ ] %[^ ] %[^ ]", method, path, protocol) != 3)
send_error(400, "Bad Request", (char*) 0, "Can't parse request.");
if ((result = resolve(path, ext, params)) == 1)
{
filepath[0] = '\0';
strcat(filepath, rootdir);
strcat(filepath, path);
do_cgi(filepath, ext, params);
return 0;
}
while (fgets(line, sizeof(line), stdin) != (char*) 0)
if (strcmp(line, "\n") == 0 || strcmp(line, "\r\n") == 0)
break;
Expand Down
7 changes: 1 addition & 6 deletions source/resolver.c
Expand Up @@ -6,21 +6,16 @@ static char * exts[] = {"php", "py", "sh"};

static int in_exts(char * ext);

int resolve(char * req, char * url, char * ext, char * params)
int resolve(char * url, char * ext, char * params)
{
char method[8], protocol[16];
char * dot;
char * quesmark;
sscanf(req, "%[^ ] %[^ ] %[^ ]", method, url, protocol);
dot = strrchr(url, '.');
quesmark = strrchr(url, '?');
if (dot == NULL)
return 0;
if (quesmark == NULL)
{
params = NULL;
strcpy(ext, dot + 1);
}
else
{
char * andmark;
Expand Down
18 changes: 2 additions & 16 deletions source/server.c
Expand Up @@ -7,9 +7,7 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "static.h"
#include "resolver.h"
#include "cgi.h"
#include "controller.h"

#define BUFFER_SIZE 1024
#define SOCKET_ERROR -1
Expand Down Expand Up @@ -65,13 +63,8 @@ int main(int argc, char* argv[])

for(;;)
{
char url[1024];
char path[1024];
char ext[8];
char params[1024];
int sz_read;
/* get the connected socket */
path[0] = '\0';
hSocket=accept(hServerSocket,(struct sockaddr*)&Address,(socklen_t *)&nAddressSize);
pBuffer[sz_read = read(hSocket,pBuffer,BUFFER_SIZE)] = EOF;
// write(1, pBuffer, sz_read);
Expand All @@ -92,14 +85,7 @@ int main(int argc, char* argv[])
dup(p2c[0]);
close(1);
dup(c2p[1]);
if (resolve(pBuffer, url, ext, params) == 0)
static_serve(ROOT_DIR);
else
{
strcat(path, ROOT_DIR);
strcat(path, url);
do_cgi(path, ext, params);
}
serve(ROOT_DIR);
/* close pipe ends */
close(p2c[0]);
close(c2p[1]);
Expand Down

0 comments on commit 878099c

Please sign in to comment.