コード例 #1
0
        public void Run()
        {
            if (!IsConnectedToInternet())
            {
                System.Console.WriteLine("Not connected to a network");
                return;
            }

            bool useLocalIP = true;

            if (!TryConnectLocalIP())
            {
                useLocalIP = false;
                System.Console.WriteLine("Could not connect through local IP address");
            }

            System.Console.WriteLine("Connect using {0}", baseUrl.TrimEnd('/'));

            using (var listener = new System.Net.HttpListener())
            {
                listener.Prefixes.Add(baseUrl);
                if (useLocalIP)
                {
                    listener.Prefixes.Add(ExternalUrl);
                }

                listener.Start();
                while (true)
                {
                    System.Net.HttpListenerContext ctx     = listener.GetContext();
                    System.Net.HttpListenerRequest request = ctx.Request;
                    System.Console.WriteLine(request.Url);

                    System.Net.HttpListenerResponse response = ctx.Response;

                    string urlString = request.Url.ToString();
                    string rawUrl    = request.RawUrl.ToString();

                    string responseText = null;

                    byte[] reponseBuffer = null;
                    if (rawUrl == root)
                    {
                        responseText         = defaultPage;
                        responseText         = responseText.Replace(baseUrl, urlString + "/");
                        response.ContentType = "text/HTML";
                    }
                    else if (rawUrl.StartsWith(WebService.resourcePrefix))
                    {
                        string resourceName = rawUrl.Remove(0, WebService.resourcePrefix.Length);

                        if (rawUrl.EndsWith(".jpg"))
                        {
                            reponseBuffer        = HtmlRenderer.GetEmbeddedContentAsBinary(resourceName);
                            response.ContentType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
                        }
                        else
                        {
                            responseText         = HtmlRenderer.GetEmbeddedContent(resourceName);
                            response.ContentType = "text/css";
                        }
                    }
                    else if (rawUrl.StartsWith(root + "/"))
                    {
                        var streamReader = new System.IO.StreamReader(request.InputStream, request.ContentEncoding);
                        var jsonRequest  = streamReader.ReadToEnd();

                        string requestedPage      = rawUrl.Remove(0, (root + "/").Length);
                        object unserializedObject = null;

                        Type serviceType;
                        if (mapNameToServiceType.TryGetValue(requestedPage, out serviceType))
                        {
                            unserializedObject = js.Deserialize(jsonRequest, serviceType);
                            if (unserializedObject == null)
                            {
                                unserializedObject = Activator.CreateInstance(serviceType);
                            }
                        }

                        if (unserializedObject is IRequestWithHtmlResponse)
                        {
                            responseText = ((IRequestWithHtmlResponse)unserializedObject).GetResponse(this);
                        }
                        else if (unserializedObject is IRequestWithJsonResponse)
                        {
                            object o          = ((IRequestWithJsonResponse)unserializedObject).GetResponse(this);
                            var    serialized = js.Serialize(o);
                            responseText = serialized;
                        }
                        response.ContentType = "text/html";
                    }

                    if (response.ContentType != null && response.ContentType.StartsWith("text") && responseText != null)
                    {
                        response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
                        reponseBuffer            = System.Text.Encoding.UTF8.GetBytes(responseText);
                    }

                    if (reponseBuffer != null)
                    {
                        //These headers to allow all browsers to get the response
                        response.Headers.Add("Access-Control-Allow-Credentials", "true");
                        response.Headers.Add("Access-Control-Allow-Origin", "*");
                        response.Headers.Add("Access-Control-Origin", "*");

                        //response.StatusCode = 200;
                        //response.StatusDescription = "OK";
                        // Get a response stream and write the response to it.
                        response.ContentLength64 = reponseBuffer.Length;
                        System.IO.Stream output = response.OutputStream;
                        output.Write(reponseBuffer, 0, reponseBuffer.Length);
                        output.Close();
                    }
                    response.Close();
                }
            }
        }