Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TCPObject - Reference @ http://www.garagegames.com/community/blogs/view/12262
- function TCPObject::query(%this, %func, %url, %port, %get, %post) {
- if((%func !$= "") && (%url !$= "")) {
- if(%this._func !$= "") {
- for(%i = -1; %this._query[%i++] !$= ""; ) {}
- %this._query[%i] = %func NL %url NL %port NL %get NL %post;
- } else {
- if(%url !$= "") {
- if((%start = (strpos(%url, "//") + 2)) == 1) {
- %start = 0;
- }
- %this._address = getSubStr(%url, %start, ((%page = strpos(%url, "/", %start)) - %start));
- if((%deli = strpos(%url, "?", %page)) == -1) {
- %deli = strlen(%url);
- }
- %this._page = getSubStr(%url, %page, (%deli - %page));
- if(strpos(%this._address, ":") == -1) {
- %this._address = %this._address @ ":" @ ((%port $= "") ? (80) : (%port));
- }
- }
- %this._func = %func;
- %this._data = strReplace(%post, " ", "");
- %this._query = strReplace(%get, " ", "");
- %this.connect(%this._address);
- }
- }
- }
- function TCPObject::onConnected(%this) {
- if(%this._data $= "") {
- %method = "GET";
- } else {
- %method = "POST";
- }
- %this.send( // Reference @ http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html
- %method SPC %this._page @ "?" @ %this._query SPC "HTTP/1.1" NL
- "Connection: Keep-Alive" NL
- "Host:" SPC %this._address NL
- "Content-Type: application/x-www-form-urlencoded" NL
- "Content-Length:" SPC strlen(%this._data) NL "" NL %this._data NL ""
- ); // This request should be similar to the request from the http object
- %this._address = "";
- %this._query = "";
- %this._page = "";
- %this._data = "";
- }
- function TCPObject::onLine(%this, %line) {
- if(%this._doBuffer) {
- if(%this._buffer $= "") {
- %this._buffer = %line;
- } else {
- %this._buffer = %this._buffer NL %line;
- }
- } else {
- if(%line $= "") {
- %this._doBuffer = true;
- %this.schedule(0, "queue");
- } else {
- if(%this._response $= "") {
- %this._response = %line;
- } else {
- %this._response = %this._response NL %line;
- }
- }
- }
- }
- function TCPObject::queue(%this) {
- if(%this._buffer !$= "") {
- %this.call(%this._func, %this._response, %this._buffer);
- }
- if(%this._query0 !$= "") {
- %this.schedule(0, "query",
- getRecord(%this._query0, 0),
- getRecord(%this._query0, 1),
- getRecord(%this._query0, 2),
- getRecord(%this._query0, 3),
- getRecord(%this._query0, 4)
- );
- for(%i = -1; (%this._query[%i] = %this._query[%i++ + 1]) !$= ""; ) {}
- } else {
- %this.disconnect();
- }
- %this._doBuffer = "";
- %this._response = "";
- %this._buffer = "";
- %this._func = "";
- }
- // HTTPObject
- function HTTPObject::query(%this, %func, %url, %port, %get, %post) {
- if((%func !$= "") && (%url !$= "")) {
- if(%this._func $= "") {
- %idx = strpos(%url, "//", 0);
- if(%idx != -1) {
- %url = getSubStr(%url, (%idx + 2), (strlen(%url) - %idx - 2));
- }
- %idx = strpos(%url, "/", 0);
- if(%idx != -1) {
- %address = getSubStr(%url, 0, %idx);
- %url = getSubStr(%url, %idx, (strlen(%url) - %idx));
- } else {
- %address = %url;
- %url = "/";
- }
- %address = %address @ ":" @ ((%port $= "") ? (80) : (%port));
- if(%post $= "") {
- %this.get(%address, %url, %get);
- } else {
- %this.post(%address, %url, %get, %post);
- }
- %this._func = %func;
- } else {
- for(%i = -1; %this._query[%i++] !$= ""; ) {}
- %this._query[%i] = %func NL %url NL %port NL %get NL %post;
- }
- }
- }
- function HTTPObject::onConnected(%this) {} // To block the parent function
- // Query functions
- function TCPObject::print(%this, %response, %buffer) {
- echo("");
- echo(%this.getName());
- echo("");
- warn(%response);
- echo("");
- error(%buffer);
- echo("");
- }
- function TCPObject::writeFile(%this, %response, %buffer) {
- %fileObj = new FileObject();
- if(%fileObj.OpenForWrite("./" @ %this.getName() @ ".html")) {
- %fileObj.writeLine(%buffer);
- %fileObj.close();
- }
- %fileObj.delete();
- }
- /*
- At the moment both querys do the same request
- The only difference is that the TCPObject can be changed within the onConnected function
- */
- /*
- new TCPObject(TCP_Server);
- new HTTPObject(HTP_Server);
- HTP_Server.query("print", "http://dl-web.dropbox.com/u/34832701/LTS/LTSIPA.txt");
- TCP_Server.query("print", "http://dl-web.dropbox.com/u/34832701/LTS/LTSIPA.txt");
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement