initial http parsing
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
BasedOnStyle: Google
|
BasedOnStyle: Google
|
||||||
ColumnLimit: 120
|
ColumnLimit: 120
|
||||||
UseTab: Always
|
UseTab: Always
|
||||||
IndentWidth: 8
|
IndentWidth: 4
|
||||||
TabWidth: 8
|
TabWidth: 4
|
||||||
|
|||||||
48
include/http/http.h
Normal file
48
include/http/http.h
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
#ifndef __HTTP_H__
|
||||||
|
#define __HTTP_H__
|
||||||
|
|
||||||
|
#include <stdio.h> /* Debug */
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define WWW "../www/" /**< Directory used to get html files */
|
||||||
|
/** In the future I'll move conf stuff under a server struct, I can skip just because I want something that works */
|
||||||
|
#define LOCATION_LEN 1024
|
||||||
|
#define HTTP_VERSION_LEN 8
|
||||||
|
#define USER_AGENT_LEN 1024
|
||||||
|
#define HOST_LEN 1024
|
||||||
|
|
||||||
|
enum http_method {
|
||||||
|
GET, /**< GET method */
|
||||||
|
POST, /**< POST method */
|
||||||
|
};
|
||||||
|
/* In the future I'll add HEAD, PUT, DELETE */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief HTTP request struct
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
typedef struct http {
|
||||||
|
enum http_method method; /**< HTTP request method */
|
||||||
|
char location[LOCATION_LEN]; /**< Resource requested */
|
||||||
|
char http_version[HTTP_VERSION_LEN]; /**< HTTP version */
|
||||||
|
char user_agent[USER_AGENT_LEN]; /**< User-Agent */
|
||||||
|
char host[HOST_LEN]; /**< Host */
|
||||||
|
} http_t;
|
||||||
|
/* Connection */
|
||||||
|
/* Accept-Encoding */
|
||||||
|
/* Accept-Language */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Parses a HTTP request
|
||||||
|
*
|
||||||
|
* @param request[in] The http request sent to the server
|
||||||
|
* @return Returns a http_t pointer to the request
|
||||||
|
*/
|
||||||
|
http_t *http_parse(char *request_str);
|
||||||
|
|
||||||
|
void http_parse_method(http_t *request, const char *method);
|
||||||
|
void http_free(http_t *request);
|
||||||
|
void http_send_response(http_t *request);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "hashmap.h"
|
#include "utils/hashmap.h"
|
||||||
|
|
||||||
/* On which port the server will run */
|
/* On which port the server will run */
|
||||||
#define PORT 3030
|
#define PORT 3030
|
||||||
@@ -33,12 +33,12 @@
|
|||||||
*
|
*
|
||||||
* @param hostname[in] The hostname of the server (default localhost, it could be NULL)
|
* @param hostname[in] The hostname of the server (default localhost, it could be NULL)
|
||||||
* @param service[in] The service (found in /etc/services) or the port where to run
|
* @param service[in] The service (found in /etc/services) or the port where to run
|
||||||
* @return int 0 on success, -1 on error
|
* @return 0 on success, -1 on error
|
||||||
*/
|
*/
|
||||||
int start_server(const char *hostname, const char *service);
|
int start_server(const char *hostname, const char *service);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Sets the up hints object
|
* @brief Setups hints object
|
||||||
*
|
*
|
||||||
* @param hints[out] The hints addrinfo
|
* @param hints[out] The hints addrinfo
|
||||||
* @param len[in] The length of hints
|
* @param len[in] The length of hints
|
||||||
@@ -62,7 +62,6 @@ void handle_clients(int sockfd);
|
|||||||
*/
|
*/
|
||||||
void epoll_ctl_add(int epfd, int sockfd, uint32_t events);
|
void epoll_ctl_add(int epfd, int sockfd, uint32_t events);
|
||||||
|
|
||||||
/* Remove a file descriptor from the interest list */
|
|
||||||
/**
|
/**
|
||||||
* @brief Removes a file descriptor from the interest list
|
* @brief Removes a file descriptor from the interest list
|
||||||
*
|
*
|
||||||
@@ -84,7 +83,7 @@ void setnonblocking(int sockfd);
|
|||||||
* @param sockfd[in] Server's file descriptor
|
* @param sockfd[in] Server's file descriptor
|
||||||
* @param their_sa[out] Populates the struct with client's information
|
* @param their_sa[out] Populates the struct with client's information
|
||||||
* @param theirsa_size[in] Size of the struct
|
* @param theirsa_size[in] Size of the struct
|
||||||
* @return int Returns -1 on error or the file descriptor on success
|
* @return Returns -1 on error or the file descriptor on success
|
||||||
*/
|
*/
|
||||||
int handle_new_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size);
|
int handle_new_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size);
|
||||||
|
|
||||||
@@ -9,18 +9,22 @@
|
|||||||
/* Each process on Linux can have a maximum of 1024 open file descriptors */
|
/* Each process on Linux can have a maximum of 1024 open file descriptors */
|
||||||
#define HASHMAP_MAX_CLIENTS 1024
|
#define HASHMAP_MAX_CLIENTS 1024
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Hash map struct
|
||||||
|
*
|
||||||
|
*/
|
||||||
typedef struct bucket {
|
typedef struct bucket {
|
||||||
int sockfd; /**< Client socket descriptor */
|
int sockfd; /**< Client socket descriptor */
|
||||||
struct sockaddr_storage sas; /**< Associated socket address */
|
struct sockaddr_storage sas; /**< Associated socket address */
|
||||||
struct bucket *next; /**< Next node in case of collision */
|
struct bucket *next; /**< Next node in case of collision */
|
||||||
struct bucket *prev; /**< Previous node in case of collision */
|
struct bucket *prev; /**< Previous node in case of collision */
|
||||||
} bucket_t;
|
} bucket_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Calculates the hash code of a given file descriptor
|
* @brief Calculates the hash code of a given file descriptor
|
||||||
*
|
*
|
||||||
* @param sockfd[in] The file descriptor
|
* @param sockfd[in] The file descriptor
|
||||||
* @return int Returns the hash code
|
* @return Returns the hash code
|
||||||
*/
|
*/
|
||||||
int hash(int sockfd);
|
int hash(int sockfd);
|
||||||
|
|
||||||
@@ -53,7 +57,7 @@ void hm_remove(bucket_t *bucket, int sockfd);
|
|||||||
*
|
*
|
||||||
* @param bucket[in] The hash map
|
* @param bucket[in] The hash map
|
||||||
* @param sockfd[in] The file descriptor (key)
|
* @param sockfd[in] The file descriptor (key)
|
||||||
* @return struct hashmap* Returns NULL or the key pointer
|
* @return Returns NULL or the key pointer
|
||||||
*/
|
*/
|
||||||
bucket_t *hm_lookup(bucket_t *bucket, int sockfd);
|
bucket_t *hm_lookup(bucket_t *bucket, int sockfd);
|
||||||
|
|
||||||
@@ -9,3 +9,18 @@ Accept-Language: en-us
|
|||||||
Accept-Encoding: gzip, deflate
|
Accept-Encoding: gzip, deflate
|
||||||
Connection: Keep-Alive
|
Connection: Keep-Alive
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The first line is a *request line*. It has:
|
||||||
|
- Method (GET, POST, HEAD, ...)
|
||||||
|
- Location (the request resource, file)
|
||||||
|
- HTTP version
|
||||||
|
|
||||||
|
# HTTP Response
|
||||||
|
```bash
|
||||||
|
HTTP/1.1 200 OK\r\n
|
||||||
|
Content-Type: text/html\r\n
|
||||||
|
Content-Length: 88\r\n
|
||||||
|
Connection: Closed\r\n
|
||||||
|
\r\n
|
||||||
|
<HTML>
|
||||||
|
```
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
// THIS FILE IS ONLY A TEST FOR THE BASIC STUFF
|
#include "client/client.h"
|
||||||
|
|
||||||
#include "client.h"
|
#include "utils/colors.h"
|
||||||
|
|
||||||
#include "colors.h"
|
|
||||||
|
|
||||||
int test_client_connection(const char *hostname, const char *service) {
|
int test_client_connection(const char *hostname, const char *service) {
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
@@ -32,6 +30,7 @@ int test_client_connection(const char *hostname, const char *service) {
|
|||||||
|
|
||||||
fprintf(stdout, "[client] => ");
|
fprintf(stdout, "[client] => ");
|
||||||
fgets(buf, sizeof buf, stdin);
|
fgets(buf, sizeof buf, stdin);
|
||||||
|
buf[strcspn(buf, "\n")] = '\0';
|
||||||
send(sockfd, buf, strlen(buf), 0);
|
send(sockfd, buf, strlen(buf), 0);
|
||||||
|
|
||||||
freeaddrinfo(res);
|
freeaddrinfo(res);
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
#include "client.h"
|
|
||||||
#include "colors.h"
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
|
||||||
|
#include "client/client.h"
|
||||||
|
#include "utils/colors.h"
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
fprintf(stdout, BOLD GREEN "[client] Running client...\n" RESET);
|
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 "main.h"
|
||||||
|
|
||||||
#include "colors.h"
|
#include "server/server.h"
|
||||||
#include "server.h"
|
#include "utils/colors.h"
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
fprintf(stdout, BOLD GREEN "[server] Running cws...\n" RESET);
|
fprintf(stdout, BOLD GREEN "[server] Running cws...\n" RESET);
|
||||||
|
|||||||
@@ -1,2 +1,5 @@
|
|||||||
server = files('main.c', 'server.c', 'utils.c', 'hashmap.c')
|
server = files('main.c', 'server/server.c')
|
||||||
client = files('mainc.c', 'client.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 "http/http.h"
|
||||||
#include "hashmap.h"
|
#include "utils/colors.h"
|
||||||
#include "utils.h"
|
#include "utils/hashmap.h"
|
||||||
|
#include "utils/utils.h"
|
||||||
|
|
||||||
int start_server(const char *hostname, const char *service) {
|
int start_server(const char *hostname, const char *service) {
|
||||||
struct addrinfo hints;
|
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);
|
int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||||
fprintf(stdout, YELLOW "[server] sockfd: %d\n" RESET, sockfd);
|
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);
|
status = bind(sockfd, res->ai_addr, res->ai_addrlen);
|
||||||
if (status != 0) {
|
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);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +85,10 @@ void handle_clients(int sockfd) {
|
|||||||
for (int i = 0; i < nfds; ++i) {
|
for (int i = 0; i < nfds; ++i) {
|
||||||
if (revents[i].data.fd == sockfd) {
|
if (revents[i].data.fd == sockfd) {
|
||||||
/* New client */
|
/* New client */
|
||||||
|
char ip[INET_ADDRSTRLEN];
|
||||||
client_fd = handle_new_client(sockfd, &their_sa, &theirsa_size);
|
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);
|
setnonblocking(client_fd);
|
||||||
epoll_ctl_add(epfd, client_fd, EPOLLIN);
|
epoll_ctl_add(epfd, client_fd, EPOLLIN);
|
||||||
@@ -102,17 +113,26 @@ void handle_clients(int sockfd) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
data[strcspn(data, "\n")] = '\0';
|
// fprintf(stdout, "[server] Bytes read (%d):\n%s\n", bytes_read, data);
|
||||||
fprintf(stdout, "[server] Bytes read (%d): %s\n", bytes_read, data);
|
|
||||||
if (strcmp(data, "stop") == 0) {
|
if (strcmp(data, "stop") == 0) {
|
||||||
fprintf(stdout, GREEN BOLD "[server] Stopping...\n" RESET);
|
fprintf(stdout, GREEN BOLD "[server] Stopping...\n" RESET);
|
||||||
run = 0;
|
run = 0;
|
||||||
break;
|
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);
|
free(revents);
|
||||||
close(epfd);
|
close(epfd);
|
||||||
close_fds(clients);
|
close_fds(clients);
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "hashmap.h"
|
#include "utils/hashmap.h"
|
||||||
|
|
||||||
int hash(int sockfd) { return sockfd % HASHMAP_MAX_CLIENTS; }
|
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) {
|
void print_ips(const char *hostname, const char *port) {
|
||||||
struct addrinfo ai;
|
struct addrinfo ai;
|
||||||
14
www/index.html
Normal file
14
www/index.html
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>cws</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<h1>Hello from cws!</h1>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user