Beispiel #1
0
        private async Task WriteResponseAsync(string requestUri, IOutputStream os, StreamSocketInformation socketInfo)
        {
            try
            {
                requestUri = requestUri.TrimEnd('\0'); //remove possible null from POST request

                string[] uriParts = requestUri.Split('/');


                if (requestUri == "/")
                {
                    // Request for the root page, so redirect to home page
                    await redirectToPage(NavConstants.HOME_PAGE, os);

                    return;
                }
                else if (uriParts[1].ToLower().Contains("home") || uriParts[1].ToLower().Contains("index") || uriParts[1].ToLower().Contains("default"))
                {
                    // Request for the home page
                    string html = await GeneratePageHtml(NavConstants.HOME_PAGE);

                    await WebHelper.WriteToStream(html, os);

                    return;
                }
                else if (uriParts[1].ToLower() == "api")
                {
                    string json = await _webApiHelper.Execute(requestUri);

                    await WebHelper.WriteToStream(json, os);

                    return;
                }
                else// Request for a file that is in the Assets\Web folder (e.g. logo, css file)
                {
                    using (Stream resp = os.AsStreamForWrite())
                    {
                        // Map the requested path to Assets\Web folder
                        StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(NavConstants.ASSETSWEB);

                        bool exists = await RetrieveFile(requestUri, folder, resp);

                        if (!exists)
                        {
                            if (requestUri.Contains(NavConstants.TEMP_FOLDER))
                            {
                                folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
                                exists = await RetrieveFile(requestUri, folder, resp);

                                if (!exists)
                                {
                                    // Send 404 not found if can't find file
                                    byte[] headerArray = Encoding.UTF8.GetBytes("HTTP/1.1 404 Not Found\r\nContent-Length:0\r\nConnection: close\r\n\r\n");
                                    await resp.WriteAsync(headerArray, 0, headerArray.Length);
                                }
                            }
                            else
                            {
                                if (requestUri.Contains("favicon.ico"))//https://en.wikipedia.org/wiki/Favicon
                                {
                                    folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(NavConstants.ASSETSWEB);

                                    exists = await RetrieveFile(requestUri, folder, resp);

                                    if (!exists)
                                    {
                                        // Send 404 not found if can't find file
                                        byte[] headerArray = Encoding.UTF8.GetBytes("HTTP/1.1 404 Not Found\r\nContent-Length:0\r\nConnection: close\r\n\r\n");
                                        await resp.WriteAsync(headerArray, 0, headerArray.Length);
                                    }
                                }
                                else
                                {
                                    //It raise warning message because web request shouldn't access to root installed folder
                                    byte[] headerArray = Encoding.UTF8.GetBytes("HTTP/1.1 403 Forbidden\r\nContent-Length:0\r\nConnection: close\r\n\r\n");
                                    await resp.WriteAsync(headerArray, 0, headerArray.Length);
                                }
                            }
                        }


                        await resp.FlushAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception in writeResponseAsync(): " + ex.Message);
                Debug.WriteLine(ex.StackTrace);

                // Log telemetry event about this exception
                var events = new Dictionary <string, string> {
                    { "WebServer", ex.Message }
                };
                TelemetryManager.WriteTelemetryEvent("FailedToWriteResponse", events);

                try
                {
                    // Try to send an error page back if there was a problem servicing the request
                    string html = _webhelper.GenerateErrorPage("There's been an error: " + ex.Message + "<br><br>" + ex.StackTrace);
                    await WebHelper.WriteToStream(html, os);
                }
                catch (Exception e)
                {
                    TelemetryManager.WriteTelemetryException(e);
                }
            }
        }