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 table.SetFunction(
88 "AsString",
89 () =>
90 {
91 StreamReader sr = new StreamReader(content, enc);
92
93 return sr.ReadToEnd();
94 },
96 );
97 table.SetFunction(
98 "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).ToList());
110 },
112 );
113
114 return table;
115 }
116
121 {
122 provider.RegisterObject<BadHttpContext>("Request", c => new BadHttpRequest(c.Context.Request));
123 provider.RegisterObject<BadHttpContext>("Response", c => CreateResponseTable(c.Context.Response));
124 }
125
130 {
131 provider.RegisterObject<BadHttpRequest>("ContentLength", r => r.Request.ContentLength64);
132 provider.RegisterObject<BadHttpRequest>("ContentType", r => r.Request.ContentType);
133 provider.RegisterObject<BadHttpRequest>("Headers", r => CreateNameValueTable(r.Request.Headers));
134 provider.RegisterObject<BadHttpRequest>("HttpMethod", r => r.Request.HttpMethod);
135 provider.RegisterObject<BadHttpRequest>("IsLocal", r => r.Request.IsLocal);
136 provider.RegisterObject<BadHttpRequest>("IsSecureConnection", r => r.Request.IsSecureConnection);
137 provider.RegisterObject<BadHttpRequest>("IsAuthenticated", r => r.Request.IsAuthenticated);
138 provider.RegisterObject<BadHttpRequest>("IsWebSocketRequest", r => r.Request.IsWebSocketRequest);
139 provider.RegisterObject<BadHttpRequest>("KeepAlive", r => r.Request.KeepAlive);
140 provider.RegisterObject<BadHttpRequest>("QueryString", r => CreateNameValueTable(r.Request.QueryString));
141 provider.RegisterObject<BadHttpRequest>("RawUrl", r => r.Request.RawUrl);
142 provider.RegisterObject<BadHttpRequest>("Url", r => new BadReflectedObject(r.Request.Url));
143 provider.RegisterObject<BadHttpRequest>("UrlReferrer", r => new BadReflectedObject(r.Request.UrlReferrer));
144 provider.RegisterObject<BadHttpRequest>("ServiceName", r => r.Request.ServiceName);
145 provider.RegisterObject<BadHttpRequest>("UserAgent", r => r.Request.UserAgent);
146 provider.RegisterObject<BadHttpRequest>("ClientCertificateError", r => r.Request.ClientCertificateError);
147 provider.RegisterObject<BadHttpRequest>("HasEntityBody", r => r.Request.HasEntityBody);
148 provider.RegisterObject<BadHttpRequest>("UserHostAddress", r => r.Request.UserHostAddress);
149 provider.RegisterObject<BadHttpRequest>("UserHostName", r => r.Request.UserHostName);
151 "UserLanguages",
152 r => new BadArray((r.Request.UserLanguages ?? Array.Empty<string>()).Select(x => (BadObject)x).ToList())
153 );
155 "AcceptTypes",
156 r => new BadArray((r.Request.AcceptTypes ?? Array.Empty<string>()).Select(x => (BadObject)x).ToList())
157 );
158 provider.RegisterObject<BadHttpRequest>("ContentEncoding", r => r.Request.ContentEncoding.EncodingName);
159 provider.RegisterObject<BadHttpRequest>("Cookies", r => CreateCookieTable(r.Request.Cookies));
161 "Content",
162 r => CreateContentTable(r.Request.InputStream, r.Request.ContentEncoding)
163 );
164 }
165
171 private static BadTable CreateResponseTable(HttpListenerResponse resp)
172 {
173 BadTable table = new BadTable();
174 table.SetFunction("SetHeader", (string key, string value) => resp.Headers[key] = value);
175 table.SetFunction("SetHeaders", (BadTable headers) => CopyHeaderTable(resp.Headers, headers));
176 table.SetFunction("SetCookie", (Cookie cookie) => resp.Cookies.Add(cookie));
177 table.SetFunction(
178 "SetCookies",
179 (BadArray cookies) =>
180 {
181 foreach (BadObject cookie in cookies.InnerArray)
182 {
183 if (cookie is BadReflectedObject { Instance: Cookie c })
184 {
185 resp.Cookies.Add(c);
186 }
187 else
188 {
189 throw new BadRuntimeException("Cookies must be Cookie objects");
190 }
191 }
192 }
193 );
194 table.SetFunction("SetStatusCode", (int code) => resp.StatusCode = code);
195 table.SetFunction("SetStatusDescription", (string desc) => resp.StatusDescription = desc);
196 table.SetFunction("SetContentType", (string type) => resp.ContentType = type);
197 table.SetFunction("SetContentEncoding", (string enc) => resp.ContentEncoding = Encoding.GetEncoding(enc));
198 table.SetFunction("SetContentLength", (long len) => resp.ContentLength64 = len);
199 table.SetFunction("SetKeepAlive", (bool keepAlive) => resp.KeepAlive = keepAlive);
200 table.SetFunction("Redirect", (string location) => resp.Redirect(location));
201 table.SetFunction("Abort", resp.Abort);
202 table.SetFunction("Close", resp.Close);
203 table.SetFunction(
204 "SetContent",
205 (string content) =>
206 {
207 byte[] data = (resp.ContentEncoding ?? Encoding.UTF8).GetBytes(content);
208 resp.OutputStream.Write(data, 0, data.Length);
209 }
210 );
211
212 return table;
213 }
214
216 protected override void AddExtensions(BadInteropExtensionProvider provider)
217 {
218 AddContextExtensions(provider);
219 AddRequestExtensions(provider);
220 }
221}
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