forked from HTTPArchive/legacy.httparchive.org
-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema.js
294 lines (271 loc) · 8.59 KB
/
schema.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// ************************************************************************************************
// HAR Schema Definition
// Date time fields use ISO8601 (YYYY-MM-DDThh:mm:ss.sTZD, e.g. 2009-07-24T19:20:30.45+01:00)
var dateTimePattern = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;
/**
* Root HTML Archive type.
*/
var logType = {
"logType": {
"id": "logType",
"description": "HTTP Archive structure.",
"type": "object",
"properties": {
"log": {
"type": "object",
"properties": {
"version": {"type": "string"},
"creator": {"$ref": "creatorType"},
"browser": {"$ref": "browserType"},
"pages": {"type": "array", "items": {"$ref": "pageType"}},
"entries": {"type": "array", "items": {"$ref": "entryType"}}
}
}
}
}
};
var creatorType = {
"creatorType": {
"id": "creatorType",
"description": "Name and version info of the log creator app.",
"type": "object",
"properties": {
"name" : {"type": "string"},
"version" : {"type": "string"}
}
}
};
var browserType = {
"browserType": {
"id": "browserType",
"description": "Name and version info of used browser.",
"type": "object",
"optional": true,
"properties": {
"name" : {"type": "string"},
"version" : {"type": "string"}
}
}
};
var pageType = {
"pageType": {
"id": "pageType",
"description": "Exported web page",
"optional": true,
"properties": {
"startedDateTime": {"type": "string", "format": "date-time", "pattern": dateTimePattern},
"id": {"type": "string", "unique": true},
"title": {"type": "string"},
"pageTimings": {"$ref": "pageTimingsType"}
}
}
};
var pageTimingsType = {
"pageTimingsType": {
"id": "pageTimingsType",
"description": "Timing info about page load",
"properties": {
"onContentLoad": {"type": "number", "min": -1},
"onLoad": {"type": "number", "min": -1}
}
}
};
var entryType = {
"entryType": {
"id": "entryType",
"description": "Request and Response related info",
"optional": true,
"properties": {
"pageref": {"type": "string", "optional": true},
"startedDateTime": {"type": "string", "format": "date-time", "pattern": dateTimePattern},
"time": {"type": "integer", "min": 0},
"request" : {"$ref": "requestType"},
"response" : {"$ref": "responseType"},
"cache" : {"$ref": "cacheType"},
"timings" : {"$ref": "timingsType"}
}
}
};
var requestType = {
"requestType": {
"id": "requestType",
"description": "Monitored request",
"properties": {
"method": {"type": "string", "enum": ["GET", "POST", "PUT", "DELETE"]},
"url": {"type": "string"},
"httpVersion": {"type" : "string"},
"cookies" : {"type": "array", "items": {"$ref": "cookieType"}},
"headers" : {"type": "array", "items": {"$ref": "recordType"}},
"queryString" : {"type": "array", "items": {"$ref": "recordType"}},
"postData" : {"$ref": "postDataType"},
"headersSize" : {"type": "integer"},
"bodySize" : {"type": "integer"}
}
}
};
var recordType = {
"recordType": {
"id": "recordType",
"description": "Helper name-value pair structure.",
"properties": {
"name": {"type": "string"},
"value": {"type": "string"}
}
}
};
var responseType = {
"responseType": {
"id": "responseType",
"description": "Monitored Response.",
"properties": {
"status": {"type": "integer"},
"statusText": {"type": "string"},
"httpVersion": {"type": "string"},
"cookies" : {"type": "array", "items": {"$ref": "cookieType"}},
"headers" : {"type": "array", "items": {"$ref": "recordType"}},
"content" : {"$ref": "contentType"},
"redirectURL" : {"type": "string"},
"headersSize" : {"type": "integer"},
"bodySize" : {"type": "integer"}
}
}
};
var cookieType = {
"cookieType": {
"id": "cookieType",
"description": "Cookie description.",
"properties": {
"name": {"type": "string"},
"value": {"type": "string"},
"path": {"type": "string", "optional": true},
"domain" : {"type": "string", "optional": true},
"expires" : {"type": "string", "optional": true},
"httpOnly" : {"type": "boolean", "optional": true}
}
}
}
var postDataType = {
"postDataType": {
"id": "postDataType",
"description": "Posted data info.",
"optional": true,
"properties": {
"mimeType": {"type": "string"},
"text": {"type": "string", "optional": true},
"params": {
"type": "array",
"optional": true,
"properties": {
"name": {"type": "string"},
"value": {"type": "string", "optional": true},
"fileName": {"type": "string", "optional": true},
"contentType": {"type": "string", "optional": true}
}
}
}
}
};
var contentType = {
"contentType": {
"id": "contentType",
"description": "Response content",
"properties": {
"size": {"type": "integer"},
"compression": {"type": "integer", "optional": true},
"mimeType": {"type": "string"},
"text": {"type": "string", "optional": true}
}
}
};
var cacheType = {
"cacheType": {
"id": "cacheType",
"description": "Info about a response coming from the cache.",
"properties": {
"beforeRequest": {"$ref": "cacheEntryType"},
"afterRequest": {"$ref": "cacheEntryType"}
}
}
};
var cacheEntryType = {
"cacheEntryType": {
"id": "cacheEntryType",
"optional": true,
"description": "Info about cache entry.",
"properties": {
"expires": {"type": "string", optional: "true"},
"lastAccess": {"type": "string"},
"eTag": {"type": "string"},
"hitCount": {"type": "integer"}
}
}
};
var timingsType = {
"timingsType": {
"id": "timingsType",
"description": "Info about request-response timing.",
"properties": {
"dns": {"type": "integer", "min": -1},
"connect": {"type": "integer", "min": -1},
"blocked": {"type": "integer", "min": -1},
"send": {"type": "integer", "min": -1},
"wait": {"type": "integer", "min": -1},
"receive": {"type": "integer", "min": -1}
}
}
};
// ************************************************************************************************
// Helper schema object
function Schema() {}
Schema.prototype =
{
registerType: function()
{
var doIt = function(my, obj){
for (name in obj) {
if (obj.hasOwnProperty(name) && name != "prototype") {
my[name] = obj[name];
}
}
}
var that = this;
for(i=0; i < arguments.length; i +=1) {
doIt(that, arguments[i]);
};
}
};
// ************************************************************************************************
// Registration
// Register all defined types into the final schema object.
var schema = new Schema();
schema.registerType(
logType,
creatorType,
browserType,
pageType,
pageTimingsType,
entryType,
requestType,
recordType,
responseType,
postDataType,
contentType,
cacheType,
cacheEntryType,
timingsType
);
// ************************************************************************************************
/**
* HAR file validation using Dojo:
*
* dojo.require("dojox.json.schema");
* dojo.require("dojox.json.ref");
*
* var inputData = ... // Input HAR data (JSON string)
* var resolvedSchema = dojox.json.ref.resolveJson(schema);
* var results = dojox.json.schema.validate(inputData, resolvedSchema.logType);
* if (results.valid)
* console.log("Input data OK!");
* else
* console.log(results.errors);
*/