ast_comparator/test/playground/proxy.c
/*
 * serve - the proxy server serves the request after connection 
 *         with the cliend built.
 */
the proxy server serves the request after connection
with the cliend built.
the proxy server serves the request after connection with the cliend built .
int serve(int fd, char *cache_buf, char *request, int *cnt) {
    char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
    char hdrstr[MAX_HDR_LEN] = {}, hostname[MAXLINE], pathname[MAXLINE];
    int clientfd, port, readcnt;
    CE *entry;
    rio_t rio;
    Rio_readinitb(&rio, fd);
    Rio_readlineb(&rio, buf, MAXLINE);                 
    strcpy(request, buf);
    if ((entry = is_in_cache(request)) != NULL) {
        sem_wait(mutexp);
        ca_readcnt++;
        if (ca_readcnt == 1) 
            sem_wait(w);
        sem_post(mutexp);
        Rio_writen(fd, entry->content, entry->content_size);
        sem_wait(mutexp);
        ca_readcnt--;
        if (ca_readcnt == 0)
            sem_post(w);
        sem_post(mutexp);
        update_cache(entry);
        return 0;
    }
    sscanf(buf, "%s %s %s", method, uri, version);       
    if (strcasecmp(method, "GET")) {                     
        clienterror(fd, "Sorry, prxoy server servers only GET method now.");
        return 0;
    }         
    parse_uri(uri, hostname, pathname, &port);
    build_requesthdrs(&rio, hdrstr, hostname);
    if ((clientfd = Open_clientfd(hostname, port)) < 0) {
        return 0;
    }
    sprintf(buf, "%s /%s HTTP/1.0\r\n", method, pathname);
    Rio_writen(clientfd, buf, strlen(buf));
    Rio_writen(clientfd, hdrstr, strlen(hdrstr));
    *cnt = 0;
    int cached = 1;
    while ((readcnt = Rio_readn(clientfd, buf, MAXLINE)) > 0) {
        if (*cnt + readcnt <= MAX_OBJECT_SIZE) {
            memcpy(cache_buf + *cnt, buf, readcnt);
            *cnt += readcnt;
        } else {
            cached = 0;
        }
        Rio_writen(fd, buf, readcnt);
    }
    Close(clientfd);
    return cached;
}
int serve ( int fd , char * cache_buf , char * request , int * cnt ) { char buf [ MAXLINE ] , method [ MAXLINE ] , uri [ MAXLINE ] , version [ MAXLINE ] ; char hdrstr [ MAX_HDR_LEN ] = { } , hostname [ MAXLINE ] , pathname [ MAXLINE ] ; int clientfd , port , readcnt ; CE * entry ; rio_t rio ; Rio_readinitb ( & rio , fd ) ; Rio_readlineb ( & rio , buf , MAXLINE ) ; strcpy ( request , buf ) ; if ( ( entry = is_in_cache ( request ) ) != NULL ) { sem_wait ( mutexp ) ; ca_readcnt ++ ; if ( ca_readcnt == 1 ) sem_wait ( w ) ; sem_post ( mutexp ) ; Rio_writen ( fd , entry -> content , entry -> content_size ) ; sem_wait ( mutexp ) ; ca_readcnt -- ; if ( ca_readcnt == 0 ) sem_post ( w ) ; sem_post ( mutexp ) ; update_cache ( entry ) ; return 0 ; } sscanf ( buf , " " , method , uri , version ) ; if ( strcasecmp ( method , " " ) ) { clienterror ( fd , " " ) ; return 0 ; } parse_uri ( uri , hostname , pathname , & port ) ; build_requesthdrs ( & rio , hdrstr , hostname ) ; if ( ( clientfd = Open_clientfd ( hostname , port ) ) < 0 ) { return 0 ; } sprintf ( buf , " \r \n " , method , pathname ) ; Rio_writen ( clientfd , buf , strlen ( buf ) ) ; Rio_writen ( clientfd , hdrstr , strlen ( hdrstr ) ) ; * cnt = 0 ; int cached = 1 ; while ( ( readcnt = Rio_readn ( clientfd , buf , MAXLINE ) ) > 0 ) { if ( * cnt + readcnt <= MAX_OBJECT_SIZE ) { memcpy ( cache_buf + * cnt , buf , readcnt ) ; * cnt += readcnt ; } else { cached = 0 ; } Rio_writen ( fd , buf , readcnt ) ; } Close ( clientfd ) ; return cached ; }
serve - the proxy server serves the request after connection
         with the cliend built.
serve - the proxy server serves the request after connection with the cliend built .
/* Read request line and headers */ // printf("Uri parsed result: hostname: %s, pathname: %s, port: %d\n", hostname, pathname, port); /* Following will send request to the `real' server, ignore any error */