annotate Net/HTTP.cs @ 6:5ce7a138fdba

Improvements in HTTP server
author Ivo Smits <Ivo@UCIS.nl>
date Tue, 08 Jan 2013 16:38:37 +0100
parents 9e2e6433f57a
children 4b78cc5f116b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
1 ???using System;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
2 using System.Collections.Generic;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
3 using System.IO;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
4 using System.Net;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
5 using System.Net.Sockets;
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
6 using System.Text;
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
7 using UCIS.Net;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
8 using UCIS.Util;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
9 using HTTPHeader = System.Collections.Generic.KeyValuePair<string, string>;
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
10
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
11 namespace UCIS.Net.HTTP {
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
12 public class HTTPServer : TCPServer.IModule, IDisposable {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
13 public IHTTPContentProvider ContentProvider { get; set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
14 public Boolean ServeFlashPolicyFile { get; set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
15 private Socket Listener = null;
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
16
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
17 public HTTPServer() { }
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
18
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
19 public void Listen(int port) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
20 Listen(new IPEndPoint(IPAddress.Any, port));
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
21 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
22
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
23 public void Listen(EndPoint localep) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
24 if (Listener != null) throw new InvalidOperationException("A listener exists");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
25 Listener = new Socket(localep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
26 Listener.Bind(localep);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
27 Listener.Listen(5);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
28 Listener.BeginAccept(AcceptCallback, null);
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
29 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
30
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
31 private void AcceptCallback(IAsyncResult ar) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
32 try {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
33 Socket socket = Listener.EndAccept(ar);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
34 new HTTPContext(this, socket);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
35 } catch (Exception) { }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
36 try {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
37 Listener.BeginAccept(AcceptCallback, null);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
38 } catch (Exception) { }
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
39 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
40
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
41 public void Dispose() {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
42 if (Listener != null) Listener.Close();
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
43 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
44
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
45 bool TCPServer.IModule.Accept(TCPStream stream) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
46 new HTTPContext(this, stream);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
47 return false;
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
48 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
49 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
50
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
51 public class HTTPContext {
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
52 public HTTPServer Server { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
53 public EndPoint LocalEndPoint { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
54 public EndPoint RemoteEndPoint { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
55
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
56 public String RequestMethod { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
57 public String RequestPath { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
58 public String RequestQuery { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
59 public int HTTPVersion { get; set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
60
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
61 public Socket Socket { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
62 public Boolean SuppressStandardHeaders { get; set; }
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
63 public TCPStream TCPStream { get; private set; }
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
64
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
65 private StreamWriter Writer;
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
66 private PrebufferingStream Reader;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
67 private List<HTTPHeader> RequestHeaders;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
68 private HTTPConnectionState State = HTTPConnectionState.Starting;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
69
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
70 private enum HTTPConnectionState {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
71 Starting = 0,
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
72 ReceivingRequest = 1,
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
73 ProcessingRequest = 2,
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
74 SendingHeaders = 3,
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
75 SendingContent = 4,
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
76 Closed = 5,
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
77 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
78
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
79 public HTTPContext(HTTPServer server, TCPStream stream) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
80 this.Server = server;
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
81 this.TCPStream = stream;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
82 this.Socket = stream.Socket;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
83 this.LocalEndPoint = Socket.LocalEndPoint;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
84 this.RemoteEndPoint = Socket.RemoteEndPoint;
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
85 Init(stream);
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
86 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
87
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
88 public HTTPContext(HTTPServer server, Socket socket) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
89 this.Server = server;
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
90 this.TCPStream = null;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
91 this.Socket = socket;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
92 this.LocalEndPoint = socket.LocalEndPoint;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
93 this.RemoteEndPoint = socket.RemoteEndPoint;
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
94 Init(new NetworkStream(socket, true));
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
95 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
96
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
97 private void Init(Stream Stream) {
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
98 Writer = new StreamWriter(Stream, Encoding.ASCII);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
99 Writer.NewLine = "\r\n";
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
100 Writer.AutoFlush = true;
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
101 Reader = new PrebufferingStream(Stream);
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
102 Reader.BeginPrebuffering(PrebufferCallback, null);
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
103 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
104
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
105 private String ReadLine() {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
106 StringBuilder s = new StringBuilder();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
107 while (true) {
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
108 int b = Reader.ReadByte();
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
109 if (b == -1) {
4
9e2e6433f57a Small fix for HTTP Server - Flash Cross Domain Policy File support
Ivo Smits <Ivo@UCIS.nl>
parents: 3
diff changeset
110 if (s.Length == 0) return null;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
111 break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
112 } else if (b == 13) {
4
9e2e6433f57a Small fix for HTTP Server - Flash Cross Domain Policy File support
Ivo Smits <Ivo@UCIS.nl>
parents: 3
diff changeset
113 } else if (b == 10 || b == 0) {
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
114 break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
115 } else {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
116 s.Append((Char)b);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
117 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
118 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
119 return s.ToString();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
120 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
121
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
122 private void PrebufferCallback(IAsyncResult ar) {
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
123 State = HTTPConnectionState.ReceivingRequest;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
124 try {
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
125 Reader.EndPrebuffering(ar);
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
126 String line = ReadLine();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
127 if (line == null) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
128 Close();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
129 return;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
130 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
131 if (Server.ServeFlashPolicyFile && line[0] == '<') { //<policy-file-request/>
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
132 Writer.WriteLine("<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>");
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
133 Reader.WriteByte(0);
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
134 Close();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
135 return;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
136 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
137 String[] request = line.Split(' ');
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
138 if (request.Length != 3) goto SendError400AndClose;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
139 RequestMethod = request[0];
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
140 String RequestAddress = request[1];
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
141 switch (request[2]) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
142 case "HTTP/1.0": HTTPVersion = 10; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
143 case "HTTP/1.1": HTTPVersion = 11; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
144 default: goto SendError400AndClose;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
145 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
146 request = RequestAddress.Split(new Char[] { '?' });
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
147 RequestPath = Uri.UnescapeDataString(request[0]);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
148 RequestQuery = request.Length > 1 ? request[1] : null;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
149 RequestHeaders = new List<HTTPHeader>();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
150 while (true) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
151 line = ReadLine();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
152 if (line == null) goto SendError400AndClose;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
153 if (line.Length == 0) break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
154 request = line.Split(new String[] { ": " }, 2, StringSplitOptions.None);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
155 if (request.Length != 2) goto SendError400AndClose;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
156 RequestHeaders.Add(new HTTPHeader(request[0], request[1]));
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
157 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
158 IHTTPContentProvider content = Server.ContentProvider;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
159 if (content == null) goto SendError500AndClose;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
160 State = HTTPConnectionState.ProcessingRequest;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
161 content.ServeRequest(this);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
162 } catch (Exception ex) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
163 Console.Error.WriteLine(ex);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
164 switch (State) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
165 case HTTPConnectionState.ProcessingRequest: goto SendError500AndClose;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
166 default:
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
167 Close();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
168 break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
169 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
170 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
171 return;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
172
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
173 SendError400AndClose:
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
174 SendErrorAndClose(400);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
175 return;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
176 SendError500AndClose:
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
177 SendErrorAndClose(500);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
178 return;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
179 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
180
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
181 public String GetRequestHeader(String name) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
182 if (State != HTTPConnectionState.ProcessingRequest && State != HTTPConnectionState.SendingHeaders && State != HTTPConnectionState.SendingContent) throw new InvalidOperationException();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
183 foreach (HTTPHeader h in RequestHeaders) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
184 if (name.Equals(h.Key, StringComparison.OrdinalIgnoreCase)) return h.Value;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
185 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
186 return null;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
187 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
188 public String[] GetRequestHeaders(String name) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
189 if (State != HTTPConnectionState.ProcessingRequest && State != HTTPConnectionState.SendingHeaders && State != HTTPConnectionState.SendingContent) throw new InvalidOperationException();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
190 String[] items = new String[0];
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
191 foreach (HTTPHeader h in RequestHeaders) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
192 if (name.Equals(h.Key, StringComparison.OrdinalIgnoreCase)) ArrayUtil.Add(ref items, h.Value);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
193 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
194 return items;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
195 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
196
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
197 public void SendErrorAndClose(int state) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
198 try {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
199 SendStatus(state);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
200 GetResponseStream();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
201 } catch (Exception ex) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
202 Console.Error.WriteLine(ex);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
203 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
204 Close();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
205 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
206
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
207 public void SendStatus(int code) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
208 String message;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
209 switch (code) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
210 case 101: message = "Switching Protocols"; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
211 case 200: message = "OK"; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
212 case 400: message = "Bad Request"; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
213 case 404: message = "Not Found"; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
214 case 500: message = "Internal Server Error"; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
215 default: message = "Unknown Status"; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
216 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
217 SendStatus(code, message);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
218 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
219 public void SendStatus(int code, String message) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
220 if (State != HTTPConnectionState.ProcessingRequest) throw new InvalidOperationException();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
221 StringBuilder sb = new StringBuilder();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
222 sb.Append("HTTP/");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
223 switch (HTTPVersion) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
224 case 10: sb.Append("1.0"); break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
225 case 11: sb.Append("1.1"); break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
226 default: throw new ArgumentException("The HTTP version is not supported", "HTTPVersion");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
227 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
228 sb.Append(" ");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
229 sb.Append(code);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
230 sb.Append(" ");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
231 sb.Append(message);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
232 Writer.WriteLine(sb.ToString());
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
233 State = HTTPConnectionState.SendingHeaders;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
234 if (!SuppressStandardHeaders) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
235 SendHeader("Expires", "Expires: Sun, 1 Jan 2000 00:00:00 GMT");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
236 SendHeader("Cache-Control", "no-store, no-cache, must-revalidate");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
237 SendHeader("Cache-Control", "post-check=0, pre-check=0");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
238 SendHeader("Pragma", "no-cache");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
239 SendHeader("Server", "UCIS Webserver");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
240 SendHeader("Connection", "Close");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
241 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
242 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
243 public void SendHeader(String name, String value) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
244 if (State == HTTPConnectionState.ProcessingRequest) SendStatus(200);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
245 if (State != HTTPConnectionState.SendingHeaders) throw new InvalidOperationException();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
246 Writer.WriteLine(name + ": " + value);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
247 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
248 public Stream GetResponseStream() {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
249 if (State == HTTPConnectionState.ProcessingRequest) SendStatus(200);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
250 if (State == HTTPConnectionState.SendingHeaders) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
251 Writer.WriteLine();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
252 State = HTTPConnectionState.SendingContent;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
253 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
254 if (State != HTTPConnectionState.SendingContent) throw new InvalidOperationException();
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
255 return Reader;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
256 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
257
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
258 public void Close() {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
259 if (State == HTTPConnectionState.Closed) return;
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
260 Reader.Close();
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
261 State = HTTPConnectionState.Closed;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
262 }
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
263 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
264
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
265 public interface IHTTPContentProvider {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
266 void ServeRequest(HTTPContext context);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
267 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
268 public class HTTPPathSelector : IHTTPContentProvider {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
269 private List<KeyValuePair<String, IHTTPContentProvider>> Prefixes;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
270 private StringComparison PrefixComparison;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
271 public HTTPPathSelector() : this(false) { }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
272 public HTTPPathSelector(Boolean caseSensitive) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
273 Prefixes = new List<KeyValuePair<string, IHTTPContentProvider>>();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
274 PrefixComparison = caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
275 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
276 public void AddPrefix(String prefix, IHTTPContentProvider contentProvider) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
277 Prefixes.Add(new KeyValuePair<string, IHTTPContentProvider>(prefix, contentProvider));
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
278 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
279 public void DeletePrefix(String prefix) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
280 Prefixes.RemoveAll(delegate(KeyValuePair<string, IHTTPContentProvider> item) { return prefix.Equals(item.Key, PrefixComparison); });
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
281 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
282 public void ServeRequest(HTTPContext context) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
283 KeyValuePair<string, IHTTPContentProvider> c = Prefixes.Find(delegate(KeyValuePair<string, IHTTPContentProvider> item) { return context.RequestPath.StartsWith(item.Key, PrefixComparison); });
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
284 if (c.Value != null) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
285 c.Value.ServeRequest(context);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
286 } else {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
287 context.SendErrorAndClose(404);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
288 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
289 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
290 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
291 public class HTTPFileProvider : IHTTPContentProvider {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
292 public String FileName { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
293 public String ContentType { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
294 public HTTPFileProvider(String fileName) : this(fileName, "application/octet-stream") { }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
295 public HTTPFileProvider(String fileName, String contentType) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
296 this.FileName = fileName;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
297 this.ContentType = contentType;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
298 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
299 public void ServeRequest(HTTPContext context) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
300 if (File.Exists(FileName)) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
301 using (FileStream fs = File.OpenRead(FileName)) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
302 context.SendStatus(200);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
303 context.SendHeader("Content-Type", ContentType);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
304 context.SendHeader("Content-Length", fs.Length.ToString());
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
305 long left = fs.Length;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
306 Stream response = context.GetResponseStream();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
307 byte[] buffer = new byte[1024 * 10];
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
308 while (fs.CanRead) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
309 int len = fs.Read(buffer, 0, buffer.Length);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
310 if (len <= 0) break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
311 left -= len;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
312 response.Write(buffer, 0, len);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
313 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
314 response.Close();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
315 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
316 } else {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
317 context.SendErrorAndClose(404);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
318 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
319 }
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
320 }
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
321 public class HTTPUnTarchiveProvider : IHTTPContentProvider {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
322 public String TarFileName { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
323 public HTTPUnTarchiveProvider(String tarFile) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
324 this.TarFileName = tarFile;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
325 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
326 public void ServeRequest(HTTPContext context) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
327 if (!File.Exists(TarFileName)) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
328 context.SendErrorAndClose(404);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
329 return;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
330 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
331 String reqname1 = context.RequestPath;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
332 if (reqname1.Length > 0 && reqname1[0] == '/') reqname1 = reqname1.Substring(1);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
333 String reqname2 = reqname1;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
334 if (reqname2.Length > 0 && !reqname2.EndsWith("/")) reqname2 += "/";
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
335 reqname2 += "index.htm";
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
336 using (FileStream fs = File.OpenRead(TarFileName)) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
337 while (true) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
338 Byte[] header = new Byte[512];
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
339 if (fs.Read(header, 0, 512) != 512) break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
340 int flen = Array.IndexOf<Byte>(header, 0, 0, 100);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
341 if (flen == 0) continue;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
342 if (flen == -1) flen = 100;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
343 String fname = Encoding.ASCII.GetString(header, 0, flen);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
344 String fsize = Encoding.ASCII.GetString(header, 124, 11);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
345 int fsizei = Convert.ToInt32(fsize, 8);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
346 if (reqname1.Equals(fname, StringComparison.OrdinalIgnoreCase) || reqname2.Equals(fname)) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
347 context.SendStatus(200);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
348 context.SendHeader("Content-Length", fsizei.ToString());
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
349 String ctype = null;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
350 switch (Path.GetExtension(fname).ToUpperInvariant()) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
351 case ".txt": ctype = "text/plain"; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
352 case ".htm":
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
353 case ".html": ctype = "text/html"; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
354 case ".css": ctype = "text/css"; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
355 case ".js": ctype = "application/x-javascript"; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
356 case ".png": ctype = "image/png"; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
357 case ".jpg":
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
358 case ".jpeg": ctype = "image/jpeg"; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
359 case ".gif": ctype = "image/gif"; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
360 case ".ico": ctype = "image/x-icon"; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
361 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
362 if (ctype != null) context.SendHeader("Content-Type", ctype);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
363 Stream response = context.GetResponseStream();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
364 int left = fsizei;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
365 while (left > 0) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
366 byte[] buffer = new byte[1024 * 10];
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
367 int len = fs.Read(buffer, 0, buffer.Length);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
368 if (len <= 0) break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
369 left -= len;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
370 response.Write(buffer, 0, len);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
371 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
372 response.Close();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
373 return;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
374 } else {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
375 fs.Seek(fsizei, SeekOrigin.Current);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
376 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
377 int padding = fsizei % 512;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
378 if (padding != 0) padding = 512 - padding;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
379 fs.Seek(padding, SeekOrigin.Current);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
380 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
381 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
382 context.SendErrorAndClose(404);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
383 }
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
384 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
385 }