2

So upset about this. I am trying to do a very simple thing here.

I try to insert a string at the head of a html file in an Apache module, the code is simple.

apr_bucket* txt_esc(apr_bucket_alloc_t* alloc ) 
{
    return apr_bucket_transient_create("ggggggggggggggg", 15, alloc) ;
}

apr_status_t add_string(ap_filter_t *f, apr_bucket_brigade *pbbIn)
{    
    APR_BRIGADE_INSERT_HEAD(pbbIn, txt_esc(f->r->connection->bucket_alloc));           
    return ap_pass_brigade(f->next, pbbIn);        
}

The problem is that I can insert the string at the head, but meantime, the content of the same size of the string at the end of the html file will be truncated.

Anybody knows why this happens? This Apache server is running as a proxy.

billtian
  • 6,589
  • 4
  • 18
  • 28
  • Probably because size of the document must match. I won't post it as an answer, because I'm not sure, but I would look in API documentation to find/modify document size before you add string at its beginning or end. Keep in mind, that html documents server returns to webclients are on application layer of OSI model. Can't remember exactly, but isn't there some Size: header in http response headers? If there is, I'd bet apache sends exactly the same amount of data it declares. This would confuse browsers, if there would be possible to send other amount of data than declared.. – Piotr Wadas Mar 05 '13 at 21:24
  • So you need to find the way to increase header-declared size to match original document plus size of string you add. Start with Content-Length here: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Responses – Piotr Wadas Mar 05 '13 at 21:25
  • Ok, thanks. I will try. BTW, I forgot to mention one point is that this Apache server is running as a proxy. – billtian Mar 05 '13 at 21:47
  • The best way to develop apache modules is to find module which does more-less similar thing, and look into source to see how it's done in similar, and then customize it to suit your specific needs. Depending on module, it's usually worth to try to implement it with mod_perl first, however it is always much slower than C. – Piotr Wadas Mar 06 '13 at 08:58
  • Hi Piotr, In fact what I am trying to do in the module is that everything time apache receives a request, it tries to inject a javascript into the html file and the client will run it at the browser side. The script will collect local information and then return to the server by calling a cgi. But I found that hijacking the html file is not a good idea because sometimes the website doesn't show correctly, sometimes it even refuses to work, maybe they have checksum or something. Do you have a better idea to do this job in the apache module instead of hijacking the html file? – billtian Mar 06 '13 at 20:11
  • This does not sound well. To do this, you'd need java applet or ActiveX control or dedicated xml-rpc server/client solution - depending on "local information" you want to gather. Javascript functionality is by design limited to what web browser can access locally, what further is strictly enclosed to very narrow range of things. Moreover, Apache's and www purpose is basically serving content through challenge-response protocol from the server ( possibly dynamic based on user-provided request/input), not to interfere with webclient local systems, so it's not its area of action. – Piotr Wadas Mar 07 '13 at 09:00
  • Theoretically one can turn apache into xmlrpc-over-http server, there are modules for that, but in most cases is overcombined solution. Think rather about application server like tomcat or jboss, and dedicated client app ( applet or something ) served by it. – Piotr Wadas Mar 07 '13 at 09:02

0 Answers0