feat: initial development

This commit is contained in:
2023-07-27 01:47:59 +04:00
commit 85dc5981cd
30 changed files with 3075 additions and 0 deletions

208
.editorconfig Normal file
View File

@@ -0,0 +1,208 @@
# To learn more about .editorconfig see https://aka.ms/editorconfigdocs
# top-most EditorConfig file
root = true
# Default settings:
# A newline ending every file
# Use 4 spaces as indentation
[*]
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
[*.json]
indent_size = 2
[*.{cs,xaml}]
charset = utf-8
end_of_line = crlf
# C# files
[*.cs]
# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true
# Indentation preferences
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_case_contents = true
csharp_indent_case_contents_when_block = true
csharp_indent_switch_labels = true
csharp_indent_labels = one_less_than_current
# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary : silent
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary : silent
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary : silent
dotnet_style_parentheses_in_other_operators = never_if_unnecessary : silent
# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async : suggestion
dotnet_style_require_accessibility_modifiers = for_non_interface_members : warning
# avoid this. unless absolutely necessary
dotnet_style_qualification_for_field = false : suggestion
dotnet_style_qualification_for_property = false : suggestion
dotnet_style_qualification_for_method = false : suggestion
dotnet_style_qualification_for_event = false : suggestion
# Types: use keywords instead of BCL types, and permit var only when the type is clear
csharp_style_var_for_built_in_types = false : suggestion
csharp_style_var_when_type_is_apparent = false : none
csharp_style_var_elsewhere = false : suggestion
dotnet_style_predefined_type_for_locals_parameters_members = true : suggestion
dotnet_style_predefined_type_for_member_access = true : suggestion
# Naming Conventions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case
dotnet_naming_style.camel_case_underscore_style.required_prefix = _
dotnet_naming_style.interfaces_style.capitalization = pascal_case
dotnet_naming_style.interfaces_style.required_prefix = I
# name all constant fields using PascalCase
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.required_modifiers = const
# static fields using PascalCase
dotnet_naming_rule.static_fields_should_have_prefix.severity = suggestion
dotnet_naming_rule.static_fields_should_have_prefix.symbols = static_fields
dotnet_naming_rule.static_fields_should_have_prefix.style = pascal_case_style
dotnet_naming_symbols.static_fields.applicable_kinds = field
dotnet_naming_symbols.static_fields.required_modifiers = static
dotnet_naming_symbols.static_fields.applicable_accessibilities = private, internal, private_protected
# internal and private fields should be _camelCase
dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion
dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields
dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style
dotnet_naming_symbols.private_internal_fields.applicable_kinds = field
dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal
# interfaces must have an I prefix
dotnet_naming_rule.interfaces_must_be_interfaces_style.severity = suggestion
dotnet_naming_rule.interfaces_must_be_interfaces_style.symbols = interfaces
dotnet_naming_rule.interfaces_must_be_interfaces_style.style = interfaces_style
dotnet_naming_symbols.interfaces.applicable_kinds = interface
dotnet_naming_symbols.interfaces.applicable_accessibilities = *
# Code style defaults
csharp_using_directive_placement = outside_namespace : suggestion
dotnet_sort_system_directives_first = true
csharp_prefer_braces = true : suggestion
csharp_preserve_single_line_blocks = true : none
csharp_preserve_single_line_statements = false : none
csharp_prefer_static_local_function = true : suggestion
csharp_prefer_simple_using_statement = false : none
csharp_style_prefer_switch_expression = true : suggestion
# Code quality
dotnet_style_readonly_field = true : suggestion
dotnet_code_quality_unused_parameters = non_public : suggestion
# Expression-level preferences
dotnet_style_object_initializer = true : suggestion
dotnet_style_collection_initializer = true : suggestion
dotnet_style_explicit_tuple_names = true : suggestion
dotnet_style_coalesce_expression = true : suggestion
dotnet_style_null_propagation = true : suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true : suggestion
dotnet_style_prefer_inferred_tuple_names = true : suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true : suggestion
dotnet_style_prefer_auto_properties = true : suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true : refactoring
dotnet_style_prefer_conditional_expression_over_return = true : refactoring
csharp_prefer_simple_default_expression = true : suggestion
csharp_style_deconstructed_variable_declaration = true : suggestion
# Expression-bodied members
csharp_style_expression_bodied_methods = true : refactoring
csharp_style_expression_bodied_constructors = true : refactoring
csharp_style_expression_bodied_operators = true : refactoring
csharp_style_expression_bodied_properties = true : refactoring
csharp_style_expression_bodied_indexers = true : refactoring
csharp_style_expression_bodied_accessors = true : refactoring
csharp_style_expression_bodied_lambdas = true : refactoring
csharp_style_expression_bodied_local_functions = true : refactoring
# Pattern matching
csharp_style_pattern_matching_over_is_with_cast_check = true : suggestion
csharp_style_pattern_matching_over_as_with_null_check = true : suggestion
csharp_style_inlined_variable_declaration = true : suggestion
# Null checking preferences
csharp_style_throw_expression = true : suggestion
csharp_style_conditional_delegate_call = true : suggestion
# Other features
csharp_style_prefer_index_operator = false : none
csharp_style_prefer_range_operator = false : none
csharp_style_pattern_local_over_anonymous_function = false : none
# Space preferences
csharp_space_after_cast = false
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_after_comma = true
csharp_space_after_dot = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_after_semicolon_in_for_statement = true
csharp_space_around_binary_operators = before_and_after
csharp_space_around_declaration_statements = do_not_ignore
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_before_comma = false
csharp_space_before_dot = false
csharp_space_before_open_square_brackets = false
csharp_space_before_semicolon_in_for_statement = false
csharp_space_between_empty_square_brackets = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_between_square_brackets = false
# Analyzers
dotnet_code_quality.ca1802.api_surface = private, internal
# C++ Files
[*.{cpp,h,in}]
curly_bracket_next_line = true
indent_brace_style = Allman
# Xml project files
[*.{csproj,vcxproj,vcxproj.filters,proj,nativeproj,locproj}]
indent_size = 2
# Xml build files
[*.builds]
indent_size = 2
# Xml files
[*.{xml,stylecop,resx,ruleset}]
indent_size = 2
# Xml config files
[*.{props,targets,config,nuspec}]
indent_size = 2
# Shell scripts
[*.sh]
end_of_line = lf
[*.{cmd, bat}]
end_of_line = crlf

2
.gitattributes vendored Normal file
View File

@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto

334
.gitignore vendored Normal file
View File

@@ -0,0 +1,334 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/

View File

@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\NaiveHttpServer\NaiveHttpServer.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Handlebars.Net" Version="2.1.4" />
</ItemGroup>
<ItemGroup>
<None Update="Views\index.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Static\css\feed.css">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Static\css\main.css">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Static\css\post.css">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Static\css\site.css">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

37
Blog.Server/Program.cs Normal file
View File

@@ -0,0 +1,37 @@
// See https://aka.ms/new-console-template for more information
using HandlebarsDotNet;
using NaiveHttpServer;
var server = new Server("localhost", 3000);
var source = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "Views/index.html"));
var template = Handlebars.Compile(source);
var data = new {
title = "My new post",
body = "This is my first post!"
};
var result = template(data);
// Build Routers
var router = new RouterBuilder()
.Get("/", async ctx =>
{
await ctx.Response.Html(result);
}).Build();
server
.Use(Middlewares.Log)
.Use(Middlewares.ExceptionHandling)
.Use(Middlewares.StaticFile("css/", Path.Combine(Environment.CurrentDirectory,"Static/css/")))
.Use(router)
.Use(Middlewares.NotFound(documentUrl: "http://api.project.com/v1"));
server.Start();
Console.ReadKey();
server.Stop();

View File

@@ -0,0 +1,74 @@
.blog-title {
font-family: 'Roboto', serif;
font-style: normal;
font-weight: 300;
font-size: 32px;
line-height: 40px;
color: var(--bs-info);
}
.blog-nav-list {
font-weight: 400;
font-size: 16px;
line-height: 24px;
list-style: none;
margin: 0;
padding: 0;
overflow: hidden;
}
.blog-nav-list > li {
float: left;
padding-right: 0.25rem;
color: #D9D9D9;
}
.active-nav-item {
color: var(--bs-info) !important;
}
.box {
box-sizing: border-box;
background-clip: padding-box;
}
.blog-author-avatar {
max-height: 32px;
}
.blog-author {
size: 14px;
line-height: 32px;
}
.post-link {
color: #4f4f4f !important;
}
.tag {
border: 1px solid var(--bs-info);
border-radius: 4px;
justify-content: center;
align-items: center;
padding: 2px 16px;
}
.static-avatar {
display: block;
overflow: hidden;
margin: 0 0 0 -6px;
width: 34px;
height: 34px;
border: 2px solid #fff;
border-radius: 100%;
}
.author-profile-image, .avatar-wrapper {
display: block;
width: 100%;
height: 100%;
background: #e3e9ed;
border-radius: 100%;
-o-object-fit: cover;
object-fit: cover;
}

View File

@@ -0,0 +1,205 @@
.timeline-card {
position: relative;
margin-left: 16px;
}
.timeline-card:before {
content: '';
display: inline-block;
position: absolute;
background-color: #fff;
border-radius: 100%;
width: 24px;
height: 24px;
top: 16px;
left: -12px;
border: 5px solid;
z-index: 2;
}
.timeline-body {
border-left: 2px solid #E6E9ED;
}
.timeline-card-primary:before {
border-color: var(--bs-primary);
}
.timeline-card-info:before {
border-color: var(--bs-info);
}
.timeline-card-secondary:before {
border-color: var(--bs-secondary);
}
.timeline-card-success:before {
border-color: var(--bs-teal);
}
html {
scroll-behavior: smooth;
}
html,
body {
overflow-x: hidden;
}
.container {
max-width: 1140px;
}
.site-title {
font-size: 1.25rem;
line-height: 2.5rem;
}
.nav-link {
padding: 0;
font-size: 1rem;
line-height: 2.5rem;
color: inherit;
opacity: 0.8;
}
.header-social .nav-link {
font-size: 1.25rem;
}
.nav-link:hover,
.nav-link:focus {
color: inherit;
opacity: 1;
}
.nav-item+.nav-item {
margin-left: 1rem;
}
.cover {
height: 500px;
}
.cover>img {
-o-object-fit: cover;
object-fit: cover;
width: auto;
}
.progress-bar {
text-transform: uppercase;
font-size: 10px;
letter-spacing: 1px;
}
.text-small {
font-size: 0.85rem;
}
.text-teal {
color: var(--bs-teal);
}
footer a:not(.nav-link) {
color: inherit;
border-bottom: 1px dashed;
text-decoration: none;
cursor: pointer;
}
.rounded-block {
border-radius: 0.5rem;
border: 1px solid rgba(0,0,0,.25);
}
@media (min-width: 48em) {
.site-title {
float: left;
}
.site-nav {
float: right;
}
}
@media (max-width: 767px) {
/* disable animations on mobile */
[data-aos] {
opacity: 1 !important;
transform: translate(0) scale(1) !important;
}
.p-5 {
padding: 2.5rem 2rem !important;
}
.portfolio-section .m-5 {
margin: 2rem 0 1rem !important;
}
.portfolio-reverse {
flex-direction: column-reverse;
}
.portfolio-reverse .text-end {
text-align: start !important;
}
}
@media print {
[data-aos] {
opacity: 1 !important;
transform: translate(0) scale(1) !important;
}
body.bg-light {
background-color: #fff !important;
}
.container {
width: auto;
max-width: 100%;
padding: 0;
}
.cover {
height: 360px;
margin-bottom: 1.5rem;
}
.cover>img {
display: none;
}
.shadow-1-strong {
box-shadow: none !important;
}
.resume-container>.my-5 {
margin: 0 !important;
}
.my-5.p-5 {
padding: 1.5rem 0 !important;
}
.about-section {
padding: 0 !important;
}
.skills-section,
.work-experience-section,
.education-section,
.portfolio-section,
.reference-section,
.contact-section {
padding: 0 !important;
}
.page-break {
padding-top: 5rem;
page-break-before: always;
}
}

View File

@@ -0,0 +1,51 @@
.blog-title {
font-family: 'Roboto', serif;
font-style: normal;
font-weight: 300;
font-size: 32px;
line-height: 40px;
color: var(--bs-info);
}
.static-avatar {
display: block;
overflow: hidden;
margin: 0 0 0 -6px;
width: 40px;
height: 40px;
border: 2px solid #fff;
border-radius: 100%;
}
.author-profile-image, .avatar-wrapper {
display: block;
width: 100%;
height: 100%;
background: #e3e9ed;
border-radius: 100%;
-o-object-fit: cover;
object-fit: cover;
}
.post-author {
color: #757575;
font-size: 14px;
}
.wp-block-image > img {
max-width: 100%;
width: 100%;
}
figcaption {
color: #757575;
font-size: 75%;
line-height: 1.5em;
text-align: center;
}
@media (min-width: 768px) {
.post-content {
padding: 0 170px 6vw;
}
}

View File

@@ -0,0 +1,18 @@
html {
font-size: 14px;
}
@media (min-width: 768px) {
html {
font-size: 16px;
}
}
html {
position: relative;
min-height: 100%;
}
body {
margin-bottom: 60px;
}

View File

@@ -0,0 +1,110 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Ivan Laletin - Senior Fullstack Developer's website and blog">
<meta property="og:title" content="Ivan Laletin - Senior Fullstack Developer" />
<meta property="og:description" content="Ivan Laletin - Senior Fullstack Developer's website and blog">
<meta property="og:site_name" content="Ivan Laletin">
<meta property="og:type" content="profile" />
<meta property="og:image" content="/images/I01P00012-min.webp" />
<meta property="og:url" content="https://e1lama.ru" />
<link rel="canonical" href="https://e1lama.ru">
<meta property="og:locale" content="en_US">
<meta property="profile:first_name" content="Ivan">
<meta property="profile:last_name" content="Laletin">
<meta property="profile:gender" content="male">
<meta property="profile:username" content="e1lama">
<title>Ivan Laletin - Senior Fullstack Developer</title>
<link rel="icon" type="image/x-icon" href="~/favicon.ico">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin="crossorigin" />
<link rel="preload" as="style"
href="https://fonts.googleapis.com/css2?family=Poppins:wght@600&amp;family=Roboto:wght@300;400;500;700&amp;display=swap" />
<link rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Poppins:wght@600&amp;family=Roboto:wght@300;400;500;700&amp;display=swap"
media="print" onload="this.media='all'" />
<noscript>
<link rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Poppins:wght@600&amp;family=Roboto:wght@300;400;500;700&amp;display=swap" />
</noscript>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/3.2.0/mdb.min.css" rel="stylesheet" media="all">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/aos/2.3.4/aos.css" integrity="sha512-1cK78a1o+ht2JcaW6g8OXYwqpev9+6GqOkz9xmBN9iUUhIndKtxwILGWYOSibOKjLsEdjyjZvYDq/cZwNeak0w==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link href="/css/main.css" rel="stylesheet">
<noscript>
<style type="text/css">
[data-aos] {
opacity: 1 !important;
transform: translate(0) scale(1) !important;
}
</style>
</noscript>
</head>
<body class="bg-light" id="top">
<header class="d-print-none">
<div class="container text-center text-lg-left">
<div class="pt-4 clearfix">
<h1 class="site-title mb-0">Ivan Laletin</h1>
<div class="site-nav">
<nav role="navigation">
<ul class="nav justify-content-center">
<li class="nav-item">
<a class="nav-link" title="Home" asp-area="" asp-page="/Index">
<span
class="menu-title">
Home
</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" title="Blog" asp-area="" asp-page="/Feed">
<span
class="menu-title">
Blog
</span>
</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
</header>
<main class="page-content">
{{body}}
</main>
<footer class="pt-4 pb-4 text-muted text-center d-print-none">
<div class="container">
<div class="my-3">
<div class="h4">Ivan Laletin</div>
<div class="footer-nav">
<nav role="navigation">
<ul class="nav justify-content-center">
<li class="nav-item"><a class="nav-link" href="https://twitter.com/backndr" title="Twitter"><i
class="fab fa-twitter"></i><span class="menu-title sr-only">Twitter</span></a>
</li>
<li class="nav-item"><a class="nav-link" href="https://github.com/HiveBeats" title="Github"><i
class="fab fa-github"></i><span class="menu-title sr-only">Github</span></a>
</li>
</ul>
</nav>
</div>
</div>
<div class="text-small">
<!-- todo: add, now is missed -->
<a href="./ivan.asc">PGP Public Key</a> Fingerprint: E4B4 1292 8F1A B309 7BB1 0D41 2F55 0236 0018 36A7
</div>
</div>
<!-- Please feel free to buy me a coffee -->
<!-- BTC: 1Nvb7A45ZGmS5zSSyDWWDFV7CnCYGKSUPV -->
<!-- TON: EQAmDR5k0t-SBULTN4cv63MHweHuD-r3FNuvshfe86L4cA5M -->
<!-- USDT (TRC20): TFPRYNgR5gLqE8WaLeDAf1RpWux2hdNdbK -->
</footer>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/3.2.0/mdb.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/aos/2.3.4/aos.js" integrity="sha512-A7AYk1fGKX6S2SsHywmPkrnzTZHrgiVT7GcQkLGDe2ev0aWb8zejytzS8wjo7PGEXKqJOrjQ4oORtnimIRZBtw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<!--<script src="/js/main.js?ver=1.2.1"></script>-->
</body>
</html>

14
Demo/Demo.csproj Normal file
View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\NaiveHttpServer\NaiveHttpServer.csproj" />
</ItemGroup>
</Project>

15
Demo/Program.cs Normal file
View File

@@ -0,0 +1,15 @@
using NaiveHttpServer;
var server = new Server("localhost", 2333);
server
.Use(Middlewares.Log)
.Use(Middlewares.ExceptionHandling)
.Use(Middlewares.StaticFile("/files", Environment.CurrentDirectory))
.Use(Middlewares.NotFound(documentUrl: "http://api.project.com/v1"));
server.Start();
Console.ReadKey();
server.Stop();

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Dingping Zhang
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

45
NaiveHttpServer.sln Normal file
View File

@@ -0,0 +1,45 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32929.385
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NaiveHttpServer", "NaiveHttpServer\NaiveHttpServer.csproj", "{DBE214C2-832D-4F3A-8AA5-1B3717EFEE2A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F9C0C1FB-0DBA-444E-A01C-A2BD05127557}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitignore = .gitignore
LICENSE = LICENSE
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo", "Demo\Demo.csproj", "{173B9B8A-AD3D-4BC3-917D-9E56E7B13681}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blog.Server", "Blog.Server\Blog.Server.csproj", "{1137F0C8-0B4A-4C6B-9FAD-4481E0E2C588}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DBE214C2-832D-4F3A-8AA5-1B3717EFEE2A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DBE214C2-832D-4F3A-8AA5-1B3717EFEE2A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DBE214C2-832D-4F3A-8AA5-1B3717EFEE2A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DBE214C2-832D-4F3A-8AA5-1B3717EFEE2A}.Release|Any CPU.Build.0 = Release|Any CPU
{173B9B8A-AD3D-4BC3-917D-9E56E7B13681}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{173B9B8A-AD3D-4BC3-917D-9E56E7B13681}.Debug|Any CPU.Build.0 = Debug|Any CPU
{173B9B8A-AD3D-4BC3-917D-9E56E7B13681}.Release|Any CPU.ActiveCfg = Release|Any CPU
{173B9B8A-AD3D-4BC3-917D-9E56E7B13681}.Release|Any CPU.Build.0 = Release|Any CPU
{1137F0C8-0B4A-4C6B-9FAD-4481E0E2C588}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1137F0C8-0B4A-4C6B-9FAD-4481E0E2C588}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1137F0C8-0B4A-4C6B-9FAD-4481E0E2C588}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1137F0C8-0B4A-4C6B-9FAD-4481E0E2C588}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5472BDAB-9DDB-46B0-996F-B1FE7DF2035C}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,29 @@
using System.Net;
namespace NaiveHttpServer
{
public delegate bool ParameterProvider(string key, out string value);
public class Context
{
public HttpListenerRequest Request { get; }
public HttpListenerResponse Response { get; }
public ILogger Logger { get; }
public ParameterProvider TryGetParameter { get; set; }
public Context(HttpListenerRequest request, HttpListenerResponse response, ILogger logger)
{
Request = request;
Response = response;
Logger = logger;
TryGetParameter = (string _, out string value) =>
{
value = null!;
return false;
};
}
}
}

View File

@@ -0,0 +1,38 @@
using System;
using System.Threading;
namespace NaiveHttpServer
{
internal class DefaultLogger : ILogger
{
public void Error(string message, Exception? exception = null)
{
Write(nameof(Error), message, exception);
}
public void Warning(string message, Exception? exception = null)
{
Write(nameof(Warning), message, exception);
}
public void Info(string message, Exception? exception = null)
{
Write(nameof(Info), message, exception);
}
public void Debug(string message, Exception? exception = null)
{
Write(nameof(Debug), message, exception);
}
private static void Write(string level, string message, Exception? exception)
{
Thread thread = Thread.CurrentThread;
string threadName = string.IsNullOrEmpty(thread.Name) ? thread.ManagedThreadId.ToString() : thread.Name!;
string exceptionString = exception is null ? string.Empty : $"{Environment.NewLine}{exception.Message}{Environment.NewLine}{exception.StackTrace}{Environment.NewLine}";
System.Diagnostics.Debug.WriteLine($"{DateTime.Now:HH:mm:ss.fff} {level.ToUpperInvariant()} [{threadName}] {message}{exceptionString}");
}
}
}

View File

@@ -0,0 +1,9 @@
namespace NaiveHttpServer
{
public static class ErrorCodes
{
public const string Unknown = "UNKNOWN";
public const string NotFoundApi = "NOT_FOUND_API";
public const string NotFoundFile = "NOT_FOUND_FILE";
}
}

View File

@@ -0,0 +1,83 @@
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace NaiveHttpServer
{
public static class Extensions
{
public static readonly JsonSerializerSettings JsonSettings = new()
{
MissingMemberHandling = MissingMemberHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
};
public static async Task Json(this HttpListenerResponse response, object value)
{
string jsonText = value.ToJson();
byte[] bytes = Encoding.UTF8.GetBytes(jsonText);
response.ContentEncoding = Encoding.UTF8;
response.ContentType = "application/json";
response.ContentLength64 = bytes.Length;
await response.OutputStream.WriteAsync(bytes, 0, bytes.Length);
}
public static async Task File(this HttpListenerResponse response, string filePath)
{
if (!System.IO.File.Exists(filePath))
{
return;
}
await FileHelper.ReadAsync(filePath, async stream =>
{
response.ContentType = MimeTypes.GetMimeType(filePath);
response.ContentLength64 = stream.Length;
await stream.CopyToAsync(response.OutputStream);
});
}
public static async Task Html(this HttpListenerResponse response, string template)
{
byte[] buffer = Encoding.UTF8.GetBytes(template);
response.ContentType = System.Net.Mime.MediaTypeNames.Text.Html;
response.ContentLength64 = buffer.Length;
await response.OutputStream.WriteAsync(buffer, 0, buffer.Length);
}
public static async Task Error(this HttpListenerResponse response, string errorCode, string message, int statusCode = 500)
{
response.StatusCode = statusCode;
await response.Json(new
{
errorCode,
message,
});
}
public static async Task<T?> JsonFromBody<T>(this HttpListenerRequest request)
{
string jsonText = await request.TextFromBody();
return jsonText.ToObject<T>();
}
public static async Task<string> TextFromBody(this HttpListenerRequest request)
{
using StreamReader reader = new(request.InputStream);
return await reader.ReadToEndAsync();
}
public static string ToJson(this object value, Formatting formatting = Formatting.None)
{
return JsonConvert.SerializeObject(value, formatting, JsonSettings);
}
public static T? ToObject<T>(this string json)
{
return JsonConvert.DeserializeObject<T>(json, JsonSettings);
}
}
}

View File

@@ -0,0 +1,89 @@
using System;
using System.IO;
using System.Threading.Tasks;
namespace NaiveHttpServer
{
public static class FileHelper
{
public static ILogger? Logger { get; set; }
public static async Task WriteAsync(string path, Func<Stream, Task> writer, bool isBackup = true)
{
string tempFilePath = $"{path}.writing";
using (var stream = new FileStream(
tempFilePath,
FileMode.Create,
FileAccess.Write,
FileShare.None,
0x10000,
FileOptions.SequentialScan)
)
{
await writer(stream);
}
if (File.Exists(path))
{
await SpinRetry(() => File.Replace(tempFilePath, path, isBackup ? BackupPath(path) : null, true));
}
else
{
await SpinRetry(() => File.Move(tempFilePath, path));
}
}
public static async Task ReadAsync(string path, Func<Stream, Task> reader)
{
try
{
await CriticalReadAsync(path, reader);
}
catch (Exception e)
{
string backupPath = BackupPath(path);
if (!File.Exists(backupPath))
{
throw;
}
Logger?.Warning($"Can not read {path}, turn back to backup.", e);
await CriticalReadAsync(backupPath, reader);
}
}
private static async Task CriticalReadAsync(string path, Func<Stream, Task> reader)
{
using FileStream stream = new(
path,
FileMode.OpenOrCreate,
FileAccess.Read,
FileShare.ReadWrite,
0x10000,
FileOptions.SequentialScan);
await reader(stream);
}
private static string BackupPath(string path) => $"{path}.backup";
private static async Task SpinRetry(Action action, int retryCount = 10)
{
for (int i = 0; i < retryCount; i++)
{
try
{
action();
await Task.Delay(100);
break;
}
catch
{
if (i == retryCount - 1)
{
throw;
}
}
}
}
}
}

View File

@@ -0,0 +1,10 @@
namespace NaiveHttpServer
{
public static class HttpMethods
{
public const string Get = "GET";
public const string Post = "POST";
public const string Put = "PUT";
public const string Delete = "DELETE";
}
}

View File

@@ -0,0 +1,15 @@
using System;
namespace NaiveHttpServer
{
public interface ILogger
{
void Error(string message, Exception? exception = null);
void Warning(string message, Exception? exception = null);
void Info(string message, Exception? exception = null);
void Debug(string message, Exception? exception = null);
}
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Threading.Tasks;
namespace NaiveHttpServer
{
public interface IRouterBuilder
{
IRouterBuilder Get(string url, Func<Context, Task> handler);
IRouterBuilder Post(string url, Func<Context, Task> handler);
IRouterBuilder Delete(string url, Func<Context, Task> handler);
IRouterBuilder Put(string url, Func<Context, Task> handler);
Middleware<Context> Build();
}
}

View File

@@ -0,0 +1,134 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
namespace NaiveHttpServer
{
public delegate Task Middleware<in T>(T ctx, Func<Task> next);
public static class Middlewares
{
public static Middleware<T> Empty<T>() => (_, next) => next();
public static async Task Log(Context ctx, Func<Task> next)
{
ILogger logger = ctx.Logger;
HttpListenerRequest request = ctx.Request;
HttpListenerResponse response = ctx.Response;
logger.Debug($"[In] {request.HttpMethod} {request.RawUrl}");
await next();
logger.Debug($"[Out] {request.HttpMethod} [{response.StatusCode}] {request.RawUrl}");
}
public static async Task ExceptionHandling(Context ctx, Func<Task> next)
{
using HttpListenerResponse response = ctx.Response;
try
{
await next();
}
catch (Exception e)
{
ctx.Logger.Warning("Unexpected exception occurred.", e);
await response.Error(ErrorCodes.Unknown, e.Message);
response.StatusCode = e switch
{
FileNotFoundException => 404,
DirectoryNotFoundException => 404,
UnauthorizedAccessException => 403,
_ => 500,
};
}
}
public static Middleware<Context> NotFound(string documentUrl)
{
return async (ctx, _) =>
{
ctx.Response.StatusCode = 404;
await ctx.Response.Error(
ErrorCodes.NotFoundApi,
$"Not found this api: '{ctx.Request.RawUrl}', and please read the API document: {documentUrl}.");
};
}
public static Middleware<Context> StaticFile(string route, string rootDir)
{
return async (ctx, next) =>
{
// Don't use Request.RawUrl, because it contains url parameters. (e.g. '?a=1&b=2')
string relativePath = ctx.Request.Url.AbsolutePath.TrimStart('/');
bool handled = relativePath.StartsWith(route);
if (!handled)
{
await next();
return;
}
string requestPath = HttpUtility.UrlDecode(relativePath)
.Substring(route.Length)
.ToLowerInvariant()
.TrimStart('/', '\\');
string filePath = Path.Combine(rootDir, requestPath);
switch (ctx.Request.HttpMethod)
{
case HttpMethods.Get:
await ReadLocalFile(filePath, ctx.Response, ctx.Logger);
break;
case HttpMethods.Put:
await WriteLocalFile(filePath, ctx.Request);
ctx.Response.StatusCode = 204;
break;
}
};
}
private static async Task ReadLocalFile(string filePath, HttpListenerResponse response, ILogger logger)
{
if (File.Exists(filePath))
{
await response.File(filePath);
}
else if (Directory.Exists(filePath))
{
string[] filePaths = Directory
.GetFileSystemEntries(filePath)
.Select(Path.GetFileName)
.ToArray();
await response.Json(filePaths);
}
else
{
string message = $"Not found the file: '{filePath}'.";
logger.Warning(message);
await response.Error(ErrorCodes.NotFoundFile, message, 404);
}
}
private static Task WriteLocalFile(string filePath, HttpListenerRequest request)
{
return FileHelper.WriteAsync(filePath, stream => request.InputStream.CopyToAsync(stream));
}
public static Middleware<T> Then<T>(this Middleware<T> middleware, Middleware<T> nextMiddleware)
{
return (ctx, next) => middleware(ctx, () => nextMiddleware(ctx, next));
}
public static Task Run<T>(this Middleware<T> middleware, T ctx)
{
return middleware(ctx, () =>
#if NET45
Task.FromResult(0)
#else
Task.CompletedTask
#endif
);
}
}
}

1032
NaiveHttpServer/MimeTypes.cs Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,95 @@
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Net" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<# var mediaTypes = GetMediaTypeList(); #>
// <auto-generated />
namespace <#=System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint").ToString()#>
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
/// <summary>
/// Provides utilities for mapping file names and extensions to MIME-types.
/// </summary>
[CompilerGenerated]
[DebuggerNonUserCode]
public static class MimeTypes
{
/// <summary>
/// The fallback MIME-type. Defaults to <c>application/octet-stream</c>.
/// </summary>
public static string FallbackMimeType { get; set; }
private static readonly Dictionary<string, string> TypeMap;
static MimeTypes()
{
FallbackMimeType = "application/octet-stream";
TypeMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
<# foreach (var mediaType in mediaTypes) { #>
{ "<#= mediaType.Item1 #>", "<#= mediaType.Item2 #>" },
<# } #>
};
}
/// <summary>
/// Gets the MIME-type for the given file name,
/// or <see cref="FallbackMimeType"/> if a mapping doesn't exist.
/// </summary>
/// <param name="fileName">The name of the file.</param>
/// <returns>The MIME-type for the given file name.</returns>
public static string GetMimeType(string fileName)
{
var dotIndex = fileName.LastIndexOf('.');
return dotIndex != -1 &&
fileName.Length > dotIndex + 1 &&
TypeMap.TryGetValue(fileName.Substring(dotIndex + 1), out var result)
? result
: FallbackMimeType;
}
}
}
<#+
private static IList<Tuple<string, string>> GetMediaTypeList()
{
using (var client = new WebClient())
{
var list = client.DownloadString(new Uri("http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types"));
var lines = SplitString(list, '\r', '\n');
return GetMediaTypes(lines).ToList();
}
}
private static IEnumerable<Tuple<string, string>> GetMediaTypes(IEnumerable<string> lines)
{
return lines.Where(x => !x.StartsWith("#"))
.Select(line => SplitString(line, '\t', ' '))
.SelectMany(CreateMediaTypes)
.GroupBy(x => x.Item1)
.Where(x => x.Count() == 1)
.Select(x => x.Single())
.OrderBy(x => x.Item1);
}
private static string[] SplitString(string line, params char[] separator)
{
return line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
}
private static IEnumerable<Tuple<string, string>> CreateMediaTypes(string[] parts)
{
return parts.Skip(1).Select(extension => Tuple.Create(extension, parts.First()));
}
#>

View File

@@ -0,0 +1,50 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net462</TargetFrameworks>
<Version>1.0.0</Version>
<LangVersion>latest</LangVersion>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild Condition="'$(Configuration)'=='Release'">true</GeneratePackageOnBuild>
<Authors>Dingping Zhang</Authors>
<Copyright>Copyright (c) 2020-2021 Dingping Zhang</Copyright>
<PackageProjectUrl>https://github.com/DingpingZhang/NaiveHttpServer</PackageProjectUrl>
<RepositoryUrl>https://github.com/DingpingZhang/NaiveHttpServer</RepositoryUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Description>A simple C# http server based on the HttpListener.</Description>
<PackageTags>http;http-server;file-server;simple-http-server</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="[13.0.1,)" />
<PackageReference Condition="'$(TargetFramework)' == 'net462'" Include="System.ValueTuple" Version="[4.5.0,)" />
</ItemGroup>
<ItemGroup>
<Reference Condition="'$(TargetFramework)' == 'net462'" Include="System.Web" />
</ItemGroup>
<ItemGroup>
<None Update="MimeTypes.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>MimeTypes.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>
<ItemGroup>
<Compile Update="MimeTypes.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>MimeTypes.tt</DependentUpon>
</Compile>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,50 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
namespace NaiveHttpServer
{
public static class NetAclChecker
{
public static ILogger? Logger { get; set; }
public static void AddAddress(string address)
{
AddAddress(address, Environment.UserDomainName, Environment.UserName);
}
public static void AddAddress(string address, string domain, string user)
{
string args = $@"http add urlacl url={address}, user={domain}\{user}";
try
{
ProcessStartInfo processStartInfo = new("netsh", args)
{
Verb = "runas",
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true,
};
var process = Process.Start(processStartInfo);
process?.WaitForExit();
}
catch (Win32Exception e)
{
if (e.NativeErrorCode == 1223)
{
Logger?.Info("User canceled the operation by rejected the UAC.");
}
else
{
Logger?.Warning($"Failed to 'netsh http add urlacl {address}' with an {nameof(Win32Exception)}.", e);
}
}
catch (Exception e)
{
Logger?.Warning($"Failed to 'netsh http add urlacl {address}'.", e);
}
}
}
}

View File

@@ -0,0 +1,135 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
namespace NaiveHttpServer
{
public class RouterBuilder : IRouterBuilder
{
private static readonly Regex PathParameterRegex = new("(?<=/):(.+?)(?:(?=/)|$)", RegexOptions.Compiled);
private static readonly char[] Separator = { '/' };
private readonly List<(string url, Func<Context, Task> handler)> _getRoutes = new();
private readonly List<(string url, Func<Context, Task> handler)> _postRoutes = new();
private readonly List<(string url, Func<Context, Task> handler)> _deleteRoutes = new();
private readonly List<(string url, Func<Context, Task> handler)> _putRoutes = new();
public IRouterBuilder Get(string url, Func<Context, Task> handler)
{
_getRoutes.Add((url, handler));
return this;
}
public IRouterBuilder Post(string url, Func<Context, Task> handler)
{
_postRoutes.Add((url, handler));
return this;
}
public IRouterBuilder Delete(string url, Func<Context, Task> handler)
{
_deleteRoutes.Add((url, handler));
return this;
}
public IRouterBuilder Put(string url, Func<Context, Task> handler)
{
_putRoutes.Add((url, handler));
return this;
}
public Middleware<Context> Build()
{
var getRoutes = GenerateRegexRoutes(_getRoutes);
var postRoutes = GenerateRegexRoutes(_postRoutes);
var deleteRoutes = GenerateRegexRoutes(_deleteRoutes);
var putRoutes = GenerateRegexRoutes(_putRoutes);
return async (ctx, next) =>
{
bool handled = ctx.Request.HttpMethod.ToUpperInvariant() switch
{
HttpMethods.Get => await TryMatch(getRoutes, ctx),
HttpMethods.Post => await TryMatch(postRoutes, ctx),
HttpMethods.Delete => await TryMatch(deleteRoutes, ctx),
HttpMethods.Put => await TryMatch(putRoutes, ctx),
_ => false,
};
if (!handled)
{
await next();
}
};
}
private static IReadOnlyList<(Regex regex, Func<Context, Task> handler)> GenerateRegexRoutes(IEnumerable<(string url, Func<Context, Task> handler)> routes)
{
var toSortRoutes = routes
.Select(item => (
fragmentCount: item.url.Split(Separator, StringSplitOptions.RemoveEmptyEntries).Length,
item.url,
item.handler))
.ToList();
toSortRoutes.Sort((x, y) => y.fragmentCount - x.fragmentCount);
return toSortRoutes
.Select(item => (regex: GetPathRegex(item.url), item.handler))
.ToList();
}
private static Regex GetPathRegex(string url)
{
HashSet<string> parameterNames = new();
string urlRegex = PathParameterRegex
.Replace(url.Trim('/'), match =>
{
if (!parameterNames.Add(match.Value))
{
throw new ArgumentException($"Cannot contains duplicate variable name: '{match.Value}'.", nameof(url));
}
return $"(?<{match.Groups[1]}>.+?)";
});
return new Regex($"{urlRegex}$", RegexOptions.Compiled);
}
private static async Task<bool> TryMatch(IEnumerable<(Regex regex, Func<Context, Task> handler)> routes, Context ctx)
{
string requestPath = ctx.Request.Url.LocalPath.ToLowerInvariant();
foreach ((Regex regex, Func<Context, Task> handler) in routes)
{
Match match = regex.Match(requestPath);
if (match.Success)
{
NameValueCollection query = HttpUtility.ParseQueryString(ctx.Request.Url.Query, Encoding.UTF8);
ctx.TryGetParameter = (string key, out string value) =>
{
Group group = match.Groups[key];
value = HttpUtility.UrlDecode(group.Value);
if (!group.Success)
{
value = query.Get(key);
}
return !string.IsNullOrEmpty(value);
};
await handler(ctx);
return true;
}
}
return false;
}
}
}

114
NaiveHttpServer/Server.cs Normal file
View File

@@ -0,0 +1,114 @@
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
namespace NaiveHttpServer
{
public class Server
{
private ILogger _logger = new DefaultLogger();
private HttpListener? _listener;
private Middleware<Context> _middleware = Middlewares.Empty<Context>();
public bool IsRunning => _listener is { IsListening: true };
public string HostUrl { get; }
public Server(string host, int port)
{
HostUrl = $"http://{host}:{port}/";
}
public Server Use(ILogger logger)
{
_logger = logger;
return this;
}
public Server Use(Middleware<Context> middleware)
{
_middleware = _middleware.Then(middleware);
return this;
}
public bool Start()
{
try
{
StartHttpListener();
return true;
}
catch (HttpListenerException e)
{
_logger.Warning("Failed to start HttpListener.", e);
if (e.ErrorCode == 5)
{
NetAclChecker.AddAddress(HostUrl);
StartHttpListener();
return true;
}
return false;
}
}
public void Stop()
{
if (_listener is { IsListening: true })
{
_listener.Stop();
_logger.Info("Http server has been stopped.");
}
}
private void StartHttpListener()
{
_listener = new HttpListener();
_listener.Prefixes.Add(HostUrl);
_listener.Start();
AsyncProcessRequest();
_logger.Info($"Http server has started listening: {HostUrl}...");
}
private void AsyncProcessRequest()
{
Task.Run(async () =>
{
while (_listener!.IsListening)
{
try
{
HttpListenerContext context = await _listener.GetContextAsync();
Context ctx = new(context.Request, context.Response, _logger);
#pragma warning disable 4014
_middleware.Run(ctx);
#pragma warning restore 4014
}
catch (IOException e)
{
_logger.Warning(nameof(IOException), e);
}
catch (HttpListenerException e)
{
const int errorOperationAborted = 995;
if (e.ErrorCode == errorOperationAborted)
{
// The IO operation has been aborted because of either a thread exit or an application request.
break;
}
_logger.Warning(nameof(HttpListenerException), e);
}
catch (InvalidOperationException e)
{
_logger.Warning(nameof(InvalidOperationException), e);
}
}
});
}
}
}

4
README.md Normal file
View File

@@ -0,0 +1,4 @@
# Credits
[NaiveHttpServer](https://github.com/DingpingZhang/NaiveHttpServer) - A simple library wrapper around HttpListener to build minimal HttpServer
[Handlebars.Net](https://github.com/Handlebars-Net/Handlebars.Net) - Handlebars port to .NET