From 73938622ef9b3a53d45063aee70ad5155202605a Mon Sep 17 00:00:00 2001 From: Jens Geyer Date: Fri, 7 Feb 2014 22:22:36 +0100 Subject: [PATCH] THRIFT-2346 C#: UTF-8 sent by PHP as JSON is not understood by TJsonProtocol Patch: Jens Geyer --- lib/csharp/src/Protocol/TJSONProtocol.cs | 49 ++++++++------ lib/csharp/test/JSON/JSONTest.csproj | 67 +++++++++++++++++++ lib/csharp/test/JSON/Program.cs | 55 +++++++++++++++ .../test/JSON/Properties/AssemblyInfo.cs | 36 ++++++++++ lib/csharp/test/JSON/app.config | 3 + 5 files changed, 190 insertions(+), 20 deletions(-) create mode 100644 lib/csharp/test/JSON/JSONTest.csproj create mode 100644 lib/csharp/test/JSON/Program.cs create mode 100644 lib/csharp/test/JSON/Properties/AssemblyInfo.cs create mode 100644 lib/csharp/test/JSON/app.config diff --git a/lib/csharp/src/Protocol/TJSONProtocol.cs b/lib/csharp/src/Protocol/TJSONProtocol.cs index 14db9ccc..9c62becb 100644 --- a/lib/csharp/src/Protocol/TJSONProtocol.cs +++ b/lib/csharp/src/Protocol/TJSONProtocol.cs @@ -58,7 +58,6 @@ namespace Thrift.Protocol private static byte[] RBRACKET = new byte[] { (byte)']' }; private static byte[] QUOTE = new byte[] { (byte)'"' }; private static byte[] BACKSLASH = new byte[] { (byte)'\\' }; - private static byte[] ZERO = new byte[] { (byte)'0' }; private byte[] ESCSEQ = new byte[] { (byte)'\\', (byte)'u', (byte)'0', (byte)'0' }; @@ -735,28 +734,38 @@ namespace Thrift.Protocol { break; } - if (ch == ESCSEQ[0]) + + // escaped? + if (ch != ESCSEQ[0]) + { + buffer.Write(new byte[] { (byte)ch }, 0, 1); + continue; + } + + // distinguish between \uXXXX and \? + ch = reader.Read(); + if (ch != ESCSEQ[1]) // control chars like \n { - ch = reader.Read(); - if (ch == ESCSEQ[1]) + int off = Array.IndexOf(ESCAPE_CHARS, (char)ch); + if (off == -1) { - ReadJSONSyntaxChar(ZERO); - ReadJSONSyntaxChar(ZERO); - trans.ReadAll(tempBuffer, 0, 2); - ch = (byte)((HexVal((byte)tempBuffer[0]) << 4) + HexVal(tempBuffer[1])); + throw new TProtocolException(TProtocolException.INVALID_DATA, + "Expected control char"); } - else - { - int off = Array.IndexOf(ESCAPE_CHARS, (char)ch); - if (off == -1) - { - throw new TProtocolException(TProtocolException.INVALID_DATA, - "Expected control char"); - } - ch = ESCAPE_CHAR_VALS[off]; - } - } - buffer.Write(new byte[] { (byte)ch }, 0, 1); + ch = ESCAPE_CHAR_VALS[off]; + buffer.Write(new byte[] { (byte)ch }, 0, 1); + continue; + } + + + // it's \uXXXX + trans.ReadAll(tempBuffer, 0, 4); + var wch = (short)((HexVal((byte)tempBuffer[0]) << 12) + + (HexVal((byte)tempBuffer[1]) << 8) + + (HexVal((byte)tempBuffer[2]) << 4) + + HexVal(tempBuffer[3])); + var tmp = utf8Encoding.GetBytes(new char[] { (char)wch }); + buffer.Write(tmp, 0, tmp.Length); } return buffer.ToArray(); } diff --git a/lib/csharp/test/JSON/JSONTest.csproj b/lib/csharp/test/JSON/JSONTest.csproj new file mode 100644 index 00000000..73303b84 --- /dev/null +++ b/lib/csharp/test/JSON/JSONTest.csproj @@ -0,0 +1,67 @@ + + + + Debug + x86 + 8.0.30703 + 2.0 + {E37A0034-DCBF-4886-A0DA-25A03D12D975} + Exe + Properties + JSONTest + JSONTest + v4.0 + + + 512 + + + x86 + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + x86 + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + {499EB63C-D74C-47E8-AE48-A2FC94538E9D} + Thrift + + + + + \ No newline at end of file diff --git a/lib/csharp/test/JSON/Program.cs b/lib/csharp/test/JSON/Program.cs new file mode 100644 index 00000000..7bdb7f5f --- /dev/null +++ b/lib/csharp/test/JSON/Program.cs @@ -0,0 +1,55 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using Thrift.Protocol; +using Thrift.Transport; + +namespace JSONTest +{ + class Program + { + static void Main(string[] args) + { + TestThrift2336(); + } + + public static void TestThrift2336() + { + const string RUSSIAN_TEXT = "\u0420\u0443\u0441\u0441\u043a\u043e\u0435 \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435"; + const string RUSSIAN_JSON = "\"\\u0420\\u0443\\u0441\\u0441\\u043a\\u043e\\u0435 \\u041d\\u0430\\u0437\\u0432\\u0430\\u043d\\u0438\\u0435\""; + + // prepare buffer with JOSN data + byte[] rawBytes = new byte[RUSSIAN_JSON.Length]; + for (var i = 0; i < RUSSIAN_JSON.Length; ++i) + rawBytes[i] = (byte)(RUSSIAN_JSON[i] & (char)0xFF); // only low bytes + + // parse and check + var stm = new MemoryStream(rawBytes); + var trans = new TStreamTransport(stm, null); + var prot = new TJSONProtocol(trans); + Debug.Assert(prot.ReadString() == RUSSIAN_TEXT, "reading JSON with hex-encoded chars > 8 bit"); + } + } +} diff --git a/lib/csharp/test/JSON/Properties/AssemblyInfo.cs b/lib/csharp/test/JSON/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..a60ebc1e --- /dev/null +++ b/lib/csharp/test/JSON/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die mit einer Assembly verknüpft sind. +[assembly: AssemblyTitle("JSONTest")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("JSONTest")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar +// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von +// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("2b2e7d56-3e65-4368-92d7-e34d56b7105e")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern +// übernehmen, indem Sie "*" eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/lib/csharp/test/JSON/app.config b/lib/csharp/test/JSON/app.config new file mode 100644 index 00000000..cb2586be --- /dev/null +++ b/lib/csharp/test/JSON/app.config @@ -0,0 +1,3 @@ + + + -- 2.17.1