Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem: Create a web proxy server that can be connected by a single client and

ID: 3592787 • Letter: P

Question

Problem:

Create a web proxy server that can be connected by a single client and would only allow http requests. The proxy server should be able to cache up to five recent websites.

Requirements:

1. Create a C/ Java based client-server architecture using sockets

2. The proxy server should be able to accept and service single client’s http requests

3. The server should be run on cse01.cse.unt.edu machine and the clients should be run on any machine between cse02.cse.unt.edu and cse06.cse.unt.edu. The IP address of cse01.cse.unt.edu is 129.120.151.94

4. The proxy server should be able to cache at least five recent requested webpages, if available

Procedure:

1. Create a C/ Java based server that can accept single client’s request using socket

2. The created proxy server should also be able to connect to the client requested website through port 80

3. Make sure the proxy server runs on cse01.cse.unt.edu and the format to start the proxy server is: pserver where pserver is the proxy server executable and port_number is the port number on which the proxy server listens

4. Create a C/ Java based client that can connect to the proxy server using socket

5. Make sure the client runs on any of the CSE machines between cse02.cse.unt.edu and cse06.cse.unt.edu, and connects the proxy server. The user can request the desired web page using the format given below: client url: where client is the client executable, port_number is the port number on which the client connects the server and url is the requested URL

6. Once the proxy server gets a request from the client, it then forwards the request to the web server.

7. The proxy server checks for the response from the web server

8. If the HTTP response is 200, the returned web page from the web server is cached in the proxy server. The proxy server stores the webpage in a file and assigns a filename based on the URL (eg. www.google.com).

9. A list file (list.txt) will store the URL of the webpage 10.The list file stores five recent URLs. The cached websites that are not listed in the list file should be deleted

11.Once the returned web page is cached, the web page is forwarded to the client. Verify to see if the returned page is same as the browser returned page

12.If the HTTP response is not 200, do not cache the web page instead forward the HTTP response to the client

13.Test web caching by accessing multiple websites

14.A sample list.txt file is available on Blackboard for reference

Thank you, I will give you good rate if the code is good! Thanks in advance!

Explanation / Answer

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>

#define MAX_MSG 100
#define END_LINE 0x0A

#define SUCCESS 0
#define ERROR 1

#define BUFFER_SIZE 10000

#define MAX_STR_LEN 512
#define BUFFER_SIZE 10000


parse_URL(char *url, char *hostname, int *port, char *identifier)
{
char protocol[MAX_STR_LEN], scratch[MAX_STR_LEN], *ptr=0, *nptr=0;

strcpy(scratch, url);
ptr = (char *)strchr(scratch, ':');
if (!ptr)
{
fprintf(stderr, "Wrong url: no protocol specified ");
exit(ERROR);
}
strcpy(ptr, "");
strcpy(protocol, scratch);
if (strcmp(protocol, "http"))
{
fprintf(stderr, "Wrong protocol: %s ", protocol);
exit(ERROR);
}

strcpy(scratch, url);
ptr = (char *)strstr(scratch, "//");
if (!ptr)
{
fprintf(stderr, "Wrong url: no server specified ");
exit(ERROR);
}
ptr += 2;

strcpy(hostname, ptr);
nptr = (char *)strchr(ptr, ':');
if (!nptr)
{
*port = 80;
nptr = (char *)strchr(hostname, '/');
}
else
{
sscanf(nptr, ":%d", port);
nptr = (char *)strchr(hostname, ':');
}

if (nptr)
*nptr = '';

nptr = (char *)strchr(ptr, '/');

if (!nptr)
{
fprintf(stderr, "Wrong url: no file specified ");
exit(ERROR);
}

strcpy(identifier, nptr);

}

//parse_TIME(char *time, int *day, int *hour, int *min)
//{
// char scratch[MAX_STR_LEN], *ptr=0, *oldptr = 0;
//char TEMP[MAX_STR_LEN];

//strcpy(scratch, time);
//ptr = (char *)strchr(scratch, ':');
//if (!ptr)
//{
// printf("Wrong time format, code 1 ");
// exit(ERROR);
//}
//strcpy(ptr, "");
//strcpy(TEMP, scratch);
//*day = atoi(TEMP);

//oldptr = ptr;
//ptr = (char *)strchr(ptr+1, ':');
//if (!ptr)
//{
// printf("Wrong time format, code 2 ");
// exit(ERROR);
//}
//strcpy(ptr, "");
//strcpy(TEMP, oldptr+1);
//*hour = atoi(TEMP);

// strcpy(TEMP, ptr+1);
// *min = atoi(TEMP);
//}


main( int argc, char *argv[ ])
{


char line[MAX_MSG];
char response[] = "HTTP/1.0 501 Not Implemented ";
int len, bytes_sent;

// open a socket, bind the server address (use port number 9798) to it,
int sd,newSd, cliLen;

struct sockaddr_in cliAddr, servAddr;
char msg[MAX_MSG];
char buffer[BUFFER_SIZE];

if(2!=argc){

fprintf(stderr, "Usage: %s <port> ", argv[0]);

exit(1);

}


int port = atoi(argv[1]);

printf("Welcome to Web Proxy Server! Usage: server %d ", port);
if (argc == 2)
port = atoi(argv[1]);


// create socket
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd<0) {
perror("cannot open socket ");
return ERROR;
}

// bind server port
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(port);

if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0)
{
perror("cannot bind port ");
return ERROR;
}

// listen to incoming connection requests.
listen(sd,5);

// If there is a connection request, then the server
// accepts the connection, reads the request from socket, displays the request
// on standard output and sends back the default response.

// server infinite loop
while(1) {
printf(" Waiting for data on port %u ",port);
cliLen = sizeof(cliAddr);

newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen);
if(newSd<0) {
perror("cannot accept connection ");
return ERROR;
}

memset(buffer,0x0,BUFFER_SIZE); // init line

// receive request
read(newSd, buffer,BUFFER_SIZE);
printf(" Received request from %s: %s ", inet_ntoa(cliAddr.sin_addr),buffer);

// send default response
printf(" Sending default response... ");
len = strlen(response);
bytes_sent = send(newSd, response,len,0);
// bytes_sent = write(newSd, response, len);
if (len != bytes_sent )
{
printf("error during send response, total: %d, sent:%d ", len, bytes_sent);
exit(ERROR);
}
close(newSd);
printf(" -- End of a session -- ");

} // while (1)
close(sd);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote