Implemented hostnames
This commit is contained in:
@@ -19,6 +19,6 @@ sitefiles also allow comments with #
|
|||||||
# Part 3: Variables
|
# Part 3: Variables
|
||||||
|
|
||||||
* ```respondto```: - The type of http request to respond to. One of:
|
* ```respondto```: - The type of http request to respond to. One of:
|
||||||
* GET
|
* GET (defualt)
|
||||||
* POST
|
* POST
|
||||||
Default: GET
|
* ```host```: - The hostname to respond to. Case insensitive, default: localhost
|
||||||
|
|||||||
1
example/site/easteregg.html
Normal file
1
example/site/easteregg.html
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<h1>You found the easter egg!</h1>
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
|
set host localhost:8000
|
||||||
read ^/$ site/index.html
|
read ^/$ site/index.html
|
||||||
read ^/hello$ site/hello.html
|
read ^/hello$ site/hello.html
|
||||||
read ^/blog/.*$ site/blog/
|
read ^/blog/.*$ site/blog/
|
||||||
#/blog/2021-1-25.html turns into site/blog//blog/2021-1-25.html
|
#/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
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
#define _HAVE_RESPONSE_UTIL
|
#define _HAVE_RESPONSE_UTIL
|
||||||
#include <connections.h>
|
#include <connections.h>
|
||||||
|
|
||||||
|
#define ERROR_400 "400 Bad Request"
|
||||||
#define ERROR_403 "403 Forbidden"
|
#define ERROR_403 "403 Forbidden"
|
||||||
#define ERROR_404 "404 Not Found"
|
#define ERROR_404 "404 Not Found"
|
||||||
#define ERROR_500 "500 Internal Server Error"
|
#define ERROR_500 "500 Internal Server Error"
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ typedef enum {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
RequestType respondto;
|
RequestType respondto;
|
||||||
|
char *host;
|
||||||
Command command;
|
Command command;
|
||||||
regex_t path;
|
regex_t path;
|
||||||
char *arg;
|
char *arg;
|
||||||
|
|||||||
@@ -30,5 +30,7 @@ typedef enum {
|
|||||||
//INVALID in HTTP/1.1.
|
//INVALID in HTTP/1.1.
|
||||||
} RequestType;
|
} RequestType;
|
||||||
|
|
||||||
|
int istrcmp(char *s1, char *s2);
|
||||||
|
//case insensitive strcmp
|
||||||
RequestType getType(char *str);
|
RequestType getType(char *str);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -66,7 +66,6 @@ static void readResponse(Connection *conn, char *path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
file = fopen(requestPath, "r");
|
file = fopen(requestPath, "r");
|
||||||
puts(requestPath);
|
|
||||||
free(assembledPath);
|
free(assembledPath);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -98,9 +97,22 @@ forbidden:
|
|||||||
}
|
}
|
||||||
|
|
||||||
int sendResponse(Connection *conn, Sitefile *site) {
|
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++) {
|
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 (istrcmp(site->content[i].host, host))
|
||||||
|
continue;
|
||||||
if (regexec(&site->content[i].path, conn->path, 0, NULL, 0)
|
if (regexec(&site->content[i].path, conn->path, 0, NULL, 0)
|
||||||
== 0) {
|
== 0) {
|
||||||
switch (site->content[i].command) {
|
switch (site->content[i].command) {
|
||||||
|
|||||||
@@ -180,6 +180,7 @@ Sitefile *parseFile(char *path) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
RequestType respondto = GET;
|
RequestType respondto = GET;
|
||||||
|
char *host = copyString("localhost");
|
||||||
int argc;
|
int argc;
|
||||||
char **argv;
|
char **argv;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@@ -201,8 +202,21 @@ Sitefile *parseFile(char *path) {
|
|||||||
if (respondto == INVALID)
|
if (respondto == INVALID)
|
||||||
goto error;
|
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
|
else
|
||||||
goto error;
|
goto error;
|
||||||
|
setValue:
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (ret->size >= allocatedLength) {
|
if (ret->size >= allocatedLength) {
|
||||||
@@ -228,6 +242,8 @@ Sitefile *parseFile(char *path) {
|
|||||||
else
|
else
|
||||||
goto error;
|
goto error;
|
||||||
freeTokens(argc, argv);
|
freeTokens(argc, argv);
|
||||||
|
ret->content[ret->size].respondto = respondto;
|
||||||
|
ret->content[ret->size].host = host;
|
||||||
ret->size++;
|
ret->size++;
|
||||||
}
|
}
|
||||||
error:
|
error:
|
||||||
|
|||||||
12
src/util.c
12
src/util.c
@@ -15,11 +15,23 @@
|
|||||||
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
#include <ctype.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include <util.h>
|
#include <util.h>
|
||||||
|
|
||||||
|
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) {
|
RequestType getType(char *str) {
|
||||||
if (strlen(str) >= 8)
|
if (strlen(str) >= 8)
|
||||||
return INVALID;
|
return INVALID;
|
||||||
|
|||||||
Reference in New Issue
Block a user