Add connecting to socket

attempt-2
Nick Krichevsky 2018-08-28 00:08:03 -04:00
parent 65fdb4ad69
commit 1c469ad845
5 changed files with 75 additions and 3 deletions

View File

@ -1,6 +1,6 @@
CC = gcc
CFLAGS = -Wall -g -c -std=gnu99
LDFLAGS =
LDFLAGS = -lm
BUILD_DIR = build
COMMON_OBJ_FILES = $(patsubst %.c,$(BUILD_DIR)/%.o,$(wildcard common/*.c))
CLIENT_OBJ_FILES = $(patsubst %.c,$(BUILD_DIR)/%.o,$(wildcard client/*.c))

View File

@ -1,5 +1,6 @@
#include "http_client.h"
#include "../common/http_message.h"
#include "http_socket.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@ -31,6 +32,11 @@ int main(int argc, char* argv[]) {
}
get_parts(dest, path_buffer, host_buffer);
printf("%s\n%s\n", path_buffer, host_buffer);
struct http_message req = build_basic_request("GET", host_buffer, path_buffer, port_num);
char * req_result = send_request(req);
printf("%s", req_result);
free(req_result);
free(req.contents);
free(host_buffer);
free(path_buffer);
}

View File

@ -4,8 +4,6 @@
#define RTT_FLAG "-p"
#define USAGE_STRING "Usage: ./http_client [-p] server_url port_number\n"
#define PORT_ERROR "Port must be a number in the range 1-65535"
#define MIN_PORT 1
#define MAX_PORT 65535
void get_parts(const char *dest, char *path_buffer, char *host_buffer);

View File

@ -1,7 +1,13 @@
#include "http_socket.h"
#include "../common/socket_helper.h"
#include <math.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
/**
* Gets the buffer size to allocate based on the given components.
@ -40,3 +46,62 @@ struct http_message build_basic_request(const char *method, const char *host, co
return message;
}
/**
* Get the address info for a given hostname and port.
*
* @param hostname The hostname to read
* @param port The port to get the address info on
* @param info A pointer to a location where getaddrinfo can store data
*
* @return The result of getadddrinfo. See `man getaddrinfo` for details.
*/
static int get_addr_info(const char *hostname, int port, struct addrinfo **info) {
struct addrinfo hints;
// Force all hints to be null
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
// Get the number of chars in the port
int port_length = floor(log10(port)) + 1;
char *port_string = malloc((port_length + 1) * sizeof(char));
sprintf(port_string, "%d", port);
int addr_status = getaddrinfo(hostname, port_string, &hints, info);
free(port_string);
return addr_status;
}
/**
* Send a given HTTP request
*
* @param req The HTTP request to send
*
* @return The data returned from the socket - must be freed by caller.
*/
char *send_request(struct http_message req) {
struct addrinfo *addr_info;
int addr_status = get_addr_info(req.address, req.port, &addr_info);
if (addr_status != 0) {
return NULL;
}
int socket_fd = socket(addr_info->ai_family, addr_info->ai_socktype, addr_info->ai_protocol);
if (socket_fd < 0) {
return NULL;
}
int connection_status = connect(socket_fd, addr_info->ai_addr, addr_info->ai_addrlen);
if (connection_status != 0) {
return NULL;
}
send(socket_fd, req.contents, strlen(req.contents), 0);
char * remote_parts = get_all_remote_parts(socket_fd);
close(socket_fd);
return remote_parts;
}

View File

@ -4,7 +4,10 @@
#include "../common/http_message.h"
#define HTTP_VERSION "HTTP/1.1"
#define MIN_PORT 1
#define MAX_PORT 65535
struct http_message build_basic_request(const char *method, const char *host, const char *path, int port);
char *send_request(struct http_message req);
#endif