I would like to apologize for posting a message related to
modules in the core apache development group, but I was
unable to post a message in the Apache Modules development
list.
My problem is that I am developing a very simple output
filter that does nothing more than offering a simple
interface to insert any custom text in the response body
(Actually I just modified the code from Nick Kew's book ).
I think, I have done something wrong with the buckets and
brigades, however I am a novice in this field
( an undergrad student), so thought that the experts could
help a bit.
Following is the code from the module: the main callback
function of the filter doesn't do much interesting work,
and everything in it was working properly,following
function is the latest code that i wrote and I believe
it has triggered this problem.
/** insert_text function inserts the specified text in
* the bucket brigade at the specified place (text and
* place are string parameters).
*params:
*
* @place: the string before which we need to insert text
* @text: the string that needs to be inserted.
*/
static void insert_text(request_rec *r, apr_bucket_brigade *bb,
const char *place, const char *text)
{
apr_bucket *b;
const char *buf;
const char *tag;
int sz, flag = 0;
size_t offset = 0;
/* loop through the bucket brigade and read each
* bucekt to scan for the
*string place
*/
for (b = APR_BRIGADE_FIRST(bb) ;
b != APR_BRIGADE_SENTINEL(bb) ;
b = APR_BUCKET_NEXT(b) ) {
apr_bucket_read(b, &buf, &sz, APR_BLOCK_READ);
if (buf == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"mod_hoverin: buf = NULL");
return;
}
/* check if the buffer contains place*/
tag = ap_strcasestr(buf, place);
if (tag != NULL) {
/*calculate the position (offset) where to split the
bucket
*/
offset = ((unsigned int) tag - (unsigned int) buf
)/sizeof(char) ;
/* split the bucket at the calculated offset.
*/
apr_bucket_split(b, offset);
b = APR_BUCKET_NEXT(b);
APR_BUCKET_INSERT_BEFORE(b,
apr_bucket_transient_create(
(const
char *)text, strlen(text),
r->connection->bucket_alloc ));
/* Increment the flag, so that we may know that
place was found
* and we can break out from the loop
*/
flag++;
}
}
if (flag) {
return;
}
}
}