1
0
Fork 0
mirror of https://gitlab.com/niansa/simpsh-httpd.git synced 2025-03-06 20:53:36 +01:00

Deleted cport because it's garbage

This commit is contained in:
niansa 2020-12-18 22:25:04 +01:00
parent 84f6721954
commit ec3174b204
13 changed files with 0 additions and 537 deletions

View file

@ -1,17 +0,0 @@
# simpsh-httpd C port
Need more performance? Even though this ports memory usage is everything but optimized, it'll probably be a lot more efficient than the original bash version.
## Requirments
* tcc
# Hint
Connection specific configuration option will have to be applied to the config.sh instead of the config.c!
## Installation (dynmic way, but slower)
Copy the main.sh from this directory to the parent directory; changes to configuration will be applied dynamically
## Installation (static way, fastest)
Run `tcc ./main.c -o ../main.sh` from this directory; needs to be done every time configuration is changed
## Uninstallation
Run `git stash` from the parent directory; **this will reset your configuration!!!**

View file

@ -1,32 +0,0 @@
struct databuffer SUUSER;
struct databuffer FILES;
struct databuffer HTMLTITLE;
struct databuffer OUTFILE;
uint PORT;
bool ENABLE_PHP;
bool ENABLE_MARKDOWN;
struct databuffer ERROR404;
struct databuffer ERROR403;
void config() {
// The web server is running under this user (requires to start the socat script as root!). This option may be empty
SUUSER = auto_db("");
// Here is the webroot (homefolder for the website)
FILES = auto_db("/var/www/html/");
// This port will used for listening
PORT = 8888;
// Should PHP be enabled? You need to install php-cli package to use it. May be false or true
ENABLE_PHP = false;
// Should Markdown be enabled? You need to install pandoc package to use it. May be false or true
ENABLE_MARKDOWN = false;
// Error code pages, they need to be HTML documents
ERROR404 = auto_db("/404.html");
ERROR403 = auto_db("/403.html");
}

View file

@ -1,167 +0,0 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mman.h>
struct databuffer {
size_t len;
char *data;
};
void db_init(struct databuffer *db) { // Initialises the db
db->len = 0;
db->data = malloc(1);
db->data[0] = 0x00;
}
void db_deinit(struct databuffer *db) { // Deinitialises the db (TODO)
//free(db->data);
}
const char *db_raw(struct databuffer *db) { // Append temporary null-terminator and returns data
db->data = realloc(db->data, db->len + 1);
db->data[db->len] = 0x00;
return db->data;
}
void db_clear(struct databuffer *db) { // Reinitialises ("clears") the db
db_deinit(db);
db_init(db);
}
void db_append(struct databuffer *db, const struct databuffer *db_src) { // Appends db_src to db
if (db_src->len == 0) {
return;
}
db->len += db_src->len;
db->data = realloc(db->data, db->len);
memcpy(db->data + db->len - db_src->len, db_src->data, db_src->len);
}
void db_set(struct databuffer *db, const struct databuffer *db_src) { // Resets db to db_src
db_clear(db);
db_append(db, db_src);
}
const struct databuffer sized_db(const char *buffer, size_t len) { // Generates sized-size uninitialised (!!!) db
struct databuffer thisdb;
thisdb.data = buffer;
thisdb.len = len;
return thisdb;
}
const struct databuffer auto_db(const char *buffer) { // Same as above but autodetects size
return sized_db(buffer, strlen(buffer));
}
bool db_compare(const struct databuffer *db1, const struct databuffer *db2) { // Compares two dbs
if (db1->len != db2->len) {
return true;
} else {
for (size_t it = 0; it != db1->len; it++) {
if (db1->data[it] != db2->data[it]) {
return false;
}
}
return true;
}
}
ssize_t db_find(const struct databuffer *db, const struct databuffer *db_src) { // Finds db_src in db, returns position or -1
if (db->len < db_src->len) {
return -1;
}
struct databuffer thisdb;
thisdb.len = db_src->len;
for (char *byte = db->data; byte != db->data + db->len - db_src->len + 1; byte++) {
thisdb.data = byte;
if (db_compare(&thisdb, db_src)) {
return byte - db->data;
}
}
return -1;
}
bool db_contains(const struct databuffer *db, const struct databuffer *db_src) { // Checks if db contains db_src
return db_find(db, db_src) != -1;
}
bool db_startswith(const struct databuffer *db, const struct databuffer *db_src) { // Checks if db starts with db_src
return db_find(db, db_src) == 0;
}
bool db_endswith(const struct databuffer *db, const struct databuffer *db_src) { // Checks if db ends with db_src
if (db->len < db_src->len) {
return false;
}
struct databuffer db_aligned;
db_aligned = sized_db(db->data + db->len - db_src->len, db_src->len);
return db_compare(&db_aligned, db_src);
}
bool db_fcheck(FILE *stream) {
if (stream == NULL) {
perror("Couldn't handle file");
return false;
} else {
return true;
}
}
void db_fwrite(const struct databuffer *db, FILE *output) { // writes data from db to output
db_fcheck(output);
fwrite(db->data, db->len, 1, output);
}
void db_fread(struct databuffer *db, FILE *input) { // Reads data from input to db
size_t readbytes;
struct databuffer readdb;
char outb[16];
db_fcheck(input);
while (feof(input) == 0) {
readbytes = fread(outb, 1, sizeof(outb), input);
readdb = sized_db(outb, readbytes);
db_append(db, &readdb);
}
}
void db_readline(struct databuffer *db, FILE *input) { // Appends one line from input to db
struct databuffer thisdb;
char *buffer = NULL;
ssize_t size = 0;
db_fcheck(input);
size = getline(&buffer, &size, input);
assert(size != -1);
thisdb = sized_db(buffer, size);
db_append(db, &thisdb);
}
int runcmd(const char **command, bool wait_exit) { // Dumbly runs command
fflush(stdout);
if (fork() == 0) {
execvp(command[0], command);
perror(command[0]);
}
if (wait_exit) {
int status;
wait(&status);
return status;
} else {
return 0;
}
}
void db_cmdread(struct databuffer *db, const char **command) { // Appends output (stdout) of command to db
struct databuffer thisdb;
int stdout_backup = dup(1);
dup2(memfd_create("pipe", 0), 1);
runcmd(command, true);
FILE *proc_out = fdopen(1, "r");
fseek(proc_out, 0, SEEK_SET);
db_fread(db, proc_out);
close(1);
dup2(stdout_backup, 1);
thisdb = auto_db("\n");
if (db_endswith(db, &thisdb)) {
db->data[db->len - 1] = 0x00;
}
}

View file

@ -1,25 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define uint unsigned int
#define sint signed int
#define uchar unsigned char
#define schar signed char
#define ushort unsigned short
#define sshort signed short
#define ulong unsigned long
#define slong signed long;
#define bool ushort
#define true 1
#define false 0
#include "databuffer.h"
#include "filelib.h"

View file

@ -1,50 +0,0 @@
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
bool file_accessible(const char *pathname, int mode) {
return access(pathname, mode) != -1;
}
bool file_exists(const char *pathname) {
return file_accessible(pathname, F_OK);
}
mode_t file_getmode(const char *pathname) {
struct stat statbuf;
assert(stat(pathname, &statbuf) == 0);
return statbuf.st_mode;
}
bool file_isdir(const char *pathname) {
if (!file_exists(pathname)) {
return false;
}
mode_t filemode = file_getmode(pathname);
return S_ISDIR(filemode);
}
bool file_isregfile(const char *pathname) {
if (!file_exists(pathname)) {
return false;
}
mode_t filemode = file_getmode(pathname);
return S_ISREG(filemode);
}
struct databuffer file_listdir(const char *pathname) { // Returns databuffer with filenames in pathnames seperated by null-terminators
struct databuffer filelist;
struct databuffer filename;
DIR *dir;
struct dirent *entry;
db_init(&filelist);
assert((dir = opendir(pathname)) != NULL);
while ((entry = readdir(dir)) != NULL) {
filename = sized_db(entry->d_name, strlen(entry->d_name) + 1);
db_append(&filelist, &filename);
}
closedir(dir);
return filelist;
}

View file

@ -1,46 +0,0 @@
void filelist() {
struct databuffer thisdb;
struct databuffer thisdb2;
thisdb = auto_db("<html>\n"
" <head>\n"
" <title>Index of: ");
db_append(&outfile, &thisdb);
db_append(&outfile, &url);
thisdb = auto_db("</title>\n"
" </head>\n"
" <body>\n"
" <p>");
db_append(&outfile, &thisdb);
db_append(&outfile, &url);
thisdb = auto_db("</p>\n"
" <h2>Directory list:</h2><br />\n"
" <a href=\"../\">..</a><br />\n");
db_append(&outfile, &thisdb);
struct databuffer filelist = file_listdir(db_raw(&file));
const char *filenameptr = filelist.data;
bool lastwasbegin = false;
for (; filenameptr != filelist.data + filelist.len; filenameptr++) {
if (*filenameptr != 0 && !lastwasbegin) {
if (filenameptr[0] != '.') {
thisdb2 = auto_db(filenameptr);
thisdb = auto_db(" <a href=\"./");
db_append(&outfile, &thisdb);
db_append(&outfile, &thisdb2);
thisdb = auto_db("\">");
db_append(&outfile, &thisdb);
db_append(&outfile, &thisdb2);
thisdb = auto_db("</a><br />\n");
db_append(&outfile, &thisdb);
}
lastwasbegin = true;
} else if (*filenameptr == 0) {
lastwasbegin = false;
}
}
thisdb = auto_db(" </body>\n"
"</html>\n");
db_append(&outfile, &thisdb);
}

View file

@ -1,17 +0,0 @@
void httpheaders() {
struct databuffer thisdb;
// Generation of the HTTP-headers
db_append(&outfile, &status);
thisdb = auto_db("\nDate: ");
db_append(&outfile, &thisdb);
const char *command[] = {"date", NULL};
db_cmdread(&outfile, command);
thisdb = auto_db("\nServer: httpd");
db_append(&outfile, &thisdb);
thisdb = auto_db("\nContent-Type: ");
db_append(&outfile, &thisdb);
db_append(&outfile, &contenttype);
thisdb = auto_db("\n\n");
db_append(&outfile, &thisdb);
}

View file

@ -1,8 +0,0 @@
struct databuffer status;
void init() {
// Default variables
status = auto_db("HTTP/1.0 200 OK");
}

View file

@ -1,52 +0,0 @@
//usr/bin/tcc -run
#include "definitions.h"
struct databuffer outfile;
static bool done = false;
struct databuffer url;
struct databuffer file;
struct databuffer contenttype;
#include "init.c"
#include "config.c"
#include "readrequest.c"
#include "httpheaders.c"
#include "urlcheck.c"
#include "filelist.c"
#include "sendfile.c"
int main() {
db_init(&outfile);
db_init(&url);
db_init(&file);
db_init(&contenttype);
// Variable intialisation
init();
// Read the config file
config();
// Read the HTTP-request
readrequest();
// Some URL checks and modifications
urlcheck();
// Skip following step, if the document was already created successfully
if (!done) {
// Write finished file
sendfile();
}
// Print finished document
db_fwrite(&outfile, stdout);
// Remove the temporary file
db_deinit(&outfile);
return 0;
}

View file

@ -1,4 +0,0 @@
#! /bin/sh
cd cport
exec ./main.c

View file

@ -1,19 +0,0 @@
// TODO: Readd de-escaper
void readrequest() {
struct databuffer thisdb;
// Read the request
char dummybuffer[sizeof("GET")];
fread(dummybuffer, sizeof(dummybuffer), 1, stdin);
db_readline(&url, stdin);
// Process the request
thisdb = auto_db(" ");
ssize_t spacepos = db_find(&url, &thisdb);
assert(spacepos != -1);
url.data[spacepos] = 0x00;
url.len = strlen(url.data);
db_append(&file, &FILES);
db_append(&file, &url);
}

View file

@ -1,34 +0,0 @@
void sendfile() {
struct databuffer thisdb;
struct databuffer thisdb2;
// TODO: Create a Plugin system
// Default content type to text/html
thisdb = auto_db("text/html");
db_set(&contenttype, &thisdb);
// Check: Is it a Directory, a PHP script or something else?
thisdb = auto_db(".php");
thisdb2 = auto_db(".md");
if (db_endswith(&file, &thisdb) && ENABLE_PHP) {
httpheaders();
const char *command[] = {"php", db_raw(&file), NULL};
db_cmdread(&outfile, command);
} else if (db_endswith(&file, &thisdb2) && ENABLE_MARKDOWN) {
httpheaders();
const char *command[] = {"pandoc", "-f", "gfm", db_raw(&file), NULL};
db_cmdread(&outfile, command);
} else if (file_isregfile(db_raw(&file))) {
const char *command[] = {"file", "-b", "-i", db_raw(&file), NULL};
db_clear(&contenttype);
db_cmdread(&contenttype, command);
httpheaders();
FILE *filestream = fopen(db_raw(&file), "rb");
db_fread(&outfile, filestream);
fclose(filestream);
} else {
httpheaders();
filelist();
}
}

View file

@ -1,66 +0,0 @@
void urlcheck() {
struct databuffer thisdb;
struct databuffer thisdb2;
// Block tries to read documents outside the document root
thisdb = auto_db("..");
if (db_contains(&url, &thisdb)) {
thisdb = auto_db("/");
db_set(&url, &thisdb);
db_set(&file, &FILES);
}
// Avoid a bug
if (file_isdir(db_raw(&file))) {
thisdb = auto_db("/");
if (!db_endswith(&url, &thisdb)) {
thisdb2 = auto_db("HTTP/1.0 302 Moved permanently\nLocation: ");
db_set(&status, &thisdb2);
//db_append(&status, &WEBSITE);
db_append(&status, &url);
db_append(&status, &thisdb);
thisdb = auto_db("text/html");
db_set(&contenttype, &thisdb);
httpheaders();
done = true;
}
}
// Find index.html
db_set(&thisdb, &file);
thisdb2 = auto_db("index.html");
db_append(&thisdb, &thisdb2);
if (file_isregfile(thisdb.data)) {
db_append(&url, &thisdb2);
db_append(&file, &thisdb2);
}
// Find index.php
db_set(&thisdb, &file);
thisdb2 = auto_db("index.php");
db_append(&thisdb, &thisdb2);
if (file_isregfile(thisdb.data)) {
db_append(&url, &thisdb2);
db_append(&file, &thisdb2);
}
// Respond 404 if the file doesn't exist
if (!file_exists(db_raw(&file))) {
thisdb = auto_db("HTTP/1.0 404 Not Found");
db_set(&status, &thisdb);
thisdb = auto_db("text/html");
db_set(&contenttype, &thisdb);
db_set(&file, &FILES);
db_append(&file, &ERROR404);
}
// Respond 403 if the file isn't readable
if (!file_accessible(db_raw(&file), R_OK)) {
thisdb = auto_db("HTTP/1.0 403 Forbidden");
db_set(&status, &thisdb);
thisdb = auto_db("text/html");
db_set(&contenttype, &thisdb);
db_set(&file, &FILES);
db_append(&file, &ERROR403);
}
}