Commit 387f8320 authored by Erik Luo's avatar Erik Luo Committed by Commit Bot

DevTools: fix encoding for Copy as cURL

Before, the 'Copy as cURL' option would escape all characters outside
of a limited range [\x20-\x7e] with ANSI-C `\u` or `\x`. This caused
problems because `\x` does not properly encode Unicode chars outside
of the range (e.g. latin, CJK, emoji).

Instead, this CL treats control codes (C0, C1 sets) as the only
characters that need `\u` escaping, while everything else (except
single quote, backslash, newline) is not escaped.

Bug: 578054
Change-Id: I47301014ebe394bd02eb9f6c60108f4962f331ca
Reviewed-on: https://chromium-review.googlesource.com/1155924Reviewed-by: default avatarJoel Einbinder <einbinder@chromium.org>
Reviewed-by: default avatarAndrey Lushnikov <lushnikov@chromium.org>
Commit-Queue: Erik Luo <luoe@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584664}
parent f4a1ce74
...@@ -19,13 +19,13 @@ fetch: fetch("http://example.org/path", {"credentials":"omit","headers":{"conten ...@@ -19,13 +19,13 @@ fetch: fetch("http://example.org/path", {"credentials":"omit","headers":{"conten
cURL Windows: curl "http://example.org/path" -H "Content-Type: application/binary" --data-binary ^"1234^ cURL Windows: curl "http://example.org/path" -H "Content-Type: application/binary" --data-binary ^"1234^
00^^^^'^\^"^!^" --compressed 00^^^^'^\^"^!^" --compressed
cURL Unix: curl 'http://example.org/path' -H 'Content-Type: application/binary' --data-binary $'1234\r\n00\x02\x03\x04\x05\'"!' --compressed cURL Unix: curl 'http://example.org/path' -H 'Content-Type: application/binary' --data-binary $'1234\r\n00\u02\u03\u04\u05\'"!' --compressed
Powershell: Invoke-WebRequest -Uri "http://example.org/path" -Method "POST" -ContentType "application/binary" -Body ([System.Text.Encoding]::UTF8.GetBytes("1234$([char]13)$([char]10)00$([char]2)$([char]3)$([char]4)$([char]5)'`"!")) Powershell: Invoke-WebRequest -Uri "http://example.org/path" -Method "POST" -ContentType "application/binary" -Body ([System.Text.Encoding]::UTF8.GetBytes("1234$([char]13)$([char]10)00$([char]2)$([char]3)$([char]4)$([char]5)'`"!"))
fetch: fetch("http://example.org/path", {"credentials":"omit","headers":{"content-type":"application/binary"},"body":"1234\r\n00\u0002\u0003\u0004\u0005'\"!","method":"POST","mode":"cors"}); fetch: fetch("http://example.org/path", {"credentials":"omit","headers":{"content-type":"application/binary"},"body":"1234\r\n00\u0002\u0003\u0004\u0005'\"!","method":"POST","mode":"cors"});
cURL Windows: curl "http://example.org/path" -H "Content-Type: application/binary" --data-binary ^"1234^ cURL Windows: curl "http://example.org/path" -H "Content-Type: application/binary" --data-binary ^"1234^
^00^^^^'^\^"^!^" --compressed ^00^^^^'^\^"^!^" --compressed
cURL Unix: curl 'http://example.org/path' -H 'Content-Type: application/binary' --data-binary $'1234\r\n\x0100\x02\x03\x04\x05\'"!' --compressed cURL Unix: curl 'http://example.org/path' -H 'Content-Type: application/binary' --data-binary $'1234\r\n\u0100\u02\u03\u04\u05\'"!' --compressed
Powershell: Invoke-WebRequest -Uri "http://example.org/path" -Method "POST" -ContentType "application/binary" -Body ([System.Text.Encoding]::UTF8.GetBytes("1234$([char]13)$([char]10)$([char]1)00$([char]2)$([char]3)$([char]4)$([char]5)'`"!")) Powershell: Invoke-WebRequest -Uri "http://example.org/path" -Method "POST" -ContentType "application/binary" -Body ([System.Text.Encoding]::UTF8.GetBytes("1234$([char]13)$([char]10)$([char]1)00$([char]2)$([char]3)$([char]4)$([char]5)'`"!"))
fetch: fetch("http://example.org/path", {"credentials":"omit","headers":{"content-type":"application/binary"},"body":"1234\r\n\u000100\u0002\u0003\u0004\u0005'\"!","method":"POST","mode":"cors"}); fetch: fetch("http://example.org/path", {"credentials":"omit","headers":{"content-type":"application/binary"},"body":"1234\r\n\u000100\u0002\u0003\u0004\u0005'\"!","method":"POST","mode":"cors"});
cURL Windows: curl "http://example.org/path" -H "Content-Type: application/binary" --data-binary ^"^%^OS^%^ cURL Windows: curl "http://example.org/path" -H "Content-Type: application/binary" --data-binary ^"^%^OS^%^
...@@ -51,7 +51,7 @@ cURL Unix: curl 'http://example.org/path' -H 'Content-Type: application/binary' ...@@ -51,7 +51,7 @@ cURL Unix: curl 'http://example.org/path' -H 'Content-Type: application/binary'
Powershell: Invoke-WebRequest -Uri "http://example.org/path" -Method "POST" -ContentType "application/binary" -Body ([System.Text.Encoding]::UTF8.GetBytes("!@#`$%^&*()_+~``1234567890-=[]{};':`",./$([char]13)<>?$([char]13)$([char]10)qwer$([char]10)t$([char]10)$([char]10)yuiopasdfghjklmnbvcxzQWERTYUIOPLKJHGFDSAZXCVBNM")) Powershell: Invoke-WebRequest -Uri "http://example.org/path" -Method "POST" -ContentType "application/binary" -Body ([System.Text.Encoding]::UTF8.GetBytes("!@#`$%^&*()_+~``1234567890-=[]{};':`",./$([char]13)<>?$([char]13)$([char]10)qwer$([char]10)t$([char]10)$([char]10)yuiopasdfghjklmnbvcxzQWERTYUIOPLKJHGFDSAZXCVBNM"))
fetch: fetch("http://example.org/path", {"credentials":"omit","headers":{"content-type":"application/binary"},"body":"!@#$%^&*()_+~`1234567890-=[]{};':\",./\r<>?\r\nqwer\nt\n\nyuiopasdfghjklmnbvcxzQWERTYUIOPLKJHGFDSAZXCVBNM","method":"POST","mode":"cors"}); fetch: fetch("http://example.org/path", {"credentials":"omit","headers":{"content-type":"application/binary"},"body":"!@#$%^&*()_+~`1234567890-=[]{};':\",./\r<>?\r\nqwer\nt\n\nyuiopasdfghjklmnbvcxzQWERTYUIOPLKJHGFDSAZXCVBNM","method":"POST","mode":"cors"});
cURL Windows: curl "http://example.org/path" -H "Content-Type: application/binary" --data-binary "^^€^^ÿ ^܀" --compressed cURL Windows: curl "http://example.org/path" -H "Content-Type: application/binary" --data-binary "^^€^^ÿ ^܀" --compressed
cURL Unix: curl 'http://example.org/path' -H 'Content-Type: application/binary' --data-binary $'\x7f\x80\x90\xff\x09\u0700' --compressed cURL Unix: curl 'http://example.org/path' -H 'Content-Type: application/binary' --data-binary $'\u7f\u80\u90ÿ\u09܀' --compressed
Powershell: Invoke-WebRequest -Uri "http://example.org/path" -Method "POST" -ContentType "application/binary" -Body ([System.Text.Encoding]::UTF8.GetBytes("$([char]127)$([char]128)$([char]144)$([char]255)$([char]9)$([char]1792)")) Powershell: Invoke-WebRequest -Uri "http://example.org/path" -Method "POST" -ContentType "application/binary" -Body ([System.Text.Encoding]::UTF8.GetBytes("$([char]127)$([char]128)$([char]144)$([char]255)$([char]9)$([char]1792)"))
fetch: fetch("http://example.org/path", {"credentials":"omit","headers":{"content-type":"application/binary"},"body":"€ÿ\t܀","method":"POST","mode":"cors"}); fetch: fetch("http://example.org/path", {"credentials":"omit","headers":{"content-type":"application/binary"},"body":"€ÿ\t܀","method":"POST","mode":"cors"});
cURL Windows: curl "http://labs.ft.com/?querystring=^\[^\]^\{^\}" --compressed cURL Windows: curl "http://labs.ft.com/?querystring=^\[^\]^\{^\}" --compressed
......
...@@ -1630,23 +1630,19 @@ Network.NetworkLogView = class extends UI.VBox { ...@@ -1630,23 +1630,19 @@ Network.NetworkLogView = class extends UI.VBox {
* @return {string} * @return {string}
*/ */
function escapeCharacter(x) { function escapeCharacter(x) {
let code = x.charCodeAt(0); const code = x.charCodeAt(0);
if (code < 256) {
// Add leading zero when needed to not care about the next character. // Add leading zero when needed to not care about the next character.
return code < 16 ? '\\x0' + code.toString(16) : '\\x' + code.toString(16); return code < 16 ? '\\u0' + code.toString(16) : '\\u' + code.toString(16);
}
code = code.toString(16);
return '\\u' + ('0000' + code).substr(code.length, 4);
} }
if (/[^\x20-\x7E]|\'/.test(str)) { if (/[\u0000-\u001f\u007f-\u009f]|\'/.test(str)) {
// Use ANSI-C quoting syntax. // Use ANSI-C quoting syntax.
return '$\'' + return '$\'' +
str.replace(/\\/g, '\\\\') str.replace(/\\/g, '\\\\')
.replace(/\'/g, '\\\'') .replace(/\'/g, '\\\'')
.replace(/\n/g, '\\n') .replace(/\n/g, '\\n')
.replace(/\r/g, '\\r') .replace(/\r/g, '\\r')
.replace(/[^\x20-\x7E]/g, escapeCharacter) + .replace(/[\u0000-\u001f\u007f-\u009f]/g, escapeCharacter) +
'\''; '\'';
} else { } else {
// Use single quote syntax. // Use single quote syntax.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment