personal/hw2/httpserver.c
/*
 * Opens a connection to the proxy target (hostname=server_proxy_hostname and
 * port=server_proxy_port) and relays traffic to/from the stream fd and the
 * proxy target. HTTP requests from the client (fd) should be sent to the
 * proxy target, and HTTP responses from the proxy target should be sent to
 * the client (fd).
 *
 *   +--------+     +------------+     +--------------+
 *   | client | <-> | httpserver | <-> | proxy target |
 *   +--------+     +------------+     +--------------+
 */
Opens a connection to the proxy target (hostname=server_proxy_hostname and
port=server_proxy_port) and relays traffic to/from the stream fd and the
proxy target. HTTP requests from the client (fd) should be sent to the
proxy target, and HTTP responses from the proxy target should be sent to
the client (fd).


Opens a connection to the proxy target ( hostname = server_proxy_hostname and port = server_proxy_port ) and relays traffic to / from the stream fd and the proxy target . HTTP requests from the client ( fd ) should be sent to the proxy target and HTTP responses from the proxy target should be sent to the client ( fd ) .
void handle_proxy_request(int fd)
{
    struct addrinfo* ai;
    char port_str[8];
    struct addrinfo hints;
    int server_sockfd;
    fd_set rfds;
    struct timeval tv;
    int is_connect_loss = 0;
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;
    hints.ai_flags = 0;
    sprintf(port_str, "%d", server_proxy_port);
    int errorno;
    if ((errorno = getaddrinfo(server_proxy_hostname, port_str,
                &hints, &ai)) != 0) {
        perror("getaddrinfo() fail\n");
        fprintf(stderr, "Error: %s\n", gai_strerror(errorno));
    }
    server_sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    if (connect(server_sockfd, ai->ai_addr, ai->ai_addrlen) != 0)
        perror("connect() fail\n");
    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);
    FD_SET(server_sockfd, &rfds);
    while (1) {
        int ret = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
        if (ret > 0) {
            int readfd;
            int writefd;
            int readbyte;
            char buff[BUFF_SIZE];
            memset(buff, 0, BUFF_SIZE);
            if (FD_ISSET(fd, &rfds)) {
                readfd = fd;
                writefd = server_sockfd;
            } else if (FD_ISSET(server_sockfd, &rfds)) {
                readfd = server_sockfd;
                writefd = fd;
            }
            while((readbyte = read(readfd, buff, BUFF_SIZE)) > 0) {
                char sendbuff[BUFF_SIZE];
                check_host_and_replace(buff, sendbuff);
                if (write(writefd, sendbuff, strlen(sendbuff)) == -1) {
                    is_connect_loss = 1;
                    break;
                }
                fwrite(sendbuff, 1, strlen(sendbuff), stdout);
                memset(buff, 0, BUFF_SIZE);
                memset(sendbuff, 0, BUFF_SIZE);
            }
        }
        else if (ret == 0)
            perror("Timeout!\n");
        else {
            is_connect_loss = 1;
            perror("select() fail\n");
        }
        if (is_connect_loss)
            break;
    }
    close(fd);
    close(server_sockfd);
}
void handle_proxy_request ( int fd ) { struct addrinfo * ai ; char port_str [ 8 ] ; struct addrinfo hints ; int server_sockfd ; fd_set rfds ; struct timeval tv ; int is_connect_loss = 0 ; hints . ai_family = AF_INET ; hints . ai_socktype = SOCK_STREAM ; hints . ai_protocol = 0 ; hints . ai_flags = 0 ; sprintf ( port_str , " " , server_proxy_port ) ; int errorno ; if ( ( errorno = getaddrinfo ( server_proxy_hostname , port_str , & hints , & ai ) ) != 0 ) { perror ( " \n " ) ; fprintf ( stderr , " \n " , gai_strerror ( errorno ) ) ; } server_sockfd = socket ( ai -> ai_family , ai -> ai_socktype , ai -> ai_protocol ) ; if ( connect ( server_sockfd , ai -> ai_addr , ai -> ai_addrlen ) != 0 ) perror ( " \n " ) ; FD_ZERO ( & rfds ) ; FD_SET ( fd , & rfds ) ; FD_SET ( server_sockfd , & rfds ) ; while ( 1 ) { int ret = select ( FD_SETSIZE , & rfds , NULL , NULL , NULL ) ; if ( ret > 0 ) { int readfd ; int writefd ; int readbyte ; char buff [ BUFF_SIZE ] ; memset ( buff , 0 , BUFF_SIZE ) ; if ( FD_ISSET ( fd , & rfds ) ) { readfd = fd ; writefd = server_sockfd ; } else if ( FD_ISSET ( server_sockfd , & rfds ) ) { readfd = server_sockfd ; writefd = fd ; } while ( ( readbyte = read ( readfd , buff , BUFF_SIZE ) ) > 0 ) { char sendbuff [ BUFF_SIZE ] ; check_host_and_replace ( buff , sendbuff ) ; if ( write ( writefd , sendbuff , strlen ( sendbuff ) ) == -1 ) { is_connect_loss = 1 ; break ; } fwrite ( sendbuff , 1 , strlen ( sendbuff ) , stdout ) ; memset ( buff , 0 , BUFF_SIZE ) ; memset ( sendbuff , 0 , BUFF_SIZE ) ; } } else if ( ret == 0 ) perror ( " \n " ) ; else { is_connect_loss = 1 ; perror ( " \n " ) ; } if ( is_connect_loss ) break ; } close ( fd ) ; close ( server_sockfd ) ; }
Opens a connection to the proxy target (hostname=server_proxy_hostname and
 port=server_proxy_port) and relays traffic to/from the stream fd and the
 proxy target.
Opens a connection to the proxy target ( hostname = server_proxy_hostname and port = server_proxy_port ) and relays traffic to / from the stream fd and the proxy target .