Now working for simple, non-encrypted requests
making requests to something like example.com over a non-encrypted connection now works. Binary files are unlikely to work at the moment although I haven't tried. Also, non-encrypted doesn't work. I have also changed a little about how tests work. Requests tests now display much better.
This commit is contained in:
parent
a91a264a7a
commit
8a5bfe9b36
18 changed files with 725 additions and 148 deletions
71
src/proxy.c
71
src/proxy.c
|
@ -1,12 +1,50 @@
|
|||
#include "proxy.h"
|
||||
|
||||
void proxy_startListener(unsigned int port){
|
||||
Response *upstreamGetResponse(Request *request){
|
||||
//Here we pretend to be a client
|
||||
|
||||
int client_fd = 0;
|
||||
struct sockaddr_in address;
|
||||
memset( &address, 0, sizeof(address) );
|
||||
struct hostent *host = gethostbyname(request->host);
|
||||
Response *rsp = NULL;
|
||||
|
||||
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
|
||||
perror("socket failed");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_port = htons( 80 );
|
||||
// We want the request to go out to whatever the host was resolved to
|
||||
memcpy( &address.sin_addr, host->h_addr_list[0], host->h_length );
|
||||
|
||||
if((connect(client_fd, (struct sockaddr *)&address, sizeof(address)))<0) {
|
||||
perror("connect failed");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *toSend = requestToString(request);
|
||||
|
||||
if ( write( client_fd, toSend, strlen(toSend) ) != strlen(toSend) ){
|
||||
perror( "Write Error" );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rsp = newResponseFromSocket( client_fd );
|
||||
|
||||
return rsp;
|
||||
|
||||
}
|
||||
|
||||
void proxy_startListener(unsigned int port){
|
||||
//we need to act as an http server
|
||||
int server_fd, new_socket;
|
||||
struct sockaddr_in address;
|
||||
int opt = 1;
|
||||
memset( &address, 0, sizeof(address) );
|
||||
int addrlen = sizeof(address);
|
||||
char *response;
|
||||
Response *response;
|
||||
|
||||
// Creating socket file descriptor
|
||||
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
|
||||
|
@ -30,25 +68,44 @@ void proxy_startListener(unsigned int port){
|
|||
}
|
||||
|
||||
while ( true ){
|
||||
printf("Listening\n");
|
||||
printf("Listening on port %i\n", port);
|
||||
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
|
||||
(socklen_t*)&addrlen))<0) {
|
||||
perror("accept");
|
||||
return ;
|
||||
}
|
||||
|
||||
//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 {
|
||||
printf("This is a proxy request\n");
|
||||
response = upstreamGetResponse(request);
|
||||
}
|
||||
|
||||
send(new_socket , response, strlen(response) , 0 );
|
||||
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);
|
||||
close(new_socket);
|
||||
freeRequest( request );
|
||||
freeResponse( response );
|
||||
free(responseStr);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue