うに使用すればいいの脱出の代わりにencodeURI/encodeURIComponent?
-
09-06-2019 - |
質問
を符号化するときにクエリ文字列を送信するwebサーバが使用しま escape()
やきを使用していま encodeURI()
または encodeURIComponent()
:
利用脱出:
escape("% +&=");
または
利用encodeURI()/encodeURIComponent()
encodeURI("http://www.google.com?var1=value1&var2=value2");
encodeURIComponent("var1=value1&var2=value2");
解決
エスケープ()
で使用しないで下さい!escape()
定義第 B.2.1.2逃亡 の 導入テキストの別添B "と言ってい
...すべての言語の特徴や行動の指定はこの附属書がありま望ましくない特性をレガシー利用いられるこの仕様となります。...
...プログラマが利用するべきではありませんまたはの存在を仮定しこれらの特徴や行動を書く際に新しいECMAScriptコード....
行動:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape
特殊文字を符号化の例外:@*_+-./
に進数形式の文字は、コード単位の値0xFF以下は二桁のエスケープシーケンス: %xx
.
キャラクターができるようなコード単位の桁のフォーマット %uxxxx
を使用します。ことが許可されない内にクエリ文字列として定義される RFC3986):
query = *( pchar / "/" / "?" )
pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
pct-encoded = "%" HEXDIG HEXDIG
sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
/ "*" / "+" / "," / ";" / "="
割符号は、禁止されている場合がある場合は直接の一つhexdigits、年後 u
はできません。
encodeURI()
利用encodeURIしている場合、作業。この電話:
encodeURI("http://www.example.org/a file with spaces.html")
を取得す:
http://www.example.org/a%20file%20with%20spaces.html
なコencodeURIComponentうかを破壊するURLを返し
http%3A%2F%2Fwww.example.org%2Fa%20file%20with%20spaces.html
encodeURIComponent()
利用encodeURIComponentしたいときにエンコードの値をURLパラメータとします。
var p1 = encodeURIComponent("http://example.org/?a=12&b=55")
その場合のURLは必要なもの:
var url = "http://example.net/?param1=" + p1 + "¶m2=99";
ますこの完全なURL:
http://example.net/?param1=http%3A%2F%2Fexample.org%2F%Ffa%3D12%26b%3D55¶m2=99
ご注意encodeURIComponent逃がしませんの '
文字です。共通のバグが使用を作成するhtml属性など href='MyUrl'
, かが注入す。場の構築をhtml文字列は使用 "
の代わりに '
属性引用符、またはキャリーエンコーディング('
できとして符号化され%27のとおりです。
詳しい情報はこのタイプのエンコードで確認できます: http://en.wikipedia.org/wiki/Percent-encoding
他のヒント
の違い encodeURI()
や encodeURIComponent()
われている11の文字符号化されたencodeURIComponentがencodeURI:
私は生成されこのテーブルで簡単に ソフトである。テーブル Google Chromeこのコード:
var arr = [];
for(var i=0;i<256;i++) {
var char=String.fromCharCode(i);
if(encodeURI(char)!==encodeURIComponent(char)) {
arr.push({
character:char,
encodeURI:encodeURI(char),
encodeURIComponent:encodeURIComponent(char)
});
}
}
console.table(arr);
この記事を啓発す:Javascriptの狂気:クエリ文字列を構文解析
したがっundersandなぜdecodeURIComponentませんでした復号'+'を修正。こちらの抽出エキス:
String: "A + B"
Expected Query String Encoding: "A+%2B+B"
escape("A + B") = "A%20+%20B" Wrong!
encodeURI("A + B") = "A%20+%20B" Wrong!
encodeURIComponent("A + B") = "A%20%2B%20B" Acceptable, but strange
Encoded String: "A+%2B+B"
Expected Decoding: "A + B"
unescape("A+%2B+B") = "A+++B" Wrong!
decodeURI("A+%2B+B") = "A+++B" Wrong!
decodeURIComponent("A+%2B+B") = "A+++B" Wrong!
encodeURIComponentなエンコード -_.!~*'()
, が問題を掲載する場合には、データのphpのxml文字列です。
例えば:
<xml><text x="100" y="150" value="It's a value with single quote" />
</xml>
一般の脱出 encodeURI
%3Cxml%3E%3Ctext%20x=%22100%22%20y=%22150%22%20value=%22It's%20a%20value%20with%20single%20quote%22%20/%3E%20%3C/xml%3E
ご覧の通り、単一引用符はないエンコードされます。を解決する問題作成した二つの機能を解決の問題に私のプロジェクト、エンコードURL:
function encodeData(s:String):String{
return encodeURIComponent(s).replace(/\-/g, "%2D").replace(/\_/g, "%5F").replace(/\./g, "%2E").replace(/\!/g, "%21").replace(/\~/g, "%7E").replace(/\*/g, "%2A").replace(/\'/g, "%27").replace(/\(/g, "%28").replace(/\)/g, "%29");
}
のための復号URL:
function decodeData(s:String):String{
try{
return decodeURIComponent(s.replace(/\%2D/g, "-").replace(/\%5F/g, "_").replace(/\%2E/g, ".").replace(/\%21/g, "!").replace(/\%7E/g, "~").replace(/\%2A/g, "*").replace(/\%27/g, "'").replace(/\%28/g, "(").replace(/\%29/g, ")"));
}catch (e:Error) {
}
return "";
}
encodeURI()の逃避()を行うための機能でjavascript逃げないHTTP.
小比較表はJavaに対すJavaScript前クリアしました。
1. Java URLEncoder.encode (using UTF8 charset)
2. JavaScript encodeURIComponent
3. JavaScript escape
4. PHP urlencode
5. PHP rawurlencode
char JAVA JavaScript --PHP---
[ ] + %20 %20 + %20
[!] %21 ! %21 %21 %21
[*] * * * %2A %2A
['] %27 ' %27 %27 %27
[(] %28 ( %28 %28 %28
[)] %29 ) %29 %29 %29
[;] %3B %3B %3B %3B %3B
[:] %3A %3A %3A %3A %3A
[@] %40 %40 @ %40 %40
[&] %26 %26 %26 %26 %26
[=] %3D %3D %3D %3D %3D
[+] %2B %2B + %2B %2B
[$] %24 %24 %24 %24 %24
[,] %2C %2C %2C %2C %2C
[/] %2F %2F / %2F %2F
[?] %3F %3F %3F %3F %3F
[#] %23 %23 %23 %23 %23
[[] %5B %5B %5B %5B %5B
[]] %5D %5D %5D %5D %5D
----------------------------------------
[~] %7E ~ %7E %7E ~
[-] - - - - -
[_] _ _ _ _ _
[%] %25 %25 %25 %25 %25
[\] %5C %5C %5C %5C %5C
----------------------------------------
char -JAVA- --JavaScript-- -----PHP------
[ä] %C3%A4 %C3%A4 %E4 %C3%A4 %C3%A4
[ф] %D1%84 %D1%84 %u0444 %D1%84 %D1%84
をお勧めしませんのものをご使用の方法。書き独自の機能をなす。
MDNは良い例でurlエンコーディングを以下に示します。
var fileName = 'my file(2).txt';
var header = "Content-Disposition: attachment; filename*=UTF-8''" + encodeRFC5987ValueChars(fileName);
console.log(header);
// logs "Content-Disposition: attachment; filename*=UTF-8''my%20file%282%29.txt"
function encodeRFC5987ValueChars (str) {
return encodeURIComponent(str).
// Note that although RFC3986 reserves "!", RFC5987 does not,
// so we do not need to escape it
replace(/['()]/g, escape). // i.e., %27 %28 %29
replace(/\*/g, '%2A').
// The following are not required for percent-encoding per RFC5987,
// so we can allow for a little better readability over the wire: |`^
replace(/%(?:7C|60|5E)/g, unescape);
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
ることも忘れてはいけなすべてのエンコードの文字を選択し、必要な適切に表すencodeURI()符号化の少ない文字以上のencodeURIComponent()を符号化の少ない(とはまた違う、dannypのポイント)文字以上の逃避().
目的のエンコードjavascriptを指三inbuilt機能-
エスケープ()-なエンコード
@*/+
このメソッドは推奨されて後にそれぞれの端末ウィンドウ間でや3でなければならないと考えています。encodeURI()-なエンコード
~!@#$&*()=:/,;?+'
では、そのURIは、完全なURIでないエンコードの予約文字を特殊な意味を持つURIです。このメソッドを利用する場合の趣旨に変換するという事になりますのURLの代わりに特別セグメントのURL。例-encodeURI('http://stackoverflow.com');
渡- http://stackoverflow.comencodeURIComponent()-なエンコード
- _ . ! ~ * ' ( )
この関数を符号化ユニフォームリソース識別子(URIのコンポーネントはそれぞれ差し替えによりインスタンスの特定の文字をより一、二、三、四れらのエスケープシーケンスを表すときは、UTF-8エンコーディングの文字です。このメソッドを使用してください換え部品のURL。たとえばユーザが入力する必要追加 例-encodeURI('http://stackoverflow.com');
渡-http%3A%2F%2Fstackoverflow.com
すべてのこのエンコーディングはUTF8ん。インフォメーションセンタ文字に変換されるUTF-8形式です。
encodeURIComponentからencodeURIでエンコードの予約文字や数符号#のencodeURI
また実験の様々な方法が良いアメニティチェックした後でもどを取り扱う様々な用途や機能です。
その目的に向かって このサイト にとって非常に便利で確認をした疑いであろう適切にでも実績のある有のため復号するencodeURIComponent投稿内容の投稿者に文字列できるレスト解釈する.大ブックマークについて
作 Johannのテーブル, 私の延長を決定します。見たかったのをASCII文字を取得しエンコードされます。
var ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
var encoded = [];
ascii.split("").forEach(function (char) {
var obj = { char };
if (char != encodeURI(char))
obj.encodeURI = encodeURI(char);
if (char != encodeURIComponent(char))
obj.encodeURIComponent = encodeURIComponent(char);
if (obj.encodeURI || obj.encodeURIComponent)
encoded.push(obj);
});
console.table(encoded);
表されることに注意してください符号化された文字です。空の細胞のように符号化された文字は同一です。
される方もいるでしょうが、私は追加の別のテーブル urlencode()
vs rawurlencode()
.唯一の違いるようにエンコードのスペース文字です。
<script>
<?php
$ascii = str_split(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", 1);
$encoded = [];
foreach ($ascii as $char) {
$obj = ["char" => $char];
if ($char != urlencode($char))
$obj["urlencode"] = urlencode($char);
if ($char != rawurlencode($char))
$obj["rawurlencode"] = rawurlencode($char);
if (isset($obj["rawurlencode"]) || isset($obj["rawurlencode"]))
$encoded[] = $obj;
}
echo "var encoded = " . json_encode($encoded) . ";";
?>
console.table(encoded);
</script>
私はこの機能---
var escapeURIparam = function(url) {
if (encodeURIComponent) url = encodeURIComponent(url);
else if (encodeURI) url = encodeURI(url);
else url = escape(url);
url = url.replace(/\+/g, '%2B'); // Force the replacement of "+"
return url;
};
の受け答えが良いです。への最後の部:
ご注意encodeURIComponent逃がしませんの文字です。共通 バグでの使用を作成するhtmlなどの属性href='MyUrl、 きな注射す。場の構築からhtml 文字列は使用"の代わりに'属性お見積りに追加 キャリーエンコーディングすることができとして符号化され%27のとおりです。
されたい方に %エンコード自由文字 必要として符号化されます。
この方法をご利用ください逃した(ソース Mozilla)
function fixedEncodeURIComponent(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
}
// fixedEncodeURIComponent("'") --> "%27"
現代の書き換えの@johann-echavarriaの回答:
console.log(
Array(256)
.fill()
.map((ignore, i) => String.fromCharCode(i))
.filter(
(char) =>
encodeURI(char) !== encodeURIComponent(char)
? {
character: char,
encodeURI: encodeURI(char),
encodeURIComponent: encodeURIComponent(char)
}
: false
)
)
や使いいただけるのであれば、テーブルの交換 console.log
と console.table
(見出力)です。