Exemplo n.º 1
0
 public HTTPRequest SetURL(URL url)
 {
   if ((url == null) || ((url.Scheme != "http") && (url.Scheme != "https")))
   {
     url = null;
   }
   this.url = url;
   return this;
 }
Exemplo n.º 2
0
    /// <summary>Parse URL components (based on RFC3986).</summary>
    public static URL Parse(string str, string defaultScheme = "http")
    {
      if (string.IsNullOrEmpty(str) == true)
      {
        return null;
      }

      char[] chars;
      int pos;

      URL url = new URL();
      url._raw = str;

      // scheme
      pos = str.IndexOf(':');

      if (pos > 0)
      {
        url._scheme = str.Substring(0, pos).ToLower();
        str = str.Substring(pos + 1);
      }

      // authority
      pos = -1;
      if ((str.StartsWith("//") == true) &&
          (url._scheme != null) && (url._scheme != "file"))
      {
        pos = 2;
      }
      else if (str.StartsWith("/") == true)
      {
        if ((url._scheme != null) && (url._scheme != "file"))
        {
          return null;
        }

        url._scheme = "file";
      }
      else
      {
        if (string.IsNullOrEmpty(defaultScheme) == true)
        {
          return null;
        }

        url._scheme = defaultScheme.ToLower();
        pos = 0;
      }

      // file protocol does not have query nor fragment
      if (url._scheme == "file")
      {
        // path
        url.ParsePath(str);
        return url;
      }

      // validate scheme
      chars = new char[] {':', '/', '?', '#' };
      if ((string.IsNullOrEmpty(url._scheme) == true) ||
          (url._scheme.IndexOfAny(chars) >= 0))
      {
        return null;
      }

      if (pos >= 0)
      {
        str = url.ParseAuthority(str, pos);
      }

      // validate authority
      if (string.IsNullOrEmpty(url._authority) == true)
      {
        return null;
      }

      // fragment
      pos = str.IndexOf('#');

      if (pos >= 0)
      {
        url._fragment = str.Substring(pos + 1);
        str = str.Substring(0, pos);
      }

      // query
      pos = str.IndexOf('?');

      if (pos >= 0)
      {
        url._query = str.Substring(pos + 1);
        str = str.Substring(0, pos);
      }

      // path
      url.ParsePath(str);
      return url;
    }