-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTurKitHTTP.cs
108 lines (92 loc) · 3.75 KB
/
TurKitHTTP.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
using System.Threading;
namespace Soylent
{
class TurKitHTTP
{
private int PORT = 11000;
private Thread serverThread;
public TurKitHTTP()
{
serverThread = new Thread(ListenLoop);
}
/// <summary>
/// Connects to a socket on the local machine and begins listening on the socket.
/// </summary>
public void Listen()
{
serverThread.Start();
}
private void ListenLoop()
{
string[] prefixes = { "http://localhost:" + PORT + "/" };
if (!HttpListener.IsSupported)
{
Debug.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
return;
}
// URI prefixes are required,
// for example "http://contoso.com:8080/index/".
if (prefixes == null || prefixes.Length == 0)
throw new ArgumentException("prefixes");
// Create a listener.
HttpListener listener = new HttpListener();
// Add the prefixes.
foreach (string s in prefixes)
{
listener.Prefixes.Add(s);
}
listener.Start();
Debug.WriteLine("Listening...");
IAsyncResult results = listener.BeginGetContext(new AsyncCallback(GetRequest), listener);
}
public static void GetRequest(IAsyncResult result)
{
HttpListener listener = (HttpListener)result.AsyncState;
// Call EndGetContext to complete the asynchronous operation.
HttpListenerContext context = listener.EndGetContext(result);
HttpListenerRequest request = context.Request;
HandleRequestData(request);
// Obtain a response object.
HttpListenerResponse response = context.Response;
// Construct a response.
string responseString = "received by c#";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// You must close the output stream.
output.Close();
IAsyncResult results = listener.BeginGetContext(new AsyncCallback(GetRequest), listener);
}
public static void HandleRequestData(HttpListenerRequest request)
{
if (!request.HasEntityBody)
{
Debug.WriteLine("No client data was sent with the request.");
return;
}
System.IO.Stream body = request.InputStream;
System.Text.Encoding encoding = request.ContentEncoding;
System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
if (request.ContentType != null)
{
Debug.WriteLine("Client data content type " + request.ContentType);
}
Debug.WriteLine("Client data content length " + request.ContentLength64);
// Convert the data to a string and display it on the Debug.
string message = reader.ReadToEnd();
Debug.WriteLine(message);
TurKitSocKit.HandleSocketMessage(message);
body.Close();
reader.Close();
// If you are finished with the request, it should be closed also.
}
}
}