wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
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";
        }
    }
}