Skip to content

Commit fc6064e

Browse files
author
Tai Lee
committed
Add ability to set target URL with X-Proxy-URL header.
1 parent 5763c55 commit fc6064e

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,18 @@ $('#target').load(
2020
);
2121
```
2222

23-
It’s worth mentioning that both POST and GET methods work and headers are taken into consideration. That is to say, headers sent from browser to proxy are used in the cross domain request and vice versa.
23+
It’s worth mentioning that both POST and GET methods work and headers are taken into consideration. That is to say, headers sent from browser to proxy are used in the cross domain request and vice versa.
24+
25+
You can also specify the URL with the `X-Proxy-URL` header, which might be easier to set with your JavaScript library. For example, if you wanted to automatically use the proxy for external URL targets, for GET and POST requests:
26+
27+
``` JAVASCRIPT
28+
$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
29+
if (options.url.match(/^https?:/)) {
30+
options.headers['X-Proxy-URL'] = options.url;
31+
options.url = '/proxy.php';
32+
}
33+
});
34+
```
2435

2536
Configuration
2637
--------------

proxy.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
if ( substr( $key, 0, 5 ) == 'HTTP_' ) {
4141
$headername = str_replace( '_', ' ', substr( $key, 5 ) );
4242
$headername = str_replace( ' ', '-', ucwords( strtolower( $headername ) ) );
43-
if ( 'Host' != $headername ) {
43+
if ( !in_array( $headername, array( 'Host', 'X-Proxy-Url' ) ) ) {
4444
$request_headers[] = "$headername: $value";
4545
}
4646
}
@@ -49,7 +49,8 @@
4949
// identify request method, url and params
5050
$request_method = $_SERVER['REQUEST_METHOD'];
5151
$request_params = ( $request_method == 'GET' ) ? $_GET : $_POST;
52-
$request_url = urldecode( $_REQUEST['csurl'] );
52+
// Get URL from `csurl` in GET or POST data, before falling back to X-Proxy-URL header.
53+
$request_url = urldecode( isset( $_REQUEST['csurl'] ) ? $_REQUEST['csurl'] : $_SERVER['HTTP_X_PROXY_URL'] );
5354
$p_request_url = parse_url( $request_url );
5455
unset( $request_params['csurl'] );
5556

0 commit comments

Comments
 (0)