Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit56c736f

Browse files
committed
refact: format, naming
1 parent475a30d commit56c736f

File tree

1 file changed

+55
-35
lines changed

1 file changed

+55
-35
lines changed

‎SimpleHttpServer.cs‎

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,75 +5,80 @@
55
usingSystem.Text.RegularExpressions;
66
usingSystem.Web;
77

8-
classSimpleHttpServer
8+
publicclassSimpleHttpServer
99
{
10-
staticstringport="8000";
11-
staticstringhost="+";
12-
staticstringroot="./";
13-
staticstringprefix=null;
10+
privatestaticstrings_root="./";
11+
privatestaticstrings_prefix=null;
1412

15-
staticvoidMain(string[]args)
13+
publicstaticvoidMain(string[]args)
1614
{
17-
ParseOptions(args);
18-
if(prefix==null)
19-
{
20-
prefix=string.Format("http://{0}:{1}/",host,port);
21-
}
2215
try
2316
{
17+
ParseOptions(args);
2418
stringprefixPath=WebUtility.UrlDecode(
25-
Regex.Replace(prefix,@"https?://[^/]*",""));
19+
Regex.Replace(s_prefix,@"https?://[^/]*",""));
20+
2621
using(varlistener=newHttpListener())
2722
{
28-
listener.Prefixes.Add(prefix);
23+
listener.Prefixes.Add(s_prefix);
2924
listener.Start();
30-
Console.WriteLine("Listening on "+prefix);
25+
Console.WriteLine("Listening on "+s_prefix);
26+
3127
while(true)
3228
{
3329
varcontext=listener.GetContext();
3430
varrequest=context.Request;
31+
3532
using(varresponse=context.Response)
3633
{
3734
stringrawPath=WebUtility.UrlDecode(
3835
Regex.Replace(request.RawUrl,"[?;].*$",""));
36+
3937
if(0<prefixPath.Length&&rawPath.StartsWith(prefixPath))
4038
{
4139
rawPath=rawPath.Substring(prefixPath.Length-1);
4240
}
43-
stringpath=(root+rawPath).Replace("//","/").Replace("/",@"\");
41+
42+
stringpath=(s_root+rawPath)
43+
.Replace("//","/").Replace("/",@"\");
44+
4445
if(path.EndsWith(@"\")&&File.Exists(path+"index.html"))
4546
{
4647
path+="index.html";
4748
}
49+
4850
byte[]content=null;
51+
4952
if(!request.HttpMethod.Equals("GET"))
5053
{
51-
response.StatusCode=(int)HttpStatusCode.NotImplemented;//501
54+
response.StatusCode=501;//NotImplemented
5255
}
5356
elseif(path.Contains(@"\..\")||path.EndsWith(@"\.."))
5457
{
55-
response.StatusCode=(int)HttpStatusCode.BadRequest;//400
58+
response.StatusCode=400;//BadRequest
5659
}
5760
elseif(path.EndsWith(@"\")
5861
&&Directory.Exists(path.Substring(0,path.Length-1)))
5962
{
60-
stringindexPage=CreateIndexPage(path,rawPath);
61-
content=Encoding.UTF8.GetBytes(indexPage);
62-
response.ContentType="text/html";
63-
response.OutputStream.Write(content,0,content.Length);
63+
stringindexPage=CreateIndexPage(path,rawPath);
64+
content=Encoding.UTF8.GetBytes(indexPage);
65+
response.ContentType="text/html";
66+
response.OutputStream.Write(content,0,content.Length);
6467
}
6568
elseif(Directory.Exists(path))
6669
{
67-
varhosts=request.Headers.GetValues("Host");
68-
varthisHost=(hosts!=null)?hosts[0]:request.UserHostAddress;
70+
string[]hosts=request.Headers.GetValues("Host");
71+
stringthisHost=(hosts!=null)
72+
?hosts[0]
73+
:request.UserHostAddress;
6974
response.Headers.Set(
7075
"Location",
7176
string.Format("http://{0}{1}/",thisHost,request.RawUrl));
72-
response.StatusCode=(int)HttpStatusCode.MovedPermanently;//301
77+
response.StatusCode=301;//MovedPermanently
7378
}
7479
elseif(!File.Exists(path))
7580
{
76-
response.StatusCode=(int)HttpStatusCode.NotFound;//404
81+
response.StatusCode=404;//NotFound
7782
}
7883
else
7984
{
@@ -86,14 +91,17 @@ static void Main(string[] args)
8691
catch(Exceptione)
8792
{
8893
Console.Error.WriteLine(e);
89-
response.StatusCode=(int)HttpStatusCode.Forbidden;//403
94+
response.StatusCode=403;//Forbidden
9095
}
9196
}
97+
9298
Console.WriteLine(
9399
string.Format("{0} - - [{1}]\"{2} {3} HTTP/{4}\" {5} {6}",
94100
request.RemoteEndPoint.Address,
95101
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss K"),
96-
request.HttpMethod,request.RawUrl,request.ProtocolVersion,
102+
request.HttpMethod,
103+
request.RawUrl,
104+
request.ProtocolVersion,
97105
response.StatusCode,
98106
(content==null?0:content.Length)));
99107
}
@@ -106,15 +114,18 @@ static void Main(string[] args)
106114
}
107115
}
108116

109-
staticvoidParseOptions(string[]args)
117+
privatestaticvoidParseOptions(string[]args)
110118
{
119+
stringport="8000";
120+
stringhost="+";
121+
111122
for(vari=0;i<args.Length;i++)
112123
{
113-
if(args[i].Equals("-t"))prefix="http://+:80/Temporary_Listen_Addresses/";
114-
elseif(args[i].Equals("-p")&&i+1<args.Length)port=args[++i];
115-
elseif(args[i].Equals("-b")&&i+1<args.Length)host=args[++i];
116-
elseif(args[i].Equals("-r")&&i+1<args.Length)root=args[++i];
117-
elseif(args[i].Equals("-P")&&i+1<args.Length)prefix=args[++i];
124+
if(args[i].Equals("-t")){s_prefix="http://+:80/Temporary_Listen_Addresses/";}
125+
elseif(args[i].Equals("-p")&&i+1<args.Length){port=args[++i];}
126+
elseif(args[i].Equals("-b")&&i+1<args.Length){host=args[++i];}
127+
elseif(args[i].Equals("-r")&&i+1<args.Length){s_root=args[++i];}
128+
elseif(args[i].Equals("-P")&&i+1<args.Length){s_prefix=args[++i];}
118129
else
119130
{
120131
Console.Error.WriteLine(
@@ -124,20 +135,28 @@ static void ParseOptions(string[] args)
124135
Environment.Exit(0);
125136
}
126137
}
138+
139+
if(s_prefix==null)
140+
{
141+
s_prefix=string.Format("http://{0}:{1}/",host,port);
142+
}
127143
}
128144

129-
staticstringCreateIndexPage(stringpath,stringurlPath)
145+
privatestaticstringCreateIndexPage(stringpath,stringurlPath)
130146
{
131147
string[]files=Directory.GetFileSystemEntries(path);
132148
stringindexPage=string.Format(
133149
"<html><head><meta charset=\"UTF-8\" /></head>\n"+
134150
"<body><h1>List of {0}</h1><ul>\n",
135151
WebUtility.HtmlEncode(urlPath));
152+
136153
if(urlPath!="/")
137154
{
138155
indexPage+="<li><a href=\"..\">..</a></li>\n";
139156
}
140-
foreach(stringfileinfiles){
157+
158+
foreach(stringfileinfiles)
159+
{
141160
stringbasename=Path.GetFileName(file);
142161
stringlink=string.Format(
143162
"<li><a href=\"{0}{2}\">{1}{2}</a></li>\n",
@@ -146,6 +165,7 @@ static string CreateIndexPage(string path, string urlPath)
146165
Directory.Exists(file)?"/":"");
147166
indexPage+=link;
148167
}
168+
149169
indexPage+="</ul></body></html>\n";
150170
returnindexPage;
151171
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp