Esempio n. 1
0
FILE *ApacheRequest_tmpfile(ApacheRequest *req, ApacheUpload *upload)
{
    request_rec *r = req->r;
    FILE *fp;
    char prefix[] = "apreq";
    char *name = NULL;
    int fd = 0; 
    int tries = 100;

    while (--tries > 0) {
	if ( (name = tempnam(req->temp_dir, prefix)) == NULL )
	    continue;
	fd = ap_popenf(r->pool, name, O_CREAT|O_EXCL|O_RDWR|O_BINARY, 0600);
	if ( fd >= 0 )
	    break; /* success */
	else
	    free(name);
    }

    if ( tries == 0  || (fp = ap_pfdopen(r->pool, fd, "w+" "b") ) == NULL ) {
	ap_log_rerror(REQ_ERROR, "[libapreq] could not create/open temp file");
	if ( fd >= 0 ) { remove(name); free(name); }
	return NULL;
    }

    upload->fp = fp;
    upload->tempname = name;
    ap_register_cleanup(r->pool, (void *)upload,
			remove_tmpfile, ap_null_cleanup);
    return fp;

}
Esempio n. 2
0
static void *embperl_create_dir_config(apr_pool_t * p, char *d)
    {
    /*char buf [20] ;*/
    tApacheDirConfig *cfg ;
    apr_pool_t * subpool ;

    embperl_ApacheInitUnload (p) ;

#ifdef APACHE2
    apr_pool_create_ex(&subpool, p, NULL, NULL);
#else
    subpool = ap_make_sub_pool(p);
#endif
    cfg = (tApacheDirConfig *) apr_pcalloc(subpool, sizeof(tApacheDirConfig));

#if 0
#ifdef APACHE2
    apr_pool_cleanup_register(subpool, cfg, embperl_ApacheConfigCleanup, embperl_ApacheConfigCleanup); 
#else
    ap_register_cleanup(subpool, cfg, embperl_ApacheConfigCleanup, embperl_ApacheConfigCleanup);
#endif
#endif
    
    embperl_DefaultReqConfig (&cfg -> ReqConfig) ;
    embperl_DefaultAppConfig (&cfg -> AppConfig) ;
    embperl_DefaultComponentConfig (&cfg -> ComponentConfig) ;
    cfg -> bUseEnv = -1 ; 

    if (bApDebug)
        ap_log_error (APLOG_MARK, APLOG_WARNING | APLOG_NOERRNO, APLOG_STATUSCODE NULL, "EmbperlDebug: create_dir_config %s (0x%p) [%d/%d]\n", cfg -> AppConfig.sAppName?cfg -> AppConfig.sAppName:"", cfg, getpid(), gettid()) ;

    return cfg;
    }
Esempio n. 3
0
API_EXPORT(piped_log *) ap_open_piped_log (pool *p, const char *program)
{
    piped_log *pl;

    pl = ap_palloc (p, sizeof (*pl));
    pl->p = p;
    pl->program = ap_pstrdup (p, program);
    pl->pid = -1;
    ap_block_alarms ();
    if (pipe (pl->fds) == -1) {
	int save_errno = errno;
	ap_unblock_alarms();
	errno = save_errno;
	return NULL;
    }
    ap_register_cleanup (p, pl, piped_log_cleanup, piped_log_cleanup_for_exec);
    if (piped_log_spawn (pl) == -1) {
	int save_errno = errno;
	ap_kill_cleanup (p, pl, piped_log_cleanup);
	close (pl->fds[0]);
	close (pl->fds[1]);
	ap_unblock_alarms ();
	errno = save_errno;
	return NULL;
    }
    ap_unblock_alarms ();
    return pl;
}
Esempio n. 4
0
static const char *load_file(cmd_parms *cmd, void *dummy, char *filename)
{
    ap_os_dso_handle_t handle;
    char *file;

    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
    if (err != NULL) {
        return err;
    }
    
    file = ap_server_root_relative(cmd->pool, filename);
    
    if (!(handle = ap_os_dso_load(file))) {
	const char *my_error = ap_os_dso_error();
	return ap_pstrcat (cmd->pool, "Cannot load ", filename, 
			" into server:", 
			my_error ? my_error : "(reason unknown)",
			NULL);
    }
    
    ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL,
		"loaded file %s", filename);

    ap_register_cleanup(cmd->pool, (void *)handle, unload_file, ap_null_cleanup);

    return NULL;
}
Esempio n. 5
0
PyObject * MpRequest_FromRequest(request_rec *req)
{
    requestobject *result;

    result = PyMem_NEW(requestobject, 1);
    if (! result)
	return PyErr_NoMemory();

    result->request_rec = req;
    result->ob_type = &MpRequest_Type;
    result->connection = NULL;
    result->server = NULL;
    result->next = NULL;
    result->prev = NULL;
    result->main = NULL;
    result->headers_in = MpTable_FromTable(req->headers_in);
    result->headers_out = MpTable_FromTable(req->headers_out);
    result->err_headers_out = MpTable_FromTable(req->err_headers_out);
    result->subprocess_env = MpTable_FromTable(req->subprocess_env);
    result->notes = MpTable_FromTable(req->notes);
    result->Request = NULL;
    result->header_sent = 0;
    result->content_type_set = 0;
    result->hstack = NULL;
    result->rbuff = NULL;
    result->rbuff_pos = 0;
    result->rbuff_len = 0;

    _Py_NewReference(result);
    ap_register_cleanup(req->pool, (PyObject *)result, python_decref, 
			ap_null_cleanup);

    return (PyObject *) result;
}
Esempio n. 6
0
static void save_conn_info(request_rec *r)
{
    conn_rec *c = r->connection;
    local_addr =  ap_psprintf(c->pool, "%pI", &c->local_addr);
    remote_addr = ap_psprintf(c->pool, "%pI", &c->remote_addr);

    ap_register_cleanup(c->pool, NULL, clear_conn_info, ap_null_cleanup);
}
Esempio n. 7
0
static PyObject *server_register_cleanup(serverobject *self, PyObject *args)
{

    cleanup_info *ci;
    PyObject *handler = NULL;
    PyObject *data = NULL;
    PyObject *Req = NULL;
    requestobject *req = NULL;

    if (! PyArg_ParseTuple(args, "OO|O", &Req, &handler, &data))
	return NULL; 

    if (! PyObject_HasAttrString(Req, "_req")) {
	PyErr_SetString(PyExc_ValueError, "first argument must be a Request object");
	return NULL;
    }
    else {

	req = (requestobject *) PyObject_GetAttrString(Req, "_req");

	if (! MpRequest_Check(req)) {
	    PyErr_SetString(PyExc_ValueError, 
			    "first argument must be a request object");
	    return NULL;
	}
	else if(!PyCallable_Check(handler)) {
	    PyErr_SetString(PyExc_ValueError, 
			    "second argument must be a callable object");
	    return NULL;
	}
    }
    
    ci = (cleanup_info *)malloc(sizeof(cleanup_info));
    ci->request_rec = NULL;
    ci->server_rec = self->server;
    Py_INCREF(handler);
    ci->handler = handler;
    ci->interpreter = ap_table_get(req->request_rec->notes, "python_interpreter");
    if (data) {
	Py_INCREF(data);
	ci->data = data;
    }
    else {
	Py_INCREF(Py_None);
	ci->data = Py_None;
    }
    
    ap_register_cleanup(child_init_pool, ci, python_cleanup, 
			ap_null_cleanup);
    
    Py_INCREF(Py_None);
    return Py_None;
}
Esempio n. 8
0
static void *embperl_merge_dir_config (apr_pool_t *p, void *basev, void *addv)
    {
    if (!basev)
        return addv ;
    
        {
        tApacheDirConfig *mrg ; /*= (tApacheDirConfig *)ap_palloc (p, sizeof(tApacheDirConfig)); */
        tApacheDirConfig *base = (tApacheDirConfig *)basev;
        tApacheDirConfig *add = (tApacheDirConfig *)addv;
        apr_pool_t * subpool ;
#ifdef PERL_IMPLICIT_CONTEXT
        pTHX ;
        aTHX = NULL ;
#endif

#ifdef APACHE2
        apr_pool_create_ex(&subpool, p, NULL, NULL);
#else
        subpool = ap_make_sub_pool(p);
#endif
        mrg = (tApacheDirConfig *)apr_palloc (subpool, sizeof(tApacheDirConfig));

        if (bApDebug)
            ap_log_error (APLOG_MARK, APLOG_WARNING | APLOG_NOERRNO, APLOG_STATUSCODE NULL, "EmbperlDebug: merge_dir/server_config base=0x%p add=0x%p mrg=0x%p\n", basev, addv, mrg) ;

#ifdef APACHE2
        apr_pool_cleanup_register(subpool, mrg, embperl_ApacheConfigCleanup, embperl_ApacheConfigCleanup); 
#else
        ap_register_cleanup(subpool, mrg, embperl_ApacheConfigCleanup, embperl_ApacheConfigCleanup);
#endif

        memcpy (mrg, base, sizeof (*mrg)) ;

        if (bApDebug)
            ap_log_error (APLOG_MARK, APLOG_WARNING | APLOG_NOERRNO, APLOG_STATUSCODE NULL, "EmbperlDebug: merge_dir/server_config %s + %s\n", mrg -> AppConfig.sAppName, add -> AppConfig.sAppName) ;

        if (add -> AppConfig.sAppName)
            mrg -> AppConfig.sAppName = add -> AppConfig.sAppName ;

#include "epcfg.h" 

        if (add -> bUseEnv >= 0)
            mrg -> bUseEnv = add -> bUseEnv ;

        return mrg ;
        }
    }
Esempio n. 9
0
CAMLprim value
netcgi2_apache_request_register_cleanup (value rv, value f)
{
    CAMLparam2 (rv, f);
    request_rec *r = Request_rec_val (rv);
    value *v = (value *) apr_palloc (r->pool, sizeof (value));

    *v = f;
    register_global_root (v);

#if APACHE2
    apr_pool_cleanup_register (r->pool, v, run_cleanup, apr_pool_cleanup_null);
#else
    ap_register_cleanup (r->pool, v,
                         (void (*)(void *)) run_cleanup, ap_null_cleanup);
#endif

    CAMLreturn (Val_unit);
}
Esempio n. 10
0
static PyObject *req_register_cleanup(requestobject *self, PyObject *args)
{
    cleanup_info *ci;
    PyObject *handler = NULL;
    PyObject *data = NULL;

    if (! PyArg_ParseTuple(args, "O|O", &handler, &data))
	return NULL;  /* bad args */

    ci = (cleanup_info *)malloc(sizeof(cleanup_info));
    ci->request_rec = self->request_rec;
    ci->server_rec = self->request_rec->server;
    if (PyCallable_Check(handler)) {
	Py_INCREF(handler);
	ci->handler = handler;
	ci->interpreter = ap_table_get(self->request_rec->notes, "python_interpreter");
	if (data) {
	    Py_INCREF(data);
	    ci->data = data;
	}
	else {
	    Py_INCREF(Py_None);
	    ci->data = Py_None;
	}
    }
    else {
	PyErr_SetString(PyExc_ValueError, 
			"first argument must be a callable object");
	free(ci);
	return NULL;
    }
    
    ap_register_cleanup(self->request_rec->pool, ci, python_cleanup, 
			ap_null_cleanup);

    Py_INCREF(Py_None);
    return Py_None;

}
Esempio n. 11
0
static int embperl_ApacheInitUnload (apr_pool_t *p)

    {
#ifdef APACHE2
     if (!unload_subpool && p)
         {    
         apr_pool_create_ex(&unload_subpool, p, NULL, NULL); 
         apr_pool_cleanup_register(unload_subpool, NULL, embperl_ApacheInitCleanup, embperl_ApacheInitCleanup); 
         if (bApDebug)
             ap_log_error (APLOG_MARK, APLOG_WARNING | APLOG_NOERRNO, APLOG_STATUSCODE NULL, "EmbperlDebug: ApacheInitUnload [%d/%d]\n", getpid(), gettid()) ;
         }
#else
    if (!unload_subpool && p)
        {            
        unload_subpool = ap_make_sub_pool(p);
        ap_register_cleanup(unload_subpool, NULL, embperl_ApacheInitCleanup, embperl_ApacheInitCleanup);
        if (bApDebug)
            ap_log_error (APLOG_MARK, APLOG_WARNING | APLOG_NOERRNO, APLOG_STATUSCODE NULL, "EmbperlDebug: ApacheInitUnload [%d/%d]\n", getpid(), gettid()) ;
        }
#endif

    return ok ;
    }
Esempio n. 12
0
static void save_req_info(request_rec *r)
{
    /* to save for the request:
     * r->the_request + 
     * foreach header:
     *   '|' + header field
     */
    int len = strlen(r->the_request);
    char *ch;
    ap_table_do(count_headers, &len, r->headers_in, NULL);

    request_plus_headers = ap_palloc(r->pool, len + 2 /* 2 for the '\n' + '\0' at end */);
    ch = request_plus_headers;
    strcpy(ch, r->the_request);
    ch += strlen(ch);

    ap_table_do(copy_headers, &ch, r->headers_in, NULL);
    *ch = '\n';
    *(ch + 1) = '\0';

    ap_assert(ch == request_plus_headers + len);

    ap_register_cleanup(r->pool, NULL, clear_req_info, ap_null_cleanup);
}
Esempio n. 13
0
static const char *load_module(cmd_parms *cmd, void *dummy, 
                               char *modname, char *filename)
{
    ap_os_dso_handle_t modhandle;
    module *modp;
    const char *szModuleFile=ap_server_root_relative(cmd->pool, filename);
    so_server_conf *sconf;
    moduleinfo *modi;
    moduleinfo *modie;
    int i;

    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
    if (err != NULL) {
        return err;
    }
    
    /* 
     * check for already existing module
     * If it already exists, we have nothing to do 
     */
    sconf = (so_server_conf *)ap_get_module_config(cmd->server->module_config, 
	                                        &so_module);
    modie = (moduleinfo *)sconf->loaded_modules->elts;
    for (i = 0; i < sconf->loaded_modules->nelts; i++) {
        modi = &modie[i];
        if (modi->name != NULL && strcmp(modi->name, modname) == 0) {
            ap_log_error(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, cmd->server,
                          "module %s is already loaded, skipping", modname);
            return NULL;
        }
    }
    modi = ap_push_array(sconf->loaded_modules);
    modi->name = modname;

    /*
     * Load the file into the Apache address space
     */
    ap_server_strip_chroot(szModuleFile, 0);
    if (!(modhandle = ap_os_dso_load(szModuleFile))) {
	const char *my_error = ap_os_dso_error();
	return ap_pstrcat (cmd->pool, "Cannot load ", szModuleFile,
			" into server: ", 
			my_error ? my_error : "(reason unknown)",
			NULL);
    }
    ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL,
		"loaded module %s", modname);

    /*
     * Retrieve the pointer to the module structure through the module name:
     * First with the hidden variant (prefix `AP_') and then with the plain
     * symbol name.
     */
    if (!(modp = (module *)(ap_os_dso_sym(modhandle, modname)))) {
	return ap_pstrcat(cmd->pool, "Can't locate API module structure `", modname,
		       "' in file ", szModuleFile, ": ", ap_os_dso_error(), NULL);
    }
    modi->modp = modp;
    modp->dynamic_load_handle = (void *)modhandle;

    /* 
     * Make sure the found module structure is really a module structure
     * 
     */
    if (   modp->magic != MODULE_MAGIC_COOKIE_AP13 
        && modp->magic != MODULE_MAGIC_COOKIE_EAPI) {
        return ap_pstrcat(cmd->pool, "API module structure `", modname,
                          "' in file ", szModuleFile, " is garbled -"
                          " perhaps this is not an Apache module DSO?", NULL);
    }
    if (modp->magic == MODULE_MAGIC_COOKIE_AP13) {
        ap_log_error(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, NULL,
                     "Loaded DSO %s uses plain Apache 1.3 API, "
                     "this module might crash under EAPI! "
                     "(please recompile it with -DEAPI)", filename);
    }

    /* 
     * Add this module to the Apache core structures
     */
    ap_add_loaded_module(modp);

    /* 
     * Register a cleanup in the config pool (normally pconf). When
     * we do a restart (or shutdown) this cleanup will cause the
     * shared object to be unloaded.
     */
    ap_register_cleanup(cmd->pool, modi, 
		     (void (*)(void*))unload_module, ap_null_cleanup);

    /* 
     * Finally we need to run the configuration process for the module
     */
    ap_single_module_configure(cmd->pool, cmd->server, modp);

    return NULL;
}
Esempio n. 14
0
static int handle_request(request_rec * r, int role,
			   char *hdr_msg,
			   char *env_msg)
{
	pool *request_pool = r->main ? r->main->pool : r->pool;
	server_rec *main_server = r->server;
	jaxer_bucket_ctx *ctx;
	apr_status_t rv;
	const char *location;
	unsigned char *buf;
	int got_header = 0;
    int postdata_sent = 0;
	jaxer_worker *worker; // Need to get this from config
	jaxer_connection *ac;
    int should_redirect = 0;
	
	worker = get_worker_by_name(main_server, "worker1");
	
	ctx = ap_pcalloc(request_pool, sizeof(*ctx));
	if (!ctx) {
		compat_log_rerror(APLOG_MARK, APLOG_WARNING, apr_get_os_error(), r,
					 "mod_jaxer: apr_calloc bucket_ctx failed in handle_request function");
		return HTTP_INTERNAL_SERVER_ERROR;
	}
	
	if ((rv = jxr_conn_open(worker, &ctx->ac, r))!= APR_SUCCESS) 
	{
		compat_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "mod_jaxer: can't connect to socket");
		ctx->ac->has_error = 1;
		return HTTP_SERVICE_UNAVAILABLE;
	}
	ac = ctx->ac;
	ap_register_cleanup(request_pool, ctx->ac, jaxer_connection_cleanup, apr_pool_cleanup_null);


	// SEND HEADER
	if ((rv = jxr_send_message(ctx->ac, hdr_msg)) != APR_SUCCESS)
	{
		compat_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, "mod_jaxer: send HTTP header to jaxer server failed");
		ac->has_error = 1;
		return HTTP_INTERNAL_SERVER_ERROR;
	}
	
	// SEND ENV HEADER
	if ((rv = jxr_send_message(ctx->ac, env_msg)) != APR_SUCCESS) 
	{
		compat_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, "mod_jaxer: send environment vars to jaxer server failed");
		ac->has_error = 1;
		return HTTP_INTERNAL_SERVER_ERROR;
	}
	
	/* TODO: May need to send an empty doc */
	if ((rv = jxr_send_message_add_header(ac, 0, 0, BLOCKTYPE_DOCUMENT)) != APR_SUCCESS)
	{
		compat_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, "mod_jaxer: send emptty doc to jaxer error");
		ac->has_error = 1;
		return HTTP_INTERNAL_SERVER_ERROR;
	}
	if ((rv = jxr_send_message_add_header(ac, 0, 0, BLOCKTYPE_ENDREQUEST)) != APR_SUCCESS)
	{
		compat_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, "mod_jaxer: write end of (doc) request to jaxer server failed");
		ac->has_error = 1;
		return HTTP_INTERNAL_SERVER_ERROR;
	}
	

	/*
	 * Now get a response, to see if Jaxer wants anything until we get a header back
	 */
    got_header = 0;

	while (!got_header)
	{
		char msg_type;
        
		if ((rv = jxr_receive_message(ac, &buf)) != APR_SUCCESS)
		{
			compat_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, "mod_jaxer: check for request message failed");
			ac->has_error = 1;	
			return rv;
		}
        
        msg_type = jxr_msg_get_type(buf);
		switch ( msg_type)
		{
			// char uri[MAX_STRING_LEN];
			// apr_size_t pos, len;
		case BLOCKTYPE_REQUEST_POSTDATA:
			if (postdata_sent)
			{
				compat_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "mod_jaxer: requesting postdata again");
				ac->has_error = 1;	
				return rv;
			}
			if ((rv = jxr_send_postdata(ac, r)) != APR_SUCCESS)
			{
				compat_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "mod_jaxer: error while sending postdata");
				ac->has_error = 1;	
				return rv;
			}
			postdata_sent = 1;
			break;

        case BLOCKTYPE_HTTP_HEADER:
            got_header = 1;
            break;

		default:
			compat_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "mod_jaxer: invalid request (type=%d) received", msg_type);
			ac->has_error = 1;
            return (!APR_SUCCESS);
			break;
		}
	}

	if ((rv = jxr_process_response_headers(r, buf)) != APR_SUCCESS)
	{
		compat_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, "mod_jaxer: process response header failed");
		ac->has_error = 1;
		return rv;
	}

    /* Check redirect */
	location = ap_table_get(r->headers_out, "Location");
    if (location && location[0])
    {
        should_redirect = 1;
        compat_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r, "mod_jaxer: request (status=%d) will be redirected to %s",
            r->status, location);
    }

	if (should_redirect) {
		/* This redirect needs to be a GET no matter what the original 
		 * method was. 
		 */
		r->method = ap_pstrdup(r->pool, "GET");
		r->method_number = M_GET;

		/* We already read the message body (if any), so don't allow 
		 * the redirected request to think it has one. We can ignore 
		 * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR. 
		 */
		ap_table_unset(r->headers_in, "Content-Length");

        compat_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r, "mod_jaxer: redirecting request to %s",
            location);
		ap_internal_redirect_handler(location, r);
		return APR_SUCCESS;
	}

	/* Now pass to output filter */
	
	if (role == FCGI_RESPONDER && (rv = jxr_process_response_body(ac, r)) != APR_SUCCESS) {
		compat_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, "mod_jaxer: process_response_body failed");
		return HTTP_INTERNAL_SERVER_ERROR;
	}
return HTTP_OK;
}