Exemplo n.º 1
0
/* This function is the entrypoint for the library and * loads definitions of the 'web' scope */
int cortexmain(int argc, char* argv[]) {
    CX_UNUSED(argc);
    CX_UNUSED(argv);
    
    int result = web_load();
    
    /* $begin(cortexmain) */
    /* Insert user-code here */
    /* $end */

    return result;
}
Exemplo n.º 2
0
/*forward response (lines/headers/body) to client*/
int forward_response(rio_t *rio_server, char *uri, char *resp_buf, int connfd)
{
    char response[MAX_OBJECT_SIZE];
    char resp_body[MAX_OBJECT_SIZE];
    char resp_line[MAXLINE];
    int resp_size = 0;
    int length = 0;
    int body_size = 0;
    int content_len = -1;
    int cont_size = 0;
#if 1
    if((web_load(uri, response)) == 0)
	{
		printf("Cache hit!\n");
		if(rio_writen(connfd, response, sizeof(response)) < 0)
    	{
        		fprintf(stderr, "rio_writen send cache response error\n");
        		return -1;
    	}
    	memset(response, 0, sizeof(response));
        return 0;
	}
#endif
    /*send reponse line and headers to client*/
    while((length = rio_readlineb(rio_server, resp_line, MAXLINE)) > 0)
    {
        strcat(response, resp_line);
        /* send headers to clinet*/
        if (rio_writen(connfd, resp_line, length) < 0) {
            fprintf(stderr, "Error: rio_writen() in forward_response header:  %s\n", strerror(errno));  
            DEBUG_PRINT("Error: rio_writen() in forward_response header\n");
            return -1;
        }
        
        /*empty line between headers and body*/
        if(strcmp(resp_line, "\r\n") == 0)
            break;
        
        /* get size of response body from response header: Content-Length */
        if (strstr(resp_line, "Content-Length: ")) {
            content_len = parse_num(resp_line);
            if(content_len < 0)
            {
                fprintf(stderr, "Error get Content-Length: %s", resp_line);
                DEBUG_PRINT("Error get Content-Length\n");
                Close(connfd);
                return -1;
            }
        }

        memset(resp_line, 0, sizeof(resp_line));
        resp_size += length;
    }



    /*w/o content length in response headers*/
    if(content_len == -1)
        content_len = MAX_OBJECT_SIZE;
    
    cont_size = MIN(content_len, MAX_OBJECT_SIZE);
    
    /* Send response body to client */
#if DEBUG
    /* send fake response body */
    Rio_writen(connfd, test, strlen(test));
#else

    while((length = rio_readnb(rio_server, resp_body, cont_size)) > 0)
    {
        strcat(response, resp_body);
        if (rio_writen(connfd, resp_body, length) < 0) {
            fprintf(stderr, "rio_writen in forward_response body error: %s!", strerror(errno));
            DEBUG_PRINT("rio_writen in forward_response body error!");
            return -1;
        }
        body_size += length;
    }
#endif

#if 1
    resp_size += body_size;
    if(resp_size <= MAX_OBJECT_SIZE)
    {
        if(web_store(uri, response) < 0) //store response in cache
        {
            printf("web_store, cache error!\n");
            return -1;
        }
    }
#endif

#if 0
    srcfd = Open(filename, O_RDONLY, 0);
    srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0);
    Close(srcfd);
    Rio_writen(fd, srcp, filesize);
    Munmap(srcp, filesize);
#endif
    
    return 0;
}