Moves some logic out of proxy and into main
Also fixes some segfaults caused by trying to free memory that wasn't allocated with strdup or malloc etc. Fixes some tests
This commit is contained in:
parent
8a5bfe9b36
commit
6eaad263be
10 changed files with 162 additions and 121 deletions
51
src/proxy.c
51
src/proxy.c
|
@ -38,18 +38,19 @@ Response *upstreamGetResponse(Request *request){
|
|||
|
||||
}
|
||||
|
||||
void proxy_startListener(unsigned int port){
|
||||
int proxy_startListener(unsigned int port){
|
||||
//we need to act as an http server
|
||||
int server_fd, new_socket;
|
||||
int server_fd;
|
||||
struct sockaddr_in address;
|
||||
memset( &address, 0, sizeof(address) );
|
||||
int addrlen = sizeof(address);
|
||||
Response *response;
|
||||
char *responseStr;
|
||||
|
||||
// Creating socket file descriptor
|
||||
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
|
||||
perror("socket failed");
|
||||
return ;
|
||||
return -1;
|
||||
}
|
||||
|
||||
address.sin_family = AF_INET;
|
||||
|
@ -59,53 +60,15 @@ void proxy_startListener(unsigned int port){
|
|||
// Forcefully attaching socket to the port 8080
|
||||
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) != 0) {
|
||||
perror("bind failed");
|
||||
return ;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (listen(server_fd, 3) != 0) {
|
||||
perror("listen");
|
||||
return ;
|
||||
return -1;
|
||||
}
|
||||
|
||||
while ( true ){
|
||||
printf("Listening on port %i\n", port);
|
||||
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
|
||||
(socklen_t*)&addrlen))<0) {
|
||||
perror("accept");
|
||||
return ;
|
||||
}
|
||||
return server_fd;
|
||||
|
||||
//I think eventually I'd like a different thread here for each request
|
||||
//Not sure how to do that yet though so I'll keep everything on the main
|
||||
//thread
|
||||
|
||||
Request *request = newRequestFromSocket(new_socket);
|
||||
|
||||
//If this is an https request - this is the first part
|
||||
if ( strcmp( request->method, "CONNECT" ) == 0 ){
|
||||
printf("\n\n%s\n\n", requestToString( request ));
|
||||
}
|
||||
|
||||
// If the host is not defined, then it is not a proxy request
|
||||
// Note that the host here is where the request should be sent, not
|
||||
// necesarily the hosts header
|
||||
if ( strcmp( request->host, "" ) == 0 ){
|
||||
response = webserverGetResponse(request);
|
||||
} else {
|
||||
response = upstreamGetResponse(request);
|
||||
}
|
||||
|
||||
char *responseStr = responseToString( response );
|
||||
|
||||
// I'm also not convinced that strlen is the best function to use here
|
||||
// When we get to dealing with binary requests / responses, they may
|
||||
// well have null characters in them
|
||||
send(new_socket , responseStr, strlen(responseStr) , 0 );
|
||||
|
||||
close(new_socket);
|
||||
freeRequest( request );
|
||||
freeResponse( response );
|
||||
free(responseStr);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue