init
commit
2421ea92eb
@ -0,0 +1 @@
|
|||||||
|
test
|
@ -0,0 +1,28 @@
|
|||||||
|
CC ?= gcc
|
||||||
|
target ?= $(shell ${CC} -dumpmachine)
|
||||||
|
CC_FLAGS ?=
|
||||||
|
CC_FLAGS := ${CC_FLAGS} -Ilib
|
||||||
|
DESTDIR ?= /
|
||||||
|
|
||||||
|
.PHONY: all debug clean build-release install
|
||||||
|
all:
|
||||||
|
@$(shell mkdir -p bin)
|
||||||
|
all: main
|
||||||
|
|
||||||
|
main:
|
||||||
|
@${CC} -o bin/httpsrv src/main.c ${CC_FLAGS}
|
||||||
|
|
||||||
|
debug: CC_FLAGS:=-g -O0 -v ${CC_FLAGS}
|
||||||
|
debug: all
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@rm -rf bin
|
||||||
|
|
||||||
|
release: CC_FLAGS:=-O3 ${CC_FLAGS}
|
||||||
|
release: all
|
||||||
|
release:
|
||||||
|
@strip bin/*
|
||||||
|
tar -czf release.tar.gz bin/*
|
||||||
|
|
||||||
|
install:
|
||||||
|
@install -m 777 bin/httpsrv ${DESTDIR}/usr/local/bin
|
Binary file not shown.
@ -0,0 +1 @@
|
|||||||
|
-Ilib
|
@ -0,0 +1,31 @@
|
|||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
char* readFile(char* path) {
|
||||||
|
path++;
|
||||||
|
if (strlen(path) <= 1) {
|
||||||
|
printf("/ converted to /index.html\n");
|
||||||
|
path = (char*)malloc(11);
|
||||||
|
path = "index.html";
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE* fp = fopen(path, "rb");
|
||||||
|
if (!(access(path, F_OK) == 0)) {
|
||||||
|
errno = -1;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
char* buf = NULL;
|
||||||
|
fseek(fp, 0, SEEK_END);
|
||||||
|
int length = ftell(fp);
|
||||||
|
rewind(fp);
|
||||||
|
|
||||||
|
buf = (char*)malloc(sizeof(char) * (length + 1));
|
||||||
|
fread(buf, sizeof(char), length, fp);
|
||||||
|
buf[length] = '\0';
|
||||||
|
|
||||||
|
char* tmp = buf;
|
||||||
|
return tmp;
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char method[16];
|
||||||
|
char path[256];
|
||||||
|
char protocol[10];
|
||||||
|
} ReqHeader;
|
||||||
|
|
||||||
|
ReqHeader readReqHeader(int client) {
|
||||||
|
char buf[1024];
|
||||||
|
read(client, buf, 1024);
|
||||||
|
ReqHeader header;
|
||||||
|
strcpy(header.method, strtok(buf, " "));
|
||||||
|
strcpy(header.path, strtok(NULL, " "));
|
||||||
|
strcpy(header.protocol, strtok(NULL, " "));
|
||||||
|
if (header.path[1] == '\0') {
|
||||||
|
strcpy(header.path, "/index.html");
|
||||||
|
}
|
||||||
|
return header;
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int status;
|
||||||
|
char* notice;
|
||||||
|
char* mime;
|
||||||
|
char* protocol;
|
||||||
|
} ResHeader;
|
||||||
|
|
||||||
|
void makeHeader(char* out, ResHeader* header) {
|
||||||
|
sprintf(out, "%s %d %s\r\nContent-Type: %s\r\n\n", header->protocol, header->status, header->notice, header->mime);
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
#define _GNU_SOURCE
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <getopt.h>
|
||||||
|
|
||||||
|
#include "srv.c"
|
||||||
|
|
||||||
|
#define DEFAULT_ADDR "127.0.0.1"
|
||||||
|
#define DEFAULT_PORT 7677
|
||||||
|
|
||||||
|
void help(char* exe) {
|
||||||
|
char* help = "%s - a simple webserver.\n"
|
||||||
|
" --help: this help message\n"
|
||||||
|
" --addr [addr], -a [addr]: set the ip address to bind to (default: 127.0.0.1)\n"
|
||||||
|
" --port [port], -p [port]: set the port to bind to (default: 7677)\n"
|
||||||
|
" --dir [directory], -d [directory]: set the directory to serve out of (default: the CWD)\n";
|
||||||
|
printf(help, exe);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
char* ADDR = DEFAULT_ADDR;
|
||||||
|
int PORT = DEFAULT_PORT;
|
||||||
|
|
||||||
|
int c;
|
||||||
|
int opt_idx;
|
||||||
|
char* longopt;
|
||||||
|
struct option long_options[] = {
|
||||||
|
{"addr", required_argument, 0, 'a'},
|
||||||
|
{"port", required_argument, 0, 'p'},
|
||||||
|
{"dir", required_argument, 0, 'd'},
|
||||||
|
{"help", no_argument, 0, 'h'},
|
||||||
|
{0, 0, 0, 0 }
|
||||||
|
};
|
||||||
|
while (1) {
|
||||||
|
c = getopt_long(argc, argv, "a:p:d:h", long_options, &opt_idx);
|
||||||
|
|
||||||
|
if (c==-1) break;
|
||||||
|
|
||||||
|
switch (c) {
|
||||||
|
case 0:
|
||||||
|
longopt = (char*)long_options[opt_idx].name;
|
||||||
|
printf("%s\n", longopt);
|
||||||
|
if (!strcmp(longopt, "help")) {
|
||||||
|
help(argv[0]);
|
||||||
|
exit(0);
|
||||||
|
} else if (!strcmp(longopt, "addr")) {
|
||||||
|
ADDR = optarg;
|
||||||
|
} else if (!strcmp(longopt, "port")) {
|
||||||
|
PORT = atoi(optarg);
|
||||||
|
} else if (!strcmp(longopt, "dir")) {
|
||||||
|
chdir(optarg);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'a':
|
||||||
|
ADDR = optarg;
|
||||||
|
break;
|
||||||
|
case 'p':
|
||||||
|
PORT = atoi(optarg);
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
chdir(optarg);
|
||||||
|
break;
|
||||||
|
case 'h':
|
||||||
|
help(argv[0]);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
srv(ADDR, PORT);
|
||||||
|
}
|
@ -0,0 +1,92 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include "resHeader.h"
|
||||||
|
#include "reqHeader.h"
|
||||||
|
#include "file.h"
|
||||||
|
|
||||||
|
int srv(char* ADDR, int PORT) {
|
||||||
|
char buffer[BUFSIZ];
|
||||||
|
int srvSocket;
|
||||||
|
struct sockaddr_in srvAddr;
|
||||||
|
|
||||||
|
srvSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
|
||||||
|
srvAddr.sin_family = AF_INET;
|
||||||
|
srvAddr.sin_port = htons(PORT);
|
||||||
|
srvAddr.sin_addr.s_addr = inet_addr(ADDR);
|
||||||
|
|
||||||
|
if (bind(
|
||||||
|
srvSocket,
|
||||||
|
(struct sockaddr*) &srvAddr,
|
||||||
|
sizeof(srvAddr)
|
||||||
|
) < 0) {
|
||||||
|
printf("Error: Failed to bind to %s:%d.\n", ADDR, PORT);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (listen(srvSocket, 10) < 0) {
|
||||||
|
printf("Error: Unable to listen on %s:%d.\n", ADDR, PORT);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Server (probably) listening on http://%s:%d\n", ADDR, PORT);
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
int client = accept(srvSocket, NULL, NULL);
|
||||||
|
ReqHeader request = readReqHeader(client);
|
||||||
|
char* tPath = request.path;
|
||||||
|
char* dot = strrchr(tPath, '.');
|
||||||
|
|
||||||
|
char *file = readFile(request.path);
|
||||||
|
ResHeader rawHeader;
|
||||||
|
rawHeader.protocol = request.protocol;
|
||||||
|
rawHeader.status = 200;
|
||||||
|
rawHeader.notice = "OK";
|
||||||
|
if (!dot || dot == tPath) { rawHeader.mime = "text/html"; } else {
|
||||||
|
/*switch (dot) {
|
||||||
|
"html":
|
||||||
|
rawHeader.mime = "text/html";
|
||||||
|
"css":
|
||||||
|
rawHeader.mime = "text/css";
|
||||||
|
"js":
|
||||||
|
rawHeader.mime = "text/javascript";
|
||||||
|
default:
|
||||||
|
rawHeader.mime = "text/plain";
|
||||||
|
}*/
|
||||||
|
if (!strcmp(dot, ".html")) {
|
||||||
|
rawHeader.mime = "text/html";
|
||||||
|
} else if (!strcmp(dot, ".css")) {
|
||||||
|
rawHeader.mime = "text/css";
|
||||||
|
} else if (!strcmp(dot, ".ico")) {
|
||||||
|
rawHeader.mime = "image/vnd.microsoft.icon";
|
||||||
|
} else if (!strcmp(dot, ".js")) {
|
||||||
|
rawHeader.mime = "text/javascript";
|
||||||
|
} else {
|
||||||
|
rawHeader.mime = "text/plain";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (errno == -1) {
|
||||||
|
rawHeader.status = 404;
|
||||||
|
rawHeader.notice = "Not Found";
|
||||||
|
} else if (errno > 0) {
|
||||||
|
rawHeader.status = 500;
|
||||||
|
rawHeader.notice = "Internal Server Error";
|
||||||
|
}
|
||||||
|
errno = 0;
|
||||||
|
printf("%d %s\n", rawHeader.status, request.path);
|
||||||
|
char tmpHeader[1024];
|
||||||
|
makeHeader(tmpHeader, &rawHeader);
|
||||||
|
char resHeader[1024]; //= "HTTP/1.1 200 OK\r\n\nHello, world!";
|
||||||
|
sprintf(resHeader, "%s%s", (char*)tmpHeader, file);
|
||||||
|
send(client, resHeader, strlen(resHeader), 0);
|
||||||
|
close(client);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue