Advertisement
Shiska

http

Mar 18th, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.28 KB | None | 0 0
  1. // TCPObject - Reference @ http://www.garagegames.com/community/blogs/view/12262
  2.  
  3. function TCPObject::query(%this, %func, %url, %port, %get, %post) {
  4.     if((%func !$= "") && (%url !$= "")) {
  5.         if(%this._func !$= "") {
  6.             for(%i = -1; %this._query[%i++] !$= ""; ) {}
  7.  
  8.             %this._query[%i] = %func NL %url NL %port NL %get NL %post;
  9.         } else {
  10.             if(%url !$= "") {
  11.                 if((%start = (strpos(%url, "//") + 2)) == 1) {
  12.                     %start = 0;
  13.                 }
  14.                 %this._address = getSubStr(%url, %start, ((%page = strpos(%url, "/", %start)) - %start));
  15.  
  16.                 if((%deli = strpos(%url, "?", %page)) == -1) {
  17.                     %deli = strlen(%url);
  18.                 }
  19.                 %this._page = getSubStr(%url, %page, (%deli - %page));
  20.  
  21.                 if(strpos(%this._address, ":") == -1) {
  22.                     %this._address = %this._address @ ":" @ ((%port $= "") ? (80) : (%port));
  23.                 }
  24.             }
  25.             %this._func = %func;
  26.             %this._data = strReplace(%post, " ", "");
  27.             %this._query = strReplace(%get, " ", "");
  28.  
  29.             %this.connect(%this._address);
  30.         }
  31.     }
  32. }
  33.  
  34. function TCPObject::onConnected(%this) {
  35.     if(%this._data $= "") {
  36.         %method = "GET";
  37.     } else {
  38.         %method = "POST";
  39.     }
  40.     %this.send( // Reference @ http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html
  41.         %method SPC %this._page @ "?" @ %this._query SPC "HTTP/1.1" NL
  42.         "Connection: Keep-Alive" NL
  43.         "Host:" SPC %this._address NL
  44.         "Content-Type: application/x-www-form-urlencoded" NL
  45.         "Content-Length:" SPC strlen(%this._data) NL "" NL %this._data NL ""
  46.     ); // This request should be similar to the request from the http object
  47.     %this._address = "";
  48.     %this._query = "";
  49.     %this._page = "";
  50.     %this._data = "";
  51. }
  52.  
  53. function TCPObject::onLine(%this, %line) {
  54.     if(%this._doBuffer) {
  55.         if(%this._buffer $= "") {
  56.             %this._buffer = %line;
  57.         } else {
  58.             %this._buffer = %this._buffer NL %line;
  59.         }
  60.     } else {
  61.         if(%line $= "") {
  62.             %this._doBuffer = true;
  63.             %this.schedule(0, "queue");
  64.         } else {
  65.             if(%this._response $= "") {
  66.                 %this._response = %line;
  67.             } else {
  68.                 %this._response = %this._response NL %line;
  69.             }
  70.         }
  71.     }
  72. }
  73.  
  74. function TCPObject::queue(%this) {
  75.     if(%this._buffer !$= "") {
  76.         %this.call(%this._func, %this._response, %this._buffer);
  77.     }
  78.     if(%this._query0 !$= "") {
  79.         %this.schedule(0, "query",
  80.             getRecord(%this._query0, 0),
  81.             getRecord(%this._query0, 1),
  82.             getRecord(%this._query0, 2),
  83.             getRecord(%this._query0, 3),
  84.             getRecord(%this._query0, 4)
  85.         );
  86.         for(%i = -1; (%this._query[%i] = %this._query[%i++ + 1]) !$= ""; ) {}
  87.     } else {
  88.         %this.disconnect();
  89.     }
  90.     %this._doBuffer = "";
  91.     %this._response = "";
  92.     %this._buffer = "";
  93.     %this._func = "";
  94. }
  95.  
  96. // HTTPObject
  97.  
  98. function HTTPObject::query(%this, %func, %url, %port, %get, %post) {
  99.     if((%func !$= "") && (%url !$= "")) {
  100.         if(%this._func $= "") {
  101.             %idx = strpos(%url, "//", 0);
  102.             if(%idx != -1) {
  103.                 %url = getSubStr(%url, (%idx + 2), (strlen(%url) - %idx - 2));
  104.             }
  105.             %idx = strpos(%url, "/", 0);
  106.             if(%idx != -1) {
  107.                 %address = getSubStr(%url, 0, %idx);
  108.                 %url = getSubStr(%url, %idx, (strlen(%url) - %idx));
  109.             } else {
  110.                 %address = %url;
  111.                 %url = "/";
  112.             }
  113.             %address = %address @ ":" @ ((%port $= "") ? (80) : (%port));
  114.  
  115.             if(%post $= "") {
  116.                 %this.get(%address, %url, %get);
  117.             } else {
  118.                 %this.post(%address, %url, %get, %post);
  119.             }
  120.             %this._func = %func;
  121.         } else {
  122.             for(%i = -1; %this._query[%i++] !$= ""; ) {}
  123.  
  124.             %this._query[%i] = %func NL %url NL %port NL %get NL %post;
  125.         }
  126.     }
  127. }
  128.  
  129. function HTTPObject::onConnected(%this) {} // To block the parent function
  130.  
  131. // Query functions
  132.  
  133. function TCPObject::print(%this, %response, %buffer) {
  134.     echo("");
  135.     echo(%this.getName());
  136.     echo("");
  137.     warn(%response);
  138.     echo("");
  139.     error(%buffer);
  140.     echo("");
  141. }
  142.  
  143. function TCPObject::writeFile(%this, %response, %buffer) {
  144.     %fileObj = new FileObject();
  145.     if(%fileObj.OpenForWrite("./" @ %this.getName() @ ".html")) {
  146.         %fileObj.writeLine(%buffer);
  147.         %fileObj.close();
  148.     }
  149.     %fileObj.delete();
  150. }
  151. /*
  152.     At the moment both querys do the same request
  153.     The only difference is that the TCPObject can be changed within the onConnected function
  154. */
  155. /*
  156. new TCPObject(TCP_Server);
  157. new HTTPObject(HTP_Server);
  158.  
  159. HTP_Server.query("print", "http://dl-web.dropbox.com/u/34832701/LTS/LTSIPA.txt");
  160. TCP_Server.query("print", "http://dl-web.dropbox.com/u/34832701/LTS/LTSIPA.txt");
  161. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement