├── .gitignore ├── LICENSE.md ├── README.md ├── example.jsx └── http.jsx /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.swp 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Burak Tamtürk 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Simple HTTP client library written in JavaScript to be used in Adobe programs (such as Indesign). 2 | 3 | I writed one because i needed it. Examples can be found in examples.jsx file, pull requests are welcome. 4 | 5 | # The MIT License (MIT) 6 | 7 | Copyright (c) 2015 Burak Tamtürk 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 14 | -------------------------------------------------------------------------------- /example.jsx: -------------------------------------------------------------------------------- 1 | 2 | #include "http.jsx" 3 | 4 | // GET example 5 | 6 | var data = $http({ 7 | method: 'GET', 8 | url: 'http://date.jsontest.com/' 9 | }); 10 | 11 | alert(data.payload.time); 12 | 13 | // GET with query string example 14 | 15 | data = $http({ 16 | method: 'GET', 17 | url: 'http://md5.jsontest.com/?text=example_text' 18 | }); 19 | 20 | alert(data.payload.md5); 21 | 22 | // GET with custom headers 23 | data = $http({ 24 | method: 'GET', 25 | url: 'http://httpbin.org/headers', 26 | headers: {'Hello': 'World'} 27 | }); 28 | 29 | alert(data.payload.headers.Hello); 30 | 31 | // POST example 32 | var data = $http({ 33 | method: 'POST', 34 | payload: 'email=burak@tamturk.in', 35 | url: 'http://httpbin.org/post', 36 | headers: { 37 | 'Content-Type': 'application/x-www-form-urlencoded' 38 | } 39 | }); 40 | 41 | alert(data.payload.form.email); 42 | 43 | // POST Json example (note, payload will be converted to json string and Content-Type: application/json will be automatically added to your request) 44 | var data = $http({ 45 | method: 'POST', 46 | payload: {email: 'burak@tamturk.in'}, 47 | url: 'http://httpbin.org/post' 48 | }); 49 | 50 | alert(data.payload.json.email); 51 | 52 | -------------------------------------------------------------------------------- /http.jsx: -------------------------------------------------------------------------------- 1 |  2 | 3 | var $http = (function() { 4 | 5 | // JSON library for javascript, I took from somewhere but i don't remember where i took it. 6 | "object"!=typeof JSON&&(JSON={}),function(){"use strict";function f(t){return 10>t?"0"+t:t}function this_value(){return this.valueOf()}function quote(t){return escapable.lastIndex=0,escapable.test(t)?'"'+t.replace(escapable,function(t){var e=meta[t];return"string"==typeof e?e:"\\u"+("0000"+t.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+t+'"'}function str(t,e){var n,r,o,u,f,i=gap,a=e[t];switch(a&&"object"==typeof a&&"function"==typeof a.toJSON&&(a=a.toJSON(t)),"function"==typeof rep&&(a=rep.call(e,t,a)),typeof a){case"string":return quote(a);case"number":return isFinite(a)?String(a):"null";case"boolean":case"null":return String(a);case"object":if(!a)return"null";if(gap+=indent,f=[],"[object Array]"===Object.prototype.toString.apply(a)){for(u=a.length,n=0;u>n;n+=1)f[n]=str(n,a)||"null";return o=0===f.length?"[]":gap?"[\n"+gap+f.join(",\n"+gap)+"\n"+i+"]":"["+f.join(",")+"]",gap=i,o}if(rep&&"object"==typeof rep)for(u=rep.length,n=0;u>n;n+=1)"string"==typeof rep[n]&&(r=rep[n],o=str(r,a),o&&f.push(quote(r)+(gap?": ":":")+o));else for(r in a)Object.prototype.hasOwnProperty.call(a,r)&&(o=str(r,a),o&&f.push(quote(r)+(gap?": ":":")+o));return o=0===f.length?"{}":gap?"{\n"+gap+f.join(",\n"+gap)+"\n"+i+"}":"{"+f.join(",")+"}",gap=i,o}}"function"!=typeof Date.prototype.toJSON&&(Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null},Boolean.prototype.toJSON=this_value,Number.prototype.toJSON=this_value,String.prototype.toJSON=this_value);var cx,escapable,gap,indent,meta,rep;"function"!=typeof JSON.stringify&&(escapable=/[\\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,meta={"\b":"\\b"," ":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},JSON.stringify=function(t,e,n){var r;if(gap="",indent="","number"==typeof n)for(r=0;n>r;r+=1)indent+=" ";else"string"==typeof n&&(indent=n);if(rep=e,e&&"function"!=typeof e&&("object"!=typeof e||"number"!=typeof e.length))throw new Error("JSON.stringify");return str("",{"":t})}),"function"!=typeof JSON.parse&&(cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,JSON.parse=function(text,reviver){function walk(t,e){var n,r,o=t[e];if(o&&"object"==typeof o)for(n in o)Object.prototype.hasOwnProperty.call(o,n)&&(r=walk(o,n),void 0!==r?o[n]=r:delete o[n]);return reviver.call(t,e,o)}var j;if(text=String(text),cx.lastIndex=0,cx.test(text)&&(text=text.replace(cx,function(t){return"\\u"+("0000"+t.charCodeAt(0).toString(16)).slice(-4)})),/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,"")))return j=eval("("+text+")"),"function"==typeof reviver?walk({"":j},""):j;throw new SyntaxError("JSON.parse")})}(); 7 | 8 | return function(config) { 9 | var url = (/^(.*):\/\/([A-Za-z0-9\-\.]+):?([0-9]+)?(.*)$/).exec(config.url); 10 | if(url == null) { 11 | throw "unable to parse URL"; 12 | } 13 | 14 | url = { 15 | scheme: url[1], 16 | host: url[2], 17 | port: url[3] || (url[1] == "https" ? 443 : 80), 18 | path: url[4] 19 | }; 20 | 21 | if(url.scheme != "http") { 22 | throw "non-http url's not supported yet!"; 23 | } 24 | 25 | var s = new Socket(); 26 | 27 | if(!s.open(url.host + ':' + url.port, 'binary')) { 28 | throw 'can\'t connect to ' + url.host + ':' + url.port; 29 | } 30 | 31 | var method = config.method || 'GET'; 32 | 33 | var request = method + ' ' + url.path + " HTTP/1.0\r\nConnection: close\r\nHost: " + url.host; 34 | var header; 35 | 36 | if(config.payload) { 37 | if(typeof config.payload === 'object') { 38 | config.payload = JSON.stringify(config.payload); 39 | (config.headers = config.headers || {})["Content-Type"] = "application/json"; 40 | } 41 | 42 | (config.headers = config.headers || {})["Content-Length"] = config.payload.length; 43 | } 44 | 45 | for(header in (config.headers || {})) { 46 | request += "\r\n" + header + ': ' + config.headers[header] ; 47 | } 48 | 49 | s.write(request+"\r\n\r\n"); 50 | 51 | if(config.payload) { 52 | s.write(config.payload); 53 | } 54 | 55 | var data, response, payload, http = {}; 56 | 57 | data = s.read(); 58 | while(!s.eof) { 59 | data += s.read(); 60 | } 61 | 62 | var response = data.indexOf("\r\n\r\n"); 63 | if(response == -1) { 64 | throw "No HTTP payload found in the response!"; 65 | } 66 | 67 | payload = data.substr(response + 4); 68 | response = data.substr(0, response); 69 | 70 | var http = /^HTTP\/([\d\.?]+) (\d+) (.*)\r/.exec(response), header; 71 | if(http == null) { 72 | throw "No HTTP payload found in the response!"; 73 | } 74 | 75 | http = { 76 | ver: Number(http[1]), 77 | status: Number(http[2]), 78 | statusMessage: http[3], 79 | headers: {} 80 | }; 81 | 82 | var httpregex = /(.*): (.*)\r/g; 83 | 84 | while(header = httpregex.exec(response)) { 85 | http.headers[header[1]] = header[2]; 86 | } 87 | 88 | var contenttype = (http.headers["Content-Type"] || http.headers["content-type"] || '').split(";"); 89 | var charset = config.charset || (contenttype[1] ? /charset=(.*)/.exec(contenttype[1])[1] : null); 90 | if(charset) payload = payload.toString(charset); 91 | contenttype = contenttype[0]; 92 | 93 | if(config.forcejson || contenttype == "application/json") { 94 | http.payload = JSON.parse(payload); 95 | } else { 96 | http.payload = payload; 97 | } 98 | 99 | return http; 100 | }; 101 | })(); 102 | 103 | --------------------------------------------------------------------------------