Exemplo n.º 1
0
void * handle_request(void * args){
    int * fd = (int *)args;
    int run = 1;
    int ptr = 0;
    ssize_t got_bytes = 0;
    char * buf = malloc(BUFFER_SIZE);
    while( run ) {
        got_bytes = read( *fd, buf + ptr, BUFFER_SIZE - ptr);
        if(got_bytes <= 0){
            buf[ptr] = 0; // Terminate string
            break;
        }
        ptr += got_bytes;
        if( get_line(buf, ptr) ){
            break;
        }
    }
    
    struct request_t * req = malloc(sizeof(struct request_t));
    req->fd = *fd;
    req->selector = buf;
    req->selector_len = strlen(req->selector);
    req->path = resolve_selector(NULL, req->selector);
    req->path_len = strlen(req->path);
    
    plog("Request: %s", buf);
    
    if( is_menu( req ) ) {
        char * gopherfile;
        asprintf(&gopherfile, "%s%s", req->path, GOPHERMAP_FILENAME);
        plog("Sending menu @ %s", gopherfile);
        menu_item * items[1024];
        int item_count = parse_gophermap( gopherfile, &items[0], DEFAULT_HOST, DEFAULT_PORT );
        plog("Gophermap parsed, items: %u", item_count);
        for( int i = 0; i < item_count; i++ ){
            print_menu_item( req->fd, items[i] );
        }
        free( gopherfile) ;
    } else if ( is_file( req->path ) ) {
        plog("Sending file");
        print_file(req);
    } else if ( is_dir( req->path ) ) {
        plog("Sending dir");
        print_directory(req);
    }
    
    close_socket(req->fd, 1);
    free(req->selector);
    free(req->path);
    free(args);
    pthread_exit(NULL);
}
Exemplo n.º 2
0
void process_argument(struct memory_region *operands,
		      int *current_operand,
		      char **c) {
    char *selector;
    char sigil;
    switch(**c) {
	case '0': case '1': case '2':
	case '3': case '4': case '5':
	case '6': case '7': case '8':
	case '9':
	    operands->data[(*current_operand)++] =
		strtol(*c,c,10);
	    break;
	case '\'':
	    selector = ++*c;
	    for(;**c != '\'';(*c)++)
		;
	    **c = '\0';
	    (*c)++;
	    operands->data[(*current_operand)++] =
		resolve_selector(selector);
	    break;
	case '#':
	case '@':
	case '$':
	case '%':
	    sigil = **c;
	    (*c)++;
	    char *name = grab_identifier(c);
	    char *name_with_sigil = bytecode_malloc(strlen(name) + 2);
	    name_with_sigil[0] = sigil;
	    name_with_sigil[1] = '\0';
	    strcat(name_with_sigil,name);
	    mr_mark_name(operands,(*current_operand)++,name_with_sigil);
	    break;
	default:
	    fprintf(stderr,"hit operand default: %s\n",*c);
	    abort();
    }
}