File: BaseHttpServer.cs

package info (click to toggle)
banshee 2.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 47,160 kB
  • ctags: 25,019
  • sloc: xml: 163,694; cs: 137,409; sh: 11,650; ansic: 4,790; makefile: 2,928; python: 38
file content (362 lines) | stat: -rw-r--r-- 11,839 bytes parent folder | download | duplicates (4)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//
// BaseWebServer.cs
//
// Author:
//   Aaron Bockover <aaron@aaronbock.net>
//   James Wilcox   <snorp@snorp.net>
//   Neil Loknath   <neil.loknath@gmail.com
//
// Copyright (C) 2005-2006 Novell, Inc.
// Copyright (C) 2009 Neil Loknath
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using System.Text;
using System.Web;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
using System.Collections.Generic;

using Hyena;

namespace Banshee.Web
{
    public abstract class BaseHttpServer
    {
        protected Socket server;
        private bool running;
        private int backlog;
        private ushort port;

        protected readonly ArrayList clients = new ArrayList();

        public BaseHttpServer (EndPoint endpoint, string name)
        {
            this.end_point = endpoint;
            this.name = name;
        }

        public BaseHttpServer (EndPoint endpoint, string name, int chunk_length) : this (endpoint, name)
        {
            this.chunk_length = chunk_length;
        }

        private string name = "Banshee Web Server";
        public string Name {
            get { return name; }
        }

        public bool IsBound {
            get { return server != null && server.IsBound; }
        }

        public bool IsRunning {
            get { return running; }
        }

        private EndPoint end_point = new IPEndPoint (IPAddress.Loopback, 80);
        protected EndPoint EndPoint {
            get { return end_point; }
            set {
                if (value == null) {
                    throw new ArgumentNullException ("end_point");
                }
                if (IsBound) {
                    throw new InvalidOperationException ("Cannot set EndPoint while running.");
                }
                end_point = value;
            }
        }

        private int chunk_length = 8192;
        public int ChunkLength {
            get { return chunk_length; }
        }

        public ushort Port {
            get { return port; }
        }

        public void Start ()
        {
            Start (10);
        }

        public void Start (int backlog)
        {
            if (backlog < 0) {
                throw new ArgumentOutOfRangeException ("backlog");
            }

            if (running) {
                return;
            }

            this.backlog = backlog;
            running = true;
            Thread thread = new Thread (ServerLoop);
            thread.Name = this.Name;
            thread.IsBackground = true;
            thread.Start ();
        }

        public virtual void Stop ()
        {
            running = false;

            if (server != null) {
                server.Close ();
                server = null;
            }

            foreach (Socket client in (ArrayList)clients.Clone ()) {
                client.Close ();
            }
        }

        private void ServerLoop ()
        {
            if (!BindServerSocket ()) {
                running = false;
                return;
            }

            server.Listen (backlog);

            IPEndPoint ip_endpoint;
            if ((ip_endpoint = server.LocalEndPoint as IPEndPoint) != null) {
                port = (ushort) ip_endpoint.Port;
            }

            Log.DebugFormat ("{0} listening for connections on port {1}", name, port);

            while (true) {
                try {
                    if (!running) {
                        break;
                    }

                    Socket client = server.Accept ();
                    clients.Add (client);
                    ThreadPool.QueueUserWorkItem (HandleConnection, client);
                } catch (SocketException) {
                    break;
                }
            }
        }

        private void HandleConnection (object o)
        {
            Socket client = (Socket) o;

            try {
                while (HandleRequest(client));
            } catch (IOException) {
            } catch (Exception e) {
                Log.Exception (e);
            } finally {
                clients.Remove (client);
                client.Close ();
            }
        }

        protected virtual bool BindServerSocket ()
        {
            server = new Socket (this.EndPoint.AddressFamily, SocketType.Stream, ProtocolType.IP);
            try {
                server.Bind (this.EndPoint);
            } catch (System.Net.Sockets.SocketException e) {
                if (e.SocketErrorCode == SocketError.AddressAlreadyInUse && this.EndPoint is IPEndPoint) {
                    Log.InformationFormat ("Unable to bind {0} to port {1}", name, (EndPoint as IPEndPoint).Port);
                } else {
                    Log.Exception (e);
                }
                return false;
            }

            return true;
        }

        protected virtual long ParseRangeRequest (string line)
        {
            long offset = 0;
            if (String.IsNullOrEmpty (line)) {
                return offset;
            }

            string [] split_line = line.Split (' ', '=', '-');
            foreach (string word in split_line) {
                if (long.TryParse (word, out offset)) {
                    return offset;
                }
            }

            return offset;
        }

        protected virtual bool HandleRequest (Socket client)
        {
            if (client == null || !client.Connected) {
                return false;
            }

            bool keep_connection = true;

            using (StreamReader reader = new StreamReader (new NetworkStream (client, false))) {
                string request_line = reader.ReadLine ();

                if (request_line == null) {
                    return false;
                }

                List <string> request_headers = new List <string> ();
                string line = null;

                do {
                    line = reader.ReadLine ();
                    if (line.ToLower () == "connection: close") {
                        keep_connection = false;
                    }
                    request_headers.Add (line);
                } while (line != String.Empty && line != null);

                string [] split_request_line = request_line.Split ();

                if (split_request_line.Length < 3) {
                    WriteResponse (client, HttpStatusCode.BadRequest, "Bad Request");
                    return keep_connection;
                } else {
                    try {
                        HandleValidRequest (client, split_request_line, request_headers.ToArray () );
                    } catch (IOException) {
                        keep_connection = false;
                    } catch (Exception e) {
                        keep_connection = false;
                        Console.Error.WriteLine("Trouble handling request {0}: {1}", split_request_line[1], e);
                    }
                }
            }

            return keep_connection;
        }

        protected abstract void HandleValidRequest(Socket client, string [] split_request, string [] request_headers);

        protected void WriteResponse (Socket client, HttpStatusCode code, string body)
        {
            WriteResponse (client, code, Encoding.UTF8.GetBytes (body));
        }

        protected virtual void WriteResponse (Socket client, HttpStatusCode code, byte [] body)
        {
            if (client == null || !client.Connected) {
                return;
            }
            else if (body == null) {
                throw new ArgumentNullException ("body");
            }

            StringBuilder headers = new StringBuilder ();
            headers.AppendFormat ("HTTP/1.1 {0} {1}\r\n", (int) code, code.ToString ());
            headers.AppendFormat ("Content-Length: {0}\r\n", body.Length);
            headers.Append ("Content-Type: text/html\r\n");
            headers.Append ("Connection: close\r\n");
            headers.Append ("\r\n");

            using (BinaryWriter writer = new BinaryWriter (new NetworkStream (client, false))) {
                writer.Write (Encoding.UTF8.GetBytes (headers.ToString ()));
                writer.Write (body);
            }

            client.Close ();
        }

        protected void WriteResponseStream (Socket client, Stream response, long length, string filename)
        {
            WriteResponseStream (client, response, length, filename, 0);
        }

        protected virtual void WriteResponseStream (Socket client, Stream response, long length, string filename, long offset)
        {
            if (client == null || !client.Connected) {
                return;
            }
            if (response == null) {
                throw new ArgumentNullException ("response");
            }
            if (length < 1) {
                throw new ArgumentOutOfRangeException ("length", "Must be > 0");
            }
            if (offset < 0) {
                throw new ArgumentOutOfRangeException ("offset", "Must be positive.");
            }

            using (BinaryWriter writer = new BinaryWriter (new NetworkStream (client, false))) {
                StringBuilder headers = new StringBuilder ();

                if (offset > 0) {
                    headers.Append ("HTTP/1.1 206 Partial Content\r\n");
                    headers.AppendFormat ("Content-Range: {0}-{1}\r\n", offset, offset + length);
                } else {
                    headers.Append ("HTTP/1.1 200 OK\r\n");
                }

                if (length > 0) {
                    headers.AppendFormat ("Content-Length: {0}\r\n", length);
                }

                if (filename != null) {
                    headers.AppendFormat ("Content-Disposition: attachment; filename=\"{0}\"\r\n",
                        filename.Replace ("\"", "\\\""));
                }

                headers.Append ("Connection: close\r\n");
                headers.Append ("\r\n");

                writer.Write (Encoding.UTF8.GetBytes (headers.ToString ()));

                using (BinaryReader reader = new BinaryReader (response)) {
                    while (true) {
                        byte [] buffer = reader.ReadBytes (ChunkLength);
                        if (buffer == null) {
                            break;
                        }

                        writer.Write(buffer);

                        if (buffer.Length < ChunkLength) {
                            break;
                        }
                    }
                }
            }
        }

        protected static string Escape (string input)
        {
            return String.IsNullOrEmpty (input) ? "" : System.Web.HttpUtility.HtmlEncode (input);
        }
    }
}