using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
|
using Android.App;
|
using Android.Content;
|
using Android.OS;
|
using Android.Runtime;
|
using Android.Views;
|
using Android.Widget;
|
using Java.Net;
|
using Java.IO;
|
using Java.Util.Regex;
|
using Android.Database;
|
using com.hdl.on;
|
|
namespace Shared.Other
|
{
|
class ServerHandler : Java.Lang.Thread
|
{
|
private BufferedReader in1;
|
private PrintWriter out1;
|
private string documentRoot;
|
private Context context;
|
private Socket toClient;
|
|
|
public ServerHandler(string d, Context c, Socket s)
|
{
|
|
documentRoot = d;
|
context = c;
|
toClient = s;
|
|
}
|
|
|
public override void Run()
|
{
|
string dokument = "";
|
|
try
|
{
|
in1 = new BufferedReader(new InputStreamReader(toClient.InputStream));
|
|
// Receive data
|
while (true)
|
{
|
var s = in1.ReadLine().Trim();
|
|
if (s == (""))
|
{
|
break;
|
}
|
|
if (s.Substring(0, 3) == ("GET"))
|
{
|
int leerstelle = s.IndexOf(" HTTP/");
|
dokument = s.Substring(5, leerstelle-5);
|
//dokument = dokument.Replace("[/]+", "/");
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
Server.remove(toClient);
|
try
|
{
|
toClient.Close();
|
}
|
catch (Exception ex) { }
|
}
|
|
Java.Util.Regex.Pattern taskerPattern = Java.Util.Regex.Pattern.Compile("tasker/(.+)");
|
Matcher taskerMatcher = taskerPattern.Matcher(dokument);
|
|
if (taskerMatcher.Matches())
|
{
|
try
|
{
|
dokument = Java.Net.URLDecoder.Decode(taskerMatcher.Group(1), "UTF-8");
|
sendTasker(dokument);
|
}
|
catch (UnsupportedEncodingException e)
|
{
|
// TODO Auto-generated catch block
|
e.PrintStackTrace();
|
showHtml("403.html");
|
}
|
}
|
else if (dokument == ("tasker/"))
|
{
|
listTaskerTasks();
|
}
|
else
|
{
|
showHtml(dokument);
|
}
|
}
|
|
private void sendTasker(string taskName)
|
{
|
////net.dinglisch.android.tasker.TaskerIntent
|
//if (TaskerIntent.testStatus(context)==(TaskerIntent.Status.OK))
|
//{
|
// TaskerIntent i = new TaskerIntent(taskName);
|
// context.sendBroadcast(i);
|
|
// send("Sent intent \"" + taskName + "\" to tasker.");
|
//}
|
//else
|
//{
|
// send("Could not sent intent \"" + taskName + "\" to tasker (" +
|
// TaskerIntent.testStatus(context) + ").");
|
//}
|
}
|
|
private void listTaskerTasks()
|
{
|
ICursor c = context.ContentResolver.Query(Android.Net.Uri.Parse("content://net.dinglisch.android.tasker/tasks"), null, null, null, null);
|
|
string text = "Found tasks:<ul>";
|
if (c != null)
|
{
|
Android.Util.Log.Debug("Webserver", "Cursor is not null");
|
int nameCol = c.GetColumnIndex("name");
|
int projNameCol = c.GetColumnIndex("project_name");
|
|
while (c.MoveToNext())
|
{
|
text = text + "<li><a href=\"/tasker/" + c.GetString(nameCol) + "\">" + c.GetString(projNameCol) + ": " + c.GetString(nameCol) + "</a></li>";
|
}
|
c.Close();
|
}
|
else
|
{
|
Android.Util.Log.Debug("Webserver", "Cursor is null");
|
}
|
text = text + "</ul>";
|
send(text);
|
}
|
|
private void send(string text)
|
{
|
string header = getHeaderBase();
|
header = header.Replace("%code%", "200 ok");
|
header = header.Replace("%length%", "" + text.Length);
|
try
|
{
|
out1 = new PrintWriter(toClient.OutputStream, true);
|
out1.Print(header);
|
out1.Print(text);
|
out1.Flush();
|
Server.remove(toClient);
|
toClient.Close();
|
}
|
catch (Exception e)
|
{
|
|
}
|
}
|
|
private void showHtml(string dokument)
|
{
|
for (int i = 0; i < Shared.MusicInfo.MusicInfoList.Count; i++)
|
{
|
var m = Shared.MusicInfo.MusicInfoList[i];
|
if ((m.ID + "")==(dokument))
|
{
|
dokument = m.Data;
|
break;
|
}
|
}
|
|
//Standard-Doc
|
if (dokument == (""))
|
{
|
dokument = "index.html";
|
}
|
|
// Don't allow directory traversal
|
if (dokument.IndexOf("..") != -1)
|
{
|
dokument = "403.html";
|
}
|
|
// Search for files in docroot
|
//dokument = documentRoot + dokument;
|
Android.Util.Log.Debug("Webserver", "Got " + dokument);
|
dokument = dokument.Replace("/+", "/");
|
|
if (dokument.Substring(dokument.Length - 1) == "/")
|
{
|
dokument = documentRoot + "404.html";
|
}
|
|
string header = getHeaderBase();
|
header = header.Replace("%code%", "403 Forbidden");
|
|
try
|
{
|
File f = new File(dokument);
|
if (!f.Exists())
|
{
|
header = getHeaderBase();
|
header = header.Replace("%code%", "404 File not found");
|
dokument = "404.html";
|
}
|
}
|
catch (Exception e) { }
|
|
if (!(dokument == (documentRoot + "403.html")))
|
{
|
header = getHeaderBase().Replace("%code%", "200 OK");
|
}
|
|
Android.Util.Log.Debug("Webserver", "Serving " + dokument);
|
|
try
|
{
|
File f = new File(dokument);
|
if (f.Exists())
|
{
|
BufferedInputStream in1 = new BufferedInputStream(new System.IO.FileStream(dokument,System.IO.FileMode.Open,System.IO.FileAccess.Read));
|
BufferedOutputStream out1 = new BufferedOutputStream(toClient.OutputStream);
|
ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
|
|
byte[] buf = new byte[4096];
|
int count = 0;
|
while ((count = in1.Read(buf)) != -1) {
|
tempOut.Write(buf, 0, count);
|
}
|
|
tempOut.Flush();
|
header = header.Replace("%length%", "" + tempOut.Size());
|
|
|
out1.Write(System.Text.Encoding.UTF8.GetBytes(header));
|
out1.Write(tempOut.ToByteArray());
|
out1.Flush();
|
}
|
else
|
{
|
// Send HTML-File (Ascii, not as a stream)
|
header = getHeaderBase();
|
header = header.Replace("%code%", "404 File not found");
|
header = header.Replace("%length%", "" + "404 - File not Found".Length);
|
out1 = new PrintWriter(toClient.OutputStream, true);
|
out1.Print(header);
|
out1.Print("404 - File not Found");
|
out1.Flush();
|
}
|
|
Server.remove(toClient);
|
toClient.Close();
|
}
|
catch (Exception e)
|
{
|
|
}
|
}
|
|
private string getHeaderBase()
|
{
|
return "HTTP/1.1 %code%\n" +
|
"Server: AndroidWebserver/1.0\n" +
|
"Content-Length: %length%\n" +
|
"Connection: close\n" +
|
"Content-Type: text/html; charset=iso-8859-1\n\n";
|
}
|
}
|
}
|