• <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>

            jake1036

            larbin源代碼分析<三> url類分析

            larbin源代碼分析<三> url類分析

              一 分析utils包中的url類
                  該類代表實(shí)際中的一個(gè)url,成員變量主要有 ,char * file ,char * host , uint16_t port  , int8_t depth, char * cookie 
                  還有一個(gè)public 屬性的 in_addr 表示一個(gè)ipv4的地址。
                  成員函數(shù)中主要有一些,比如構(gòu)造函數(shù),返回url,添加cookie等操作。
              二 實(shí)例代碼如下
                  

            // Larbin
            // Sebastien Ailleret
            // 15-11-99 -> 14-03-02

            /* This class describes an URL */

            #ifndef URL_H
            #define URL_H

            #include 
            <netinet/in.h>
            #include 
            <sys/types.h>
            #include 
            <sys/socket.h>
            #include 
            <stdlib.h>

            #include 
            "types.h"

            bool fileNormalize (char *file);

            class url {
             
            private:
              
            char *host;
              
            char *file;
              uint16_t port; 
            // the order of variables is important for physical size
              int8_t depth;
              
            /* parse the url */
              
            void parse (char *s);
              
            /** parse a file with base */
              
            void parseWithBase (char *u, url *base);
              
            /* normalize file name */
              
            bool normalize (char *file);
              
            /* Does this url starts with a protocol name */
              
            bool isProtocol (char *s);
              
            /* constructor used by giveBase */
              url (
            char *host, uint port, char *file);

             
            public:
              
            /* Constructor : Parses an url (u is deleted) */
              url (
            char *u, int8_t depth, url *base);

              
            /* constructor used by input */
              url (
            char *line, int8_t depth);

              
            /* Constructor : read the url from a file (cf serialize) */
              url (
            char *line);

              
            /* Destructor */
              
            ~url ();

              
            /* inet addr (once calculated) */
              
            struct in_addr addr;

              
            /* Is it a valid url ? */
              
            bool isValid ();

              
            /* print an URL */
              
            void print ();

              
            /* return the host */
              inline 
            char *getHost () return host; }

              
            /* return the port */
              inline 
            uint getPort () return port; }

              
            /* return the file */
              inline 
            char *getFile () return file; }

              
            /** Depth in the Site */
              inline int8_t getDepth () 
            return depth; }

              
            /* Set depth to max if we are at an entry point in the site
               * try to find the ip addr
               * answer false if forbidden by robots.txt, true otherwise 
            */

              
            bool initOK (url *from);

              
            /** return the base of the url
               * give means that you have to delete the string yourself
               
            */

              url 
            *giveBase ();

              
            /** return a char * representation of the url
               * give means that you have to delete the string yourself
               
            */

              
            char *giveUrl ();

              
            /** write the url in a buffer
               * buf must be at least of size maxUrlSize
               * returns the size of what has been written (not including '\0')
               
            */

              
            int writeUrl (char *buf);

              
            /* serialize the url for the Persistent Fifo */
              
            char *serialize ();

              
            /* very thread unsafe serialisation in a static buffer */
              
            char *getUrl();

              
            /* return a hashcode for the host of this url */
              
            uint hostHashCode ();

              
            /* return a hashcode for this url */
              
            uint hashCode ();

            #ifdef URL_TAGS
              
            /* tag associated to this url */
              
            uint tag;
            #endif // URL_TAGS

            #ifdef COOKIES
              
            /* cookies associated with this page */
              
            char *cookie;
              
            void addCookie(char *header);
            #else // COOKIES
              inline 
            void addCookie(char *header) {}
            #endif // COOKIES
            }
            ;

            #endif // URL_H

             
            三 代碼分析
                 url中的實(shí)現(xiàn)類主要是,創(chuàng)建url,其中創(chuàng)建規(guī)則如下:
                 http://www.hach.com/r/0343/ttt.html
                則host為www.hach.com   , file 為/r/0343/ttt.html
               url的構(gòu)造函數(shù)即根據(jù) 上述規(guī)則構(gòu)建 url類。 若是含有base url 則新的url的file為 base->file + 新url 的file。
               (2)另外url的hash函數(shù)即是 利用了 file 字符串和 host字符串。
                       /* return a hashcode for this url */
                   uint url::hashCode () {
                     unsigned int h=port;
                     unsigned int i=0;
                     while (host[i] != 0) {
                         h = 31*h + host[i];
                         i++;
                      }
                       i=0;
                    while (file[i] != 0) {
                         h = 31*h + file[i];
                         i++;
                     }
                   return h % hashSize;
                   }
               (3) cookie的處理函數(shù)如下
                     若addCookie(char * head) 中的head字符串是以 set-cookie: 開始的,則將head之后的12個(gè)字符
                     添加到cookie變量中。
               
             
            四綜上: 
              url 類中的成員變量,char * file ,char * host , port , cookie 能夠表示一個(gè)url。
              并且url類中提供了解析函數(shù),使用戶可以根據(jù)從網(wǎng)頁(yè)中爬取的url構(gòu)造url類對(duì)象。






             

            posted on 2011-06-11 21:42 kahn 閱讀(881) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            无码任你躁久久久久久| 欧美丰满熟妇BBB久久久| 国产精品免费久久久久久久久| 久久青草国产精品一区| 久久久亚洲精品蜜桃臀| 三上悠亚久久精品| 看全色黄大色大片免费久久久| 伊人久久精品无码二区麻豆| 97精品伊人久久久大香线蕉| 亚洲色大成网站WWW久久九九| 久久免费小视频| 久久精品国产精品亚洲毛片| 蜜臀久久99精品久久久久久| 久久97精品久久久久久久不卡| 免费精品久久天干天干| 999久久久国产精品| 久久久久人妻一区精品色| 尹人香蕉久久99天天拍| 国产精品免费久久| 国产日韩久久久精品影院首页| 久久亚洲精品无码AV红樱桃| 波多野结衣久久精品| 久久99久久成人免费播放| 久久精品国产91久久综合麻豆自制| 日韩av无码久久精品免费| 狠狠色婷婷久久综合频道日韩| 四虎国产精品成人免费久久| 亚洲成色999久久网站| 久久久九九有精品国产| 97久久久久人妻精品专区| 久久精品国产99久久无毒不卡| 午夜天堂av天堂久久久| 久久久久国色AV免费看图片| yellow中文字幕久久网| 久久精品国产亚洲一区二区| 精品国产VA久久久久久久冰| 国产精品无码久久综合| 久久国产精品-国产精品| 久久99国产精品99久久| 国产精品内射久久久久欢欢| 久久久久国产精品嫩草影院|