Example #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);
}
Example #2
0
static LRESULT CALLBACK
wndprocret_hook_proc(int code, WPARAM cur_thread, LPARAM details)
{
    HWND hwnd;
    UINT msg;
    WPARAM wparam;
    LPARAM lparam;

    LONG style;

    if (!g_wm_seamless_focus)
        goto end;

    if (code < 0)
        goto end;

    hwnd = ((CWPRETSTRUCT *) details)->hwnd;
    msg = ((CWPRETSTRUCT *) details)->message;
    wparam = ((CWPRETSTRUCT *) details)->wParam;
    lparam = ((CWPRETSTRUCT *) details)->lParam;

    if (!is_toplevel(hwnd)) {
        goto end;
    }

    style = GetWindowLong(hwnd, GWL_STYLE);

    switch (msg) {
    case WM_WINDOWPOSCHANGED:
    {
        WINDOWPOS *wp = (WINDOWPOS *) lparam;

        if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))
            break;

        if (!(wp->flags & SWP_NOZORDER))
            update_zorder(hwnd);

        break;
    }


    case WM_SETTEXT:
    {
        unsigned short title[150];
        if (!(style & WS_VISIBLE))
            break;
        /* We cannot use the string in lparam because
           we need unicode. */
        GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));
        vchannel_write("TITLE", "0x%08lx,%s,0x%08x", hwnd,
                       vchannel_strfilter_unicode(title), 0);
        break;
    }

    case WM_SETICON:
    {
        HICON icon;
        if (!(style & WS_VISIBLE))
            break;

        /*
         * Somehow, we never get WM_SETICON for the small icon.
         * So trigger a read of it every time the large one is
         * changed.
         */
        icon = get_icon(hwnd, 0);
        if (icon) {
            update_icon(hwnd, icon, 0);
            DeleteObject(icon);
        }
    }

    default:
        break;
    }

    if (msg == g_wm_seamless_focus) {
        /* For some reason, SetForegroundWindow() on menus
           closes them. Ignore focus requests for menu windows. */
        if ((GetForegroundWindow() != hwnd) && !is_menu(hwnd))
            SetForegroundWindow(hwnd);

        vchannel_write("ACK", "%u", g_shdata->blocked_focus_serial);
    }

end:
    return CallNextHookEx(g_wndprocret_hook, code, cur_thread, details);
}