Open Source Pastebin in .NET
A pastebin is designed to be an easy way to quickly post some text online, so that others can see it. It also has applications in cross-firewall data traversal, where one peer can upload data to a pastebin, and the other peer can poll the pastebin for the message to arrive. This was the kind of pastebin I was after.
I looked at pastebin.com and paste.kde.org, to see if they were suitable. Paste.kde.org seemed promising, where you could paste easily, using a URL like this:
Which would return an ID, which you could retrieve using http://paste.kde.org/api/xml/117613, for example. The problem came, when I wanted to list all pastes by a particular user, or project, and I spotted that it wasn’t working as expected, like http://paste.kde.org/~fiachspastebin/api/xml/all/ seemed to return numerous pastes by other users.
So, I decided to write my own pastebin, which would store the messages in-memory. This meant that they would be deleted as the IIS worker process recycled, but, I didn’t need longevity.
Using two simple actions “action=update” which either created or updated a paste, and assigned it to a “user“, containing the text contained in the “text” querystring parameter. Then when “action=get” then the text associated with that “user” would be returned.
So, to store text with a user, you simply call:
http://url/pastebin.aspx?action=update&user=dananos&text=Hello+World
Then to retrieve that text you call
http://url/pastebin.aspx?action=get&user=dananos
And that’s it. The code I used is below:
public static Hashtable data = new Hashtable(); protected void Page_Load(object sender, EventArgs e) { string action = Request.QueryString["Action"]; string user = Request.QueryString["User"]; string text = Request.QueryString["Text"]; if (string.IsNullOrEmpty(action) || string.IsNullOrEmpty(user)) { Response.Write("Action and User is required"); return; } if (action == "update") { PasteBin.data[user] = text; Response.Write("OK"); } if (action == "get") { if (PasteBin.data.ContainsKey(user)) { Response.Write(PasteBin.data[user]); } else { Response.Write("NO DATA"); } } }
I’ve ported the same code to PHP, for Linux users. This code requires the $_APP library, which is posted at http://www.leosingleton.com/projects/code/phpapp/
This is hosted at http://pastebin.omadataobjects.com/bin.php
<?php include("app.php"); application_start(); $action = $_REQUEST["action"]; $user = $_REQUEST["user"]; $text = $_REQUEST["text"]; if ($action == "" || $user == "") { echo("action and user is required"); } else { if ($action=="update") { $_APP[$user] = $text; echo "OK"; } if ($action=="get") { echo stripslashes($_APP[$user]); } } application_end(); ?>
I added an extra function to the PHP version as follows;
if ($action==”log”)
{
date_default_timezone_set(‘America/New_York’);
$_APP[$user] .= $_SERVER[‘REMOTE_ADDR’] . ‘ ‘ . date(‘Y-m-d H:i:s’) . ‘ ‘ . $text . ‘
‘;
echo “OK”;
}
LikeLike