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

In this assignment, write a c program to create a Web proxy. When your proxy rec

ID: 3670198 • Letter: I

Question

In this assignment, write a c program to create a Web proxy. When your proxy receives an HTTP request for an object from a browser, it generates a new HTTP request for the same object and sends it to the origin server. When the proxy receives the corresponding HTTP response with the object from the origin server, it creates a new HTTP response, including the object, and sends it to the client. This proxy will be multi-threaded, so that it will be able to handle multiple requests at the same time. For this assignment, the companion Web site provides the skeleton code for the proxy server. Your job is to complete the code, and then test it by having different browsers request Web objects via your proxy.

Please show output using pictures and how to compile. Otherwise no credit

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>

#include <arpa/inet.h>

#include <err.h>

#include <string.h>

  

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

{

    char get[] = "GET /index.html HTTP/1.1 ";

    char response[] = "HTTP/1.1 200 OK "

    "Content-Type: text/html; charset=UTF-8 "

    "<doctype !html> ";

    char page[10000];

     

    int client_fd;

    struct sockaddr_in svr_addr, cli_addr, out_addr;

    socklen_t sin_len = sizeof(cli_addr);

     

    out_addr.sin_family = AF_INET;

    out_addr.sin_port = htons(8085);

    out_addr.sin_addr.s_addr=inet_addr("129.120.151.94");

     

    int sock1 = socket(AF_INET, SOCK_STREAM, 0);

    int sock2 = socket(AF_INET, SOCK_STREAM, 0);

    setsockopt(sock1, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));

    setsockopt(sock2, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));

    int port = 8084;

    svr_addr.sin_family = AF_INET;

    svr_addr.sin_addr.s_addr = INADDR_ANY;

    svr_addr.sin_port = htons(port);

    if (bind(sock1, (struct sockaddr *) &svr_addr, sizeof(svr_addr)) == -1) {

        close(sock1);

        err(1, "Can't bind listening");

    }

    listen(sock1, 5);

    int bytesRec;

    char buf[1024];

    char* request;

    int x=0;

     

   int connreturn;

     

    while (1) {

         

        //signal(SIGPIPE, signal_callback_handler); /* Catch Signal Handler SIGPIPE */

        client_fd = accept(sock1, (struct sockaddr *) &cli_addr, &sin_len);

        printf("got connection ");

        printf("Requesting index from local server... ");

         

        connreturn = connect(sock2, (struct sockaddr *) &out_addr,(socklen_t)sizeof(out_addr));

        printf("Connect returns: %d ",connreturn);

        send(sock2, &get, sizeof(get),0);

        recv(sock2, page, sizeof(page)-1, 0);

        //page[sizeof(page)-1] = '/0';

        write(client_fd, response, sizeof(response) - 1);

        write(client_fd, page, sizeof(page)-1);

        //send(sock2, &page, sizeof(page),0);

        close(client_fd);

        close(sock2);

        memset(page, 0, sizeof(page[0]) * 10000);

        continue;

         

         

    }

     

}