Implemented regex and directories

This commit is contained in:
Nate Choe
2022-01-22 21:20:57 -06:00
parent fbb3a4e8cd
commit be8e081cca
4 changed files with 69 additions and 21 deletions

View File

@@ -12,7 +12,7 @@ sitefiles also allow comments with #
# Part 2: Commands # Part 2: Commands
* ```set [variable] [value]``` - sets some variable for the following pages * ```set [variable] [value]``` - sets some variable for the following pages
* ```read [http path] [file path]``` - if the requested path matches ```[http path]```, return the contents of ```[file path]``` * ```read [http path] [file path]``` - if the requested path matches ```[http path]```, return the contents of ```[file path]```. If [file path] is a directory, then the http path is appended to [file path] and that is read instead.
##### Other than set, commands should take in a regex as argument 1 and operate on a file specified in argument 2. ##### Other than set, commands should take in a regex as argument 1 and operate on a file specified in argument 2.

View File

@@ -17,6 +17,8 @@
*/ */
#ifndef _HAVE_SITEFILE #ifndef _HAVE_SITEFILE
#define _HAVE_SITEFILE #define _HAVE_SITEFILE
#include <regex.h>
#include <util.h> #include <util.h>
typedef enum { typedef enum {
@@ -26,7 +28,7 @@ typedef enum {
typedef struct { typedef struct {
RequestType respondto; RequestType respondto;
Command command; Command command;
char *path; regex_t path;
char *arg; char *arg;
} SiteCommand; } SiteCommand;

View File

@@ -19,7 +19,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include <limits.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h>
#include <responses.h> #include <responses.h>
@@ -35,14 +37,76 @@ static int sendConnection(Connection *conn, char *format, ...) {
va_start(ap, format); va_start(ap, format);
vsnprintf(data, len + 1, format, ap); vsnprintf(data, len + 1, format, ap);
write(conn->fd, data, len); if (write(conn->fd, data, len) < len) {
free(data);
return 1;
}
free(data); free(data);
return 0; return 0;
} }
static void readResponse(Connection *conn, char *path) { static void readResponse(Connection *conn, char *path) {
FILE *file = fopen(path, "r"); FILE *file;
struct stat statbuf;
if (stat(path, &statbuf))
return;
if (S_ISDIR(statbuf.st_mode)) {
long reqPathLen = strlen(conn->path);
long pathLen = strlen(path);
char *assembledPath = malloc(reqPathLen + pathLen + 1);
if (assembledPath == NULL)
return;
memcpy(assembledPath, path, pathLen);
memcpy(assembledPath + pathLen, conn->path, reqPathLen + 1);
char requestPath[PATH_MAX];
if (realpath(assembledPath, requestPath) == NULL) {
free(assembledPath);
return;
}
char responsePath[PATH_MAX];
if (realpath(path, responsePath) == NULL) {
free(assembledPath);
return;
}
size_t responsePathLen = strlen(responsePath);
if (memcmp(requestPath, responsePath, responsePathLen)) {
free(assembledPath);
goto forbidden;
}
file = fopen(requestPath, "r");
free(assembledPath);
}
else
file = fopen(path, "r");
if (file == NULL) { if (file == NULL) {
goto forbidden;
}
fseek(file, 0, SEEK_END);
long len = ftell(file);
char *data = malloc(len);
if (data == NULL)
return;
fseek(file, 0, SEEK_SET);
if (fread(data, 1, len, file) < len)
goto error;
fclose(file);
sendConnection(conn,
"HTTP/1.1 200 OK\r\n"
"Server: swebs/0.1\r\n"
"Content-Length: %ld\r\n"
"\r\n", len
);
if (write(conn->fd, data, len) < len)
goto error;
free(data);
fsync(conn->fd);
return;
error:
fclose(file);
return;
forbidden:
sendConnection(conn, sendConnection(conn,
"HTTP/1.1 403 Forbidden\r\n" "HTTP/1.1 403 Forbidden\r\n"
"Content-Type: text/html; charset=UTF-8\r\n" "Content-Type: text/html; charset=UTF-8\r\n"
@@ -53,32 +117,14 @@ static void readResponse(Connection *conn, char *path) {
); );
return; return;
} }
fseek(file, 0, SEEK_END);
long len = ftell(file);
char *data = malloc(len);
if (data == NULL)
return;
fseek(file, 0, SEEK_SET);
fread(data, 1, len, file);
fclose(file);
sendConnection(conn,
"HTTP/1.1 200 OK\r\n"
"Server: swebs/0.1\r\n"
"Content-Length: %ld\r\n"
"\r\n", len
);
write(conn->fd, data, len);
free(data);
fsync(conn->fd);
}
int sendResponse(Connection *conn, Sitefile *site) { int sendResponse(Connection *conn, Sitefile *site) {
if (conn->path == NULL) puts(conn->path);
return 1;
for (int i = 0; i < site->size; i++) { for (int i = 0; i < site->size; i++) {
if (site->content[i].respondto != conn->type) if (site->content[i].respondto != conn->type)
continue; continue;
if (strcmp(conn->path, site->content[i].path) == 0) { if (regexec(&site->content[i].path, conn->path, 0, NULL, 0)
== 0) {
switch (site->content[i].command) { switch (site->content[i].command) {
case READ: case READ:
readResponse(conn, site->content[i].arg); readResponse(conn, site->content[i].arg);

View File

@@ -216,8 +216,8 @@ Sitefile *parseFile(char *path) {
if (argc < 3) if (argc < 3)
goto error; goto error;
ret->content[ret->size].path = copyString(argv[1]); if (regcomp(&ret->content[ret->size].path, argv[1],
if (ret->content[ret->size].path == NULL) REG_EXTENDED | REG_NOSUB))
goto error; goto error;
if (strcmp(argv[0], "read") == 0) { if (strcmp(argv[0], "read") == 0) {
@@ -239,7 +239,7 @@ nterror:
void freeSitefile(Sitefile *site) { void freeSitefile(Sitefile *site) {
for (long i = 0; i < site->size; i++) { for (long i = 0; i < site->size; i++) {
free(site->content[i].path); regfree(&site->content[i].path);
free(site->content[i].arg); free(site->content[i].arg);
} }
free(site->content); free(site->content);