Commit 4964af40 authored by Matthew Burket's avatar Matthew Burket

Got something that does the basics

parent a18ad6e5
......@@ -2,7 +2,9 @@
<dictionary name="mburket">
<words>
<w>connetion</w>
<w>descired</w>
<w>initalize</w>
<w>sucesss</w>
</words>
</dictionary>
</component>
\ No newline at end of file
This diff is collapsed.
......@@ -5,39 +5,67 @@
#include "buffer.h"
#include "string.h"
/// Initalize the buffer
///
/// \param arr pointer to the array struct
/// \param initSize inital size of the buffer
void initBuffer(GrowthBuffer *arr, size_t initSize) {
arr->array = (char *) malloc(sizeof(char) * initSize);
arr->total_size = initSize;
arr->used = 0;
}
void appendBuffer(GrowthBuffer *arr, char *value, int length) {
/// Appends to the buffer
///
/// \param arr buffer to append to
/// \param value value to append
/// \param length length to append
/// \return 0 if sucesss
int appendBuffer(GrowthBuffer *arr, char *value, int length) {
int dLength = length + 1;
// Grow Array if not big enough
if (!hasSpaceBuffer(arr, dLength)) {
int growthFactor = arr->total_size;
if (growthFactor < dLength) {
growthFactor = dLength;
}
// Double the size
int arrNewSize = growthFactor * 2;
// realloc the array
char *newArr = realloc(arr->array, dLength * sizeof(char));
// Set the new array
arr->array = newArr;
// Set the new total size
arr->total_size = arrNewSize;
}
// Position in the array
int buffPos;
int copiedBytesCount;
// Count of the number bytes we have written
int copiedBytesCount = 0;
for (int i = 0; i < length; i++) {
if (value[i] == NULL) {
// Don't write a null byte to the buffer
if (value[i] == '\0') {
break;
}
// The place to write the write to the buffer is arr->used + i
buffPos = arr->used + i;
// Set arr->array + buffPos to the value of given string at i
*(arr->array + buffPos) = value[i];
// Copied a byte, so increase counter
copiedBytesCount++;
}
// Update the used counter
arr->used += copiedBytesCount;
}
/// Free the memory used by the buffer
///
/// \param arr buffer to free
void freeBuffer(GrowthBuffer *arr) {
free(arr->array);
arr->array = NULL;
......@@ -45,6 +73,11 @@ void freeBuffer(GrowthBuffer *arr) {
}
/// Checks if the array has space for the given length
///
/// \param arr
/// \param length
/// \return
int hasSpaceBuffer(GrowthBuffer *arr, int length) {
size_t unused = arr->total_size - arr->used;
......
......@@ -14,7 +14,7 @@ typedef struct {
} GrowthBuffer;
void initBuffer(GrowthBuffer *arr, size_t initSize);
void appendBuffer(GrowthBuffer *arr, char *value, int length);
int appendBuffer(GrowthBuffer *arr, char *value, int length);
void freeBuffer(GrowthBuffer *arr);
int hasSpaceBuffer(GrowthBuffer *arr, int length);
......
......@@ -3,6 +3,7 @@
//
#include "connect.h"
#include "buffer.h"
/// Initalize the connection
///
......@@ -12,7 +13,7 @@
void init_connection(char *hostname, int port, struct addrinfo **info) {
struct addrinfo hints;
memset(&hints,0, sizeof(struct addrinfo));
memset(&hints, 0, sizeof(hints));
hints.ai_family= AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
......@@ -20,7 +21,7 @@
errno = 0;
int erro = getaddrinfo(hostname, NULL, &hints, info);
int erro = getaddrinfo(hostname, "80", &hints, info);
if (erro != 0) {
fprintf(stderr, "%s", gai_strerror(erro));
......@@ -29,43 +30,91 @@
}
/// Establishs a connection to the server described info
///
/// \param info socket info
/// \return returns the created socket
int establish_connect(struct addrinfo *info) {
int socketfd, connection;
errno = 0;
socketfd = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
if (socketfd > 0) {
if (socketfd < 0) {
fprintf(stderr, "\nFailure to create socket.\n");
fprintf(stderr, "%s", strerror(errno));
exit(EXIT_FAILURE);
}
errno = 0;
connection = connect(socketfd, info->ai_addr, info->ai_protocol);
connection = connect(socketfd, info->ai_addr, info->ai_addrlen);
if (connection != 0) {
fprintf(stderr, "\nUnable to connect.\n");
fprintf(stderr, "%s", strerror(errno));
fprintf(stderr, "%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
return socketfd;
}
int execate_request(int socketfd, char *hostname, char *path) {
char *request = "GET /index.shtml HTTP/1.1\\r\\nHost: www.cs.iastate.edu\\r\\n\\r\\n";
ssize_t sentByte = send(socketfd, request, strlen(request), 0);
/// Given a url, hostname, and socket will create a request and fire it off the the server
/// \param socketfd socket to use
/// \param hostname hostname to use, used in the headers
/// \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) {
// Reqeust is a buffer
GrowthBuffer request;
// Init the buffer
initBuffer(&request, 500);
// Figure out the verb
char *firstLine;
if (head) {
firstLine = "HEAD ";
} else {
firstLine = "GET ";
}
// Building verb part
appendBuffer(&request, firstLine, strlen(firstLine));
appendBuffer(&request, path, strlen(path));
char *endOfFirstLine = " HTTP/1.1\r\n";
appendBuffer(&request, endOfFirstLine, strlen(endOfFirstLine));
// Build host header
char *hostLine = "Host: ";
appendBuffer(&request, hostLine, strlen(hostLine));
appendBuffer(&request, hostname, strlen(hostname));
appendBuffer(&request, "\r\n", 2);
// Close off the request
appendBuffer(&request, "\r\n", 2);
appendBuffer(&request, "\r\n", 2);
// Send the request
ssize_t sentByte = send(socketfd, request.array, strlen(request.array), 0);
printf("%s", request.array);
return sentByte;
}
int get_result(int socketfd, char *recvBuff, int recvBuffSize) {
int bytesRecv;
int get_result(int socketfd, GrowthBuffer *recvBuff, int recvBuffSize) {
size_t bytesRecv;
char revcData[recvBuffSize];
char recvData[recvBuffSize];
while (1) {
bytesRecv = recv(socketfd, )
bytesRecv = recv(socketfd, revcData, RECV_SIZE, 0);
if (bytesRecv == -1) {
fprintf(stderr, "\n Unable to recv data from socket.");
return -1;
} else if (bytesRecv == 0) {
return 0;
}
if (bytesRecv > 0) {
appendBuffer(recvBuff, revcData, bytesRecv);
}
if (bytesRecv < RECV_SIZE) {
return 0;
}
}
}
\ No newline at end of file
......@@ -11,10 +11,13 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "buffer.h"
#define RECV_SIZE 1024
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 get_result(int socketfd, char *recvBuff, int recvBuffSize);
int execate_request(int socketfd, char *hostname, char *path, int head);
int get_result(int socketfd, GrowthBuffer *recvBuff, int recvBuffSize);
#endif //COMS352_PROJECT2_PART1_CONNECT_H
......@@ -3,6 +3,7 @@
#include <stdio.h>
#include <ctype.h>
#include "connect.h"
#include "buffer.h"
/// Main method for the program
///
......@@ -54,5 +55,9 @@ int main (int argc, char **argv) {
init_connection(argv[optind], 80, &addressInfo);
int sockfd = establish_connect(addressInfo);
execate_request(sockfd, "www.cs.iastate.edu", "/index.html", 0);
GrowthBuffer gp;
initBuffer(&gp, 50);
get_result(sockfd, &gp, RECV_SIZE);
printf("%s", gp.array);
}
\ 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