public override HttpMessageWriter ProcessPutItem(ByteArrayPart username, ByteArrayPart domain, HttpMessageReader reader, ArraySegment<byte> content)
		{
			var statusCode = StatusCodes.OK;

			int sipIfMatch = simpleModule.InvalidEtag;
			if (reader.Count.IfMatches > 0)
			{
				if (HexEncoding.TryParseHex8(reader.IfMatches[0].Bytes, reader.IfMatches[0].Begin, out sipIfMatch) == false)
					statusCode = StatusCodes.PreconditionFailed;
			}

			if (statusCode == StatusCodes.OK)
			{
				if (simpleModule.Publish(username.ToString() + "@" + domain.ToString(), ref sipIfMatch, 60, content) == false)
					statusCode = StatusCodes.BadRequest;
			}

			HttpMessageWriter response;
			if (statusCode != StatusCodes.OK)
			{
				response = CreateResponse(statusCode);
			}
			else
			{
				response = CreateNotFinishedResponse(statusCode, ContentType.None);

				response.WriteEtag(sipIfMatch);
				response.WriteCRLF();
			}

			return response;
		}
Пример #2
0
		private void InitializeHttpReader(bool forceDefaultValue)
		{
			if (httpReader == null)
			{
				httpReader = new HttpMessageReader();
				httpReader.SetDefaultValue();
			}
			else if (forceDefaultValue)
			{
				httpReader.SetDefaultValue();
			}
		}
Пример #3
0
		public void SetUp()
		{
			{
				var dfa = new SipMessageReader();
				dfa.LoadTables(@"..\..\..\Server\dll\Sip.Message.dfa");
				dfa.SetDefaultValue();
				dfa.Parse(new byte[] { 0 }, 0, 1);
			}
			{
				HttpMessageReader.LoadTables(@"..\..\..\Server\dll\");

				var dfa = new HttpMessageReader();
				dfa.SetDefaultValue();
				dfa.Parse(new byte[] { 0 }, 0, 1);
			}
		}
Пример #4
0
		static void Main(string[] args)
		{
			Console.Write("Loading...");
			int start = Environment.TickCount;

			int loadTablesDelay = Environment.TickCount - start;
			HttpMessageReader.LoadTables(@"..\..\..\Http.Message\bin\Release");
			start = Environment.TickCount;
			var dfa = new HttpMessageReader();
			dfa.SetDefaultValue();
			dfa.Parse(new byte[] { 0 }, 0, 1);
			Console.WriteLine("Done (LoadTables {0} ms + JIT {1} ms)", loadTablesDelay, Environment.TickCount - start);

			var utf = new UTF8Encoding();

			var message0 = utf.GetBytes(
				"POST /enlighten/calais.asmx/Enlighten HTTP/1.1\r\n" +
				"Test: test\r\n" +
				"Host: api.opencalais.com:9000\r\n" +
				"Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n" +
				//"Content-Type: application/x-www-form-urlencoded\r\n" +
				"Content-Length: 123\r\n" +
				"Upgrade: first, websocket, websocketex, websocket/v15, second/567,     third\r\n" +
				"Referer: http://localhost/#ffff\r\n" +
				//"Referer: referer.com\r\n" +
				"Origin: http://example.com\r\n" +
				"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" +
				"Sec-WebSocket-Protocol: chat  ,   superchat\r\n" +
				"Cookie: session-id1=1; session-id2=2\r\n" +
				"Cookie: session-id3=3; session-id4=4\r\n" +
				"Sec-WebSocket-Version: 13\r\n" +
				"If-Match: \"01234567\"\r\n" +
				"\r\n");

			dfa.SetDefaultValue();
			int proccessed = dfa.Parse(message0, 0, message0.Length);
			dfa.SetArray(message0);

			Console.WriteLine("Total: {0}", message0.Length);
			Console.WriteLine("Proccessed: {0}", proccessed);
			Console.WriteLine("Final: {0}", dfa.IsFinal);
			Console.WriteLine("Error: {0}", dfa.IsError);

			Console.WriteLine("Method: {0}", dfa.Method);
			Console.WriteLine("Request-URI: |{0}|", dfa.RequestUri.ToString());
			Console.WriteLine("Host: |{0}| : {1}", dfa.Host.Host.ToString(), dfa.Host.Port);
			Console.WriteLine("Content-Type: |{0}|", dfa.ContentType.Value.ToString());
			Console.WriteLine("Content-Length: {0}", dfa.ContentLength);
			Console.WriteLine("Referer: |{0}|", dfa.Referer.ToString());

			Console.WriteLine("Cookie");
			for (int i = 0; i < dfa.Count.Cookie; i++)
				Console.WriteLine("  #{0} |{1}| : |{2}|", i, dfa.Cookie[i].Name.ToString(), dfa.Cookie[i].Value.ToString());

			//Console.WriteLine("Upgrade");
			//for (int i = 0; i < dfa.Count.UpgradeCount; i++)
			//    Console.WriteLine("  #{0} |{1}| + |{2}| = |{3}| ({4})", i, dfa.Upgrades[i].Name.ToString(), dfa.Upgrades[i].Version.ToString(), dfa.Upgrades[i].Value.ToString(), dfa.Upgrades[i].Upgrate);

			Console.Write("Upgrade: ");
			for (int i = 0; i < dfa.Count.Upgrade; i++)
				Console.Write("{0}, ", dfa.Upgrade[i]);
			Console.WriteLine();

			Console.Write("If-Match: ");
			for (int i = 0; i < dfa.Count.IfMatches; i++)
				Console.Write("{0}, ", dfa.IfMatches[i].ToString());
			Console.WriteLine();

			Console.WriteLine("Sec-WebSocket-Key: |{0}|", dfa.SecWebSocketKey.ToString());
			Console.Write("Sec-WebSocket-Protocol: ");
			for (int i = 0; i < dfa.Count.SecWebSocketProtocol; i++)
				Console.Write("|{0}|, ", dfa.SecWebSocketProtocol[i].ToString());
			Console.WriteLine();
			Console.WriteLine("Sec-WebSocket-Version: {0}", dfa.SecWebSocketVersion);

			var message1 = utf.GetBytes(
				"GET /api/role?time=1343516363983&sig=4c25f6162d70ede434b37571cbe23201 HTTP/1.1\r\n" +
				"Accept: */*\r\n" +
				"Content-Type: application/json\r\n" +
				"X-Requested-With: XMLHttpRequest\r\n" +
				"Referer: http://localhost/#\r\n" +
				"Accept-Language: ru\r\n" +
				"Accept-Encoding: gzip, deflate\r\n" +
				"User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)\r\n" +
				"Host: localhost\r\n" +
				"Connection: Keep-Alive\r\n" +
				"\r\n");


			//TestSpeed(dfa, message0);
		}
Пример #5
0
		private static void TestSpeed(HttpMessageReader dfa, byte[] message)
		{
			Console.WriteLine("Testing speed");

			int repeat = 1000000;
			int start2 = Environment.TickCount;
			for (int i = 0; i < repeat; i++)
			{
				dfa.SetDefaultValue();
				dfa.Parse(message, 0, message.Length);
				dfa.SetArray(message);
			}
			int spent = Environment.TickCount - start2;

			Console.WriteLine("Parsed {0} times, {1} ms", repeat, spent);
		}
Пример #6
0
		void IHttpServerAgent.HandleRequest(BaseConnection c, HttpMessageReader httpReader, ArraySegment<byte> httpContent)
		{
			if (httpReader.Method != Methods.Get)
			{
				var writer = new HttpMessageWriter();
				writer.WriteResponse(StatusCodes.NotAcceptable);

				httpServer.SendResponse(c, writer);
			}
			else
			{
				if (httpReader.RequestUri.Equals(uri1))
				{
					var writer = new HttpMessageWriter();

					writer.WriteStatusLine(StatusCodes.MovedPermanently);
					writer.WriteContentLength(0);
					writer.WriteLocation(c.LocalEndPoint.Protocol == ServerProtocol.Tcp, httpReader.Host.Host, httpReader.Host.Port, uri2);
					writer.WriteCRLF();

					httpServer.SendResponse(c, writer);
				}
				else
				{
					var rawFile = GetFile(httpReader);

					if (rawFile.HasValue == false)
					{
						var writer = new HttpMessageWriter();
						writer.WriteResponse(StatusCodes.NotFound);

						httpServer.SendResponse(c, writer);
					}
					else
					{
						var file = rawFile.Value;

						var writer = new HttpMessageWriter();
						writer.WriteStatusLine(StatusCodes.OK);
						writer.WriteContentType(file.ContentType);
						writer.WriteContentLength(file.Content.Length);
						writer.WriteCRLF();

						httpServer.SendResponse(c, writer);

						httpServer.SendResponse(c, new ArraySegment<byte>(file.Content));
					}
				}
			}
		}
Пример #7
0
		bool IHttpServerAgent.IsAuthorized(HttpMessageReader reader, ByteArrayPart username)
		{
			return true;
		}
Пример #8
0
		HttpServerAgent.IsHandledResult IHttpServerAgent.IsHandled(HttpMessageReader httpReader)
		{
			return (string.IsNullOrEmpty(path) == false) &&
				(httpReader.RequestUri.Equals(uri1) || httpReader.RequestUri.StartsWith(uri2));
		}
Пример #9
0
		private HttpFile? GetFile(HttpMessageReader reader)
		{
			var uri = reader.RequestUri.ToString(uri1.Length);
			if (uri == null)
				return null;

			int parameters = uri.IndexOf('?');
			if (parameters != -1)
				uri = uri.Substring(0, parameters);

			lock (files)
			{
				HttpFile file;
				if (uri.EndsWith("/") == false)
				{
					if (files.TryGetValue(uri, out file))
						return file;
				}
				else
				{
					foreach (var defaultName in defaultFiles)
						if (files.TryGetValue(uri + defaultName, out file))
							return file;
				}
			}

			return null;
		}
Пример #10
0
public static int CompileParseMethod()
{
int start = Environment.TickCount;
var reader = new HttpMessageReader();
reader.SetDefaultValue();
reader.Parse(new byte[] { 0 }, 0, 1);
return Environment.TickCount - start;
}
Пример #11
0
			public static void Update(int currentConnectionId, ArraySegment<byte> header)
			{
				connectionId = currentConnectionId;

				if (Value == null)
					Value = new HttpMessageReader();

				Value.SetDefaultValue();
				Value.Parse(header.Array, header.Offset, header.Count);
				Value.SetArray(header.Array);
			}
Пример #12
0
			public static HttpMessageReader GetEmpty()
			{
				connectionId = -1;

				if (Value == null)
					Value = new HttpMessageReader();
				Value.SetDefaultValue();

				return Value;
			}
Пример #13
0
		private static int GetSessionId(HttpMessageReader httpReader)
		{
			var prefix = @"id=";
			var requestUri = httpReader.RequestUri.ToString();

			int s = requestUri.IndexOf(prefix);
			if (s >= 0)
			{
				s += prefix.Length;

				int result = 0;
				for (int i = s; i < requestUri.Length; i++)
					if (requestUri[i] >= '0' && requestUri[i] <= '9')
					{
						result *= 10;
						result += requestUri[i] - '0';
					}
					else
					{
						break;
					}

				return (result == 0) ? Session.InvalidId : result;
			}

			return Session.InvalidId;
		}
Пример #14
0
		private int GenerateSessionId(HttpMessageReader httpReader)
		{
			int newSessionCount, oldSessionCount;
			do
			{
				oldSessionCount = sessionCount;
				newSessionCount = (oldSessionCount < int.MaxValue) ? oldSessionCount + 1 : 1;
			}
			while (Interlocked.CompareExchange(ref sessionCount, newSessionCount, oldSessionCount) != oldSessionCount);

			return newSessionCount;
		}
Пример #15
0
		public static bool IsAjax(HttpMessageReader rawReader, byte[] array)
		{
			if (rawReader.RequestUri.Length < ajaxUri.Length)
				return false;

			for (int i = 0; i < ajaxUri.Length; i++)
				if (ajaxUri[i] != array[rawReader.RequestUri.Begin + i])
					return false;

			return true;
		}
Пример #16
0
		protected bool IsAuthorizedByAgent(HttpMessageReader reader, ByteArrayPart username, int agentIndex)
		{
			return agents[agentIndex].Value.IsAuthorized(reader, username);
		}
Пример #17
0
			private void ParseHtppMessage(string message)
			{
				var bytes = Encoding.UTF8.GetBytes(message);

				httpExpected = new HttpMessageReader();

				httpExpected.SetDefaultValue();
				int parsed = httpExpected.Parse(bytes, 0, bytes.Length);
				httpExpected.SetArray(bytes);
				if (httpExpected.ContentLength < 0)
					httpExpected.ContentLength = 0;

				httpHeader = message.Substring(0, parsed);
				httpContent = message.Substring(parsed);
			}
Пример #18
0
		public virtual HttpMessageWriter ProcessPutItem(ByteArrayPart username, ByteArrayPart domain, HttpMessageReader reader, ArraySegment<byte> content)
		{
			return CreateErrorResponse(XcapErrors.CannotInsert, "Not Implemented");
		}