예제 #1
0
 private void VerifyState(HttpServerState state)
 {
     if (_disposed)
         throw new ObjectDisposedException(GetType().Name);
     if (_state != state)
         throw new InvalidOperationException(String.Format("Expected server to be in the '{0}' state", state));
 }
예제 #2
0
        // Helpers

        private void SetHttpServerState(HttpServerState state)
        {
            lock (_lock)
            {
                _state = state;
            }
        }
예제 #3
0
 private void EnsureHttpServerState(HttpServerState state)
 {
     lock (_lock)
     {
         if (_state != state)
         {
             ThrowInvalidOperationException($"Server State: {_state}");
         }
     }
 }
예제 #4
0
 /// <summary>
 /// Verifies the new state.
 /// </summary>
 /// <param name="state">New state of the server.</param>
 private void VerifyState(HttpServerState state)
 {
     if (_disposed)
     {
         throw new ObjectDisposedException(this.GetType().Name);
     }
     if (_state != state)
     {
         throw new InvalidOperationException("Expected server to be in the state");
     }
 }
예제 #5
0
 public HardwareHttpServer(int port)
 {
    this.port = port;
    state = HttpServerState.Offline;
    Messages = new Queue<string>();
    listener = new HttpListener();
    listener.Prefixes.Add("http://+:" + this.port + "/");
    listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
    worker = new BackgroundWorker();
    worker.DoWork += new DoWorkEventHandler(worker_DoWork);
    worker.WorkerSupportsCancellation = true;
 }
예제 #6
0
        internal void CreateHomePage(HttpServerState Server, HttpRequest Request, HttpResponse Response, Object Model)
        {
            increment++;
            String idx = "<h1>A long time ago, in a galaxy far, far away...</h1><h2>It's the ship that made the Kessel run in less than " + increment + " parsecs!</h2><h3>Aren't you a little short for a stormtrooper?</h3>";
            Response.Response.StatusCode = 200;
            Response.Response.StatusDescription = "It's All Good";
            Response.Response.AddHeader("x-do-the-dance", "safety-dance");

            String q = "<hr/>";
            foreach (String s in Request.QueryCollection) {
                q = q + s + "=" + Request.QueryCollection[s] + "<br/>";
            }
            idx = idx + q;
            byte[] b = System.Text.Encoding.UTF8.GetBytes(idx);
            Response.Response.OutputStream.Write(b, 0, idx.Length);
        }
예제 #7
0
        internal void GetEchoFile(HttpServerState Server, HttpRequest Request, HttpResponse Response, Object Model)
        {
            increment++;
            String idx = "<h1>Form Results</h1>";
            foreach (String key in Request.HeaderCollection.AllKeys) {
                idx = idx + "<b>" + key + ":</b> " + Request.HeaderCollection[key] + "<br/>";
            }
            Response.Response.StatusCode = 200;
            Response.Response.StatusDescription = "It's All Good";
            Response.Response.AddHeader("x-do-the-dance", "safety-dance");

            /*
            idx = idx + "<hr/><h3>Body:</h3>";
            idx = idx + "Content Type: " + Request.Request.ContentType + "<br/>Encoding Type: " + Request.EncodingType + "<br/><br/>";
            idx = idx + Request.RequestBody;
            */

            if (Request.AttachmentList.Count > 0) {
                idx = idx + "<hr/><h3>Attachments</h3>";
                foreach (HttpAttachment a in Request.AttachmentList) {
                    idx = idx + "<b>Name:</b> " + a.Name + "<br/>";
                    idx = idx + "<b>Content-Disposition:</b> " + a.ContentDisposition + "<br/>";
                    idx = idx + "<b>Content-Type:</b> " + a.ContentType + "<br/>";
                    idx = idx + "<b>File Name:</b> " + a.FileName + "<br/>";
                    idx = idx + "<hr/>";
                    if (a.Body != null) {
                        if (a.FileName != null && a.FileName != "") {
                            System.IO.File.WriteAllBytes(a.FileName, a.Body);
                        }
                    } else {
                        idx = idx + "<b>Value:</b> " + a.Value + "<br/>";
                    }
                }
            }

            if (Request.FormCollection != null && Request.FormCollection.AllKeys.Length > 0) {
                String q = "<br/><br/><hr/><h3>Collection</h3><br/><br/>";
                foreach (String s in Request.FormCollection) {
                    q = q + s + " = " + Request.FormCollection[s] + "<br/>";
                }
                idx = idx + q;
            }
            byte[] b = System.Text.Encoding.UTF8.GetBytes(idx);
            Response.Response.OutputStream.Write(b, 0, idx.Length);
        }
예제 #8
0
        // Constructor

        public StateChangedEventArgs(HttpServerState previousState, HttpServerState currentState)
        {
            PreviousState = previousState;
            CurrentState  = currentState;
        }
예제 #9
0
        internal void GetStaticFile(HttpServerState Server, HttpRequest Request, HttpResponse Response, Object Model)
        {
            String path = staticFilePath + Request.Request.Url.AbsolutePath;
            if (System.IO.File.Exists(path) == false) {
                String page = this.CreateStatusPage(404, Request.Request.Url, null);
                Byte[] p = System.Text.Encoding.UTF8.GetBytes(page);
                Response.Response.OutputStream.Write(p, 0, p.Length);
            }

            try {
                byte[] b = System.IO.File.ReadAllBytes(path);
                Response.Response.StatusCode = 200;
                Response.Response.OutputStream.Write(b, 0, b.Length);
            } catch (Exception e) {
                String page = this.CreateStatusPage(500, Request.Request.Url, e);
                Byte[] p = System.Text.Encoding.UTF8.GetBytes(page);
                Response.Response.OutputStream.Write(p, 0, p.Length);
            }
        }
예제 #10
0
        internal void GetEchoForm(HttpServerState Server, HttpRequest Request, HttpResponse Response, Object Model)
        {
            try {
                EchoModel model = ((EchoModel)Model);
                String str = model.EchoString;
                if (model.UseUpperCase) {
                    str = str.ToUpperInvariant();
                }
                byte[] ba = System.Text.Encoding.UTF8.GetBytes("Complete: " + str);
                Response.Response.Cookies.Add(new System.Net.Cookie() {
                    Name = "SES-PUBLIC",
                    Value = "ABCDEF1234-567890"
                });
                Response.Response.Cookies.Add(new System.Net.Cookie() {
                    Name = "SES-EXPIRES",
                    Value = (DateTime.Now.Ticks + 1000).ToString()
                });
                Response.Response.Cookies.Add(new System.Net.Cookie() {
                    Name = "SES-FINGERPRINT",
                    Value = "ASKljdsajlkdiuAUSD7898234hTG&^7676789"
                });
                Response.Response.OutputStream.Write(ba, 0, ba.Length);
            } catch (Exception e) {
                String page = this.CreateStatusPage(500, Request.Request.Url, e);
                Byte[] p = System.Text.Encoding.UTF8.GetBytes(page);

                Response.Response.OutputStream.Write(p, 0, p.Length);
            }
        }
예제 #11
0
 private void HttpServerOnServerState(HttpServerState state)
 {
     try
     {
         Invoke(new Action<HttpServerState>(HttpServerChangeState), state);
     }
     catch (Exception e)
     {
     }
 }
예제 #12
0
 public void Stop()
 {
     if (listener.IsListening)
     {
         listener.Stop();
         State = HttpServerState.Offline;
     }
     worker.CancelAsync();
 }
예제 #13
0
 public void Start()
 {
     if (!listener.IsListening)
     {
         listener.Start();
         State = HttpServerState.Listening;
     }
     worker.RunWorkerAsync(port);
 }
예제 #14
0
 private void GetThreadFunction(object obj)
 {
     HttpListenerContext context = (HttpListenerContext)obj;
     var hb = 0;
     while (Thread.CurrentThread.ThreadState == ThreadState.Running)
     {
         while (true)
         {
             var message = "";
             lock(Messages){
                 if (Messages.Count == 0) break;
                 message = Messages.Dequeue();
             }
             Thread.Sleep(50);
             try
             {
                 var data = Encoding.UTF8.GetBytes(message);
                 context.Response.OutputStream.Write(data, 0, data.Length);
                 context.Response.OutputStream.Flush();
             }
             catch (Exception)
             {
                 break;
             }
         }
         Thread.Sleep(100);
         hb++;
         if (hb >= 30)
         {
             hb = 0;
             try
             {
                 context.Response.OutputStream.WriteByte(00);
                 context.Response.OutputStream.Flush();
             }
             catch (Exception)
             {
                 State = HttpServerState.Free;
                 return;
             }
         }
     }
     State = HttpServerState.Free;
     context.Response.Close();
 }
예제 #15
0
 public void AcceptClient(HttpListenerContext context)
 {
     context.Response.AddHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
     context.Response.AddHeader("Access-Control-Request-Header", "X-Prototype-Version, x-requested-with");
     context.Response.AddHeader("Access-Control-Allow-Origin", "*");
     context.Response.ContentType = "text/json";
     context.Response.StatusCode = 200;
     context.Response.ContentEncoding = Encoding.UTF8;
     context.Response.SendChunked = false;
     context.Response.KeepAlive = false;
     context.Response.OutputStream.Flush();
     if (OnConnect != null){
         OnConnect(context);
     }
     if (context.Request.HttpMethod == "GET"){
         var client = new Thread(GetThreadFunction);
         client.Start(context);
         State = HttpServerState.Connected;
         return;
     }
     if (context.Request.HttpMethod == "POST"){
         ReceiveData(context);
         return;
     }
     context.Response.StatusCode = 401;
     context.Response.Close();
 }
예제 #16
0
 public HttpEventArgs(HttpServerState previous, HttpServerState next) : base()
 {
     this._newState      = next;
     this._previousState = previous;
 }
예제 #17
0
 private void VerifyState(HttpServerState state)
 {
     if (_disposed)
         throw new ObjectDisposedException(GetType().Name);
     if (_state != state)
         throw new InvalidOperationException(String.Format("Expected server to be in the '{0}' state", state));
 }
예제 #18
0
 protected void HttpServerChangeState(HttpServerState state)
 {
     if (state == HttpServerState.Connected)
     {
         lblHttpState.ForeColor = Color.Green;
     }
     if (state == HttpServerState.Listening || state == HttpServerState.Free)
     {
         lblHttpState.ForeColor = Color.Orange;
     }
     if (state == HttpServerState.Offline)
     {
         lblHttpState.ForeColor = Color.DarkRed;
     }
     lblHttpState.Text = state.ToString() + " " + Program.HttpServer.Port;
 }