• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-161  評論-223  文章-30  trackbacks-0
            字符集合 
               依據(jù)RFC3986 2規(guī)范,HTTP URI中允許出現(xiàn)的US-ASCII字符的子集,可以分成保留未保留轉(zhuǎn)義這幾類,每類的全部字符列表如下
                  ● 保留:  : / ? # [ ] @ ! $ & '( ) * + ,; =共18個,一般用于URI部件分隔符。
                  ● 未保留:  a-z A-Z 0-9 - . _ ~共66個,一般用于部件內(nèi)數(shù)據(jù)。
                  ● 轉(zhuǎn)義:  %HEXHEX,HEX表示一個十六進制數(shù)字[0-9A-F]或[0-9a-f],通常采用大寫,這兩個HEX就表示一個US-ASCII字符代碼,轉(zhuǎn)義用于在URI內(nèi)部插入保留字符及原本不支持的字符。


            編碼原理
             
               當構(gòu)建URI的部件時,其中的一個八位字節(jié)碼相應的字符超出了允許的集合或被用作分隔符,就需要編碼,正是在這個時候,由實現(xiàn)決定保留字符的哪些被用于子部件分隔符,哪個被安全地用于數(shù)據(jù)。一個百分號編碼的八位字節(jié)碼被編碼成一個三重字符, 包括百分號字符"%"和隨后的兩個十六進制數(shù)字展示那個八位字節(jié)的數(shù)值。

            字符映射表
               用于快速判斷一個字符是否為未保留字符,定義如下
             1const char http_uri_table[256= 
             2{
             3    /* 0 */
             4    00000000,   00000000,
             5    00000000,   00000000,
             6    00000000,   00000110,
             7    11111111,   11000000,
             8    /* 64 */
             9    01111111,   11111111,
            10    11111111,   11100001,
            11    01111111,   11111111,
            12    11111111,   11100010,
            13    /* 128 */
            14    00000000,   00000000,
            15    00000000,   00000000,
            16    00000000,   00000000,
            17    00000000,   00000000,
            18    /* 192 */
            19    00000000,   00000000,
            20    00000000,   00000000,
            21    00000000,   00000000,
            22    00000000,   00000000,
            23}
            ;
            24#define HTTP_CHAR_IS_UNRESERVED(c) (http_uri_table[(unsigned char)(c)])

            接口實現(xiàn)
               http_uri_encode有2個版本:一個帶uri長度參數(shù)len,另一個則不帶。
             1void http_uri_encode(const char *uri, size_t len, std::string &str,bool space_as_plus/*=false*/)
             2{
             3    char c,buf[4];
             4
             5    for (size_t i = 0; i < len; i++{
             6        c = uri[i];
             7        if (HTTP_CHAR_IS_UNRESERVED(c)) {
             8            str.push_back(c);
             9        }
            else if(c == ' ' && space_as_plus) {
            10            str.push_back('+');
            11        }
            else{
            12            sprintf(buf,"%%%02X",(unsigned char)c);
            13            str.append(buf);
            14        }

            15    }

            16}

            17
            18void http_uri_encode(const char *uri, std::string &str,bool space_as_plus/*=false*/)
            19{
            20    char c,buf[4];
            21
            22    for (; c=*uri; ++uri) {
            23        if (HTTP_CHAR_IS_UNRESERVED(c)) {
            24            str.push_back(c);
            25        }
            else if(c == ' ' && space_as_plus) {
            26            str.push_back('+');
            27        }
            else{
            28            sprintf(buf,"%%%02X",(unsigned char)c);
            29            str.append(buf);
            30        }

            31    }

            32}


            解碼原理
               當解析URI的時候,首先要根據(jù)HTTP協(xié)議分離各個部件,再將各部件內(nèi)可能的轉(zhuǎn)義數(shù)據(jù)進行反轉(zhuǎn)義以還原。

            接口實現(xiàn)
               http_uri_decode有2種版本,一種提供存儲解碼后的uri參數(shù)str;另一種則不提供即在原uri上解碼,返回實際解碼后的字節(jié)數(shù)。每種版本又有2個版本,一個帶uri長度參數(shù)len,另一個則不帶。
             1void http_uri_decode(const char *uri, size_t len, std::string &str, int decode_plus /*= 0*/)
             2{
             3    char c,t[3]={'\0'};
             4
             5    for (size_t i = 0; i < len; i++{
             6        c = uri[i];
             7        if (c == '?'{
             8            if(decode_plus < 0)
             9                decode_plus = 1;
            10        }
             else if (c == '+' && decode_plus) {
            11            c = ' ';
            12        }
             else if (c == '%' && isxdigit(uri[i+1]) && isxdigit(uri[i+2])) {
            13            t[0= uri[++i],t[1= uri[++i];
            14            c = (char)strtol(t, NULL, 16);
            15        }

            16        str.push_back(c);
            17    }

            18}

            19
            20void http_uri_decode(const char *uri, std::string &str, int decode_plus/*=0*/)
            21{
            22    char c,t[3]={'\0'};
            23
            24    for (; c=*uri; ++uri) {
            25        if (c == '?'{
            26            if(decode_plus < 0)
            27                decode_plus = 1;
            28        }
             else if (c == '+' && decode_plus) {
            29            c = ' ';
            30        }
             else if (c == '%' && isxdigit(*(uri+1)) && isxdigit(*(uri+2))) {
            31            t[0= *++uri,t[1= *++uri;
            32            c = (char)strtol(t, NULL, 16);
            33        }

            34        str.push_back(c);
            35    }

            36}

            37
            38//in place decode function
            39size_t http_uri_decode(char *uri, size_t len, int decode_plus/*=0*/)
            40{
            41    char c,t[3]={'\0'};
            42    size_t i,j;
            43
            44    for (i=j=0; i < len; ++i,++j) {
            45        c = uri[i];    
            46        if (c == '?'{
            47            if(decode_plus < 0)
            48                decode_plus = 1;
            49        }
             else if (c == '+' && decode_plus) {
            50            c = ' ';
            51        }
             else if (c == '%' && isxdigit(uri[i+1]) && isxdigit(uri[i+2])) {
            52            t[0= uri[++i],t[1= uri[++i];
            53            c = (char)strtol(t, NULL, 16);
            54        }

            55        uri[j] = c;
            56    }

            57
            58    uri[j] = '\0';
            59    return j;
            60}

            61
            62size_t http_uri_decode(char *uri,int decode_plus/*=0*/)
            63{
            64    char c,*s=uri,*d=uri,t[3]={'\0'};
            65
            66    for (; c=*s; ++s,++d) {
            67        if (c == '?'{
            68            if(decode_plus < 0)
            69                decode_plus = 1;
            70        }
             else if (c == '+' && decode_plus) {
            71            c = ' ';
            72        }
             else if (c == '%' && isxdigit(*(s+1)) && isxdigit(*(s+2))) {
            73            t[0= *++s,t[1= *++s;
            74            c = (char)strtol(t, NULL, 16);
            75        }

            76        *= c;
            77    }

            78
            79    *d= '\0';
            80    return d-uri;
            81}
            posted on 2015-02-10 18:40 春秋十二月 閱讀(4748) 評論(1)  編輯 收藏 引用 所屬分類: Network

            評論:
            # re: HTTP URI編解碼 2015-03-12 09:08 | wxj
            好文,收藏  回復  更多評論
              
            久久777国产线看观看精品| .精品久久久麻豆国产精品| 国内精品久久久久久麻豆| 94久久国产乱子伦精品免费| 精品无码人妻久久久久久| 亚洲国产精品成人AV无码久久综合影院| 国产精品伊人久久伊人电影 | 亚洲国产精品成人久久| 国产成人精品久久免费动漫| 久久久久人妻一区精品| 久久国产亚洲高清观看| 日韩影院久久| 91久久精品国产91性色也| 久久精品国产亚洲AV忘忧草18| 久久精品一区二区| 久久久久亚洲av成人网人人软件 | 亚洲中文字幕无码久久2020| 国产香蕉97碰碰久久人人| 久久久久亚洲AV无码专区体验| 国产女人aaa级久久久级| 久久久久亚洲AV无码网站| 亚洲欧美一级久久精品| 国产精品久久久天天影视香蕉| 久久国产精品99国产精| 一本久久知道综合久久| 色偷偷88欧美精品久久久| 国产福利电影一区二区三区久久老子无码午夜伦不 | 欧美精品福利视频一区二区三区久久久精品 | 理论片午午伦夜理片久久 | 色综合久久久久久久久五月| 亚洲人成网站999久久久综合 | 国内精品久久久久| 久久精品国产亚洲AV无码麻豆| 婷婷综合久久中文字幕蜜桃三电影| 日本加勒比久久精品| 亚洲а∨天堂久久精品| 天天做夜夜做久久做狠狠| 久久久久久久国产免费看| 久久无码人妻精品一区二区三区 | 久久这里只有精品18| 久久亚洲欧美国产精品|