Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add request (input) header for output handler #40

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions docs/Readme.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ <h3>Benefits</h3>
<li>Uses apache internals</li>
<li>Optimal delivery through sendfile and mmap (if available).</li>
<li>Sets correct cache headers such as Etag and If-Modified-Since as if the file was statically served.</li>
<li>Processes cache headers such as If-None-Match or If-Modified-Since.</li>
<li>Processes cache headers such as If-None-Match or If-Modified-Since.</li>
<li>Support for ranges.</li>
</ul>
</section>
Expand Down Expand Up @@ -111,10 +111,17 @@ <h2>Installation</h2>
<h2>Configuration</h2>

<h3>Headers</h3>

The following headers are added to requests for which this module is enabled:
<ul>
<li><code>X-SENDFILE-IS-ENABLED</code> - the name and version of the module, e.g. <code>mod_xsendfile/1.0</code></li>
</ul>
Output handlers can check for the presence of this header to determine when X-SENDFILE is supported and what bugs/quirks may need addressing. The module will process these response headers from the output handler if present:
<ul>
<li><code>X-SENDFILE</code> - Send the file referenced by this headers instead of the current response body</li>
<li><code>X-SENDFILE</code> - Discard the current response body and send the file referenced by this header</li>
<li><code>X-SENDFILE-TEMPORARY</code> - Like <code>X-SENDFILE</code>, but the file will be deleted afterwards. The file must originate from a path that has the <code>AllowFileDelete</code> flag set.</li>
</ul>
If both headers are present in the response <code>X-SENDFILE</code> takes precedence over <code>X-SENDFILE-TEMPORARY</code>.

<h3>XSendFile</h3>

Expand Down Expand Up @@ -267,7 +274,7 @@ <h4>Remarks - Relative paths</h4>
<ol>
<li><code>/var/www/../pool2/file = /var/pool2/file</code> - Not within bounds of <code>/var/www</code></li>
<li><code>/tmp/pool/../pool2/file = /tmp/pool2/file</code> - Not within bounds of <code>/tmp/pool</code></li>
<li><code>/tmp/pool2/../pool2/file = /tmp/pool2/file</code> - Within bounds of <code>/tmp/pool2</code>, OK</li>
<li><code>/tmp/pool2/../pool2/file = /tmp/pool2/file</code> - Within bounds of <code>/tmp/pool2</code>, OK</li>
</ol>
<p>You still can only access paths that are whitelisted. However you have might expect a different behavior here, hence the documentation.</p>
<p class="remark"><strong>Please note:</strong> It is recommended to always use absolute paths.</p>
Expand Down
38 changes: 31 additions & 7 deletions mod_xsendfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Nils Maier <[email protected]>
* Ben Timby - URL decoding
* Jake Rhee - X-SENDFILE-TEMPORARY
* Jeff Frey - X-SENDFILE-IS-ENABLED request header addition
****/

/****
Expand All @@ -34,6 +35,7 @@
****/

/* Version: 1.0 */
#define AP_XSENDFILE_VERSION "1.0"

#include "apr.h"
#include "apr_lib.h"
Expand All @@ -59,6 +61,11 @@
#define AP_XSENDFILE_HEADER "X-SENDFILE"
#define AP_XSENDFILETEMPORARY_HEADER "X-SENDFILE-TEMPORARY"

#define AP_XSENDFILE_IS_ENABLED_HEADER "X-SENDFILE-IS-ENABLED"
#ifndef AP_XSENDFILE_IS_ENABLED_HEADER_VALUE
#define AP_XSENDFILE_IS_ENABLED_HEADER_VALUE "mod_xsendfile/" AP_XSENDFILE_VERSION
#endif

module AP_MODULE_DECLARE_DATA xsendfile_module;

typedef enum {
Expand Down Expand Up @@ -292,8 +299,23 @@ static apr_status_t ap_xsendfile_get_filepath(request_rec *r,
return rv;
}

static xsendfile_conf_active_t ap_xsendfile_enabled_for_request(request_rec *r) {
xsendfile_conf_active_t enabled = ((xsendfile_conf_t *)ap_get_module_config(r->per_dir_config, &xsendfile_module))->enabled;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is duplicated code, add some function like ap_xsendfile_enabled_for(request_rec*) and use it in all places.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented ap_xsendfile_enabled_for_request()

if (XSENDFILE_UNSET == enabled) {
enabled = ((xsendfile_conf_t*)ap_get_module_config(r->server->module_config, &xsendfile_module))->enabled;
}
return enabled;
}

static int ap_xsendfile_add_request_header(request_rec *r) {
if (XSENDFILE_ENABLED == ap_xsendfile_enabled_for_request(r)) {
apr_table_setn(r->headers_in, AP_XSENDFILE_IS_ENABLED_HEADER, AP_XSENDFILE_IS_ENABLED_HEADER_VALUE);
}
return OK;
}

static apr_status_t ap_xsendfile_output_filter(ap_filter_t *f, apr_bucket_brigade *in) {
request_rec *r = f->r, *sr = NULL;
request_rec *r = f->r;

xsendfile_conf_t
*dconf = ap_get_module_config(r->per_dir_config, &xsendfile_module),
Expand Down Expand Up @@ -631,12 +653,7 @@ static apr_status_t ap_xsendfile_output_filter(ap_filter_t *f, apr_bucket_brigad
}

static void ap_xsendfile_insert_output_filter(request_rec *r) {
xsendfile_conf_active_t enabled = ((xsendfile_conf_t *)ap_get_module_config(r->per_dir_config, &xsendfile_module))->enabled;
if (XSENDFILE_UNSET == enabled) {
enabled = ((xsendfile_conf_t*)ap_get_module_config(r->server->module_config, &xsendfile_module))->enabled;
}

if (XSENDFILE_ENABLED != enabled) {
if (XSENDFILE_ENABLED != ap_xsendfile_enabled_for_request(r)) {
return;
}

Expand Down Expand Up @@ -693,6 +710,13 @@ static void xsendfile_register_hooks(apr_pool_t *p) {
AP_FTYPE_CONTENT_SET
);

ap_hook_fixups(
ap_xsendfile_add_request_header,
NULL,
NULL,
APR_HOOK_LAST
);

ap_hook_insert_filter(
ap_xsendfile_insert_output_filter,
NULL,
Expand Down