Small changes to proxy

master
Jonathan Hodgson 3 years ago
parent 62ab5ea68d
commit 18d50ed7a6
  1. 22
      src/proxy.c
  2. 1
      src/proxy.h

@ -1,12 +1,12 @@
#include "proxy.h"
void proxy_startListener(unsigned int port){
// https://www.geeksforgeeks.org/socket-programming-cc/
int server_fd, new_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char line[1024] = {0};
char *response;
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
@ -15,19 +15,20 @@ void proxy_startListener(unsigned int port){
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( port );
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) {
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) != 0) {
perror("bind failed");
return ;
}
if (listen(server_fd, 3) < 0) {
if (listen(server_fd, 3) != 0) {
perror("listen");
return ;
}
while ( true ){
printf("Listening\n");
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
@ -37,6 +38,17 @@ void proxy_startListener(unsigned int port){
}
Request *request = newRequestFromSocket(new_socket);
if ( strcmp( request->host, "" ) == 0 ){
response = webserverGetResponse(request);
} else {
printf("This is a proxy request\n");
}
send(new_socket , response, strlen(response) , 0 );
//close(new_socket);
}
}

@ -10,6 +10,7 @@
#include <stdbool.h>
#include "request.h"
#include "webserver.h"
void proxy_startListener( unsigned int port );

Loading…
Cancel
Save