Ejemplo n.º 1
0
        private void HandleTokenResponse(NameValueCollection parameters, IRestResponse response)
        {
            AfterGetAccessToken(new BeforeAfterRequestArgs
            {
                Response   = response,
                Parameters = parameters
            });

            AccessToken = ParseTokenResponse(response.Content, AccessTokenKey);
            if (String.IsNullOrEmpty(AccessToken))
            {
                throw new UnexpectedResponseException(AccessTokenKey);
            }

            if (GrantType != "refresh_token")
            {
                RefreshToken = ParseTokenResponse(response.Content, RefreshTokenKey);
            }

            TokenType = ParseTokenResponse(response.Content, TokenTypeKey);

            int expiresIn;

            if (Int32Ex.TryParse(ParseTokenResponse(response.Content, ExpiresKey), out expiresIn))
            {
                ExpiresAt = DateTime.Now.AddSeconds(expiresIn);
            }
        }
Ejemplo n.º 2
0
        public static byte[] CreatePacket_StartSizeStopCompressSum(byte[] data)
        {
            if (data.IsNullOrEmpty())
            {
                return(null);
            }

            byte[] data_compressed = data.GZip();
            byte   compression     = 0;

            if (data_compressed.Length < data.Length - 1)
            {
                compression = 1;
                data        = data_compressed;
            }

            byte[] length   = Int32Ex.ToBytes(data.Length);
            Int32  checksum = data.ChecksumInt32();

            byte[] check = Int32Ex.ToBytes(checksum);

            List <byte> result = new List <byte>();

            #region packet
            result.Add(TcpAsyncCommon.StartByte);
            result.Add(compression);
            result.AddRange(length);
            result.AddRange(check);
            result.AddRange(data);
            result.Add(TcpAsyncCommon.EndByte);
            #endregion

            return(result.ToArray());
        }
        private static void removePrefix(string uriPrefix, HttpListener listener)
        {
            var pref = new HttpListenerPrefix(uriPrefix);

            var addr = convertToIPAddress(pref.Host);

            if (addr == null)
            {
                return;
            }

            if (!addr.IsLocal())
            {
                return;
            }

            int port;

#if SSHARP
            if (!Int32Ex.TryParse(pref.Port, out port))
#else
            if (!Int32.TryParse(pref.Port, out port))
#endif
            { return; }

            if (!port.IsPortNumber())
            {
                return;
            }

            var path = pref.Path;
            if (path.IndexOf('%') != -1)
            {
                return;
            }

            if (path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                return;
            }

            var endpoint = new IPEndPoint(addr, port);

            EndPointListener lsnr;
            if (!_endpoints.TryGetValue(endpoint, out lsnr))
            {
                return;
            }

            if (lsnr.IsSecure ^ pref.IsSecure)
            {
                return;
            }

            lsnr.RemovePrefix(pref, listener);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="packet"></param>
        /// <param name="start">Is used to quickly find packet start without calculations</param>
        /// <param name="offest"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static bool RecreatePacket_StartSizeStopCompressSum(ref byte[] packet, out Int32 offest, out Int32 length, out byte compression)
        {
            offest      = -1;
            length      = -1;
            compression = 0;

            if (packet.IsNullOrEmpty())
            {
                return(false);
            }

            byte  start = TcpAsyncCommon.StartByte;
            byte  end = TcpAsyncCommon.EndByte;
            int   packet_length = packet.Length;
            Int32 _length, _offset;
            byte  _compression;

            for (int i = 0; i < packet_length - 11; i++)
            {
                if (packet[i] != start)
                {
                    continue;
                }

                _compression = packet[i + 1];

                _length = Int32Ex.FromBytes(packet, i + 2);
                _offset = i + 10;

                if (_length <= 0 ||                         //incorrect value test
                    (_length + _offset) <= 0 ||             //overflow test
                    packet_length <= (_length + _offset) || //invalid size test
                    packet[_offset + _length] != end)
                {
                    continue;
                }

                Int32 _checksum      = Int32Ex.FromBytes(packet, i + 6);
                Int32 _checksum_test = packet.ChecksumInt32(_offset, _length);  //(Int32)packet.SumValues(i + 1, 4);

                if (_checksum != _checksum_test || _length <= 0)
                {
                    continue;
                }

                offest      = _offset;
                length      = _length;
                compression = _compression;
                return(true);
            }

            return(false);
        }
Ejemplo n.º 5
0
        /*public static int ToIndex2D(int x, int y, int width, int height)
         * {
         *  if (x < 0 || y < 0 || width <= 0 || height <= 0 || x >= width || y >= height)
         *      return -1;
         *
         *  return x + (width * y);
         * }
         *
         * public static Point FromIndex2D(int index, int width, int height)
         * {
         *  if (index < 0 || width <= 0 || height <= 0 || index >= (width * height))
         *      return new Point(-1, -1);
         *
         *
         *  int y = index / width;
         *  int x = index + (width * y);
         *
         *
         *  return new Point(x, y);
         * }*/

        /// <summary>
        /// maximum allowed matrix dimentions are 32767x32767
        /// </summary>
        /// <param name="bitmaps"></param>
        /// <returns></returns>
        public static byte[] ToByteArray_16(this Bitmap[,] bitmaps, ImageFormat format = null)
        {
            if (bitmaps.IsNullOrEmpty())
            {
                return(null);
            }

            int xParts = bitmaps.Width();
            int yParts = bitmaps.Height();

            if (!xParts.InClosedInterval(1, Int16.MaxValue))
            {
                return(null);
            }
            if (!yParts.InClosedInterval(1, Int16.MaxValue))
            {
                return(null);
            }


            List <byte> result = new List <byte>();

            result.AddRange(Int16Ex.ToBytes((Int16)xParts));
            result.AddRange(Int16Ex.ToBytes((Int16)yParts));

            Int16 x = 0, y;

            byte[] dd;
            for (; x < xParts; x++)
            {
                for (y = 0; y < yParts; y++)
                {
                    dd = bitmaps[x, y].ToByteArray(format);

                    if (dd.IsNullOrEmpty())
                    {
                        continue;
                    }

                    result.AddRange(Int16Ex.ToBytes(x));
                    result.AddRange(Int16Ex.ToBytes(y));
                    result.AddRange(Int32Ex.ToBytes(dd.Length));
                    result.AddRange(dd);
                }
            }

            if (result.IsCountLessOrEqual(24))
            {
                return(null);
            }

            return(result.ToArray());
        }
Ejemplo n.º 6
0
        public static Bitmap[,] ToBitmap2D_16(this byte[] data, int offset)
        {
            if (data.IsCountLessOrEqual(24 + offset))
            {
                return(null);
            }

            int xParts = Int16Ex.FromBytes(data);
            int yParts = Int16Ex.FromBytes(data, 2);

            if (1.IsGreaterThenAny(xParts, yParts))
            {
                return(null);
            }

            Bitmap[,] result = new Bitmap[xParts, yParts];

            int i = 4;

            for (; i < data.Length;)
            {
                int x = Int16Ex.FromBytes(data, i);
                int y = Int16Ex.FromBytes(data, i + 2);
                int l = Int32Ex.FromBytes(data, i + 4);

                if (
                    !x.InClosedInterval(0, xParts - 1) ||
                    !y.InClosedInterval(0, yParts - 1) ||
                    l <= 0)
                {
                    return(null);
                }


                byte[] packet = data.SubArray(i + 8, l);

                result[x, y] = packet.ToBitmap();

                i += (8 + l);
            }

            return(result);
        }
        private static CookieCollection parseResponse(string value)
        {
            var ret = new CookieCollection();

            Cookie cookie = null;

            var caseInsensitive = StringComparison.InvariantCultureIgnoreCase;
            var pairs           = value.SplitHeaderValue(',', ';').ToList();

            for (var i = 0; i < pairs.Count; i++)
            {
                var pair = pairs[i].Trim();
                if (pair.Length == 0)
                {
                    continue;
                }

                var idx = pair.IndexOf('=');
                if (idx == -1)
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (pair.Equals("port", caseInsensitive))
                    {
                        cookie.Port = "\"\"";
                        continue;
                    }

                    if (pair.Equals("discard", caseInsensitive))
                    {
                        cookie.Discard = true;
                        continue;
                    }

                    if (pair.Equals("secure", caseInsensitive))
                    {
                        cookie.Secure = true;
                        continue;
                    }

                    if (pair.Equals("httponly", caseInsensitive))
                    {
                        cookie.HttpOnly = true;
                        continue;
                    }

                    continue;
                }

                if (idx == 0)
                {
                    if (cookie != null)
                    {
                        ret.add(cookie);
                        cookie = null;
                    }

                    continue;
                }

                var name = pair.Substring(0, idx).TrimEnd(' ');
                var val  = idx < pair.Length - 1
                                                         ? pair.Substring(idx + 1).TrimStart(' ')
                                                         : String.Empty;

                if (name.Equals("version", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    int num;
                    if (!Int32Ex.TryParse(val.Unquote(), out num))
                    {
                        continue;
                    }

                    cookie.Version = num;
                    continue;
                }

                if (name.Equals("expires", caseInsensitive))
                {
                    if (val.Length == 0)
                    {
                        continue;
                    }

                    if (i == pairs.Count - 1)
                    {
                        break;
                    }

                    i++;

                    if (cookie == null)
                    {
                        continue;
                    }

                    if (cookie.Expires != DateTime.MinValue)
                    {
                        continue;
                    }

                    var buff = new StringBuilder(val, 32);
                    buff.AppendFormat(", {0}", pairs[i].Trim());

                    DateTime expires;
                    if (!DateTimeEx.TryParseExact(
                            buff.ToString(),
                            new[] { "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'", "r" },
                            CultureInfo.CreateSpecificCulture("en-US"),
                            DateTimeStyles.AdjustToUniversal
                            | DateTimeStyles.AssumeUniversal,
                            out expires
                            )
                        )
                    {
                        continue;
                    }

                    cookie.Expires = expires.ToLocalTime();
                    continue;
                }

                if (name.Equals("max-age", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    int num;
                    if (!Int32Ex.TryParse(val.Unquote(), out num))
                    {
                        continue;
                    }

                    cookie.setMaxAge(num);

                    continue;
                }

                if (name.Equals("path", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    cookie.Path = val;
                    continue;
                }

                if (name.Equals("domain", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    cookie.Domain = val;
                    continue;
                }

                if (name.Equals("port", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    cookie.Port = val;
                    continue;
                }

                if (name.Equals("comment", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    cookie.Comment = urlDecode(val, Encoding.UTF8);
                    continue;
                }

                if (name.Equals("commenturl", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    cookie.CommentUri = val.Unquote().ToUri();
                    continue;
                }

                if (cookie != null)
                {
                    ret.add(cookie);
                }

                cookie = new Cookie(name, val);
            }

            if (cookie != null)
            {
                ret.add(cookie);
            }

            return(ret);
        }
        private static CookieCollection parseRequest(string value)
        {
            var ret = new CookieCollection();

            Cookie cookie = null;
            var    ver    = 0;

            var caseInsensitive = StringComparison.InvariantCultureIgnoreCase;
            var pairs           = value.SplitHeaderValue(',', ';').ToList();

            for (var i = 0; i < pairs.Count; i++)
            {
                var pair = pairs[i].Trim();
                if (pair.Length == 0)
                {
                    continue;
                }

                var idx = pair.IndexOf('=');
                if (idx == -1)
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (pair.Equals("$port", caseInsensitive))
                    {
                        cookie.Port = "\"\"";
                        continue;
                    }

                    continue;
                }

                if (idx == 0)
                {
                    if (cookie != null)
                    {
                        ret.add(cookie);
                        cookie = null;
                    }

                    continue;
                }

                var name = pair.Substring(0, idx).TrimEnd(' ');
                var val  = idx < pair.Length - 1
                                                         ? pair.Substring(idx + 1).TrimStart(' ')
                                                         : String.Empty;

                if (name.Equals("$version", caseInsensitive))
                {
                    if (val.Length == 0)
                    {
                        continue;
                    }

                    int num;
                    if (!Int32Ex.TryParse(val.Unquote(), out num))
                    {
                        continue;
                    }

                    ver = num;
                    continue;
                }

                if (name.Equals("$path", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    cookie.Path = val;
                    continue;
                }

                if (name.Equals("$domain", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    cookie.Domain = val;
                    continue;
                }

                if (name.Equals("$port", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    cookie.Port = val;
                    continue;
                }

                if (cookie != null)
                {
                    ret.add(cookie);
                }

                cookie = new Cookie(name, val);

                if (ver != 0)
                {
                    cookie.Version = ver;
                }
            }

            if (cookie != null)
            {
                ret.add(cookie);
            }

            return(ret);
        }
        private static void addPrefix(string uriPrefix, HttpListener listener
#if SSHARP
                                      , EthernetAdapterType adapter
#endif
                                      )
        {
            var pref = new HttpListenerPrefix(uriPrefix);

            var addr = convertToIPAddress(pref.Host);

            if (!addr.IsLocal())
            {
                throw new HttpListenerException(87, "Includes an invalid host.");
            }

            int port;

#if SSHARP
            if (!Int32Ex.TryParse(pref.Port, out port))
#else
            if (!Int32.TryParse(pref.Port, out port))
#endif
            { throw new HttpListenerException(87, "Includes an invalid port."); }

            if (!port.IsPortNumber())
            {
                throw new HttpListenerException(87, "Includes an invalid port.");
            }

            var path = pref.Path;
            if (path.IndexOf('%') != -1)
            {
                throw new HttpListenerException(87, "Includes an invalid path.");
            }

            if (path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                throw new HttpListenerException(87, "Includes an invalid path.");
            }

            var endpoint = new IPEndPoint(addr, port);

            EndPointListener lsnr;
            if (_endpoints.TryGetValue(endpoint, out lsnr))
            {
                if (lsnr.IsSecure ^ pref.IsSecure)
                {
                    throw new HttpListenerException(87, "Includes an invalid scheme.");
                }
            }
            else
            {
                lsnr = new EndPointListener(endpoint, pref.IsSecure, listener.CertificateFolderPath,
#if !NETCF || BCC || SSL
                                            listener.SslConfiguration,
#endif
                                            listener.ReuseAddress,
#if SSHARP
                                            adapter,
#endif
                                            listener.Log);

                _endpoints.Add(endpoint, lsnr);
            }

            lsnr.AddPrefix(pref, listener);
        }