Skip to content

Commit c90c5bd

Browse files
committed
refactor: update example pages for new interop and listener
1 parent 14c1731 commit c90c5bd

File tree

4 files changed

+107
-11
lines changed

4 files changed

+107
-11
lines changed

src/ExamplePages/ExamplePages.sln

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.5.2.0
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExamplePages", "ExamplePages.csproj", "{AC8BFE92-028A-8B37-2A5A-2A08C70E1BF0}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
Release|Any CPU = Release|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{AC8BFE92-028A-8B37-2A5A-2A08C70E1BF0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{AC8BFE92-028A-8B37-2A5A-2A08C70E1BF0}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
{AC8BFE92-028A-8B37-2A5A-2A08C70E1BF0}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
{AC8BFE92-028A-8B37-2A5A-2A08C70E1BF0}.Release|Any CPU.Build.0 = Release|Any CPU
17+
EndGlobalSection
18+
GlobalSection(SolutionProperties) = preSolution
19+
HideSolutionNode = FALSE
20+
EndGlobalSection
21+
GlobalSection(ExtensibilityGlobals) = postSolution
22+
SolutionGuid = {091DD2BC-26A6-4C99-BF1C-E4F9214843A5}
23+
EndGlobalSection
24+
EndGlobal

src/ExamplePages/Form.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ to compile and serve at runtime (where the namespace
1313
won't be an issue)
1414
*/
1515

16-
using SharpWebserver.Interop;
17-
using SharpWebserver.Clients;
16+
using System.Net;
1817
using System.Text;
19-
using System.Collections.Generic;
18+
using SharpWebserver.Interop;
2019

2120
public class Webpage : IScriptPage
2221
{
23-
public byte[] CreatePage(ConnectedClient client, Dictionary<string, string> arguments)
22+
public byte[] CreatePage(HttpListenerRequest request)
2423
{
2524
StringBuilder buffer = new();
2625
buffer.Append("""

src/ExamplePages/Index.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ to compile and serve at runtime (where the namespace
1313
won't be an issue)
1414
*/
1515

16-
using SharpWebserver.Interop;
17-
using SharpWebserver.Clients;
16+
using System.Net;
1817
using System.Text;
19-
using System.Collections.Generic;
18+
using SharpWebserver.Extensions;
19+
using SharpWebserver.Interop;
2020
public class Webpage : IScriptPage
2121
{
22-
public byte[] CreatePage(ConnectedClient client, Dictionary<string, string> arguments)
22+
public byte[] CreatePage(HttpListenerRequest request)
2323
{
24+
25+
var PostArgs = request.GetDecodedFormDataFirstOrDefault();
26+
var GetArgs = request.QueryString.GetFirstOrDefaultForAll();
27+
2428
StringBuilder buffer = new();
2529
buffer.Append("""
2630
<html>
@@ -33,10 +37,11 @@ public byte[] CreatePage(ConnectedClient client, Dictionary<string, string> argu
3337
This is the home page, generated from Index.cs!
3438
</p>
3539
""");
36-
buffer.Append($"<p>Your client id is: {client.ClientID} <br />Your remote is {client.Remote}</p>");
37-
buffer.Append("<p>Your request sent the following arguments</p>");
40+
buffer.Append($"<p>Your client remote is {request.RemoteEndPoint}</p>");
41+
42+
buffer.Append("<p>Your get request sent the following arguments</p>");
3843
buffer.AppendLine("<ul>");
39-
foreach (var item in arguments)
44+
foreach (var item in GetArgs)
4045
{
4146
buffer.Append("<li>");
4247
buffer.Append(item.Key);
@@ -45,6 +50,22 @@ public byte[] CreatePage(ConnectedClient client, Dictionary<string, string> argu
4550
buffer.AppendLine("</li>");
4651
}
4752
buffer.AppendLine("</ul>");
53+
54+
if (PostArgs is not null)
55+
{
56+
buffer.Append("<p>Your POST request sent the following arguments");
57+
buffer.AppendLine("<ul>");
58+
foreach (var item in PostArgs)
59+
{
60+
buffer.Append("<li>");
61+
buffer.Append(item.Key);
62+
buffer.Append(" : ");
63+
buffer.Append(item.Value);
64+
buffer.AppendLine("</li>");
65+
}
66+
buffer.AppendLine("</ul>");
67+
}
68+
4869
buffer.Append("""
4970
</body>
5071
</html>

src/ExamplePages/Ref.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
The contents of this file are provided under the CC0
3+
"No rights reserved" license in the hopes it will be
4+
useful.
5+
6+
Original Author: Robyn (robyn@mamallama.dev)
7+
8+
Notes:
9+
There will be errors shown in your IDE because these
10+
pages are not meant to be compiled. They are examples
11+
you may copy into your /www/ folder for SharpWebserver
12+
to compile and serve at runtime (where the namespace
13+
won't be an issue)
14+
15+
This page requires you to build ExampleLibrary and place
16+
it in your /references/ folder to demo assembly resolution
17+
*/
18+
19+
using System.Net;
20+
using System.Text;
21+
using ExampleLibrary;
22+
using SharpWebserver.Interop;
23+
24+
public class Webpage : IScriptPage
25+
{
26+
public byte[] CreatePage(HttpListenerRequest request)
27+
{
28+
var thing = new MyCoolThing(5);
29+
30+
StringBuilder buffer = new();
31+
buffer.Append("""
32+
<!DOCTYPE html>
33+
<html lang="en">
34+
<head>
35+
<meta charset="UTF-8">
36+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
37+
<title>Simple Form Example</title>
38+
</head>
39+
<body>
40+
""");
41+
42+
buffer.Append(thing.AmazingMethod);
43+
44+
buffer.Append(
45+
"""
46+
</body>
47+
</html>
48+
""");
49+
50+
return Encoding.UTF8.GetBytes(buffer.ToString());
51+
}
52+
}

0 commit comments

Comments
 (0)