Warning: This code has not been tested outside Sindome and could depend on the way things are coded here.
It depends on $string_utils and $math_utils to exist and function as ours does. In all likelihood, it should
work, but no guarantees are made. - Johnny
@chmod #5064."host" r
;;#5064.("host") = #-1
@chmod #5064."port" r
;;#5064.("port") = 80
@chmod #5064."path" r
;;#5064.("path") = ""
@chmod #5064."links" r
;;#5064.("links") = {}
@chmod #5064."old_data" r
;;#5064.("old_data") = ""
@chmod #5064."raw_data" r
;;#5064.("raw_data") = ""
@chmod #5064."parsed_data" r
;;#5064.("parsed_data") = ""
@chmod #5064."user_agent" r
;;#5064.("user_agent") = "SindomeMOO/1.0 (Unix; I)"
@chmod #5064."hostobj" r
;;#5064.("hostobj") = #-1
@chmod #5064."headers" r
;;#5064.("headers") = {}
@chmod #5064."base64" r
;;#5064.("base64") = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
@chmod #5064."debugging" r
;;#5064.("debugging") = 0
@chmod #5064."hostname" r
@chown #5064."hostname" #98
;;#5064.("hostname") = "www.sindome.org"
@chmod #5064."http_version" r
@chown #5064."http_version" #98
;;#5064.("http_version") = "1.1"
@chmod #5064."url_encode_table" r
@chown #5064."url_encode_table" #98
;;#5064.("url_encode_table") = {{";", "%3B"}, {"?", "%3F"}, {"/", "%2F"}, {":", "%3A"}, {"#", "%23"}, {"&", "%24"}, {"=", "%3D"},
{"+", "%2B"}, {"$", "%26"}, {",", "%2C"}, {" ", "%20"}, {"%", "%25"}, {"<", "%3C"}, {">", "%3E"}, {"~", "%7E"}, {"%", "%25"}}
;;#5064.("aliases") = {"east", "e"}
;;#5064.("object_size") = {3693, 1755134567}
@args #5064:"_get" this none this
@program #5064:_get
{host, port, path, ?username = "", ?password = ""} = args;
this.old_data = this.raw_data;
"this:zero_out_props()";
try
this.hostobj = open_network_connection(host, port);
except errorcodes (ANY)
"$critical(\"MOO WEB BROWSER TB => :_get \", errorcodes);
return;
endtry
set_connection_option(this.hostobj, "hold-input", 1);
this.host = host;
this.port = port;
this.path = path;
get_req = tostr("GET ", path, " HTTP/", this.http_version);
notify(this.hostobj, get_req);
notify(this.hostobj, tostr("Connection: Close"));
notify(this.hostobj, tostr("User-Agent: ", this.user_agent));
notify(this.hostobj, tostr("Host: ", host, ":", port));
notify(this.hostobj, tostr("Accept: */*"));
if (username && password)
notify(this.hostobj, tostr("Authorization: BASIC ", this:url_encode((username + ":") + password)));
endif
notify(this.hostobj, "");
this:receive_data();
return this.parsed_data;
"VMS NOTE testing";
"VMS VERSION 1.13";
"Last modified by Johnny (#98) on Fri Apr 24 12:50:45 2009 PDT";
.
@args #5064:"zero_out_props" this none this
@program #5064:zero_out_props
for x in ({"old_data", "parsed_data", "raw_data", "links", "host", "port", "path"})
try
clear_property(this, x);
except ecodes (ANY)
"$alert(ecodes)";
endtry
endfor
"VMS NOTE clears prop values";
"VMS VERSION 1.13";
"Last modified by Johnny (#1673) on Wed Dec 5 23:29:02 2001 PST";
.
@args #5064:"receive_data" this none this
@program #5064:receive_data
have_head = 0;
this.headers = {};
data = {};
while (!this.hostobj)
suspend(0);
try
strg = read(this.hostobj, 0);
except (E_INVARG)
"connection has closed";
break;
endtry
$alert(strg);
if ((have_head == 0) && (strg == ""))
have_head = 1;
elseif (have_head == 0)
this.headers = {@this.headers, strg};
else
this.raw_data = this.raw_data + strg;
endif
endwhile
response = this.headers[1];
if (index(response, "200"))
"ok - parse page";
this.parsed_data = this:parse_html(this.raw_data);
elseif (index(response, "401"))
"authentication required";
elseif (index(response, "404"))
"document not found";
else
"unhandled responses fall here";
endif
"VMS NOTE added debug flags";
"VMS VERSION 1.11";
"Last modified by Johnny (#98) on Fri Apr 24 12:33:57 2009 PDT";
.
@args #5064:"parse_html" this none this
@program #5064:parse_html
"VMS VERSION 1.0";
rstrg = original = args[1];
rstrg = this:link_mod(rstrg);
return rstrg;
.
@args #5064:"link_mod" this none this
@program #5064:link_mod
{strg} = {original} = args;
if (1)
links = {};
saved_links = this.links;
if (s = index(strg, "");
if (e)
link_start = {strg[s..e], s, e};
strg = strg[e + 1..$];
if (s = index(strg, ""))
start = link_start[1];
label = strg[1..s - 1];
s = index(start, "href=") + 5;
e = index(start, ">") - 1;
link = start[s..e];
saved_links = {@saved_links, {label, link}};
endif
endif
endif
endif
this.links = saved_links;
return strg;
"VMS NOTE blahh.";
"VMS VERSION 1.02";
"Last modified by Johnny (#1673) on Thu Dec 6 00:37:18 2001 PST";
.
@args #5064:"base64_decode" this none this
@program #5064:base64_decode
"VMS VERSION 1.0";
{string} = args;
out = "";
b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
while (string)
set = string[1..min(4, $)];
string = string[5..$];
n = 0;
while (set)
m = (index(b64, set[1], 1) - 1) % 64;
n = (n * 64) + m;
set = set[2..$];
endwhile
o = "";
while (n)
m = n % 128;
if ((m > 32) && (m != 127))
o = $string_utils:from_ASCII(m) + o;
endif
n = n / 256;
endwhile
out = out + o;
endwhile
return out;
.
@args #5064:"base64_encode" this none this
@program #5064:base64_encode
"VMS VERSION 1.0";
for x in ($string_utils:char_list(args[1]))
binary = {@`binary ! ANY => {}', $string_utils:from_list($math_utils:blfromint($string_utils:to_ascii(x)))[$ - 7..$]};
$cu:sin(0);
endfor
while (binary)
bit24 = $string_utils:from_list(binary[1..min(3, $)]);
binary = binary[4..$];
$cu:sin(0);
strg24 = "";
while (bit24)
strg24 = strg24 + this.base64[$math_utils:intfrombl($string_utils:left(bit24[1..min(6, $)], 6, "0")) + 1];
bit24 = bit24[7..$];
$cu:sin(0);
endwhile
encoded_strg = `encoded_strg ! ANY => ""' + $string_utils:left(strg24, 4, "=");
endwhile
return encoded_strg;
.
@args #5064:"_post" this none this
@chown #5064:_post #98
@program #5064:_post
{host, port, path, parameters, ?username = "", ?password = ""} = args;
post_content = this:string_parameters(parameters);
post = {};
post_req = tostr("POST ", path, " HTTP/", this.http_version);
post = {@post, post_req};
post = {@post, tostr("Host: ", host, ":", port)};
post = {@post, tostr("User-Agent: ", this.user_agent)};
post = {@post, tostr("Content-Length: ", length(post_content))};
post = {@post, "Content-Type: application/x-www-form-urlencoded"};
if (username && password)
post = {@post, tostr("Authorization: Basic ", this:base64_encode((username + ":") + password))};
endif
post = {@post, ""};
post = {@post, post_content};
try
this.hostobj = open_network_connection(host, port);
except ecodes (ANY)
"$critical(\"MOO WEB BROWSER TB => :_post \", ecodes)";
return;
endtry
set_connection_option(this.hostobj, "hold-input", 1);
this.host = host;
this.port = port;
this.path = path;
for x in (post)
notify(this.hostobj, x);
endfor
this:receive_data();
return this.parsed_data;
"VMS NOTE basic HTTP POST method";
"VMS VERSION 1.01";
"Last modified by Johnny (#98) on Fri Apr 24 12:44:18 2009 PDT";
.
@args #5064:"url_encode" this none this
@chown #5064:url_encode #98
@program #5064:url_encode
{value} = args;
return $string_utils:subst(value, this.url_encode_table);
"VMS NOTE url encoding method";
"VMS VERSION 1.0";
"Last modified by Johnny (#98) on Fri Apr 24 11:34:59 2009 PDT";
.
@args #5064:"string_parameters" this none this
@chown #5064:string_parameters #98
@program #5064:string_parameters
{parameters} = args;
string = "";
for pair in (parameters)
$cu:sin(0);
{name, value} = pair;
if (string != "")
string = string + "&";
endif
string = ((string + name) + "=") + this:url_encode(value);
endfor
return string;
"VMS NOTE stringify a hash array of parameters";
"VMS VERSION 1.01";
"Last modified by Johnny (#98) on Fri Apr 24 12:45:06 2009 PDT";
.
"***finished***
Example Twitter Browser for posting status updates:
@args #18084:"post_status" this none this
@program #18084:post_status
{user, pass, status} = args;
if (length(status) > 137)
raise(E_INVARG, "Please limit your status to 137 characters.");
endif
this:_post("twitter.com", 80, "/statuses/update.json", {{"status", status + "#sd"}}, user, pass);
if (this.headers == {})
raise(E_NONE, "no response from twitter");
elseif (this.headers[1] != "HTTP/1.1 200 OK")
raise(E_NONE, "status post was not accepted");
endif
return 1;
"VMS NOTE attempt at a twitter status updater";
"VMS VERSION 3.67";
"Last modified by Johnny (#98) on Fri Apr 24 12:53:55 2009 PDT";
.
"***finished***
|