Added dynamic pages with dynamic linking during runtime

This commit is contained in:
Nate Choe
2022-02-14 12:08:09 -06:00
parent 8df741a078
commit 6825f12163
28 changed files with 435 additions and 84 deletions

View File

@@ -0,0 +1,62 @@
# Part 1: The problem
A static webpage is easy. Just tell swebs to read a few files given a few http paths and you're done. Dyanmic pages are harder. There are a few solutions, you could execute a program and return stdout (too slow), encourage each website to create their own forks of swebs and make it really easy to directly modify the source code to add a dynamic page (inelegant), write a php interpreter (bloated), execute a program the user creates and keep it running while the web server communicates with it (what's the point of writing a web server if the user does all the work of setting up a server and responding to requests?)
# Part 2: The solution
The solution I thought of was to dynamically load a C library the user optionally writes that creates pages. The user is responsible for writing C code that generates pages, and swebs is responsible for parsing requests and asking for said pages. The library the user creates must be a shared object file, and define this function:
```int getResponse(Request *request, Response *response)```
```getResponse``` returns the HTTP response code of that request.
```Request``` and ```Response``` are defined in ```<swebs/types.h>```. ```getResponse()```. ```<swebs/types.h>``` is guarunteed to be included by ```<swebs/swebs.h>```, where ```getResponse()``` is defined.
The specific library to use is set with the ```library``` global variable in a sitefile.
The various data types important to you in this scenario are:
```
typedef struct {
char *field;
char *value;
} Field;
/*HTTP field*/
typedef struct {
long fieldCount;
Field *fields;
} Request;
/*HTTP request, pretty self explanatory*/
typedef enum {
FILE_KNOWN_LENGTH,
/* A file where the total length is known (i.e. a file on disk) */
FILE_UNKNOWN_LENGTH,
/* A file where the total length is unknown (i.e. a pipe) */
BUFFER,
/* A buffer stored in memory. free() will be called on said buffer. */
DEFAULT
/* The default response for the response code */
} ResponseType;
typedef struct {
int fd;
size_t len;
/* This field is sometimes optional */
} File;
typedef struct {
void *data;
/* This data will be freed. */
size_t len;
} Buffer;
typedef struct {
ResponseType type;
union {
File file;
Buffer buffer;
} response;
} Response;
```

View File

@@ -28,9 +28,11 @@ sitefiles also allow comments with #
# Part 4: Global variables
* ```port``` - the port to use. Note that this is a global variable, and so one instance of swebs cannot use multiple ports.
* ```transport``` - the type of connection to use. One of:
* TCP (default)
* TLS
* ```key``` - The filepath of the private key to use if transport == TLS
* ```cert``` - The filepath of the certificate to use if transport == TLS
* ```timeout``` - The amount of time to wait for data before closing the connection in ms
* ```library``` - the path of a library that is linked in during runtime if ```DYNAMIC_LINKED_PAGES```is set.