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

Commiteccca70

Browse files
committed
feat: add index page
1 parent556b79d commiteccca70

File tree

2 files changed

+60
-22
lines changed

2 files changed

+60
-22
lines changed

‎README.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ Alternatively you can use `build.bat` .
2121
##Usage
2222

2323
```
24-
usage: SimpleHttpServer [-p PORT] [-b ADDR] [-r DIR]
25-
or SimpleHttpServer [-t] [-r DIR]
26-
or SimpleHttpServer [-P PREFIX] [-r DIR]
24+
usage: SimpleHttpServer [-r DIR] [-p PORT] [-b ADDR]
25+
or SimpleHttpServer [-r DIR] [-t]
26+
or SimpleHttpServer [-r DIR] [-P PREFIX]
2727
```
2828

2929
options:
3030

31+
*`-r DIR` : Specify the document root. The default is the current directory.
3132
*`-p PORT` : Specify the port number to listen on. The default is 8000
3233
*`-b ADDR` : Specify the address to bind to. The default is to accept all addresses.
33-
*`-r DIR` : Specify the document root. The default is the current directory.
34-
*`-t` : Use a prefix that does not require admin rights (`http://+:80/Temporary_Listen_Addresses/`)
34+
*`-t` : Use a prefix that does not require admin privileges (`http://+:80/Temporary_Listen_Addresses/`)
3535
*`-P PREFIX` : specify a prefix.
3636

3737
Administrative privileges are required unless the`-t` option is used.

‎SimpleHttpServer.cs‎

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
usingSystem;
22
usingSystem.IO;
33
usingSystem.Net;
4+
usingSystem.Text;
45
usingSystem.Text.RegularExpressions;
56

67
classSimpleHttpServer
@@ -18,6 +19,7 @@ class SimpleHttpServer
1819
{".svg","image/svg+xml"},
1920
{".json","application/json"},
2021
{".zip","application/zip"},
22+
{".pdf","application/pdf"},
2123
};
2224
staticstringport="8000";
2325
staticstringhost="+";
@@ -33,7 +35,8 @@ static void Main(string[] args)
3335
}
3436
try
3537
{
36-
stringprefixPath=Regex.Replace(prefix,@"https?://[^/]*","");
38+
stringprefixPath=WebUtility.UrlDecode(
39+
Regex.Replace(prefix,@"https?://[^/]*",""));
3740
using(varlistener=newHttpListener())
3841
{
3942
listener.Prefixes.Add(prefix);
@@ -45,36 +48,46 @@ static void Main(string[] args)
4548
varrequest=context.Request;
4649
using(varresponse=context.Response)
4750
{
48-
stringrawUrl=request.RawUrl;
49-
if(0<prefixPath.Length&&rawUrl.StartsWith(prefixPath))
51+
stringrawPath=WebUtility.UrlDecode(
52+
Regex.Replace(request.RawUrl,"[?;].*$",""));
53+
if(0<prefixPath.Length&&rawPath.StartsWith(prefixPath))
5054
{
51-
rawUrl=rawUrl.Substring(prefixPath.Length-1);
55+
rawPath=rawPath.Substring(prefixPath.Length-1);
5256
}
53-
stringpath=(root+Regex.Replace(rawUrl,"[?;].*$",""))
54-
.Replace("//","/").Replace("/",@"\");
55-
if(path.EndsWith(@"\"))
57+
stringpath=(root+rawPath).Replace("//","/").Replace("/",@"\");
58+
if(path.EndsWith(@"\")&&File.Exists(path+"index.html"))
5659
{
5760
path+="index.html";
5861
}
5962
byte[]content=null;
6063
if(!request.HttpMethod.Equals("GET"))
6164
{
62-
response.StatusCode=(int)HttpStatusCode.NotImplemented;
65+
response.StatusCode=(int)HttpStatusCode.NotImplemented;// 501
6366
}
6467
elseif(path.Contains(@"\..\")||path.EndsWith(@"\.."))
6568
{
66-
response.StatusCode=(int)HttpStatusCode.BadRequest;
69+
response.StatusCode=(int)HttpStatusCode.BadRequest;// 400
70+
}
71+
elseif(path.EndsWith(@"\")
72+
&&Directory.Exists(path.Substring(0,path.Length-1)))
73+
{
74+
stringindexPage=CreateIndexPage(path,rawPath);
75+
content=Encoding.UTF8.GetBytes(indexPage);
76+
response.ContentType="text/html";
77+
response.OutputStream.Write(content,0,content.Length);
6778
}
6879
elseif(Directory.Exists(path))
6980
{
7081
varhosts=request.Headers.GetValues("Host");
7182
varthisHost=(hosts!=null)?hosts[0]:request.UserHostAddress;
72-
response.Headers.Set("Location",string.Format("http://{0}{1}/",thisHost,request.RawUrl));
73-
response.StatusCode=(int)HttpStatusCode.MovedPermanently;
83+
response.Headers.Set(
84+
"Location",
85+
string.Format("http://{0}{1}/",thisHost,request.RawUrl));
86+
response.StatusCode=(int)HttpStatusCode.MovedPermanently;// 301
7487
}
7588
elseif(!File.Exists(path))
7689
{
77-
response.StatusCode=(int)HttpStatusCode.NotFound;
90+
response.StatusCode=(int)HttpStatusCode.NotFound;// 404
7891
}
7992
else
8093
{
@@ -87,10 +100,11 @@ static void Main(string[] args)
87100
catch(Exceptione)
88101
{
89102
Console.Error.WriteLine(e);
90-
response.StatusCode=(int)HttpStatusCode.Forbidden;
103+
response.StatusCode=(int)HttpStatusCode.Forbidden;// 403
91104
}
92105
}
93-
Console.WriteLine(string.Format("{0} - - [{1}]\"{2} {3} HTTP/{4}\" {5} {6}",
106+
Console.WriteLine(
107+
string.Format("{0} - - [{1}]\"{2} {3} HTTP/{4}\" {5} {6}",
94108
request.RemoteEndPoint.Address,
95109
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss K"),
96110
request.HttpMethod,request.RawUrl,request.ProtocolVersion,
@@ -118,9 +132,9 @@ static void ParseOptions(string[] args)
118132
else
119133
{
120134
Console.Error.WriteLine(
121-
"usage: SimpleHttpServer [-p PORT] [-b ADDR] [-r DIR]\n"+
122-
" or SimpleHttpServer [-t] [-r DIR]"+
123-
" or SimpleHttpServer [-P PREFIX] [-r DIR]");
135+
"usage: SimpleHttpServer [-r DIR] [-p PORT] [-b ADDR]\n"+
136+
" or SimpleHttpServer [-r DIR] [-t]"+
137+
" or SimpleHttpServer [-r DIR] [-P PREFIX]");
124138
Environment.Exit(0);
125139
}
126140
}
@@ -137,4 +151,28 @@ static string ContentType(string path)
137151
}
138152
return"application/octet-stream";
139153
}
154+
155+
staticstringCreateIndexPage(stringpath,stringurlPath)
156+
{
157+
string[]files=Directory.GetFileSystemEntries(path);
158+
stringindexPage=string.Format(
159+
"<html><head><meta charset=\"UTF-8\" /></head>\n"+
160+
"<body><h1>List of {0}</h1><ul>\n",
161+
WebUtility.HtmlEncode(urlPath));
162+
if(urlPath!="/")
163+
{
164+
indexPage+="<li><a href=\"..\">..</a></li>\n";
165+
}
166+
foreach(stringfileinfiles){
167+
stringbasename=Path.GetFileName(file);
168+
stringlink=string.Format(
169+
"<li><a href=\"{0}{2}\">{1}{2}</a></li>\n",
170+
WebUtility.UrlEncode(basename),
171+
WebUtility.HtmlEncode(basename),
172+
Directory.Exists(file)?"/":"");
173+
indexPage+=link;
174+
}
175+
indexPage+="</ul></body></html>\n";
176+
returnindexPage;
177+
}
140178
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp