Exemplo n.º 1
0
void getInfo(int fd, rio_t client, int id){
	char buf1[MAXLINE];
	int jobID=id;
	int numBytes;
  	char * saveptr=NULL;
	Rio_readinitb( &client,fd);
	while(true){
  		numBytes = Rio_readlineb(&client, buf1, MAXLINE);
		id1 = strtok_r(buf1, " \r\n",&saveptr);
		int fileStartID=atoi(id1);
		id2 = strtok_r(buf1, " \r\n",&saveptr);
		int fileEndID=atoi(id2);

	numBytes = Rio_readlineb(c, buf, MAXLINE);
  	if(numBytes<0){
    		Close(fd);
    		return ans;
  	}
  	buf[numBytes]=0;
  	seperate(buf, &ans.pre);


	fd=Open_clientfd(ipAddress, atoi(port));

}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
  char *host, *filename;
  int port;
  int clientfd;

  if (argc != 4) {
    fprintf(stderr, "Usage: %s <host> <port> <filename>\n", argv[0]);
    exit(1);
  }

  host = argv[1];
  port = atoi(argv[2]);
  filename = argv[3];

  /* Open a single connection to the specified host and port */
  int i;
  for(i=0;i<5;i++){
  clientfd = Open_clientfd(host, port);
  
  clientSend(clientfd, filename);
  clientPrint(clientfd);
    
  Close(clientfd);
  }

  exit(0);
}
int main(int argc, char **argv) 
{
    int clientfd, port;
    char *host, buf[MAXLINE];
    rio_t rio;

    if (argc != 3) {
	fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
	exit(0);
    }
    host = argv[1];
    port = atoi(argv[2]);

    clientfd = Open_clientfd(host, port);
    Rio_readinitb(&rio, clientfd);

    while (Fgets(buf, MAXLINE, stdin) != NULL) {
    printf("client get: %s\n",buf);
	Rio_writen(clientfd, buf, strlen(buf));
    printf("client buf: %s\n",buf);
	Rio_readlineb(&rio, buf, MAXLINE);
	Fputs(buf, stdout);
    }
    
//    printf("outside\n");
    Close(clientfd); //line:netp:echoclient:close
    exit(0);
}
int main(int argc, char** argv) {
    int clientfd;
    char* hostname = argv[1];
    char* filename = argv[2];
    int port = atoi(argv[3]);

    char data[60];
    strcpy(data, "GET /");
    strcat(data, filename);
    strcat(data, " HTTP/1.1\r\nHost: ");
    strcat(data, hostname);
    strcat(data, "\r\n\r\n");
    printf("Constructed Data: %s\n", data);

    char buffer[MAXLINE];   // Using MAXLINE from csapp
    bzero(buffer, MAXLINE);
    buffer[0] = 'x';

    rio_t rio;

    clientfd = Open_clientfd(hostname, port);

    Rio_readinitb(&rio, clientfd);
    rio_writen(clientfd, data, strlen(data));

    while (strlen(buffer)>0) {

        Rio_readlineb (&rio, buffer, MAXLINE);
        printf("%s\n", buffer);
    }

    close(clientfd);
    exit(0);
}
Exemplo n.º 5
0
int echoClient(int argc, char** argv) {
    int clientfd, port;
    char* host, buf[MAXLINE];
    rio_t rio;

    if (argc != 3) {
        fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
        exit(0);
    }
    host = argv[1];
    port = atoi(argv[2]);

    clientfd = Open_clientfd(host, port);
    Rio_readinitb(&rio, clientfd);

    while (Fgets(buf, MAXLINE, stdin) != NULL) {
        Rio_writen(clientfd, buf, strlen(buf));
        // get response from server
        Rio_readlineb(&rio, buf, MAXLINE);
        //Fputs(buf, stdout);
    }

    Close(clientfd);
    exit(0);
}
Exemplo n.º 6
0
void reduceFunction::sendInfo(){
	printf("print result:\n");
	int fd=Open_clientfd(dataSystemIp, dataSystemPort);
	printf("%d\n",fd);
	if(fd<0){
		printf("data system is missing.\n"); 
		return;	
	}
	char buf[80];
	printf("print result:\n");
	sprintf(buf,"ADD\n");
      	Rio_writep(fd, buf, strlen(buf));
	for (std::map<std::string,int>::iterator it=myMap.begin(); it!=myMap.end(); ++it){
		//printf("%s: %d\n",it->first.data(),it->second);
		sprintf(buf,"%s#%d\n",it->first.data(),it->second);
      		Rio_writep(fd, buf, strlen(buf));
	}
	printf("aaa: %s", buf);
	close(fd);
	/*printf("print node addresses:\n");
	for (std::map<int, mapNode>::iterator nit=mapAddress.begin(); nit!=mapAddress.end(); ++nit){
		printf("%d: %s %d\n",nit->first, nit->second.mapIp, nit->second.mapPort);
		//sprintf(buf,"%s*%d\n",it->first.data(),it->second);
      		//Rio_writep(fd, buf, strlen(buf));
	}*/


}
Exemplo n.º 7
0
int main(int argc, char **argv)
{
  int clientfd, port;
  char *host, buf[MAXLINE];
  rio_t rio;

  if (argc != 3)
  {
    fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
    exit(0);
  }
  host = argv[1];
  port = atoi(argv[2]);

  clientfd = Open_clientfd(host, port);
  Rio_readinitb(&rio, clientfd);

  printf("type:"); fflush(stdout);
  while (Fgets(buf, MAXLINE, stdin) != NULL) {
    Rio_writen(clientfd, buf, strlen(buf));
    Rio_readlineb(&rio, buf, MAXLINE);
    printf("echo:");
    Fputs(buf, stdout);
    printf("type:"); fflush(stdout);
  }
  Close(clientfd);
  exit(0);
}
Exemplo n.º 8
0
int main(int argc, char *argv[])
{
    int clientfd, port;
    char *host, buf[MAXLINE];
    rio_t rio;

    /*
    if(argc!= 3)
    {
        fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
        exit(1);
    }
    */

    //host = argv[1];
    //port = atoi(argv[2]);
    host ="127.0.0.1";
    port=8080;

    clientfd = Open_clientfd(host, port);
    Rio_readinitb(&rio, clientfd);

    while(Fgets(buf, MAXLINE, stdin) != NULL)
    {
        Rio_writen(clientfd, buf, strlen(buf));
        Fputs(buf, stdout);
    }
    close(clientfd);
    exit(0);
}
Exemplo n.º 9
0
int main(int argc, char *argv[])
{
  char *host, *filename;
  int port;
  int clientfd;
  int runs;
  if (argc != 5) {
    fprintf(stderr, "Usage: %s <host> <port> <filename> <runs>\n", argv[0]);
    exit(1);
  }

  host = argv[1];
  port = atoi(argv[2]);
  filename = argv[3];
  //runs = atoi(argv[4]);
  /* Open a single connection to the specified host and port */
  printf("host: %s port: %d filename: %s runs: %d\n", host, port, filename, runs);
  clientfd = Open_clientfd(host, port);
  clientSend(clientfd, filename);
  clientPrint(clientfd);
    
  Close(clientfd);

  exit(0);
}
Exemplo n.º 10
0
void* workers(void* _filename){
  char* filename = (char*) _filename;
  //printf("file:%s\n", filename);
  int clientfd = Open_clientfd("localhost", 8559);
  clientSend(clientfd, filename);
  clientPrint(clientfd);
  Close(clientfd);  
  return NULL;
}
Exemplo n.º 11
0
char *fetch(char *link) {
  char url[256];
  snprintf(url, 256, "/~harter/537/p4/%s", link);
  int clientfd = Open_clientfd("pages.cs.wisc.edu", 80);
  ///~harter/537/  clientSend(clientfd, "/~harter/537/slides.html");
  clientSend(clientfd, url);
  char *page = grab_page(clientfd);
  Close(clientfd);
  return page;
}
Exemplo n.º 12
0
int main(int argc, char * argv[]) {

    int clientfd;
    char *host, buf[MAXLINE];
    int port;
    rio_t rio;
    pthread_t tid;

    fprintf(stderr,"fprintf 1\n");

    if(argc < 3) {
        fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
        exit(0);
    }

    host = argv[1];
    port = atoi(argv[2]);

    fprintf(stderr,"fprintf 2\n");

    clientfd = Open_clientfd(host, port);

    fprintf(stderr,"fprintf 3\n");

    active = 1;

    Pthread_create(&tid, NULL, keyboard, &clientfd);

    fprintf(stderr,"fprintf 4\n");

    Rio_readinitb(&rio, clientfd);
    while(Rio_readlineb(&rio, buf, MAXLINE) != 0) {

        fprintf(stderr,"fprintf 5\n");

        if(strcmp(buf, "$q") == 0) {
            printf("received $q command from server\n");
            active = 0;
        }
        else {
            fprintf(stderr,"fprintf 6\n");
            Fputs(buf, stdout);
        }
    }

    Close(clientfd);

    /* note: if the server closes the connection, the keyboard thread
       will be in Fgets and will try to write to a closed connection... */

    return 0;
}
Exemplo n.º 13
0
void* sendpeer_message(void* args){
    int i = (((int*)args)[0]);
    int client_socket_fd;
    //printf("%s %d\n",user_list[i].IP,user_list[i].port);
    client_socket_fd=Open_clientfd(user_list[i].IP,user_list[i].port);

    if (client_socket_fd == -1) {
        perror("the Ip and port is:");
        exit(1);
    }

    char sendbuf[BUFFER_SIZE];
    sprintf(sendbuf, "%s %s %d %d\r\n", "Get_piece", filename, total_num, i+1);
    //printf("sendbuf=%s\n",sendbuf);
    if(send(client_socket_fd, sendbuf, BUFFER_SIZE, 0) < 0)
    {
        perror("Send File Name Failed:");
        exit(1);
    }
    //set piece name
    int temp = 0xaa + i;
    char temp2[10];
    sprintf(temp2,"%x",temp);
    char piecename[100];
    sprintf(piecename,"%s%s","x",temp2);
    //printf("piecename=%s\n",piecename);
    
    FILE *fp = fopen(piecename, "w");
    if(NULL == fp)
    {
        printf("File:\t%s Can Not Open To Write\n", piecename);
        exit(1);
    }
    
    char buffer[BUFFER_SIZE];
    bzero(buffer, BUFFER_SIZE);
    int length = 0;
    while((length = recv(client_socket_fd, buffer, BUFFER_SIZE, 0)) > 0)
    {
        if(fwrite(buffer, sizeof(char), length, fp) < length)
        {
            printf("File:\t%s Write Failed\n", piecename);
            break;
        }
        bzero(buffer, BUFFER_SIZE);
    }
    rec_num++;
    close(client_socket_fd);
    fclose(fp);
}
Exemplo n.º 14
0
void doit(int fd)
{
    int clientfd;
    char buf[MAXLINE], method[MAXLINE], uri[MAXLINE];
    char host[MAXLINE], port[MAXLINE];
    char filename[MAXLINE];
    char cache_buf[MAX_OBJECT_SIZE];
    size_t buflen;
    struct cache *cache;
    int cache_size = 0, cache_flag = 1;
    rio_t c_rio, s_rio;

    /* Read request line and headers */
    Rio_readinitb(&c_rio, fd);
    if (!Rio_readlineb(&c_rio, buf, MAXLINE))
	return;
    printf("%s", buf);

    sscanf(buf, "%s %s", method, uri);
    if (strcasecmp(method, "GET")) {
	printf("this proxy can handle only \"GET\"\n");
	return;
    }
    construct_requesthdrs(&c_rio, buf, filename, uri, host, port);

    if (find_cache(&cache, uri) == 1) {
	Rio_writen(fd, cache->data, strlen(cache->data));
    } else {
	clientfd = Open_clientfd(host, port);
	if (clientfd < 0) {
	    printf("there is no such server\n");
	    return;
	}
	Rio_readinitb(&s_rio, clientfd);
	Rio_writen(clientfd, buf, strlen(buf));

	while ((buflen = Rio_readlineb(&s_rio, buf, MAXLINE)) != 0) {
	    Rio_writen(fd, buf, buflen);
	    if (cache_size + buflen > MAX_OBJECT_SIZE) {
		cache_flag = 0;
	    } else {
		memcpy(cache_buf + cache_size, buf, buflen);
		cache_size += buflen;
	    }
	}
	if (cache_flag == 1)
	    create_cache(cache_buf, uri);
	Close(clientfd);
    }
}
Exemplo n.º 15
0
void *func(void *arg)
{
    int n = (int) arg;
    char rbuf[5];
    const char *msg = "test";
    int clientfd = Open_clientfd("127.0.0.1", 12400);
    int i;
    for (i = 0; i < n; ++i) {
        Rio_writen(clientfd, msg, 5);
        Rio_readn(clientfd, rbuf, 5);
    }
    Close(clientfd);
    return NULL;
}
Exemplo n.º 16
0
/*****support functions*****/
void sendnode_message(char* IP, int port, char* cmd, char* name){
    //printf("IP is %s\n",IP);
    //printf("port is %d\n",port);
    char sendbuf[MAXLINE];
    sprintf(sendbuf, "%s %s %s %d\r\n", cmd, name, myIP, myport);
    clientfd = Open_clientfd(IP, port);
    if(clientfd<0){
        perror("Open_clientfd Failed!\n");
        exit(1);
    }
    //printf("sendbuf is %s\n",sendbuf);
    Rio_writen(clientfd, sendbuf, strlen(sendbuf));
    memset(sendbuf, 0, strlen(sendbuf));
}
Exemplo n.º 17
0
/*
 * doit - handle one HTTP request/response transaction
 */
void doit(int fd) 
{
  int is_static;
  struct stat sbuf;
  char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
  char filename[MAXLINE], cgiargs[MAXLINE];
  rio_t rio_c, rio_h;

  /* Ren's Local Vars */
  char host[MAXLINE], url[MAXLINE], request[MAXLINE], header[MAXLINE];
  int hostfd;
  /* Ren's Local Vars END */


  /* Read request line and headers */
  Rio_readinitb(&rio_c, fd);
  Rio_readlineb(&rio_c, buf, MAXLINE);
  sscanf(buf, "%s %s %s", method, uri, version);
  if (strcasecmp(method, "GET")) { 
    clienterror(fd, method, "501", "Not Implemented",   
                "Tiny does not implement this method");
    return;
  }
  printf("STUFF FROM THE CLIENT:\n");
  printf("%s\n",buf);
  read_requesthdrs(&rio_c);

  /* Ren's code */
  parseURL(buf, host, uri, version); /* parse url for hostname and uri */
  hostfd = Open_clientfd(host, PORT); /* connect to host as client */
  Rio_readinitb(&rio_h, hostfd); /* set up host file discriptor */

  /* generate and send request to host*/  
  genrequest(request, method, uri);
  genheader(host, header);
  strcat(request, header);
  printf("%s\n",request); 
  Rio_writen(hostfd, request, strlen(request));

  /* stream information from server to client */
  printf("STUFF FROM THE SERVER:\n");
  while(Rio_readlineb(&rio_h, buf, MAXLINE)){
    printf("%s\n",buf);
    Rio_writen(fd, buf, MAXLINE);
  }

  printf("stream ended\n");
  /* Ren's code */
}
Exemplo n.º 18
0
char * clienttest(char *host, char *request) {
    int clientfd;
    char *port = "80";
    char *result;

    // Open the socket and set it up
    clientfd = Open_clientfd(host, port);
    // Write an http request to end-server
    Rio_writen(clientfd, request, strlen(request));
    // Read response from end-server until ending html tag encountered
    result = read_until(clientfd, "</html>\0");
    // Close the connection and return the end-server's response
    // Fputs(result, stdout);
    Close(clientfd);
    return result;
}
Exemplo n.º 19
0
/*
 * forwardRequest2Server - forward request from proxy to server
 *							when cannot find data in cache
 *
 * Pamameters:
 *		fd:					file descriptor
 *	request2server:			store the request that will send to server
 *		
 */
void forwardRequest2Server(int fd, char *request2server, 
                            char *hostname, char *uri, char *port) {
    dbg_printf("--------In forwardRequest2Server function--------\n");

    char cache_resp[MAX_OBJECT_SIZE];   // store response from cache
    char server_resp[MAX_OBJECT_SIZE];  // store response from server
    rio_t rio_proxy_ser; 
    unsigned int obj_size, cnt;

    /* open a socket descriptor ready to read and write */
    int clientfd = Open_clientfd(hostname, port); 
    Rio_readinitb(&rio_proxy_ser, clientfd);

    /* send connection to server */
    dbg_printf("send connection to server: \n");
    Rio_writen(clientfd, request2server, strlen(request2server));

    /* receive data from server */
    dbg_printf("start receiving data from server:\n");
    // memset(cache_resp, 0, sizeof(cache_resp));
    obj_size = 0;
    while ((cnt = Rio_readnb(&rio_proxy_ser, 
            server_resp, sizeof(server_resp))) > 0) {
        
        /* accumulate cache object size */
        obj_size += cnt;
        strcat(cache_resp, server_resp);

        dbg_printf("server response: \n '%s' \n", server_resp);
        Rio_writen(fd, server_resp, cnt);
        memset(server_resp, 0, sizeof(server_resp));
    }

    /* check if data can be cached */
    if (obj_size <= MAX_OBJECT_SIZE) {
        P(&mutex);
        caching_from_server(uri, cache_resp);
        V(&mutex);
    }

    /* close proxy connection with server */
    Close(clientfd);
    dbg_printf("--------In forwardRequest2Server function END--------\n");
}
Exemplo n.º 20
0
int main(int argc, char **argv) 
{
    int clientfd, port;
    char *host, buf[MAX_LINE];
    
    if (argc == 2 || argc == 3) {
        host = argv[1];
        port = (argc == 2) ? DEFAULT_PORT : atoi(argv[2]);
    }
    else {
        fprintf(stderr, "usage: %s <host>\n", argv[0]);
        exit(0);
    }    
    
    clientfd = Open_clientfd(host, port);
    
    printf("Bank client v.%d connected to %s on port %d\n", VERSION, host, port);
    
    msg_t *request = new_msg();
    msg_t *response = new_msg();
    
    printf("> "); fflush(stdout);
    while (Fgets(buf, MAX_LINE, stdin) != NULL) {
        if (parse_buf(buf, request)) {
            Rio_writen(clientfd, (void *) request, sizeof(msg_t)); // Send bits to server
            Rio_readn(clientfd, (void *) response, sizeof(msg_t)); // Get bits back from server
            print_response(response);    
        }
        
        clear_msg(request);
        clear_msg(response);        
        printf("> "); fflush(stdout);
    }
    
    Close(clientfd); 
    free(request);
    free(response);
    exit(0);
}
Exemplo n.º 21
0
/*
 * handle_request - handle one HTTP request/response transaction
 */
void handle_request(int fd) 
{
    struct stat sbuf;
    char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
    char headers[MAXBUF];
    char request[MAXLINE], hostname[MAXLINE], arguments[MAXLINE], port[MAXLINE];
    int serverfd;
    rio_t rio_client;
    rio_t rio_server;

    /* Read request line and headers */
    Rio_readinitb(&rio_client, fd);
    if (!Rio_readlineb(&rio_client, buf, MAXLINE))
        return;
    printf("%s", buf);
    sscanf(buf, "%s %s %s", method, uri, version);
    if (strcasecmp(method, "GET")) {
        clienterror(fd, method, "501", "Not Implemented", "Proxy does not implement this method");
        return;
    }

    process_uri(uri, hostname, arguments, port);
    process_hdrs(&rio_client, headers, hostname);
    printf("%s\n", request);
    printf("%s\n", headers);

    sprintf(request, "GET %s HTTP/1.0 \r\n", arguments);

    /* send request to server */
    serverfd = Open_clientfd(hostname, port);

    Rio_writen(serverfd, request, strlen(request));
    Rio_writen(serverfd, headers, strlen(headers));

    /* answer to client */
    Rio_readinitb(&rio_server, serverfd);
    process_answer(&rio_server, fd);  
}
Exemplo n.º 22
0
int tinyhttpClient(int argc, char** argv) {
    int clientfd, port;
    char* host, buf[MAXLINE];
    rio_t rio;

    if (argc != 3) {
        fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
        exit(0);
    }
    host = argv[1];
    port = atoi(argv[2]);

    clientfd = Open_clientfd(host, port);
    Rio_readinitb(&rio, clientfd);

    fprintf(stdout, "GET /../ 1.0\r\n");
    sprintf(buf, "GET /../ 1.0\r\n");
    Fputs(buf, stdout);
    Rio_writen(clientfd, buf, strlen(buf));
    // get response from server
    Rio_readlineb(&rio, buf, MAXLINE);

    fprintf(stdout, "asdaz..0\r\n");
    sprintf(buf, "asdaz..0\r\n");
    Fputs(buf, stdout);
    Rio_writen(clientfd, buf, strlen(buf));

    fprintf(stdout, "\r\n");
    sprintf(buf, "\r\n");
    Fputs(buf, stdout);
    Rio_writen(clientfd, buf, strlen(buf));
    // get response from server
    Rio_readlineb(&rio, buf, MAXLINE);

    Close(clientfd);
    exit(0);
}
Exemplo n.º 23
0
int main(int argc, char *argv[]) {
	if (argc != 3) {
		fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
		exit(0);
	}

	char *host = argv[1];
	int port = atoi(argv[2]);

	int fd = Open_clientfd(host, port);

	rio_t rio;
	Rio_readinitb(&rio, fd);

	char buf[MAXLINE];
	while (Fgets(buf, MAXLINE, stdin) != NULL) {
		Rio_writen(fd, buf, strlen(buf));
		Rio_readlineb(&rio, buf, MAXLINE);
		Fputs(buf, stdout);
	}

	Close(fd);
	exit(0);
}
Exemplo n.º 24
0
/*
 * forward_to_server - forward the http request to server
 *
 * this function is little bit long since it handles both GET and
 * non GET methods;
 *
 * it is hard to refactor since many variables need to be passed
 *
 * return -1 when all kinds of error
 * return 0 for GET method
 * return 1 when cache hit (not forward to server)
 * return 2 for non GET method
 */
int forward_to_server(int fd, int *to_server_fd, char *cache_id,
                      void *cache_content, unsigned int *cache_length) {
    char buf[MAXLINE], request_buf[MAXLINE];
    char method[MAXLINE], protocol[MAXLINE];
    char host_port[MAXLINE];
    char remote_host[MAXLINE], remote_port[MAXLINE], resource[MAXLINE];
    char version[MAXLINE];
    char origin_request_line[MAXLINE];
    char origin_host_header[MAXLINE];

    int has_user_agent_str = 0, has_accept_str = 0,
        has_accept_encoding_str = 0,
        has_connection_str = 0, has_proxy_connection_str = 0,
        has_host_str = 0;

    rio_t rio_client;

    strcpy(remote_host, "");
    strcpy(remote_port, "80");
    memset(cache_content, 0, MAX_OBJECT_SIZE);

    Rio_readinitb(&rio_client, fd);
    if (Rio_readlineb(&rio_client, buf, MAXLINE) == -1) {
        return -1;
    }
    // used incase dns lookup failed
    strcpy(origin_request_line, buf);

    if (parse_request_line(buf, method, protocol, host_port,
                           resource, version) == -1) {
        return -1;
    }
    parse_host_port(host_port, remote_host, remote_port);

    if (strstr(method, "GET") != NULL) {
        // GET method

        // compose our request line
        strcpy(request_buf, method);
        strcat(request_buf, " ");
        strcat(request_buf, resource);
        strcat(request_buf, " ");
        strcat(request_buf, http_version_str);

        // process request header
        while (Rio_readlineb(&rio_client, buf, MAXLINE) != 0) {
            if (strcmp(buf, "\r\n") == 0) {
                break;
            } else if (strstr(buf, "User-Agent:") != NULL) {
                strcat(request_buf, user_agent_str);
                has_user_agent_str = 1;
            } else if (strstr(buf, "Accept-Encoding:") != NULL) {
                strcat(request_buf, accept_encoding_str);
                has_accept_encoding_str = 1;
            } else if (strstr(buf, "Accept:") != NULL) {
                strcat(request_buf, accept_str);
                has_accept_str = 1;
            } else if (strstr(buf, "Connection:") != NULL) {
                strcat(request_buf, connection_str);
                has_connection_str = 1;
            } else if (strstr(buf, "Proxy Connection:") != NULL) {
                strcat(request_buf, proxy_connection_str);
                has_proxy_connection_str = 1;
            } else if (strstr(buf, "Host:") != NULL) {
                strcpy(origin_host_header, buf);
                if (strlen(remote_host) < 1) {
                    // if host not specified in request line
                    // get host from host header
                    sscanf(buf, "Host: %s", host_port);
                    parse_host_port(host_port, remote_host, remote_port);
                }
                strcat(request_buf, buf);
                has_host_str = 1;
            } else {
                strcat(request_buf, buf);
            }
        }
        // if not sent, copy in out headers
        if (has_user_agent_str != 1) {
            strcat(request_buf, user_agent_str);
        }
        if (has_accept_encoding_str != 1) {
            strcat(request_buf, accept_encoding_str);
        }
        if (has_accept_str != 1) {
            strcat(request_buf, accept_str);
        }
        if (has_connection_str != 1) {
            strcat(request_buf, connection_str);
        }
        if (has_proxy_connection_str != 1) {
            strcat(request_buf, proxy_connection_str);
        }
        if (has_host_str != 1) {
            sprintf(buf, "Host: %s:%s\r\n", remote_host, remote_port);
            strcat(request_buf, buf);
        }
        strcat(request_buf, "\r\n");
        if (strcmp(remote_host, "") == 0) {
            return -1;
        }

        // compose cache id
        strcpy(cache_id, method);
        strcat(cache_id, " ");
        strcat(cache_id, remote_host);
        strcat(cache_id, ":");
        strcat(cache_id, remote_port);
        strcat(cache_id, resource);
        strcat(cache_id, " ");
        strcat(cache_id, version);

        // search in the cache
        if (read_cache_node_lru_sync(cache, cache_id, cache_content,
                                     cache_length) != -1) {
            // cache hit
            return 1;
        }

        // client to server
        *to_server_fd = Open_clientfd(remote_host, atoi(remote_port),
                                    origin_request_line, origin_host_header);
        if (*to_server_fd == -1) {
            return -1;
        } else if (*to_server_fd == -2) {
            // dns lookup failed, write our response page
            // caused by invalid host
            strcpy(buf, client_bad_request_str);
            Rio_writen(fd, buf, strlen(buf));
            return -1;
        }
        if (Rio_writen(*to_server_fd, request_buf,
                       strlen(request_buf)) == -1) {
            return -1;
        }
        return 0;
    } else {
        // non GET method
        unsigned int length = 0, size = 0;
        strcpy(request_buf, buf);
        while (strcmp(buf, "\r\n") != 0 && strlen(buf) > 0) {
            if (Rio_readlineb(&rio_client, buf, MAXLINE) == -1) {
                return -1;
            }
            if (strstr(buf, "Host:") != NULL) {
                strcpy(origin_host_header, buf);
                if (strlen(remote_host) < 1) {
                    sscanf(buf, "Host: %s", host_port);
                    parse_host_port(host_port, remote_host, remote_port);
                }
            }
            get_size(buf, &size);
            strcat(request_buf, buf);
        }
        if (strcmp(remote_host, "") == 0) {
            return -1;
        }
        *to_server_fd = Open_clientfd(remote_host, atoi(remote_port),
                                    origin_request_line, origin_host_header);
        if (*to_server_fd < 0) {
            return -1;
        }
        // write request line
        if (Rio_writen(*to_server_fd, request_buf,
                       strlen(request_buf)) == -1) {
            return -1;
        }
        // write request body
        while (size > MAXLINE) {
            if ((length = Rio_readnb(&rio_client, buf, MAXLINE)) == -1) {
                return -1;
            }
            if (Rio_writen(*to_server_fd, buf, length) == -1) {
                return -1;
            }
            size -= MAXLINE;
        }
        if (size > 0) {
            if ((length = Rio_readnb(&rio_client, buf, size)) == -1) {
                return -1;
            }
            if (Rio_writen(*to_server_fd, buf, length) == -1) {
                return -1;
            }
        }
        return 2;
    }
}
Exemplo n.º 25
0
int main(int argc, char *argv[])
{
	int listenfd, connfd;
	char hostname[MAXLINE], port[MAXLINE];
	socklen_t clientlen;
	struct sockaddr_storage clientaddr;
	int acc_count = 0;

	//if (strcmp(argv[2], "compute-0-29.local") == 0)
	//{
	printf("%s listening\n", argv[2]);
	listenfd = Open_listenfd ("15618");
	//}
	sleep(5);

	//int send_val = 5;
	printf("Hostname is %s\n", argv[2]);
	printf("Int got as %d\n", atoi(argv[1]));

	if (strcmp(argv[2], "compute-0-29.local") == 0)
	{
		while (1)
		{
			clientlen = sizeof (clientaddr);

			// accept connections 
			connfd = Accept (listenfd, (SA *) & clientaddr, &clientlen);
			Getnameinfo ((SA *) & clientaddr, clientlen, hostname, MAXLINE,
					port, MAXLINE, 0);
			printf ("Accepted connection from (%s, %s). Connfd is %d\n", hostname, port, connfd);

			//newfd = (int *) malloc (sizeof (int));
			//newfd = connfd;

			// go serve this client! 
			// pthread_create (&tid, NULL, doit, newfd);
			acc_count++;

			double send_double = 232.23;
			int retval = Rio_writen (connfd, (void *)&send_double, sizeof(double));
			if (retval < 0)
			{   
				printf("Rio_writen to %d encountered a problem.\n", connfd);

				unix_error ("Rio_writen error");
			}   

			retval = Rio_writen (connfd, (void *)&send_double, sizeof(double));
			if (retval < 0)
			{   
				printf("Rio_writen to %d encountered a problem.\n", connfd);

				unix_error ("Rio_writen error");
			}
			int len = Rio_readn (connfd, (void *)&send_double, sizeof(double));

			if (len < 0)
			{
				unix_error ("Rio_readlineb error");
			}
			printf("Host %s got len as %d and receive_val as %lf\n", argv[2], len, send_double);

			len = Rio_readn (connfd, (void *)&send_double, sizeof(double));

			if (len < 0)
			{
				unix_error ("Rio_readlineb error");
			}
			printf("Host %s got len as %d and receive_val as %lf\n", argv[2], len, send_double);

			if (acc_count == 3)
			{
				printf("Accepted 3 connections.\n");
				break;
			}
		}
	}
	else
	{
		int serverfd = Open_clientfd ("10.22.1.241", "15618");
		printf("In host %s, serverfd is %d\n", argv[2], serverfd);

		double buf;
		int len = Rio_readn (serverfd, (void *)&buf, sizeof(double));

		if (len < 0)
		{
			unix_error ("Rio_readlineb error");
		}
		printf("Host %s got len as %d and buf as %lf\n", argv[2], len, buf);

		len = Rio_readn (serverfd, (void *)&buf, sizeof(double));

		if (len < 0)
		{
			unix_error ("Rio_readlineb error");
		}
		printf("Host %s got len as %d and buf as %lf\n", argv[2], len, buf);

		buf = 99.104;
		int retval = Rio_writen (serverfd, (void *)&buf, sizeof(double));
		if (retval < 0)
		{   
			printf("Rio_writen to %d encountered a problem.\n", serverfd);

			unix_error ("Rio_writen error");
		}   

		retval = Rio_writen (serverfd, (void *)&buf, sizeof(double));

		if (retval < 0)
		{   
			printf("Rio_writen to %d encountered a problem.\n", serverfd);

			unix_error ("Rio_writen error");
		}   
	}
	return 0;
}
Exemplo n.º 26
0
/*
 * serve - handle one HTTP request/response transaction
 */
void serve(int to_client_fd)
{
	char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
	char filename[MAXLINE], hostname[MAXLINE];
	char port[10];
	rio_t rio_to_client;
	rio_t rio_to_server;
	int to_server_fd;

	/* Read request line and headers */
	Rio_readinitb(&rio_to_client, to_client_fd);
	Rio_readlineb(&rio_to_client, buf, MAXLINE);
	sscanf(buf, "%s %s %s", method, uri, version);

	/* if not GET method, ignore it */
	if (strcasecmp(method, "GET")) {
		return;
	}

	/* Parse URI from GET request */
	parse_uri(uri, hostname, port, filename);
	// strange display error in csapp page
	if (strcasecmp(hostname, "csapp.cs.cmu.edu") == 0) {
		return;
	}

	sprintf(buf, "%s %s %s\r\n", "GET", filename, "HTTP/1.0");

	/* search content in cache list */
	struct cache_block* ptr = search_cache(head, uri);

	/* cache found, directly send to client */
	if (ptr) {
		// add the number of existing threads reading
		add_reading_cnt(ptr);
		// send cache to client
		Rio_writen(to_client_fd, ptr->file, ptr->size);
		// subtract the number of existing threads reading
		sub_reading_cnt(ptr);
		// update timestamp and reorder LRU list
		update_timestamp(head, ptr);

		return;
	}

	/* cache not found, connect with server */
	to_server_fd = Open_clientfd(hostname, port);
	Rio_readinitb(&rio_to_server, to_server_fd);
	// send request line: GET HTTP/1.0
	Rio_writen(to_server_fd, buf, strlen(buf));
	// send host to server
	sprintf(buf, "Host: %s\r\n", hostname);
	Rio_writen(to_server_fd, buf, strlen(buf));

	
	/* read http headers from client and write to server */
	Rio_readlineb(&rio_to_client, buf, MAXLINE);
	while (strncmp(buf, "\r\n", MAXLINE) != 0) {
		if (strstr(buf, "User-Agent"))
			strncpy(buf, user_agent_hdr, MAXLINE);
		else if (strstr(buf, "Connection"))
			strncpy(buf, "Connection: close\r\n", MAXLINE);
		else if (strstr(buf, "Proxy-Connection"))
			strncpy(buf, "Proxy-Connection: close\r\n", MAXLINE);
		else if (strstr(buf, "Host")) {
			// ignore, because we already sent one
			Rio_readlineb(&rio_to_client, buf, MAXLINE);
			continue;
		}
		Rio_writen(to_server_fd, buf, strlen(buf));
		Rio_readlineb(&rio_to_client, buf, MAXLINE);
	}
	/* terminates request headers */
	Rio_writen(to_server_fd, "\r\n", 2);


	/* read http response from server and write to client */
	memset(buf, 0, MAXLINE);
	Rio_readlineb(&rio_to_server, buf, MAXLINE);
	int content_len = 0;
	
	while (strncmp(buf, "\r\n", MAXLINE) != 0) {
		char* ptr = strstr(buf, "Content-length");
		if (ptr) {
			content_len = atoi(ptr + 16);
		}
		//printf("write2: %s\n", uri);
		//printf("fd: %d\n", to_client_fd);
		//printf("%d: %s\n", strlen(buf), buf);
		Rio_writen(to_client_fd, buf, strlen(buf));
		memset(buf, 0, MAXLINE);
		Rio_readlineb(&rio_to_server, buf, MAXLINE);
	}
	/* terminates response headers */
	Rio_writen(to_client_fd, "\r\n", 2);

	int size = 0;
	int need_cache = 0;
	/* shall we cache it? */
	if (content_len < MAX_OBJECT_SIZE)
		need_cache = 1;

	/* init a new cache block */
	struct cache_block* blk = (struct cache_block*) 
								malloc(sizeof(struct cache_block));
	init_cache(blk);
	strncpy(blk->uri, uri, MAXLINE);

	if (content_len > 0)
		blk->file = (char*) malloc(sizeof(char) * content_len);
	else
		blk->file = (char*) malloc(sizeof(char) * MAX_OBJECT_SIZE);

	memset(buf, 0, MAXLINE);
	int total_size = 0;
	char* headptr = blk->file;

	/* read response contents and write to client */
	while ((size = Rio_readnb(&rio_to_server, buf, MAXLINE)) > 0) {
		total_size += size;
		if (total_size > MAX_OBJECT_SIZE)
			need_cache = 0;
		if (need_cache) {
			memcpy(headptr, buf, size);
			headptr += size;
		}
		//printf("write3: %s\n", uri);
		Rio_writen(to_client_fd, buf, size);
		memset(buf, 0, MAXLINE);
	}

	/* add cache block */
	if (need_cache) {
		blk->size = total_size;
		// resize block
		blk->file = (char*) realloc(blk->file, blk->size);
		// size overflow, need to evict using LRU
		if (blk->size + cache_size > MAX_CACHE_SIZE)
			evict_cache(head, blk->size);
		// add it
		add_cache(head, blk);
	}
	/* prevent memory leakage */
	else {
		free_cache_node(blk);
	}

	Close(to_server_fd);
	return;
}
Exemplo n.º 27
0
int main(int argc, char **argv)
{
  //declare variables
  int clientfd, port; 
  char *host, buf[MAXLINE], *page, *temp_name, *modify; 
  rio_t rio; 

  //init parameters
  host = argv[1]; //host name
  port = atoi(argv[2]); //port number
  page = argv[3]; //file path

  //open socket
  clientfd = Open_clientfd(host, port);
  Rio_readinitb(&rio, clientfd); 

  //check temp
  temp_name = (char*) malloc(100 * sizeof(char));
  modify = (char*) malloc(100 * sizeof(char));
  strcat(temp_name, "./temp/");
  strcat(temp_name, host);
  strcat(temp_name, page + 1);
  
  FILE *temp_file = fopen(temp_name, "r");
  if(temp_file == NULL) { //temp file does not exist
    modify[0] = '\0';
  } else { //temp file exists
    char * line = (char*) malloc(1000 * sizeof(char));
    int * len = (int*) malloc(sizeof(int));
    while(getline(&line, len, temp_file) != -1) {
      if(strstr(line, "Last-Modified:") != NULL) {
	strcpy(modify, "If-Modified-Since: ");
	strcat(modify, line + 15);
	break;
      }
    }
    fclose(temp_file);
    free(line);
    free(len);
  }

  //construct request
  strcpy(buf, "GET ");
  strcat(buf, page);
  strcat(buf, " HTTP/1.1\r\nHost:");
  strcat(buf, host);
  strcat(buf, "\r\n");
  if(strlen(modify) != 0) strcat(buf, modify);
  strcat(buf, "\r\n");
  
  //send request
  Rio_writen(clientfd, buf, strlen(buf));

  //get response
  int conLen = 0;
  int body = 0;
  int count = 0;
  while (1) { 
    Rio_readlineb(&rio, buf, MAXLINE);
    
    //304 read local file
    if(strstr(buf, "304 Not Modified")) {
      temp_file = fopen(temp_name, "r");
      char * line = (char*) malloc(1000 * sizeof(char));
      int * len = (int*) malloc(sizeof(int));
      while(getline(&line, len, temp_file) != -1) {
	Fputs(line, stdout);
	fflush(stdout);
      }
      break;
    }

    //404 not found
    if(strstr(buf, "404 Not Found")) {
	Fputs(buf, stdout);
	Fputs("\n", stdout);
	fflush(stdout);
	break;
    }

    //read remote file
    if(count == 0) {
      //if(temp_file != NULL) fclose(temp_file);
      temp_file = fopen(temp_name, "w");
    }
    if(strstr(buf, "Content-Length") != NULL) {
      char conLenChar[100];
      strcpy(conLenChar, (buf + 16));
      conLen = atoi(conLenChar);
    }
    if(strlen(buf) == 2) body = 1;
    Fputs(buf, stdout); 
    fprintf(temp_file, "%s", buf);
    fflush(stdout);    
    if(body == 1) {
      conLen -= strlen(buf) * sizeof(char);
      if(conLen <= 0) {
	break;
      }
    }
    count++;
  } 

  //terminate the program
  close(clientfd); 
  if(temp_file != NULL) fclose(temp_file);
  exit(0); 
}
Exemplo n.º 28
0
int main ( int argc, char *argv[] )
{

    int clientfd, port;
    char *host, buf[MAXLINE];
    rio_t rio;
    char usrNameBuf[40];
    char usrPassBuf[40];
    char cmdBuf[128];
    char *quit = "quit\n"; 
    char sendLine[BUFFSIZE], recvLine[BUFFSIZE];

    if (argc != 3) {
	fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
	exit(0);
    }
    host = argv[1];
    port = atoi(argv[2]);


   printf("Username:\n");
   Fgets(usrNameBuf, 39, stdin);
   usrNameBuf[39]='\n';
   printf("Password:\n");
   Fgets(usrPassBuf, 39, stdin);
   usrPassBuf[39]='\n';

  //makd tcp connnection
   clientfd = Open_clientfd(host, port);
   Rio_readinitb(&rio, clientfd);
   Rio_writen(clientfd, usrNameBuf, strlen(usrNameBuf));
   Rio_writen(clientfd, usrPassBuf, strlen(usrPassBuf));
   printf("User Name = %s", usrNameBuf);
   printf("User Pass = %s", usrPassBuf);
 
   //the client sends the user's password 
   //Rio_writen(clientfd, usrPassBuf, strlen(usrNameBuf));

   Rio_readlineb(&rio, buf, MAXLINE);
   printf("Login status: %s\n", buf);

   //clsoe connection if login failed
   if(!strcmp(buf, "Login Failed\n")){
	exit(0);
	
   }

   if(!strcmp(buf, "Login Approved\n")){

	printf("rrsh >");
	while(Fgets(cmdBuf, 127, stdin)!=NULL){
		//if the user decide to quit
		if(!strcmp(cmdBuf, "quit\n")){
			exit(0);
		}


	        if(strcmp(recvLine,"RRSH COMMAND COMPLETED\n")==0){
//			printf("rrsh >");
		}
		if(strcmp(buf, "\n")){
			Rio_writen(clientfd, cmdBuf, strlen(cmdBuf));
			while(Rio_readlineb(&rio, recvLine, MAXLINE)!=0){
				if(strcmp(recvLine,"RRSH COMMAND COMPLETED\n")==0)
					break;
				else		
					 Fputs(recvLine, stdout);
			}
			printf("rrsh >");			

		}

	}

   }


    Close(clientfd); //line:netp:echoclient:close
    exit(0);
}
int main(int argc, char **argv) 
{
    int clientfd;
    char *host, *port, buf[MAXLINE], receive[MAXLINE];
    rio_t rio;
    char *login = "******";
    char *Complete = "RRSH COMMAND COMPLETED\n";
    char *quit = "quit\n";
    if (argc != 3) {
	fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
	exit(0);
    }
    host = argv[1];
    port = argv[2];

    clientfd = Open_clientfd(host, port);
    Rio_readinitb(&rio, clientfd);

    Fputs("Username: "******"Password: "******"\n")))
    {
	Close(clientfd);
	exit(125);
    }
    while(1)
    {
    	Fputs("rrsh> ", stdout);
	Fgets(buf, MAXLINE, stdin);
	
	if(strcmp(buf, quit) == 0)
	{
		Rio_writen(clientfd, quit, strlen(quit));
		break;
	}
		
	if(strcmp(buf, "\n") == 0)
	{
		continue;
	}
	
	Rio_writen(clientfd, buf, strlen(buf));
	Rio_readlineb(&rio, buf, MAXLINE);

	while(strcmp(buf, Complete))
	{
		Fputs(buf,stdout);
		Rio_readlineb(&rio, buf, MAXLINE);
	}
    }	 
    Close(clientfd); //line:netp:echoclient:close
    exit(0);
}
Exemplo n.º 30
0
/*
 * Listens for incoming connections on a port number specified on the command 
 * line. Once a connection is established, it reads the entirety of the 
 * request from the client and parse the request. It determines whether the 
 * client has sent a valid HTTP request; if so, it then establishes its own
 * connection to the appropriate web server then requests the object the client
 * specified. Finally, the proxy reads the server’s response and forwards it to 
 * the client.
 */
void doit(int fd)
{
	int isnot_safe;
	char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE], 
	temp[MAXLINE], host[MAXLINE], clientheaders[MAXLINE], readRequests[MAXLINE],
	path[MAXLINE];
	char *data = malloc(MAX_OBJECT_SIZE*sizeof(char));
	size_t sizeOfTotalData = 0;
	char *dataBuffer = malloc(MAX_OBJECT_SIZE*sizeof(char));
  char *temporaryData = dataBuffer;
	char* port_pointer;
	int port = 80, len, len2;
	rio_t rio, rio1;
	int serverConn_fd;
  cache_node *fileObj = NULL;
  //cache_node *addFile = malloc(sizeof(cache_node));

	//associates rio with the descriptor
	Rio_readinitb(&rio, fd);
	//reads and flushes the buf with the request
	Rio_readlineb(&rio, buf, MAXLINE);
	
	//parsing the initial request
	isnot_safe = safe_scan(sscanf(buf, "%s %s %s\r\n", method, uri, version));

	if (isnot_safe)
		return;

	//cannot perform if it is not a Get method
    if (strcasecmp(method, "GET")) {
		printf("501 Not Implemented Proxy does not handle this method");
		return;
    }
    //printf("Does it reach here after Get?\n");

    //Parse the URL properly
    if (strstr(uri, "http://"))
    {
        isnot_safe = safe_scan(sscanf(uri, "%[^'/']%s", temp,  uri));

        if (isnot_safe)
        	return;

        memmove(uri, uri+2, strlen(uri));
    }

    //printf("%s\n", uri);
    if(strchr(uri, '/') != NULL) {
		isnot_safe = safe_scan(sscanf(uri, "%[^'/']%s", host, path));

        if (isnot_safe)
        	return;
    }
    else {
    	//if no path is specified then it is just "/".
		strcpy(path, "/");
		strcpy(host, uri);
    }
    //printf("HOST %s\n", host);
    //Get the port if it is specified
    if ((port_pointer = strchr(host, ':')) != NULL){
		isnot_safe = safe_scan(sscanf(port_pointer, ":%d", &port));

        if (isnot_safe)
        	return;

		isnot_safe = safe_scan(sscanf(host, "%[^0-9:]", host));

        if (isnot_safe)
        	return;
    }

    //Look up the object in the cache
    if ((fileObj = find(uri)) != NULL)
    {
      rio_writen(fd, fileObj->file, fileObj->size);
      return;
    }    
    //flush the readRequests associated with the path and host
    sprintf(readRequests, "GET %s HTTP/1.0\r\nHost: %s\r\n", path, host);

    //open a server connection with the host and port
    serverConn_fd = Open_clientfd(host, port);

    //associate the file descriptor and write to it from readRequests
    
    Rio_readinitb(&rio1, serverConn_fd);
    isnot_safe = safe_rio(rio_writen(serverConn_fd, readRequests, 
    						strlen(readRequests)));

    if (isnot_safe)
        return;

    //Send in all necessary headers
    isnot_safe = safe_rio(rio_writen(serverConn_fd, (void *) user_agent_hdr, 
                                                  strlen(user_agent_hdr)));

    if (isnot_safe)
        return;

    isnot_safe = safe_rio(rio_writen(serverConn_fd, (void *) accept_hdr, 
                                                  strlen(accept_hdr)));

    if (isnot_safe)
        return;

    isnot_safe = safe_rio(rio_writen(serverConn_fd, 
                (void *) accept_encoding_hdr, strlen(accept_encoding_hdr)));

    if (isnot_safe)
        return;

    isnot_safe = safe_rio(rio_writen(serverConn_fd,
      "Proxy-Connection: close\r\nConnection:\tclose\r\n\r\n", 52));

    if (isnot_safe)
        return;


    //Get additional requests by clients that are not already passed and 
    //pass them through without changing them
    while((len = rio_readlineb(&rio, readRequests, MAXBUF)) && 
    			strcmp(readRequests, "\r\n"))
    {
      //Get the headers
	    isnot_safe = safe_scan(sscanf(readRequests, "%s ",clientheaders));

        if (isnot_safe)
        	return;

        //see if the headers are not the ones we already passed.
	    if(!strstr(clientheaders, "Host:")&& !strstr(clientheaders,"Connection:")
        && !strstr(clientheaders, "User-Agent:") && !strstr(clientheaders,
          "Accept-Encoding:") && !strstr(clientheaders,"Accept:") && 
        !strstr(clientheaders,"Proxy-Connection:"))

	    {

	        isnot_safe = safe_rio(rio_writen(serverConn_fd, readRequests, len));
			
        	if (isnot_safe)
        		return;	   	
    	}
  	}


  //Finally write to the client desciptor what the server responds with
  while((len2 = rio_readnb(&rio1, data, MAXLINE))){
    sizeOfTotalData += len2;

    //Keep writing to the memory at temporaryData as long as size of the data 
    //so far is less than max object size
    if (sizeOfTotalData < MAX_OBJECT_SIZE){
      memcpy(temporaryData,data,len2);
      temporaryData += len2;
    }
    rio_writen(fd, data, len2);
  }

  //We shouldn't add the object to the cache if the size of the
  //data exceeds the max object size or is 0
  if(sizeOfTotalData <= MAX_OBJECT_SIZE && sizeOfTotalData != 0)
  {
    pthread_rwlock_wrlock(&lock);
    addToFront(uri, dataBuffer, sizeOfTotalData);
    pthread_rwlock_unlock(&lock);
  }

  free(data);

  return;

}