forked from JCMais/node-libcurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12-debug-callback.js
63 lines (52 loc) · 1.7 KB
/
12-debug-callback.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
/**
* Copyright (c) Jonathan Cardoso Machado. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Example of how to use the DEBUGFUNCTION option.
* This can be used to change the behavior of the VERBOSE option.
* You can for instance use this to save that log somewhere else.
*/
const { Curl, CurlInfoDebug } = require('../dist')
const curl = new Curl()
const url = process.argv[2] || 'http://www.google.com'
const EOL = process.platform === 'win32' ? '\r\n' : '\n'
const debugCallback = (infoType, content) => {
let text = ''
const contentString = content.toString('utf8')
switch (infoType) {
case CurlInfoDebug.Text:
text = contentString
break
case CurlInfoDebug.DataIn:
text = '-- RECEIVING DATA: ' + EOL + contentString
break
case CurlInfoDebug.DataOut:
text = '-- SENDING DATA: ' + EOL + contentString
break
case CurlInfoDebug.HeaderIn:
text = '-- RECEIVING HEADER: ' + EOL + contentString
break
case CurlInfoDebug.HeaderOut:
text = '-- SENDING HEADER: ' + EOL + contentString
break
case CurlInfoDebug.SslDataIn:
text = '-- RECEIVING SSL DATA: ' + EOL + contentString
break
case CurlInfoDebug.SslDataOut:
text = '-- SENDING SSL DATA: ' + EOL + contentString
break
}
// we are just printing it to stdout.
console.log(text)
// must return 0, otherwise libcurl will abort the request.
return 0
}
curl.setOpt('URL', url)
curl.setOpt('VERBOSE', true)
curl.setOpt('DEBUGFUNCTION', debugCallback)
curl.on('end', curl.close.bind(curl))
curl.on('error', curl.close.bind(curl))
curl.perform()