annotate Net/HTTP.cs @ 84:146a8d224d86

Added ArrayUtil.Remove, fixed some exceptons
author Ivo Smits <Ivo@UCIS.nl>
date Thu, 05 Jun 2014 00:21:02 +0200
parents 50d4aed66c67
children ebdff34b9e4f
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;
10
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
5 using System.Net.Security;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
6 using System.Net.Sockets;
10
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
7 using System.Security.Cryptography.X509Certificates;
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
8 using System.Text;
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
9 using UCIS.Net;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
10 using UCIS.Util;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
11 using HTTPHeader = System.Collections.Generic.KeyValuePair<string, string>;
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
12
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
13 namespace UCIS.Net.HTTP {
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
14 public class HTTPServer : TCPServer.IModule, IDisposable {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
15 public IHTTPContentProvider ContentProvider { get; set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
16 public Boolean ServeFlashPolicyFile { get; set; }
10
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
17 public X509Certificate SSLCertificate { get; set; }
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
18 private Socket Listener = null;
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
19
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
20 public HTTPServer() { }
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
21
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
22 public void Listen(int port) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
23 Listen(new IPEndPoint(IPAddress.Any, port));
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
24 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
25
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
26 public void Listen(EndPoint localep) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
27 if (Listener != null) throw new InvalidOperationException("A listener exists");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
28 Listener = new Socket(localep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
29 Listener.Bind(localep);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
30 Listener.Listen(5);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
31 Listener.BeginAccept(AcceptCallback, null);
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
32 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
33
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
34 private void AcceptCallback(IAsyncResult ar) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
35 try {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
36 Socket socket = Listener.EndAccept(ar);
10
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
37 if (SSLCertificate != null) {
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
38 SslStream ssl = new SslStream(new NetworkStream(socket, true));
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
39 ssl.BeginAuthenticateAsServer(SSLCertificate, SslAuthenticationCallback, new Object[] { socket, ssl });
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
40 } else {
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
41 new HTTPContext(this, socket);
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
42 }
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
43 } catch (Exception) { }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
44 try {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
45 Listener.BeginAccept(AcceptCallback, null);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
46 } catch (Exception) { }
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
47 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
48
10
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
49 private void SslAuthenticationCallback(IAsyncResult ar) {
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
50 Object[] args = (Object[])ar.AsyncState;
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
51 Socket socket = (Socket)args[0];
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
52 SslStream ssl = (SslStream)args[1];
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
53 try {
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
54 ssl.EndAuthenticateAsServer(ar);
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
55 new HTTPContext(this, ssl, socket);
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
56 } catch (Exception) { }
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
57 }
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
58
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
59 public void Dispose() {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
60 if (Listener != null) Listener.Close();
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
61 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
62
45
8df7f4dc5615 HTTP Server: enable KeepAlive option on TCP sockets
Ivo Smits <Ivo@UCIS.nl>
parents: 16
diff changeset
63 public void HandleClient(Socket client) {
8df7f4dc5615 HTTP Server: enable KeepAlive option on TCP sockets
Ivo Smits <Ivo@UCIS.nl>
parents: 16
diff changeset
64 new HTTPContext(this, client);
8df7f4dc5615 HTTP Server: enable KeepAlive option on TCP sockets
Ivo Smits <Ivo@UCIS.nl>
parents: 16
diff changeset
65 }
8df7f4dc5615 HTTP Server: enable KeepAlive option on TCP sockets
Ivo Smits <Ivo@UCIS.nl>
parents: 16
diff changeset
66
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
67 bool TCPServer.IModule.Accept(TCPStream stream) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
68 new HTTPContext(this, stream);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
69 return false;
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
70 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
71 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
72
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
73 public enum HTTPResponseStreamMode {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
74 None = -1,
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
75 Direct = 0,
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
76 Buffered = 1,
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
77 Chunked = 2,
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
78 Hybrid = 3,
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
79 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
80
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
81 public class HTTPContext {
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
82 public HTTPServer Server { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
83 public EndPoint LocalEndPoint { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
84 public EndPoint RemoteEndPoint { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
85
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
86 public String RequestMethod { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
87 public String RequestPath { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
88 public String RequestQuery { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
89 public int HTTPVersion { get; set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
90
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
91 public Socket Socket { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
92 public Boolean SuppressStandardHeaders { get; set; }
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
93 public TCPStream TCPStream { get; private set; }
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
94
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
95 private StreamWriter Writer;
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
96 private PrebufferingStream Reader;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
97 private List<HTTPHeader> RequestHeaders;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
98 private HTTPConnectionState State = HTTPConnectionState.Starting;
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
99 private KeyValuePair<String, String>[] QueryParameters = null, PostParameters = null;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
100 private HTTPOutputStream ResponseStream = null;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
101 private HTTPInputStream RequestStream = null;
10
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
102
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
103 private enum HTTPConnectionState {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
104 Starting = 0,
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
105 ReceivingRequest = 1,
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
106 ProcessingRequest = 2,
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
107 SendingHeaders = 3,
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
108 SendingContent = 4,
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
109 Completed = 5,
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
110 Closed = 6,
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
111 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
112
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
113 private class HTTPOutputStream : Stream {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
114 public HTTPResponseStreamMode Mode { get; private set; }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
115 public HTTPContext Context { get; private set; }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
116 private Stream OutputStream = null;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
117 private MemoryStream Buffer = null;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
118 private long BytesWritten = 0;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
119 private long MaxLength;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
120
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
121 public HTTPOutputStream(HTTPContext context, HTTPResponseStreamMode mode) : this(context, mode, -1) { }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
122 public HTTPOutputStream(HTTPContext context, HTTPResponseStreamMode mode, long length) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
123 this.Context = context;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
124 this.Mode = mode;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
125 this.MaxLength = length;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
126 switch (Mode) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
127 case HTTPResponseStreamMode.Direct:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
128 if (MaxLength != -1) Context.SendHeader("Content-Length", MaxLength.ToString());
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
129 OutputStream = Context.BeginResponseData();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
130 break;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
131 case HTTPResponseStreamMode.Chunked:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
132 Context.SendHeader("Transfer-Encoding", "chunked");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
133 OutputStream = Context.BeginResponseData();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
134 break;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
135 case HTTPResponseStreamMode.Buffered:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
136 case HTTPResponseStreamMode.Hybrid:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
137 if (Context.State != HTTPConnectionState.ProcessingRequest && Context.State != HTTPConnectionState.SendingHeaders) throw new InvalidOperationException("The response stream can not be created in the current state");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
138 break;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
139 default: throw new InvalidOperationException("Response stream mode is not supported");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
140 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
141 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
142
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
143 private void WriteBuffered(byte[] buffer, int offset, int count) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
144 if (Buffer == null) Buffer = new MemoryStream();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
145 Buffer.Write(buffer, offset, count);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
146 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
147 private void WriteChunked(byte[] buffer, int offset, int count) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
148 Byte[] lb = Encoding.ASCII.GetBytes(count.ToString("X") + "\r\n");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
149 OutputStream.Write(lb, 0, lb.Length);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
150 if (count != 0) OutputStream.Write(buffer, offset, count);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
151 OutputStream.Write(new Byte[] { (Byte)'\r', (Byte)'\n' }, 0, 2);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
152 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
153 private void HybridSwitchToChunked() {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
154 MemoryStream oldbuffer = Buffer;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
155 Buffer = null;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
156 Context.SendHeader("Transfer-Encoding", "chunked");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
157 OutputStream = Context.BeginResponseData();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
158 Mode = HTTPResponseStreamMode.Chunked;
84
146a8d224d86 Added ArrayUtil.Remove, fixed some exceptons
Ivo Smits <Ivo@UCIS.nl>
parents: 75
diff changeset
159 if (oldbuffer != null) oldbuffer.WriteTo(this);
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
160 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
161
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
162 public override void Write(byte[] buffer, int offset, int count) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
163 if (offset < 0 || count < 0 || offset + count > buffer.Length) throw new ArgumentOutOfRangeException("buffer", "Offset and count arguments exceed the buffer dimensions");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
164 switch (Mode) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
165 case HTTPResponseStreamMode.Direct:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
166 if (MaxLength != -1 && BytesWritten + count > MaxLength) throw new InvalidOperationException("The write operation exceeds the transfer length");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
167 OutputStream.Write(buffer, offset, count);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
168 BytesWritten += count;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
169 break;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
170 case HTTPResponseStreamMode.Buffered:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
171 WriteBuffered(buffer, offset, count);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
172 break;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
173 case HTTPResponseStreamMode.Chunked:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
174 if (count != 0) WriteChunked(buffer, offset, count);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
175 BytesWritten += count;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
176 break;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
177 case HTTPResponseStreamMode.Hybrid:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
178 if (count > 1024 || (Buffer != null && Buffer.Length + count > 1024)) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
179 HybridSwitchToChunked();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
180 if (count != 0) WriteChunked(buffer, offset, count);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
181 } else {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
182 WriteBuffered(buffer, offset, count);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
183 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
184 break;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
185 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
186 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
187 class CompletedAsyncResult : AsyncResultBase {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
188 public CompletedAsyncResult(AsyncCallback callback, Object state) : base(callback, state) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
189 SetCompleted(true, null);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
190 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
191 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
192 public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
193 if (offset < 0 || count < 0 || offset + count > buffer.Length) throw new ArgumentOutOfRangeException("buffer", "Offset and count arguments exceed the buffer dimensions");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
194 switch (Mode) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
195 case HTTPResponseStreamMode.Direct:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
196 if (MaxLength != -1 && BytesWritten + count > MaxLength) throw new InvalidOperationException("The write operation exceeds the transfer length");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
197 BytesWritten += count;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
198 return OutputStream.BeginWrite(buffer, offset, count, callback, state);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
199 case HTTPResponseStreamMode.Buffered:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
200 case HTTPResponseStreamMode.Chunked:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
201 case HTTPResponseStreamMode.Hybrid:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
202 Write(buffer, offset, count);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
203 return new CompletedAsyncResult(callback, state);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
204 default: return null;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
205 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
206 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
207 public override void EndWrite(IAsyncResult asyncResult) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
208 switch (Mode) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
209 case HTTPResponseStreamMode.Direct:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
210 OutputStream.EndWrite(asyncResult);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
211 break;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
212 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
213 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
214 public override void SetLength(long value) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
215 switch (Mode) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
216 case HTTPResponseStreamMode.Buffered:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
217 case HTTPResponseStreamMode.Hybrid:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
218 if (value != 0) throw new InvalidOperationException("The length can only be set to zero using this method");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
219 Buffer = null;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
220 break;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
221 default: throw new InvalidOperationException("The operation is not supported in the current mode");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
222 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
223 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
224 public override long Length {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
225 get { return MaxLength == -1 ? Position : MaxLength; }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
226 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
227 public override long Position {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
228 get { return (Buffer == null) ? BytesWritten : BytesWritten + Buffer.Length; }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
229 set { throw new NotSupportedException(); }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
230 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
231 public override void Flush() {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
232 switch (Mode) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
233 case HTTPResponseStreamMode.Hybrid:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
234 HybridSwitchToChunked();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
235 break;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
236 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
237 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
238 public override bool CanWrite {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
239 get { return Context.State != HTTPConnectionState.Completed && Context.State != HTTPConnectionState.Closed && (OutputStream == null || OutputStream.CanWrite); }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
240 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
241 protected override void Dispose(bool disposing) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
242 base.Dispose(disposing);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
243 if (disposing) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
244 switch (Mode) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
245 case HTTPResponseStreamMode.Direct:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
246 /*if (MaxLength != -1 && MaxLength > BytesWritten) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
247 long left = MaxLength - BytesWritten;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
248 Byte[] dummy = new Byte[Math.Min(left, 1024)];
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
249 for (; left > 0; left -= dummy.Length) OutputStream.Write(dummy, 0, (int)Math.Min(left, dummy.Length));
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
250 }*/
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
251 break;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
252 case HTTPResponseStreamMode.Chunked:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
253 WriteChunked(null, 0, 0);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
254 Mode = HTTPResponseStreamMode.None;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
255 break;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
256 case HTTPResponseStreamMode.Buffered:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
257 case HTTPResponseStreamMode.Hybrid:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
258 long length = (Buffer == null) ? 0 : Buffer.Length;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
259 Context.SendHeader("Content-Length", length.ToString());
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
260 OutputStream = Context.BeginResponseData();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
261 if (Buffer != null) Buffer.WriteTo(OutputStream);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
262 Buffer = null;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
263 Mode = HTTPResponseStreamMode.None;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
264 break;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
265 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
266 Context.EndResponseData();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
267 if (MaxLength != -1 && MaxLength > BytesWritten) Context.Close();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
268 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
269 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
270 public override bool CanTimeout {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
271 get { return OutputStream == null ? true : OutputStream.CanTimeout; }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
272 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
273 public override int WriteTimeout {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
274 get { return OutputStream == null ? 0 : OutputStream.WriteTimeout; }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
275 set { if (OutputStream != null) OutputStream.WriteTimeout = value; }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
276 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
277
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
278 public override bool CanRead { get { return false; } }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
279 public override bool CanSeek { get { return false; } }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
280
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
281 public override int Read(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
282 public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
283 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
284 private class HTTPInputStream : Stream {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
285 public HTTPResponseStreamMode Mode { get; private set; }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
286 private Stream InputStream = null;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
287 private long BytesRead = 0;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
288 private long BytesLeft = 0;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
289 public HTTPInputStream(HTTPContext context) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
290 String TransferEncoding = context.GetRequestHeader("Transfer-Encoding");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
291 String ContentLength = context.GetRequestHeader("Content-Length");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
292 InputStream = context.Reader;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
293 if (TransferEncoding != null && TransferEncoding.StartsWith("chunked", StringComparison.InvariantCultureIgnoreCase)) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
294 Mode = HTTPResponseStreamMode.Chunked;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
295 } else if (ContentLength != null) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
296 Mode = HTTPResponseStreamMode.Direct;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
297 if (!long.TryParse(ContentLength, out BytesLeft)) BytesLeft = 0;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
298 } else {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
299 Mode = HTTPResponseStreamMode.None;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
300 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
301 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
302 private int ReadDirect(Byte[] buffer, int offset, int count) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
303 if (count > BytesLeft) count = (int)BytesLeft;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
304 if (count == 0) return 0;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
305 int read = InputStream.Read(buffer, offset, count);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
306 if (read >= 0) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
307 BytesRead += read;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
308 BytesLeft -= read;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
309 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
310 return read;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
311 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
312 public override int Read(byte[] buffer, int offset, int count) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
313 if (offset < 0 || count < 0 || offset + count > buffer.Length) throw new ArgumentOutOfRangeException("buffer", "Offset and count arguments exceed the buffer dimensions");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
314 switch (Mode) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
315 case HTTPResponseStreamMode.None:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
316 return 0;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
317 case HTTPResponseStreamMode.Direct:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
318 return ReadDirect(buffer, offset, count);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
319 case HTTPResponseStreamMode.Chunked:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
320 if (BytesLeft == 0) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
321 String length = ReadLine(InputStream);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
322 if (!long.TryParse(length, out BytesLeft)) BytesLeft = 0;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
323 if (BytesLeft == 0) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
324 while (true) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
325 String line = ReadLine(InputStream);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
326 if (line == null || line.Length == 0) break;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
327 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
328 Mode = HTTPResponseStreamMode.None;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
329 return 0;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
330 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
331 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
332 return ReadDirect(buffer, offset, count);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
333 default:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
334 return 0;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
335 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
336 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
337 class CompletedAsyncResult : AsyncResultBase {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
338 public int Count { get; private set; }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
339 public CompletedAsyncResult(AsyncCallback callback, Object state, int count)
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
340 : base(callback, state) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
341 this.Count = count;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
342 SetCompleted(true, null);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
343 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
344 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
345 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
346 if (BytesLeft > 0) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
347 if (count > BytesLeft) count = (int)BytesLeft;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
348 if (count == 0) return new CompletedAsyncResult(callback, state, 0);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
349 return InputStream.BeginRead(buffer, offset, count, callback, state);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
350 } else {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
351 int ret = Read(buffer, offset, count);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
352 return new CompletedAsyncResult(callback, state, ret);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
353 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
354 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
355 public override int EndRead(IAsyncResult asyncResult) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
356 CompletedAsyncResult car = asyncResult as CompletedAsyncResult;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
357 if (car != null) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
358 return car.Count;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
359 } else {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
360 return InputStream.EndRead(asyncResult);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
361 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
362 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
363 public override long Length {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
364 get { return BytesRead + BytesLeft; }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
365 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
366 public override long Position {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
367 get { return BytesRead; }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
368 set { throw new NotSupportedException(); }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
369 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
370 public override bool CanRead {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
371 get { return (BytesLeft > 0 || Mode == HTTPResponseStreamMode.Chunked) && InputStream.CanRead; }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
372 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
373 public override bool CanTimeout {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
374 get { return InputStream.CanTimeout; }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
375 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
376 public override int ReadTimeout {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
377 get { return InputStream.ReadTimeout; }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
378 set { InputStream.ReadTimeout = value; }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
379 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
380 protected override void Dispose(bool disposing) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
381 base.Dispose(disposing);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
382 Byte[] dummy = new Byte[1024];
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
383 while (Read(dummy, 0, dummy.Length) != 0) ;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
384 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
385
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
386 public override bool CanSeek { get { return false; } }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
387 public override bool CanWrite { get { return false; } }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
388 public override void Flush() { }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
389 public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
390 public override void SetLength(long value) { throw new NotSupportedException(); }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
391 public override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
392 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
393
10
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
394 public HTTPContext(HTTPServer server, TCPStream stream) : this(server, stream, stream.Socket) { }
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
395 public HTTPContext(HTTPServer server, Socket socket) : this(server, null, socket) { }
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
396 public HTTPContext(HTTPServer server, Stream stream, Socket socket) {
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
397 this.Server = server;
10
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
398 this.Socket = socket;
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
399 if (socket != null) {
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
400 this.LocalEndPoint = socket.LocalEndPoint;
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
401 this.RemoteEndPoint = socket.RemoteEndPoint;
45
8df7f4dc5615 HTTP Server: enable KeepAlive option on TCP sockets
Ivo Smits <Ivo@UCIS.nl>
parents: 16
diff changeset
402 if (socket.ProtocolType == ProtocolType.Tcp) socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
10
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
403 if (stream == null) stream = new NetworkStream(socket, true);
7269e91c6e26 HTTPServer: added support for HTTPS
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
404 }
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
405 Init(stream);
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
406 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
407
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
408 private void Init(Stream Stream) {
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
409 Writer = new StreamWriter(Stream, Encoding.ASCII);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
410 Writer.NewLine = "\r\n";
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
411 Writer.AutoFlush = true;
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
412 Reader = new PrebufferingStream(Stream);
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
413 Reader.BeginPrebuffering(PrebufferCallback, null);
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
414 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
415
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
416 private static String ReadLine(Stream stream) {
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
417 StringBuilder s = new StringBuilder();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
418 while (true) {
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
419 int b = stream.ReadByte();
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
420 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
421 if (s.Length == 0) return null;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
422 break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
423 } 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
424 } else if (b == 10 || b == 0) {
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
425 break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
426 } else {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
427 s.Append((Char)b);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
428 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
429 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
430 return s.ToString();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
431 }
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
432 private String ReadLine() {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
433 return ReadLine(Reader);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
434 }
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
435
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
436 private void PrebufferCallback(IAsyncResult ar) {
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
437 State = HTTPConnectionState.ReceivingRequest;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
438 try {
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
439 Reader.EndPrebuffering(ar);
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
440 String line = ReadLine();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
441 if (line == null) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
442 Close();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
443 return;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
444 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
445 if (Server.ServeFlashPolicyFile && line[0] == '<') { //<policy-file-request/>
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
446 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
447 Reader.WriteByte(0);
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
448 Close();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
449 return;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
450 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
451 String[] request = line.Split(' ');
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
452 if (request.Length != 3) goto SendError400AndClose;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
453 RequestMethod = request[0];
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
454 String RequestAddress = request[1];
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
455 switch (request[2]) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
456 case "HTTP/1.0": HTTPVersion = 10; break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
457 case "HTTP/1.1": HTTPVersion = 11; break;
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
458 default: goto SendError505AndClose;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
459 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
460 request = RequestAddress.Split(new Char[] { '?' });
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
461 RequestPath = Uri.UnescapeDataString(request[0]);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
462 RequestQuery = request.Length > 1 ? request[1] : null;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
463 RequestHeaders = new List<HTTPHeader>();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
464 while (true) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
465 line = ReadLine();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
466 if (line == null) goto SendError400AndClose;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
467 if (line.Length == 0) break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
468 request = line.Split(new String[] { ": " }, 2, StringSplitOptions.None);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
469 if (request.Length != 2) goto SendError400AndClose;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
470 RequestHeaders.Add(new HTTPHeader(request[0], request[1]));
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
471 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
472 IHTTPContentProvider content = Server.ContentProvider;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
473 if (content == null) goto SendError500AndClose;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
474 State = HTTPConnectionState.ProcessingRequest;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
475 content.ServeRequest(this);
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
476 Close();
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
477 } catch (Exception ex) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
478 Console.Error.WriteLine(ex);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
479 switch (State) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
480 case HTTPConnectionState.ProcessingRequest: goto SendError500AndClose;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
481 default:
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
482 Close();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
483 break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
484 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
485 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
486 return;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
487
54
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
488 SendError400AndClose:
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
489 State = HTTPConnectionState.ProcessingRequest;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
490 SendErrorAndClose(400);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
491 return;
54
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
492 SendError500AndClose:
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
493 State = HTTPConnectionState.ProcessingRequest;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
494 SendErrorAndClose(500);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
495 return;
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
496 SendError505AndClose:
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
497 State = HTTPConnectionState.ProcessingRequest;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
498 SendErrorAndClose(400);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
499 return;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
500 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
501
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
502 public String GetRequestHeader(String name) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
503 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
504 foreach (HTTPHeader h in RequestHeaders) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
505 if (name.Equals(h.Key, StringComparison.OrdinalIgnoreCase)) return h.Value;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
506 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
507 return null;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
508 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
509 public String[] GetRequestHeaders(String name) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
510 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
511 String[] items = new String[0];
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
512 foreach (HTTPHeader h in RequestHeaders) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
513 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
514 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
515 return items;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
516 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
517
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
518 private static String UnescapeUrlDataString(String text) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
519 return Uri.UnescapeDataString(text.Replace('+', ' '));
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
520 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
521 private static KeyValuePair<String, String>[] DecodeUrlEncodedFields(String data) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
522 List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
523 foreach (String arg in data.Split('&')) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
524 String[] parts = arg.Split(new Char[] { '=' }, 2);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
525 String key = UnescapeUrlDataString(parts[0]);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
526 String value = (parts.Length > 1) ? UnescapeUrlDataString(parts[1]) : String.Empty;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
527 list.Add(new KeyValuePair<string, string>(key, value));
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
528 }
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
529 return list.ToArray();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
530 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
531
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
532 public String GetQueryParameter(String name) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
533 foreach (KeyValuePair<String, String> kvp in GetQueryParameters()) if (kvp.Key == name) return kvp.Value;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
534 return null;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
535 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
536 public String[] GetQueryParameters(String name) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
537 List<String> list = new List<string>();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
538 foreach (KeyValuePair<String, String> kvp in GetQueryParameters()) if (kvp.Key == name) list.Add(kvp.Value);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
539 return list.ToArray();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
540 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
541 public KeyValuePair<String, String>[] GetQueryParameters() {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
542 if (QueryParameters == null) QueryParameters = DecodeUrlEncodedFields(RequestQuery);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
543 return QueryParameters;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
544 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
545
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
546 public String GetPostParameter(String name) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
547 foreach (KeyValuePair<String, String> kvp in GetPostParameters()) if (kvp.Key == name) return kvp.Value;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
548 return null;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
549 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
550 public String[] GetPostParameters(String name) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
551 List<String> list = new List<string>();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
552 foreach (KeyValuePair<String, String> kvp in GetPostParameters()) if (kvp.Key == name) list.Add(kvp.Value);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
553 return list.ToArray();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
554 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
555 public KeyValuePair<String, String>[] GetPostParameters() {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
556 if (PostParameters == null) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
557 if (RequestMethod == "POST" && GetRequestHeader("Content-Type") == "application/x-www-form-urlencoded") {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
558 String data;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
559 using (StreamReader reader = new StreamReader(OpenRequestStream(), Encoding.UTF8)) data = reader.ReadToEnd();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
560 PostParameters = DecodeUrlEncodedFields(data);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
561 } else {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
562 PostParameters = new KeyValuePair<string, string>[0];
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
563 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
564 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
565 return PostParameters;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
566 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
567
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
568 public Stream OpenRequestStream() {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
569 if (RequestStream == null) RequestStream = new HTTPInputStream(this);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
570 return RequestStream;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
571 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
572
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
573 private static String GetMessageForStatus(int code) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
574 switch (code) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
575 case 101: return "Switching Protocols";
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
576 case 200: return "OK";
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
577 case 301: return "Moved Permanently";
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
578 case 302: return "Found";
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
579 case 303: return "See Other";
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
580 case 304: return "Not Modified";
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
581 case 307: return "Temporary Redirect";
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
582 case 400: return "Bad Request";
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
583 case 401: return "Access denied";
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
584 case 403: return "Forbidden";
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
585 case 404: return "Not Found";
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
586 case 500: return "Internal Server Error";
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
587 case 505: return "HTTP Version Not Supported";
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
588 default: return "Unknown Status";
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
589 }
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
590 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
591
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
592 public void SendStatus(int code) {
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
593 String message = GetMessageForStatus(code);
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
594 SendStatus(code, message);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
595 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
596 public void SendStatus(int code, String message) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
597 if (State != HTTPConnectionState.ProcessingRequest) throw new InvalidOperationException();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
598 StringBuilder sb = new StringBuilder();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
599 sb.Append("HTTP/");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
600 switch (HTTPVersion) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
601 case 10: sb.Append("1.0"); break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
602 case 11: sb.Append("1.1"); break;
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
603 default: sb.Append("1.0"); break;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
604 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
605 sb.Append(" ");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
606 sb.Append(code);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
607 sb.Append(" ");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
608 sb.Append(message);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
609 Writer.WriteLine(sb.ToString());
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
610 State = HTTPConnectionState.SendingHeaders;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
611 if (!SuppressStandardHeaders) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
612 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
613 SendHeader("Cache-Control", "no-store, no-cache, must-revalidate");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
614 SendHeader("Cache-Control", "post-check=0, pre-check=0");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
615 SendHeader("Pragma", "no-cache");
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
616 SendHeader("Server", "UCIS Embedded Webserver");
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
617 SendHeader("Connection", "Close");
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
618 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
619 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
620 public void SendHeader(String name, String value) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
621 if (State == HTTPConnectionState.ProcessingRequest) SendStatus(200);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
622 if (State != HTTPConnectionState.SendingHeaders) throw new InvalidOperationException();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
623 Writer.WriteLine(name + ": " + value);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
624 }
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
625 public void SendErrorResponse(int state) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
626 String message = GetMessageForStatus(state);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
627 try {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
628 SendStatus(state, message);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
629 SendHeader("Content-Type", "text/plain");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
630 WriteResponseData(Encoding.ASCII.GetBytes(String.Format("Error {0}: {1}", state, message)));
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
631 } catch (Exception ex) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
632 Console.Error.WriteLine(ex);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
633 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
634 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
635 public Stream OpenResponseStream(HTTPResponseStreamMode mode) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
636 if (ResponseStream != null) throw new InvalidOperationException("The response stream has already been opened");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
637 return ResponseStream = new HTTPOutputStream(this, mode);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
638 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
639 public Stream OpenResponseStream(long length) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
640 if (ResponseStream != null) throw new InvalidOperationException("The response stream has already been opened");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
641 if (length < 0) throw new ArgumentException("Response length can not be negative", "length");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
642 return ResponseStream = new HTTPOutputStream(this, HTTPResponseStreamMode.Direct, length);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
643 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
644 public void WriteResponseData(Byte[] buffer) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
645 WriteResponseData(buffer, 0, buffer.Length);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
646 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
647 public void WriteResponseData(Byte[] buffer, int offset, int count) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
648 if (offset < 0 || count < 0 || offset + count > buffer.Length) throw new ArgumentOutOfRangeException("buffer", "Offset and count arguments exceed the buffer dimensions");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
649 SendHeader("Content-Length", count.ToString());
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
650 Stream stream = BeginResponseData();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
651 stream.Write(buffer, offset, count);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
652 EndResponseData();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
653 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
654 private Stream BeginResponseData() {
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
655 if (State == HTTPConnectionState.ProcessingRequest) SendStatus(200);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
656 if (State == HTTPConnectionState.SendingHeaders) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
657 Writer.WriteLine();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
658 State = HTTPConnectionState.SendingContent;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
659 }
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
660 if (State != HTTPConnectionState.SendingContent) throw new InvalidOperationException("The response stream can not be opened in the current state");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
661 return Reader;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
662 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
663 private void EndResponseData() {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
664 if (State == HTTPConnectionState.Completed || State == HTTPConnectionState.Closed) return;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
665 OpenRequestStream().Close();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
666 if (State != HTTPConnectionState.SendingContent) WriteResponseData(new Byte[0]);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
667 State = HTTPConnectionState.Completed;
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
668 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
669
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
670 public Stream GetDirectStream() {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
671 if (State == HTTPConnectionState.Closed) throw new InvalidOperationException("The context has been closed");
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
672 BeginResponseData();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
673 State = HTTPConnectionState.Closed;
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
674 return Reader;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
675 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
676
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
677 private void SendErrorAndClose(int code) {
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
678 SendErrorResponse(code);
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
679 Close();
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
680 }
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
681 private void Close() {
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
682 if (State == HTTPConnectionState.Closed) return;
6
5ce7a138fdba Improvements in HTTP server
Ivo Smits <Ivo@UCIS.nl>
parents: 4
diff changeset
683 Reader.Close();
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
684 State = HTTPConnectionState.Closed;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
685 }
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
686 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
687
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
688 public interface IHTTPContentProvider {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
689 void ServeRequest(HTTPContext context);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
690 }
12
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents: 10
diff changeset
691 public delegate void HTTPContentProviderDelegate(HTTPContext context);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents: 10
diff changeset
692 public class HTTPContentProviderFunction : IHTTPContentProvider {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents: 10
diff changeset
693 public HTTPContentProviderDelegate Handler { get; private set; }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents: 10
diff changeset
694 public HTTPContentProviderFunction(HTTPContentProviderDelegate handler) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents: 10
diff changeset
695 this.Handler = handler;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents: 10
diff changeset
696 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents: 10
diff changeset
697 public void ServeRequest(HTTPContext context) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents: 10
diff changeset
698 Handler(context);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents: 10
diff changeset
699 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents: 10
diff changeset
700 }
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
701 public class HTTPPathSelector : IHTTPContentProvider {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
702 private List<KeyValuePair<String, IHTTPContentProvider>> Prefixes;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
703 private StringComparison PrefixComparison;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
704 public HTTPPathSelector() : this(false) { }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
705 public HTTPPathSelector(Boolean caseSensitive) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
706 Prefixes = new List<KeyValuePair<string, IHTTPContentProvider>>();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
707 PrefixComparison = caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
708 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
709 public void AddPrefix(String prefix, IHTTPContentProvider contentProvider) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
710 Prefixes.Add(new KeyValuePair<string, IHTTPContentProvider>(prefix, contentProvider));
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
711 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
712 public void DeletePrefix(String prefix) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
713 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
714 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
715 public void ServeRequest(HTTPContext context) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
716 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
717 if (c.Value != null) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
718 c.Value.ServeRequest(context);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
719 } else {
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
720 context.SendErrorResponse(404);
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
721 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
722 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
723 }
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
724 public class HTTPStaticContent : IHTTPContentProvider {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
725 public ArraySegment<Byte> ContentBuffer { get; set; }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
726 public String ContentType { get; set; }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
727 public HTTPStaticContent() : this(new ArraySegment<Byte>()) { }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
728 public HTTPStaticContent(ArraySegment<Byte> content) : this(content, "application/octet-stream") { }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
729 public HTTPStaticContent(String content, String contentType) : this(Encoding.UTF8.GetBytes(content), contentType) { }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
730 public HTTPStaticContent(String contentType) : this(new ArraySegment<Byte>(), contentType) { }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
731 public HTTPStaticContent(Byte[] content, String contentType) : this(new ArraySegment<Byte>(content), contentType) { }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
732 public HTTPStaticContent(ArraySegment<Byte> content, String contentType) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
733 this.ContentBuffer = content;
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
734 this.ContentType = contentType;
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
735 }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
736 public void SetContent(Byte[] bytes) { ContentBuffer = new ArraySegment<byte>(bytes); }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
737 public void SetContent(Byte[] bytes, int offset, int count) { ContentBuffer = new ArraySegment<byte>(bytes, offset, count); }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
738 public void SetContent(String content, Encoding encoding) { SetContent(encoding.GetBytes(content)); }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
739 public void SetContent(String content) { SetContent(content, Encoding.UTF8); }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
740 public void ServeRequest(HTTPContext context) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
741 ArraySegment<Byte> content = ContentBuffer;
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
742 if (content.Array == null) {
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
743 context.SendErrorResponse(404);
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
744 return;
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
745 }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
746 String contentType = ContentType;
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
747 context.SendStatus(200);
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
748 if (contentType != null) context.SendHeader("Content-Type", contentType);
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
749 context.WriteResponseData(content.Array, content.Offset, content.Count);
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
750 }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 6
diff changeset
751 }
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
752 public class HTTPFileProvider : IHTTPContentProvider {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
753 public String FileName { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
754 public String ContentType { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
755 public HTTPFileProvider(String fileName) : this(fileName, "application/octet-stream") { }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
756 public HTTPFileProvider(String fileName, String contentType) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
757 this.FileName = fileName;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
758 this.ContentType = contentType;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
759 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
760 public void ServeRequest(HTTPContext context) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
761 if (File.Exists(FileName)) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
762 using (FileStream fs = File.OpenRead(FileName)) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
763 context.SendStatus(200);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
764 context.SendHeader("Content-Type", ContentType);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
765 long left = fs.Length;
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
766 Stream response = context.OpenResponseStream(fs.Length);
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
767 byte[] buffer = new byte[1024 * 10];
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
768 while (fs.CanRead) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
769 int len = fs.Read(buffer, 0, buffer.Length);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
770 if (len <= 0) break;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
771 left -= len;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
772 response.Write(buffer, 0, len);
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
773 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
774 response.Close();
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
775 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
776 } else {
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
777 context.SendErrorResponse(404);
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
778 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
779 }
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
780 }
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
781 public class HTTPUnTarchiveProvider : IHTTPContentProvider {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
782 public String TarFileName { get; private set; }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
783 public HTTPUnTarchiveProvider(String tarFile) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
784 this.TarFileName = tarFile;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
785 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
786 public void ServeRequest(HTTPContext context) {
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
787 if (!File.Exists(TarFileName)) {
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
788 context.SendErrorResponse(404);
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
789 return;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
790 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
791 String reqname1 = context.RequestPath;
54
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
792 if (reqname1.StartsWith("/")) reqname1 = reqname1.Substring(1);
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
793 String reqname2 = reqname1;
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
794 if (reqname2.Length > 0 && !reqname2.EndsWith("/")) reqname2 += "/";
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
795 reqname2 += "index.htm";
54
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
796 foreach (TarchiveEntry file in new TarchiveReader(TarFileName)) {
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
797 if (!file.IsFile) continue;
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
798 if (!reqname1.Equals(file.Name, StringComparison.OrdinalIgnoreCase) && !reqname2.Equals(file.Name, StringComparison.OrdinalIgnoreCase)) continue;
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
799 context.SendStatus(200);
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
800 String ctype = null;
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
801 switch (Path.GetExtension(file.Name).ToLowerInvariant()) {
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
802 case ".txt": ctype = "text/plain"; break;
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
803 case ".htm":
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
804 case ".html": ctype = "text/html"; break;
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
805 case ".css": ctype = "text/css"; break;
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
806 case ".js": ctype = "application/x-javascript"; break;
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
807 case ".png": ctype = "image/png"; break;
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
808 case ".jpg":
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
809 case ".jpeg": ctype = "image/jpeg"; break;
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
810 case ".gif": ctype = "image/gif"; break;
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
811 case ".ico": ctype = "image/x-icon"; break;
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
812 }
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
813 if (ctype != null) context.SendHeader("Content-Type", ctype);
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
814 using (Stream response = context.OpenResponseStream(file.Size), source = file.GetStream()) {
54
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
815 byte[] buffer = new byte[Math.Min(source.Length, 1024 * 10)];
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
816 while (source.CanRead) {
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
817 int len = source.Read(buffer, 0, buffer.Length);
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
818 if (len <= 0) break;
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
819 response.Write(buffer, 0, len);
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
820 }
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
821 }
54
ba4e2cb031e0 Added general purpose tar archive reader class
Ivo Smits <Ivo@UCIS.nl>
parents: 45
diff changeset
822 return;
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
823 }
75
50d4aed66c67 Improved HTTP classes
Ivo Smits <Ivo@UCIS.nl>
parents: 54
diff changeset
824 context.SendErrorResponse(404);
3
0cc7be03775f Improved HTTP Server components
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
825 }
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
826 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
827 }