httpd-modules-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sorin Manolache <sor...@gmail.com>
Subject Re: What is the best way of reading of post request body at hooks module from 2 hooks procedures (+) ?
Date Mon, 18 May 2020 07:32:56 GMT
On 17/05/2020 19.37, CpServiceSPb . wrote:
> May you provide an example what is structure to store to ?
> I tried to make some structure.
> But it would become empty when running came to ap_hook_handler.
> I suppose that I non correctly tried to get data at handler step saved
> value at authn step from such structure.

Hello,

For example:

your_check_authn_callback(request_rec *r) {
    /* read POST body as you explained */
    MyStruct *obj = apr_pcalloc(r->pool, sizeof(MyStruct));
    /* store what you extracted from the POST body in this obj */
    obj->field1 = ...;
    obj->field2 = ...;
    ...
    /* store your obj in the request configuration */
    ap_set_module_config(r->request_config, &your_module, obj);
}

your_handler_callback(request_rec *r) {
    /* retrieve the object initialized and stored in the auth_callback */
    MyStruct *obj = (MyStruct *)ap_get_module_config(r->request_config, 
&your_module);
    /* use obj->field1, obj->field2, etc according to your needs */
}

You don't have to parse everything in authn_callback, you could just 
read the body, and store it unparsed:

your_check_authn_callback(request_rec *r) {
    /* read POST body as you explained */
    char *body = ...;
    ap_set_module_config(r->request_config, &your_module, body);
    /* extract user/pass/etc from body */
}

your_handler_callback(request_rec *r) {
    /* retrieve the body from where you stored it: */
    char *body = (char *)ap_get_module_config(r->request_config, 
&your_module);
    /* extract the other data from your body */
}

So the body (with ap_get_brigade) is read only once, namely in the first 
callback, the check_authn callback. The second callback just retrieves 
either the raw body from where you stored it or a structure that 
contains data that were extracted from the body in the first callback.

HTH,
Sorin

> 
> вс, 17 мая 2020 г. в 10:32, Sorin Manolache <sorinm@gmail.com>:
> 
>> On 15/05/2020 23.39, CpServiceSPb . wrote:
>>> I write hook module for Apache2 consisted of hooks of 2 procedures:
>>> ap_hook_check_authn/ap_hook_check_user_id and ap_hook_handler.
>>> This module reads content of post method request, mainly body, and
>> extract
>>> necessary information for functioning of the each of mentioned above
>>> procedure.
>>> For 1st one - it is user/password passed from clients at post request
>> body,
>>> for 2nd one - it is some data.
>>> That is reading post request body content from brigades/buckets and
>> saving
>>> it into char buffer is called twice - from ap_hook_check_authn and and
>>> ap_hook_handler.
>>> But after callig reading post request body content reading function for
>> the
>>> first time from ap_hook_check_authn, other calling of the function (from
>>> ap_hook_handler) returns an empty body buffer.
>>> Even other calling of post request body content reading function from
>>> ap_hook_check returns already an emptybody biffer.
>>>
>>> I read post body to a buffer at the following way:
>>> 1. apr_brigade_create
>>> 2. do-while cycle until eos_bucket is got set to 1
>>> 3. within the point 2 cycle ap_get_brigade
>>> 4. in for (bucket = APR_BRIGADE_FIRST(bb); bucket !=
>>> APR_BRIGADE_SENTINEL(bb); bucket = APR_BUCKET_NEXT(bucket))
>>> if bucket EOS then eos_bucket set to 1
>>> else (transien bucket) apr_bucket_read is called and then all chunks are
>>> reads to buffer
>>> 5. body buffer and its len is got finally
>>>
>>> But in such pot body reading all buckest are deleted from brigade (not by
>>> me) .
>>
>> Hello,
>>
>> I think you are doing everything fine.
>>
>> You just cannot read the post body several times, because reading it is
>> consuming it.
>>
>> So you just have to read it only once (in ap_hook_check_authn) and store
>> it yourself in a structure that belongs to your module. Then you can
>> reuse that structure in ap_hook_handler.
>>
>> HTH,
>> Sorin
>>
>>>
>>> So what is the best way to read post body content to a buffer as many
>> times
>>> as necessary as at ap_hook_check_authn step as at ap_hook_handler step
>>> without loosing the content ?
>>> Or may be it's more efficiency to read post body content once at
>>> ap_hook_check_authn and then pass it to ap_hook_handler ?
>>> But I don' t understand now how.
>>>
>>> P. S.:
>>> Way of calling of
>>>
>>> r->kept_body = apr_brigade_create(r->pool, r->connection->bucket_alloc);
>>> apr_bucket *bucketnew = apr_bucket_transient_create(bodycontent,
>> bodysize,
>>> r->connection->bucket_alloc);
>>> APR_BRIGADE_INSERT_TAIL(r->kept_body, bucketnew);
>>>
>>> after 1st calling of post request body content reading function worked
>> for
>>> some versions ago (may be even for 2.2 only) .
>>> But it doesn' t work now.
>>>
>>
>>
> 


Mime
View raw message