Commit aeef988e authored by Matthew Burket's avatar Matthew Burket

mem and comments

parent 31b8fc0e
Pipeline #345 passed with stage
in 13 seconds
This diff is collapsed.
cmake_minimum_required(VERSION 3.9) cmake_minimum_required(VERSION 3.3)
project(coms352_project2_part1 C) project(coms352_project2_part1 C)
set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD 11)
......
//w //
// Created by mburket on 3/29/18. // Created by mburket on 3/29/18.
// //
...@@ -116,23 +116,30 @@ int execate_request(int socketfd, char *hostname, char *path, int head, int date ...@@ -116,23 +116,30 @@ int execate_request(int socketfd, char *hostname, char *path, int head, int date
return sentByte; return sentByte;
} }
int get_result(int socketfd, GrowthBuffer *recvBuff, int recvBuffSize) { /// Get result from the request
/// \param socketfd socket used to make the request
/// \param recvBuff buffer to send the request back in
/// \param recvBuffSize
/// \return 0 if success, otherwise not zero
int get_result(int socketfd, GrowthBuffer *recvBuff) {
size_t bytesRecv; size_t bytesRecv;
char revcData[recvBuffSize]; char revcData[RECV_SIZE];
while (1) { while (1) {
errno = 0;
bytesRecv = recv(socketfd, revcData, RECV_SIZE, 0); bytesRecv = recv(socketfd, revcData, RECV_SIZE, 0);
if (bytesRecv == -1) { if (bytesRecv == -1) {
fprintf(stderr, "\n Unable to recv data from socket."); fprintf(stderr, "\n Unable to recv data from socket. [Error %d]: %s\n", errno, strerror(errno));
return -1; return -1;
} else if (bytesRecv == 0) { } else if (bytesRecv == 0) {
return 0; return 0;
} }
// Got something append it
if (bytesRecv > 0) { if (bytesRecv > 0) {
appendBuffer(recvBuff, revcData, bytesRecv); appendBuffer(recvBuff, revcData, bytesRecv);
} }
// Didn't anything, we are done
if (bytesRecv < RECV_SIZE) { if (bytesRecv < RECV_SIZE) {
return 0; return 0;
} }
......
...@@ -18,6 +18,6 @@ ...@@ -18,6 +18,6 @@
void init_connection(char *hostname, int port, struct addrinfo **info); void init_connection(char *hostname, int port, struct addrinfo **info);
int establish_connect(struct addrinfo *info); int establish_connect(struct addrinfo *info);
int execate_request(int socketfd, char *hostname, char *path, int head, int date, char *date_string); 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); int get_result(int socketfd, GrowthBuffer *recvBuff);
#endif //COMS352_PROJECT2_PART1_CONNECT_H #endif //COMS352_PROJECT2_PART1_CONNECT_H
...@@ -6,15 +6,17 @@ ...@@ -6,15 +6,17 @@
#include "buffer.h" #include "buffer.h"
#include "urlParse.h" #include "urlParse.h"
/// Main method for the program
///
/// \param argc
/// \param argv
/// \return
void show_help_exit(char *argv); void show_help_exit(char *argv);
/// Main method for the program
///
/// \param argc count of args
/// \param argv arg vector
/// \return non-zero if error, otherwise 0
int main (int argc, char **argv) { int main (int argc, char **argv) {
// Setup values and memory for program
int header_only = 0; int header_only = 0;
int timeDelta = 0; int timeDelta = 0;
char *cvalue = NULL; char *cvalue = NULL;
...@@ -54,30 +56,37 @@ int main (int argc, char **argv) { ...@@ -54,30 +56,37 @@ int main (int argc, char **argv) {
} }
} }
// Advance index
for (index = optind; index < argc; index++); for (index = optind; index < argc; index++);
// No URL Exit
if (argv[optind] == NULL) { if (argv[optind] == NULL) {
show_help_exit(argv[0]); show_help_exit(argv[0]);
} }
// Setup Connection Memory
char *host = malloc(sizeof(char) * 256); char *host = malloc(sizeof(char) * 256);
int port = 80; int port = 80;
char *page = malloc(sizeof(char) * 256); char *page = malloc(sizeof(char) * 256);
// Parse the Url
getUrlParts(argv[optind], &host, &port, &page); getUrlParts(argv[optind], &host, &port, &page);
// Setup Connection
init_connection(host, port, &addressInfo); init_connection(host, port, &addressInfo);
int sockfd = establish_connect(addressInfo); int sockfd = establish_connect(addressInfo);
// Fire Off Request
execate_request(sockfd, host, page, header_only, timeDelta, cvalue); execate_request(sockfd, host, page, header_only, timeDelta, cvalue);
// Setup for getting response
GrowthBuffer gp; GrowthBuffer gp;
initBuffer(&gp, 50); initBuffer(&gp, 50);
get_result(sockfd, &gp, RECV_SIZE); // Get response
get_result(sockfd, &gp);
// Save to file // Save to file
FILE *fp; FILE *fp;
fp = fopen("response", "w"); fp = fopen("response", "w");
fwrite(gp.array, 1, strlen(gp.array), fp); fwrite(gp.array, 1, strlen(gp.array), fp);
fclose(fp); fclose(fp);
exit(EXIT_SUCCESS);
} }
/// Show help /// Show help
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
/// \param rpage return /// \param rpage return
/// \return 0 if success /// \return 0 if success
int getUrlParts(char *url, char **rhost, int *rport, char **rpage) { int getUrlParts(char *url, char **rhost, int *rport, char **rpage) {
// Setup Memory
char *host = malloc(sizeof(char) * 256); char *host = malloc(sizeof(char) * 256);
int port = 80; int port = 80;
char *page = malloc(sizeof(char) * 256); char *page = malloc(sizeof(char) * 256);
...@@ -27,11 +28,10 @@ int getUrlParts(char *url, char **rhost, int *rport, char **rpage) { ...@@ -27,11 +28,10 @@ int getUrlParts(char *url, char **rhost, int *rport, char **rpage) {
} else { } else {
sscanf(url, "http://%99[^/]/%99[^\n]", host, page); sscanf(url, "http://%99[^/]/%99[^\n]", host, page);
} }
// Copy out
strcpy(*rhost, host); strcpy(*rhost, host);
*rport = port; *rport = port;
strcpy(*rpage, page); strcpy(*rpage, page);
free(page);
free(host);
return 0; return 0;
} }
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