diff --git a/documentation/sitefiles.md b/documentation/sitefiles.md
index bc9e777..672a193 100644
--- a/documentation/sitefiles.md
+++ b/documentation/sitefiles.md
@@ -19,6 +19,6 @@ sitefiles also allow comments with #
# Part 3: Variables
* ```respondto```: - The type of http request to respond to. One of:
- * GET
+ * GET (defualt)
* POST
-Default: GET
+* ```host```: - The hostname to respond to. Case insensitive, default: localhost
diff --git a/example/site/easteregg.html b/example/site/easteregg.html
new file mode 100644
index 0000000..972bba0
--- /dev/null
+++ b/example/site/easteregg.html
@@ -0,0 +1 @@
+
You found the easter egg!
diff --git a/example/sitefile b/example/sitefile
index 811337f..8cbb562 100644
--- a/example/sitefile
+++ b/example/sitefile
@@ -1,4 +1,7 @@
+set host localhost:8000
read ^/$ site/index.html
read ^/hello$ site/hello.html
read ^/blog/.*$ site/blog/
#/blog/2021-1-25.html turns into site/blog//blog/2021-1-25.html
+set host 127.0.0.1:8000
+read ^/$ site/easteregg.html
diff --git a/src/include/responseutil.h b/src/include/responseutil.h
index 292484d..2c7755a 100644
--- a/src/include/responseutil.h
+++ b/src/include/responseutil.h
@@ -19,6 +19,7 @@
#define _HAVE_RESPONSE_UTIL
#include
+#define ERROR_400 "400 Bad Request"
#define ERROR_403 "403 Forbidden"
#define ERROR_404 "404 Not Found"
#define ERROR_500 "500 Internal Server Error"
diff --git a/src/include/sitefile.h b/src/include/sitefile.h
index 9adc50f..4ed092b 100644
--- a/src/include/sitefile.h
+++ b/src/include/sitefile.h
@@ -27,6 +27,7 @@ typedef enum {
typedef struct {
RequestType respondto;
+ char *host;
Command command;
regex_t path;
char *arg;
diff --git a/src/include/util.h b/src/include/util.h
index d634187..158fc82 100644
--- a/src/include/util.h
+++ b/src/include/util.h
@@ -30,5 +30,7 @@ typedef enum {
//INVALID in HTTP/1.1.
} RequestType;
+int istrcmp(char *s1, char *s2);
+//case insensitive strcmp
RequestType getType(char *str);
#endif
diff --git a/src/responses.c b/src/responses.c
index 3b4f2f9..39528aa 100644
--- a/src/responses.c
+++ b/src/responses.c
@@ -66,7 +66,6 @@ static void readResponse(Connection *conn, char *path) {
}
file = fopen(requestPath, "r");
- puts(requestPath);
free(assembledPath);
}
else
@@ -98,9 +97,22 @@ forbidden:
}
int sendResponse(Connection *conn, Sitefile *site) {
+ char *host = NULL;
+ for (int i = 0; i < conn->fieldCount; i++) {
+ if (strcmp(conn->fields[i].field, "Host") == 0) {
+ host = conn->fields[i].value;
+ break;
+ }
+ }
+ if (host == NULL) {
+ sendErrorResponse(conn, ERROR_400);
+ return 1;
+ }
for (int i = 0; i < site->size; i++) {
if (site->content[i].respondto != conn->type)
continue;
+ if (istrcmp(site->content[i].host, host))
+ continue;
if (regexec(&site->content[i].path, conn->path, 0, NULL, 0)
== 0) {
switch (site->content[i].command) {
diff --git a/src/sitefile.c b/src/sitefile.c
index a0b531b..d6004f1 100644
--- a/src/sitefile.c
+++ b/src/sitefile.c
@@ -180,6 +180,7 @@ Sitefile *parseFile(char *path) {
return NULL;
}
RequestType respondto = GET;
+ char *host = copyString("localhost");
int argc;
char **argv;
for (;;) {
@@ -201,8 +202,21 @@ Sitefile *parseFile(char *path) {
if (respondto == INVALID)
goto error;
}
+ else if (strcmp(argv[1], "host") == 0) {
+ if (istrcmp(host, argv[2]) == 0)
+ goto setValue;
+ for (int i = 0; i < ret->size; i++) {
+ if (istrcmp(argv[2],
+ ret->content[i].host) == 0) {
+ host = ret->content[i].host;
+ goto setValue;
+ }
+ }
+ host = copyString(argv[2]);
+ }
else
goto error;
+setValue:
continue;
}
if (ret->size >= allocatedLength) {
@@ -228,6 +242,8 @@ Sitefile *parseFile(char *path) {
else
goto error;
freeTokens(argc, argv);
+ ret->content[ret->size].respondto = respondto;
+ ret->content[ret->size].host = host;
ret->size++;
}
error:
diff --git a/src/util.c b/src/util.c
index 2a881ae..ef515d0 100644
--- a/src/util.c
+++ b/src/util.c
@@ -15,11 +15,23 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
+#include
#include
#include
#include
+int istrcmp(char *s1, char *s2) {
+ for (int i = 0;; i++) {
+ char c1 = tolower(s1[i]);
+ char c2 = tolower(s2[i]);
+ if (c1 != c2)
+ return c1 - c2;
+ if (c1 == '\0')
+ return 0;
+ }
+}
+
RequestType getType(char *str) {
if (strlen(str) >= 8)
return INVALID;