static void Main(string[] args)
        {
            var routes = new List <Route>()
            {
                new Route()
                {
                    Name     = "Products",
                    UrlRegex = @"\/.+\..+",
                    Method   = RequestMethod.GET,
                    Callable = request =>
                    {
                        string decodedUrl    = WebUtility.UrlDecode(request.Url);
                        string normalizedUrl = RepoPath + decodedUrl.Replace('/', '\\');
                        var    response      = new HttpResponse()
                        {
                            Content    = File.ReadAllBytes(normalizedUrl),
                            StatusCode = ResponseStatusCode.Ok
                        };
                        string fileType = decodedUrl.Substring(decodedUrl.LastIndexOf('.') + 1);
                        response.Header.ContentType = MimeMapper.MapMimeToType(fileType);
                        return(response);
                    }
                },
                new Route()
                {
                    Name     = "Products",
                    UrlRegex = @"^/",
                    Method   = RequestMethod.GET,
                    Callable = request =>
                    {
                        return(new HttpResponse()
                        {
                            ContentAsUtf8 = PageBuilder.BuildMyRepoPage(RepoPath + request.Url),
                            StatusCode = ResponseStatusCode.Ok
                        });
                    }
                },
                new Route()
                {
                    Name     = "Products",
                    UrlRegex = @"^/",
                    Method   = RequestMethod.POST,
                    Callable = request =>
                    {
                        PostRequestHandler.HandleFolderCreation(RepoPath + request.Url, request.Content);
                        //MemoryStream ms = new MemoryStream(bytes);
                        //Image image = Image.FromStream(ms,true,true);
                        //image.Save(@"D:\\UPLOADEDIMAGE.png", ImageFormat.Png);
                        return(new HttpResponse()
                        {
                            ContentAsUtf8 = PageBuilder.BuildMyRepoPage(RepoPath + request.Url),
                            StatusCode = ResponseStatusCode.Ok
                        });
                    }
                },
            };
            HttpServer server = new HttpServer(8081, routes);

            server.Listen();
        }
Пример #2
0
        public async Task <IActionResult> Format()
        {
            try
            {
                IFormFile file = Request.Form.Files[0];

                String NewspaperPrinterAddress = _config.GetValue <String>("NewspaperPrinterApiAddress");
                String inputFolderPath         = _environment.ContentRootPath + _config.GetValue <String>("InputFolder");
                String inputFilePath           = inputFolderPath + FileNameGenerator.getName(6) + _config.GetValue <String>("IoFileType");

                int choice, readabilityLevel, noOfCols, noOfRows, widthOfPage, columnSpacing;

                if (!int.TryParse(Request.Form["choice"], out choice))
                {
                    choice = 1;
                }
                if (!int.TryParse(Request.Form["noOfCols"], out noOfCols))
                {
                    noOfCols = 3;
                }
                if (!int.TryParse(Request.Form["noOfRows"], out noOfRows))
                {
                    noOfRows = 20;
                }
                if (!int.TryParse(Request.Form["widthOfPage"], out widthOfPage))
                {
                    widthOfPage = 80;
                }
                if (!int.TryParse(Request.Form["columnSpacing"], out columnSpacing))
                {
                    columnSpacing = 3;
                }
                if (!int.TryParse(Request.Form["readabilityLevel"], out readabilityLevel))
                {
                    readabilityLevel = 2;
                }

                if (!Directory.Exists(inputFolderPath))
                {
                    Directory.CreateDirectory(inputFolderPath);
                }

                if (file.Length > 0)
                {
                    using (var stream = System.IO.File.Create(inputFilePath))
                    {
                        await file.CopyToAsync(stream);
                    }
                }

                _streamReader.setPath(inputFilePath);
                String text = _streamReader.read();

                Content content = new Content(text);

                NewspaperProperties newspaperProperties =
                    new NewspaperProperties(noOfCols, noOfRows, widthOfPage,
                                            (NewspaperProperties.ReadabilityLevel)readabilityLevel, columnSpacing);

                NewspaperMachine newspaperMachine = new NewspaperMachine(choice, content, newspaperProperties);
                Newspaper        newspaper        = newspaperMachine.getNewspaper();

                string newspaperString = JsonSerializer.Serialize(newspaper);

                PostRequestHandler postRequestHandler = new PostRequestHandler();

                HttpResponseMessage response = await postRequestHandler.send(NewspaperPrinterAddress, newspaperString);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return(Ok("Newspaper printed!"));
                }
                else
                {
                    return(NotFound("Newspaper printing failed!"));
                }
            }
            catch (ArgumentOutOfRangeException)
            {
                return(BadRequest("Please attach input file!"));
            }
            catch (Exception e)
            {
                return(NotFound(e.Message));
            }
        }
        static void Main()
        {
            PageBuilderFactory factory = new PageBuilderFactory();
            var routes = new List <Route>()
            {
                new Route()
                {
                    Name     = "Products",
                    UrlRegex = @"^/products.html$",
                    Method   = RequestMethod.GET,
                    Callable = request =>
                    {
                        var response = new HttpResponse()
                        {
                            ContentAsUtf8 = PageBuilder.BuildKnivesPage(factory, request.Header.CookieCollection),
                            StatusCode    = ResponseStatusCode.Ok
                        };
                        return(response);
                    }
                },

                new Route()
                {
                    Name     = "Home Directory",
                    Method   = RequestMethod.GET,
                    UrlRegex = "^\\/.+\\.html",
                    Callable = (request) =>
                    {
                        string           url     = string.Empty;
                        CookieCollection cookies = UrlHandler.HandleIncomingQueryParameters(request.Url);

                        foreach (var cookie in cookies)
                        {
                            request.Header.CookieCollection.AddCookie(cookie);
                        }
                        if (!request.Header.CookieCollection.ContainsKey("theme"))
                        {
                            request.Header.CookieCollection.AddCookie(new Cookie("theme", "dark"));
                        }
                        if (cookies.Count > 0)
                        {
                            url = request.Url.Substring(1, request.Url.IndexOf('?') - 1);
                        }
                        else
                        {
                            url = request.Url;
                        }

                        var response = new HttpResponse()
                        {
                            StatusCode    = ResponseStatusCode.Ok,
                            ContentAsUtf8 = factory.BuildThemedPage(request.Header.CookieCollection, url)
                        };
                        response.Header.CookieCollection.AddCookie(request.Header.CookieCollection["theme"]);
                        return(response);
                    }
                },
                new Route()
                {
                    Name     = "Carousel CSS",
                    Method   = RequestMethod.GET,
                    UrlRegex = "^/content/css/carousel.css$",
                    Callable = (request) =>
                    {
                        var response = new HttpResponse()
                        {
                            StatusCode    = ResponseStatusCode.Ok,
                            ContentAsUtf8 = File.ReadAllText("../../content/css/carousel.css")
                        };
                        response.Header.ContentType = "text/css";
                        return(response);
                    }
                },
                new Route()
                {
                    Name     = "Bootstrap JS",
                    Method   = RequestMethod.GET,
                    UrlRegex = "^/bootstrap/js/bootstrap.min.js$",
                    Callable = (request) =>
                    {
                        var response = new HttpResponse()
                        {
                            StatusCode    = ResponseStatusCode.Ok,
                            ContentAsUtf8 = File.ReadAllText("../../content/bootstrap/js/bootstrap.min.js")
                        };
                        response.Header.ContentType = "application/x-javascript";
                        return(response);
                    }
                },
                new Route()
                {
                    Name     = "Bootstrap CSS map",
                    Method   = RequestMethod.GET,
                    UrlRegex = "^/bootstrap/css/bootstrap.min.css.map$",
                    Callable = (request) =>
                    {
                        var response = new HttpResponse()
                        {
                            StatusCode    = ResponseStatusCode.Ok,
                            ContentAsUtf8 = File.ReadAllText("../../content/bootstrap/css/bootstrap.min.css.map")
                        };
                        response.Header.ContentType = "text/css";
                        return(response);
                    }
                },
                new Route()
                {
                    Name     = "Bootstrap CSS",
                    UrlRegex = @"^/bootstrap/css/bootstrap.min.css$",
                    Method   = RequestMethod.GET,
                    Callable = (request) =>
                    {
                        var response = new HttpResponse
                        {
                            ContentAsUtf8 = File.ReadAllText(@"../../content/bootstrap/css/bootstrap.min.css"),
                            StatusCode    = ResponseStatusCode.Ok,
                        };
                        response.Header.ContentType = "text/css";
                        return(response);
                    }
                },
                new Route()
                {
                    Name     = "JQuery JS",
                    UrlRegex = @"^/content/jquery/jquery-3.1.1.js$",
                    Method   = RequestMethod.GET,
                    Callable = (request) =>
                    {
                        var response = new HttpResponse
                        {
                            ContentAsUtf8 = File.ReadAllText(@"../../content/jquery/jquery-3.1.1.js"),
                            StatusCode    = ResponseStatusCode.Ok,
                        };
                        response.Header.ContentType = "application/x-javascript";
                        return(response);
                    }
                },
                new Route()
                {
                    Name     = "img placeholder lib",
                    UrlRegex = @"^/content/imsky-holder-8220898/holder.min.js$",
                    Method   = RequestMethod.GET,
                    Callable = request =>
                    {
                        var response = new HttpResponse()
                        {
                            ContentAsUtf8 = File.ReadAllText(@"../../content/imsky-holder-8220898/holder.min.js"),
                            StatusCode    = ResponseStatusCode.Ok
                        };
                        response.Header.ContentType = "application/x-javascript";
                        return(response);
                    }
                },
                new Route()
                {
                    Name     = "Contacts POST",
                    UrlRegex = @"^/contacts$",
                    Method   = RequestMethod.POST,

                    Callable = request =>
                    {
                        PostRequestHandler.HandleContactsPost(request.Content);
                        var response = new HttpResponse()
                        {
                            ContentAsUtf8 = factory.BuildThemedPage(request.Header.CookieCollection, request.Url),
                            StatusCode    = ResponseStatusCode.Ok
                        };
                        return(response);
                    }
                },
                new Route()
                {
                    Name     = "Products",
                    UrlRegex = @"^/products$",
                    Method   = RequestMethod.POST,
                    Callable = request =>
                    {
                        string where = PostRequestHandler.HandleProductsPost(request.Content);
                        var response = new HttpResponse()
                        {
                            ContentAsUtf8 = PageBuilder.BuildKnivesPage(factory, request.Header.CookieCollection, where),
                            StatusCode    = ResponseStatusCode.Ok
                        };
                        return(response);
                    }
                },
            };

            HttpServer server = new HttpServer(8081, routes);

            server.Listen();
        }
        private static async Task RegisterAndAssertWrongPhaseAsync(InputsRegistrationRequest req, PostRequestHandler handler)
        {
            var ex = await Assert.ThrowsAsync <WabiSabiProtocolException>(async() => await handler.RegisterInputAsync(req));

            Assert.Equal(WabiSabiProtocolErrorCode.WrongPhase, ex.ErrorCode);
        }