aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Common/XmlRpcCS/SimpleHttpRequest.cs
blob: e5326c3d9011ccf868bacc6dc44d929fec4fb30e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
namespace Nwc.XmlRpc
{
    using System;
    using System.IO;
    using System.Net.Sockets;
    using System.Collections;

    ///<summary>Very basic HTTP request handler.</summary>
    ///<remarks>This class is designed to accept a TcpClient and treat it as an HTTP request.
    /// It will do some basic header parsing and manage the input and output streams associated
    /// with the request.</remarks>
    public class SimpleHttpRequest
    {
        private String _httpMethod = null;
        private String _protocol;
        private String _filePathFile = null;
        private String _filePathDir = null;
        private String __filePath;
        private TcpClient _client;
        private StreamReader _input;
        private StreamWriter _output;
        private Hashtable _headers;

        /// <summary>A constructor which accepts the TcpClient.</summary>
        /// <remarks>It creates the associated input and output streams, determines the request type,
        /// and parses the remaining HTTP header.</remarks>
        /// <param name="client">The <c>TcpClient</c> associated with the HTTP connection.</param>
        public SimpleHttpRequest(TcpClient client)
        {
            _client = client;
            _output = new StreamWriter(client.GetStream());
            _input = new StreamReader(client.GetStream());
            GetRequestMethod();
            GetRequestHeaders();
        }

        /// <summary>The output <c>StreamWriter</c> associated with the request.</summary>
        public StreamWriter Output
        {
            get { return _output; }
        }

        /// <summary>The input <c>StreamReader</c> associated with the request.</summary>
        public StreamReader Input
        {
            get { return _input; }
        }

        /// <summary>The <c>TcpClient</c> with the request.</summary>
        public TcpClient Client
        {
            get { return _client; }
        }

        private String _filePath
        {
            get { return __filePath; }
            set
            {
                __filePath = value;
                _filePathDir = null;
                _filePathFile = null;
            }
        }

        /// <summary>The type of HTTP request (i.e. PUT, GET, etc.).</summary>
        public String HttpMethod
        {
            get { return _httpMethod; }
        }

        /// <summary>The level of the HTTP protocol.</summary>
        public String Protocol
        {
            get { return _protocol; }
        }

        /// <summary>The "path" which is part of any HTTP request.</summary>
        public String FilePath
        {
            get { return _filePath; }
        }

        /// <summary>The file portion of the "path" which is part of any HTTP request.</summary>
        public String FilePathFile
        {
            get
            {
                if (_filePathFile != null)
                    return _filePathFile;

                int i = FilePath.LastIndexOf("/");

                if (i == -1)
                    return "";

                i++;
                _filePathFile = FilePath.Substring(i, FilePath.Length - i);
                return _filePathFile;
            }
        }

        /// <summary>The directory portion of the "path" which is part of any HTTP request.</summary>
        public String FilePathDir
        {
            get
            {
                if (_filePathDir != null)
                    return _filePathDir;

                int i = FilePath.LastIndexOf("/");

                if (i == -1)
                    return "";

                i++;
                _filePathDir = FilePath.Substring(0, i);
                return _filePathDir;
            }
        }

        private void GetRequestMethod()
        {
            string req = _input.ReadLine();
            if (req == null)
                throw new ApplicationException("Void request.");

            if (0 == String.Compare("GET ", req.Substring(0, 4), true))
                _httpMethod = "GET";
            else if (0 == String.Compare("POST ", req.Substring(0, 5), true))
                _httpMethod = "POST";
            else
                throw new InvalidOperationException("Unrecognized method in query: " + req);

            req = req.TrimEnd();
            int idx = req.IndexOf(' ') + 1;
            if (idx >= req.Length)
                throw new ApplicationException("What do you want?");

            string page_protocol = req.Substring(idx);
            int idx2 = page_protocol.IndexOf(' ');
            if (idx2 == -1)
                idx2 = page_protocol.Length;

            _filePath = page_protocol.Substring(0, idx2).Trim();
            _protocol = page_protocol.Substring(idx2).Trim();
        }

        private void GetRequestHeaders()
        {
            String line;
            int idx;

            _headers = new Hashtable();

            while ((line = _input.ReadLine()) != "")
            {
                if (line == null)
                {
                    break;
                }

                idx = line.IndexOf(':');
                if (idx == -1 || idx == line.Length - 1)
                {
                    Logger.WriteEntry("Malformed header line: " + line, LogLevel.Information);
                    continue;
                }

                String key = line.Substring(0, idx);
                String value = line.Substring(idx + 1);

                try
                {
                    _headers.Add(key, value);
                }
                catch (Exception)
                {
                    Logger.WriteEntry("Duplicate header key in line: " + line, LogLevel.Information);
                }
            }
        }

        /// <summary>
        /// Format the object contents into a useful string representation.
        /// </summary>
        ///<returns><c>String</c> representation of the <c>SimpleHttpRequest</c> as the <i>HttpMethod FilePath Protocol</i>.</returns>
        override public String ToString()
        {
            return HttpMethod + " " + FilePath + " " + Protocol;
        }

        /// <summary>
        /// Close the <c>SimpleHttpRequest</c>. This flushes and closes all associated io streams.
        /// </summary>
        public void Close()
        {
            _output.Flush();
            _output.Close();
            _input.Close();
            _client.Close();
        }
    }
}