BadScript 2
Loading...
Searching...
No Matches
BadNetHostExtensions.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Collections.Specialized;
4using System.IO;
5using System.Linq;
6using System.Net;
7using System.Text;
8
17
19
24{
30 private static BadTable CreateNameValueTable(NameValueCollection headers)
31 {
32 BadTable t = new BadTable();
33
34 foreach (string key in headers.AllKeys)
35 {
36 t.SetProperty(key, headers[key]);
37 }
38
39 return t;
40 }
41
47 private static BadArray CreateCookieTable(CookieCollection cookies)
48 {
49 BadArray table = new BadArray();
50
51 foreach (Cookie cookie in cookies)
52 {
53 table.InnerArray.Add(new BadReflectedObject(cookie));
54 }
55
56 return table;
57 }
58
65 private static void CopyHeaderTable(NameValueCollection dst, BadTable src)
66 {
67 foreach (KeyValuePair<string, BadObject> kvp in src.InnerTable)
68 {
69 if (kvp.Value is not IBadString v)
70 {
71 throw new BadRuntimeException("Header Table must only contain string keys and values");
72 }
73
74 dst[kvp.Key] = v.Value;
75 }
76 }
77
84 private static BadTable CreateContentTable(Stream content, Encoding enc)
85 {
86 BadTable table = new BadTable();
87
88 table.SetFunction("AsString",
89 () =>
90 {
91 StreamReader sr = new StreamReader(content, enc);
92
93 return sr.ReadToEnd();
94 },
96 );
97
98 table.SetFunction("AsBytes",
99 () =>
100 {
101 byte[] data = new byte[content.Length];
102 int read = content.Read(data, 0, data.Length);
103
104 if (read != data.Length)
105 {
106 throw new BadNetworkConsoleException("Could not read all data from stream");
107 }
108
109 return new BadArray(data.Select(x => (BadObject)x)
110 .ToList()
111 );
112 },
114 );
115
116 return table;
117 }
118
123 {
124 provider.RegisterObject<BadHttpContext>("Request", c => new BadHttpRequest(c.Context.Request));
125 provider.RegisterObject<BadHttpContext>("Response", c => CreateResponseTable(c.Context.Response));
126 }
127
132 {
133 provider.RegisterObject<BadHttpRequest>("ContentLength", r => r.Request.ContentLength64);
134 provider.RegisterObject<BadHttpRequest>("ContentType", r => r.Request.ContentType);
135 provider.RegisterObject<BadHttpRequest>("Headers", r => CreateNameValueTable(r.Request.Headers));
136 provider.RegisterObject<BadHttpRequest>("HttpMethod", r => r.Request.HttpMethod);
137 provider.RegisterObject<BadHttpRequest>("IsLocal", r => r.Request.IsLocal);
138 provider.RegisterObject<BadHttpRequest>("IsSecureConnection", r => r.Request.IsSecureConnection);
139 provider.RegisterObject<BadHttpRequest>("IsAuthenticated", r => r.Request.IsAuthenticated);
140 provider.RegisterObject<BadHttpRequest>("IsWebSocketRequest", r => r.Request.IsWebSocketRequest);
141 provider.RegisterObject<BadHttpRequest>("KeepAlive", r => r.Request.KeepAlive);
142 provider.RegisterObject<BadHttpRequest>("QueryString", r => CreateNameValueTable(r.Request.QueryString));
143 provider.RegisterObject<BadHttpRequest>("RawUrl", r => r.Request.RawUrl);
144 provider.RegisterObject<BadHttpRequest>("Url", r => new BadReflectedObject(r.Request.Url));
145 provider.RegisterObject<BadHttpRequest>("UrlReferrer", r => new BadReflectedObject(r.Request.UrlReferrer));
146 provider.RegisterObject<BadHttpRequest>("ServiceName", r => r.Request.ServiceName);
147 provider.RegisterObject<BadHttpRequest>("UserAgent", r => r.Request.UserAgent);
148 provider.RegisterObject<BadHttpRequest>("ClientCertificateError", r => r.Request.ClientCertificateError);
149 provider.RegisterObject<BadHttpRequest>("HasEntityBody", r => r.Request.HasEntityBody);
150 provider.RegisterObject<BadHttpRequest>("UserHostAddress", r => r.Request.UserHostAddress);
151 provider.RegisterObject<BadHttpRequest>("UserHostName", r => r.Request.UserHostName);
152
153 provider.RegisterObject<BadHttpRequest>("UserLanguages",
154 r => new BadArray((r.Request.UserLanguages ?? Array.Empty<string>())
155 .Select(x => (BadObject)x)
156 .ToList()
157 )
158 );
159
160 provider.RegisterObject<BadHttpRequest>("AcceptTypes",
161 r => new BadArray((r.Request.AcceptTypes ?? Array.Empty<string>())
162 .Select(x => (BadObject)x)
163 .ToList()
164 )
165 );
166 provider.RegisterObject<BadHttpRequest>("ContentEncoding", r => r.Request.ContentEncoding.EncodingName);
167 provider.RegisterObject<BadHttpRequest>("Cookies", r => CreateCookieTable(r.Request.Cookies));
168
169 provider.RegisterObject<BadHttpRequest>("Content",
170 r => CreateContentTable(r.Request.InputStream,
171 r.Request.ContentEncoding
172 )
173 );
174 }
175
181 private static BadTable CreateResponseTable(HttpListenerResponse resp)
182 {
183 BadTable table = new BadTable();
184 table.SetFunction("SetHeader", (string key, string value) => resp.Headers[key] = value);
185 table.SetFunction("SetHeaders", (BadTable headers) => CopyHeaderTable(resp.Headers, headers));
186 table.SetFunction("SetCookie", (Cookie cookie) => resp.Cookies.Add(cookie));
187
188 table.SetFunction("SetCookies",
189 (BadArray cookies) =>
190 {
191 foreach (BadObject cookie in cookies.InnerArray)
192 {
193 if (cookie is BadReflectedObject { Instance: Cookie c })
194 {
195 resp.Cookies.Add(c);
196 }
197 else
198 {
199 throw new BadRuntimeException("Cookies must be Cookie objects");
200 }
201 }
202 }
203 );
204 table.SetFunction("SetStatusCode", (int code) => resp.StatusCode = code);
205 table.SetFunction("SetStatusDescription", (string desc) => resp.StatusDescription = desc);
206 table.SetFunction("SetContentType", (string type) => resp.ContentType = type);
207 table.SetFunction("SetContentEncoding", (string enc) => resp.ContentEncoding = Encoding.GetEncoding(enc));
208 table.SetFunction("SetContentLength", (long len) => resp.ContentLength64 = len);
209 table.SetFunction("SetKeepAlive", (bool keepAlive) => resp.KeepAlive = keepAlive);
210 table.SetFunction("Redirect", (string location) => resp.Redirect(location));
211 table.SetFunction("Abort", resp.Abort);
212 table.SetFunction("Close", resp.Close);
213
214 table.SetFunction("SetContent",
215 (string content) =>
216 {
217 byte[] data = (resp.ContentEncoding ?? Encoding.UTF8).GetBytes(content);
218 resp.OutputStream.Write(data, 0, data.Length);
219 }
220 );
221
222 return table;
223 }
224
226 protected override void AddExtensions(BadInteropExtensionProvider provider)
227 {
228 AddContextExtensions(provider);
229 AddRequestExtensions(provider);
230 }
231}
Exception that is thrown when the remote console encounters an error.
Wrapper for the HttpListenerContext.
HttpListenerContext Context
The underlying HttpListenerContext.
Wrapper for the HttpListenerRequest.
HttpListenerRequest Request
The underlying HttpListenerRequest.
Implements Extensions for the "NetHost" Api.
void AddRequestExtensions(BadInteropExtensionProvider provider)
Adds Extensions for the BadHttpRequest Object.
override void AddExtensions(BadInteropExtensionProvider provider)
static void CopyHeaderTable(NameValueCollection dst, BadTable src)
Copies the Headers from the BadScript Table to the Http Header Table.
static BadTable CreateContentTable(Stream content, Encoding enc)
Creates an Http Content Table.
static BadTable CreateNameValueTable(NameValueCollection headers)
Creates a table from a name value collection.
void AddContextExtensions(BadInteropExtensionProvider provider)
Adds Extensions for the BadHttpContext Object.
static BadArray CreateCookieTable(CookieCollection cookies)
Creates a Cookie Table from the Cookie Collection.
static BadTable CreateResponseTable(HttpListenerResponse resp)
Creates a Response Table for the given Response Object.
Public Extension API for the BS2 Runtime.
void RegisterObject(Type t, string propName, BadObject obj)
Registers the specified extension for the specified type.
Implements a Dynamic List/Array for the BadScript Language.
Definition BadArray.cs:17
List< BadObject > InnerArray
The Inner Array.
Definition BadArray.cs:45
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Implements a Table Structure for the BadScript Language.
Definition BadTable.cs:14
Helper Class that Builds a Native Class from a Prototype.
static BadClassPrototype GetNative(string name)
Returns a Native Class Prototype for the given Native Type.
Implements the Interface for Native Strings.
Definition IBadString.cs:7
Contains the Implementation of the Remote Console Abstraction over TCP.
Contains Network Hosting Extensions and APIs for the BadScript2 Runtime.
Contains the Error Objects for the BadScript2 Language.
Contains the Extension Classes for Functions.
Contains the Classes for Reflection Objects.
Contains the Interop Abstractions and Implementations for the BadScript2 Language.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10