Initial Commit
This commit is contained in:
parent
284cedb680
commit
076c2c8869
|
|
@ -0,0 +1,8 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CMakePythonSetting">
|
||||||
|
<option name="pythonIntegrationState" value="YES" />
|
||||||
|
</component>
|
||||||
|
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/untitled1.iml" filepath="$PROJECT_DIR$/.idea/untitled1.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
cmake_minimum_required(VERSION 3.28)
|
||||||
|
project(HttpServer C)
|
||||||
|
|
||||||
|
set(CMAKE_C_STANDARD 23)
|
||||||
|
|
||||||
|
add_executable(HttpServer main.c
|
||||||
|
handle_client.c
|
||||||
|
handle_client.h
|
||||||
|
util.c
|
||||||
|
util.h)
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
//
|
||||||
|
// Created by franchioping on 6/24/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "handle_client.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define BUFFER_SIZE 256
|
||||||
|
|
||||||
|
char** request_get_lines_from_buff(const char* buff, size_t buff_len, int* lines_count){
|
||||||
|
int line_count = 0;
|
||||||
|
for(int i = 0; i < buff_len-1; i++){
|
||||||
|
if(buff[i] == '\r' && buff[i+1]=='\n')
|
||||||
|
line_count ++;
|
||||||
|
}
|
||||||
|
char** lines = (char**) malloc(sizeof(char*) * line_count);
|
||||||
|
int line_start = 0;
|
||||||
|
int line_num = 0;
|
||||||
|
for(int i = 0; i < buff_len; i++){
|
||||||
|
if(buff[i] == '\r' && buff[i+1]=='\n'){
|
||||||
|
char* line = malloc(sizeof(char) * (i - line_start + 1));
|
||||||
|
memcpy(line, &buff[line_start], i - line_start);
|
||||||
|
line[i - line_start] = '\0';
|
||||||
|
|
||||||
|
lines[line_num++] = line;
|
||||||
|
|
||||||
|
line_start=i+2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*lines_count = line_count;
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
void handle_client(void* arg){
|
||||||
|
int client_fd = *((int*) arg);
|
||||||
|
|
||||||
|
char* buff = (char*) malloc(BUFFER_SIZE * sizeof(char));
|
||||||
|
|
||||||
|
ssize_t bytes_received = recv(client_fd, buff, BUFFER_SIZE, 0);
|
||||||
|
if(bytes_received < 0){
|
||||||
|
printf("Failed to receive.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print Request
|
||||||
|
printf("Bytes Received: %lu\n", bytes_received);
|
||||||
|
printf("Request:\n");
|
||||||
|
|
||||||
|
//print_string(buff, bytes_received);
|
||||||
|
|
||||||
|
int lines_count = 0;
|
||||||
|
char** lines = request_get_lines_from_buff(buff, bytes_received, &lines_count);
|
||||||
|
|
||||||
|
for(int i = 0; i < lines_count; i++){
|
||||||
|
char* line = lines[i];
|
||||||
|
printf("%s\n", line);
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
free(lines);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
//
|
||||||
|
// Created by franchioping on 6/24/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef UNTITLED1_HANDLE_CLIENT_H
|
||||||
|
#define UNTITLED1_HANDLE_CLIENT_H
|
||||||
|
|
||||||
|
void handle_client(void* arg);
|
||||||
|
|
||||||
|
|
||||||
|
#endif //UNTITLED1_HANDLE_CLIENT_H
|
||||||
|
|
@ -0,0 +1,64 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
|
||||||
|
|
||||||
|
#include "handle_client.h"
|
||||||
|
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int serv_sock_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
|
||||||
|
if(serv_sock_fd < 0){
|
||||||
|
printf("Error while creating socket\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int enable = 1;
|
||||||
|
if (setsockopt(serv_sock_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
|
||||||
|
printf("setsockopt(SO_REUSEADDR) failed");
|
||||||
|
printf("Created Socket File Descriptor Successfully.\n");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
struct sockaddr_in server_addr;
|
||||||
|
|
||||||
|
server_addr.sin_family = AF_INET;
|
||||||
|
server_addr.sin_port = htons(8080);
|
||||||
|
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||||
|
|
||||||
|
if(bind(serv_sock_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0){
|
||||||
|
printf("Couldn't bind to port\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
printf("Socket Bound to Port Successfully.\n");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(listen(serv_sock_fd, 10) < 0){
|
||||||
|
printf("Couldn't listen to socket\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
printf("Socket Listening...\n");
|
||||||
|
|
||||||
|
while(1){
|
||||||
|
struct sockaddr_in client_addr;
|
||||||
|
socklen_t client_addr_len = sizeof(client_addr);
|
||||||
|
int *client_fd = malloc(sizeof(int));
|
||||||
|
|
||||||
|
*client_fd = accept(serv_sock_fd, (struct sockaddr *)&client_addr, &client_addr_len);
|
||||||
|
if(*client_fd < 0){
|
||||||
|
perror("Accept failed\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("================================\n");
|
||||||
|
printf("Client Connected, Handling...\n");
|
||||||
|
handle_client((void*) client_fd);
|
||||||
|
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
//
|
||||||
|
// Created by franchioping on 6/24/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
void print_string(char* string, size_t string_len){
|
||||||
|
for(int i = 0; i < string_len; i++){
|
||||||
|
if(string[i] == '\r'){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
printf("%c", string[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue