コード例 #1
0
ファイル: WebInterface.cs プロジェクト: shemanaev/Elpis
            public void OnRequest(HttpRequestHead request, IDataProducer requestBody, IHttpResponseDelegate response)
            {
                var status = "404 Not Found";
                var body   = new BufferedProducer($"The resource you requested ('{request.Uri}') could not be found.");

                try
                {
                    foreach (var route in _routes)
                    {
                        if (request.Path == route.Key)
                        {
                            body   = route.Value.Execute(request.QueryString);
                            status = "200 OK";
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    status = "500 Internal Server Error";
                    var responseBody = $"The resource you requested ('{request.Uri}') produced an error: {e.Message}";
                    body = new BufferedProducer(responseBody);
                }

                var headers = new HttpResponseHead()
                {
                    Status  = status,
                    Headers = new Dictionary <string, string>()
                    {
                        { "Content-Type", body.MimeType },
                        { "Content-Length", body.Size.ToString() },
                    }
                };

                response.OnResponse(headers, body);
            }
コード例 #2
0
            public void OnRequest(HttpRequestHead request, IDataProducer requestBody, IHttpResponseDelegate response)
            {
                if (request.Method.ToUpperInvariant() == "GET" && request.Uri.StartsWith("/next"))
                {
                    // when you subscribe to the request body before calling OnResponse,
                    // the server will automatically send 100-continue if the client is
                    // expecting it.
                    bool ret = MainWindow.Next();

                    var body = ret ? "Successfully skipped." : "You have to wait for 20 seconds to skip again.";

                    var headers = new HttpResponseHead()
                    {
                        Status  = "200 OK",
                        Headers = new Dictionary <string, string>()
                        {
                            { "Content-Type", "text/plain" },
                            { "Content-Length", body.Length.ToString() },
                        }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else if (request.Method.ToUpperInvariant() == "GET" && request.Uri.StartsWith("/pause"))
                {
                    MainWindow.Pause();
                    var body = "Paused.";

                    var headers = new HttpResponseHead()
                    {
                        Status  = "200 OK",
                        Headers = new Dictionary <string, string>()
                        {
                            { "Content-Type", "text/plain" },
                            { "Content-Length", body.Length.ToString() },
                        }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else if (request.Method.ToUpperInvariant() == "GET" && request.Uri.StartsWith("/play"))
                {
                    MainWindow.Play();
                    var body = "Playing.";

                    var headers = new HttpResponseHead()
                    {
                        Status  = "200 OK",
                        Headers = new Dictionary <string, string>()
                        {
                            { "Content-Type", "text/plain" },
                            { "Content-Length", body.Length.ToString() },
                        }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else if (request.Method.ToUpperInvariant() == "GET" && request.Uri.StartsWith("/toggleplaypause"))
                {
                    var body = "";
                    if (MainWindow._player.Playing)
                    {
                        body = "Paused.";
                    }
                    else
                    {
                        body = "Playing.";
                    }
                    MainWindow.PlayPauseToggle();

                    var headers = new HttpResponseHead()
                    {
                        Status  = "200 OK",
                        Headers = new Dictionary <string, string>()
                        {
                            { "Content-Type", "text/plain" },
                            { "Content-Length", body.Length.ToString() },
                        }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else if (request.Method.ToUpperInvariant() == "GET" && request.Uri.StartsWith("/like"))
                {
                    MainWindow.Like();
                    var body = "Like";
                    if (MainWindow.GetCurrentSong().Loved)
                    {
                        body = "Liked";
                    }

                    var headers = new HttpResponseHead()
                    {
                        Status  = "200 OK",
                        Headers = new Dictionary <string, string>()
                        {
                            { "Content-Type", "text/plain" },
                            { "Content-Length", body.Length.ToString() },
                        }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else if (request.Method.ToUpperInvariant() == "GET" && request.Uri.StartsWith("/dislike"))
                {
                    MainWindow.Dislike();
                    var body = "Disliked.";

                    var headers = new HttpResponseHead()
                    {
                        Status  = "200 OK",
                        Headers = new Dictionary <string, string>()
                        {
                            { "Content-Type", "text/plain" },
                            { "Content-Length", body.Length.ToString() },
                        }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else if (request.Method.ToUpperInvariant() == "GET" && request.Uri.StartsWith("/currentsong"))
                {
                    Song s    = MainWindow.GetCurrentSong();
                    var  body = JsonConvert.SerializeObject(s);

                    var headers = new HttpResponseHead()
                    {
                        Status  = "200 OK",
                        Headers = new Dictionary <string, string>()
                        {
                            { "Content-Type", "text/plain" },
                            { "Content-Length", body.Length.ToString() },
                        }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else if (request.Method.ToUpperInvariant() == "GET" && request.Uri.StartsWith("/connect"))
                {
                    var body = "true";

                    var headers = new HttpResponseHead()
                    {
                        Status  = "200 OK",
                        Headers = new Dictionary <string, string>()
                        {
                            { "Content-Type", "text/plain" },
                            { "Content-Length", body.Length.ToString() },
                        }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else if (request.Uri.StartsWith("/"))
                {
                    var body = string.Format(
                        "Hello world.\r\nHello.\r\n\r\nUri: {0}\r\nPath: {1}\r\nQuery:{2}\r\nFragment: {3}\r\n",
                        request.Uri,
                        request.Path,
                        request.QueryString,
                        request.Fragment);

                    var headers = new HttpResponseHead()
                    {
                        Status  = "200 OK",
                        Headers = new Dictionary <string, string>()
                        {
                            { "Content-Type", "text/plain" },
                            { "Content-Length", body.Length.ToString() },
                        }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else
                {
                    var responseBody = "The resource you requested ('" + request.Uri + "') could not be found.";
                    var headers      = new HttpResponseHead()
                    {
                        Status  = "404 Not Found",
                        Headers = new Dictionary <string, string>()
                        {
                            { "Content-Type", "text/plain" },
                            { "Content-Length", responseBody.Length.ToString() }
                        }
                    };
                    var body = new BufferedProducer(responseBody);

                    response.OnResponse(headers, body);
                }
            }