Commit 2a0b489b authored by Matthew Burket's avatar Matthew Burket

I belive I now have a MVP

parent d8496bbd
<component name="ProjectDictionaryState">
<dictionary name="mburket">
<words>
<w>calcuates</w>
<w>connetion</w>
<w>descired</w>
<w>initalize</w>
......
This diff is collapsed.
......@@ -4,6 +4,9 @@
#include "connect.h"
#include "buffer.h"
#include <time.h>
int calc_date(char *deltaString, char *returnTime);
/// Initalize the connection
///
......@@ -65,7 +68,7 @@ int establish_connect(struct addrinfo *info) {
/// \param path path to request
/// \param head if true use the head method on the url
/// \return number of bytes sent to the server
int execate_request(int socketfd, char *hostname, char *path, int head) {
int execate_request(int socketfd, char *hostname, char *path, int head, int date, char *date_string) {
// Reqeust is a buffer
GrowthBuffer request;
// Init the buffer
......@@ -87,6 +90,15 @@ int execate_request(int socketfd, char *hostname, char *path, int head) {
appendBuffer(&request, hostLine, strlen(hostLine));
appendBuffer(&request, hostname, strlen(hostname));
appendBuffer(&request, "\r\n", 2);
// Build If-Modified-Since Header
if (date) {
char *ModLine = "If-Modified-Since: ";
appendBuffer(&request, ModLine, strlen(ModLine));
char *timeStamp = malloc(sizeof(char) * 256);
calc_date(date_string, timeStamp);
appendBuffer(&request, timeStamp, strlen(timeStamp));
appendBuffer(&request, "\r\n", 2);
}
// Close off the request
appendBuffer(&request, "\r\n", 2);
appendBuffer(&request, "\r\n", 2);
......@@ -117,4 +129,20 @@ int get_result(int socketfd, GrowthBuffer *recvBuff, int recvBuffSize) {
return 0;
}
}
}
/// Calculates the time stamp based on the time delta string
///
/// \param deltaString
/// \param returnTime
/// \return
int calc_date(char *deltaString, char *returnTime) {
int day = 0;
int hour = 0;
int min = 0;
sscanf(deltaString, "%d:%d:%d", &day, &hour, &min);
time_t n_time;
n_time=time(0);
n_time=n_time-(day*24*3600+hour*3600+min*60);
strcpy(returnTime, ctime(&n_time));
}
\ No newline at end of file
......@@ -17,7 +17,7 @@
void init_connection(char *hostname, int port, struct addrinfo **info);
int establish_connect(struct addrinfo *info);
int execate_request(int socketfd, char *hostname, char *path, int head);
int execate_request(int socketfd, char *hostname, char *path, int head, int date, char *date_string);
int get_result(int socketfd, GrowthBuffer *recvBuff, int recvBuffSize);
#endif //COMS352_PROJECT2_PART1_CONNECT_H
......@@ -30,7 +30,7 @@ int main (int argc, char **argv) {
// getopts section based on
// https://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html#Example-of-Getopt
while ((c = getopt(argc, argv, "hd:")) != -1)
while ((c = getopt(argc, argv, "hd:")) != -1) {
switch (c) {
case 'h':
header_only = 1;
......@@ -52,15 +52,12 @@ int main (int argc, char **argv) {
default:
abort();
}
}
printf("header_only = %d, timeDelta = %d, cvalue = %s\n",
header_only, timeDelta, cvalue);
for (index = optind; index < argc; index++)
for (index = optind; index < argc; index++) {
printf("Non-option argument %s\n", argv[index]);
}
printf("\noptind: %s\n", (argv[optind]));
if (argv[optind] == NULL) {
show_help_exit(argv[0]);
}
......@@ -69,14 +66,9 @@ int main (int argc, char **argv) {
int port = 80;
char *page = malloc(sizeof(char) * 256);
getUrlParts(argv[optind], &host, &port, &page);
printf("\nhost: %s\n", host);
init_connection(host, 80, &addressInfo);
int sockfd = establish_connect(addressInfo);
if (header_only == 1) {
execate_request(sockfd, host, page, 1);
} else {
execate_request(sockfd, host, page, 0);
}
execate_request(sockfd, host, page, header_only, timeDelta, cvalue);
GrowthBuffer gp;
initBuffer(&gp, 50);
......@@ -87,4 +79,4 @@ int main (int argc, char **argv) {
void show_help_exit(char *argv) {
fprintf(stderr, "\n%s [–h] [–d <time-interval>] <URL>", argv);
exit(EXIT_FAILURE);
}
\ No newline at end of file
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment