public static bool Close(NSJSValue request) { NSJSObject o = request as NSJSObject; if (o == null) { return(false); } ObjectAuxiliary.RemoveInKeyValueCollection(o.Get("InputStream") as NSJSObject); NSJSArray files = o.Get("Files") as NSJSArray; if (files != null) { foreach (NSJSValue value in files) { if (value == null) { continue; } NSJSObject posedfile = value as NSJSObject; if (posedfile == null) { continue; } ObjectAuxiliary.RemoveInKeyValueCollection(o.Get("InputStream") as NSJSObject); } } return(ObjectAuxiliary.RemoveInKeyValueCollection(request as NSJSObject)); }
private static IEnumerable <HttpPostValue> array2blobs(NSJSArray array) { if (array == null) { return(null); } IList <HttpPostValue> blobs = new List <HttpPostValue>(); int count = array.Length; for (int i = 0; i < count; i++) { NSJSObject o = array[i] as NSJSObject; if (o == null) { continue; } HttpPostValue blob = new HttpPostValue(); blob.FileName = (o.Get("FileName") as NSJSString)?.Value; NSJSBoolean boolean = o.Get("IsText") as NSJSBoolean; if (boolean != null) { blob.IsText = boolean.Value; } blob.Key = (o.Get("Key") as NSJSString)?.Value; blob.Value = (o.Get("Value") as NSJSUInt8Array)?.Buffer; } return(blobs); }
public static MailAddress ToMailAddress(NSJSValue value) { NSJSObject a = value as NSJSObject; string address = null; if (a == null) { NSJSString s = value as NSJSString; if (s != null) { address = s.Value; if (string.IsNullOrEmpty(address)) { return(null); } return(new MailAddress(address)); } return(null); } address = ValueAuxiliary.ToString(a.Get("Address")); if (string.IsNullOrEmpty(address)) { return(null); } string displayName = ValueAuxiliary.ToString(a.Get("DisplayName")); return(new MailAddress(address, displayName, Encoding.UTF8)); }
public static Attachment ToAttachment(NSJSValue value) { if (value == null) { return(null); } if (value is NSJSString) { string path = ((NSJSString)value).Value; if (!File.Exists(path)) { return(null); } return(new Attachment(path)); } if (value is NSJSObject) { NSJSObject o = (NSJSObject)value; string fileName = o.Get("FileName").As <string>(); string mediaType = o.Get("MediaType").As <string>(); string blobName = o.Get("Name").As <string>(); Stream contentStream = NSJSStream.Get(o.Get("ContentStream") as NSJSObject); if (contentStream != null && !string.IsNullOrEmpty(blobName)) { return(new Attachment(contentStream, blobName, mediaType)); } if (!File.Exists(fileName)) { return(null); } return(new Attachment(fileName, mediaType)); } return(null); }
private static void Close(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); NSJSObject self = arguments.This; ObjectAuxiliary.RemoveInKeyValueCollection(self); HttpRequest.Close(self.Get("Request")); HttpResponse.Close(self.Get("Response") as NSJSObject); }
public static MailMessage ToMailMessage(NSJSValue value) { NSJSObject mail = value as NSJSObject; if (mail == null) { return(null); } MailMessage message = new MailMessage(); message.Body = ValueAuxiliary.ToString(mail.Get("Body")); message.Subject = ValueAuxiliary.ToString(mail.Get("Subject")); message.IsBodyHtml = ValueAuxiliary.ToBoolean(mail.Get("IsBodyHtml")); message.BodyEncoding = Encoding.UTF8; message.HeadersEncoding = Encoding.UTF8; message.SubjectEncoding = Encoding.UTF8; MailAddress address = ToMailAddress(mail.Get("From")); if (address == null) { return(null); } message.From = address; message.Sender = ToMailAddress(mail.Get("Sender")); ArrayAuxiliary.Fill(mail.Get("To"), message.To); ArrayAuxiliary.Fill(mail.Get("ReplyToList"), message.ReplyToList); ArrayAuxiliary.Fill(mail.Get("Attachments"), message.Attachments); return(message); }
private static bool ProcessEvent(object sender, string evt, EventArgs e) { if (sender == null || string.IsNullOrEmpty(evt)) { return(false); } WebSocket socket = sender as WebSocket; if (socket == null) { return(false); } NSJSObject websocket = Get(socket); if (websocket == null) { return(false); } NSJSVirtualMachine machine = websocket.VirtualMachine; machine.Join(delegate { NSJSFunction callback = websocket.Get(evt) as NSJSFunction; if (callback != null) { NSJSObject data = WebSocketClient.GetMessageEventData(machine, e); callback.Call(new NSJSValue[] { data }); } }); return(true); }
public static IPAddress ToAddress(NSJSValue value) { if (value == null) { return(null); } try { NSJSObject obj = value as NSJSObject; string address = null; if (obj != null) { byte[] buffer = (obj.Get("AddressBytes") as NSJSUInt8Array)?.Buffer; if (buffer != null) { return(new IPAddress(buffer)); } address = (obj.Get("Address") as NSJSString)?.Value; if (!string.IsNullOrEmpty(address)) { IPAddress addresst; if (IPAddress.TryParse(address, out addresst)) { return(addresst); } } } address = (value as NSJSString)?.Value; if (!string.IsNullOrEmpty(address)) { IPAddress addresst; if (IPAddress.TryParse(address, out addresst)) { return(addresst); } } return(null); } catch (Exception) { return(null); } }
protected virtual NSJSFunction GetProcessRequestCallback() { NSJSObject currentHandler = this.GetCurrentHandler(); if (currentHandler == null) { return(null); } return(currentHandler.Get("ProcessRequest") as NSJSFunction); }
private static HttpClientResponse object2response(NSJSObject response) { if (response == null) { return(null); } HttpClientResponse o = new HttpClientResponse(); NSJSBoolean boolean = response.Get("ManualWriteToStream") as NSJSBoolean; if (boolean != null) { o.ManualWriteToStream = boolean.Value; } NSJSObject objectt = response.Get("ContentStream") as NSJSObject; if (objectt != null) { o.ContentStream = NSJSStream.Get(objectt); } return(o); }
public static IDbDataParameter ToDbDataParameter(DatabaseAccessAdapter adapter, NSJSValue value) { if (adapter == null) { return(null); } NSJSObject o = value as NSJSObject; if (o == null) { return(null); } string name = (o.Get("Name") as NSJSString)?.Value; if (string.IsNullOrEmpty(name)) { return(null); } object rax = o.Get("Value").As <object>(); return(adapter.CreateParameter(name, rax)); }
private static void Using(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); NSJSObject self = arguments.Length > 0 ? arguments[0] as NSJSObject : null; NSJSFunction function = arguments.Length > 1 ? arguments[1] as NSJSFunction : null; if (function != null) { function.Call(self); } if (self != null) { NSJSFunction close = self.Get("Dispose") as NSJSFunction; if (close == null) { close = self.Get("Close") as NSJSFunction; } if (close != null) { close.Call(); } } }
private static void OnBeginProcessRequest(object sender, HttpBeginProcessRequestEventArgs e) { DoProcessRequest(sender, (application, origin, machine) => { NSJSFunction callback = origin.Get("BeginProcessRequest") as NSJSFunction; if (callback != null) { NSJSObject args = NSJSObject.New(machine); args.Set("Cancel", e.Cancel); args.Set("Application", origin); args.Set("CurrentContext", HttpHandler.NewContextObject(machine, origin, e.CurrentContext)); callback.Call(args); e.Cancel = args.Get("Cancel").As <bool>(); } }); }
public static NSJSValue SendEvent(NSJSObject obj, string evt, IEnumerable <NSJSValue> args) { if (obj == null || string.IsNullOrEmpty(evt)) { return(null); } NSJSFunction callback = obj.Get(evt) as NSJSFunction; if (callback == null) { return(null); } if (args == null) { return(callback.Call()); } return(callback.Call(args)); }
public static int Fill(NSJSValue source, NameValueCollection destination) { NSJSObject objecttive = source as NSJSObject; int count = 0; if (objecttive == null || destination == null) { return(count); } foreach (string key in objecttive.GetAllKeys()) { if (string.IsNullOrEmpty(key)) { continue; } destination.Set(key, ValueAuxiliary.ToString(objecttive.Get(key))); count++; } return(count); }
public static int Fill(NSJSValue source, NSJSValue destination) { NSJSObject sourceObject = source as NSJSObject; NSJSObject destinationObject = destination as NSJSObject; int count = 0; if (sourceObject == null || destinationObject == null) { return(count); } foreach (string key in sourceObject.GetAllKeys()) { if (string.IsNullOrEmpty(key)) { continue; } count++; NSJSValue value = sourceObject.Get(key); destinationObject.Set(key, value); } return(count); }
public static void New(NSJSFunctionCallbackInfo arguments) { NSJSObject options = arguments.Length > 0 ? arguments[0] as NSJSObject : null; if (options == null) { Throwable.ArgumentNullException(arguments.VirtualMachine); } else { string username = options.Get("UserName").As <string>(); string password = options.Get("Password").As <string>(); string domain = options.Get("Domain").As <string>(); string server = options.Get("Server").As <string>(); if (username == null || password == null || server == null) { Throwable.ArgumentNullException(arguments.VirtualMachine); } else if (username.Length <= 0 || password.Length <= 0 || server.Length <= 0) { Throwable.ArgumentException(arguments.VirtualMachine); } else { int port = MailClient.DefaultPort; NSJSInt32 i = (options.Get("Port") as NSJSInt32); if (i != null) { port = i.Value; } MailClient smtp = new MailClient(server, port, username, password, domain); smtp.EnableSsl = options.Get("EnableSsl").As <bool>(); i = (options.Get("Timeout") as NSJSInt32); if (i != null) { smtp.Timeout = i.Value; } arguments.SetReturnValue(New(arguments.VirtualMachine, smtp)); } } }
private static object ToObject(ConstructorInfo constructor, NSJSObject obj) { if (constructor == null || obj == null) { return(null); } object o = constructor.Invoke(null); IDictionary <string, MemberInfo> members = InternalCheckKeyMembers(constructor.DeclaringType); foreach (string key in obj.GetAllKeys()) { MemberInfo mi; if (!members.TryGetValue(key, out mi)) { continue; } PropertyInfo pi = mi as PropertyInfo; FieldInfo fi = mi as FieldInfo; Type mt = pi != null ? pi.PropertyType : fi.FieldType; Func <NSJSValue, object> converter = (Func <NSJSValue, object>)InternalCheckConverter(mt, 1); if (converter == null) { throw new InvalidOperationException(string.Format("Unable to find a valid JavaScript value To {0} type converter", mt.FullName)); } object value = converter(obj.Get(mi.Name)); if (pi == null) { fi.SetValue(o, value); } else { pi.SetValue(o, value, null); } } return(o); }
private static bool ProcessEvent(NSJSObject server, WebSocket socket, string evt, EventArgs e, bool newing = false) { if (server == null || socket == null || string.IsNullOrEmpty(evt)) { return(false); } NSJSVirtualMachine machine = server.VirtualMachine; machine.Join(delegate { NSJSObject socketclient = WebSocketClient.Get(socket); if (newing && socketclient == null) { socketclient = WebSocketClient.New(machine, socket); } NSJSFunction function = server.Get(evt) as NSJSFunction; if (function != null) { NSJSObject data = WebSocketClient.GetMessageEventData(machine, e); function.Call(new NSJSValue[] { socketclient, data }); } }); return(true); }
public static Cookie ToCookie(NSJSValue value) { NSJSObject o = value as NSJSObject; if (o == null) { return(null); } Cookie cookie = new Cookie(); cookie.Comment = ValueAuxiliary.ToString(o.Get("Comment")); cookie.Discard = ValueAuxiliary.ToBoolean(o.Get("Discard")); Uri url = default(Uri); if (Uri.TryCreate(ValueAuxiliary.ToString(o.Get("CommentUri")), UriKind.RelativeOrAbsolute, out url)) { cookie.CommentUri = url; } cookie.Domain = ValueAuxiliary.ToString(o.Get("Domain")); cookie.Expired = ValueAuxiliary.ToBoolean(o.Get("Expired")); cookie.Expires = ValueAuxiliary.ToDateTime(o.Get("Expires")); cookie.HttpOnly = ValueAuxiliary.ToBoolean(o.Get("HttpOnly")); cookie.Name = ValueAuxiliary.ToString(o.Get("Name")); cookie.Path = ValueAuxiliary.ToString(o.Get("Path")); cookie.Port = ValueAuxiliary.ToString(o.Get("Port")); cookie.Secure = ValueAuxiliary.ToBoolean(o.Get("Secure")); cookie.Value = ValueAuxiliary.ToString(o.Get("Value")); cookie.Version = ValueAuxiliary.ToInt32(o.Get("Version")); return(cookie); }
private static HttpClientOptions object2options(NSJSObject options) { if (options == null) { return(null); } HttpClientOptions o = new HttpClientOptions(); NSJSBoolean boolean = options.Get("AllowAutoRedirect") as NSJSBoolean; if (boolean != null) { o.AllowAutoRedirect = boolean.Value; } NSJSInt32 int32 = options.Get("AutomaticDecompression") as NSJSInt32; if (int32 != null) { o.AutomaticDecompression = (DecompressionMethods)int32.Value; } int32 = options.Get("CachePolicy") as NSJSInt32; if (int32 != null) { o.CachePolicy = new HttpRequestCachePolicy((HttpRequestCacheLevel)int32.Value); } int32 = options.Get("MaximumAutomaticRedirections") as NSJSInt32; if (int32 != null) { o.MaximumAutomaticRedirections = int32.Value; } int32 = options.Get("Timeout") as NSJSInt32; if (int32 != null) { o.Timeout = int32.Value; } NSJSString stringt = options.Get("Proxy") as NSJSString; if (stringt != null) { o.Proxy = new WebProxy(stringt.Value); } stringt = options.Get("Referer") as NSJSString; if (stringt != null) { o.Referer = stringt.Value; } NSJSInt32Array int32array = options.Get("Range") as NSJSInt32Array; if (int32array != null) { o.Range = int32array.Buffer; } NSJSArray array = options.Get("Headers") as NSJSArray; if (array != null) { NameValueCollection headers = o.Headers; int count = array.Length; for (int i = 0; i < count; i++) { stringt = array[i] as NSJSString; if (stringt == null) { continue; } headers.Add(headers); } } return(o); }