diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..3063e7f
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,25 @@
+CC=cc
+MODS=main.o parse.o socket.o signal.o config.o
+all: httpd
+
+httpd: $(MODS)
+ $(CC) -o httpd $(MODS)
+
+main.o: main.c
+ $(CC) -c main.c
+
+parse.o: parse.c
+ $(CC) -c parse.c
+
+socket.o: socket.c
+ $(CC) -c socket.c
+
+signal.o: signal.c
+ $(CC) -c signal.c
+
+config.o: config.c
+ $(CC) -c config.c
+
+clean:
+ rm -f $(EXAMPLES) *.o
+
diff --git a/config.c b/config.c
new file mode 100644
index 0000000..67fd4a2
--- /dev/null
+++ b/config.c
@@ -0,0 +1,36 @@
+#include "include/httpd.h"
+
+int readconfig(conf *cf, char *file) {
+ char data[512];
+ char *a,*b;
+ struct stat sb;
+ cf[1].port = 80;
+ sprintf(cf[1].servername,"10.0.0.3");
+ if (stat(file,&sb) == -1) {
+ printf("Sorry File Does Not Exist\n");
+ sprintf(file,"ubieserv.cfg");
+ printf("sdf");
+ if (stat(file,&sb) == -1) {
+ printf("Must Specify Config File\n");
+ exit(0);
+ }
+ }
+ printf("la");
+ cff = fopen(file,"r");
+ fgets(data,512,cff);
+ while (!feof(cff)) {
+ printf("%s",data);
+ if (strstr(data," ")) {
+ a = strtok(data, " ");
+ b = strtok(NULL,"\n");
+ if (!strcmp(a,"port")) {
+ cf[1].port = atoi(b);
+ }
+ else if (!strcmp(a,"servername")) {
+ sprintf(cf[1].servername,b);
+ }
+ }
+ fgets(data,512,cff);
+ }
+ return(0);
+ }
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..267b978
--- /dev/null
+++ b/main.c
@@ -0,0 +1,60 @@
+#include "include/httpd.h"
+
+
+int main(int argv,char *argc[]) {
+ int s,i=1,con=0,h;
+ char file[64] = "ubieserver.cfg";
+ hash socket[128];
+ conf cf[1];
+// background(cf);
+// dlopen();
+ dosignal(cf);
+ readconfig(cf,file);
+ printf("Perpairing To Server\n");
+ cf[1].s = serve(cf);
+ printf("Now Servering(%i)\n",cf[1].s);
+ /*
+ while (1) {
+ con = getcon(cf[1].s,con,socket);
+ for (i=1;i<=con;i++) {
+ if (socket[i].socket > 0) {
+ if (sockread(socket[i].socket, socket[i].data)) {
+ parse(i,socket);
+ }
+ }
+ }
+ }
+ */
+ while (1) {
+ con = getcon(cf[1].s,con,socket);
+ if (socket[i].socket > 0) {
+ if (sockread(socket[i].socket, socket[i].data)) {
+ printf("con: %i, Socket: %i,i: %i\n",con,socket[i].socket,i);
+ parse(i,socket);
+ h = write(socket[i].socket,socket[i].data,0);
+ printf("Hmm: %i\n",h);
+ if (h == -1) { socket[i].socket = 0; }
+ }
+ }
+ if ((i >= con) || (con == 0)) { i = 0; }
+ i++;
+ }
+ }
+
+int background()
+{
+ int pid;
+ signal(SIGCHLD,SIG_IGN);
+ pid = fork();
+ if(pid>0)
+ {
+ sleep(1);
+ exit(EXIT_SUCCESS);
+ }
+ if(pid==0)
+ {
+ signal(SIGCHLD,SIG_DFL);
+ return getpid();
+ }
+ return -1;
+}
diff --git a/parse.c b/parse.c
new file mode 100644
index 0000000..7341b7c
--- /dev/null
+++ b/parse.c
@@ -0,0 +1,97 @@
+#include "include/httpd.h"
+
+int parse(int i, hash *socket) {
+ char *a,*b,*c,*d;
+ if (!strcmp(socket[i].data,"\n")) {
+ sendpage(i,socket);
+ printf("Closed Socket\n");
+// close(socket[i].socket);
+// socket[i].socket = 0;
+ return -1;
+ }
+ if (strstr(socket[i].data,"Connection:")) {
+ a = strtok(socket[i].data," ");
+ b = strtok(NULL,"\n");
+ sprintf(socket[i].connection,b);
+ }
+ if (strstr(socket[i].data,"GET")) {
+ a = strtok(socket[i].data, " ");
+ b = strtok(NULL,"\n");
+ if (strstr(b, " ")) {
+ c = strtok(b, " ");
+ d = strtok(NULL, "\n");
+ }
+ if (!strcmp(b,"/")) {
+ sprintf(socket[i].location,"/index.html");
+ }
+ else {
+ sprintf(socket[i].location,b);
+ }
+ sprintf(socket[i].method,a);
+ if (strstr(b,"jpg")) { sprintf(socket[i].contenttype,"image/jpeg"); }
+ else if (strstr(b,"gif")) { sprintf(socket[i].contenttype,"image/gif"); }
+ else { sprintf(socket[i].contenttype,"text/html"); }
+ }
+ return -1;
+ }
+
+
+int sendpage(int i,hash *socket) {
+ int ch,h;
+ char file[128],header[256];
+ char *input;
+ struct stat sb;
+ struct dirent *dp;
+ DIR *d;
+ printf("Sending Page [%i] - File: %s - File2: %c\n", i,socket[i].location,socket[i].location[strlen(socket[i].location)-1]);
+ if (socket[i].location[strlen(socket[i].location)-1] == '/') {
+ printf("Gotcha");
+ socket[i].location[strlen(socket[i].location)-1] = '\0';
+ }
+ sprintf(file, "/usr/home/reddawg/htdocs%s",socket[i].location);
+ if (stat(file,&sb) == -1) {
+ printf("Sorry File Does Not Exist\n");
+ sprintf(file,"/home/reddawg/htdocs/notfound.html");
+ stat(file,&sb);
+ }
+ printf("%ld\n",sb.st_mode);
+ if (sb.st_mode == 16877) {
+ sprintf(header, "HTTP/1.1 200 OK\nServer: UbieServer/0.1 (FreeBSD)\nConnection: close\nContent-Type: text/html\n\n");
+ }
+ else {
+ sprintf(header, "HTTP/1.1 200 OK\nServer: UbieServer/0.1 (FreeBSD)\nAccept-Ranges: bytes\nContent-Length: %ld\n", sb.st_size);
+ sprintf(header,"%sConnection: %s\n",header,socket[i].connection);
+ sprintf(header,"%sContent-Type: %s\n\n",header,socket[i].contenttype);
+ }
+ send_to_sock(socket[i].socket,header);
+ if (sb.st_mode == 16877) {
+ d = opendir(file);
+ while ((dp = readdir(d)) != NULL) {
+ if (!strcmp(dp->d_name,"..")) {
+ sprintf(file,"Parent Directory
\n",socket[i].parentdir);
+ }
+ else if (!strcmp(dp->d_name,".")) { sprintf(file,""); }
+ else {
+ sprintf(file,"%s
\n",socket[i].location,dp->d_name,dp->d_name);
+ }
+ printf("Link: %s\n", file);
+ h = write(socket[i].socket,file,strlen(file));
+ printf("Dir: %i\n", h);
+ }
+ sprintf(socket[i].parentdir,socket[i].location);
+ closedir(d);
+ return(0);
+ }
+ fd = fopen(file,"rb");
+ input = malloc(1);
+ printf("Start Sending %s\n",file);
+ while((ch = fgetc(fd)) != EOF) {
+ sprintf(input, "%c", ch);
+ write(socket[i].socket,input,1);
+ }
+ printf("[%i]",strlen(input));
+ write(socket[i].socket,input,strlen(input));
+
+ printf("Done Sending\n");
+ return 0;
+ }
diff --git a/signal.c b/signal.c
new file mode 100644
index 0000000..0e77007
--- /dev/null
+++ b/signal.c
@@ -0,0 +1,23 @@
+#include "include/httpd.h"
+
+void SignalHUP(int);
+
+int dosignal(conf *ch) {
+ signal(SIGPIPE,SIG_IGN);
+// signal(SIGSEGV,SIG_IGN);
+ Signal_Handler(ch);
+ }
+
+int Signal_Handler (conf *ch) {
+ struct sigaction sa;
+ sa.sa_handler = SignalHUP;
+ sa.sa_flags = 1;
+ sigemptyset(&sa.sa_mask);
+ sigaction(1, &sa, (struct sigaction *)0);
+ return 0;
+}
+
+
+void SignalHUP(int x) {
+ printf("Got a SIGHUP - %i\n",x);
+ }
diff --git a/socket.c b/socket.c
new file mode 100644
index 0000000..8a726db
--- /dev/null
+++ b/socket.c
@@ -0,0 +1,159 @@
+#include "include/httpd.h"
+#include "include/socket.h"
+
+
+int getcon(int s,int y,hash *socket) {
+ int size,g,one=0;
+ struct sockaddr_in remote;
+
+ int bufnum, n;
+ fd_set fds;
+ struct timeval wait;
+
+ bufnum = 0;
+ if (y == 128) { y = 1; }
+ size = sizeof(struct sockaddr_in);
+
+ if (s==-1)
+ {
+ return(-1);
+ }
+ wait.tv_sec = 0L;
+ wait.tv_usec = 2500L;
+ FD_ZERO(&fds);
+ FD_SET(s, &fds);
+ if (select(s+1, &fds, NULL, 0, &wait) > 0)
+ {
+ y++;
+ if ((socket[y].socket = accept (s, (struct sockaddr *)&remote, &size))== -1) {
+ perror ("accept");
+ exit(1);
+ }
+ if (setsockopt(socket[y].socket, SOL_SOCKET, SO_KEEPALIVE, (char *) &one,sizeof(int)) < 0) {
+ perror("setsockopt");
+ exit(1);
+ }
+ getpeername(socket[y].socket,(struct sockaddr *)&remote,&size);
+ sprintf(socket[y].peername,inet_ntoa(remote.sin_addr));
+ printf("Con Sock ID: %i\n",socket[y].socket);
+ }
+ return y;
+ }
+
+int serve(conf *cf) {
+ int s,one=1;
+ struct sockaddr_in local;
+ struct linger li;
+ local.sin_family = AF_INET;
+ local.sin_port = htons (cf[1].port);
+ local.sin_addr.s_addr = get_ip(cf[1].servername);
+ li.l_onoff = 1;
+ li.l_linger = 30;
+
+ if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
+ perror("socket");
+ exit(1);
+ }
+/*
+ if (setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(struct linger)) < 0) {
+ perror("setsockopt");
+ exit(1);
+ }
+*/
+ if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(int)) < 0) {
+ perror("setsockopt");
+ exit(1);
+ }
+
+ if (bind (s, (struct sockaddr *)&local, sizeof(struct sockaddr)) == -1) {
+ perror("bind");
+ exit(1);
+ }
+ if (listen(s, 5) == -1) {
+ perror("listen");
+ exit(1);
+ }
+ return s;
+ }
+
+int sockread(int s,char *buf) {
+ char inc;
+ int bufnum, n;
+ fd_set fds;
+ struct timeval wait;
+ bufnum = 0;
+ if (s==-1) {
+ return(-1);
+ }
+ wait.tv_sec = 0L;
+ wait.tv_usec = 2500L;
+ FD_ZERO(&fds);
+ FD_SET(s, &fds);
+ if (select(s+1, &fds, NULL, 0, &wait) > 0) {
+ do {
+ n = recv(s, &inc, 1,0);
+ if (n == 0) {
+ return -1;
+ }
+ if (bufnum < BUFFER_SIZE - 1 ) {
+ if (inc != 13) {
+ buf[bufnum++] = inc;
+ }
+ }
+ }
+ while (inc != '\n');
+ {
+ buf[bufnum] = '\0';
+ }
+ brx += bufnum;
+ return bufnum;
+ }
+ return 0;
+ }
+
+
+int get_ip(char *host) {
+ struct hostent *hp;
+ struct in_addr *in;
+ int ip;
+ if ((ip = inet_addr(host)) == -1) {
+ hp = gethostbyname(host);
+ if (!hp)
+ return(-1);
+ in = (struct in_addr*)(hp->h_addr_list[0]);
+ ip = (int)(in->s_addr);
+ return(ip);
+ }
+ return(ip);
+ }
+
+
+//int send_to_sock(int socket, const char *fmt, ...) {
+int send_to_sock(int s,char *string) {
+/*
+ char *string;
+ va_list args;
+ string = malloc(MAXBUFLENGTH);
+ va_start(args, fmt);
+ vsnprintf(string, MAXBUFLENGTH, fmt, args);
+ va_end(args);
+*/
+/*
+ int bufnum, n;
+ fd_set fds;
+ struct timeval wait;
+ bufnum = 0;
+ if (s==-1) {
+ return(-1);
+ }
+ wait.tv_sec = 0L;
+ wait.tv_usec = 2500L;
+ FD_ZERO(&fds);
+ FD_SET(s, &fds);
+ if (select(s+1, NULL, &fds, 0, &wait) > 0) {
+*/
+ send(s, string, strlen(string), 0);
+ //write(s,string,strlen(string));
+ // }
+ return(strlen(string));
+ }