1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-04 10:35:08 +02:00

Bugfix/#559 html escaping (#573)

Added jettison for json encoding
This commit is contained in:
Tim Voronov 2020-11-23 20:12:04 -05:00 committed by GitHub
parent 40cb01b52b
commit 2baac62d1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
35 changed files with 223 additions and 72 deletions

2
go.mod
View File

@ -20,7 +20,7 @@ require (
github.com/rs/zerolog v1.19.0
github.com/sethgrid/pester v1.1.0
github.com/smartystreets/goconvey v1.6.4
github.com/stretchr/testify v1.4.0 // indirect
github.com/wI2L/jettison v0.7.1
golang.org/x/net v0.0.0-20200421231249-e086a090c8fd
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f // indirect

19
go.sum
View File

@ -11,6 +11,7 @@ github.com/antchfx/htmlquery v1.2.3/go.mod h1:B0ABL+F5irhhMWg54ymEZinzMSi0Kt3I2i
github.com/antchfx/xpath v1.1.6/go.mod h1:Yee4kTMuNiPYJ7nSNorELQMr1J33uOpXDMByNYhvtNk=
github.com/antchfx/xpath v1.1.10 h1:cJ0pOvEdN/WvYXxvRrzQH9x5QWKpzHacYO8qzCcDYAg=
github.com/antchfx/xpath v1.1.10/go.mod h1:Yee4kTMuNiPYJ7nSNorELQMr1J33uOpXDMByNYhvtNk=
github.com/antchfx/xpath v1.1.11 h1:WOFtK8TVAjLm3lbgqeP0arlHpvCEeTANeWZ/csPpJkQ=
github.com/antchfx/xpath v1.1.11/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
github.com/antlr/antlr4 v0.0.0-20200417160354-8c50731894e0 h1:j7MyDjg6pb7A2ziow17FDZ2Oj5vGnJsLyDmjpN4Jkcg=
github.com/antlr/antlr4 v0.0.0-20200417160354-8c50731894e0/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y=
@ -23,26 +24,38 @@ github.com/codegangsta/cli v1.20.0/go.mod h1:/qJNoX69yVSKu5o4jLyXAENLRyk1uhi7zkb
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/corpix/uarand v0.1.1 h1:RMr1TWc9F4n5jiPDzFHtmaUXLKLNUFK0SgCLo4BhX/U=
github.com/corpix/uarand v0.1.1/go.mod h1:SFKZvkcRoLqVRFZ4u25xPmp6m9ktANfbpXZ7SJ0/FNU=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/derekparker/trie v0.0.0-20200317170641-1fdf38b7b0e9 h1:G765iDCq7bP5opdrPkXk+4V3yfkgV9iGFuheWZ/X/zY=
github.com/derekparker/trie v0.0.0-20200317170641-1fdf38b7b0e9/go.mod h1:D6ICZm05D9VN1n/8iOtBxLpXtoGp6HDFUJ1RNVieOSE=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mafredri/cdp v0.30.0 h1:Lvcwjajq6wB6Uk8dYeCLrF26LG85rUdpMxgrwdEvU0o=
github.com/mafredri/cdp v0.30.0/go.mod h1:71D84qPmWUvBWYj24Zp+U69mrUof4o8qL2X1fQJ/lHc=
github.com/mafredri/go-lint v0.0.0-20180911205320-920981dfc79e/go.mod h1:k/zdyxI3q6dup24o8xpYjJKTCf2F7rfxLp6w/efTiWs=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
github.com/ngdinhtoan/glide-cleanup v0.2.0/go.mod h1:UQzsmiDOb8YV3nOsCxK/c9zPpCZVNoHScRE3EO9pVMM=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@ -50,6 +63,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.19.0 h1:hYz4ZVdUgjXTBUmrkrw55j1nHx68LfOKIQk5IYtyScg=
github.com/rs/zerolog v1.19.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo=
github.com/segmentio/encoding v0.1.10/go.mod h1:RWhr02uzMB9gQC1x+MfYxedtmBibb9cZ6Vv9VxRSSbw=
github.com/sethgrid/pester v1.1.0 h1:IyEAVvwSUPjs2ACFZkBe5N59BBUpSIkQ71Hr6cM5A+w=
github.com/sethgrid/pester v1.1.0/go.mod h1:Ad7IjTpvzZO8Fl0vh9AzQ+j/jYZfyp2diGwI8m5q+ns=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
@ -59,6 +73,9 @@ github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/wI2L/jettison v0.7.1 h1:XNq/WvSOAiJhFww9F5JZZcBZtKFL2Y/9WHHEHLDq9TE=
github.com/wI2L/jettison v0.7.1/go.mod h1:dj49nOP41M7x6Jql62BqqF/+nW+XJgBaWzJR0hd6M84=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
@ -92,5 +109,7 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -15,6 +15,7 @@ import (
"github.com/mafredri/cdp/protocol/runtime"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/wI2L/jettison"
"golang.org/x/net/html"
"github.com/MontFerret/ferret/pkg/drivers"
@ -186,7 +187,7 @@ func (el *HTMLElement) Type() core.Type {
}
func (el *HTMLElement) MarshalJSON() ([]byte, error) {
return json.Marshal(el.String())
return jettison.MarshalOpts(el.String(), jettison.NoHTMLEscaping())
}
func (el *HTMLElement) String() string {

View File

@ -1,8 +1,9 @@
package dom
import (
"github.com/mafredri/cdp/protocol/page"
"sync"
"github.com/mafredri/cdp/protocol/page"
)
type (

View File

@ -3,20 +3,19 @@ package dom
import (
"bytes"
"context"
"encoding/json"
"errors"
"golang.org/x/net/html"
"time"
"github.com/MontFerret/ferret/pkg/drivers/cdp/eval"
"github.com/MontFerret/ferret/pkg/drivers/cdp/templates"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/PuerkitoBio/goquery"
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/protocol/dom"
"github.com/mafredri/cdp/protocol/page"
"github.com/mafredri/cdp/protocol/runtime"
"golang.org/x/net/html"
"github.com/MontFerret/ferret/pkg/drivers/cdp/eval"
"github.com/MontFerret/ferret/pkg/drivers/cdp/templates"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
var emptyExpires = time.Time{}
@ -54,7 +53,7 @@ func setInnerHTML(ctx context.Context, client *cdp.Client, exec *eval.ExecutionC
objID = repl.Object.ObjectID
}
b, err := json.Marshal(innerHTML.String())
b, err := innerHTML.MarshalJSON()
if err != nil {
return err
@ -65,7 +64,7 @@ func setInnerHTML(ctx context.Context, client *cdp.Client, exec *eval.ExecutionC
ObjectID: objID,
},
runtime.CallArgument{
Value: json.RawMessage(b),
Value: b,
},
)
@ -130,7 +129,7 @@ func setInnerText(ctx context.Context, client *cdp.Client, exec *eval.ExecutionC
objID = repl.Object.ObjectID
}
b, err := json.Marshal(innerText.String())
b, err := innerText.MarshalJSON()
if err != nil {
return err
@ -141,7 +140,7 @@ func setInnerText(ctx context.Context, client *cdp.Client, exec *eval.ExecutionC
ObjectID: objID,
},
runtime.CallArgument{
Value: json.RawMessage(b),
Value: b,
},
)

View File

@ -2,19 +2,21 @@ package dom
import (
"context"
"github.com/MontFerret/ferret/pkg/drivers/cdp/events"
"github.com/MontFerret/ferret/pkg/drivers/cdp/input"
"github.com/MontFerret/ferret/pkg/drivers/common"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"io"
"sync"
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/protocol/dom"
"github.com/mafredri/cdp/protocol/page"
"github.com/mafredri/cdp/rpcc"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"io"
"sync"
"github.com/MontFerret/ferret/pkg/drivers/cdp/events"
"github.com/MontFerret/ferret/pkg/drivers/cdp/input"
"github.com/MontFerret/ferret/pkg/drivers/common"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
var (

View File

@ -3,15 +3,15 @@ package eval
import (
"context"
"fmt"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/mafredri/cdp/protocol/dom"
"strings"
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/protocol/dom"
"github.com/mafredri/cdp/protocol/page"
"github.com/mafredri/cdp/protocol/runtime"
"github.com/pkg/errors"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)

View File

@ -4,9 +4,10 @@ import (
"fmt"
"strconv"
"github.com/mafredri/cdp/protocol/runtime"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/mafredri/cdp/protocol/runtime"
)
func PrepareEval(exp string) string {

View File

@ -2,8 +2,9 @@ package events
import (
"errors"
"github.com/MontFerret/ferret/pkg/runtime/core"
"sync"
"github.com/MontFerret/ferret/pkg/runtime/core"
)
type SourceCollection struct {

View File

@ -2,13 +2,15 @@ package cdp
import (
"context"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/drivers/common"
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/protocol/emulation"
"github.com/mafredri/cdp/protocol/network"
"github.com/mafredri/cdp/protocol/page"
"golang.org/x/sync/errgroup"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/drivers/common"
)
type (

View File

@ -2,11 +2,11 @@ package input
import (
"context"
"github.com/pkg/errors"
"time"
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/protocol/input"
"github.com/pkg/errors"
)
const DefaultDelay = 25

View File

@ -1,10 +1,12 @@
package network
import (
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/mafredri/cdp/protocol/network"
"strings"
"time"
"github.com/mafredri/cdp/protocol/network"
"github.com/MontFerret/ferret/pkg/drivers"
)
var emptyExpires = time.Time{}

View File

@ -3,20 +3,22 @@ package network
import (
"context"
"encoding/json"
"github.com/MontFerret/ferret/pkg/drivers/cdp/eval"
"github.com/MontFerret/ferret/pkg/drivers/cdp/templates"
"io"
"regexp"
"sync"
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/protocol/network"
"github.com/mafredri/cdp/protocol/page"
"github.com/mafredri/cdp/rpcc"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"io"
"regexp"
"sync"
"github.com/wI2L/jettison"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/drivers/cdp/eval"
"github.com/MontFerret/ferret/pkg/drivers/cdp/events"
"github.com/MontFerret/ferret/pkg/drivers/cdp/templates"
"github.com/MontFerret/ferret/pkg/drivers/common"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
@ -180,7 +182,7 @@ func (m *Manager) SetHeaders(ctx context.Context, headers drivers.HTTPHeaders) e
m.headers = headers
j, err := json.Marshal(headers)
j, err := jettison.MarshalOpts(headers, jettison.NoHTMLEscaping())
if err != nil {
return errors.Wrap(err, "failed to marshal headers")

View File

@ -1,8 +1,9 @@
package common
import (
"github.com/MontFerret/ferret/pkg/runtime/core"
"sync"
"github.com/MontFerret/ferret/pkg/runtime/core"
)
type (

View File

@ -1,9 +1,10 @@
package common
import (
"io"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/rs/zerolog"
"io"
)
var (

View File

@ -2,13 +2,14 @@ package drivers
import (
"context"
"encoding/json"
"fmt"
"hash/fnv"
"strconv"
"strings"
"time"
"github.com/wI2L/jettison"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
@ -152,7 +153,7 @@ func (c HTTPCookie) MarshalJSON() ([]byte, error) {
"same_site": c.SameSite.String(),
}
out, err := json.Marshal(v)
out, err := jettison.MarshalOpts(v, jettison.NoHTMLEscaping())
if err != nil {
return nil, err

View File

@ -3,13 +3,14 @@ package drivers
import (
"context"
"encoding/binary"
"encoding/json"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
"hash/fnv"
"sort"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
"github.com/wI2L/jettison"
)
type HTTPCookies map[string]HTTPCookie
@ -19,7 +20,7 @@ func NewHTTPCookies() HTTPCookies {
}
func (c HTTPCookies) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]HTTPCookie(c))
return jettison.MarshalOpts(map[string]HTTPCookie(c), jettison.NoHTMLEscaping())
}
func (c HTTPCookies) Type() core.Type {

View File

@ -3,7 +3,6 @@ package drivers
import (
"bytes"
"context"
"encoding/json"
"fmt"
"hash/fnv"
"net/textproto"
@ -13,6 +12,8 @@ import (
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
"github.com/wI2L/jettison"
)
// HTTPHeaders HTTP header object
@ -111,7 +112,7 @@ func (h HTTPHeaders) MarshalJSON() ([]byte, error) {
headers[key] = strings.Join(val, ", ")
}
out, err := json.Marshal(headers)
out, err := jettison.MarshalOpts(headers, jettison.NoHTMLEscaping())
if err != nil {
return nil, err

View File

@ -4,11 +4,12 @@ import (
"context"
"hash/fnv"
"github.com/PuerkitoBio/goquery"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/drivers/common"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/PuerkitoBio/goquery"
)
type HTMLDocument struct {

View File

@ -2,18 +2,18 @@ package http
import (
"context"
"encoding/json"
"hash/fnv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/antchfx/htmlquery"
"github.com/antchfx/xpath"
"github.com/wI2L/jettison"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/drivers/common"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/PuerkitoBio/goquery"
"github.com/antchfx/htmlquery"
"github.com/antchfx/xpath"
)
type HTMLElement struct {
@ -32,7 +32,7 @@ func NewHTMLElement(node *goquery.Selection) (drivers.HTMLElement, error) {
}
func (el *HTMLElement) MarshalJSON() ([]byte, error) {
return json.Marshal(el.String())
return jettison.MarshalOpts(el.String(), jettison.NoHTMLEscaping())
}
func (el *HTMLElement) Type() core.Type {

View File

@ -2,11 +2,12 @@ package drivers
import (
"context"
"encoding/json"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
"github.com/wI2L/jettison"
)
// HTTPResponse HTTP response object.
@ -66,10 +67,10 @@ type responseMarshal struct {
func (resp *HTTPResponse) MarshalJSON() ([]byte, error) {
if resp == nil {
return json.Marshal(values.None)
return values.None.MarshalJSON()
}
return json.Marshal(responseMarshal(*resp))
return jettison.MarshalOpts(responseMarshal(*resp), jettison.NoHTMLEscaping())
}
func (resp *HTTPResponse) GetIn(ctx context.Context, path []core.Value) (core.Value, error) {

View File

@ -3,10 +3,11 @@ package values
import (
"context"
"encoding/binary"
"encoding/json"
"hash/fnv"
"sort"
"github.com/wI2L/jettison"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
@ -30,7 +31,7 @@ func NewArrayWith(values ...core.Value) *Array {
}
func (t *Array) MarshalJSON() ([]byte, error) {
return json.Marshal(t.items)
return jettison.MarshalOpts(t.items, jettison.NoHTMLEscaping())
}
func (t *Array) Type() core.Type {

View File

@ -1,11 +1,12 @@
package values
import (
"encoding/json"
"hash/fnv"
"io"
"io/ioutil"
"github.com/wI2L/jettison"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
@ -27,7 +28,7 @@ func NewBinaryFrom(stream io.Reader) (Binary, error) {
}
func (b Binary) MarshalJSON() ([]byte, error) {
return json.Marshal([]byte(b))
return jettison.MarshalOpts([]byte(b), jettison.NoHTMLEscaping())
}
func (b Binary) Type() core.Type {

View File

@ -1,10 +1,11 @@
package values
import (
"encoding/json"
"hash/fnv"
"strings"
"github.com/wI2L/jettison"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
@ -51,7 +52,7 @@ func MustParseBoolean(input interface{}) Boolean {
}
func (t Boolean) MarshalJSON() ([]byte, error) {
return json.Marshal(bool(t))
return jettison.MarshalOpts(bool(t), jettison.NoHTMLEscaping())
}
func (t Boolean) Type() core.Type {

View File

@ -4,6 +4,8 @@ import (
"hash/fnv"
"time"
"github.com/wI2L/jettison"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
@ -56,7 +58,7 @@ func MustParseDateTime(input interface{}) DateTime {
}
func (t DateTime) MarshalJSON() ([]byte, error) {
return t.Time.MarshalJSON()
return jettison.MarshalOpts(t.Time, jettison.NoHTMLEscaping())
}
func (t DateTime) Type() core.Type {

View File

@ -1,9 +1,11 @@
package values_test
import (
"encoding/json"
"github.com/MontFerret/ferret/pkg/runtime/values"
. "github.com/smartystreets/goconvey/convey"
"testing"
"time"
)
func TestDateTime(t *testing.T) {
@ -22,4 +24,18 @@ func TestDateTime(t *testing.T) {
So(d.Hash(), ShouldEqual, d.Hash())
})
})
Convey(".MarshalJSON", t, func() {
Convey("It should correctly serialize value", func() {
value := time.Now()
json1, err := json.Marshal(value)
So(err, ShouldBeNil)
json2, err := values.NewDateTime(value).MarshalJSON()
So(err, ShouldBeNil)
So(json1, ShouldResemble, json2)
})
})
}

View File

@ -2,12 +2,13 @@ package values
import (
"encoding/binary"
"encoding/json"
"fmt"
"hash/fnv"
"math"
"strconv"
"github.com/wI2L/jettison"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
@ -74,7 +75,7 @@ func IsInf(input Float, sign Int) Boolean {
}
func (t Float) MarshalJSON() ([]byte, error) {
return json.Marshal(float64(t))
return jettison.MarshalOpts(float64(t), jettison.NoHTMLEscaping())
}
func (t Float) Type() core.Type {

View File

@ -1,6 +1,7 @@
package values_test
import (
"encoding/json"
"github.com/MontFerret/ferret/pkg/runtime/values"
. "github.com/smartystreets/goconvey/convey"
"testing"
@ -26,4 +27,18 @@ func TestFloat(t *testing.T) {
So(v.Hash(), ShouldEqual, v.Hash())
})
})
Convey(".MarshalJSON", t, func() {
Convey("It should correctly serialize value", func() {
value := float64(10)
json1, err := json.Marshal(value)
So(err, ShouldBeNil)
json2, err := values.NewFloat(value).MarshalJSON()
So(err, ShouldBeNil)
So(json1, ShouldResemble, json2)
})
})
}

View File

@ -2,9 +2,12 @@ package values_test
import (
"context"
"encoding/json"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
"testing"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
@ -418,5 +421,29 @@ func TestHelpers(t *testing.T) {
So(arr.Get(values.NewInt(2)) == input.MustGet("qaz"), ShouldBeTrue)
})
})
Convey("Unmarshal", func() {
Convey("Should deserialize object", func() {
input := map[string]interface{}{
"foo": []string{
"bar",
"qaz",
},
}
json1, err := json.Marshal(input)
So(err, ShouldBeNil)
val, err := values.Unmarshal(json1)
So(err, ShouldBeNil)
So(val.Type(), ShouldResemble, types.Object)
json2, err := val.MarshalJSON()
So(err, ShouldBeNil)
So(json2, ShouldResemble, json1)
})
})
})
}

View File

@ -2,10 +2,11 @@ package values
import (
"encoding/binary"
"encoding/json"
"hash/fnv"
"strconv"
"github.com/wI2L/jettison"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
@ -62,7 +63,7 @@ func MustParseInt(input interface{}) Int {
}
func (t Int) MarshalJSON() ([]byte, error) {
return json.Marshal(int64(t))
return jettison.MarshalOpts(int64(t), jettison.NoHTMLEscaping())
}
func (t Int) Type() core.Type {

View File

@ -1,6 +1,7 @@
package values_test
import (
"encoding/json"
"github.com/MontFerret/ferret/pkg/runtime/values"
. "github.com/smartystreets/goconvey/convey"
"testing"
@ -26,4 +27,18 @@ func TestInt(t *testing.T) {
So(v.Hash(), ShouldEqual, v.Hash())
})
})
Convey(".MarshalJSON", t, func() {
Convey("It should correctly serialize value", func() {
value := 10
json1, err := json.Marshal(value)
So(err, ShouldBeNil)
json2, err := values.NewInt(value).MarshalJSON()
So(err, ShouldBeNil)
So(json1, ShouldResemble, json2)
})
})
}

View File

@ -3,10 +3,11 @@ package values
import (
"context"
"encoding/binary"
"encoding/json"
"hash/fnv"
"sort"
"github.com/wI2L/jettison"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
@ -43,7 +44,7 @@ func NewObjectWith(props ...*ObjectProperty) *Object {
}
func (t *Object) MarshalJSON() ([]byte, error) {
return json.Marshal(t.value)
return jettison.MarshalOpts(t.value, jettison.NoHTMLEscaping())
}
func (t *Object) Type() core.Type {

View File

@ -1,11 +1,12 @@
package values
import (
"encoding/json"
"fmt"
"hash/fnv"
"strings"
"github.com/wI2L/jettison"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
@ -68,7 +69,7 @@ func MustParseString(input interface{}) String {
}
func (t String) MarshalJSON() ([]byte, error) {
return json.Marshal(string(t))
return jettison.MarshalOpts(string(t), jettison.NoHTMLEscaping())
}
func (t String) Type() core.Type {

View File

@ -1,6 +1,8 @@
package values_test
import (
"encoding/json"
"fmt"
"github.com/MontFerret/ferret/pkg/runtime/values"
. "github.com/smartystreets/goconvey/convey"
"testing"
@ -34,4 +36,32 @@ func TestString(t *testing.T) {
So(str.Length(), ShouldEqual, 7)
})
})
Convey(".MarshalJSON", t, func() {
Convey("It should correctly serialize value", func() {
value := "foobar"
json1, err := json.Marshal(value)
So(err, ShouldBeNil)
json2, err := values.NewString(value).MarshalJSON()
So(err, ShouldBeNil)
So(json1, ShouldResemble, json2)
})
Convey("It should NOT escape HTML", func() {
value := "<div><span>Foobar</span></div>"
json1, err := json.Marshal(value)
So(err, ShouldBeNil)
json2, err := values.NewString(value).MarshalJSON()
So(err, ShouldBeNil)
So(json1, ShouldNotResemble, json2)
So(string(json2), ShouldEqual, fmt.Sprintf(`"%s"`, value))
})
})
}

View File

@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"github.com/wI2L/jettison"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
@ -39,7 +41,7 @@ func JSONStringify(_ context.Context, args ...core.Value) (core.Value, error) {
return values.EmptyString, err
}
out, err := json.Marshal(args[0])
out, err := jettison.MarshalOpts(args[0])
if err != nil {
return values.EmptyString, err