Guest User

Untitled

a guest
Jan 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. // "httpRequest" [ url, body, headers ]
  2. // httpRequest = parseURI(method, uri);
  3.  
  4. function _parseQuery (uriObj) {
  5. var buf = [];
  6. if (! uriObj) return null;
  7. if (typeof uriObj === 'string') return uriObj;
  8. if (typeof uriObj === 'object') {
  9. if (uriObj.length && uriObj.length > 0) {
  10. uriObj.foreach(function (keyVal, i) {
  11. buf.push([ keyVal[0], encodeURIComponent(keyVal[1]) ].join('='));
  12. });
  13. return buf.join('&');
  14. }
  15. if (uriObj) {
  16. for (var key in uriObj) {
  17. buf.push([ key, encodeURIComponent(uriObj[key]) ].join('='));
  18. }
  19. return buf.join('&');
  20. }
  21. }
  22. return null;
  23. }
  24. function _parseURI1 (method) {
  25. if (method === 'POST') {
  26. if (this[0].match(/\?/)) {
  27. var buf = this[0].split(/\?/);
  28. this[0] = buf[0];
  29. this[1] = buf[1];
  30. }
  31. }
  32. }
  33. function _parseURI2 (method) {
  34. // this[1] == null 以外は、stringに直し、method === 'POST' -> body, method === 'GET' -> urlに追加
  35. var buf = _parseQuery(this[1]);
  36. if (method === 'POST') {
  37. this[1] = buf;
  38. } else {
  39. this[1] = null;
  40. this[0] = (buf) ? [ this[0], buf ].join((buf.match(/^\?/)) ? '' : '?') : this[0];
  41. }
  42. }
  43. function parseURI (method, uri) {
  44. if (! method) return { error : '"method" not found' };
  45. if (! uri) return { error : '"uri object" not found' };
  46.  
  47. method = (method.match(/^post/i)) ? 'POST' : 'GET';
  48.  
  49. var url, body = null, headers = {}, uriObj = [ url, body, headers ];
  50.  
  51. if (typeof uri === 'string') {
  52. uriObj[0] = uri;
  53. _parseURI1.apply(uriObj, [ method ]);
  54. return uriObj;
  55. }
  56.  
  57. if (! uri.length) return { error : 'uri must be "string" or "array"' };
  58. if (typeof uri[0] !== 'string') return { error : '"url" must be "string"' };
  59.  
  60. uriObj[0] = uri[0];
  61. _parseURI1.apply(uriObj, [ method ]);
  62.  
  63. uriObj[1] = uri[1] || uriObj[1];
  64. _parseURI2.apply(uriObj, [ method ]);
  65.  
  66. uriObj[2] = uri[2] || {};
  67. return uriObj;
  68. }
  69.  
  70. function test (resFunc, forecast) {
  71. resFunc = JSON.stringify(resFunc);
  72. forecast = JSON.stringify(forecast);
  73. console.log(
  74. (resFunc == forecast)
  75. ? [ 'success', resFunc ].join(" --- ")
  76. : [ '!failed', resFunc, forecast ].join(" --- ")
  77. );
  78. }
  79.  
  80. if (! Array.prototype.foreach) {
  81. Array.prototype.foreach = function (func) {
  82. foreach.apply(null, [ this, func ]);
  83. };
  84. }
  85. function foreach (arry, func) {
  86. var i = 0, len = arry.length;
  87. for (; i < len; i+=1) {
  88. func(arry[i], i);
  89. }
  90. }
  91.  
  92.  
  93. // TEST
  94.  
  95. console.log('method === "GET"');
  96. var method = 'GET';
  97. test(parseURI(), { error : '"method" not found' });
  98. test(parseURI(method), { error : '"uri object" not found' });
  99.  
  100. test(parseURI(method, "/tora"), [ '/tora', null, {} ]);
  101. test(parseURI(method, "/tora?q=abc"), [ '/tora?q=abc', null, {} ]);
  102.  
  103. test(parseURI(method, []), { error : 'uri must be "string" or "array"' });
  104.  
  105. test(parseURI(method, [ "/tora" ]), [ '/tora', null, {} ]);
  106. test(parseURI(method, [ "/tora?q=abc" ]), [ '/tora?q=abc', null, {} ]);
  107.  
  108. console.log('\narguments.length === 2');
  109. test(parseURI(method, [ "/tora", "q=abc&qq=xyz" ]), [ '/tora?q=abc&qq=xyz', null, {} ]);
  110. test(parseURI(method, [ "/tora", { q : 'abc', qq : 'xyz' } ]), [ '/tora?q=abc&qq=xyz', null, {} ]);
  111. test(parseURI(method, [ "/tora", [["q", "abc"],["qq", "xyz"]] ]), [ '/tora?q=abc&qq=xyz', null, {} ]);
  112. test(parseURI(method, [ "/tora?q=abc&qq=xyz", null ]), [ '/tora?q=abc&qq=xyz', null, {} ]);
  113.  
  114. console.log('\narguments.length === 3');
  115. var headers = { 'Connection' : 'keep-alive', 'X-header' : 'heaven' };
  116. test(parseURI(method, [ "/tora", "q=abc&qq=xyz", headers ]), [ '/tora?q=abc&qq=xyz', null, headers ]);
  117. test(parseURI(method, [ "/tora", { q : 'abc', qq : 'xyz' }, headers ]), [ '/tora?q=abc&qq=xyz', null, headers ]);
  118. test(parseURI(method, [ "/tora", [["q", "abc"],["qq", "xyz"]], headers ]), [ '/tora?q=abc&qq=xyz', null, headers ]);
  119. test(parseURI(method, [ "/tora?q=abc&qq=xyz", null, headers ]), [ '/tora?q=abc&qq=xyz', null, headers ]);
  120.  
  121.  
  122. console.log('\nmethod === "POST"');
  123. var method = 'POST';
  124. test(parseURI(), { error : '"method" not found' });
  125. test(parseURI(method), { error : '"uri object" not found' });
  126.  
  127. test(parseURI(method, "/tora"), [ '/tora', null, {} ]);
  128. test(parseURI(method, "/tora?q=abc"), [ '/tora', 'q=abc', {} ]);
  129.  
  130. test(parseURI(method, []), { error : 'uri must be "string" or "array"' });
  131.  
  132. test(parseURI(method, [ "/tora" ]), [ '/tora', null, {} ]);
  133. test(parseURI(method, [ "/tora?q=abc" ]), [ '/tora', 'q=abc', {} ]);
  134.  
  135. console.log('\narguments.length === 2');
  136. test(parseURI(method, [ "/tora", "q=abc&qq=xyz" ]), [ '/tora', 'q=abc&qq=xyz', {} ]);
  137. test(parseURI(method, [ "/tora", { q : 'abc', qq : 'xyz' } ]), [ '/tora', 'q=abc&qq=xyz', {} ]);
  138. test(parseURI(method, [ "/tora", [["q", "abc"],["qq", "xyz"]] ]), [ '/tora', 'q=abc&qq=xyz', {} ]);
  139. test(parseURI(method, [ "/tora?q=abc&qq=xyz", null ]), [ '/tora', 'q=abc&qq=xyz', {} ]);
  140.  
  141. console.log('\narguments.length === 3');
  142. var headers = { 'Connection' : 'keep-alive', 'X-header' : 'heaven' };
  143. test(parseURI(method, [ "/tora", "q=abc&qq=xyz", headers ]), [ '/tora', 'q=abc&qq=xyz', headers ]);
  144. test(parseURI(method, [ "/tora", { q : 'abc', qq : 'xyz' }, headers ]), [ '/tora', 'q=abc&qq=xyz', headers ]);
  145. test(parseURI(method, [ "/tora", [["q", "abc"],["qq", "xyz"]], headers ]), [ '/tora', 'q=abc&qq=xyz', headers ]);
  146. test(parseURI(method, [ "/tora?q=abc&qq=xyz", null, headers ]), [ '/tora', 'q=abc&qq=xyz', headers ]);
Add Comment
Please, Sign In to add comment