Commit c13eb436 authored by Matthew Burket's avatar Matthew Burket
Browse files

Docs and moving stuff

parent dca912b4
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -3,4 +3,4 @@ project(coms352_project2_part2 C)

set(CMAKE_C_STANDARD 11)

add_executable(coms352_project2_part2 main.c http.c http.h utils.c utils.h buffer.c buffer.h)
 No newline at end of file
add_executable(coms352_project2_part2 main.c http.c http.h utils.c utils.h buffer.c buffer.h network.c network.h file.c file.h)
 No newline at end of file

file.c

0 → 100644
+26 −0
Original line number Diff line number Diff line
//
// Created by mburket on 4/12/18.
//

#include <stdio.h>
#include <zconf.h>
#include <memory.h>
#include <fcntl.h>
#include "file.h"
void serveFile(int clientfd, const char *path) {
    struct _IO_FILE *fp;
    unsigned long numBytesReadFile;
    fp = fopen(path, "r");
    char fileBuff[1024];
    if (fp) {
        printf("\nGot a fp\n");
        while ((numBytesReadFile = fread(fileBuff, 1023, 1, fp)) > 0) {
            write(clientfd, fileBuff, strlen(fileBuff));
        }
    }
    fclose(fp);
}

int checkFile(char *fileName) {
    return access(fileName, R_OK);
}
 No newline at end of file

file.h

0 → 100644
+10 −0
Original line number Diff line number Diff line
//
// Created by mburket on 4/12/18.
//

#ifndef COMS352_PROJECT2_PART2_FILE_H
#define COMS352_PROJECT2_PART2_FILE_H
void serveFile(int clientfd, const char *path);

int checkFile(char *fileName);
#endif //COMS352_PROJECT2_PART2_FILE_H
+5 −142
Original line number Diff line number Diff line
/** @file main.c
 *  @brief Main method for the server
 *
 *  @author Matthew Burket
 */
//
// Created by mburket on 4/11/18.
//

#include <stdio.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
@@ -16,23 +14,7 @@
#include <poll.h>
#include "buffer.h"
#include "http.h"


#define PORT 26885
#define DEBUG 0
#define TRUE 1

int bind_socket();

int accept_connection(int socketfd);

int handle_client(int clientfd);

void getRequest(int clientfd, GrowthBuffer *request);

int checkFile(char *fileName);

void serveFile(int clientfd, const char *path);
#include "network.h"

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
@@ -59,123 +41,4 @@ int main() {

#pragma clang diagnostic pop

///  This creates and binds the socket
///
/// \return the file descriptor of the socket
int bind_socket() {
    int socketfd;
    errno = 0;
    socketfd = socket(AF_INET, SOCK_STREAM, 0);
    if (errno != 0) {
        fprintf(stderr, "\nUnable to create socket.\n");
        fprintf(stderr, strerror(errno));
        fprintf(stderr, "\n");
    }
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(PORT);
    errno = 0;
    bind(socketfd, (struct sockaddr *) &addr, sizeof(addr));
    if (errno != 0) {
        fprintf(stderr, "Unable to bind: [Error %d]: %s", errno, strerror(errno));
        exit(EXIT_FAILURE);
    }
    listen(socketfd, 2048);
    return socketfd;
}

/// Accepts a connect from an open socket
///
/// \param socketfd
/// \return
int accept_connection(int socketfd) {
    struct sockaddr_in client_address;
    socklen_t *client_length = (socklen_t *) sizeof(client_address);
    errno = 0;
    int connection = accept(socketfd, (struct sockaddr *) &client_address, (socklen_t *) &client_length);
    return connection;
}

/// Handles the client from the client file descriptor
///
/// \param clientfd file descriptor of the client
/// \return
int handle_client(int clientfd) {
    int pid = fork();
    if (pid == 0) {
        GrowthBuffer request;
        getRequest(clientfd, &request);
        HTTP_VERB verb = get_http_verb(request.array);
        if (verb == HTTP_UNKNOWN) {
            char *basicHeader = "HTTP/1.1 400 Bad Request\r\n\r\n";
            write(clientfd, basicHeader, strlen(basicHeader));
            shutdown(clientfd, SHUT_RDWR);
            exit(EXIT_SUCCESS);
        }
        char *path = malloc(sizeof(char) * 1024);
        get_http_path(request.array, path);
        fprintf(stdout, "Verb:%d Path:%s\n", verb, path);
        GrowthBuffer response;
        initBuffer(&response, 5000);
        if (checkFile(path) != -1) {
            char *basicHeader = "HTTP/1.1 200 OK\r\n\r\n";
            appendBuffer(&response, basicHeader, strlen(basicHeader));
            write(clientfd, response.array, strlen(response.array));
            if (verb == HTTP_GET) {
                serveFile(clientfd, path);
            }
        } else {
            char *basicHeader = "HTTP/1.1 404 Not Found\r\n\r\n";
            appendBuffer(&response, basicHeader, strlen(basicHeader));
            write(clientfd, response.array, strlen(response.array));
        }

        shutdown(clientfd, SHUT_RDWR);
        free(path);
        freeBuffer(&request);
        exit(EXIT_SUCCESS);
    }
#ifdef DEBUG
    fprintf(stdout, "\nProcessing client on pid %d\n", pid);
#endif
}

void serveFile(int clientfd, const char *path) {
    FILE *fp;
    size_t numBytesReadFile;
    fp = fopen(path, "r");
    char fileBuff[1024];
    if (fp) {
        printf("\nGot a fp\n");
        while ((numBytesReadFile = fread(fileBuff, 1023, 1, fp)) > 0) {
            write(clientfd, fileBuff, strlen(fileBuff));
        }
    }
    fclose(fp);
}

void getRequest(int clientfd, GrowthBuffer *request) {
    struct pollfd pollfd = {
                .fd = clientfd,
                .events = POLLIN
        };
    ssize_t readCount;
    initBuffer(request, 500);
    char *buff = malloc(1024);
    poll(&pollfd, 1, 0);
    while (pollfd.revents & POLLIN) {
        readCount = read(clientfd, buff, 1024);
        appendBuffer(request, buff, readCount);

        if (readCount <= 0) {
            break;
        }
        poll(&pollfd, 1, 0);
    }
}

int checkFile(char *fileName) {
    return access(fileName, R_OK);
}
 No newline at end of file

network.c

0 → 100644
+119 −0
Original line number Diff line number Diff line
//
// Created by mburket on 4/12/18.
//

#include <sys/socket.h>
#include <stdlib.h>
#include <errno.h>
#include <memory.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <poll.h>

#include "network.h"
#include "buffer.h"
#include "http.h"
#include "file.h"

/// Accepts a connect from an open socket
///
/// \param socketfd
/// \return
int accept_connection(int socketfd) {
    struct sockaddr_in client_address;
    socklen_t *client_length = (socklen_t *) sizeof(client_address);
    errno = 0;
    int connection = accept(socketfd, (struct sockaddr *) &client_address, (socklen_t *) &client_length);
    return connection;
}

/// Handles the client from the client file descriptor
///
/// \param clientfd file descriptor of the client
/// \return
int handle_client(int clientfd) {
    int pid = fork();
    if (pid == 0) {
        GrowthBuffer request;
        getRequest(clientfd, &request);
        HTTP_VERB verb = get_http_verb(request.array);
        if (verb == HTTP_UNKNOWN) {
            char *basicHeader = "HTTP/1.1 400 Bad Request\r\n\r\n";
            write(clientfd, basicHeader, strlen(basicHeader));
            shutdown(clientfd, SHUT_RDWR);
            exit(EXIT_SUCCESS);
        }
        char *path = malloc(sizeof(char) * 1024);
        get_http_path(request.array, path);
        fprintf(stdout, "Verb:%d Path:%s\n", verb, path);
        GrowthBuffer response;
        initBuffer(&response, 5000);
        if (checkFile(path) != -1) {
            char *basicHeader = "HTTP/1.1 200 OK\r\n\r\n";
            appendBuffer(&response, basicHeader, strlen(basicHeader));
            write(clientfd, response.array, strlen(response.array));
            if (verb == HTTP_GET) {
                serveFile(clientfd, path);
            }
        } else {
            char *basicHeader = "HTTP/1.1 404 Not Found\r\n\r\n";
            appendBuffer(&response, basicHeader, strlen(basicHeader));
            write(clientfd, response.array, strlen(response.array));
        }

        shutdown(clientfd, SHUT_RDWR);
        free(path);
        freeBuffer(&request);
        exit(EXIT_SUCCESS);
    }
#ifdef DEBUG
    fprintf(stdout, "\nProcessing client on pid %d\n", pid);
#endif
}

///  This creates and binds the socket
///
/// \return the file descriptor of the socket
int bind_socket() {
    int socketfd;
    errno = 0;
    socketfd = socket(AF_INET, SOCK_STREAM, 0);
    if (errno != 0) {
        fprintf(stderr, "\nUnable to create socket.\n");
        fprintf(stderr, strerror(errno));
        fprintf(stderr, "\n");
    }
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(PORT);
    errno = 0;
    bind(socketfd, (struct sockaddr *) &addr, sizeof(addr));
    if (errno != 0) {
        fprintf(stderr, "Unable to bind: [Error %d]: %s", errno, strerror(errno));
        exit(EXIT_FAILURE);
    }
    listen(socketfd, 2048);
    return socketfd;
}

void getRequest(int clientfd, GrowthBuffer *request) {
    struct pollfd pollfd = {
                .fd = clientfd,
                .events = POLLIN
        };
    ssize_t readCount;
    initBuffer(request, 500);
    char *buff = malloc(1024);
    poll(&pollfd, 1, 0);
    while (pollfd.revents & POLLIN) {
        readCount = read(clientfd, buff, 1024);
        appendBuffer(request, buff, readCount);

        if (readCount <= 0) {
            break;
        }
        poll(&pollfd, 1, 0);
    }
}
 No newline at end of file
Loading