Loading http.c +32 −1 Original line number Diff line number Diff line Loading @@ -3,9 +3,14 @@ // #include <stdio.h> #include <stdlib.h> #define _XOPEN_SOURCE #include <time.h> #include <locale.h> #include "http.h" #include "utils.h" #include "string.h" #include <string.h> /// Gets the HTTP verb from the /// \param string Loading Loading @@ -36,3 +41,29 @@ void get_http_path(char *request, char *path) { int parseHeader(char *header) { } int parseHeaders(char *request, struct tm *tm, int *hasDate) { char *strtok_status, *line; line = strtok_r(request, "\n", &strtok_status); line = strtok_r(NULL, "\n", &strtok_status); int c = 0; do { char key[100], value[100]; sscanf(line, "%99[^:]: %99[^\r]", key, value); fprintf(stderr, "\nc:%d key: %s value: %s\n", c, key, value); char *headerName = "If-Modified-Since"; if (strncmp(headerName, key, strlen(headerName)) == 0) { time_t t; if (strptime(value, "%a %b %d %H:%M:%S %Y", tm) == NULL) { fprintf(stderr, "Invalid Date"); } char buf[255]; strftime(buf, sizeof(buf), "%d %b %Y %H:%M", tm); fprintf(stderr, "\nParsed date: %s\n", buf); fprintf(stderr, "Has date"); *hasDate = 1; } c++; } while ((line = strtok_r(NULL, "\n", &strtok_status)) != NULL); } No newline at end of file http.h +4 −0 Original line number Diff line number Diff line Loading @@ -4,6 +4,8 @@ #ifndef COMS352_PROJECT2_PART2_HTTP_H #define COMS352_PROJECT2_PART2_HTTP_H #include <time.h> // Struct of methods used on this project typedef enum { HTTP_GET, Loading @@ -13,4 +15,6 @@ typedef enum { HTTP_VERB get_http_verb(char *request); void get_http_path(char *request, char *path); int parseHeaders(char *request, struct tm *tm, int *hasDate); #endif //COMS352_PROJECT2_PART2_HTTP_H main.c +1 −3 Original line number Diff line number Diff line Loading @@ -40,5 +40,3 @@ int main() { } #pragma clang diagnostic pop No newline at end of file network.c +19 −0 Original line number Diff line number Diff line Loading @@ -10,6 +10,8 @@ #include <unistd.h> #include <netinet/in.h> #include <poll.h> #include <sys/stat.h> #include <time.h> #include "network.h" #include "buffer.h" Loading Loading @@ -52,11 +54,28 @@ int handle_client(int clientfd) { char *path = malloc(sizeof(char) * 1024); get_http_path(request.array, path); fprintf(stdout, "Verb:%d Path:%s\n", verb, path); int hasDate; struct tm tm; parseHeaders(request.array, &tm, &hasDate); // Setup for getting the response GrowthBuffer response; initBuffer(&response, 5000); // If the exsits return 200 if (checkFile(path) != -1) { if (hasDate == 1) { fprintf(stderr, "DEUBG: started date secton"); struct stat statbuff; stat(path, &statbuff); time_t headerTime = mktime(&tm); time_t fileTime = statbuff.st_ctime; fprintf(stderr, "\nfileTime = %d", fileTime); if (difftime(fileTime, headerTime) < 0.0) { char *basicHeader = "HTTP/1.1 304 Not Modified\r\n\r\n"; write(clientfd, basicHeader, strlen(basicHeader)); shutdown(clientfd, SHUT_RDWR); exit(EXIT_SUCCESS); } } char *basicHeader = "HTTP/1.1 200 OK\r\n\r\n"; appendBuffer(&response, basicHeader, strlen(basicHeader)); write(clientfd, response.array, strlen(response.array)); Loading network.h +1 −1 Original line number Diff line number Diff line Loading @@ -15,7 +15,7 @@ int bind_socket(); void getRequest(int clientfd, GrowthBuffer *request); #define PORT 26885 #define PORT 2681 #define DEBUG 0 #define TRUE 1 #endif //COMS352_PROJECT2_PART2_NETWORK_H Loading
http.c +32 −1 Original line number Diff line number Diff line Loading @@ -3,9 +3,14 @@ // #include <stdio.h> #include <stdlib.h> #define _XOPEN_SOURCE #include <time.h> #include <locale.h> #include "http.h" #include "utils.h" #include "string.h" #include <string.h> /// Gets the HTTP verb from the /// \param string Loading Loading @@ -36,3 +41,29 @@ void get_http_path(char *request, char *path) { int parseHeader(char *header) { } int parseHeaders(char *request, struct tm *tm, int *hasDate) { char *strtok_status, *line; line = strtok_r(request, "\n", &strtok_status); line = strtok_r(NULL, "\n", &strtok_status); int c = 0; do { char key[100], value[100]; sscanf(line, "%99[^:]: %99[^\r]", key, value); fprintf(stderr, "\nc:%d key: %s value: %s\n", c, key, value); char *headerName = "If-Modified-Since"; if (strncmp(headerName, key, strlen(headerName)) == 0) { time_t t; if (strptime(value, "%a %b %d %H:%M:%S %Y", tm) == NULL) { fprintf(stderr, "Invalid Date"); } char buf[255]; strftime(buf, sizeof(buf), "%d %b %Y %H:%M", tm); fprintf(stderr, "\nParsed date: %s\n", buf); fprintf(stderr, "Has date"); *hasDate = 1; } c++; } while ((line = strtok_r(NULL, "\n", &strtok_status)) != NULL); } No newline at end of file
http.h +4 −0 Original line number Diff line number Diff line Loading @@ -4,6 +4,8 @@ #ifndef COMS352_PROJECT2_PART2_HTTP_H #define COMS352_PROJECT2_PART2_HTTP_H #include <time.h> // Struct of methods used on this project typedef enum { HTTP_GET, Loading @@ -13,4 +15,6 @@ typedef enum { HTTP_VERB get_http_verb(char *request); void get_http_path(char *request, char *path); int parseHeaders(char *request, struct tm *tm, int *hasDate); #endif //COMS352_PROJECT2_PART2_HTTP_H
main.c +1 −3 Original line number Diff line number Diff line Loading @@ -40,5 +40,3 @@ int main() { } #pragma clang diagnostic pop No newline at end of file
network.c +19 −0 Original line number Diff line number Diff line Loading @@ -10,6 +10,8 @@ #include <unistd.h> #include <netinet/in.h> #include <poll.h> #include <sys/stat.h> #include <time.h> #include "network.h" #include "buffer.h" Loading Loading @@ -52,11 +54,28 @@ int handle_client(int clientfd) { char *path = malloc(sizeof(char) * 1024); get_http_path(request.array, path); fprintf(stdout, "Verb:%d Path:%s\n", verb, path); int hasDate; struct tm tm; parseHeaders(request.array, &tm, &hasDate); // Setup for getting the response GrowthBuffer response; initBuffer(&response, 5000); // If the exsits return 200 if (checkFile(path) != -1) { if (hasDate == 1) { fprintf(stderr, "DEUBG: started date secton"); struct stat statbuff; stat(path, &statbuff); time_t headerTime = mktime(&tm); time_t fileTime = statbuff.st_ctime; fprintf(stderr, "\nfileTime = %d", fileTime); if (difftime(fileTime, headerTime) < 0.0) { char *basicHeader = "HTTP/1.1 304 Not Modified\r\n\r\n"; write(clientfd, basicHeader, strlen(basicHeader)); shutdown(clientfd, SHUT_RDWR); exit(EXIT_SUCCESS); } } char *basicHeader = "HTTP/1.1 200 OK\r\n\r\n"; appendBuffer(&response, basicHeader, strlen(basicHeader)); write(clientfd, response.array, strlen(response.array)); Loading
network.h +1 −1 Original line number Diff line number Diff line Loading @@ -15,7 +15,7 @@ int bind_socket(); void getRequest(int clientfd, GrowthBuffer *request); #define PORT 26885 #define PORT 2681 #define DEBUG 0 #define TRUE 1 #endif //COMS352_PROJECT2_PART2_NETWORK_H