Created
March 10, 2011 17:21
meh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <stdio.h> | |
#include "handler.h" | |
void handle_request(int sockfd, const char *request) | |
{ | |
char *res = "HTTP 200 OK\r\n\r\n"; | |
char file_buf[1024]; | |
char url[1024]; | |
FILE *file; | |
int req_len = strlen(request); | |
int i, bytes; | |
int copied = 1; | |
int started = 0; | |
url[0] = '.'; | |
for (i=0; i<req_len; i++) { | |
if (!started) { | |
if (request[i] == ' ') { | |
started = 1; | |
continue; | |
} | |
} | |
else { | |
if (request[i] == ' ') { | |
url[copied] = '\0'; | |
copied++; | |
break; | |
} | |
url[copied] = request[i]; | |
copied++; | |
} | |
} | |
printf("Requested: %s\n", url); | |
file = fopen(url, "r"); | |
if (file != 0) { | |
write(sockfd, res, strlen(res)); | |
while(bytes = fread(file_buf, 1, sizeof(file_buf), file)) { | |
write(sockfd, file_buf, bytes); | |
} | |
fclose(file); | |
} | |
else { | |
char *four_oh_four = "HTTP 404 NOT FOUND\r\n\r\nNot Found\r\n\r\n"; | |
write(sockfd, four_oh_four, strlen(four_oh_four)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment