var _ = require("lodash"); var $ = require("jquery"); var MessageUtils = { getContentType: function (message) { return this.get_first_header(message, /^Content-Type$/i); }, get_first_header: function (message, regex) { //FIXME: Cache Invalidation. if (!message._headerLookups) Object.defineProperty(message, "_headerLookups", { value: {}, configurable: false, enumerable: false, writable: false }); if (!(regex in message._headerLookups)) { var header; for (var i = 0; i < message.headers.length; i++) { if (!!message.headers[i][0].match(regex)) { header = message.headers[i]; break; } } message._headerLookups[regex] = header ? header[1] : undefined; } return message._headerLookups[regex]; }, match_header: function (message, regex) { var headers = message.headers; var i = headers.length; while (i--) { if (regex.test(headers[i].join(" "))) { return headers[i]; } } return false; }, getContentURL: function(flow, message){ if(message === flow.request){ message = "request"; } else if (message === flow.response){ message = "response"; } return "/flows/" + flow.id + "/" + message + "/content"; }, getContent: function(flow, message){ var url = MessageUtils.getContentURL(flow, message); return $.get(url); } }; var defaultPorts = { "http": 80, "https": 443 }; var RequestUtils = _.extend(MessageUtils, { pretty_host: function (request) { //FIXME: Add hostheader return request.host; }, pretty_url: function (request) { var port = ""; if (defaultPorts[request.scheme] !== request.port) { port = ":" + request.port; } return request.scheme + "://" + this.pretty_host(request) + port + request.path; } }); var ResponseUtils = _.extend(MessageUtils, {}); module.exports = { ResponseUtils: ResponseUtils, RequestUtils: RequestUtils, MessageUtils: MessageUtils };