diff --git a/internal/magic/text.go b/internal/magic/text.go index d23bd9d5..a8778d86 100644 --- a/internal/magic/text.go +++ b/internal/magic/text.go @@ -251,6 +251,47 @@ func NdJson(raw []byte, limit uint32) bool { return parsed > 2 && parsed == len(raw) } +// Har matches a HAR Spec file. +// Spec: http://www.softwareishard.com/blog/har-12-spec/ +func HAR(raw []byte, limit uint32) bool { + s := []byte(`"log"`) + si, sl := bytes.Index(raw, s), len(s) + + if si == -1 { + return false + } + + // If the "log" string is the suffix of the input, + // there is no need to search for the value of the key. + if si+sl == len(raw) { + return false + } + // Skip the "log" part. + raw = raw[si+sl:] + // Skip any whitespace before the colon. + raw = trimLWS(raw) + // Check for colon. + if len(raw) == 0 || raw[0] != ':' { + return false + } + // Skip any whitespace after the colon. + raw = trimLWS(raw[1:]) + + harJsonTypes := [][]byte{ + []byte(`"version"`), + []byte(`"creator"`), + []byte(`"entries"`), + } + for _, t := range harJsonTypes { + si := bytes.Index(raw, t) + if si > -1 { + return true + } + } + + return false +} + // Svg matches a SVG file. func Svg(raw []byte, limit uint32) bool { return bytes.Contains(raw, []byte("\n\n\n Example Domain\n\n \n \n \n \n\n\n\n
\n

Example Domain

\n

This domain is for use in illustrative examples in documents. You may use this\n domain in literature without prior coordination or asking for permission.

\n

More information...

\n
\n\n\n" + }, + "redirectURL": "", + "headersSize": 381, + "bodySize": 1256, + "_transferSize": 1637, + "_error": null + }, + "serverIPAddress": "127.0.0.1", + "startedDateTime": "2021-08-14T04:22:59.968Z", + "time": 948.011000000406, + "timings": { + "blocked": 10.241999999793713, + "dns": -1, + "ssl": -1, + "connect": -1, + "send": 0.19499999999999984, + "wait": 936.1970000001099, + "receive": 1.3770000005024485, + "_blocked_queueing": 6.790999999793712 + } + } + ] + } +} \ No newline at end of file diff --git a/tree.go b/tree.go index c9a1985c..e6e0296c 100644 --- a/tree.go +++ b/tree.go @@ -76,7 +76,8 @@ var ( oggVideo = newMIME("video/ogg", ".ogv", magic.OggVideo) text = newMIME("text/plain", ".txt", magic.Text, html, svg, xml, php, js, lua, perl, python, json, ndJson, rtf, tcl, csv, tsv, vCard, iCalendar, warc) xml = newMIME("text/xml", ".xml", magic.Xml, rss, atom, x3d, kml, xliff, collada, gml, gpx, tcx, amf, threemf, xfdf, owl2) - json = newMIME("application/json", ".json", magic.Json, geoJson) + json = newMIME("application/json", ".json", magic.Json, geoJson, har) + har = newMIME("application/json", ".har", magic.HAR) csv = newMIME("text/csv", ".csv", magic.Csv) tsv = newMIME("text/tab-separated-values", ".tsv", magic.Tsv) geoJson = newMIME("application/geo+json", ".geojson", magic.GeoJson)