Jump to content

HTTP Name2Key Server w/ LSL


Shymus Roffo
 Share

You are about to reply to a thread that has been inactive for 1189 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Now with the new functions that were released onto the Wiki, a lot of my web servers used a Name2Key service(Such as W-Hat, or similar). One problem we were having is some services are not 100% reliable. Every now and then, we had NULL_KEYs returned. These scripts we made; we used as a backup when the online services were not working or did not produce the desired result.

This is not meant to be used as an alternative to Name2Key with LSL scripts, it is used for websites that need Name2Key and are looking for a backup to their current Name2Key. This could obviously be expanded and is very basic. I would expand on this to allow multiple in-world servers in case of an outage and make it update to the prim's URLs into a database and not a text file.

Server Directory/Structure

/Name2Key
|
| - .htaccess
| - index.php

Once your directory is set up on your server, here are the files!

.httaccess

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php?name=$1 [L,QSA]

index.php

<?php

if(!empty($_GET['name'])) {
	if($_GET['name'] == "update_url") {
		$fh = fopen("server_url.txt","w");
		fwrite($fh,base64_decode($_GET['url']));
		fclose($fh);
	} else {
		header("Content-Type: text/plain");
		$fh = fopen("server_url.txt","r");
		$url = fread($fh, filesize("server_url.txt"));
		fclose($fh);
		$ch = curl_init($url);
		curl_setopt_array($ch,array(
			CURLOPT_RETURNTRANSFER=>TRUE,
			CURLOPT_POST=>TRUE,
			CURLOPT_HEADER=>"application/x-www-form-urlencoded",
			CURLOPT_POSTFIELDS=>base64_encode($_GET['name'])
		));
		$data = curl_exec($ch);
		curl_close($ch);
		echo $data;
	}
}

Now for the in-world scripts. This is meant to go into a single prim and be left to sit there.

Name2Key.lsl

string update_url = "http://WebsiteName.com/Name2Key/update_url&url=";
string url;
key urlRequestId;
key selfCheckRequestId;
key name_query;
key return_key;
 
request_url() {
    llReleaseURL(url);
    url = "";
    llSetTimerEvent(0.0);
    urlRequestId = llRequestURL();
}

throw_exception(string inputString) {
    llInstantMessage(llGetOwner(), inputString);
    llResetScript();
}

default {
    state_entry() { request_url(); }
    on_rez(integer start_param) { llResetScript(); }
    changed(integer change) {
        if (change & CHANGED_OWNER | CHANGED_REGION | CHANGED_REGION_START)
            llResetScript();
    }
 
    timer() {
        selfCheckRequestId = llHTTPRequest(url,[HTTP_METHOD, "GET",HTTP_VERBOSE_THROTTLE, FALSE,HTTP_BODY_MAXLENGTH, 16384],"");
    }
    
    dataserver(key i, string d) {
        if(i == name_query) {
            if(d != NULL_KEY) {
                llHTTPResponse(return_key,200,d);
            } else {
                llHTTPResponse(return_key,200,NULL_KEY);
            }
        }
    }
    
    http_request(key id, string method, string body) {
        integer responseStatus = 400;
        string responseBody = "Unsupported method";
        if (method == URL_REQUEST_DENIED)
            throw_exception("The following error occurred while attempting to get a free URL for this device:\n \n" + body);
        else if (method == URL_REQUEST_GRANTED) {
            url = body;
            llHTTPRequest(update_url+""+llStringToBase64(url),[HTTP_METHOD,"GET",HTTP_VERBOSE_THROTTLE,FALSE],"");
            llSetTimerEvent(300.0);
        }
        else if (method == "GET") {
            llHTTPResponse(id, 400, "Unsupported Method");
        }
        else if (method == "POST") {
            body = llBase64ToString(body);
            name_query = llRequestUserKey(body);
            return_key = id;
        }
    }
 
    http_response(key id, integer status, list metaData, string body) {
        if (id == selfCheckRequestId) {
            selfCheckRequestId = NULL_KEY;
            if (status != 200)
                request_url();
        }
        else if (id == NULL_KEY)
            throw_exception("Too many HTTP requests too fast!");
    }
}

 

As I said, this is not a "finished" product, as it still needs some upgrades. We highly recommend that you use it as a backup to other online Name2Key Services when you get a NULL_KEY. Hope this helps out other users!

Link to comment
Share on other sites

Yes, it is for an apache set up. It could be modified to work with other systems. I'm not that familiar with other languages or servers, so this is what I made it for. If anyone else makes the server code with a different language, let me know and I'll put it onto the thread.

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1189 days.

Please take a moment to consider if this thread is worth bumping.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...