Example #1
0
 public SpiderRequest AddCookie(HTTPCookie Cookie)
 {
     this.Cookie.AddCookie(Cookie);
     return(this);
 }
        public override long Parse(byte[] data, uint offset, uint ends)
        {
            string[] sMethod = null;
            string[] sLines  = null;

            uint headerSize = 0;

            for (uint i = offset; i < ends - 3; i++)
            {
                if (data[i] == '\r' && data[i + 1] == '\n' &&
                    data[i + 2] == '\r' && data[i + 3] == '\n')
                {
                    sLines = Encoding.ASCII.GetString(data, (int)offset, (int)(i - offset)).Split(new string[] { "\r\n" },
                                                                                                  StringSplitOptions.None);

                    headerSize = i + 4;
                    break;
                }
            }

            if (headerSize == 0)
            {
                return(-1);
            }

            //Cookies = new DStringDictionary();
            //Headers = new DStringDictionary(true);

            sMethod = sLines[0].Split(' ');

            if (sMethod.Length == 3)
            {
                Version = sMethod[0].Trim();
                Number  = (ResponseCode)(Convert.ToInt32(sMethod[1].Trim()));
                Text    = sMethod[2];
            }

            // Read all headers

            for (int i = 1; i < sLines.Length; i++)
            {
                if (sLines[i] == String.Empty)
                {
                    // Invalid header
                    return(0);
                }

                if (sLines[i].IndexOf(':') == -1)
                {
                    // Invalid header
                    return(0);
                }

                string[] header = sLines[i].Split(new char[] { ':' }, 2);

                header[0]          = header[0].ToLower();
                Headers[header[0]] = header[1].Trim();

                //Set-Cookie: NAME=VALUE; expires=DATE;

                if (header[0] == "set-cookie")
                {
                    string[] cookie = header[1].Split(';');

                    if (cookie.Length >= 1)
                    {
                        string[]   splitCookie = cookie[0].Split('=');
                        HTTPCookie c           = new HTTPCookie(splitCookie[0], splitCookie[1]);

                        for (int j = 1; j < cookie.Length; j++)
                        {
                            splitCookie = cookie[j].Split('=');
                            switch (splitCookie[0].ToLower())
                            {
                            case "domain":
                                c.Domain = splitCookie[1];
                                break;

                            case "path":
                                c.Path = splitCookie[1];
                                break;

                            case "httponly":
                                c.HttpOnly = true;
                                break;

                            case "expires":
                                // Wed, 13-Jan-2021 22:23:01 GMT
                                c.Expires = DateTime.Parse(splitCookie[1]);
                                break;
                            }
                        }
                    }
                }
            }

            // Content-Length

            try
            {
                uint contentLength = uint.Parse((string)Headers["content-length"]);

                // check limit
                if (contentLength > data.Length - headerSize)
                {
                    return(contentLength - (data.Length - headerSize));
                }

                Message = DC.Clip(data, offset, contentLength);

                return(headerSize + contentLength);
            }
            catch
            {
                return(0);
            }
        }