I'm doing a school project where we are building a simple web server in C. To implement this I read the first line of the request (all I need for my purposes) and parse the middle string as the file name using strtok
. So a typical request would be something like:
GET / HTTP/1.0
and my response code would be something like this:
// first token is request type
token = strtok(buffer, " \n\r");
if(strcmp(token, "GET") != 0) {
// sendResponse just sends a response to the client
// with the appropriate headers
sendResponse(connection, -1, HTTP_11_400, strlen(HTTP_11_400), NULL);
break;
}
char *request_type = token;
// second token is url
token = strtok(NULL, " \n\r");
if(strstr(token, "../") != NULL) {
sendResponse(connection, -1, HTTP_11_403, strlen(HTTP_11_403), NULL);
break;
}
char *request_url = token;
// third token is HTTP protocol
token = strtok(NULL, " \n\r");
if(strcmp(token, "HTTP/1.0") != 0 && strcmp(token, "HTTP/1.1") != 0) {
sendResponse(connection, -1, HTTP_11_400, strlen(HTTP_11_400), NULL);
break;
}
char *protocol = token;
printf("filename: %s\n", request_url);
if(strcmp(request_url, "/") == 0) {
request_url = "index.html";
}
if(*request_url == '/') {
request_url = request_url++;
}
char* fileType = strrchr(request_url, '.') + sizeof(char);
fd = open(request_url, O_RDONLY);
As you can see I already check to make sure the client is not able to go up a directory. Looking at my code, I'm basically putting a user-inputted string and trusting the user with it, are there other security features that I should take into account? (I also have to write a paper on this implementation and I think it would be nice to write about security).