Advertisement
Guest User

Untitled

a guest
Sep 19th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.31 KB | None | 0 0
  1. var util = require('util');
  2. var http = require('http');
  3. var fs = require('fs');
  4. var querystring = require('querystring');
  5. var winston = require('winston');
  6. var config = require('../config/config');
  7. const pHost = config.server_communications.dserver_ip;
  8. const pPort = config.server_communications.dserver_port;
  9.  
  10. function addSpaces(value) {
  11. return value + new Array(16-value.length + 1).join(' ');
  12. }
  13.  
  14. module.exports = {
  15.  
  16. /* unused function */
  17. getControllerHTTP: function (pCommandJSON, pHost, pPort, pUsername, pPassword, callback) {
  18. //var username = "s1";
  19. //var password = "s1";
  20. var auth = "Basic " + new Buffer(pUsername + ":" + pPassword).toString("base64");
  21.  
  22. //var post_data = querystring.stringify({
  23. // pCommandName : ''
  24. //});
  25. var post_data = querystring.stringify(pCommandJSON);
  26. winston.info("HTTP helper getControllerHTTP HOST: " + pHost);
  27.  
  28. var post_options = {
  29. host: pHost,
  30. port: pPort,
  31. path: '/',
  32. method: 'POST',
  33. headers: {
  34. 'Content-Type': 'application/x-www-form-urlencoded',
  35. 'Content-Length': Buffer.byteLength(post_data),
  36. 'Authorization': auth,
  37. 'Cache-Control': 'no-cache'
  38. }
  39. };
  40. console.log("auth: " + auth);
  41.  
  42. var post_req = http.request(post_options, function (res1) {
  43. //res.setEncoding('utf8');
  44. res1.on('data', function (chunk) {
  45. //console.log('Response: ' + util.inspect(chunk.toString()));
  46. var chunkStr = chunk.toString();
  47. var ind1 = chunkStr.indexOf("<h1>") + 4;
  48. var ind2 = chunkStr.indexOf("</h1>");
  49. var h1Resp = chunkStr;//.substring(ind1, ind2);
  50. callback(null, {
  51. h1_resp: h1Resp
  52. });
  53. });
  54. });
  55.  
  56. post_req.on('error', function (err) {
  57. console.log("error on requesting devices state");
  58. callback(null, {error: err});
  59. });
  60.  
  61. post_req.write(post_data);
  62. post_req.end();
  63. },
  64.  
  65. sendControllerCommandHTTP: function (pCommandJSON, deviceSerialNumber, callback) {
  66. var h1Resp = {fromServer: 'firmware:2.3.76WDT\r\nreleased:2016/08/03\r\npartnumb:0123456789ABCDEF\r\nserialnu:0000000000000100\r\n'};
  67. return callback(null, {h1_resp: h1Resp});
  68.  
  69. var post_data = querystring.stringify({serial_number: addSpaces(deviceSerialNumber), command:pCommandJSON});
  70. var post_options = {
  71. host: pHost,
  72. port: pPort,
  73. path: '/',
  74. method: 'POST',
  75. headers: {
  76. 'Content-Type': 'application/x-www-form-urlencoded',
  77. 'Content-Length': Buffer.byteLength(post_data),
  78. 'Cache-Control': 'no-cache'
  79. }
  80. };
  81.  
  82. /*callback = function(response) {
  83. var str = ''
  84. response.on('data', function (chunk) {
  85. str += chunk;
  86. });
  87.  
  88. response.on('end', function () {
  89. console.log(str);
  90. });
  91. }
  92.  
  93. var req = http.request(options, callback);
  94. req.write(post_data);
  95. req.end();*/
  96. var post_req = http.request(post_options, function (res1) {
  97. //res.setEncoding('utf8');
  98. res1.on('data', function (chunk) {
  99. //console.log('Response: ' + util.inspect(chunk.toString()));
  100. var chunkStr = chunk.toString();
  101. var ind1 = chunkStr.indexOf("<h1>") + 4;
  102. var ind2 = chunkStr.indexOf("</h1>");
  103. var h1Resp = chunkStr;//.substring(ind1, ind2);
  104. callback(null, {h1_resp: h1Resp});
  105. });
  106. });
  107. post_req.on('error', function (err) {
  108. console.log("error on requesting devices state");
  109. callback(null, {error: err});
  110. });
  111.  
  112. post_req.write(post_data);
  113. post_req.end();
  114. },
  115.  
  116. sendControllerCommandKeyHTTP: function (keyValue, deviceSerialNumber, callback) {
  117. var h1Resp = fs.readFileSync("PBFACU.CFG", "utf8");
  118. console.log("h1Resp body: " + h1Resp);
  119. return callback(null, {h1_resp: h1Resp});
  120.  
  121. var post_data = querystring.stringify({serial_number:deviceSerialNumber, key_value:keyValue});
  122. var post_options = {
  123. host: pHost,
  124. port: pPort,
  125. path: '/',
  126. method: 'POST',
  127. headers: {
  128. 'Content-Type': 'application/x-www-form-urlencoded',
  129. 'Content-Length': Buffer.byteLength(post_data),
  130. 'Cache-Control': 'no-cache'
  131. }
  132. };
  133.  
  134. /*callback = function(response) {
  135. var str = ''
  136. response.on('data', function (chunk) {
  137. str += chunk;
  138. });
  139.  
  140. response.on('end', function () {
  141. console.log(str);
  142. });
  143. }
  144.  
  145. var req = http.request(options, callback);
  146. req.write(post_data);
  147. req.end();*/
  148. var post_req = http.request(post_options, function (res1) {
  149. //res.setEncoding('utf8');
  150. res1.on('data', function (chunk) {
  151. //console.log('Response: ' + util.inspect(chunk.toString()));
  152. var chunkStr = chunk.toString();
  153. var ind1 = chunkStr.indexOf("<h1>") + 4;
  154. var ind2 = chunkStr.indexOf("</h1>");
  155. var h1Resp = chunkStr;//.substring(ind1, ind2);
  156. callback(null, {h1_resp: h1Resp});
  157. });
  158. });
  159. post_req.on('error', function (err) {
  160. console.log("error on requesting devices state");
  161. callback(null, {error: err});
  162. });
  163. post_req.write(post_data);
  164. post_req.end();
  165. },
  166.  
  167. getControllerSetupCommandHTTP: function (deviceSerialNumber, callback) {
  168. /* var h1Resp = fs.readFileSync("PBFACU.CFG", "utf8");
  169. console.log("h1Resp body: " + h1Resp);
  170. return callback(null, {h1_resp: h1Resp}); */
  171.  
  172. var configFileName = "PBFACU.CFG";
  173. var pCommand = "GET_CONFIG";
  174. var post_data = querystring.stringify({serial_number: addSpaces(deviceSerialNumber), command:pCommand, file_name:configFileName});
  175. var post_options = {
  176. host: pHost,
  177. port: pPort,
  178. path: '/',
  179. method: 'POST',
  180. headers: {
  181. 'Content-Type': 'application/x-www-form-urlencoded',
  182. 'Content-Length': Buffer.byteLength(post_data),
  183. 'Cache-Control': 'no-cache'
  184. }
  185. };
  186.  
  187. var post_req = http.request(post_options, function (res1) {
  188. res1.on('data', function (chunk) {
  189. var chunkStr = chunk.toString();
  190. var ind1 = chunkStr.indexOf("<h1>") + 4;
  191. var ind2 = chunkStr.indexOf("</h1>");
  192. var h1Resp = chunkStr;
  193. callback(null, {h1_resp: h1Resp});
  194. });
  195. });
  196. post_req.on('error', function (err) {
  197. console.log("error on requesting devices state");
  198. callback(null, {error: err});
  199. });
  200. post_req.write(post_data);
  201. post_req.end();
  202. },
  203.  
  204. getControllerLogCommandHTTP: function (deviceSerialNumber, callback) {
  205. var pCommand = "GET_LOG";
  206. var post_data = querystring.stringify({serial_number: addSpaces(deviceSerialNumber), command:pCommand});
  207. var post_options = {
  208. host: pHost,
  209. port: pPort,
  210. path: '/',
  211. method: 'POST',
  212. headers: {
  213. 'Content-Type': 'application/x-www-form-urlencoded',
  214. 'Content-Length': Buffer.byteLength(post_data),
  215. 'Cache-Control': 'no-cache'
  216. }
  217. };
  218.  
  219. var post_req = http.request(post_options, function (res1) {
  220. var buffArray = [];
  221. res1.on('data', function (chunk) {
  222. buffArray.push(chunk.toString());
  223. });
  224. res1.on('end', function (res) {
  225. callback(null, {h1_resp: buffArray.join()});
  226. });
  227. });
  228. post_req.on('error', function (err) {
  229. console.log("error on requesting devices state");
  230. callback(null, {error: err});
  231. });
  232. post_req.write(post_data);
  233. post_req.end();
  234. //var h1Resp = "2016/04/22-13:38:19 Log created\r\n2016/04/22-13:38:19 Powered up\r\n2016/04/22-13:38:19 Last shutdown time 1970/01/01-00:00:00\r\n2016/04/22-13:38:29 ************************************************************";
  235. //callback(null, {h1_resp: h1Resp});
  236. },
  237.  
  238. getControllerFileCommandHTTP: function (configFileName, deviceSerialNumber, callback) {
  239. var pCommand = "GET_CONFIG";
  240. var post_data = querystring.stringify({serial_number: addSpaces(deviceSerialNumber), command:pCommand, file_name:configFileName});
  241. var post_options = {
  242. host: pHost,
  243. port: pPort,
  244. path: '/',
  245. method: 'POST',
  246. headers: {
  247. 'Content-Type': 'application/x-www-form-urlencoded',
  248. 'Content-Length': Buffer.byteLength(post_data),
  249. 'Cache-Control': 'no-cache'
  250. }
  251. };
  252.  
  253. var post_req = http.request(post_options, function (res1) {
  254. var buffArray = new Buffer('');
  255. res1.on('data', function (chunk) {
  256. buffArray = Buffer.concat([buffArray,chunk]);
  257. });
  258. res1.on('end', function (res) {
  259. callback(null, {h1_resp: buffArray});
  260. });
  261. });
  262.  
  263. post_req.on('error', function (err) {
  264. console.log("error on requesting devices state");
  265. callback(null, {error: err});
  266. });
  267. post_req.write(post_data);
  268. post_req.end();
  269. },
  270.  
  271. uploadControllerHTTP: function (pFileName, pMimeType, pOriginalFileName, pDeviceSerialNumber, callback) {
  272. //var fs = require("fs");
  273.  
  274. var data = null; // надо правильно обрабаотывать ошибку если файл не найден!
  275. try {
  276. data = fs.readFileSync("upload/" + pFileName);
  277. } catch (e) {
  278. console.error("File not found");
  279. return callback(e, {h1_resp: ''});
  280. }
  281. var client;
  282. var request;
  283.  
  284. /* As per http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html */
  285. var crlf = "\r\n",
  286. boundary = '--------120315120542026', // Boundary: "--" + up to 70 ASCII chars + "\r\n"
  287. delimiter = "--" + boundary,
  288. preamble = "", // ignored. a good place for non-standard mime info
  289. epilogue = ""; // ignored. a good place to place a checksum, etc
  290. //application/octet-stream
  291. var qOriginalFileName = pOriginalFileName+ " " + addSpaces(pDeviceSerialNumber);
  292. console.log("uploadHTTP pOriginalFileName: " + qOriginalFileName);
  293. var headers = [
  294. 'Content-Disposition: form-data; name="userfile"; filename="' + qOriginalFileName + '"' + crlf,
  295. //'Content-Type: application/octet-stream' + crlf,
  296. //'Content-Type: ' + pMimeType + crlf
  297. 'Content-Type: application/octet-stream' + crlf
  298. ],
  299. //bodyPart = headers.join('') + crlf + data.toString(),
  300. //encapsulation = delimiter + crlf + bodyPart,
  301. closeDelimiter = delimiter + "--",
  302. multipartBody; // = preamble + encapsulation + closeDelimiter + epilogue + crlf /* node doesn't add this */;
  303. multipartBody = Buffer.concat([
  304. new Buffer(preamble + delimiter + crlf + headers.join('') + crlf),
  305. data,
  306. new Buffer(crlf + closeDelimiter + epilogue + crlf)]
  307. );
  308. //console.log("multipartBody: " + multipartBody);
  309. console.log("multipartBody length: " + multipartBody.length);
  310.  
  311.  
  312. //client = http.request(pPort, pHost);
  313. /* headers copied from a browser request logged in wireshark */
  314. //'POST', '/cgi-bin/upload',
  315.  
  316. var post_options = {
  317. host: pHost,
  318. port: pPort,
  319. path: '/cgi-bin/upload',
  320. method: 'POST',
  321. headers: {
  322. 'Accept': 'text/html, */*',
  323. 'Accept-Encoding': 'identity',
  324. 'User-Agent': 'Mozilla/3.0 (compatible; Indy Library)',//'Node.JS',
  325. 'Connection': 'keep-alive',
  326. 'Content-Type': 'multipart/form-data; boundary=' + boundary,
  327. 'Content-Length': multipartBody.length
  328. }
  329. };
  330. console.log('post_options:'+ JSON.stringify(post_options));
  331. var request1 = http.request(post_options, function (res) {
  332. //console.log('STATUS: ${res.statusCode}');
  333. //console.log('HEADERS: ${JSON.stringify(res.headers)}');
  334. res.setEncoding('utf8');
  335. var h1Resp = "H1 response not found";
  336. res.on('data', function (chunk) {
  337. var chunkStr = chunk.toString();
  338. //console.log("onData chunkStr: " + chunkStr);
  339. var ind1 = chunkStr.indexOf("<h1>");
  340. var ind2 = chunkStr.indexOf("</h1>");
  341. if (ind1 != -1) {
  342. h1Resp = chunkStr.substring(ind1 + 4, ind2);
  343. }
  344. return callback(null, {
  345. h1_resp: "File has been uploaded"
  346. });
  347. });
  348. }
  349. );
  350.  
  351. request1.on('error', function (err) {
  352. console.log("Error event occured. Err: " + util.inspect(err));
  353. callback(err, {h1_resp: ''});
  354. });
  355.  
  356. request1.write(multipartBody);
  357. request1.end();
  358. },
  359.  
  360. getControllersStatusesListHTTP: function (pHost, pPort, callback) {
  361. var pCommand = "GET_CONTROLLERS_STATUSES";
  362. var post_data = querystring.stringify({command:pCommand});
  363. var post_options = {
  364. host: pHost,
  365. port: pPort,
  366. path: '/',
  367. method: 'POST',
  368. headers: {
  369. 'Content-Type': 'application/x-www-form-urlencoded',
  370. 'Content-Length': Buffer.byteLength(post_data),
  371. 'Cache-Control': 'no-cache'
  372. }
  373. };
  374.  
  375. var post_req = http.request(post_options, function (res1) {
  376. var buffArray = [];
  377. res1.on('data', function (chunk) {
  378. buffArray.push(chunk.toString());
  379. });
  380. res1.on('end', function (res) {
  381. callback(null, {h1_resp: buffArray.join()});
  382. });
  383. });
  384. post_req.on('socket', function (socket) {
  385. socket.setTimeout(5000);
  386. socket.on('timeout', function() {
  387. post_req.abort();
  388. callback(null, {error: "err"});
  389. });
  390. });
  391. post_req.on('error', function (err) {
  392. console.log("error on requesting devices state");
  393. callback(null, {error: err});
  394. });
  395. post_req.write(post_data);
  396. post_req.end();
  397. //var h1Resp = "2016/04/22-13:38:19 Log created\r\n2016/04/22-13:38:19 Powered up\r\n2016/04/22-13:38:19 Last shutdown time 1970/01/01-00:00:00\r\n2016/04/22-13:38:29 ************************************************************";
  398. //callback(null, {h1_resp: h1Resp});
  399. },
  400.  
  401. uploadControllerConfigFileHTTP: function (pFileName, pMimeType, pDeviceSerialNumber, pData, callback) {
  402. return callback(null, {
  403. h1_resp: "File has been uploaded"
  404. });
  405.  
  406. //var fs = require("fs");
  407. var data = pData; // надо правильно обрабаотывать ошибку если файл не найден!
  408. /*try {
  409. data = fs.readFileSync("upload/" + pFileName);
  410. } catch (e) {
  411. console.error("File not found");
  412. return callback(e, {h1_resp: ''});
  413. }*/
  414. var client;
  415. var request;
  416. //var saveConfigCommand = "SAVE_CONFIG\r\n";
  417. /* As per http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html */
  418. var crlf = "\r\n",
  419. boundary = '--------120315120542026', // Boundary: "--" + up to 70 ASCII chars + "\r\n"
  420. delimiter = "--" + boundary,
  421. preamble = "", // ignored. a good place for non-standard mime info
  422. epilogue = ""; // ignored. a good place to place a checksum, etc
  423. //application/octet-stream
  424. var qOriginalFileName = pFileName+ " " + addSpaces(pDeviceSerialNumber);
  425. console.log("uploadHTTP pOriginalFileName: " + qOriginalFileName);
  426. var headers = [
  427. 'Content-Disposition: form-data; name="userfile"; filename="' + qOriginalFileName + '"' + crlf,
  428. //'Content-Type: application/octet-stream' + crlf,
  429. //'Content-Type: ' + pMimeType + crlf
  430. 'Content-Type: application/octet-stream' + crlf
  431. ],
  432. //bodyPart = headers.join('') + crlf + data.toString(),
  433. //encapsulation = delimiter + crlf + bodyPart,
  434. closeDelimiter = delimiter + "--",
  435. multipartBody; // = preamble + encapsulation + closeDelimiter + epilogue + crlf /* node doesn't add this */;
  436. multipartBody = Buffer.concat([
  437. new Buffer(preamble + delimiter + crlf + headers.join('') + crlf),
  438. new Buffer(data),
  439. //new Buffer(saveConfigCommand),
  440. new Buffer(crlf + closeDelimiter + epilogue + crlf)]
  441. );
  442. //console.log("multipartBody: " + multipartBody);
  443. console.log("multipartBody length: " + multipartBody.length);
  444.  
  445.  
  446. //client = http.request(pPort, pHost);
  447. /* headers copied from a browser request logged in wireshark */
  448. //'POST', '/cgi-bin/upload',
  449.  
  450. var post_options = {
  451. host: pHost,
  452. port: pPort,
  453. path: '/cgi-bin/upload',
  454. method: 'POST',
  455. headers: {
  456. 'Accept': 'text/html, */*',
  457. 'Accept-Encoding': 'identity',
  458. 'User-Agent': 'Mozilla/3.0 (compatible; Indy Library)',//'Node.JS',
  459. 'Connection': 'keep-alive',
  460. 'Content-Type': 'multipart/form-data; boundary=' + boundary,
  461. 'Content-Length': multipartBody.length
  462. }
  463. };
  464. console.log('post_options:'+ JSON.stringify(post_options));
  465. var request1 = http.request(post_options, function (res) {
  466. //console.log('STATUS: ${res.statusCode}');
  467. //console.log('HEADERS: ${JSON.stringify(res.headers)}');
  468. res.setEncoding('utf8');
  469. var h1Resp = "H1 response not found";
  470. res.on('data', function (chunk) {
  471. var chunkStr = chunk.toString();
  472. //console.log("onData chunkStr: " + chunkStr);
  473. var ind1 = chunkStr.indexOf("<h1>");
  474. var ind2 = chunkStr.indexOf("</h1>");
  475. if (ind1 != -1) {
  476. h1Resp = chunkStr.substring(ind1 + 4, ind2);
  477. }
  478. return callback(null, {
  479. h1_resp: "File has been uploaded"
  480. });
  481. });
  482. }
  483. );
  484.  
  485. request1.on('error', function (err) {
  486. console.log("Error event occured. Err: " + util.inspect(err));
  487. callback(err, {h1_resp: ''});
  488. });
  489.  
  490. request1.write(multipartBody);
  491. request1.end();
  492. },
  493.  
  494. uploadControllerFileHTTP: function (pFileName, pMimeType, pDeviceSerialNumber, pData, callback) {
  495. var data = pData; // надо правильно обрабаотывать ошибку если файл не найден!
  496. var client;
  497. var request;
  498. //var saveConfigCommand = "SAVE_CONFIG\r\n";
  499. /* As per http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html */
  500. var crlf = "\r\n",
  501. boundary = '--------120315120542026', // Boundary: "--" + up to 70 ASCII chars + "\r\n"
  502. delimiter = "--" + boundary,
  503. preamble = "", // ignored. a good place for non-standard mime info
  504. epilogue = ""; // ignored. a good place to place a checksum, etc
  505. //application/octet-stream
  506. var qOriginalFileName = pFileName+ " " + addSpaces(pDeviceSerialNumber);
  507. console.log("uploadHTTP pOriginalFileName: " + qOriginalFileName);
  508. var headers = [
  509. 'Content-Disposition: form-data; name="userfile"; filename="' + qOriginalFileName + '"' + crlf,
  510. //'Content-Type: application/octet-stream' + crlf,
  511. //'Content-Type: ' + pMimeType + crlf
  512. 'Content-Type: application/octet-stream' + crlf
  513. ],
  514. //bodyPart = headers.join('') + crlf + data.toString(),
  515. //encapsulation = delimiter + crlf + bodyPart,
  516. closeDelimiter = delimiter + "--",
  517. multipartBody = ""; // = preamble + encapsulation + closeDelimiter + epilogue + crlf /* node doesn't add this */;
  518. try {
  519. multipartBody = Buffer.concat([
  520. new Buffer(preamble + delimiter + crlf + headers.join('') + crlf),
  521. new Buffer(data),
  522. //new Buffer(saveConfigCommand),
  523. new Buffer(crlf + closeDelimiter + epilogue + crlf)]
  524. );
  525. } catch (e) {
  526. winston.error("Error on creating multipart body: " + e);
  527. }
  528. //console.log("multipartBody: " + multipartBody);
  529. console.log("multipartBody length: " + multipartBody.length);
  530.  
  531.  
  532. //client = http.request(pPort, pHost);
  533. /* headers copied from a browser request logged in wireshark */
  534. //'POST', '/cgi-bin/upload',
  535.  
  536. var post_options = {
  537. host: pHost,
  538. port: pPort,
  539. path: '/cgi-bin/upload',
  540. method: 'POST',
  541. headers: {
  542. 'Accept': 'text/html, */*',
  543. 'Accept-Encoding': 'identity',
  544. 'User-Agent': 'Mozilla/3.0 (compatible; Indy Library)',//'Node.JS',
  545. 'Connection': 'keep-alive',
  546. 'Content-Type': 'multipart/form-data; boundary=' + boundary,
  547. 'Content-Length': multipartBody.length
  548. }
  549. };
  550. console.log('post_options:'+ JSON.stringify(post_options));
  551. var request1 = http.request(post_options, function (res) {
  552. //console.log('STATUS: ${res.statusCode}');
  553. //console.log('HEADERS: ${JSON.stringify(res.headers)}');
  554. res.setEncoding('utf8');
  555. var h1Resp = "H1 response not found";
  556. res.on('data', function (chunk) {
  557. var chunkStr = chunk.toString();
  558. //console.log("onData chunkStr: " + chunkStr);
  559. var ind1 = chunkStr.indexOf("<h1>");
  560. var ind2 = chunkStr.indexOf("</h1>");
  561. if (ind1 != -1) {
  562. h1Resp = chunkStr.substring(ind1 + 4, ind2);
  563. }
  564. return callback(null, {
  565. h1_resp: "File has been uploaded"
  566. });
  567. });
  568. }
  569. );
  570.  
  571. request1.on('error', function (err) {
  572. console.log("Error event occured. Err: " + util.inspect(err));
  573. callback(err, {h1_resp: ''});
  574. });
  575.  
  576. request1.write(multipartBody);
  577. request1.end();
  578. },
  579.  
  580. /* unused function */
  581. uploadHTTP: function (pFileName, pUsername, pPassword, pHost, pPort, pMimeType, pOriginalFileName, callback) {
  582. /*console.log(pFileName);
  583. console.log(pUsername);
  584. console.log(pPassword);
  585. console.log(pHost);
  586. console.log(pPort);*/
  587.  
  588. var fs = require("fs");
  589. var data = null; // надо правильно обрабаотывать ошибку если файл не найден!
  590. try {
  591. data = fs.readFileSync("upload/" + pFileName);
  592. } catch (e) {
  593. console.error("File not found");
  594. return callback(e, {h1_resp: ''});
  595. }
  596. var client;
  597. var request;
  598. //var data = "4090440380500854 46066184 5028602486509006 618927954312334113874277740948824551711527614232";
  599. var auth = "Basic " + new Buffer(pUsername + ":" + pPassword).toString("base64");
  600.  
  601. /* As per http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html */
  602. var crlf = "\r\n",
  603. boundary = '--------120315120542026', // Boundary: "--" + up to 70 ASCII chars + "\r\n"
  604. delimiter = "--" + boundary,
  605. preamble = "", // ignored. a good place for non-standard mime info
  606. epilogue = ""; // ignored. a good place to place a checksum, etc
  607. //application/octet-stream
  608. console.log("uploadHTTP pOriginalFileName: " + pOriginalFileName);
  609. var headers = [
  610. 'Content-Disposition: form-data; name="userfile"; filename="' + pOriginalFileName + '"' + crlf,
  611. //'Content-Type: application/octet-stream' + crlf,
  612. //'Content-Type: ' + pMimeType + crlf
  613. 'Content-Type: application/octet-stream' + crlf
  614. ],
  615. //bodyPart = headers.join('') + crlf + data.toString(),
  616. //encapsulation = delimiter + crlf + bodyPart,
  617. closeDelimiter = delimiter + "--",
  618. multipartBody; // = preamble + encapsulation + closeDelimiter + epilogue + crlf /* node doesn't add this */;
  619. multipartBody = Buffer.concat([
  620. new Buffer(preamble + delimiter + crlf + headers.join('') + crlf),
  621. data,
  622. new Buffer(crlf + closeDelimiter + epilogue + crlf)]
  623. );
  624. //console.log("multipartBody: " + multipartBody);
  625. console.log("multipartBody length: " + multipartBody.length);
  626.  
  627.  
  628. //client = http.request(pPort, pHost);
  629. /* headers copied from a browser request logged in wireshark */
  630. //'POST', '/cgi-bin/upload',
  631.  
  632. var post_options = {
  633. host: pHost,
  634. port: pPort,
  635. path: '/cgi-bin/upload',
  636. method: 'POST',
  637. headers: {
  638. 'Accept': 'text/html, */*',
  639. 'Accept-Encoding': 'identity',
  640. 'User-Agent': 'Mozilla/3.0 (compatible; Indy Library)',//'Node.JS',
  641. 'Authorization': auth,
  642. 'Connection': 'keep-alive',
  643. 'Content-Type': 'multipart/form-data; boundary=' + boundary,
  644. 'Content-Length': multipartBody.length
  645. }
  646. };
  647.  
  648. var request1 = http.request(post_options, function (res) {
  649. //console.log('STATUS: ${res.statusCode}');
  650. //console.log('HEADERS: ${JSON.stringify(res.headers)}');
  651. res.setEncoding('utf8');
  652. var h1Resp = "H1 response not found";
  653. res.on('data', function (chunk) {
  654. var chunkStr = chunk.toString();
  655. //console.log("onData chunkStr: " + chunkStr);
  656. var ind1 = chunkStr.indexOf("<h1>");
  657. var ind2 = chunkStr.indexOf("</h1>");
  658. if (ind1 != -1) {
  659. h1Resp = chunkStr.substring(ind1 + 4, ind2);
  660. }
  661. return callback(null, {
  662. h1_resp: h1Resp
  663. });
  664. });
  665. /*res.on('end', function() {
  666. console.log('End of response event occured');
  667. return callback(null, {
  668. h1_resp: h1Resp
  669. });
  670. });*/
  671. /*res.on('error', function (err) {
  672. console.log("ERR123: " + err);
  673. return callback(err, {
  674. h1_resp: ''
  675. });
  676. });*/
  677. }
  678. );
  679. console.log("auth: " + auth);
  680.  
  681. request1.on('error', function (err) {
  682. console.log("Error event occured. Err: " + util.inspect(err));
  683. callback(err, {h1_resp: ''});
  684. });
  685.  
  686. /*request1.setTimeout(10000, function(err){
  687. console.log("REQUEST TIMEOUT");
  688. callback(err, {h1_resp: ''});
  689. });*/
  690.  
  691. /*request1.on('response', function (response) {
  692. console.log('response');
  693.  
  694. response.setEncoding('utf8');
  695.  
  696. response.on('data', function (chunk) {
  697. //console.log('Response: ' + util.inspect(chunk.toString()));
  698. var chunkStr = chunk.toString();
  699. var ind1 = chunkStr.indexOf("<h1>") + 4;
  700. var ind2 = chunkStr.indexOf("</h1>");
  701. var h1Resp = chunkStr.substring(ind1, ind2);
  702. callback(null, {
  703. h1_resp: h1Resp
  704. });
  705. });
  706.  
  707. response.on('end', function () {
  708. console.log("end");
  709. });
  710. });*/
  711.  
  712.  
  713. request1.write(multipartBody);
  714. request1.end();
  715. }
  716.  
  717. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement