Initial commit
This commit is contained in:
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
*.out
|
||||
*.gch
|
||||
vgcore.*
|
||||
bin/
|
||||
.cache/
|
||||
compile_commands.json
|
||||
21
Makefile
Normal file
21
Makefile
Normal file
@@ -0,0 +1,21 @@
|
||||
LIBRARIES = -Iinclude
|
||||
SOURCES = ./src/* ./cmd/server.c
|
||||
OUTPUT_DIR = ./bin
|
||||
OUTPUT = -o ${OUTPUT_DIR}/PROG
|
||||
INSTALL_OUTPUT = ${OUTPUT_DIR}/PROG
|
||||
|
||||
build: output_dir
|
||||
gcc -Wall -pthread ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=server}
|
||||
|
||||
debug: output_dir
|
||||
gcc -Wall -g -pthread ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=server}
|
||||
|
||||
install:
|
||||
mv ${INSTALL_OUTPUT:PROG=server} /usr/bin/
|
||||
|
||||
output_dir:
|
||||
mkdir -p ${OUTPUT_DIR}
|
||||
|
||||
clean:
|
||||
rm -rf $(OUTPUT_DIR) **.h.gch
|
||||
|
||||
98
cmd/server.c
Normal file
98
cmd/server.c
Normal file
@@ -0,0 +1,98 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
// Local Includes
|
||||
#include "httpStruct.h"
|
||||
#include "socketHelp.h"
|
||||
|
||||
#define PORT 8080
|
||||
|
||||
int parseHTTPRequest(char buffer[], struct HTTPRequest *r) {
|
||||
char temp[1];
|
||||
char *token;
|
||||
int line = 0;
|
||||
char * checkLine;
|
||||
checkLine = malloc(1000);
|
||||
|
||||
for (int i = 0; i < strlen(buffer); i++) {
|
||||
temp[0] = buffer[i];
|
||||
if ((!strcmp(temp, "\n")) && (i != 0)) {
|
||||
// Config Check
|
||||
if (line == 0) {
|
||||
token = strtok(checkLine, " ");
|
||||
// HTTP Request Type
|
||||
if (!strcmp(token, "GET")) {
|
||||
r->requestType = malloc(strlen(token));
|
||||
strcpy(r->requestType, token);
|
||||
}
|
||||
} else {
|
||||
token = strtok(checkLine, ":");
|
||||
// Host Check
|
||||
if (!strcmp(token, "Host")) {
|
||||
token = strtok(NULL, "");
|
||||
r->requestHost = malloc(strlen(token));
|
||||
strcpy(r->requestHost, token);
|
||||
}
|
||||
// Reset checkline
|
||||
}
|
||||
if (strlen(checkLine) > 0) {
|
||||
// Clear checkLine
|
||||
memset(checkLine,0,strlen(checkLine));
|
||||
}
|
||||
line++;
|
||||
} else {
|
||||
strcat(checkLine, temp);
|
||||
}
|
||||
}
|
||||
free(checkLine);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int returnRequest(int socket) {
|
||||
char *hello = "HTTP/1.1 200 OK\nContent-Length: 5\nConnection: close\n\nhello\n\n";
|
||||
printf("Returning\n%s", hello);
|
||||
send(socket, hello, strlen(hello), 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int handleRequest(char buffer[], int socket) {
|
||||
struct HTTPRequest r;
|
||||
// Grabbing relevant information out of request
|
||||
parseHTTPRequest(buffer, &r);
|
||||
returnRequest(socket);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
struct sockaddr_in address;
|
||||
int server_fd, new_socket;
|
||||
int checkerr = 0;
|
||||
int addrlen = sizeof(address);
|
||||
|
||||
char buffer[1024] = {};
|
||||
|
||||
checkerr = createSocket(PORT, &server_fd, &address, &addrlen);
|
||||
|
||||
if (checkerr != 0) {
|
||||
perror("Error creating socket");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Handle incoming requests
|
||||
while(1) {
|
||||
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
|
||||
(socklen_t *)&addrlen))<0) {
|
||||
perror("Accept connection error");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
read(new_socket, buffer, 1024);
|
||||
printf("Conn received:\n%s\n", buffer);
|
||||
handleRequest(buffer, new_socket);
|
||||
}
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
9
include/httpStruct.h
Normal file
9
include/httpStruct.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// HTTP Request Struct
|
||||
struct HTTPRequest {
|
||||
char *requestType;
|
||||
char *requestVersion;
|
||||
char *requestHost;
|
||||
};
|
||||
5
include/socketHelp.h
Normal file
5
include/socketHelp.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#include <stdio.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
int createSocket(int port, int *server_fd, struct sockaddr_in *address, int *addrlen);
|
||||
|
||||
38
src/socketHelp.c
Normal file
38
src/socketHelp.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
int createSocket(int port, int *server_fd, struct sockaddr_in *address, int *addrlen) {
|
||||
int opt = 1;
|
||||
|
||||
// Create socket fd
|
||||
if ((*server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
|
||||
perror("socket failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Attach socket to PORT
|
||||
if (setsockopt(*server_fd, SOL_SOCKET, SO_REUSEADDR |SO_REUSEPORT,
|
||||
&opt, sizeof(opt))) {
|
||||
perror("Set socket opt failure");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
address->sin_family = AF_INET;
|
||||
address->sin_addr.s_addr = INADDR_ANY;
|
||||
address->sin_port = htons (port);
|
||||
|
||||
// Attach to PORT
|
||||
if (bind(*server_fd, (struct sockaddr *)address,
|
||||
sizeof(*address))<0) {
|
||||
perror("Failed to bind");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (listen(*server_fd, 3) < 0) {
|
||||
perror("Failed to listen");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user