• <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類
                  該類代表實際中的一個url,成員變量主要有 ,char * file ,char * host , uint16_t port  , int8_t depth, char * cookie 
                  還有一個public 屬性的 in_addr 表示一個ipv4的地址。
                  成員函數中主要有一些,比如構造函數,返回url,添加cookie等操作。
              二 實例代碼如下
                  

            // 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中的實現類主要是,創建url,其中創建規則如下:
                 http://www.hach.com/r/0343/ttt.html
                則host為www.hach.com   , file 為/r/0343/ttt.html
               url的構造函數即根據 上述規則構建 url類。 若是含有base url 則新的url的file為 base->file + 新url 的file。
               (2)另外url的hash函數即是 利用了 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的處理函數如下
                     若addCookie(char * head) 中的head字符串是以 set-cookie: 開始的,則將head之后的12個字符
                     添加到cookie變量中。
               
             
            四綜上: 
              url 類中的成員變量,char * file ,char * host , port , cookie 能夠表示一個url。
              并且url類中提供了解析函數,使用戶可以根據從網頁中爬取的url構造url類對象。






             

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

            久久亚洲精品无码观看不卡| 狠狠狠色丁香婷婷综合久久五月| 亚洲国产成人久久精品动漫| 国产精品女同一区二区久久| 中文字幕无码久久久| 国产∨亚洲V天堂无码久久久| 国产精品成人精品久久久| 久久久久亚洲AV无码观看| 好久久免费视频高清| 欧美久久久久久精选9999| 色欲av伊人久久大香线蕉影院| A狠狠久久蜜臀婷色中文网| 久久久久九国产精品| 91久久精一区二区三区大全| 伊人久久大香线蕉精品不卡| 国产亚洲婷婷香蕉久久精品| 久久频这里精品99香蕉久| 久久91精品久久91综合| 精品多毛少妇人妻AV免费久久| 丰满少妇人妻久久久久久4| 久久久久久久亚洲Av无码| 久久久久婷婷| 国产香蕉97碰碰久久人人| 国产精品久久久久AV福利动漫 | 亚洲精品tv久久久久久久久久| 蜜臀av性久久久久蜜臀aⅴ麻豆| 色偷偷91久久综合噜噜噜噜| 女人香蕉久久**毛片精品| 久久精品国产第一区二区三区| 四虎国产精品成人免费久久| 久久久精品日本一区二区三区 | 久久线看观看精品香蕉国产| 无码精品久久久天天影视| 亚洲午夜久久久影院| 麻豆av久久av盛宴av| 国产精品久久久香蕉| 亚洲精品无码久久一线| 人妻精品久久无码专区精东影业| 天堂久久天堂AV色综合| 2021少妇久久久久久久久久| 国产精品9999久久久久|