コード例 #1
0
ファイル: Http.cs プロジェクト: 24/source_04
        private void GetWebRequestHeaderValues()
        {
            if (_webResponse is HttpWebResponse)
            {
                HttpWebResponse httpResponse = (HttpWebResponse)_webResponse;
                _resultCharset = httpResponse.CharacterSet;
                if (_resultCharset != null)
                {
                    _resultCharset = _resultCharset.ToLower();
                }

                string   s  = httpResponse.ContentType.ToLower();
                string[] s2 = zsplit.Split(s, ';', true);
                if (s2.Length > 0)
                {
                    _resultContentType = s2[0];
                }

                _resultContentLength = httpResponse.ContentLength;
            }
            else if (_webResponse is FileWebResponse)
            {
                //Uri uri = new Uri(_httpRequest.Url);
                //string ext = zPath.GetExtension(uri.LocalPath).ToLower();
                //switch (ext)
                //{
                //    case ".xml":
                //        _resultContentType = "text/xml";
                //        break;
                //    case ".htm":
                //    case ".html":
                //    case ".asp":
                //    case ".php":
                //        _resultContentType = "text/html";
                //        break;
                //    case ".txt":
                //        _resultContentType = "text/txt";
                //        break;
                //    default:
                //        if (ext.Length > 1)
                //            _resultContentType = "/" + ext.Substring(1);
                //        break;
                //}
                // modif le 09/11/2015 ne gère plus les extensions inconnues ("/unknow_ext")
                _resultContentType = HttpTools.GetContentTypeFromFileExtension(zPath.GetExtension(new Uri(_httpRequest.Url).LocalPath));
            }
        }
コード例 #2
0
        //public static string UrlToFileName(string url, UrlFileNameType type, string ext = null, HttpRequestParameters requestParameters = null)
        //{
        //    string httpRequestContent = null;
        //    if (requestParameters != null)
        //        httpRequestContent = requestParameters.content;
        //    return UrlToFileName(url, type, ext, httpRequestContent);
        //}

        //public static string UrlToFileName(HttpRequest request, UrlFileNameType type, string ext = null)
        //{
        //    return UrlToFileName(request.Url, type, ext, request.Content);
        //}

        public static string UrlToFileName(string url, UrlFileNameType type, string ext = null, string httpRequestContent = null)
        {
            // source from Http.UrlToFileName()

            // url                                   : "http://www.site.com/toto/tata/index.php?name=search&page=2"
            // uri.Host                              : "www.site.com"
            // uri.Authority                         : "www.site.com"
            // uri.DnsSafeHost                       : "www.site.com"
            // uri.AbsoluteUri                       : "http://www.site.com/toto/tata/index.php?name=search&page=2"
            // uri.PathAndQuery                      : "/toto/tata/index.php?name=search&page=2"
            // uri.AbsolutePath                      : "/toto/tata/index.php"
            // uri.LocalPath                         : "/toto/tata/index.php"
            // uri.Query                             : "?name=search&page=2"
            // uri.Segments                          : "/, toto/, tata/, index.php"

            // url                                   : "http://www.site.com/toto/tata/"
            // uri.Host                              : "www.site.com"
            // uri.Authority                         : "www.site.com"
            // uri.DnsSafeHost                       : "www.site.com"
            // uri.AbsoluteUri                       : "http://www.site.com/toto/tata/"
            // uri.PathAndQuery                      : "/toto/tata/"
            // uri.AbsolutePath                      : "/toto/tata/"
            // uri.LocalPath                         : "/toto/tata/"
            // uri.Query                             : ""
            // uri.Segments                          : "/, toto/, tata/"

            // uri.GetComponents(UriComponents.UserInfo)
            // uri.GetLeftPart(UriPartial.Scheme)

            if (url == null)
            {
                return(null);
            }

            string file = "";
            //bool queryOrContent = false;
            Uri uri = new Uri(url);

            if ((type & UrlFileNameType.Host) == UrlFileNameType.Host)
            {
                file = uri.Host;
            }

            if ((type & UrlFileNameType.Path) == UrlFileNameType.Path)
            {
                if (file != "")
                {
                    file += "_";
                }
                //file += HttpUtility.UrlDecode(uri.AbsolutePath);
                string urlPath = HttpUtility.UrlDecode(uri.AbsolutePath);
                //urlExt = zPath.GetExtension(urlPath);
                if (urlPath.StartsWith("/"))
                {
                    urlPath = urlPath.Substring(1);
                }
                if (urlPath.EndsWith("/"))
                {
                    urlPath = urlPath.Substring(0, urlPath.Length - 1);
                }

                if ((type & UrlFileNameType.Ext) != UrlFileNameType.Ext)
                {
                    // attention with zpath.PathSetExt() / => \
                    urlPath = zpath.PathSetExtension(urlPath, null);
                }
                file += urlPath;
            }
            else if ((type & UrlFileNameType.FileName) == UrlFileNameType.FileName)
            {
                if (file != "")
                {
                    file += "_";
                }
                //file += HttpUtility.UrlDecode(uri.Segments[uri.Segments.Length - 1]);
                //if (file.EndsWith("/"))
                //    file = file.Substring(0, file.Length - 1);
                string urlFilename = HttpUtility.UrlDecode(uri.Segments[uri.Segments.Length - 1]);
                //urlExt = zPath.GetExtension(urlFilename);
                if (urlFilename.EndsWith("/"))
                {
                    urlFilename = urlFilename.Substring(0, urlFilename.Length - 1);
                }
                if ((type & UrlFileNameType.Ext) != UrlFileNameType.Ext)
                {
                    // attention with zpath.PathSetExt() / => \
                    urlFilename = zpath.PathSetExtension(urlFilename, null);
                }
                file += urlFilename;
            }

            if ((type & UrlFileNameType.Query) == UrlFileNameType.Query)
            {
                if (file != "")
                {
                    file += "_";
                }
                file += HttpUtility.UrlDecode(uri.Query);
                //queryOrContent = true;
            }

            if ((type & UrlFileNameType.Content) == UrlFileNameType.Content)
            {
                //if (requestParameters != null && requestParameters.content != null)
                if (httpRequestContent != null)
                {
                    if (file != "")
                    {
                        file += "_";
                    }
                    //file += requestParameters.content;
                    file += httpRequestContent;
                }
                //queryOrContent = true;
            }

            //file = __rgBadFilenameChars.Replace(file, "_");
            file = zfile.ReplaceBadFilenameChars(file, "_");
            //file = file.Replace('/', '_');
            //file = file.Replace('?', '_');
            //file = file.Replace('%', '_');
            //file = file.Replace('&', '_');
            file = file.Trim('_');
            file = __rgMultiUnderline.Replace(file, "_");
            //if (ext != null)
            //    file = zpath.PathSetExt(file, ext);
            //else if (!zPath.HasExtension(file))
            //    file = zpath.PathSetExt(file, ".html");

            if (file == "")
            {
                throw new PBException("error can't generate filename from url \"{0}\" of type {1}", url, type);
            }

            if (ext == null)
            {
                ext = zPath.GetExtension(uri.AbsolutePath);
                // modif le 09/11/2015 appel Http.GetContentTypeFromFileExtension() pour vérifier le type d'extension
                if (HttpTools.GetContentTypeFromFileExtension(ext) == null)
                {
                    file += ext;
                    ext   = "";
                }
                ////////////// annulation modif le 24/07/2016 ajout else pour ne pas ajouter ".html" si Http.GetContentTypeFromFileExtension(ext) = null
                if (ext == "")
                {
                    ext = ".html";
                }
                //else if (!queryOrContent)
                //    ext = null;
            }

            if (ext != null)
            {
                //if (!queryOrContent)
                //    file = zpath.PathSetExt(file, ext);
                //else
                //    file += ext;
                if (!file.EndsWith(ext))
                {
                    file += ext;
                }
            }
            if (file.Length > __maxFilenameLength)
            {
                ext = zPath.GetExtension(file);
                string filename = zPath.GetFileNameWithoutExtension(file);
                int    i        = filename.LastIndexOf('.');
                if (i != -1 && filename.Length - i < 10)
                {
                    ext      = filename.Substring(i) + ext;
                    filename = filename.Substring(0, i);
                }
                file = filename.Substring(0, filename.Length - (file.Length - __maxFilenameLength)) + ext;
            }

            return(file);
        }