From ec3174b204eefafca75cd4f9608e26f3e8b8ce2e Mon Sep 17 00:00:00 2001 From: niansa Date: Fri, 18 Dec 2020 22:25:04 +0100 Subject: [PATCH] Deleted cport because it's garbage --- cport/README.md | 17 ----- cport/config.c | 32 --------- cport/databuffer.h | 167 -------------------------------------------- cport/definitions.h | 25 ------- cport/filelib.h | 50 ------------- cport/filelist.c | 46 ------------ cport/httpheaders.c | 17 ----- cport/init.c | 8 --- cport/main.c | 52 -------------- cport/main.sh | 4 -- cport/readrequest.c | 19 ----- cport/sendfile.c | 34 --------- cport/urlcheck.c | 66 ----------------- 13 files changed, 537 deletions(-) delete mode 100644 cport/README.md delete mode 100644 cport/config.c delete mode 100644 cport/databuffer.h delete mode 100644 cport/definitions.h delete mode 100644 cport/filelib.h delete mode 100644 cport/filelist.c delete mode 100644 cport/httpheaders.c delete mode 100644 cport/init.c delete mode 100755 cport/main.c delete mode 100755 cport/main.sh delete mode 100644 cport/readrequest.c delete mode 100644 cport/sendfile.c delete mode 100644 cport/urlcheck.c diff --git a/cport/README.md b/cport/README.md deleted file mode 100644 index 925273a..0000000 --- a/cport/README.md +++ /dev/null @@ -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!!!** diff --git a/cport/config.c b/cport/config.c deleted file mode 100644 index 2fae7dd..0000000 --- a/cport/config.c +++ /dev/null @@ -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"); -} diff --git a/cport/databuffer.h b/cport/databuffer.h deleted file mode 100644 index 3f44231..0000000 --- a/cport/databuffer.h +++ /dev/null @@ -1,167 +0,0 @@ -#include -#include -#include -#include - -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; - } -} diff --git a/cport/definitions.h b/cport/definitions.h deleted file mode 100644 index 3375a72..0000000 --- a/cport/definitions.h +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include -#include -#include - - -#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" diff --git a/cport/filelib.h b/cport/filelib.h deleted file mode 100644 index aa14b8b..0000000 --- a/cport/filelib.h +++ /dev/null @@ -1,50 +0,0 @@ -#include -#include -#include -#include -#include - -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; -} diff --git a/cport/filelist.c b/cport/filelist.c deleted file mode 100644 index e559932..0000000 --- a/cport/filelist.c +++ /dev/null @@ -1,46 +0,0 @@ -void filelist() { - struct databuffer thisdb; - struct databuffer thisdb2; - - thisdb = auto_db("\n" - " \n" - " Index of: "); - db_append(&outfile, &thisdb); - db_append(&outfile, &url); - thisdb = auto_db("\n" - " \n" - " \n" - "

"); - db_append(&outfile, &thisdb); - db_append(&outfile, &url); - thisdb = auto_db("

\n" - "

Directory list:


\n" - " ..
\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(" "); - db_append(&outfile, &thisdb); - db_append(&outfile, &thisdb2); - thisdb = auto_db("
\n"); - db_append(&outfile, &thisdb); - } - lastwasbegin = true; - } else if (*filenameptr == 0) { - lastwasbegin = false; - } - } - - thisdb = auto_db(" \n" - "\n"); - db_append(&outfile, &thisdb); -} diff --git a/cport/httpheaders.c b/cport/httpheaders.c deleted file mode 100644 index 1e2f0f0..0000000 --- a/cport/httpheaders.c +++ /dev/null @@ -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); -} diff --git a/cport/init.c b/cport/init.c deleted file mode 100644 index 94d178b..0000000 --- a/cport/init.c +++ /dev/null @@ -1,8 +0,0 @@ -struct databuffer status; - - - -void init() { - // Default variables - status = auto_db("HTTP/1.0 200 OK"); -} diff --git a/cport/main.c b/cport/main.c deleted file mode 100755 index 60bad35..0000000 --- a/cport/main.c +++ /dev/null @@ -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; -} diff --git a/cport/main.sh b/cport/main.sh deleted file mode 100755 index 4f02fde..0000000 --- a/cport/main.sh +++ /dev/null @@ -1,4 +0,0 @@ -#! /bin/sh - -cd cport -exec ./main.c diff --git a/cport/readrequest.c b/cport/readrequest.c deleted file mode 100644 index 4f1eb15..0000000 --- a/cport/readrequest.c +++ /dev/null @@ -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); -} diff --git a/cport/sendfile.c b/cport/sendfile.c deleted file mode 100644 index fa4a7cc..0000000 --- a/cport/sendfile.c +++ /dev/null @@ -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(); - } -} diff --git a/cport/urlcheck.c b/cport/urlcheck.c deleted file mode 100644 index ab383a8..0000000 --- a/cport/urlcheck.c +++ /dev/null @@ -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); - } -}