単純なプロキシ実装例
提供: svg2wiki
				
								
				(版間での差分)
				
																
				
				
								
				|  (→svgMap.jsの初期化) |  (→svgMap.jsの初期化) | ||
| 28行: | 28行: | ||
| * corsProxy.jsは[[クロスオリジンアクセス#corsProxy.js]]を参照 | * corsProxy.jsは[[クロスオリジンアクセス#corsProxy.js]]を参照 | ||
| * <code>corsProxy.setService</code>の、requireEncoding フラグはtrueにします。 | * <code>corsProxy.setService</code>の、requireEncoding フラグはtrueにします。 | ||
| + | ** 本php実装ではfileクエリに記載される取得先URLがURLエンコードされていることを想定しているため | ||
| == ソースコード == | == ソースコード == | ||
2024年11月5日 (火) 07:22時点における版
この例は、かなり単純なCORSプロキシの実装例です。よりセキュリティに配慮した実装が必要なケースがありますので、クロスオリジンアクセスの他の実装を参照の上利用を検討してください。
svgMap.jsの初期化
<!doctype html>
<html>
...
<script type="module">
import { CorsProxy } from 'https://cdn.jsdelivr.net/gh/svgmap/svgmapjs@latest/CorsProxyModule.js';
import { svgMap } from 'https://cdn.jsdelivr.net/gh/svgmap/svgmapjs@latest/SVGMapLv0.1_r18module.js';
window.svgMap=svgMap
var corsProxy = new CorsProxy();
// プロキシの設定
var proxyPath = "https://..url..of../simpleCORSproxy.php?file=";
corsProxy.setService(proxyPath, null, true, true); // 第4パラメータをtrueにするとアクセス先URLをエンコードする
window.corsProxy = corsProxy;
svgMap.setProxyURLFactory(null,null,null, corsProxy.getURLfunction(), true); // ビットイメージ非線形図法変換の時のみプロキシを使う
</script>
...
</html>
- corsProxy.jsはクロスオリジンアクセス#corsProxy.jsを参照
-  corsProxy.setServiceの、requireEncoding フラグはtrueにします。- 本php実装ではfileクエリに記載される取得先URLがURLエンコードされていることを想定しているため
 
ソースコード
- simpleCORSproxy.php
-  if (preg_match("|^https?://svgmap\.org|", $referer) ..の行を適宜書き換えてください
<?php
function file_get_contents_curl($url) {
	$headers = array(
	    "HTTP/1.0",
	    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
//	    "Accept-Encoding:gzip ,deflate",
	    "Accept-Language:ja,en-us;q=0.7,en;q=0.3",
	    "Connection:keep-alive",
	    "User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:26.0) Gecko/20100101 Firefox/26.0"
	    );
    // **/
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);       
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);       
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
if($_GET["file"]){
	
	$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
	// accept only referrer within this site
	header("Access-Control-Allow-Origin: " . "*"); // MUST SET for CORS
	if (preg_match("|^https?://svgmap\.org|", $referer) || preg_match("|^https?://www\.svgmap\.org|", $referer)) {
		if ( $_GET["type"]){
			header("Content-type: " . $_GET["type"]);
		} else {
			if(strpos($_GET["file"],'png')){
				header("Content-type: image/png");
			} else {
				header("Content-Type:image/jpeg;");
			}
		}
		echo file_get_contents_curl( urldecode($_GET["file"]), true);
	} else {
		echo "ERR : referer : " .  $referer;
	}
} else {
	foreach (getallheaders() as $name => $value) {
		echo "$name: $value<br>";
	}
	foreach ($_GET as $key => $value) {
	echo "GET Key:".$key.",  Value:".$value."<br>";
	}
	echo "referrer: ".$_SERVER['HTTP_REFERER'];
}
?>
