case Packet.RequestHTTPGet:
{
if (player.stage == TcpProtocol.Stage.WebBrowser)
{
// string requestText = reader.ReadString();
// Example of an HTTP request:
// GET / HTTP/1.1
// Host: 127.0.0.1:5127
// Connection: keep-alive
// User-Agent: Chrome/47.0.2526.80
StringBuilder sb
= new StringBuilder
();
string requestText = reader.ReadString();
if (!string.IsNullOrEmpty(requestText))
{
int getIndex = requestText.IndexOf("GET");
int httpIndex = requestText.IndexOf("HTTP");
// lots of sanity checking here. probably not necessary.
if ((getIndex == 0) && (httpIndex > 0) && (requestText.Length > httpIndex + 1))
{
// input: 192.168.1.2:5127/channels/json
// parsed: channels/json
// parts: { "channels", "json" }
requestText = requestText.Substring(5, (httpIndex - 6));
string[] parts = requestText.Split('/');
// proposed format:
// data_type / (opt) output_format / (opt) additional_data
switch (parts[0])
{
case "channels":
{
if (parts.Length > 1)
{
if (parts[1] == "json")
{
// TO-DO:
// return channels data as json
sb.Append("Unsupported");
break;
}
}
// return channels data as raw
for (int index = 0; index < mChannelList.size; index++)
{
Channel ch = mChannelList.buffer[index];
sb.Append(ch.id);
sb.Append("|");
sb.Append(ch.players.size);
sb.Append("|");
sb.Append(ch.playerLimit);
sb.Append("|");
sb.Append(ch.host.name);
sb.Append("|");
sb.Append(ch.level);
sb.Append("|");
sb.AppendLine((ch.isOpen && !ch.isLocked && string.IsNullOrEmpty(ch.password)).ToString());
}
break;
}
}
}
}
else
{
// Server name
sb.Append("Name: ");
sb.AppendLine(name);
// Number of connected clients
sb.Append("Clients: ");
sb.AppendLine(playerCount.ToString());
// Detailed list of clients
for (int i = 0, count = 0; i < mPlayerList.size; ++i)
{
TcpPlayer p = (TcpPlayer)mPlayerList[i];
if (p.stage == TcpProtocol.Stage.Connected)
{
sb.Append(++count);
sb.Append(" ");
sb.AppendLine(p.name);
}
}
}
// Create the header indicating that the connection should be severed after receiving the data
string text = sb.ToString();
sb
= new StringBuilder
(); sb.AppendLine("HTTP/1.1 200 OK");
sb.AppendLine("Server: TNet 3");
sb.AppendLine("Content-Length: " + text.Length);
sb.AppendLine("Content-Type: text/plain");
sb.AppendLine("Connection: Closed\n");
sb.Append(text);
// Send the response
mBuffer = Buffer.Create();
BinaryWriter bw = mBuffer.BeginWriting(false);
bw.Write(Encoding.ASCII.GetBytes(sb.ToString()));
player.SendTcpPacket(mBuffer);
mBuffer.Recycle();
mBuffer = null;
}
break;
}