gbalduzzi
gbalduzzi

Reputation: 10166

Set custom header to apache response within a module

I'm creating an apache module that encrypt data with AES. My final goal is to use a different AES key for every request, generating a 16 byte new key used to AES encrypt the file and send the key (encrypted with RSA) as a custom header

The problem is that i can't find any documentation to programmatically set a custom header.

I'm expecting something like ap_set_handler("HeaderName","content")

i only found this file that use such a function: http://opensource.apple.com/source/apache/apache-643/apache/src/modules/proxy/proxy_ftp.c

The problem is that including it in my source code give me a implicit declaration of function 'ap_set_header' error even though i included the same .h files of that file.

I'm pretty sure it can be done but i really don't know where to search

Upvotes: 1

Views: 1816

Answers (1)

gbalduzzi
gbalduzzi

Reputation: 10166

After 2 days of struggling i found how to do it looking at the source code of mod_headers (otherwise it is almost impossible to find in the documentation without already knowing it)

Actually,the request_rec *r instance Apache gives you in the handler has a very useful r->headers_outfield.

you can find the 'documentation' here: https://ci.apache.org/projects/httpd/trunk/doxygen/structrequest__rec.html#afecc56ea8fb015aa52477dba471a6612

r->headers_out is an apr_table_t so you can modify it using proper functions:

/* Add header at the end of table */
ap_table_mergen(r->headers_out, "NameField", "value");
/* Overwrite value of "NameField" header or add it (if not existing) */
ap_table_setn(r->headers_out, "NameField", "value");
/* Unset header */
ap_table_unset(r->headers_out, "NameField");

Upvotes: 7

Related Questions