initial http parsing
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
// THIS FILE IS ONLY A TEST FOR THE BASIC STUFF
|
||||
#include "client/client.h"
|
||||
|
||||
#include "client.h"
|
||||
|
||||
#include "colors.h"
|
||||
#include "utils/colors.h"
|
||||
|
||||
int test_client_connection(const char *hostname, const char *service) {
|
||||
struct addrinfo hints;
|
||||
@@ -32,6 +30,7 @@ int test_client_connection(const char *hostname, const char *service) {
|
||||
|
||||
fprintf(stdout, "[client] => ");
|
||||
fgets(buf, sizeof buf, stdin);
|
||||
buf[strcspn(buf, "\n")] = '\0';
|
||||
send(sockfd, buf, strlen(buf), 0);
|
||||
|
||||
freeaddrinfo(res);
|
||||
@@ -1,7 +1,8 @@
|
||||
#include "client.h"
|
||||
#include "colors.h"
|
||||
#include "main.h"
|
||||
|
||||
#include "client/client.h"
|
||||
#include "utils/colors.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
fprintf(stdout, BOLD GREEN "[client] Running client...\n" RESET);
|
||||
|
||||
40
src/http/http.c
Normal file
40
src/http/http.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "http/http.h"
|
||||
|
||||
#include "utils/colors.h"
|
||||
|
||||
http_t *http_parse(char *request_str) {
|
||||
http_t *request = malloc(sizeof(http_t));
|
||||
fprintf(stdout, YELLOW "[http] REQUEST:\n%s\n" RESET, request_str);
|
||||
|
||||
/* Parse HTTP method */
|
||||
char *pch = strtok(request_str, " ");
|
||||
printf("%s\n", pch);
|
||||
http_parse_method(request, pch);
|
||||
|
||||
/* Parse location */
|
||||
pch = strtok(NULL, " ");
|
||||
printf("%s\n", pch);
|
||||
strncpy(request->location, pch, LOCATION_LEN);
|
||||
|
||||
/* Parse HTTP version */
|
||||
pch = strtok(NULL, " \r\n");
|
||||
printf("%s\n", pch);
|
||||
strncpy(request->http_version, pch, HTTP_VERSION_LEN);
|
||||
|
||||
/* Parse other stuff... */
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
void http_parse_method(http_t *request, const char *method) {
|
||||
if (strcmp(method, "GET") == 0) {
|
||||
request->method = GET;
|
||||
}
|
||||
if (strcmp(method, "POST") == 0) {
|
||||
request->method = POST;
|
||||
}
|
||||
}
|
||||
|
||||
void http_send_response(http_t *request) { /* TODO */ }
|
||||
|
||||
void http_free(http_t *request) { free(request); }
|
||||
@@ -1,7 +1,8 @@
|
||||
#include "main.h"
|
||||
|
||||
#include "colors.h"
|
||||
#include "server.h"
|
||||
#include "server/server.h"
|
||||
#include "utils/colors.h"
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
fprintf(stdout, BOLD GREEN "[server] Running cws...\n" RESET);
|
||||
|
||||
@@ -1,2 +1,5 @@
|
||||
server = files('main.c', 'server.c', 'utils.c', 'hashmap.c')
|
||||
client = files('mainc.c', 'client.c')
|
||||
server = files('main.c', 'server/server.c')
|
||||
server += files('utils/utils.c', 'utils/hashmap.c')
|
||||
server += files('http/http.c')
|
||||
|
||||
client = files('client/main.c', 'client/client.c')
|
||||
@@ -1,8 +1,9 @@
|
||||
#include "server.h"
|
||||
#include "server/server.h"
|
||||
|
||||
#include "colors.h"
|
||||
#include "hashmap.h"
|
||||
#include "utils.h"
|
||||
#include "http/http.h"
|
||||
#include "utils/colors.h"
|
||||
#include "utils/hashmap.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
int start_server(const char *hostname, const char *service) {
|
||||
struct addrinfo hints;
|
||||
@@ -19,9 +20,16 @@ int start_server(const char *hostname, const char *service) {
|
||||
int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||
fprintf(stdout, YELLOW "[server] sockfd: %d\n" RESET, sockfd);
|
||||
|
||||
int opt = 1;
|
||||
status = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt);
|
||||
if (status != 0) {
|
||||
fprintf(stderr, RED BOLD "[server] setsockopt(): %s\n" RESET, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
status = bind(sockfd, res->ai_addr, res->ai_addrlen);
|
||||
if (status != 0) {
|
||||
fprintf(stderr, RED BOLD "[server] bind(): %s\n" RESET, gai_strerror(status));
|
||||
fprintf(stderr, RED BOLD "[server] bind(): %s\n" RESET, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -77,7 +85,10 @@ void handle_clients(int sockfd) {
|
||||
for (int i = 0; i < nfds; ++i) {
|
||||
if (revents[i].data.fd == sockfd) {
|
||||
/* New client */
|
||||
char ip[INET_ADDRSTRLEN];
|
||||
client_fd = handle_new_client(sockfd, &their_sa, &theirsa_size);
|
||||
get_client_ip(&their_sa, ip);
|
||||
fprintf(stdout, BLUE "[server] Client (%s) connected\n" RESET, ip);
|
||||
|
||||
setnonblocking(client_fd);
|
||||
epoll_ctl_add(epfd, client_fd, EPOLLIN);
|
||||
@@ -102,17 +113,26 @@ void handle_clients(int sockfd) {
|
||||
continue;
|
||||
}
|
||||
|
||||
data[strcspn(data, "\n")] = '\0';
|
||||
fprintf(stdout, "[server] Bytes read (%d): %s\n", bytes_read, data);
|
||||
// fprintf(stdout, "[server] Bytes read (%d):\n%s\n", bytes_read, data);
|
||||
if (strcmp(data, "stop") == 0) {
|
||||
fprintf(stdout, GREEN BOLD "[server] Stopping...\n" RESET);
|
||||
run = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Parse HTTP request */
|
||||
http_t *request = http_parse(data);
|
||||
fprintf(stdout, "[server] request location: %s\n", request->location);
|
||||
http_send_response(request);
|
||||
http_free(request);
|
||||
|
||||
/* Clear str */
|
||||
memset(data, 0, sizeof data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Clean up everything */
|
||||
free(revents);
|
||||
close(epfd);
|
||||
close_fds(clients);
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "hashmap.h"
|
||||
#include "utils/hashmap.h"
|
||||
|
||||
int hash(int sockfd) { return sockfd % HASHMAP_MAX_CLIENTS; }
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "utils.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
#include "colors.h"
|
||||
#include "utils/colors.h"
|
||||
|
||||
void print_ips(const char *hostname, const char *port) {
|
||||
struct addrinfo ai;
|
||||
Reference in New Issue
Block a user