Skip to content

Commit

Permalink
Merge pull request #174 from wabarc/har
Browse files Browse the repository at this point in the history
Add support for HAR files
  • Loading branch information
gabriel-vasile committed Aug 18, 2021
2 parents d65b082 + ff67d25 commit 3b6e27b
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 2 deletions.
41 changes: 41 additions & 0 deletions internal/magic/text.go
Expand Up @@ -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("<svg"))
Expand Down
1 change: 1 addition & 0 deletions mimetype_test.go
Expand Up @@ -69,6 +69,7 @@ var files = map[string]string{
"gml.gml": "application/gml+xml",
"gpx.gpx": "application/gpx+xml",
"gz.gz": "application/gzip",
"har.har": "application/json",
"hdr.hdr": "image/vnd.radiance",
"heic.single.heic": "image/heic",
"heif.heif": "image/heif",
Expand Down
3 changes: 2 additions & 1 deletion supported_mimes.md
@@ -1,4 +1,4 @@
## 163 Supported MIME types
## 164 Supported MIME types
This file is automatically generated when running tests. Do not edit manually.

Extension | MIME type | Aliases
Expand Down Expand Up @@ -158,6 +158,7 @@ Extension | MIME type | Aliases
**.py** | application/x-python | -
**.json** | application/json | -
**.geojson** | application/geo+json | -
**.har** | application/json | -
**.ndjson** | application/x-ndjson | -
**.rtf** | text/rtf | -
**.tcl** | text/x-tcl | application/x-tcl
Expand Down
163 changes: 163 additions & 0 deletions testdata/har.har
@@ -0,0 +1,163 @@
{
"log": {
"version": "1.2",
"creator": {
"name": "WebInspector",
"version": "537.36"
},
"pages": [
{
"startedDateTime": "2021-08-14T04:22:59.975Z",
"id": "page_2",
"title": "http://example.com/",
"pageTimings": {
"onContentLoad": 992.4480000008771,
"onLoad": 1054.7850000002654
}
}
],
"entries": [
{
"_initiator": {
"type": "other"
},
"_priority": "VeryHigh",
"_resourceType": "document",
"cache": {},
"connection": "215113",
"pageref": "page_2",
"request": {
"method": "GET",
"url": "http://example.com/",
"httpVersion": "HTTP/1.1",
"headers": [
{
"name": "Host",
"value": "example.com"
},
{
"name": "Proxy-Connection",
"value": "keep-alive"
},
{
"name": "Pragma",
"value": "no-cache"
},
{
"name": "Cache-Control",
"value": "no-cache"
},
{
"name": "Upgrade-Insecure-Requests",
"value": "1"
},
{
"name": "User-Agent",
"value": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36"
},
{
"name": "Accept",
"value": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
},
{
"name": "Accept-Encoding",
"value": "gzip, deflate"
},
{
"name": "Accept-Language",
"value": "en-US,en;q=0.9"
}
],
"queryString": [],
"cookies": [],
"headersSize": 467,
"bodySize": 0
},
"response": {
"status": 200,
"statusText": "OK",
"httpVersion": "HTTP/1.1",
"headers": [
{
"name": "Accept-Ranges",
"value": "bytes"
},
{
"name": "Age",
"value": "390086"
},
{
"name": "Cache-Control",
"value": "max-age=604800"
},
{
"name": "Content-Type",
"value": "text/html; charset=UTF-8"
},
{
"name": "Date",
"value": "Sat, 14 Aug 2021 04:23:00 GMT"
},
{
"name": "Etag",
"value": "\"3147526947\""
},
{
"name": "Expires",
"value": "Sat, 21 Aug 2021 04:23:00 GMT"
},
{
"name": "Last-Modified",
"value": "Thu, 17 Oct 2019 07:18:26 GMT"
},
{
"name": "Server",
"value": "ECS (dcb/7EC6)"
},
{
"name": "Vary",
"value": "Accept-Encoding"
},
{
"name": "X-Cache",
"value": "HIT"
},
{
"name": "Content-Length",
"value": "1256"
},
{
"name": "Proxy-Connection",
"value": "keep-alive"
}
],
"cookies": [],
"content": {
"size": 1256,
"mimeType": "text/html",
"compression": 0,
"text": "<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <style type=\"text/css\">\n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: -apple-system, system-ui, BlinkMacSystemFont, \"Segoe UI\", \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 2em;\n background-color: #fdfdff;\n border-radius: 0.5em;\n box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n @media (max-width: 700px) {\n div {\n margin: 0 auto;\n width: auto;\n }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</h1>\n <p>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.</p>\n <p><a href=\"https://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\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
}
}
]
}
}
3 changes: 2 additions & 1 deletion tree.go
Expand Up @@ -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)
Expand Down

0 comments on commit 3b6e27b

Please sign in to comment.