static foreign_t pl_order_table_mapping(term_t handle, term_t from, term_t to, control_t ctrl) { OrdTable t; int f; if ( !get_order_table(handle, &t) ) return FALSE; if ( PL_get_integer(from, &f) && f >= 0 && f <= 255 ) return unify_mapped_code(to, ORD(t, f)); if ( PL_is_variable(from) ) { switch(PL_foreign_control(ctrl)) { case PL_FIRST_CALL: f = 0; break; case PL_REDO: f = (int)PL_foreign_context(ctrl); break; case PL_PRUNED: return TRUE; } while( f <= 255 && !unify_mapped_code(to, ORD(t, f)) ) f++; if ( f <= 255 ) { if ( !PL_unify_integer(from, f) ) return FALSE; PL_retry(f+1); } return FALSE; } return FALSE; }
foreign_t fcgi_accept(control_t h) { FCGX_Request *req; fd_set fds; fcgi_context *ctxt; int status; FCGI_debug("fcgi_accept()"); if ( FCGX_IsCGI() ) { return TRUE; } ctxt = pthread_getspecific(key); if ( !ctxt ) { ctxt = malloc(sizeof(*ctxt)); memset(ctxt, 0, sizeof(*ctxt)); } if ( ctxt->req ) { fcgi_finish(); } req = malloc(sizeof(*req)); memset(req, 0, sizeof(*req)); status = FCGX_InitRequest(req, 0, 0); if ( status != FCGI_SUCCESS ) { return fcgi_raise_error(status, "FCGX_InitRequest() failed"); } FD_ZERO(&fds); FD_SET(req->listen_sock, &fds); if ( select(req->listen_sock+1, &fds, NULL, NULL, NULL) < 1 ) { return FALSE; } status = FCGX_Accept_r(req); if ( status != FCGI_SUCCESS ) { return fcgi_raise_error(status, "FCGX_Accept_r() failed"); } FCGI_debug("REMOTE_ADDR: %s, REQUEST_METHOD: %s, REQUEST_URI: %s", FCGX_GetParam("REMOTE_ADDR", req->envp), FCGX_GetParam("REQUEST_METHOD", req->envp), FCGX_GetParam("REQUEST_URI", req->envp)); if ( !ctxt ) { ctxt = malloc(sizeof(*ctxt)); memset(ctxt, 0, sizeof(*ctxt)); } ctxt->req = req; ctxt->pl_in = Suser_input; ctxt->h_in = Suser_input->handle; ctxt->fn_in = Suser_input->functions; ctxt->fcgi_in = req->in; ctxt->pl_out = Suser_output; ctxt->h_out = Suser_output->handle; ctxt->fn_out = Suser_output->functions; ctxt->fcgi_out = req->out; ctxt->pl_err = Suser_error; ctxt->h_err = Suser_error->handle; ctxt->fn_err = Suser_error->functions; ctxt->fcgi_err = req->err; ctxt->env = req->envp; pthread_setspecific(key, ctxt); Suser_input->handle = req->in; Suser_input->functions = &fcgi_functions; Suser_output->handle = req->out; Suser_output->functions = &fcgi_functions; Suser_error->handle = req->err; Suser_error->functions = &fcgi_functions; PL_retry(0); }