module AP_MODULE_DECLARE_DATA readbody_module = {
STANDARD20_MODULE_STUFF,
NULL, /*Per-directory configuration handler */
NULL, /*Merge handler for per-directory configurations */
NULL, /*Per-server configuration handler */
NULL, /*Merge handler for per-server configurations */
NULL,
regiter_hooks /*Our hook registering function */
};
static void register_hooks(apr_pool_t *pool)
{
ap_hook_handler(readbody_handler, NULL, NULL, -10);
}
static int readbody_handler(request_rec *r)
{
const char *buffer;
if (!r->handler || strcmp(r->handler, "readbody-handler")) return (DECLINED);
if (util_read(r, &buffer) == OK) //reading the body and assigning it into buffer
{
char s[2] = ":";
char s2[2] = "\"";
char *indexname;
indexname = strtok(buffer, s);
indexname = strtok(NULL, s);
indexname = strtok(indexname, s2);
indexname = strtok(NULL, s2);
apr_table_setn(r->headers_out, "IndexPattern", indexname); //setting up response header
}
return OK;
}
static int util_read(request_rec *r, const char **rbuf)
{
int rc;
if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK)
{
return rc;
}
if (ap_should_client_block(r))
{
char argsbuffer[HUGE_STRING_LEN];
int rsize, len_read, rpos = 0;
long length = r->remaining;
*rbuf = apr_pcalloc(r->pool, length + 1);
while ((len_read = ap_get_client_block(r, argsbuffer, sizeof(argsbuffer))) > 0)
{
if ((rpos + len_read) > length)
{
rsize = length - rpos;
}
else
{
rsize = len_read;
}
memcpy((char*) *rbuf + rpos, argsbuffer, rsize);
rpos += rsize;
}
}
}
return rc;