Example #1
0
        public virtual void OnSuccess(WWW www)
        {
            bytes = www.bytes;

              // Unity does not support multiple response headers of the same name, luckily we can extract the raw
              // string through the assembly.
              PropertyInfo property = typeof(WWW).GetProperty("responseHeadersString", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
              string responseHeaders = property.GetValue(www, null) as string;

              string[] lines = responseHeaders.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);

              for (int i = 1; i < lines.Length; ++i) {
            string[] kv = lines[i].Split(new char[] { ':' }, 2);

            if (kv.Length >= 2) {
              string k = kv[0];
              string v = kv[1].Trim();

              if (!headers.ContainsKey(k)) {
            headers[k] = new List<string>();
              }

              headers[k].Add(v);
            }
              }

            #if UNITY_IOS
              // Unity will, depending on the platform, give back a single Set-Cookie header with all the values joined with a comma. Because the HTTP spec
              // for the Expires parameter has a comma in it this forces us to do some really painful string parsing below.
              if (headers.ContainsKey("Set-Cookie")) {
            foreach (string header in headers["Set-Cookie"]) {
              // Split all possible fields, including possibly multiple cookies.
              MatchCollection matches = CookieRegex.Matches(header);

              // Build the cookie(s) by reading values in-order.
              Cookie cookie = null;

              foreach (Match match in matches) {
            if (match.Success) {
              for (int i = 1; i < match.Groups.Count; ++i) {
                Group group = match.Groups[i];
                string[] kv = group.Value.Split('=');
                string k = kv[0];
                string v = kv.Length > 1 ? kv[1] : null;

                switch (k) {
                  case "Expires":
                    cookie.Expires = DateTime.Parse(v);
                    break;
                  case "Path":
                    cookie.Path = v;
                    break;
                  case "Domain":
                    cookie.Domain = v;
                    break;
                  case "Secure":
                    cookie.IsSecure = true;
                    break;
                  case "HttpOnly":
                    cookie.IsHttpOnly = true;
                    break;
                  default:
                    // New cookie, add the old one.
                    if (cookie != null) {
                      cookies.Add(cookie);
                      cookie = null;
                    }

                    cookie = new Cookie();
                    cookie.Name = k;
                    cookie.Value = v;
                    break;
                }
              }
            }
              }

              // This header has been parsed. Add the final cookie, if any.
              if (cookie != null) {
            cookies.Add(cookie);
            cookie = null;
              }
            }
              }
            #else
              if (headers.ContainsKey("Set-Cookie")) {
            foreach (string header in headers["Set-Cookie"]) {
              cookies.Add(new Cookie(header));
            }
              }
            #endif

              status.OnSuccess(www);

              if (status.Ok) {
            foreach (Cookie cookie in cookies) {
              Internal.Cookies.SetCookie(cookie);
            }
              }

              if (IsGzipped()) {
            Unzip();
              }
        }
Example #2
0
 public static void SetCookie(Cookie cookie)
 {
     all.Add(cookie);
 }