fix: exclude carriage return characters in parsing

This commit is contained in:
kevinj 2024-08-01 12:27:38 +02:00
parent 5f77691429
commit 383699c9ed
2 changed files with 23 additions and 10 deletions

26
proxy.c
View File

@ -10,6 +10,7 @@
#include <regex.h> #include <regex.h>
#include "structs.h" #include "structs.h"
int debug = 1;
struct http_msg *child_msg; struct http_msg *child_msg;
regex_t preg; regex_t preg;
regmatch_t pmatch[REGEX_MATCHN]; regmatch_t pmatch[REGEX_MATCHN];
@ -120,7 +121,6 @@ void free_message() {
free(child_msg); free(child_msg);
} }
int parse_line(char *line, int line_count) { int parse_line(char *line, int line_count) {
int ret = 0; int ret = 0;
@ -155,6 +155,10 @@ void handle_request(int sockfd) {
goto end_sock; goto end_sock;
} }
if (debug) {
fprintf(stdout, "Received buffer: %s\n", msgbuff);
}
char *ln = strtok(msgbuff, "\n"); char *ln = strtok(msgbuff, "\n");
while (ln) { while (ln) {
parse_line(ln, par_line); parse_line(ln, par_line);
@ -193,6 +197,13 @@ int doserver(void) {
return -1; return -1;
} }
int on = 1;
ret = setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (ret < 0) {
fprintf(stderr, "Failed flagging server socket as reusable\n");
return -1;
}
struct sockaddr_in serv_addr; struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr)); memset(&serv_addr, 0, sizeof(serv_addr));
@ -204,7 +215,7 @@ int doserver(void) {
ret = bind(server_socket, (struct sockaddr *) &serv_addr, ret = bind(server_socket, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)); sizeof(serv_addr));
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "Failed to bind to port %d\n", PROXY_PORT); fprintf(stderr, "Failed to bind to port %d\n", PROXY_PORT);
return -1; return -1;
} }
@ -232,12 +243,13 @@ int doserver(void) {
ret = fork(); ret = fork();
switch (ret) { switch (ret) {
case -1: case -1:
fprintf(stderr, "[CLIENT SOCKET %d] Failed to fork child process to" fprintf(stderr, "[CLIENT SOCKET %d] Failed to fork child process"
"handle the request\n", client_socket); "to handle the request\n", client_socket);
return -1; return -1;
break; break;
case 0: case 0:
handle_request(client_socket); handle_request(client_socket);
return 0;
break; break;
default: default:
fprintf(stdout, "[PROGRAM] Successfully forked a new child process" fprintf(stdout, "[PROGRAM] Successfully forked a new child process"
@ -273,7 +285,8 @@ int doclient(void) {
return -1; return -1;
} }
ret = connect(client_socket, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); ret = connect(client_socket, (struct sockaddr *) &serv_addr,
sizeof(serv_addr));
if(ret < 0) if(ret < 0)
{ {
fprintf(stderr, "Failed connecting to remote server\n"); fprintf(stderr, "Failed connecting to remote server\n");
@ -282,7 +295,8 @@ int doclient(void) {
int bytes = 0; int bytes = 0;
do { do {
bytes += send(client_socket, CLIENT_MESSAGE, sizeof(CLIENT_MESSAGE), 0); bytes += send(client_socket, CLIENT_MESSAGE,
sizeof(CLIENT_MESSAGE), 0);
} while (bytes != sizeof(CLIENT_MESSAGE)); } while (bytes != sizeof(CLIENT_MESSAGE));
fprintf(stdout, "Sent %d bytes to server\n", bytes); fprintf(stdout, "Sent %d bytes to server\n", bytes);

View File

@ -9,9 +9,8 @@
#define PROXY_MAX_MSGLEN 10*1024 #define PROXY_MAX_MSGLEN 10*1024
#define REGEX_MATCHN 4 #define REGEX_MATCHN 4
#define REGEX_TITLE "^([A-Z]+)[ ]+([a-zA-Z0-9\\:\\/\\_\\-\\.\\,]+)"\ #define REGEX_TITLE "^([A-Z]+)[ ]+([a-zA-Z0-9\\:/_.,-]+)[ ]+([a-zA-Z0-9_.,/-]+)[\n\r]*$"
"[ ]+([a-zA-Z0-9\\_\\-\\.\\,\\/]+)[ ]*$|\n|\r" #define REGEX_HEADER "^([a-zA-Z0-9_-]*):[ \t]+([^\r\n]*)"
#define REGEX_HEADER "^([a-zA-Z0-9\\-\\_]*):[ \t]+(.*)$|\n|\r"
#define CLIENT_MESSAGE "GET http://google.com/auth HTTP/1.0\n"\ #define CLIENT_MESSAGE "GET http://google.com/auth HTTP/1.0\n"\
"\n"\ "\n"\