Commit d8496bbd authored by Matthew Burket's avatar Matthew Burket

The basics seem to be working

parent cd40899e
This diff is collapsed.
...@@ -3,4 +3,4 @@ project(coms352_project2_part1 C) ...@@ -3,4 +3,4 @@ project(coms352_project2_part1 C)
set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD 11)
add_executable(http_client main.c connect.c connect.h buffer.c buffer.h) add_executable(http_client main.c connect.c connect.h buffer.c buffer.h urlParse.c urlParse.h)
\ No newline at end of file \ No newline at end of file
...@@ -73,9 +73,9 @@ int execate_request(int socketfd, char *hostname, char *path, int head) { ...@@ -73,9 +73,9 @@ int execate_request(int socketfd, char *hostname, char *path, int head) {
// Figure out the verb // Figure out the verb
char *firstLine; char *firstLine;
if (head) { if (head) {
firstLine = "HEAD "; firstLine = "HEAD /";
} else { } else {
firstLine = "GET "; firstLine = "GET /";
} }
// Building verb part // Building verb part
appendBuffer(&request, firstLine, strlen(firstLine)); appendBuffer(&request, firstLine, strlen(firstLine));
...@@ -114,7 +114,7 @@ int get_result(int socketfd, GrowthBuffer *recvBuff, int recvBuffSize) { ...@@ -114,7 +114,7 @@ int get_result(int socketfd, GrowthBuffer *recvBuff, int recvBuffSize) {
} }
if (bytesRecv < RECV_SIZE) { if (bytesRecv < RECV_SIZE) {
return 0; return 0;
} }
} }
} }
\ No newline at end of file
...@@ -4,12 +4,16 @@ ...@@ -4,12 +4,16 @@
#include <ctype.h> #include <ctype.h>
#include "connect.h" #include "connect.h"
#include "buffer.h" #include "buffer.h"
#include "urlParse.h"
/// Main method for the program /// Main method for the program
/// ///
/// \param argc /// \param argc
/// \param argv /// \param argv
/// \return /// \return
void show_help_exit(char *argv);
int main (int argc, char **argv) { int main (int argc, char **argv) {
int header_only = 0; int header_only = 0;
int timeDelta = 0; int timeDelta = 0;
...@@ -20,6 +24,10 @@ int main (int argc, char **argv) { ...@@ -20,6 +24,10 @@ int main (int argc, char **argv) {
opterr = 0; opterr = 0;
if (argc < 2) {
show_help_exit(argv[0]);
}
// getopts section based on // getopts section based on
// https://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html#Example-of-Getopt // 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)
...@@ -52,12 +60,31 @@ int main (int argc, char **argv) { ...@@ -52,12 +60,31 @@ int main (int argc, char **argv) {
for (index = optind; index < argc; index++) for (index = optind; index < argc; index++)
printf("Non-option argument %s\n", argv[index]); printf("Non-option argument %s\n", argv[index]);
printf("\noptind: %s\n", (argv[optind]));
if (argv[optind] == NULL) {
show_help_exit(argv[0]);
}
init_connection(argv[optind], 80, &addressInfo); char *host = malloc(sizeof(char) * 256);
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); int sockfd = establish_connect(addressInfo);
execate_request(sockfd, "www.cs.iastate.edu", "/index.html", 0); if (header_only == 1) {
execate_request(sockfd, host, page, 1);
} else {
execate_request(sockfd, host, page, 0);
}
GrowthBuffer gp; GrowthBuffer gp;
initBuffer(&gp, 50); initBuffer(&gp, 50);
get_result(sockfd, &gp, RECV_SIZE); get_result(sockfd, &gp, RECV_SIZE);
printf("%s", gp.array); printf("%s", gp.array);
}
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
//
// Created by mburket on 4/9/18.
//
#include "urlParse.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/// Gets the parts of a url from a string
///
/// \param url the full string of the url
/// \param rhost return host
/// \param rport return port
/// \param rpage return
/// \return 0 if success
int getUrlParts(char *url, char **rhost, int *rport, char **rpage) {
char *host = malloc(sizeof(char) * 256);
int port = 80;
char *page = malloc(sizeof(char) * 256);
for (int i = 0; i < 100; i++) {
page[0] = '\0';
}
// I know this bad
// But it works somewhat
// Idea from https://stackoverflow.com/a/726214
if (sscanf(url, "http://%99[^:]:%99d/%99[^\n]", host, &port, page) == 3) {
} else {
sscanf(url, "http://%99[^/]/%99[^\n]", host, page);
}
strcpy(*rhost, host);
*rport = port;
strcpy(*rpage, page);
free(page);
free(host);
return 0;
}
//
// Created by mburket on 4/9/18.
//
#ifndef COMS352_PROJECT2_PART1_URLPARSE_H
#define COMS352_PROJECT2_PART1_URLPARSE_H
int getUrlParts(char *url, char **rhost, int *rport, char **rpage);
#endif //COMS352_PROJECT2_PART1_URLPARSE_H
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