Пример #1
0
        /// <summary>
        /// Sets variable's type.
        /// </summary>
        /// <param name="ctx">Current runtime context.</param>
        /// <param name="variable">The variable.</param>
        /// <param name="type">The string identifying a new type. See PHP manual for details.</param>
        /// <returns>Whether <paramref name="type"/> is valid type identifier.</returns>
        /// <exception cref="PhpException"><paramref name="type"/> has invalid value.</exception>
        public static bool settype(Context ctx, ref PhpValue variable, string type)
        {
            switch (type.ToLowerInvariant())
            {
            case "bool":
            case "boolean":
                variable = PhpValue.Create(variable.ToBoolean());
                return(true);

            case "int":
            case "integer":
                variable = PhpValue.Create(variable.ToLong());
                return(true);

            case "float":
            case "double":
                variable = PhpValue.Create(variable.ToDouble());
                return(true);

            case "string":
                if (variable.TypeCode != PhpTypeCode.MutableString)        // already a string with possible binary data
                {
                    variable = PhpValue.Create(variable.ToString(ctx));
                }
                return(true);

            case "array":
                variable = PhpValue.Create(variable.ToArray());
                return(true);

            case "object":
                variable = PhpValue.FromClass(variable.ToClass());
                return(true);

            case "null":
                variable = PhpValue.Null;
                return(true);
            }

            PhpException.InvalidArgument(nameof(type), Resources.LibResources.invalid_type_name);
            return(false);
        }
Пример #2
0
        internal static bool TrySetOption(this CURLResource ch, int option, PhpValue value)
        {
            switch (option)
            {
            case CURLOPT_URL: return((ch.Url = value.AsString()) != null);

            case CURLOPT_DEFAULT_PROTOCOL: return((ch.DefaultSheme = value.AsString()) != null);

            case CURLOPT_HTTPGET: if (value.ToBoolean())
                {
                    ch.Method = WebRequestMethods.Http.Get;
                }
                break;

            case CURLOPT_POST: if (value.ToBoolean())
                {
                    ch.Method = WebRequestMethods.Http.Post;
                }
                break;

            case CURLOPT_PUT: if (value.ToBoolean())
                {
                    ch.Method = WebRequestMethods.Http.Put;
                }
                break;

            case CURLOPT_NOBODY: if (value.ToBoolean())
                {
                    ch.Method = WebRequestMethods.Http.Head;
                }
                break;

            case CURLOPT_CUSTOMREQUEST: return((ch.Method = value.AsString()) != null);

            case CURLOPT_POSTFIELDS: ch.PostFields = value.GetValue().DeepCopy(); break;

            case CURLOPT_FOLLOWLOCATION: ch.FollowLocation = value.ToBoolean(); break;

            case CURLOPT_MAXREDIRS: ch.MaxRedirects = (int)value.ToLong(); break;

            case CURLOPT_REFERER: return((ch.Referer = value.AsString()) != null);

            case CURLOPT_RETURNTRANSFER:
                ch.ProcessingResponse = value.ToBoolean()
                        ? ProcessMethod.Return
                        : ProcessMethod.StdOut;
                break;

            case CURLOPT_HEADER:
                ch.ProcessingHeaders = value.ToBoolean()
                        ? ProcessMethod.StdOut // NOTE: if ProcessingResponse is RETURN, RETURN headers as well
                        : ProcessMethod.Ignore;
                break;

            case CURLOPT_HTTPHEADER: ch.Headers = value.ToArray().DeepCopy(); break;

            case CURLOPT_ENCODING: ch.AcceptEncoding = value.ToStringOrNull().EmptyToNull(); break;

            case CURLOPT_COOKIE: return((ch.CookieHeader = value.AsString()) != null);

            case CURLOPT_COOKIEFILE: ch.CookieFileSet = true; break;

            case CURLOPT_FILE: return(TryProcessMethodFromStream(value, ProcessMethod.StdOut, ref ch.ProcessingResponse));

            case CURLOPT_INFILE: return(TryProcessMethodFromStream(value, ProcessMethod.Ignore, ref ch.ProcessingRequest, readable: true));

            case CURLOPT_WRITEHEADER: return(TryProcessMethodFromStream(value, ProcessMethod.Ignore, ref ch.ProcessingHeaders));

            //case CURLOPT_STDERR: return TryProcessMethodFromStream(value, ProcessMethod.Ignore, ref ch.ProcessingErr);

            case CURLOPT_HEADERFUNCTION: return(TryProcessMethodFromCallable(value, ProcessMethod.Ignore, ref ch.ProcessingHeaders));

            case CURLOPT_WRITEFUNCTION: return(TryProcessMethodFromCallable(value, ProcessMethod.StdOut, ref ch.ProcessingResponse));

            //case CURLOPT_READFUNCTION:
            //case CURLOPT_PROGRESSFUNCTION:

            case CURLOPT_USERAGENT: return((ch.UserAgent = value.AsString()) != null);

            case CURLOPT_BINARYTRANSFER: break;       // no effect

            case CURLOPT_PRIVATE: ch.Private = value.GetValue().DeepCopy(); break;

            case CURLOPT_TIMEOUT: { if (value.IsLong(out long l))
                                    {
                                        ch.Timeout = (int)l * 1000;
                                    }
                                    break; }

            case CURLOPT_TIMEOUT_MS: { if (value.IsLong(out long l))
                                       {
                                           ch.Timeout = (int)l;
                                       }
                                       break; }

            case CURLOPT_CONNECTTIMEOUT: return(false);         // TODO: is there an alternative in .NET ?

            case CURLOPT_CONNECTTIMEOUT_MS: return(false);      // TODO: is there an alternative in .NET ?

            case CURLOPT_BUFFERSIZE:
            {
                if (value.IsLong(out long l) && l < int.MaxValue && l >= 0)
                {
                    ch.BufferSize = (int)l;
                    return(true);
                }
                return(false);
            }

            case CURLOPT_EXPECT_100_TIMEOUT_MS: { if (value.IsLong(out long l))
                                                  {
                                                      ch.ContinueTimeout = (int)l;
                                                  }
                                                  break; }

            case CURLOPT_HTTP_VERSION:
                switch ((int)value.ToLong())
                {
                case CURL_HTTP_VERSION_NONE: ch.ProtocolVersion = null; break;

                case CURL_HTTP_VERSION_1_0: ch.ProtocolVersion = HttpVersion.Version10; break;

                case CURL_HTTP_VERSION_1_1: ch.ProtocolVersion = HttpVersion.Version11; break;

                case CURL_HTTP_VERSION_2_0:                                                 // == CURL_HTTP_VERSION_2:
                case CURL_HTTP_VERSION_2TLS: ch.ProtocolVersion = new Version(2, 0); break; // HttpVersion.Version20

                default: return(false);
                }
                break;

            case CURLOPT_USERNAME: ch.Username = value.ToString(); break;

            case CURLOPT_USERPWD: (ch.Username, ch.Password) = SplitUserPwd(value.ToString()); break;

            case CURLOPT_PROTOCOLS: ch.Protocols = (int)value.ToLong(); break;

            case CURLOPT_REDIR_PROTOCOLS:
                PhpException.ArgumentValueNotSupported(nameof(option), nameof(CURLOPT_REDIR_PROTOCOLS));
                break;

            case CURLOPT_SSL_VERIFYHOST:
            case CURLOPT_SSL_VERIFYPEER:
            case CURLOPT_SSL_VERIFYSTATUS:
                // always enabled
                break;

            case CURLINFO_HEADER_OUT: ch.StoreRequestHeaders = value.ToBoolean(); break;

            //
            default:
                PhpException.ArgumentValueNotSupported(nameof(option), TryGetOptionName(option));
                return(false);
            }

            return(true);
        }