コード例 #1
0
        private static void WriteAllText(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            bool      success   = false;
            Exception exception = null;

            if (arguments.Length > 1)
            {
                string   path      = (arguments[0] as NSJSString)?.Value;
                string   contents  = (arguments[1] as NSJSString)?.Value;
                Encoding encodings = NSJSEncoding.DefaultEncoding;
                if (arguments.Length > 2)
                {
                    encodings = NSJSEncoding.GetEncoding(arguments[2] as NSJSObject);
                }
                try
                {
                    FILE.WriteAllText(path, contents, encodings);
                    success = true;
                }
                catch (Exception e)
                {
                    exception = e;
                }
            }
            if (exception == null)
            {
                arguments.SetReturnValue(success);
            }
            else
            {
                Throwable.Exception(arguments.VirtualMachine, exception);
            }
        }
コード例 #2
0
        public static void GetEncoding(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            Encoding encoding = NSJSEncoding.DefaultEncoding;

            if (arguments.Length > 0)
            {
                string path = (arguments[0] as NSJSString)?.Value;
                if (path != null)
                {
                    encoding = GetEncoding(path);
                }
                else
                {
                    byte[] buffer = (arguments[0] as NSJSUInt8Array)?.Buffer;
                    int    offset = 0;
                    if (buffer != null && arguments.Length > 1)
                    {
                        NSJSInt32 i = arguments[1] as NSJSInt32;
                        offset = (i == null ? 0x00 : i.Value);
                    }
                    encoding = GetEncoding(buffer, offset);
                }
            }
            arguments.SetReturnValue(NSJSEncoding.New(arguments.VirtualMachine, encoding));
        }
コード例 #3
0
        private static void ContentEncoding(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            HTTPResponse             response  = GetResponse(arguments.This);

            if (response == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                NSJSVirtualMachine machine = arguments.VirtualMachine;
                if (arguments.Length <= 0)
                {
                    arguments.SetReturnValue(NSJSEncoding.New(machine, response.ContentEncoding));
                }
                else
                {
                    var encoding = NSJSEncoding.GetEncoding(arguments[0] as NSJSObject);
                    if (encoding == null)
                    {
                        encoding = NSJSEncoding.DefaultEncoding;
                    }
                    response.ContentEncoding = encoding;
                    arguments.SetReturnValue(true);
                }
            }
        }
コード例 #4
0
        private void ComputeHashValueOrString(IntPtr info, bool hashValue)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);

            byte[] buffer = null;
            if (arguments.Length > 0)
            {
                buffer = (arguments[0] as NSJSUInt8Array)?.Buffer;
                if (buffer == null)
                {
                    string s = (arguments[0] as NSJSString).Value;
                    if (s != null)
                    {
                        Encoding encoding = NSJSEncoding.DefaultEncoding;
                        if (arguments.Length > 1)
                        {
                            encoding = NSJSEncoding.GetEncoding(arguments[1] as NSJSObject);
                        }
                        buffer = encoding.GetBytes(s);
                    }
                }
            }
            if (buffer == null)
            {
                Throwable.ArgumentNullException(arguments.VirtualMachine);
            }
            else
            {
                try
                {
                    using (HASHAlgorithm hash = New())
                    {
                        buffer = hash.ComputeHash(buffer);
                        if (hashValue)
                        {
                            arguments.SetReturnValue(buffer);
                        }
                        else
                        {
                            string s = string.Empty;
                            for (int i = 0; i < buffer.Length; i++)
                            {
                                s += buffer[i].ToString("X2");
                            }
                            arguments.SetReturnValue(s);
                        }
                    }
                }
                catch (Exception e)
                {
                    Throwable.Exception(arguments.VirtualMachine, e);
                }
            }
        }
コード例 #5
0
ファイル: HttpRequest.cs プロジェクト: liulilittle/nsjs
        public static NSJSObject New(NSJSVirtualMachine machine, NSJSObject context, HTTPRequest request)
        {
            if (machine == null || context == null || request == null)
            {
                return(null);
            }
            NSJSObject objective = NSJSObject.New(machine);

            objective.Set("HttpMethod", request.HttpMethod);
            objective.Set("IsLocal", request.IsLocal);
            objective.Set("KeepAlive", request.KeepAlive);
            objective.Set("ContentType", request.ContentType);
            objective.Set("CurrentContext", context);
            objective.Set("Files", ObjectAuxiliary.ToObject(machine, request.Files));
            objective.Set("InputStream", NSJSValue.NullMerge(machine, NSJSStream.New(machine, request.InputStream)));
            objective.Set("Cookies", ArrayAuxiliary.ToArray(machine, request.Cookies));
            objective.Set("RequestTraceIdentifier", request.RequestTraceIdentifier.ToString()); // "D"
            objective.Set("RemoteEndPoint", ObjectAuxiliary.ToObject(machine, request.RemoteEndPoint));
            objective.Set("ContentEncoding", NSJSEncoding.New(machine, request.ContentEncoding ?? NSJSEncoding.DefaultEncoding));
            objective.Set("Form", ObjectAuxiliary.ToObject(machine, request.Form));
            objective.Set("QueryString", ObjectAuxiliary.ToObject(machine, request.QueryString));
            objective.Set("ContentLength", Convert.ToDouble(request.ContentLength));
            objective.Set("AcceptTypes", ArrayAuxiliary.ToArray(machine, request.AcceptTypes));
            objective.Set("Path", request.Path);
            objective.Set("RawUrl", request.RawUrl);
            objective.Set("ServiceName", request.ServiceName);
            objective.Set("Url", request.Url?.ToString());
            objective.Set("UrlReferrer", request.UrlReferrer?.ToString());
            objective.Set("UserAgent", request.UserAgent);
            objective.Set("UserHostAddress", request.UserHostAddress);
            objective.Set("UserHostName", request.UserHostName);
            objective.Set("ProtocolVersion", request.ProtocolVersion.ToString());
            objective.Set("LocalEndPoint", ObjectAuxiliary.ToObject(machine, request.LocalEndPoint));
            objective.Set("UserLanguages", ArrayAuxiliary.ToArray(machine, request.UserLanguages));
            return(objective);
        }
コード例 #6
0
        private static void ReadAllText(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            string    path      = arguments.Length > 0 ? (arguments[0] as NSJSString)?.Value : null;
            string    contents  = null;
            Exception exception = null;

            if (FILE.Exists(path))
            {
                Encoding encodings = NSJSEncoding.DefaultEncoding;
                if (arguments.Length > 1)
                {
                    encodings = NSJSEncoding.GetEncoding(arguments[1] as NSJSObject);
                }
                try
                {
                    contents = FILE.ReadAllText(path, encodings);
                }
                catch (Exception e)
                {
                    exception = e;
                }
            }
            if (contents != null)
            {
                arguments.SetReturnValue(contents);
            }
            else if (exception == null)
            {
                Throwable.FileNotFoundException(arguments.VirtualMachine);
            }
            else
            {
                Throwable.Exception(arguments.VirtualMachine, exception);
            }
        }