1

There is a client which is implemented in Nodejs. I wand to send post request to Apache HTTP server and then that request is to be forwarded to back end server(It locates at somewhere else). So I implemented Node js client and Apache module. Request and response between those two are ok. Now my problem is how to forward this request from Apache HTTP server to back end server.

Nodejs client.js

var request = require('request');
var userDetails={
    username:"username",
    password:"abc123"

}
var optsSingup = {
    url: "http://localhost:80/a",
    method: "POST",
    headers: {
        "Content-Type": "application/x-www-form-urlencoded"
    },
    form:{

        username:userDetails.username,
        password:userDetails.password,

    }

};

request(optsSingup, function (err, res, body) {
    console.log('error', err);
    //console.log('status', res.statusCode);
    console.log('body', body);
});

Apache module (I've been following this tutorial http://httpd.apache.org/docs/2.4/developer/modguide.html example mod_example_config.c). Here I will share handler part of my module. Others parts are same as mod_example_config.c.

 typedef struct {
    const char* key;
    const char* value;
 } keyValuePair;

 static int example_handler(request_rec *r)
{

        formData = readPost(r);
        if (formData) {
        int i;
        for (i = 0; &formData[i]; i++) {
                if (formData[i].key && formData[i].value) {
                        ap_rprintf(r, "%s = %s\n", formData[i].key, formData[i].value);
                 } else if (formData[i].key) {
                        ap_rprintf(r, "%s\n", formData[i].key);
                } else if (formData[i].value) {
                    ap_rprintf(r, "= %s\n", formData[i].value);
                 } else {
                    break;
                }
        }
        }
        return OK;
        }


    keyValuePair* readPost(request_rec* r) {
    apr_array_header_t *pairs = NULL;
    apr_off_t len;
    apr_size_t size;
    int res;
    int i = 0;
    char *buffer;
    keyValuePair* kvp;

    res = ap_parse_form_data(r, NULL, &pairs, -1, HUGE_STRING_LEN);
    if (res != OK || !pairs) return NULL; 
    kvp = apr_pcalloc(r->pool, sizeof(keyValuePair) * (pairs->nelts + 1));
    while (pairs && !apr_is_empty_array(pairs)) {
        ap_form_pair_t *pair = (ap_form_pair_t *) apr_array_pop(pairs);
        apr_brigade_length(pair->value, 1, &len);
        size = (apr_size_t) len;
        buffer = apr_palloc(r->pool, size + 1);
        apr_brigade_flatten(pair->value, buffer, &size);
        buffer[len] = 0;
        kvp[i].key = apr_pstrdup(r->pool, pair->name);
        kvp[i].value = buffer;
        i++;
    }
    return kvp;
    }
GPrathap
  • 7,336
  • 7
  • 65
  • 83
  • You should use mod_proxy module and set it up as a reverse proxy (gateway). Mod proxy will easily forward requests to your backend server. There is no need to reinvent the wheel on this one. http://httpd.apache.org/docs/current/mod/mod_proxy.html – Boris Jan 20 '15 at 02:39

1 Answers1

0

Actually this is not the proper way to do this. Please see the answer in this question How to set a Set Handler for proxy request

Community
  • 1
  • 1
GPrathap
  • 7,336
  • 7
  • 65
  • 83