Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
# Written: May 8, 2014

GCC = gcc -g -O0 -Wall -Wextra -std=gnu99 -pthread
CSOURCE = http_parser.c utils.c proxy_server.c
CHEADER = http_parser.h utils.h
CSOURCE = http_parser.c http_response.c utils.c proxy_server.c
CHEADER = http_parser.h http_response.h utils.h

all: proxy_server

proxy_server: proxy_server.o utils.o http_parser.o log.o siteblock.o
${GCC} -o proxy_server proxy_server.o utils.o http_parser.o log.o siteblock.o
proxy_server: proxy_server.o utils.o http_parser.o http_response.o log.o siteblock.o
${GCC} -o proxy_server proxy_server.o utils.o http_parser.o http_response.o log.o siteblock.o

proxy_server.o: proxy_server.c
${GCC} -c proxy_server.c
Expand All @@ -20,6 +20,9 @@ utils.o: utils.c
http_parser.o: http_parser.c
${GCC} -c http_parser.c

http_response.o: http_response.c
${GCC} -c http_response.c

log.o: log.c
${GCC} -c log.c

Expand Down
72 changes: 72 additions & 0 deletions http_response.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// File: http_response.c
// Created June 9, 2014
// Michael Baptist - [email protected]

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <dirent.h>
#include <sys/stat.h>

// comment this out to turn on debug prints.
//#define NDEBUG NDEBUG

#include "http_response.h"
#include "utils.h"

#define SUCCESS 0
#define FAILURE 1

void forbidden_response(char *buf, size_t buflen) {
char buffer[buflen];
bzero(buffer, buflen);
strncpy(buffer, "HTTP/1.1 403 Forbidden\r\n", 24);
strncat(buffer, "Content-type: text/html\n\n", 25);
strncat(buffer, "<html>\n", 7);
strncat(buffer, " <body>\n", 8);
strncat(buffer, " <h1>403 Forbidden</h1>\n", 25);
strncat(buffer, " <p>Access to this site is denied.</p>\n", 36);
strncat(buffer, " <body>\n", 8);
strncat(buffer, "<html>\n", 7);
strncat(buffer, "\r\n", 2);
strncat(buffer, "\r\n", 2);
memcpy(buf, buffer, buflen);
}

void unimplmented_response(char *buf, size_t buflen) {
char buffer[buflen];
bzero(buffer, buflen);
strncpy(buffer, "HTTP/1.1 501 Not Implemented\r\n", 24);
strncat(buffer, "Content-type: text/html\n\n", 25);
strncat(buffer, "<html>\n", 7);
strncat(buffer, " <body>\n", 8);
strncat(buffer, " <h1>501 Not Implemented</h1>\n", 31);
strncat(buffer, " <p>The HTTP request you made is not implemented.</p>\n", 55);
strncat(buffer, " <body>\n", 8);
strncat(buffer, "<html>\n", 7);
strncat(buffer, "\r\n", 2);
strncat(buffer, "\r\n", 2);
memcpy(buf, buffer, buflen);
}


// returns a http response
char *http_response(const uint status) {
char *buffer = calloc(1, 1024);
if (403 == status) {
forbidden_response(buffer, 1024);
return buffer;
} else if (501 == status) {
unimplmented_response(buffer, 1024);
return buffer;
}
return NULL;
}





34 changes: 34 additions & 0 deletions http_response.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// File: http_response.h
// Created June 9, 2014
// Michael Baptist - [email protected]

#ifndef __HTTP_RESPONSE_H__
#define __HTTP_RESPONSE_H__

//#define NDEBUG NoDebug

/**
* @file http_response.h
* This file contains the http header and body response functions.
*/

/**
* This function will return a formatted http response for the given status. Must be freed after use due to allocated on the heap.
*
* @param status The http status to respond to a request with.
*
* @return Returns a formatted http response or null if not implemented.
*
*/
char *http_response(const uint status);




#endif






Binary file modified proxy-app-protocol-mbaptist.odt
Binary file not shown.
Binary file modified proxyCodeDocs.pdf
Binary file not shown.
9 changes: 7 additions & 2 deletions proxy_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ EXIT STATUS

#include "utils.h"
#include "http_parser.h"
#include "http_response.h"
#include "log.h"
#include "siteblock.h"

Expand Down Expand Up @@ -254,8 +255,9 @@ void *handle_client_request(void *c) {
} else {
//log_request(timestamp, httptokens[0], httptokens[2], inet_ntoa(client_info.sin_addr), httptokens[1], NULL, "Filtered", "HTTP command not supported");
fprintf(stderr, "%s command not supported.\n", httptokens[0]);
write(client_socket, "501 Not Implemented",
strlen("501 Not Implemented"));
char *response = http_response(501);
send(client_socket, response, strlen(response), MSG_NOSIGNAL);
free(response);
free_parse_allocs(httptokens, 100);
free_parse_allocs(httpstrs, numlines);
close_client(client_socket, &master);
Expand All @@ -276,6 +278,9 @@ void *handle_client_request(void *c) {
if (allowed_site(httptokens[1]) == -1) {
//log_request(timestamp, command, http_version, inet_ntoa(client_info.sin_addr), uri, NULL, "Filtered", "Site Blocked");
fprintf(stderr, "Error: site not allowed by proxy.\n");
char *response = http_response(403);
send(client_socket, response, strlen(response), MSG_NOSIGNAL);
free(response);
free_parse_allocs(httpstrs, numlines);
free_parse_allocs(httptokens, 100);
close_client(client_socket, &master);
Expand Down