コード例 #1
0
ファイル: HttpServer.cs プロジェクト: hungdluit/sipserver
		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);
			}
		}
コード例 #2
0
ファイル: BaseHttpServer.cs プロジェクト: hungdluit/sipserver
		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);
			}
		}
コード例 #3
0
ファイル: HttpFileServer.cs プロジェクト: hungdluit/sipserver
		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));
					}
				}
			}
		}
コード例 #4
0
ファイル: AjaxWebsocket.cs プロジェクト: hungdluit/sipserver
		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);
			}
		}
コード例 #5
0
ファイル: AjaxWebsocket.cs プロジェクト: hungdluit/sipserver
		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);
			}
		}
コード例 #6
0
		//public int CopyTo(Func<int, ArraySegment<byte>> getBuffer)
		//{
		//    lock (tuples)
		//    {
		//        int length = CountLength();

		//        var segment = getBuffer(length);

		//        if (segment.Count >= length)
		//        {
		//            int offset = 0;
		//            BlockCopy(openPresence, segment, ref offset, openPresence.Length);

		//            for (int i = 0; i < tuples.Count; i++)
		//            {
		//                Buffer.BlockCopy(tuples[i].Data.Array, tuples[i].Data.Offset,
		//                    segment.Array, segment.Offset + offset, tuples[i].Data.Count);
		//                offset += tuples[i].Data.Count;
		//            }

		//            BlockCopy(closePresence, segment, ref offset, closePresence.Length);

		//            return length;
		//        }

		//        return 0;
		//    }
		//}

		public void WriteLenghtAndContent(HttpMessageWriter writer)
		{
			lock (tuples)
			{
				int length = CountLength();

				writer.WriteContentLength(length);
				writer.WriteCRLF();

				WriteContent(writer, length);
			}
		}