Beispiel #1
0
		public void ProcessIncomingRequest(Connection c)
		{
			if (c.HttpReader.HasUpgarde(Upgrades.Websocket))
			{
				// c.HttpReader.SecWebSocketProtocol

				var key = c.HttpReader.SecWebSocketKey.ToString();
				byte[] accept;
				lock (sha1)
					accept = sha1.ComputeHash(Encoding.ASCII.GetBytes(key + guid));

				using (var writer = new HttpMessageWriter())
				{
					writer.WriteStatusLine(StatusCodes.SwitchingProtocols);
					writer.WriteConnectionUpgrade();
					writer.WriteUpgradeWebsocket();
					writer.WriteSecWebSocketAccept(accept);
					writer.WriteSecWebSocketProtocol(sip);
					writer.WriteContentLength(0);
					writer.WriteCRLF();

					SendResponse(c, writer, InvalidAgentIndex);
				}

				c.UpgradeToWebsocket();
			}
			else
			{
				base.ProcessIncomingRequest(c);
			}
		}
Beispiel #2
0
		public void ProcessIncomingRequest(BaseHttpConnection c)
		{
			bool handled = false;

			for (int i = 0; i < agents.Length; i++)
			{
				var result = agents[i].Value.IsHandled(c.HttpReader);
				if (result.IsHandled)
				{
					bool isAuthorized = true;

					if (agents[i].IsAuthorizationEnabled && result.IsAuthorizationRequred)
					{
						var writer = IsAuthorized(c, result.Realm, i);
						if (writer != null)
						{
							isAuthorized = false;
							SendResponse(c, writer, InvalidAgentIndex);
						}
					}

					if (isAuthorized)
						agents[i].Value.HandleRequest(c, c.HttpReader, c.Content);

					handled = true;

					break;
				}
			}

			if (handled == false)
			{
				var writer = new HttpMessageWriter();

				writer.WriteStatusLine(StatusCodes.TemporaryRedirect);
				writer.WriteLocation(c.LocalEndPoint.Protocol == ServerProtocol.Tcp, c.HttpReader.Host.Host, c.HttpReader.Host.Port, defaultUri);
				writer.WriteContentLength(0);
				writer.WriteCRLF();

				SendResponse(c, writer, InvalidAgentIndex);
			}
		}
Beispiel #3
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));
					}
				}
			}
		}
Beispiel #4
0
		private void SendJsonMessage(Session session)
		{
			using (var writer = new HttpMessageWriter())
			{
				//byte[] json = Encoding.UTF8.GetBytes(string.Format(@"{{id:{0}}}", session.Id));
				byte[] json = Encoding.UTF8.GetBytes(string.Format(@"{0}", session.Id));

				writer.WriteStatusLine(StatusCodes.OK);
				//writer.Write(Encoding.UTF8.GetBytes("Content-Type: application/json\r\n"));
				writer.WriteContentLength(json.Length);
				writer.WriteAccessControlHeaders();
				writer.WriteCRLF();
				writer.Write(json);

				SendWriter(session, writer);
			}
		}
Beispiel #5
0
		private static void WriteHttpHeader(ServerAsyncEventArgs e)
		{
			using (var writer = new HttpMessageWriter())
			{
				writer.WriteStatusLine(StatusCodes.OK);
				writer.WriteContentLength(e.Count);
				writer.WriteAccessControlHeaders();
				writer.WriteCRLF();

				e.OffsetOffset -= writer.Count;
				e.Count += writer.Count;
				Buffer.BlockCopy(writer.Buffer, writer.Offset, e.Buffer, e.Offset, writer.Count);
			}
		}