forked from readium/readium-lcp-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop escaping some characters on json serialization of a license
Fixes readium#162, issue with character '&' (and '<', '>') in hints.
- Loading branch information
Showing
4 changed files
with
47 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,45 @@ | ||
// Copyright (c) 2016 Readium Foundation | ||
// | ||
// Redistribution and use in source and binary forms, with or without modification, | ||
// are permitted provided that the following conditions are met: | ||
// | ||
// 1. Redistributions of source code must retain the above copyright notice, this | ||
// list of conditions and the following disclaimer. | ||
// 2. Redistributions in binary form must reproduce the above copyright notice, | ||
// this list of conditions and the following disclaimer in the documentation and/or | ||
// other materials provided with the distribution. | ||
// 3. Neither the name of the organization nor the names of its contributors may be | ||
// used to endorse or promote products derived from this software without specific | ||
// prior written permission | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
// Copyright 2017 European Digital Reading Lab. All rights reserved. | ||
// Licensed to the Readium Foundation under one or more contributor license agreements. | ||
// Use of this source code is governed by a BSD-style license | ||
// that can be found in the LICENSE file exposed on Github (readium) in the project repository. | ||
|
||
package sign | ||
|
||
import "encoding/json" | ||
import "strings" | ||
import "io" | ||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"strings" | ||
) | ||
|
||
func Canon(in interface{}) ([]byte, error) { | ||
// the easiest way to canonicalize is to marshal it and reify it as a map | ||
// which will sort stuff correctly | ||
b, err := json.Marshal(in) | ||
if err != nil { | ||
return b, err | ||
b, err1 := json.Marshal(in) | ||
if err1 != nil { | ||
return b, err1 | ||
} | ||
|
||
var jsonObj interface{} // map[string]interface{} ==> auto sorting | ||
|
||
dec := json.NewDecoder(strings.NewReader(string(b))) | ||
dec.UseNumber() | ||
for { | ||
if er := dec.Decode(&jsonObj); er == io.EOF { | ||
if err2 := dec.Decode(&jsonObj); err2 == io.EOF { | ||
break | ||
} else if er != nil { | ||
return nil, er | ||
} else if err2 != nil { | ||
return nil, err2 | ||
} | ||
} | ||
var buf bytes.Buffer | ||
enc := json.NewEncoder(&buf) | ||
// do not escape characters | ||
enc.SetEscapeHTML(false) | ||
err := enc.Encode(jsonObj) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// remove the trailing newline, added by encode | ||
return bytes.TrimRight(buf.Bytes(), "\n"), nil | ||
|
||
return json.Marshal(jsonObj) | ||
} |