using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace UniWebServer
{
///
/// Headers is a collection class for HTTP style headers.
///
public class Headers
{
///
/// Add a header to the collection.
///
public void Add (string name, string value)
{
GetAll (name).Add (value);
}
///
/// Get the header specified by name from the collection. Returns the first value if more than one is available.
///
public string Get (string name)
{
List header = GetAll (name);
if (header.Count == 0) {
return "";
}
return header [0];
}
///
/// Returns true if the collection contains the header.
///
public bool Contains (string name)
{
List header = GetAll (name);
if (header.Count == 0) {
return false;
}
return true;
}
///
/// Gets all the values of a header.
///
public List GetAll (string name)
{
//name = name.ToLower();
foreach (string key in headers.Keys) {
if (name.ToLower () == key.ToLower ()) {
return headers [key];
}
}
List newHeader = new List ();
headers.Add (name, newHeader);
return newHeader;
}
///
/// Set the specified header to have a single value.
///
public void Set (string name, object value)
{
List header = GetAll (name);
header.Clear ();
header.Add (value.ToString ());
}
///
/// Removes a header from the collection.
///
public void Pop (string name)
{
if (headers.ContainsKey (name)) {
headers.Remove (name);
}
}
///
/// Gets the header names present in the collection.
///
public string[] Keys {
get {
return headers.Keys.ToArray ();
}
}
///
/// Removes all headers and values from the collection.
///
public void Clear ()
{
headers.Clear ();
}
public override string ToString ()
{
var sb = new StringBuilder ();
foreach (string name in headers.Keys) {
foreach (string value in headers[name]) {
sb.AppendFormat ("{0}: {1}\r\n", name, value);
}
}
return sb.ToString ();
}
public void Read (string headerText)
{
var bytes = System.Text.Encoding.UTF8.GetBytes (headerText);
Read (bytes);
}
public void Read (byte[] bytes)
{
var ms = new MemoryStream (bytes);
Read (ms);
}
public void Read (Stream stream)
{
var reader = new StreamReader (stream);
Read (reader);
}
public void AddHeaderLine (string line)
{
var parts = line.Split (new char[] {':'}, 2);
if (parts.Length == 2)
Add (parts [0].Trim (), parts [1].Trim ());
}
public void Read (StreamReader reader)
{
while (true) {
var line = reader.ReadLine ();
if (line == null || line.Trim() == string.Empty)
break;
AddHeaderLine (line);
}
}
Dictionary> headers = new Dictionary> ();
}
}