Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / phantomjs / lib / phantom / examples / netsniff.coffee
1 if not Date::toISOString
2   Date::toISOString = ->
3     pad = (n) ->
4       if n < 10 then '0' + n else n
5     ms = (n) ->
6       if n < 10 then '00' + n else (if n < 100 then '0' + n else n)
7     @getFullYear() + '-' +
8     pad(@getMonth() + 1) + '-' +
9     pad(@getDate()) + 'T' +
10     pad(@getHours()) + ':' +
11     pad(@getMinutes()) + ':' +
12     pad(@getSeconds()) + '.' +
13     ms(@getMilliseconds()) + 'Z'
14
15 createHAR = (address, title, startTime, resources) ->
16   entries = []
17
18   resources.forEach (resource) ->
19     request = resource.request
20     startReply = resource.startReply
21     endReply = resource.endReply
22
23     if not request or not startReply or not endReply
24       return
25
26     entries.push
27       startedDateTime: request.time.toISOString()
28       time: endReply.time - request.time
29       request:
30         method: request.method
31         url: request.url
32         httpVersion: 'HTTP/1.1'
33         cookies: []
34         headers: request.headers
35         queryString: []
36         headersSize: -1
37         bodySize: -1
38
39       response:
40         status: endReply.status
41         statusText: endReply.statusText
42         httpVersion: 'HTTP/1.1'
43         cookies: []
44         headers: endReply.headers
45         redirectURL: ''
46         headersSize: -1
47         bodySize: startReply.bodySize
48         content:
49           size: startReply.bodySize
50           mimeType: endReply.contentType
51
52       cache: {}
53       timings:
54         blocked: 0
55         dns: -1
56         connect: -1
57         send: 0
58         wait: startReply.time - request.time
59         receive: endReply.time - startReply.time
60         ssl: -1
61       pageref: address
62
63   log:
64     version: '1.2'
65     creator:
66       name: 'PhantomJS'
67       version: phantom.version.major + '.' + phantom.version.minor + '.' + phantom.version.patch
68
69     pages: [
70       startedDateTime: startTime.toISOString()
71       id: address
72       title: title
73       pageTimings:
74         onLoad: page.endTime - page.startTime
75     ]
76     entries: entries
77
78 page = require('webpage').create()
79 system = require 'system'
80
81 if system.args.length is 1
82   console.log 'Usage: netsniff.coffee <some URL>'
83   phantom.exit 1
84 else
85   page.address = system.args[1]
86   page.resources = []
87
88   page.onLoadStarted = ->
89     page.startTime = new Date()
90
91   page.onResourceRequested = (req) ->
92     page.resources[req.id] =
93       request: req
94       startReply: null
95       endReply: null
96
97   page.onResourceReceived = (res) ->
98     if res.stage is 'start'
99       page.resources[res.id].startReply = res
100     if res.stage is 'end'
101       page.resources[res.id].endReply = res
102
103   page.open page.address, (status) ->
104     if status isnt 'success'
105       console.log 'FAIL to load the address'
106       phantom.exit(1)
107     else
108       page.endTime = new Date()
109       page.title = page.evaluate ->
110         document.title
111
112       har = createHAR page.address, page.title, page.startTime, page.resources
113       console.log JSON.stringify har, undefined, 4
114       phantom.exit()