You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modules-dev@httpd.apache.org by Jens Frey <je...@coffeecrew.org> on 2009/10/15 10:14:40 UTC

Run Subrequest

Hi all,

i am trying to run a subrequest to a URI which get's proxied to check the return code of the 
machine the proxy points to (e.g. 200, 500). The problem now is that the proxy module answers the request,
but the content i want to send is omitted, so the question is, how can i avoid that mod_proxy 
responds and instead my response is written, any ideas?

E.g. setup

httpd.conf
ProxyPass /checkHere http://localhost:8080/checkThere

---

code:
if (ap_is_initial_req(r)) {
        
        requestURI = apr_pstrcat(r->pool, REQUEST_URI_BASE, "?code=200", NULL);
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 
                      0, r, 
                      "Have built URI %s", requestURI);
        rr = ap_sub_req_method_uri("GET", requestURI, r, NULL);
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 
                      sub_req_return_code, r, 
                      "Looked up URI");

        sub_req_return_code = ap_run_sub_req(rr);
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 
                      0, r, 
                      "Did run subrequest");

        tmp = apr_psprintf(r->pool, "%d", sub_req_return_code);
        response_body = apr_pstrcat(r->pool, response_body, "Did run call to ", requestURI, " have result code ", tmp, NULL);

        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 
                      sub_req_return_code, rr, 
                      "Subrequesting URI finished with code: %d", sub_req_return_code);
        
    } else {
        return DECLINED;
    }

    if (!r->header_only) {
        ap_rputs(response_start, r);
        ap_rputs(response_body, r);
        ap_rputs(response_end, r);
    }
    return OK;

Thanks,
Jens

Re: Run Subrequest

Posted by Joe Lewis <jo...@joe-lewis.com>.
Jens Frey wrote:
> Hi all,
>
> i am trying to run a subrequest to a URI which get's proxied to check the return code of the 
> machine the proxy points to (e.g. 200, 500). The problem now is that the proxy module answers the request,
> but the content i want to send is omitted, so the question is, how can i avoid that mod_proxy 
> responds and instead my response is written, any ideas?
>   

I have done this before.  The way I accomplished this was to place a 
filter into the subrequest that copies the data into a structure you 
have for it, and doesn't pass the brigade on to the regular filters.  e.g. :

static apr_status_t include_output_filter(ap_filter_t *f, 
apr_bucket_brigade *bb) {
[loop through the buckets to get what you want, calling 
APR_BUCKET_REMOVE() after each]
}

Then, in your include function :

  ctx->include_filter = apr_palloc(f->r->pool,sizeof(ap_filter_t));
  ctx->include_filter->ctx = current_filter;
  ctx->include_filter->next = NULL;
  (ctx)->include_filter->r = f->r;
  (ctx)->include_filter->c = f->r->connection;
  (ctx)->include_filter->frec = 
apr_palloc(f->r->pool,sizeof(ap_filter_rec_t));
  memset((ctx)->include_filter->frec,0,sizeof(ap_filter_rec_t));
  (ctx)->include_filter->frec->name = "MYMOD-INCLUDE-WRAPPER";
  (ctx)->include_filter->frec->filter_func.out_func = 
&include_output_filter;
  (ctx)->include_filter->frec->next = NULL;
  (ctx)->include_filter->frec->ftype = AP_FTYPE_RESOURCE;

  (ctx)->include_r = ap_sub_req_lookup_uri(uri,f->r,(ctx)->include_filter);

This should give you what the subrequest returned, and since you use 
your own output filter that doesn't pass anything on, it won't send 
anything to the client.

Joe
-- 
Joe Lewis
Chief Nerd 	SILVERHAWK <http://www.silverhawk.net/> 	

------------------------------------------------------------------------
/Some say the glass is half empty, some say the glass is half full, I 
say, are you going to drink that?
    --Lisa Claymen/