Code:
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define HTTP_PORT "80"
#define BUFFER_SIZE 1024
int get_host(char *host, char *url);
int get_path(char *path, char *url, int pathPosition);
int find_substring(char *string, char *substring);
int
main(int argc, char *argv[])
{
char *host, length[20], *path, readBuffer[BUFFER_SIZE], *p, request[250];
int c, cLength, contentLengthEnd, headerEnd, i, pathPosition, readBytes, sent, sock, status;
struct addrinfo hints, *res;
if(argc != 2) {
printf("Usage: %s [FILE]\n", argv[0]);
return 1;
}
memset(&hints, '\0', sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if((host = (char *)malloc(strlen(argv[1]) + 1)) == NULL) {
fprintf(stderr, "Error: Failed to allocate host memory.\n");
return 1;
}
pathPosition = get_host(host, argv[1]);
if((path = (char *)malloc(sizeof(argv[1]) + 1)) == NULL) {
fprintf(stderr, "Error: Failed to allocate path memory.\n");
return 1;
}
get_path(path, argv[1], pathPosition);
printf("Main Path 1: %s\n", path);
if((status = getaddrinfo(host, HTTP_PORT, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo() encountered an error: %s\n", gai_strerror(status));
return 1;
}
if((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
perror("socket() encountered an error");
return 1;
}
if((connect(sock, res->ai_addr, res->ai_addrlen)) != 0) {
perror("connect() encountered an error");
return 1;
}
snprintf(request, sizeof(request), "GET %s HTTP/1.1\nHost: %s\nUser-Agent: Firefox\n\n", path, host);
if((sent = send(sock, request, sizeof(request), 0)) <= 0) {
perror("send() encountered an error");
return 1;
}
if((readBytes = recv(sock, readBuffer, 1024, 0)) < 0) {
perror("recv() encountered an error");
return 1;
}
printf("Main Path 2: %s\n", path);
readBuffer[readBytes] = '\0';
if((p = strstr(readBuffer, "\r\n\r\n")) == NULL) {
fprintf(stderr, "Error: Couldn't find substring!\n");
return 1;
}
headerEnd = p - readBuffer;
if((p = strstr(readBuffer, "Content-Length:")) == NULL) {
fprintf(stderr, "Error: Couldn't find substring!\n");
return 1;
}
cLength = p - readBuffer;
printf("Header End: %d\n", headerEnd);
printf("Content Length End: %d\n", contentLengthEnd);
for(i = 0; (c = readBuffer[contentLengthEnd + i]) != '\n'; i++)
length[i] = c;
length[i] = '\0';
cLength = atoi(length);
printf("Content Length: %d\n", cLength);
free(host);
free(path);
freeaddrinfo(res);
close(sock);
return 0;
}
int
get_host(char *host, char *url)
{
int c, i, j = 0;
for(i = 7; (c = url[i]) != '/'; i++)
host[j++] = c;
host[j] = '\0';
return i;
}
int
get_path(char *path, char *url, int pathPosition)
{
int c, i, j = 0;
for(i = pathPosition; (c = url[i]) != '\0'; i++)
path[j++] = c;
printf("Function Path 1: %s\n", path);
path[j] = '\0';
printf("Function Path 2: %s\n", path);
return 0;
}
Sorry it takes me so long to reply, this is my first time writing this and it's all a bit strange and confusing when random letters start popping up.