Implemented hostnames
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
#define _HAVE_RESPONSE_UTIL
|
||||
#include <connections.h>
|
||||
|
||||
#define ERROR_400 "400 Bad Request"
|
||||
#define ERROR_403 "403 Forbidden"
|
||||
#define ERROR_404 "404 Not Found"
|
||||
#define ERROR_500 "500 Internal Server Error"
|
||||
|
||||
@@ -27,6 +27,7 @@ typedef enum {
|
||||
|
||||
typedef struct {
|
||||
RequestType respondto;
|
||||
char *host;
|
||||
Command command;
|
||||
regex_t path;
|
||||
char *arg;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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:
|
||||
|
||||
12
src/util.c
12
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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <stdint.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) {
|
||||
if (strlen(str) >= 8)
|
||||
return INVALID;
|
||||
|
||||
Reference in New Issue
Block a user