Esempio n. 1
0
classfile* read_classfile(FILE* f){
    classfile* c = malloc(sizeof(classfile));
    memcheck(c);
    c->magic = read32(f);
    c->minor_version = read16(f);
    c->major_version = read16(f);
    c->constant_pool_count = read16(f);
    c->constant_pool = malloc(sizeof(void*) * (c->constant_pool_count - 1));
    for(int i = 0; i < (c->constant_pool_count - 1); i++){
	c->constant_pool[i] = read_constant(i, f);
//	print_constant(c->constant_pool[i], stderr);
    }
    c->access_flags = read16(f);
    c->this_class = read16(f);
    c->super_class = read16(f);
    c->interfaces_count = read16(f);
    if(c->interfaces_count > 0){
	c->interfaces = malloc(sizeof(short) * c->interfaces_count);
	memcheck(c->interfaces);
	for(int i = 0; i < (c->interfaces_count); i++){
	    c->interfaces[i] = read16(f);
	}
    }
    c->fields_count = read16(f);
    if(c->fields_count > 0){
	c->fields = malloc(sizeof(void*) * c->fields_count);
	memcheck(c->fields);
	//have to read in the fields
	for(int i = 0; i < (c->fields_count); i++){
	    c->fields[i] = read_field(f);
	}
    } else
	c->fields = NULL;
    c->methods_count = read16(f);
    if(c->methods_count > 0){
	c->methods = malloc(sizeof(void*) * c->methods_count);
	memcheck(c->methods);
	for(int i = 0; i < (c->methods_count); i++){
	    c->methods[i] = read_method(c, f);
	}
    } else 
	c->methods = NULL;
    c->attributes_count = read16(f);
    if(c->attributes_count > 0){
	c->attributes = malloc(sizeof(void*) * c->attributes_count);
	memcheck(c->attributes);
	for(int i = 0; i < c->attributes_count; i++){
	    c->attributes[i] = read_attribute(f);
	}
    } else 
	c->attributes = NULL;

    int last = getc(f);
    assert(last == EOF);
    return c;
}
Esempio n. 2
0
int http_request(int fd)
{
    int file;
    struct stat file_stat;
    char buffer[DEFAULT_BUFFER_SIZE] ={0};
    char method[32] ={0};
    char version[32] ={0}; // ignore
    char uri[DEFAULT_BUFFER_SIZE] = {0};
    char path[DEFAULT_BUFFER_SIZE] = {0};
    //char cgiargs[DEFAULT_BUFFER_SIZE];

    if (read_method(fd, buffer, sizeof buffer) < 0) {
        http_error(fd, hst_bad_request);
        return -1;
    }

    //printf("first line : %s\n", buffer);

    sscanf(buffer, "%s %s %s", method, uri, version);

    if (strcasecmp(method, method_GET) != 0) {
        http_error(fd, hst_no_implemented);
        return -1;
    }

    printf("mathod is GET, uri is %s\n", uri);

    read_headers(fd, buffer, sizeof buffer); // ignore all hreaders

    //printf("DEBUG\n");

    if (parse_uri(uri, path) < 0) {
        http_error(fd, hst_not_found);
        return -1;
    }

    printf("file path is %s\n", path);

    if (stat(path, &file_stat) < 0) {
        http_error(fd, hst_not_found);
        return -1;
    }

    if((file = open(path, O_RDONLY)) < 0) {
        http_error(fd, hst_uknown);
        return -1;
    }

    if(send_file(fd, file) < 0){
        return -1;
    }

    return 0;
}