zoukankan      html  css  js  c++  java
  • [CodeReserve]GetAttributeFromHeader

    以下代码可以从http header中获取一个attribute的值。如:

    String b = GetAttributeFromHeader(ContentType, "boundary");

    private static String GetAttributeFromHeader(String headerValue, String attrName) {
        if (headerValue == null)
            return null; 
    
        int l = headerValue.Length; 
        int k = attrName.Length; 
    
        // find properly separated attribute name 
        int i = 1; // start searching from 1
    
        while (i < l) {
            i = CultureInfo.InvariantCulture.CompareInfo.IndexOf(headerValue, attrName, i, CompareOptions.IgnoreCase); 
            if (i < 0)
                break; 
            if (i+k >= l) 
                break;
     
            char chPrev = headerValue[i-1];
            char chNext = headerValue[i+k];
            if ((chPrev == ';' || chPrev == ',' || Char.IsWhiteSpace(chPrev)) && (chNext == '=' || Char.IsWhiteSpace(chNext)))
                break; 
    
            i += k; 
        } 
    
        if (i < 0 || i >= l) 
            return null;
    
        // skip to '=' and the following whitespaces
        i += k; 
        while (i < l && Char.IsWhiteSpace(headerValue[i]))
            i++; 
        if (i >= l || headerValue[i] != '=') 
            return null;
        i++; 
        while (i < l && Char.IsWhiteSpace(headerValue[i]))
            i++;
        if (i >= l)
            return null; 
    
        // parse the value 
        String attrValue = null; 
    
        int j; 
    
        if (i < l && headerValue[i] == '"') {
            if (i == l-1)
                return null; 
            j = headerValue.IndexOf('"', i+1);
            if (j < 0 || j == i+1) 
                return null; 
    
            attrValue = headerValue.Substring(i+1, j-i-1).Trim(); 
        }
        else {
            for (j = i; j < l; j++) {
                if (headerValue[j] == ' ' || headerValue[j] == ',') 
                    break;
            } 
     
            if (j == i)
                return null; 
    
            attrValue = headerValue.Substring(i, j-i).Trim();
        }
     
        return attrValue;
    } 
  • 相关阅读:
    matplotlib数据可视化之柱形图
    xpath排坑记
    Leetcode 100. 相同的树
    Leetcode 173. 二叉搜索树迭代器
    Leetcode 199. 二叉树的右视图
    Leetcode 102. 二叉树的层次遍历
    Leetcode 96. 不同的二叉搜索树
    Leetcode 700. 二叉搜索树中的搜索
    Leetcode 2. Add Two Numbers
    Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/volnet/p/1416921.html
Copyright © 2011-2022 走看看