using System;
using System.Collections;
using System.IO;
/*
* do an `export QUERY_STRING=foo\=baz\&` before testing this program
*/
namespace WebSharp
{
public class Dictionary
//had to write a Dictionary just to work things out
{
public ArrayList keys;
public ArrayList values;
public int Count;
//see I'm too lazy to complete the hashtable
public Dictionary()
{
keys=new ArrayList();
values=new ArrayList();
Count=0;
}
public void Add(Object key,Object value)
//after full impl will Interface IDictionary
{
try
{
int index=keys.IndexOf(key);
values[index]=value;
}
catch(ArgumentException)
{
keys.Add(key);
Count=Count+1;
values.Add(value);
}
}
public void Remove(Object key)
{
int index=keys.IndexOf(key);
if(index==-1)
{
throw new ArgumentException("Argument not found");
//throw new ArgumentException(_("Arg_NotFound"));
//hope this works with CSCC, nope it doesn't
}
keys.RemoveAt(index);
values.RemoveAt(index);
}
public Object Get(String key)
{
int index;
try
{
index=keys.IndexOf(key);
}
catch(Exception)
{
throw;
}
return values[index];
}
public Object this[String key1]
{
get
{
return Get(key1);
}
set
{
Add(key1,value);
}
}
public String ToString()
{
String retval="Count:"+keys.Count+"\n";
for(int i=0;i "+(String)
values[i]+" \n";
}
return retval;
}
public void Clear()
{
keys.Clear();
values.Clear();
Count=0;
}
public ArrayList Keys()
{
return keys;
}
public ArrayList Values()
{
return values;
}
public bool Contains(Object key)
{
try
{
int i=keys.IndexOf(key);
if(i==-1)return false;
return true;
}
catch(ArgumentOutOfRangeException)
{
return false;
}
return false;
}
};
/*
* Start the WEB specific parts.
* Man !
*/
public class WebRequest
{
public IDictionary EnvVars;
public Dictionary QueryParsed;
public WebRequest ()
{
QueryParsed=new Dictionary();
}
public void ReadRequest ()
{
this.EnvVars = Environment.GetEnvironmentVariables ();
if (EnvVars["QUERY_STRING"] != null)
{
String queryString = (String) EnvVars["QUERY_STRING"];
this.ParseQuery (queryString);
}
}
public void PutQueryVar(String pair)
{
if (pair.IndexOf ("=") != -1)
{
String key = pair.Substring (0, pair.IndexOf ("="));
String val = pair.Substring (pair.IndexOf ("=") + 1);
//Console.WriteLine (key + " => " + val);
// I wish I had a Hashtable to put it into
// but anyway my Dictionary will do.....
QueryParsed.Add(key,val);
//DEBUG:Console.WriteLine("Content-Type: text/plain\r\n\r\n");
//DEBUG:Console.WriteLine(QueryParsed.ToString());
//must implement multiple keys later..someday
}
else
{
//should throw an exception
}
}
public void ParseQuery (String queryString)
{
int end = 0, j = 0;
bool processed = false;
//the query string gets destroyed into a
//set of parsed values
//no problem about that
while (queryString.IndexOf ("&") != -1
&& queryString.IndexOf ("=") != -1)
{
end = queryString.IndexOf ("&");
String pair = queryString.Substring (0, end);
PutQueryVar(pair);
if (end < queryString.Length)end++;
queryString = queryString.Substring (end);
processed = true;
}
//but some browsers sumbit single values as
//?query=ok
//without the & at the end;there fore
if (queryString.IndexOf("&")==-1 && queryString.IndexOf("=")!=-1)
{
if (queryString.IndexOf ("=") != -1)
{
PutQueryVar(queryString);
}
//PutQueryVar(dat);
}
}
public String GetEnvVars (String key)
{
return (String) this.EnvVars[key];
}
};//class
public class WebResponse
{
private WebRequest req;
int header_status;
public WebResponse()
{
header_status=0;
}
//seems like my Java is bubbling up again !
//for the get set pairs.......:-)
public void SetRequest(WebRequest req)
{
this.req=req;
}
public void HeaderStart()
{
header_status=1;
}
public void Header(String name,String value)
{
if(header_status==1)
{
Console.Write(name+": "+value+"\r\n");
}
else if(header_status==0)
{
throw new Exception("Header unstarted");
}
else
{
throw new Exception("Header closed");
}
//should add trim on both params--later
}
public void HeaderEnd()
{
Console.Write("\r\n");
}
};//class
[TODO]
public class HTMLFormatter
{
public static void PageStart(String title, Dictionary attrs)
{
Console.WriteLine("");
Console.WriteLine(title);
Console.Write("");
}
public static void A(String url,String link)
{
Console.WriteLine("");
Console.WriteLine(link);
Console.WriteLine("");
}
public static void H1(String text, Dictionary attrs)
{
if(attrs==null)Console.WriteLine(""+text+"
");
else
{
Console.Write(""+text+"
");
}
}
public static void TableStart(Dictionary attrs)
{
}
public static void TableEnd(Dictionary attrs)
{
}
public static void PageEnd()
{
Console.WriteLine("");
}
}
/***********************************************************************
MY CGI class !
************************************************************************/
public class Pnetcgi
{
private WebRequest req;
private WebResponse resp;
private void run()
{
req = new WebRequest ();
req.ReadRequest ();
resp=new WebResponse();
resp.SetRequest(req);
resp.HeaderStart();
resp.Header("Content-Type","text/html");
resp.HeaderEnd();
if((req.QueryParsed.Count >0) && (req.QueryParsed.Contains("operation")) && (req.QueryParsed.Contains("int1")) && (req.QueryParsed.Contains("int2")))
{
PrintHeader();
try
{
Int16 i1=Int16.Parse((String)req.QueryParsed["int1"]);
Int16 i2=Int16.Parse((String)req.QueryParsed["int2"]);
if(req.QueryParsed["operation"].Equals("add"))
{
Console.Write(i1+" + "+i2+" = "+(i1+i2));
}
else if(req.QueryParsed["operation"].Equals("sub"))
{
Console.Write(i1+" - "+i2+" = "+(i1-i2));
}
else if(req.QueryParsed["operation"].Equals("mul"))
{
Console.Write(i1+" * "+i2+" = "+(i1*i2));
}
else if(req.QueryParsed["operation"].Equals("div"))
{
Console.Write(i1+" / "+i2+" = "+(i1/i2));
}
else
{
Console.Write("Sorry unknown operation ?");
}
}
catch(System.FormatException)
{
Console.Write("Sorry cannot add non integers");
}
PrintFooter();
}
else PrintInfo();
}
/*
* I thought about implementing an HTMLFormatter class
* but has left it half done, to speed up the work at
* hand. Handling plain HTML is easier :-)
* lazy R US....
*/
private void PrintHeader()
{
Console.WriteLine("");
Console.WriteLine(" Pnetcgi DEMO ");
Console.WriteLine("");
Console.WriteLine("");
}
private void PrintFooter()
{
Console.WriteLine("");
}
private void PrintInfo()
{
//Console.WriteLine("");
//Console.WriteLine(" Pnetcgi DEMO ");
//Console.WriteLine("");
//Console.WriteLine("");
PrintHeader();
Console.WriteLine("CGI Demo
");
Console.WriteLine("Environment Variables
");
Console.WriteLine("");
Console.WriteLine("Name | Value |
");
IDictionaryEnumerator e = req.EnvVars.GetEnumerator();
while(e.MoveNext())
{
Console.Write("");
Console.Write((String)(e.Key));
Console.Write(" | ");
Console.Write((String)(e.Value));
Console.WriteLine(" |
");
}
Console.WriteLine("
");
if(req.QueryParsed.Count > 0)
{
PrintQuery();
}
PrintArith();
Print8Puzzle();
PrintFooter();
}
private void PrintArith()
{
Console.WriteLine("
");
}
private void PrintQuery()
{
Console.WriteLine("");
Console.WriteLine("Parsed Query
");
Console.WriteLine("");
Console.WriteLine("Name | Value |
");
for(int i=0;i");
Console.Write(req.QueryParsed.Keys()[i]);
Console.Write(" | ");
Console.Write(req.QueryParsed.Values()[i]);
Console.WriteLine(" | ");
}
Console.WriteLine("
");
}
public static void Main ()
{
Pnetcgi x=new Pnetcgi();
x.run();
}
/***************FAILED 8 puzzle attempt**********************************/
private void Print8Puzzle()
{
ArrayList puzzle=new ArrayList(16);
Console.WriteLine("8-Puzzle
");
if(!(req.QueryParsed.Contains("PlayPuzzle")))
{
for(int i=0;i<16;i++)
{
puzzle.Add((i+1)%16);
}
}
else
{
Console.Write(req.QueryParsed["elem"+i]);
for(i=0;i<15;i++)
{
puzzle.Add(Int16.Parse((String)req.QueryParsed["elem"+i]));
}
}
Console.WriteLine("");
}
/****************************************************************************/
};//class
};//namespace