예제 #1
0
 void HandleReceivedRequest(object sender, HttpServerRequestEventArgs e)
 {
     if (e.Request.Url == LocalhostTokenUrl)
     {
         var reader = new StreamReader(e.Request.InputStream);
         var tokenString = reader.ReadToEnd();
         var tokens = tokenString.Split('=');
         if (tokens.Length == 2 && tokens[0] == "token")
         {
             Application.Instance.AsyncInvoke(delegate
             {
                 var getUserTask = this.GetUserId(tokens[1]);
                 getUserTask.ContinueWith(task => {
                     Application.Instance.AsyncInvoke(delegate
                     {
                         this.UserID = task.Result;
                         this.Close(DialogResult.Ok);
                     });
                 }, TaskContinuationOptions.OnlyOnRanToCompletion);
                 
                 getUserTask.ContinueWith(task => {
                     MessageBox.Show(this, "Cannot get User ID from token", MessageBoxButtons.OK, MessageBoxType.Error);
                 }, TaskContinuationOptions.OnlyOnFaulted);
             });
         }
     }
 }
예제 #2
0
        void ListenerCallback(IAsyncResult ar)
        {
            if (!listener.IsListening)
                return;

            listener.BeginGetContext(ListenerCallback, null);

            var context = listener.EndGetContext(ar);
            var request = context.Request;
            var response = context.Response;
            
            var reqArgs = new HttpServerRequestEventArgs(request);
            OnReceivedRequest(reqArgs);
            
            if (reqArgs.Cancel)
            {
                response.Abort();
                return;
            }
            
            Debug.WriteLine("SERVER: " + baseDirectory + " " + request.Url);

            response.AddHeader("Cache-Control", "no-cache");

            try
            {
                string html;
                if (staticContent.TryGetValue(request.Url.AbsolutePath, out html))
                {
                    response.ContentType = MediaTypeNames.Text.Html;
                    response.ContentEncoding = Encoding.UTF8;

                    var buffer = Encoding.UTF8.GetBytes(html);
                    response.ContentLength64 = buffer.Length;
                    response.OutputStream.Write(buffer, 0, buffer.Length);
                    response.Close();
                    return;
                }

                
                string filePath = null;
                if (!string.IsNullOrEmpty(baseDirectory))
                    filePath = Path.Combine(
                        baseDirectory,
                        request.Url.AbsolutePath.Substring(1)
                    );
                
                if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
                {
                    response.StatusCode = (int)HttpStatusCode.NotFound; // 404
                    response.StatusDescription = response.StatusCode + " Not Found";

                    response.ContentType = MediaTypeNames.Text.Html;
                    response.ContentEncoding = Encoding.UTF8;

                    var buffer = Encoding.UTF8.GetBytes("<html><body>404 Not Found</body></html>");
                    response.ContentLength64 = buffer.Length;
                    response.OutputStream.Write(buffer, 0, buffer.Length);

                    response.Close();
                    return;
                }

                byte[] entity = null;
                try
                {
                    entity = File.ReadAllBytes(filePath);
                }
                catch (Exception x)
                {
                    Debug.WriteLine("Exception reading file: " + filePath + "\n" + x);

                    response.StatusCode = (int)HttpStatusCode.InternalServerError; // 500
                    response.StatusDescription = response.StatusCode + " Internal Server Error";

                    response.ContentType = MediaTypeNames.Text.Html;
                    response.ContentEncoding = Encoding.UTF8;

                    var buffer = Encoding.UTF8.GetBytes("<html><body>500 Internal Server Error</body></html>");
                    response.ContentLength64 = buffer.Length;
                    response.OutputStream.Write(buffer, 0, buffer.Length);

                    response.Close();
                    return;
                }

                response.ContentLength64 = entity.Length;

                switch (Path.GetExtension(request.Url.AbsolutePath).ToLowerInvariant())
                {
                //images
                    case ".gif":
                        response.ContentType = MediaTypeNames.Image.Gif;
                        break;
                    case ".jpg":
                    case ".jpeg":
                        response.ContentType = MediaTypeNames.Image.Jpeg;
                        break;
                    case ".tiff":
                        response.ContentType = MediaTypeNames.Image.Tiff;
                        break;
                    case ".png":
                        response.ContentType = "image/png";
                        break;

                // application
                    case ".pdf":
                        response.ContentType = MediaTypeNames.Application.Pdf;
                        break;
                    case ".zip":
                        response.ContentType = MediaTypeNames.Application.Zip;
                        break;

                // text
                    case ".htm":
                    case ".html":
                        response.ContentType = MediaTypeNames.Text.Html;
                        break;
                    case ".txt":
                        response.ContentType = MediaTypeNames.Text.Plain;
                        break;
                    case ".xml":
                        response.ContentType = MediaTypeNames.Text.Xml;
                        break;

                // let the user agent work it out
                    default:
                        response.ContentType = MediaTypeNames.Application.Octet;
                        break;
                }

                response.OutputStream.Write(entity, 0, entity.Length);
                response.Close();
            }
            catch (Exception x)
            {
                Debug.WriteLine("Unexpected exception. Aborting...\n" + x);

                response.Abort();
            }
        }
예제 #3
0
 protected virtual void OnReceivedRequest(HttpServerRequestEventArgs e)
 {
     if (ReceivedRequest != null)
         ReceivedRequest(this, e);
 }