Ejemplo n.º 1
0
 public static void Process(HttpContext Info)
 {
     Sockets.Add(Info);
     WebSocket.EventLoop(Info, UnMask =>
     {
         Debug.Print(UnMask);
         if (r2.IsMatch(UnMask))
         {
             var match = r2.Match(UnMask);
             string Command = match.Groups[2].Value;
             string Id = match.Groups[1].Value;
             string JsonData = Api.Call(Command, false);
             byte[] Response = WebSocket.Mask(
                         Encoding.UTF8.GetBytes("ERR No API"), 0x1);
             if (JsonData != "")
             {
                 Response = Encoding.UTF8.GetBytes("+OK" + Id + " " + JsonData);
             }
             HttpResponse.SendResponseBodyWS(Info, WebSocket.Mask(
                             Response, 0x1));
         }
         else
         {
             byte[] Response = WebSocket.Mask(
                         Encoding.UTF8.GetBytes("ERR"), 0x1);
             HttpResponse.SendResponseBodyWS(Info, Response);
         }
     });
     Sockets.Remove(Info);
 }
Ejemplo n.º 2
0
 public static void EventLoop(HttpContext Context, Action<string> Handler)
 {
     HandshakeResponseSend(Context);
     while (Context.Client.Connected)
     {
         byte[] UnMaskBuf = WebSocket.GetUnMaskedFrame(Context);
         if (UnMaskBuf == null) continue;
         string UnMask = Encoding.UTF8.GetString(UnMaskBuf);
         Handler(UnMask);
     }
     return;
 }
Ejemplo n.º 3
0
 public static void Redirect(HttpContext Context, string Url)
 {
     var Domain = "localhost:8080";
     if (Context.Request.Headers.ContainsKey("host"))
     {
         Domain = Context.Request.Headers["host"];
     }
     Context.Response.StatusCode = 301;
     Context.Response.StatusText = "Moved Permanently";
     Context.Response.Headers["Location"] = "http://" + Domain + Url;
     Context.Response.Send();
 }
Ejemplo n.º 4
0
 public static void SendResponse(HttpContext Context, string Str)
 {
     SendResponse(Context, Encoding.UTF8.GetBytes(Str));
 }
Ejemplo n.º 5
0
 public static void SendResponse(HttpContext Context, byte[] Bytes)
 {
     if (!Context.Response.Headers.ContainsKey("Content-Length"))
     {
         Context.Response.Headers.Add("Content-Length", Bytes.LongLength.ToString());
     }
     Context.Response.OutputStream.Write(Bytes, 0, Bytes.Length);
     Context.Response.Send();
 }
Ejemplo n.º 6
0
 public bool RequestUrl(HttpContext Context, bool IsAuth)
 {
     var Ret = false;
     var PartName = Context.Request.Url.ToLower().Replace("/", "\\");
     if (PartName.IndexOf("..\\") >= 0)
     {
         throw new HttpResponseException(403, "Forbidden", "Security risk detected...");
     }
     if (File.Exists(".\\web\\" + PartName) && IsAuth)
     {
         string MimeType = Mime.Get(PartName, "text/html");
         if (!Mime.IsImage(PartName))
             MimeType += "; charset=utf-8";
         Context.Response.Headers.Add("Content-Type", MimeType);
         HttpContext.SendResponse(Context, File.ReadAllBytes(".\\web\\" + PartName));
         Ret = true;
     }
     else
     {
         switch (Context.Request.Url.ToLower())
         {
             case "/js/jquery.js":
                 Context.Response.Headers.Add("Content-Type", "application/javascript");
                 HttpContext.SendResponse(Context, Resources.JQuery);
                 Ret = true;
                 break;
             case "/js/bootstrap.js":
                 Context.Response.Headers.Add("Content-Type", "application/javascript");
                 HttpContext.SendResponse(Context, Resources.BootStrap);
                 Ret = true;
                 break;
             case "/js/respond.js":
                 Context.Response.Headers.Add("Content-Type", "application/javascript");
                 HttpContext.SendResponse(Context, Resources.Respond);
                 Ret = true;
                 break;
             case "/js/jquery.datatables.min.js":
                 Context.Response.Headers.Add("Content-Type", "application/javascript");
                 HttpContext.SendResponse(Context, Resources.jqury_dataTables_js);
                 Ret = true;
                 break;
             case "/js/datatables.bootstrap.js":
                 Context.Response.Headers.Add("Content-Type", "application/javascript");
                 HttpContext.SendResponse(Context, Resources.dataTables_bootstrap);
                 Ret = true;
                 break;
             case "/js/jquery.datatables.min.css":
                 Context.Response.Headers.Add("Content-Type", "text/css");
                 HttpContext.SendResponse(Context, Resources.jquery_dataTables_css);
                 Ret = true;
                 break;
             case "/css/bootstrap.css":
                 Context.Response.Headers.Add("Content-Type", "text/css");
                 HttpContext.SendResponse(Context, Resources.BootStrapStyle);
                 Ret = true;
                 break;
             case "/css/bootstrap.css.map":
                 Context.Response.Headers.Add("Content-Type", "text/plain");
                 HttpContext.SendResponse(Context, Resources.BootStrapCssMap);
                 Ret = true;
                 break;
             case "/img/not_thumb.png":
                 Context.Response.Headers.Add("Content-Type", "image/png");
                 var Stream = new MemoryStream();
                 Resources.NotThumbnail.Save(Stream, ImageFormat.Png);
                 HttpContext.SendResponse(Context, Stream.GetBuffer());
                 Stream.Close();
                 Ret = true;
                 break;
             case "/img/loader.gif":
                 Context.Response.Headers.Add("Content-Type", "image/gif");
                 var Stream1 = new MemoryStream();
                 Resources.loader.Save(Stream1, ImageFormat.Gif);
                 HttpContext.SendResponse(Context, Stream1.GetBuffer());
                 Stream1.Close();
                 Ret = true;
                 break;
             case "/fonts/glyphicons-halflings-regular.eot":
                 Ret = true;
                 HttpContext.SendResponse(Context, Resources.glyphicons_halflings_regular_eot);
                 break;
             case "/fonts/glyphicons-halflings-regular.svg":
                 Ret = true;
                 HttpContext.SendResponse(Context, Resources.glyphicons_halflings_regular_svg);
                 break;
             case "/fonts/glyphicons-halflings-regular.ttf":
                 Ret = true;
                 HttpContext.SendResponse(Context, Resources.glyphicons_halflings_regular_ttf);
                 break;
             case "/fonts/glyphicons-halflings-regular.woff":
                 Ret = true;
                 HttpContext.SendResponse(Context, Resources.glyphicons_halflings_regular_woff);
                 break;
         }
     }
     return Ret;
 }
Ejemplo n.º 7
0
        public static void HandshakeResponseSend(HttpContext Context)
        {
            if (!Context.Request.Headers.ContainsKey("upgrade") && Context.Request.Headers.ContainsKey("sec-websocket-key")) return;
            if (Context.Request.Headers["upgrade"].ToLower() == "websocket" && Context.Request.Headers["sec-websocket-key"] != "")
            {
                var Accept = GenerateAccept(Context.Request.Headers["sec-websocket-key"]);

                Context.Response.Headers.Add("Connection", "Upgrade");
                Context.Response.Headers.Add("Upgrade", "websocket");
                Context.Response.Headers.Add("Sec-WebSocket-Accept", Accept);
                Context.Response.StatusCode = 101;
                Context.Response.StatusText = "Switching Protocols";
            }
            HttpResponse.SendResponseCode(Context);
            HttpResponse.SendResponseHeader(Context, Context.Response.Headers);
        }
Ejemplo n.º 8
0
 public static byte[] GetUnMaskedFrame(HttpContext Context)
 {
     while (Context.Client.Available < 2) Thread.Sleep(10);
     long DataSize = 0;
     long NowSize = 0;
     long HdrSize = 2;
     byte[] Buffer = new byte[1024];
     int Size;
     int TotalSize = 0;
     List<byte> MaskBuffer = new List<byte>();
     while (TotalSize < 2) //Headerの全長を知るための2byteをRead
     {
         Size = Context.HttpStream.Read(Buffer, 0, 2);
         MaskBuffer.AddRange(Buffer.Take(Size));
         TotalSize += Size;
     }
     HdrSize = GetHeaderLength(MaskBuffer.ToArray()); //Headerの全長
     if (TotalSize < HdrSize) //Headerをすべて読んでいない
     {
         HdrSize -= TotalSize; //今まで読んだ分
         while (HdrSize > 0)
         {
             Size = Context.HttpStream.Read(Buffer, 0, HdrSize > Buffer.Length ? Buffer.Length : (int)HdrSize); //HeaderがBufferを超えるならBuffer分、それ以下ならHeaderの全長一気に
             MaskBuffer.AddRange(Buffer.Take(Size));
             TotalSize += Size;
             HdrSize -= Size;
         }
     }
     DataSize = GetLength(MaskBuffer.ToArray()); //データの長さ
     NowSize = DataSize - TotalSize; //読むべき残りデータ
     while (NowSize > 0)
     {
         if (NowSize < Buffer.Length) //DataがBufferを超えないならData分
         {
             Size = Context.HttpStream.Read(Buffer, 0, (int)NowSize);
         }
         else
         {
             Size = Context.HttpStream.Read(Buffer, 0, Buffer.Length);
         }
         MaskBuffer.AddRange(Buffer.Take(Size));
         NowSize -= Size;
     }
     DataSize = 0;
     if ((byte)(MaskBuffer[0] & 0x0f) == 0x8)//Close
     {
         Context.Close();
         return null;
     }
     else if ((byte)(MaskBuffer[0] & 0x0f) == 0x9) //Pingに返す
     {
         var SendFrame = MaskBuffer.ToArray();
         SendFrame[0] = 0x8A;
         HttpResponse.SendResponseBody(Context, SendFrame);
         return null;
     }
     return UnMask(MaskBuffer.ToArray()); //全部まとめてアンマスク
 }