home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 71 / CDROM71.ISO / internet / navoff / data1.cab / Sources / src / htsparse.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-01  |  89.3 KB  |  1,917 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche, Yann Philippot and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. This project has been developed by Xavier Roche and Yann Philippot,
  29. from the company Serianet at Caen, France (http://www.serianet.com)
  30. and other contributors (see the greetings file)
  31.  
  32. Please visit our Website: http://www.httrack.com
  33. */
  34.  
  35.  
  36. /* ------------------------------------------------------------ */
  37. /* File: Main source                                            */
  38. /* DIRECT INCLUDE TO httrack.c                                  */
  39. /* Author: Xavier Roche                                         */
  40. /* ------------------------------------------------------------ */
  41.  
  42.  
  43. #if HTS_ANALYSTE
  44. if (hts_htmlcheck(r.adr,(int)r.size,urladr,urlfil)) {
  45. #endif          
  46.   FILE* fp=NULL;      // fichier Θcrit localement                                               // et si level>0
  47.   char* adr=r.adr;    // pointeur (on parcourt)
  48.   char* lastsaved;    // adresse du dernier octet sauvΘ + 1
  49.   if ( (opt.debug>1) && (opt.log!=NULL) ) {
  50.     fspc(opt.log,"debug"); fprintf(opt.log,"scan file.."LF); test_flush;
  51.   }
  52.  
  53.  
  54.   // Indexing!
  55. #if HTS_MAKE_KEYWORD_INDEX
  56.   if (opt.kindex) {
  57.     if (index_keyword(r.adr,r.size,r.contenttype,savename,opt.path_html)) {
  58.       if ( (opt.debug>1) && (opt.log!=NULL) ) {
  59.         fspc(opt.log,"debug"); fprintf(opt.log,"indexing file..done"LF); test_flush;
  60.       }
  61.     } else {
  62.       if ( (opt.debug>1) && (opt.log!=NULL) ) {
  63.         fspc(opt.log,"debug"); fprintf(opt.log,"indexing file..error!"LF); test_flush;
  64.       }
  65.     }
  66.   }
  67. #endif
  68.  
  69.   // Now, parsing
  70.   if ((opt.getmode & 1) && (ptr>0)) {  // rΘcupΘrer les html sur disque       
  71.     // crΘer le fichier html local
  72.     HT_ADD_FOP;   // Θcrire peu α peu le fichier
  73.   }
  74.   
  75.   if (!error) {
  76.     int detect_title=0;  // dΘtection  du title
  77.     //
  78.     int intag=0;         // on est dans un tag
  79.     int incomment=0;     // dans un <!--
  80.     int inscript=0;      // dans un scipt pour applets javascript)
  81.     int inscript_tag=0;  // on est dans un <body onLoad="... terminΘ par >
  82.     char inscript_tag_lastc='\0';  
  83.                            // terminaison (" ou ') du "<body onLoad=.."
  84.     int inscriptgen=0;     // on est dans un code gΘnΘrant, ex aprΦs obj.write("..
  85.     char scriptgen_q='\0'; // caractΦre faisant office de guillemet (' ou ")
  86.     int nofollow=0;        // ne pas scanner
  87.     //
  88.     int parseall_lastc='\0';    // dernier caractΦre parsΘ pour parseall
  89.     int parseall_incomment=0;   // dans un /* */ (exemple: a = /* URL */ "img.gif";)
  90.     //
  91.     char* intag_start=adr;
  92.     int intag_start_valid=0;
  93.     HT_ADD_START;    // dΘbuter
  94.  
  95.  
  96.     /* statistics */
  97.     if ((opt.getmode & 1) && (ptr>0)) { 
  98.       HTS_STAT.stat_files++;
  99.       HTS_STAT.stat_bytes+=r.size;
  100.     }
  101.  
  102.  
  103.     /* Check is the file is a .js file */
  104.     if (strfield2(r.contenttype,"application/x-javascript")!=0) {      /* JavaScript js file */
  105.       inscript=1;
  106.       intag=1;     // because aprΦs <script> on y est .. - pas utile
  107.       intag_start_valid=0;    // OUI car nous sommes dans du code, plus dans du "vrai" tag
  108.       if ((opt.debug>1) && (opt.log!=NULL)) {
  109.         fspc(opt.log,"debug"); fprintf(opt.log,"note: this file is a javascript file"LF); test_flush;
  110.       }
  111.     }
  112.     
  113.     
  114.  
  115.     // ------------------------------------------------------------
  116.     // analyser ce qu'il y a en mΘmoire (fichier html)
  117.     // on scanne les balises
  118.     // ------------------------------------------------------------
  119. #if HTS_ANALYSTE==2
  120.     _hts_in_html_done=0;     // 0% scannΘs
  121.     _hts_cancel=0;           // pas de cancel
  122.     _hts_in_html_parsing=1;  // flag pour indiquer un parsing
  123. #endif
  124.     base[0]='\0';    // effacer base-href
  125.     lastsaved=adr;
  126.     do {
  127.       int p=0;
  128.       error=0;
  129.  
  130.       /* Hack to avoid NULL char problems with C syntax */
  131.       /* Yes, some bogus HTML pages can embed null chars
  132.          and therefore can not be properly handled if this hack is not done
  133.       */
  134.       if ( ! (*adr) ) {
  135.         if (( ((int) adr) - ((int) r.adr) ) < r.size)
  136.           *adr=' ';
  137.       }
  138.  
  139.  
  140.       /* Parse */
  141.       if (
  142.            (*adr=='<')    /* No starting tag */
  143.         && (!inscript)    /* Not in (java)script */
  144.         && (!incomment)   /* Not in comment (<!--) */
  145.       ) { 
  146.         intag=1;
  147.         parseall_incomment=0;
  148.         //inquote=0;  // effacer quote
  149.         intag_start=adr; intag_start_valid=1;
  150.         codebase[0]='\0';    // effacer Θventuel codebase
  151.         
  152.         if (opt.getmode & 1) {  // sauver html
  153.           p=strfield(adr,"</html");
  154.           if (p==0) p=strfield(adr,"<head>");
  155.           if (p) {
  156.             if (strnotempty(opt.footer)) {
  157.               char tempo[1024+HTS_URLMAXSIZE*2];
  158.               char gmttime[256];
  159.               tempo[0]='\0';
  160.               time_gmt_rfc822(gmttime);
  161.               strcpy(tempo,CRLF);
  162.               sprintf(tempo+strlen(tempo),opt.footer,jump_identification(urladr),urlfil,gmttime,"","","","","");
  163.               strcat(tempo,CRLF);
  164.               //fwrite(tempo,1,strlen(tempo),fp);
  165.               HT_ADD(tempo);
  166.             }
  167.           }
  168.         }        
  169.         
  170.         // Θliminer les <!-- (commentaires) : intag dΘvalidΘ
  171.         if (*(adr+1)=='!')
  172.           if (*(adr+2)=='-')
  173.             if (*(adr+3)=='-') {
  174.               intag=0;
  175.               incomment=1;
  176.               intag_start_valid=0;
  177.             }
  178.             
  179.       }
  180.       else if (
  181.            (*adr=='>')                        /* ending tag */
  182.         && ( (!inscript) || (inscript_tag) )  /* and in tag (or in script) */
  183.       ) {
  184.         if (inscript_tag) {
  185.           inscript_tag=inscript=0;
  186.           intag=0;
  187.           incomment=0;
  188.           intag_start_valid=0;
  189.         } else if (!incomment) {
  190.           intag=0; //inquote=0;
  191.           
  192.           // entrΘe dans du javascript?
  193.           // on parse ICI car il se peut qu'on ait eu a parser les src=.. dedans
  194.           //if (!inscript) {  // sinon on est dans un obj.write("..
  195.           if ((intag_start_valid) && check_tag(intag_start,"script")) {
  196.             char* a=intag_start;    // <
  197.             // ** while(is_realspace(*(--a)));
  198.             if (*a=='<') {  // s√r que c'est un tag?
  199.               inscript=1;
  200.               intag=1;     // because aprΦs <script> on y est .. - pas utile
  201.               intag_start_valid=0;    // OUI car nous sommes dans du code, plus dans du "vrai" tag
  202.             }
  203.           }
  204.         } else {                               /* end of comment? */
  205.           // vΘrifier fermeture correcte
  206.           if ( (*(adr-1)=='-') && (*(adr-2)=='-') ) {
  207.             intag=0;
  208.             incomment=0;
  209.             intag_start_valid=0;
  210.           }
  211. #if GT_ENDS_COMMENT
  212.           /* wrong comment ending */
  213.           else {
  214.             /* check if correct ending do not exist 
  215.                <!-- foo > example <!-- bar > is sometimes accepted by browsers
  216.                when no --> is used somewhere else.. darn those browsers are dirty
  217.             */
  218.             if (!strstr(adr,"-->")) {
  219.               intag=0;
  220.               incomment=0;
  221.               intag_start_valid=0;
  222.             }
  223.           }
  224. #endif
  225.         }
  226.         //}
  227.       }
  228.       //else if (*adr==34) {
  229.       //  inquote=(inquote?0:1);
  230.       //}
  231.       else if (intag || inscript) {    // nous sommes dans un tag/commentaire, tester si on recoit un tag
  232.         int p_type=0;
  233.         int p_nocatch=0;
  234.         int p_searchMETAURL=0;  // chercher ..URL=<url>
  235.         int add_class=0;        // ajouter .class
  236.         char* p_flush=NULL;
  237.         
  238.         
  239.         // ------------------------------------------------------------
  240.         // parsing ΘvolΘ
  241.         // ------------------------------------------------------------
  242.         if (((isalpha((unsigned char)*adr)) || (*adr=='/') || (inscript) || (inscriptgen))) {  // sinon pas la peine de tester..
  243.  
  244.  
  245.           /* caractΦre de terminaison pour "miniparsing" javascript=.. ? 
  246.              (ex: <a href="javascript:()" action="foo"> ) */
  247.           if (inscript_tag) {
  248.             if (inscript_tag_lastc) {
  249.               if (*adr == inscript_tag_lastc) {
  250.                 /* sortir */
  251.                 inscript_tag=inscript=0;
  252.                 incomment=0;
  253.               }
  254.             }
  255.           }
  256.  
  257.           // Construction index.html (sommaire)
  258.           // Avant de tester les a href,
  259.           // Ici on teste si l'on doit construire l'index vers le(s) site(s) miroir(s)
  260.           if (opt.makeindex && (ptr>0)) {
  261.             if (opt.getmode & 1) {  // autorisation d'Θcrire
  262.               if (!makeindex_done) {  // autoriation d'Θcrire un index
  263.                 if (opt.depth == liens[ptr]->depth) {    // on note toujours les premiers liens
  264.                   if (!detect_title) {
  265.                     p=strfield(adr,"title");  
  266.                     if (p) {
  267.                       if (*(adr-1)=='/') p=0;    // /title
  268.                     } else {
  269.                       if (strfield(adr,"/html"))
  270.                         p=-1;                    // noter, mais sans titre
  271.                       else if (strfield(adr,"body"))
  272.                         p=-1;                    // noter, mais sans titre
  273.                     }
  274.                   } else
  275.                     p=0;
  276.                   
  277.                   if (p) {    // ok center                            
  278.                     if (makeindex_fp==NULL) {
  279.                       verif_backblue(opt.path_html);    // gΘnΘrer gif
  280.                       makeindex_fp=filecreate(fconcat(opt.path_html,"index.html"));
  281.                       if (makeindex_fp!=NULL) {
  282.                         fprintf(makeindex_fp,"<HTML>"CRLF);
  283.                         fprintf(makeindex_fp,"<!-- Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"CRLF);
  284.                         fprintf(makeindex_fp,"<HEAD>"CRLF"<TITLE>");
  285.                         fprintf(makeindex_fp,"Local index");
  286.                         fprintf(makeindex_fp,"</TITLE>"CRLF"</HEAD>"CRLF"<BODY BACKGROUND=\"backblue.gif\"><H1 ALIGN=Center>");
  287.                         fprintf(makeindex_fp,"<U>Index of locally available sites:</U>"CRLF"</H1>"CRLF"<BR><BR>"CRLF);
  288.                         fprintf(makeindex_fp,"<TABLE BORDER=\"0\" WIDTH=\"100%%\" CELLSPACING=\"1\" CELLPADDING=\"0\">"CRLF);
  289.                         fprintf(makeindex_fp,"<UL>"CRLF);
  290.                       } else makeindex_done=-1;    // fait, erreur
  291.                     }
  292.                     
  293.                     if (makeindex_fp!=NULL) {
  294.                       char tempo[HTS_URLMAXSIZE*2];
  295.                       char s[HTS_URLMAXSIZE*2];
  296.                       char* a=NULL;
  297.                       char* b=NULL;
  298.                       s[0]='\0';
  299.                       if (p>0) {
  300.                         a=strchr(adr,'>');
  301.                         if (a!=NULL) {
  302.                           a++;
  303.                           while(is_space(*a)) a++;    // sauter espaces & co
  304.                           b=strchr(a,'<');   // prochain tag
  305.                         }
  306.                       }
  307.                       if (lienrelatif(tempo,liens[ptr]->sav,concat(opt.path_html,"index.html"))==0) {
  308.                         detect_title=1;      // ok dΘtectΘ pour cette page!
  309.                         makeindex_links++;   // un de plus
  310.                         strcpy(makeindex_firstlink,tempo);
  311.                         //
  312.                         fprintf(makeindex_fp,"<TR>"CRLF"<TD BACKGROUND=\"fade.gif\">"CRLF);
  313.                         //fprintf(makeindex_fp,"<UL>"CRLF);
  314.                         fprintf(makeindex_fp,"<LI>"CRLF);
  315.                         if ((b==a) || (a==NULL) || (b==NULL)) {    // pas de titre
  316.                           fprintf(makeindex_fp,"<A HREF=\"%s\">%s</A><BR>"CRLF,tempo,tempo);
  317.                         } else if ((b-a)<256) {
  318.                           b--;
  319.                           while(is_space(*b)) b--;
  320.                           strncpy(s,a,b-a+1);
  321.                           *(s+(b-a)+1)='\0';
  322.                           fprintf(makeindex_fp,"<A HREF=\"%s\">%s</A><BR>"CRLF,tempo,s);
  323.                         }
  324.                         fprintf(makeindex_fp,"</LI>"CRLF);
  325.                         //fprintf(makeindex_fp,"</UL>"CRLF);
  326.                         fprintf(makeindex_fp,"</TD>"CRLF"</TR>"CRLF);
  327.                       }
  328.                     }
  329.                   }
  330.                   
  331.                 } else if (liens[ptr]->depth<opt.depth) {   // on a sautΘ level1+1 et level1
  332.                   if (makeindex_fp) {
  333.                     fprintf(makeindex_fp,"</UL>"CRLF);
  334.                     fprintf(makeindex_fp,"</TABLE>"CRLF);
  335.                     fprintf(makeindex_fp,"<BR>"CRLF"<BR>"CRLF"<BR>"CRLF"</BODY>"CRLF);
  336.                     fprintf(makeindex_fp,"<I><H6 ALIGN=\"RIGHT\">Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS"</H6></I>"CRLF);
  337.                     fprintf(makeindex_fp,"<!-- Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"CRLF);
  338.                     fprintf(makeindex_fp,"<!-- Thanks for using HTTrack Website Copier! -->"CRLF);
  339.                     if (makeindex_links == 1) {
  340.                       fprintf(makeindex_fp,"<meta HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=%s\">"CRLF,makeindex_firstlink);
  341.                     }
  342.                     fprintf(makeindex_fp,"</HTML>"CRLF);
  343.                     fflush(makeindex_fp);
  344.                     fclose(makeindex_fp);  // α ne pas oublier sinon on passe une nuit blanche
  345.                     makeindex_fp=NULL;
  346.                     usercommand(0,NULL,fconcat(opt.path_html,"index.html"));                            
  347.                   }
  348.                   makeindex_done=1;    // ok c'est fait
  349.                 }
  350.               }
  351.             }
  352.           } // if (opt.makeindex)
  353.           // FIN Construction index.html (sommaire)
  354.  
  355.           
  356.           // Note:
  357.           // Certaines pages ne respectent pas le html
  358.           // notamment les guillements ne sont pas fixΘs
  359.           // Nous sommes dans un tag, donc on peut faire un test plus
  360.           // large pour pouvoi prendre en compte ces particularitΘs
  361.           
  362.           // α vΘrifier: ACTION, CODEBASE, VRML
  363.           
  364.           if (ptr>0) {        /* pas premiΦre page 0 (primary) */
  365.             p=0;  // saut pour le nom de fichier: adresse nom fichier=adr+p
  366.             
  367.             // ------------------------------
  368.             // dΘtection d'Θcriture JavaScript.
  369.             // osons les obj.write et les obj.href=.. ! osons!
  370.             // note: inscript==1 donc on sautera aprΦs les \"
  371.             if (inscript) {
  372.               if (inscriptgen) {          // on est dΘja dans un objet gΘnΘrant..
  373.                 if (*adr==scriptgen_q) {  // fermeture des " ou '
  374.                   if (*(adr-1)!='\\') {   // non
  375.                     inscriptgen=0;        // ok parsing terminΘ
  376.                   }
  377.                 }
  378.               } else {
  379.                 char* a=NULL;
  380.                 char check_this_fking_line=0;  // parsing code javascript..
  381.                 char must_be_terminated=0;     // caractΦre obligatoire de terminaison!
  382.                 int token_size;
  383.                 if (!(token_size=strfield(adr,".writeln"))) // dΘtection ...objet.write[ln]("code html")...
  384.                   token_size=strfield(adr,".write");
  385.                 if (token_size) {
  386.                   a=adr+token_size;
  387.                   while(is_realspace(*a)) a++; // sauter espaces
  388.                   if (*a=='(') {  // dΘbut parenthΦse
  389.                     check_this_fking_line=2;  // α parser!
  390.                     must_be_terminated=')';
  391.                     a++;  // sauter (
  392.                   }
  393.                 }
  394.                 // euhh ??? ???
  395.                 /* else if (strfield(adr,".href")) {  // dΘtection ...objet.href="...
  396.                 a=adr+5;
  397.                 while(is_realspace(*a)) a++; // sauter espaces
  398.                 if (*a=='=') {  // ohh un Θgal
  399.                 check_this_fking_line=1;  // α noter!
  400.                 must_be_terminated=';';   // et si t'as oubliΘ le ; tu sais pas coder
  401.                 a++;   // sauter =
  402.                 }
  403.                 
  404.               }*/
  405.                 
  406.                 // on a un truc du genre instruction"code gΘnΘrΘ" dont on parse le code
  407.                 if (check_this_fking_line) {
  408.                   while(is_realspace(*a)) a++;
  409.                   if ((*a=='\'') || (*a=='"')) {  // dΘpart de '' ou ""
  410.                     char *b;
  411.                     int ex=0;
  412.                     scriptgen_q=*a;    // quote
  413.                     b=a+1;      // dΘpart de la chaεne
  414.                     // vΘrifier forme ("code") et pas ("code"+var), ingΘrable
  415.                     do {
  416.                       a++;  // caractΦre suivant
  417.                       if (*a==scriptgen_q) if (*(a-1)!='\\')  // quote non slash
  418.                         ex=1;            // sortie
  419.                       if ((*a==10) || (*a==13))
  420.                         ex=1;
  421.                     } while(!ex);
  422.                     if (*a==scriptgen_q) {  // fin du quote
  423.                       a++;
  424.                       while(is_realspace(*a)) a++;
  425.                       if (*a==must_be_terminated) {  // parenthΦse fermante: ("..")
  426.                         
  427.                         // bon, on doit parser une ligne javascript
  428.                         // 1) si check.. ==1 alors c'est un nom de fichier direct, donc
  429.                         // on fixe p sur le saut nΘcessaire pour atteindre le nom du fichier
  430.                         // et le moteur se dΘbrouillera ensuite tout seul comme un grand
  431.                         // 2) si check==2 c'est un peu plus tordu car lα on gΘnΘre du
  432.                         // code html au sein de code javascript au sein de code html
  433.                         // dans ce cas on doit fixer un flag α un puis ensuite dans la boucle
  434.                         // on devra parser les instructions standard comme <a href etc
  435.                         // NOTE: le code javascript autogΘnΘrΘ n'est pas pris en compte!!
  436.                         // (et ne marche pas dans 50% des cas de toute facon!)
  437.                         if (check_this_fking_line==1) {
  438.                           p=(int) b-(int) adr;  // calculer saut!
  439.                         } else {
  440.                           inscriptgen=1;        // SCRIPTGEN actif
  441.                           adr=b;                // jump
  442.                         }
  443.                         
  444.                         if ((opt.debug>1) && (opt.log!=NULL)) {
  445.                           char str[512];
  446.                           str[0]='\0';
  447.                           strncat(str,b,minimum((int) a-(int) b+1,32));
  448.                           fspc(opt.log,"debug"); fprintf(opt.log,"active code (%s) detected in javascript: %s"LF,(check_this_fking_line==2)?"parse":"pickup",str); test_flush;
  449.                         }
  450.                       }
  451.                       
  452.                     }
  453.                     
  454.                   }
  455.                   
  456.                   
  457.                 }
  458.               }
  459.             }
  460.             // fin detection code gΘnΘrant javascript vers html
  461.             // ------------------------------
  462.             
  463.             
  464.             // analyse proprement dite, A HREF=.. etc..
  465.             if (!p) {
  466.               // si dans un tag, et pas dans un script - sauf si on analyse un obj.write("..
  467.               if ((intag && (!inscript)) || inscriptgen) {
  468.                 if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) {   // <tag < tag etc
  469.                   // <A HREF=.. pour les liens HTML
  470.                   p=rech_tageq(adr,"href");
  471.                   if (p) {    // href.. tester si c'est une bas href!
  472.                     if ((intag_start_valid) && check_tag(intag_start,"base")) {  // oui!
  473.                       // ** note: base href et codebase ne font pas bon mΘnage..
  474.                       p_type=2;    // c'est un chemin
  475.                     }
  476.                   }
  477.                   
  478.                   /* Tags supplΘmentaires α vΘrifier (<img src=..> etc) */
  479.                   if (p==0) {
  480.                     int i=0;
  481.                     while( (p==0) && (strnotempty(hts_detect[i])) ) {
  482.                       p=rech_tageq(adr,hts_detect[i]);
  483.                       i++;
  484.                     }
  485.                   }
  486.  
  487.                   /* Tags supplΘmentaires α vΘrifier : URL=.. */
  488.                   if (p==0) {
  489.                     int i=0;
  490.                     while( (p==0) && (strnotempty(hts_detectURL[i])) ) {
  491.                       p=rech_tageq(adr,hts_detectURL[i]);
  492.                       i++;
  493.                     }
  494.                     if (p)
  495.                       p_searchMETAURL=1;
  496.                   }
  497.  
  498.                   /* Tags supplΘmentaires α vΘrifier, mais α ne pas capturer */
  499.                   if (p==0) {
  500.                     int i=0;
  501.                     while( (p==0) && (strnotempty(hts_detectandleave[i])) ) {
  502.                       p=rech_tageq(adr,hts_detectandleave[i]);
  503.                       i++;
  504.                     }
  505.                     if (p)
  506.                       p_nocatch=1;      /* ne pas rechercher */
  507.                   }
  508.  
  509.                   /* EvΘnements */
  510.                   if (p==0) {
  511.                     int i=0;
  512.                     /* dΘtection onLoad etc */
  513.                     while( (p==0) && (strnotempty(hts_detect_js[i])) ) {
  514.                       p=rech_tageq(adr,hts_detect_js[i]);
  515.                       i++;
  516.                     }
  517.                     /* non dΘtectΘ - dΘtecter Θgalement les onXxxxx= */
  518.                     if (p==0) {
  519.                       if ( (*adr=='o') && (*(adr+1)=='n') && isUpperLetter(*(adr+2)) ) {
  520.                         p=0;
  521.                         while(isalpha((unsigned char)adr[p]) && (p<64) ) p++;
  522.                         if (p<64) {
  523.                           while(is_space(adr[p])) p++;
  524.                           if (adr[p]=='=')
  525.                             p++;
  526.                           else p=0;
  527.                         } else p=0;
  528.                       }
  529.                     }
  530.                     /* OK, ΘvΘnement repΘrΘ */
  531.                     if (p) {
  532.                       inscript_tag_lastc=*(adr+p-1);     /* α attendre α la fin */
  533.                       adr+=p;     /* saut */
  534.                                   /*
  535.                                   On est dΘsormais dans du code javascript
  536.                       */
  537.                       inscript_tag=inscript=1;
  538.                     }
  539.                     p=0;        /* quoi qu'il arrive, ne rien dΘmarrer ici */
  540.                   }
  541.  
  542.                   // <APPLET CODE=.. pour les applet java.. [CODEBASE (chemin..) α faire]
  543.                   if (p==0) {
  544.                     p=rech_tageq(adr,"code");
  545.                     if (p) {
  546.                       if ((intag_start_valid) && check_tag(intag_start,"applet")) {  // dans un <applet !
  547.                         p_type=-1;  // juste le nom de fichier+dossier, Θcire avant codebase 
  548.                         add_class=1;   // ajouter .class au besoin                         
  549.                         
  550.                         // vΘrifier qu'il n'y a pas de codebase APRES
  551.                         // sinon on swappe les deux.
  552.                         // pas trΦs propre mais c'est ce qu'il y a de plus simple α faire!!
  553.                         
  554.                         {
  555.                           char *a;
  556.                           a=adr;
  557.                           while((*a) && (*a!='>') && (!rech_tageq(a,"codebase"))) a++;
  558.                           if (rech_tageq(a,"codebase")) {  // banzai! codebase=
  559.                             char* b;
  560.                             b=strchr(a,'>');
  561.                             if (b) {
  562.                               if (((int) b - (int) adr) < 1000) {    // au total < 1Ko
  563.                                 char tempo[HTS_URLMAXSIZE*2];
  564.                                 tempo[0]='\0';
  565.                                 strncat(tempo,a,(int) b - (int) a);
  566.                                 strcat( tempo," ");
  567.                                 strncat(tempo,adr,(int) a - (int) adr - 1);
  568.                                 // Θventuellement remplire par des espaces pour avoir juste la taille
  569.                                 while((int) strlen(tempo)<((int) b - (int) adr))
  570.                                   strcat(tempo," ");
  571.                                 // pas d'erreur?
  572.                                 if ((int) strlen(tempo) == ((int) b - (int) adr)) {
  573.                                   strncpy(adr,tempo,strlen(tempo));   // PAS d'octet nul α la fin!
  574.                                   p=0;    // DEVALIDER!!
  575.                                   p_type=0;
  576.                                   add_class=0;
  577.                                 }
  578.                               }
  579.                             }
  580.                           }
  581.                         }
  582.                         
  583.                       }
  584.                     }
  585.                   }
  586.                   
  587.                   // liens α patcher mais pas α charger (ex: codebase)
  588.                   if (p==0) {  // note: si non chargΘ (ex: ignorer .class) patchΘ tout de mΩme
  589.                     p=rech_tageq(adr,"codebase");
  590.                     if (p) {
  591.                       if ((intag_start_valid) && check_tag(intag_start,"applet")) {  // dans un <applet !
  592.                         p_type=-2;
  593.                       } else p=-1;   // ne plus chercher
  594.                     }
  595.                   }
  596.                   
  597.                   
  598.                   // Meta tags pour robots
  599.                   if (p==0) {
  600.                     if (opt.robots) {
  601.                       if ((intag_start_valid) && check_tag(intag_start,"meta")) {
  602.                         if (rech_tageq(adr,"name")) {    // name=robots.txt
  603.                           char tempo[1100];
  604.                           char* a;
  605.                           tempo[0]='\0';
  606.                           a=strchr(adr,'>');
  607. #if DEBUG_ROBOTS
  608.                           printf("robots.txt meta tag detected\n");
  609. #endif
  610.                           if (a) {
  611.                             if (((int) a - (int) adr) < 999 ) {
  612.                               strncat(tempo,adr,(int) a - (int) adr);
  613.                               if (strstrcase(tempo,"content")) {
  614.                                 if (strstrcase(tempo,"robots")) {
  615.                                   if (strstrcase(tempo,"nofollow")) {
  616. #if DEBUG_ROBOTS
  617.                                     printf("robots.txt meta tag: nofollow in %s%s\n",urladr,urlfil);
  618. #endif
  619.                                     nofollow=1;       // NE PLUS suivre liens dans cette page
  620.                                     if (opt.errlog) {
  621.                                       fspc(opt.errlog,"warning"); fprintf(opt.errlog,"Link %s%s not scanned (follow robots meta tag)"LF,urladr,urlfil);
  622.                                       test_flush;
  623.                                     }
  624.                                   }
  625.                                 }
  626.                               }
  627.                             }
  628.                           }
  629.                         }
  630.                       }
  631.                     }
  632.                   }
  633.                   
  634.                   // entrΘe dans une applet javascript
  635.                   /*if (!inscript) {  // sinon on est dans un obj.write("..
  636.                   if (p==0)
  637.                   if (rech_sampletag(adr,"script"))
  638.                   if (check_tag(intag_start,"script")) {
  639.                   inscript=1;
  640.                   }
  641.                         }*/
  642.                   
  643.                   // Ici on procΦde α une analyse du code javascript pour tenter de rΘcupΘrer
  644.                   // certains fichiers Θvidents.
  645.                   // C'est devenu obligatoire vu le nombre de pages qui intΦgrent
  646.                   // des images rΘactives par exemple
  647.                 }
  648.               } else if (inscript) {
  649.                 if (strfield(adr,"/script") ) {
  650.                   char* a=adr;
  651.                   //while(is_realspace(*(--a)));
  652.                   while( is_realspace(*a) ) a--;
  653.                   a--;
  654.                   if (*a=='<') {  // s√r que c'est un tag?
  655.                     inscript=0;
  656.                   }
  657.                 } else {
  658.                   int nc;
  659.                   char  expected     = '=';          // caractΦre attendu aprΦs
  660.                   char* expected_end = ";";
  661.                   if (inscript_tag)
  662.                     expected_end=";\"\'";            // voir a href="javascript:doc.location='foo'"
  663.                   nc = strfield(adr,".src");  // nom.src="image";
  664.                   if (!nc) nc = strfield(adr,".location");  // document.location="doc"
  665.                   if (!nc) nc = strfield(adr,".href");  // document.location="doc"
  666.                   if (!nc) if ( (nc = strfield(adr,".open")) ) { // window.open("doc",..
  667.                     expected='(';    // parenthΦse
  668.                     expected_end="),";  // fin: virgule ou parenthΦse
  669.                   }
  670.                   if (!nc) if ( (nc = strfield(adr,".replace")) ) { // window.replace("url")
  671.                     expected='(';    // parenthΦse
  672.                     expected_end=")";  // fin: parenthΦse
  673.                   }
  674.                   if (!nc) if ( (nc = strfield(adr,".link")) ) { // window.link("url")
  675.                     expected='(';    // parenthΦse
  676.                     expected_end=")";  // fin: parenthΦse
  677.                   }
  678.                   if (nc) {
  679.                     char *a;
  680.                     a=adr+nc;
  681.                     while(is_space(*a)) a++;
  682.                     if (*a == expected) {
  683.                       a++;
  684.                       while(is_realspace(*a)) a++;
  685.                       if ((*a==34) || (*a=='\'')) {
  686.                         char *b,*c;
  687.                         a++;
  688.                         b=a;
  689.                         while((*b!=34) && (*b!='\'') && (*b!='\0')) b++;
  690.                         c=b--; c++;
  691.                         while(*c==' ') c++;
  692.                         if ((strchr(expected_end,*c)) || (*c=='\n') || (*c=='\r')) {
  693.                           c-=2;
  694.                           if ((int) c-(int) a+1) {
  695.                             if ((opt.debug>1) && (opt.log!=NULL)) {
  696.                               char str[512];
  697.                               str[0]='\0';
  698.                               strncat(str,a,minimum((int) c-(int) a+1,32));
  699.                               fspc(opt.log,"debug"); fprintf(opt.log,"link detected in javascript: %s"LF,str); test_flush;
  700.                             }
  701.                             p=(int) a- (int) adr;    // p non nul: TRAITER CHAINE COMME FICHIER
  702.                           }
  703.                         }
  704.                         
  705.                         
  706.                       }
  707.                     }
  708.                   }
  709.                   
  710.                 }
  711.               }
  712.             }
  713.                 
  714.           } else
  715.             p=rech_tageq(adr,"primary");    // lien primaire, yeah
  716.           }
  717.           
  718.           
  719.           // ------------------------------------------------------------
  720.           // dernier recours - parsing "sale" : dΘtection systΘmatique des .gif, etc.
  721.           // risque: gΘnΘrer de faux fichiers parazites
  722.           // fix: ne parse plus dans les commentaires
  723.           // ------------------------------------------------------------
  724.           if ( (opt.parseall) && (ptr>0)) {           // option parsing "brut"
  725.             int incomment_justquit=0;
  726.             if (!is_realspace(*adr)) {
  727.               int noparse=0;
  728.  
  729.               // Gestion des /* */
  730.               if (inscript) {
  731.                 if (parseall_incomment) {
  732.                   if ((*adr=='/') && (*(adr-1)=='*'))
  733.                     parseall_incomment=0;
  734.                   incomment_justquit=1;       // ne pas noter dernier caractΦre
  735.                 } else {
  736.                   if ((*adr=='/') && (*(adr+1)=='*'))
  737.                     parseall_incomment=1;
  738.                 }
  739.               } else
  740.                 parseall_incomment=0;
  741.  
  742.               /* vΘrifier que l'on est pas dans un <!-- --> pur */
  743.               if ( (!intag) && (incomment) && (!inscript))
  744.                 noparse=1;        /* commentaire */
  745.  
  746.               // recherche d'URLs
  747.               if ((!parseall_incomment) && (!noparse)) {
  748.                 if (!p) {                   // non dΘja trouvΘ
  749.                   if (adr != r.adr) {     // >1 caractΦre
  750.                     // scanner les chaines
  751.                     if ((*adr == '\"') || (*adr=='\'')) {         // "xx.gif" 'xx.gif'
  752.                       if (strchr("=(,",parseall_lastc)) {    // exemple: a="img.gif..
  753.                         char *a=adr;
  754.                         char stop=*adr;  // " ou '
  755.                         int count=0;
  756.                         
  757.                         // sauter caractΦres
  758.                         a++;
  759.                         while((*a) && (*a!='\'') && (*a!='\"') && (count<HTS_URLMAXSIZE)) { count++; a++; }
  760.                         
  761.                         // ok chaine terminΘe par " ou '
  762.                         if ((*a == stop) && (count<HTS_URLMAXSIZE) && (count>0)) {
  763.                           char c;
  764.                           char* aend;
  765.                           //
  766.                           aend=a;     // sauver dΘbut
  767.                           a++;
  768.                           while(is_realspace(*a)) a++;
  769.                           c=*a;
  770.                           if (strchr("),;>/+",c)) {     // exemple: ..img.gif";
  771.                             // le / est pour funct("img.gif" /* URL */);
  772.                             char tempo[HTS_URLMAXSIZE*2];
  773.                             char type[256];
  774.                             int url_ok=0;      // url valide?
  775.                             tempo[0]='\0'; type[0]='\0';
  776.                             //
  777.                             strncat(tempo,adr+1,count);
  778.                             //
  779.                             if ((!strchr(tempo,' ')) || inscript) {   // espace dedans: mΘfiance! (sauf dans code javascript)
  780.                               int invalid_url=0;
  781.                               
  782.                               // vΘrifier qu'il n'y a pas de caractΦres spΘciaux
  783.                               if (strchr(tempo,'*')
  784.                                 || strchr(tempo,'<')
  785.                                 || strchr(tempo,'>'))
  786.                                 invalid_url=1;
  787.                               
  788.                               /* non invalide? */
  789.                               if (!invalid_url) {
  790.                                 // Un plus α la fin? Alors ne pas prendre sauf si extension ("/toto.html#"+tag)
  791.                                 if (c!='+') {    // PAS de plus α la fin
  792.                                   char* a;
  793.                                   if ((strncmp(tempo,"http://",7)==0) || (strncmp(tempo,"ftp://",6)==0))  // ok pas de problΦme
  794.                                     url_ok=1;
  795.                                   else if (tempo[strlen(tempo)-1]=='/') {        // un slash: ok..
  796.                                     if (inscript)   // sinon si pas javascript, mΘfiance (rΘpertoire style base?)
  797.                                       url_ok=1;
  798.                                   } else if ((a=strchr(tempo,'/'))) {        // un slash: ok..
  799.                                     if (inscript) {    // sinon si pas javascript, mΘfiance (style "text/css")
  800.                                       if (strchr(a+1,'/'))  // un seul / : abandon (STYLE type='text/css')
  801.                                         url_ok=1;
  802.                                     }
  803.                                   }
  804.                                 }
  805.                                 // Prendre si extension reconnue
  806.                                 if (!url_ok) {
  807.                                   get_httptype(type,tempo,0);
  808.                                   if (strnotempty(type))     // type reconnu!
  809.                                     url_ok=1;
  810.                                 }
  811.                                 //
  812.                                 // Ok, cela pourrait Ωtre une URL
  813.                                 if (url_ok) {
  814.                                   // Accepter URL, on la traitera comme une URL normale!!
  815.                                   p=1;
  816.                                 }
  817.                               }
  818.                             }
  819.                           }
  820.                         }
  821.                       }
  822.                     }
  823.                   }
  824.                 }  // p == 0
  825.                 
  826.                 // plus dans un commentaire
  827.                 if (!incomment_justquit)
  828.                   parseall_lastc=*adr;             // caractΦre avant le prochain
  829.                 
  830.               } // not in comment
  831.               
  832.             }  // if realspace
  833.           }  // if parseall
  834.           
  835.           
  836.           // ------------------------------------------------------------
  837.           // p!=0 : on a repΘrΘ un Θventuel lien
  838.           // ------------------------------------------------------------
  839.           //
  840.           if (p>0) {    // on a repΘrΘ un lien
  841.             //int lien_valide=0;
  842.             char* eadr=NULL;          /* fin de l'URL */
  843.             char* quote_adr=NULL;     /* adresse du ? dans l'adresse */
  844.             int ok=1;
  845.             char quote='\0';
  846.  
  847.             // TEST
  848.             /*{
  849.               static int loop=0;
  850.               if ((++loop)%5000==0)
  851.                 loop=0;
  852.             }*/
  853.             
  854.             // si nofollow a ΘtΘ dΘclenchΘ, rΘΘcrire tous les liens en externe
  855.             if (nofollow)
  856.               p_nocatch=1;
  857.  
  858.             // Θcrire codebase avant, flusher avant code
  859.             if ((p_type==-1) || (p_type==-2)) {
  860.               if ((opt.getmode & 1) && (ptr>0)) {
  861.                 HT_ADD_ADR;    // refresh
  862.               }
  863.               lastsaved=adr;    // dernier Θcrit+1
  864.             }
  865.             
  866.             // sauter espaces
  867.             adr+=p;
  868.             while((is_space(*adr)) && (quote=='\0')) {
  869.               if (!quote)
  870.                 if ((*adr=='\"') || (*adr=='\''))
  871.                   quote=*adr;                     // on doit attendre cela α la fin
  872.                                                   // puis quitter
  873.                 adr++;    // sauter les espaces, "" et cie
  874.             }
  875.             /* s'arrΩter que ce soit un ' ou un " : pour document.write('<img src="foo'+a); par exemple! */
  876.             if (inscript)
  877.               quote='\0';
  878.             
  879.             // sauter Θventuel \" ou \' javascript
  880.             if (inscript) {    // on est dans un obj.write("..
  881.               if (*adr=='\\') {
  882.                 if ((*(adr+1)=='\'') || (*(adr+1)=='"')) {  // \" ou \'
  883.                   adr+=2;    // sauter
  884.                 }
  885.               }
  886.             }
  887.             
  888.             // sauter content="1;URL=http://..
  889.             if (p_searchMETAURL) {
  890.               int l=0;
  891.               while(!strfield(adr+l,"URL=") && (l<128) ) l++;
  892.               if (!strfield(adr,"URL="))
  893.                 ok=-1;
  894.               else
  895.                 adr+=(l+4);
  896.             }
  897.  
  898.             /* Θviter les javascript:document.location=.. : les parser, plut⌠t */
  899.             if (ok!=-1) {
  900.               if (strfield(adr,"javascript:")) {
  901.                 ok=-1;
  902.                 /*
  903.                 On est dΘsormais dans du code javascript
  904.                 */
  905.                 inscript_tag=inscript=1;
  906.                 inscript_tag_lastc=quote;     /* α attendre α la fin */
  907.               }
  908.             }
  909.             
  910.             if (p_type==1) {
  911.               if (*adr=='#') {
  912.                 adr++;           // sauter # pour usemap etc
  913.               }
  914.             }
  915.             eadr=adr;
  916.             
  917.             // ne pas flusher aprΦs code si on doit Θcrire le codebase avant!
  918.             if ((p_type!=-1) && (p_type!=2) && (p_type!=-2)) {
  919.               if ((opt.getmode & 1) && (ptr>0)) {
  920.                 HT_ADD_ADR;    // refresh
  921.               }
  922.               lastsaved=adr;    // dernier Θcrit+1
  923.               // aprΦs on Θcrira soit les donnΘes initiales,
  924.               // soir une URL/lien modifiΘ!
  925.             } else if (p_type==-1) p_flush=adr;    // flusher jusqu'α adr ensuite
  926.             
  927.             if (ok!=-1) {    // continuer
  928.               // dΘcouper le lien
  929.               do {
  930.                 if ((* (unsigned char*) eadr)<32) {   // caractΦre de contr⌠le (ou \0)
  931.                   if (!is_space(*eadr))
  932.                     ok=0; 
  933.                 }
  934.                 if ( ( ((int) eadr) - ((int) adr) ) > HTS_URLMAXSIZE)  // ** trop long, >HTS_URLMAXSIZE caractΦres (on prΘvoit HTS_URLMAXSIZE autres pour path)
  935.                   ok=-1;    // ne pas traiter ce lien
  936.                 
  937.                 if (ok) {
  938.                   //if (*eadr!=' ') {  
  939.                   if (is_space(*eadr)) {   // guillemets,CR, etc
  940.                     if ((!quote) || (*eadr==quote))     // si pas d'attente de quote spΘciale ou si quote atteinte
  941.                       ok=0; 
  942.                   }
  943.                   else {
  944.                     switch(*eadr) {
  945.                     case '>': 
  946.                       if (!quote) {
  947.                         if (!inscript) {
  948.                           intag=0;    // PLUS dans un tag!
  949.                           intag_start_valid=0;
  950.                         }
  951.                         ok=0;
  952.                       }
  953.                       break;
  954.                       /*case '<':*/ case '#': ok=0; break;    // case '?': non!
  955.                     case '\\': if (inscript) ok=0; break;     // \" ou \' point d'arrΩt
  956.                     case '?': quote_adr=adr; break;           // noter position query
  957.                     }
  958.                   }
  959.                   //}
  960.                 } 
  961.                 eadr++;
  962.               } while(ok==1);     
  963.               
  964.               if ( (((int) eadr)-((int) adr)) <= 1) ok=-1;     // lien vide
  965.             }
  966.             
  967.             if (ok==0) {    // tester un lien
  968.               char lien[HTS_URLMAXSIZE*2];
  969.               int meme_adresse=0;      // 0 par dΘfaut pour primary
  970.               //char *copie_de_adr=adr;
  971.               //char* p;
  972.               
  973.               // construire lien (dΘcoupage)
  974.               if ( (((int) eadr)-((int) adr)-1) < HTS_URLMAXSIZE  ) {    // pas trop long?
  975.                 strncpy(lien,adr,((int) eadr)-((int) adr)-1);
  976.                 *(lien+  (((int) eadr)-((int) adr))-1  )='\0';
  977.                 //printf("link: %s\n",lien);          
  978.                 // supprimer les espaces
  979.                 while((lien[strlen(lien)-1]==' ') && (strnotempty(lien))) lien[strlen(lien)-1]='\0';
  980.                 // supprimer les // en / (sauf pour http://)
  981.                 {
  982.                   char *a,*p,*q;
  983.                   int done=0;
  984.                   a=strstr(lien,":/");    // http://
  985.                   if (a) {
  986.                     a++;
  987.                     while(*a=='/') a++;    // position aprΦs http://
  988.                   } else {
  989.                     a=lien;                // dΘbut
  990.                   }
  991.                   q=strchr(a,'?');     // ne pas traiter aprΦs '?'
  992.                   if (!q)
  993.                     q=a+strlen(a)-1;
  994.                   while(( p=strstr(a,"//")) && (!done) ) {    // remplacer // par /
  995.                     if ((int) p>(int) q) {   // aprΦs le ? (toto.cgi?param=1//2.3)
  996.                       done=1;    // stopper
  997.                     } else {
  998.                       char tempo[HTS_URLMAXSIZE*2];
  999.                       tempo[0]='\0';
  1000.                       strncat(tempo,a,(int) p - (int) a);
  1001.                       strcat (tempo,p+1);
  1002.                       strcpy(a,tempo);    // recopier
  1003.                     }
  1004.                   }
  1005.                 }
  1006.               } else
  1007.                 lien[0]='\0';    // erreur
  1008.               
  1009.               // ------------------------------------------------------
  1010.               // Lien repΘrΘ et extrait
  1011.               if (strnotempty(lien)>0) {           // construction du lien
  1012.                 char adr[HTS_URLMAXSIZE*2],fil[HTS_URLMAXSIZE*2];          // ATTENTION adr cache le "vrai" adr
  1013.                 int forbidden_url=-1;              // lien non interdit (mais non autorisΘ..)
  1014.                 int just_test_it=0;                // mode de test des liens
  1015.                 int set_prio_to=0;                 // pour capture de page isolΘe
  1016.                 int import_done=0;                 // lien importΘ (ne pas scanner ensuite *α priori*)
  1017.                 //
  1018.                 adr[0]='\0'; fil[0]='\0';
  1019.                 //
  1020.                 // 0: autorisΘ
  1021.                 // 1: interdit (patcher tout de mΩme adresse)
  1022.                 
  1023.                 if ((opt.debug>1) && (opt.log!=NULL)) {
  1024.                   fspc(opt.log,"debug"); fprintf(opt.log,"link detected in html: %s"LF,lien); test_flush;
  1025.                 }
  1026.  
  1027.                 // purger CR,LF rΘsiduels (IMG SRC="foo.<\n>gif")
  1028.                 {
  1029.                   char* a;
  1030.                   while ((a=strchr(lien,'\n'))) {
  1031.                     char tempo[HTS_URLMAXSIZE*2];
  1032.                     tempo[0]='\0';
  1033.                     strncat(tempo,lien,(int) a - (int) lien);
  1034.                     strcat(tempo,a+1);
  1035.                     strcpy(lien,tempo);
  1036.                   }
  1037.                   while ((a=strchr(lien,'\r'))) {
  1038.                     char tempo[HTS_URLMAXSIZE*2];
  1039.                     tempo[0]='\0';
  1040.                     strncat(tempo,lien,(int) a - (int) lien);
  1041.                     strcat(tempo,a+1);
  1042.                     strcpy(lien,tempo);
  1043.                   }
  1044.                 }
  1045.                 
  1046.                 /* Unescape/escape %20 and other   */
  1047.                 {
  1048.                   char query[HTS_URLMAXSIZE*2];
  1049.                   char* a=strchr(lien,'?');
  1050.                   if (a) {
  1051.                     strcpy(query,a);
  1052.                     *a='\0';
  1053.                   } else
  1054.                     query[0]='\0';
  1055.                   // conversion & -> & et autres joyeusetΘs
  1056.                   unescape_amp(lien);
  1057.                   // dΘcoder l'inutile (%2E par exemple) et coder espaces
  1058.                   strcpy(lien,unescape_http(lien));
  1059.                   escape_spc_url(lien);
  1060.                   strcat(lien,query);     /* restore */
  1061.                 }
  1062.                 
  1063.                 // convertir les Θventuels \ en des / pour Θviter des problΦmes de reconnaissance!
  1064.                 {
  1065.                   char* a=jump_identification(lien);
  1066.                   while( (a=strchr(a,'\\')) ) *a='/';
  1067.                 }
  1068.                 
  1069.                 // supprimer le(s) ./
  1070.                 while ((lien[0]=='.') && (lien[1]=='/')) {
  1071.                   char tempo[HTS_URLMAXSIZE*2];
  1072.                   strcpy(tempo,lien+2);
  1073.                   strcpy(lien,tempo);
  1074.                 }
  1075.                 if (strnotempty(lien)==0)  // sauf si plus de nom de fichier
  1076.                   strcpy(lien,"./");
  1077.                 
  1078.                 // vΘrifie les /~machin -> /~machin/
  1079.                 // supposition dangereuse?
  1080.                 if (lien[strlen(lien)-1]!='/') {
  1081.                   char *a=lien+strlen(lien)-1;
  1082.                   // Θviter aussi index~1.html
  1083.                   while (((int) a>(int) lien) && (*a!='~') && (*a!='/') && (*a!='.')) a--;
  1084.                   if (*a=='~') {
  1085.                     strcat(lien,"/");    // ajouter slash
  1086.                   }
  1087.                 }
  1088.                 
  1089.                 
  1090.                 // Θliminer les Θventuels :80 (port par dΘfaut!)
  1091.                 {
  1092.                   char* a;
  1093.                   a=strstr(lien,"://");
  1094.                   if (a)
  1095.                     a+=3;
  1096.                   else
  1097.                     a=lien;
  1098.                   while((*a) && (*a!='/') && (*a!=':')) a++;
  1099.                   if (*a==':') {  // port
  1100.                     int port=0;
  1101.                     char* b=a+1;
  1102.                     while(isdigit((unsigned char)*b)) { port*=10; port+=(int) (*b-'0'); b++; }
  1103.                     if (port==80) {  // port 80, default
  1104.                       char tempo[HTS_URLMAXSIZE*2];
  1105.                       tempo[0]='\0';
  1106.                       strncat(tempo,lien,(int) a-(int) lien);
  1107.                       strcat(tempo,a+3);  // sauter :80
  1108.                       strcpy(lien,tempo);
  1109.                     }
  1110.                   }
  1111.                 }
  1112.                 
  1113.                 // filtrer les parazites (mailto & cie)
  1114.                 if (strfield(lien,"mailto:")) {  // ne pas traiter
  1115.                   error=1;
  1116.                 } else if (strfield(lien,"news:")) {  // ne pas traiter
  1117.                   error=1;
  1118.                 }
  1119.                 
  1120.                 // vΘrifier que l'on ne doit pas ajouter de .class
  1121.                 if (!error) {
  1122.                   if (add_class) {
  1123.                     char *a = lien+strlen(lien)-1;
  1124.                     while(((int) a > (int) lien) && (*a!='/') && (*a!='.')) a--;
  1125.                     if (*a != '.')
  1126.                       strcat(lien,".class");    // ajouter .class
  1127.                   }
  1128.                 }
  1129.                 
  1130.                 // si c'est un chemin, alors vΘrifier (toto/toto.html -> http://www/toto/)
  1131.                 if (!error) {
  1132.                   if ((opt.debug>1) && (opt.log!=NULL)) {
  1133.                     fspc(opt.log,"debug"); fprintf(opt.log,"position link check %s"LF,lien); test_flush;
  1134.                   }
  1135.                   
  1136.                   if ((p_type==2) || (p_type==-2)) {   // code ou codebase                        
  1137.                     // VΘrifier les codebase=applet (au lieu de applet/)
  1138.                     if (p_type==-2) {    // codebase
  1139.                       if (strnotempty(lien)) {
  1140.                         if (fil[strlen(lien)-1]!='/') {  // pas rΘpertoire
  1141.                           strcat(lien,"/");
  1142.                         }
  1143.                       }
  1144.                     }
  1145.                     /* only one ending / (bug on some pages) */
  1146.                     if ((int)strlen(lien)>2) {
  1147.                       while( (lien[strlen(lien)-2]=='/') && ((int)strlen(lien)>2) )    /* double // (bug) */
  1148.                         lien[strlen(lien)-1]='\0';
  1149.                     }
  1150.                     // copier nom host si besoin est
  1151.                     if (strstr(lien,"://")==NULL) {  // pas de http://
  1152.                       char adr2[HTS_URLMAXSIZE*2],fil2[HTS_URLMAXSIZE*2];  // ** euh ident_url_relatif??
  1153.                       if (ident_url_relatif(lien,urladr,urlfil,adr2,fil2)<0) {                        
  1154.                         error=1;
  1155.                       } else {
  1156.                         strcpy(lien,"http://");
  1157.                         strcat(lien,adr2);
  1158.                         if (*fil2!='/')
  1159.                           strcat(lien,"/");
  1160.                         strcat(lien,fil2);
  1161.                         {
  1162.                           char* a;
  1163.                           a=lien+strlen(lien)-1;
  1164.                           while((*a) && (*a!='/') && ((int) a> (int) lien)) a--;
  1165.                           if (*a=='/') {
  1166.                             *(a+1)='\0';
  1167.                           }
  1168.                         }
  1169.                         //char tempo[HTS_URLMAXSIZE*2];
  1170.                         //strcpy(tempo,"http://");
  1171.                         //strcat(tempo,urladr);    // host
  1172.                         //if (*lien!='/')
  1173.                         //  strcat(tempo,"/");
  1174.                         //strcat(tempo,lien);
  1175.                         //strcpy(lien,tempo);
  1176.                       }
  1177.                     }
  1178.                     
  1179.                     if (!error) {  // pas d'erreur?
  1180.                       if (p_type==2) {   // code ET PAS codebase      
  1181.                         char* a=lien+strlen(lien)-1;
  1182.                         while( ((int) a > (int) lien) && (*a) && (*a!='/')) a--;
  1183.                         if (*a=='/')     // ok on a repΘrΘ le dernier /
  1184.                           *(a+1)='\0';   // couper
  1185.                         else {
  1186.                           *lien='\0';    // Θliminer
  1187.                           error=1;   // erreur, ne pas poursuivre
  1188.                         }      
  1189.                       }
  1190.                       
  1191.                       // stocker base ou codebase?
  1192.                       switch(p_type) {
  1193.                       case 2: { 
  1194.                         //if (*lien!='/') strcat(base,"/");
  1195.                         strcpy(base,lien);
  1196.                               }
  1197.                         break;      // base
  1198.                       case -2: {
  1199.                         //if (*lien!='/') strcat(codebase,"/");
  1200.                         strcpy(codebase,lien); 
  1201.                                }
  1202.                         break;  // base
  1203.                       }
  1204.                       
  1205.                       if ((opt.debug>1) && (opt.log!=NULL)) {
  1206.                         fspc(opt.log,"debug"); fprintf(opt.log,"code/codebase link %s base %s"LF,lien,base); test_flush;
  1207.                       }
  1208.                       //printf("base code: %s - %s\n",lien,base);
  1209.                     }
  1210.                     
  1211.                   } else {
  1212.                     char* _base;
  1213.                     if (p_type==-1)   // code (applet)
  1214.                       _base=codebase;
  1215.                     else
  1216.                       _base=base;
  1217.                     
  1218.                     // ajouter chemin de base href..
  1219.                     if (strnotempty(_base)) {       // considΘrer base
  1220.                       if (!strstr(lien,"://")) {    // non absolue
  1221.                         if (*lien!='/') {           // non absolu sur le site (/)
  1222.                           if ( ((int) strlen(_base)+(int) strlen(lien))<HTS_URLMAXSIZE) {
  1223.                             char tempo[HTS_URLMAXSIZE*2];
  1224.                             // base est absolue
  1225.                             strcpy(tempo,_base);
  1226.                             strcat(tempo,lien);
  1227.                             strcpy(lien,tempo);        // patcher en considΘrant base
  1228.                             // ** vΘrifier que ../ fonctionne (ne doit pas arriver mais bon..)
  1229.                             
  1230.                             if ((opt.debug>1) && (opt.log!=NULL)) {
  1231.                               fspc(opt.log,"debug"); fprintf(opt.log,"link modified with code/codebase %s"LF,lien); test_flush;
  1232.                             }
  1233.                           } else {
  1234.                             error=1;    // erreur
  1235.                             if (opt.errlog) {
  1236.                               fspc(opt.errlog,"error"); fprintf(opt.errlog,"Link %s too long with base href"LF,lien);
  1237.                               test_flush;
  1238.                             }
  1239.                           }
  1240.                         }
  1241.                       }
  1242.                     }
  1243.                   }
  1244.                   }
  1245.                   
  1246.                   
  1247.                   // transformer lien quelconque (http, relatif, etc) en une adresse
  1248.                   // et un chemin+fichier (adr,fil)
  1249.                   if (!error) {
  1250.                     int reponse;
  1251.                     if ((opt.debug>1) && (opt.log!=NULL)) {
  1252.                       fspc(opt.log,"debug"); fprintf(opt.log,"build relative link %s with %s%s"LF,lien,urladr,urlfil); test_flush;
  1253.                     }
  1254.                     if ((reponse=ident_url_relatif(lien,urladr,urlfil,adr,fil))<0) {                        
  1255.                       adr[0]='\0';    // erreur
  1256.                       if (reponse==-2) {
  1257.                         if (opt.errlog) {
  1258.                           fspc(opt.errlog,"warning"); fprintf(opt.errlog,"Link %s not caught (unknown ftp:// protocol)"LF,lien);
  1259.                           test_flush;
  1260.                         }
  1261.                       }
  1262.                     }
  1263.                   } else {
  1264.                     if ((opt.debug>1) && (opt.log!=NULL)) {
  1265.                       fspc(opt.log,"debug"); fprintf(opt.log,"link %s not build, error detected before"LF,lien); test_flush;
  1266.                     }
  1267.                     adr[0]='\0';
  1268.                   }
  1269.                   
  1270. #if HTS_CHECK_STRANGEDIR
  1271.                   // !ATTENTION!
  1272.                   // Ici on teste les exotiques du genre www.truc.fr/machin (sans slash α la fin)
  1273.                   // je n'ai pas encore trouvΘ le moyen de faire la diffΘrence entre un rΘpertoire
  1274.                   // et un fichier en http A PRIORI : je fais donc un test
  1275.                   // En cas de moved xxx, on recalcule adr et fil, tout simplement
  1276.                   // DEFAUT: test effectuΘ plusieurs fois! α revoir!!!
  1277.                   if ((adr[0]!='\0') && (strcmp(adr,"file://") && (p_type!=2) && (p_type!=-2)) {
  1278.                     //## if ((adr[0]!='\0') && (adr[0]!=lOCAL_CHAR) && (p_type!=2) && (p_type!=-2)) {
  1279.                     if (fil[strlen(fil)-1]!='/') {  // pas rΘpertoire
  1280.                       if (ishtml(fil)==-2) {    // pas d'extension
  1281.                         char loc[HTS_URLMAXSIZE*2];  // Θventuelle nouvelle position
  1282.                         loc[0]='\0';
  1283.                         if ((opt.debug>1) && (opt.log!=NULL)) {
  1284.                           fspc(opt.log,"debug"); fprintf(opt.log,"link-check-directory: %s%s"LF,adr,fil);
  1285.                           test_flush;
  1286.                         }
  1287.                         
  1288.                         // tester Θventuelle nouvelle position
  1289.                         switch (http_location(adr,fil,loc).statuscode) {
  1290.                         case 200: // ok au final
  1291.                           if (strnotempty(loc)) {  // a changΘ d'adresse
  1292.                             if (opt.errlog) {
  1293.                               fspc(opt.errlog,"warning"); fprintf(opt.errlog,"Link %s%s has moved to %s for %s%s"LF,adr,fil,loc,urladr,urlfil);
  1294.                               test_flush;
  1295.                             }
  1296.                             
  1297.                             // recalculer adr et fil!
  1298.                             if (ident_url(loc,adr,fil)==-1) {
  1299.                               adr[0]='\0';  // cancel
  1300.                               if ((opt.debug>1) && (opt.log!=NULL)) {
  1301.                                 fspc(opt.log,"debug"); fprintf(opt.log,"link-check-dir: %s%s"LF,adr,fil);
  1302.                                 test_flush;
  1303.                               }
  1304.                             }
  1305.                             
  1306.                           }
  1307.                           break;
  1308.                         case -2: case -3:  // timeout ou erreur grave
  1309.                           if (opt.errlog) {
  1310.                             fspc(opt.errlog,"warning"); fprintf(opt.errlog,"Connection too slow for testing link %s%s (from %s%s)"LF,adr,fil,urladr,urlfil);
  1311.                             test_flush;
  1312.                           }
  1313.                           
  1314.                           break;
  1315.                         }
  1316.                         
  1317.                       }
  1318.                     } 
  1319.                   }
  1320. #endif
  1321.                   
  1322.                   // Le lien doit juste Ωtre rΘΘcrit, mais ne doit pas gΘnΘrer un lien
  1323.                   // exemple: <FORM ACTION="url_cgi">
  1324.                   if (p_nocatch) {
  1325.                     forbidden_url=1;    // interdire rΘcupΘration du lien
  1326.                     if ((opt.debug>1) && (opt.log!=NULL)) {
  1327.                       fspc(opt.log,"debug"); fprintf(opt.log,"link ignored at %s%s"LF,adr,fil);
  1328.                       test_flush;
  1329.                     }
  1330.                   }
  1331.                   
  1332.                   // Tester si un lien doit Ωtre acceptΘ ou refusΘ (wizard)
  1333.                   // forbidden_url=1 : lien refusΘ
  1334.                   // forbidden_url=0 : lien acceptΘ
  1335.                   //if ((ptr>0) && (p_type!=2) && (p_type!=-2)) {    // tester autorisations?
  1336.                   if ((p_type!=2) && (p_type!=-2)) {    // tester autorisations?
  1337.                     if (!p_nocatch) {
  1338.                       if (adr[0]!='\0') {          
  1339.                         if ((opt.debug>1) && (opt.log!=NULL)) {
  1340.                           fspc(opt.log,"debug"); fprintf(opt.log,"wizard link test at %s%s.."LF,adr,fil);
  1341.                           test_flush;
  1342.                         }
  1343.                         forbidden_url=hts_acceptlink(&opt,ptr,lien_tot,liens,
  1344.                           adr,fil,
  1345.                           filters,&filptr,filter_max,
  1346.                           &robots,
  1347.                           &set_prio_to,
  1348.                           &just_test_it);
  1349.                         if ((opt.debug>1) && (opt.log!=NULL)) {
  1350.                           fspc(opt.log,"debug"); fprintf(opt.log,"result for wizard link test: %d"LF,forbidden_url);
  1351.                           test_flush;
  1352.                         }
  1353.                       }
  1354.                     }
  1355.                   }
  1356.                   
  1357.                   // calculer meme_adresse
  1358.                   meme_adresse=strfield2(adr,urladr);
  1359.                   
  1360.                   
  1361.                   
  1362.                   // DΘbut partie sauvegarde
  1363.                   
  1364.                   // ici on forme le nom du fichier α sauver, et on patche l'URL
  1365.                   if (adr[0]!='\0') {
  1366.                     // savename: simplifier les ../ et autres joyeusetΘs
  1367.                     char save[HTS_URLMAXSIZE*2];
  1368.                     int r_sv=0;
  1369.                     // En cas de moved, adresse premiΦre
  1370.                     char former_adr[HTS_URLMAXSIZE*2];
  1371.                     char former_fil[HTS_URLMAXSIZE*2];
  1372.                     //
  1373.                     save[0]='\0'; former_adr[0]='\0'; former_fil[0]='\0';
  1374.                     //
  1375.                     
  1376.                     // nom du chemin α sauver si on doit le calculer
  1377.                     // note: url_savename peut dΘcider de tester le lien si il le trouve
  1378.                     // suspect, et modifier alors adr et fil
  1379.                     // dans ce cas on aura une rΘfΘrence directe au lieu des traditionnels
  1380.                     // moved en cascade (impossible α reproduire α priori en local, lorsque des fichiers
  1381.                     // gif sont impliquΘs par exemple)
  1382.                     if ((p_type!=2) && (p_type!=-2)) {  // pas base href ou codebase
  1383.                       if (forbidden_url!=1) {
  1384.                         char last_adr[HTS_URLMAXSIZE*2];
  1385.                         last_adr[0]='\0';
  1386.                         //char last_fil[HTS_URLMAXSIZE*2]="";
  1387.                         strcpy(last_adr,adr);    // ancienne adresse
  1388.                         //strcpy(last_fil,fil);    // ancien chemin
  1389.                         r_sv=url_savename(adr,fil,save,former_adr,former_fil,liens[ptr]->adr,liens[ptr]->fil,&opt,liens,lien_tot,back,back_max,&cache,&hash,ptr,numero_passe);
  1390.                         if (strcmp(last_adr,adr) != 0) {  // a changΘ
  1391.                           
  1392.                           // 2e test si moved
  1393.                           
  1394.                           // Tester si un lien doit Ωtre acceptΘ ou refusΘ (wizard)
  1395.                           // forbidden_url=1 : lien refusΘ
  1396.                           // forbidden_url=0 : lien acceptΘ
  1397.                           if ((ptr>0) && (p_type!=2) && (p_type!=-2)) {    // tester autorisations?
  1398.                             if (!p_nocatch) {
  1399.                               if (adr[0]!='\0') {          
  1400.                                 if ((opt.debug>1) && (opt.log!=NULL)) {
  1401.                                   fspc(opt.log,"debug"); fprintf(opt.log,"wizard moved link retest at %s%s.."LF,adr,fil);
  1402.                                   test_flush;
  1403.                                 }
  1404.                                 forbidden_url=hts_acceptlink(&opt,ptr,lien_tot,liens,
  1405.                                   adr,fil,
  1406.                                   filters,&filptr,filter_max,
  1407.                                   &robots,
  1408.                                   &set_prio_to,
  1409.                                   &just_test_it);
  1410.                                 if ((opt.debug>1) && (opt.log!=NULL)) {
  1411.                                   fspc(opt.log,"debug"); fprintf(opt.log,"result for wizard moved link retest: %d"LF,forbidden_url);
  1412.                                   test_flush;
  1413.                                 }
  1414.                               }
  1415.                             }
  1416.                           }
  1417.                           
  1418.                           //import_done=1;    // c'est un import!
  1419.                           meme_adresse=0;   // on a changΘ
  1420.                         }
  1421.                       } else {
  1422.                         strcpy(save,"");  // dummy
  1423.                       }
  1424.                     }
  1425.                     if (r_sv!=-1) {  // pas d'erreur, on continue
  1426.                       /* log */
  1427.                       if ((opt.debug>1) && (opt.log!=NULL)) {
  1428.                         fspc(opt.log,"debug");
  1429.                         if (forbidden_url!=1) {    // le lien va Ωtre chargΘ
  1430.                           if ((p_type==2) || (p_type==-2)) {  // base href ou codebase, pas un lien
  1431.                             fprintf(opt.log,"Code/Codebase: %s%s"LF,adr,fil);
  1432.                           } else if ((opt.getmode & 4)==0) {
  1433.                             fprintf(opt.log,"Record: %s%s -> %s"LF,adr,fil,save);
  1434.                           } else {
  1435.                             if (!ishtml(fil))
  1436.                               fprintf(opt.log,"Record after: %s%s -> %s"LF,adr,fil,save);
  1437.                             else
  1438.                               fprintf(opt.log,"Record: %s%s -> %s"LF,adr,fil,save);
  1439.                           } 
  1440.                         } else
  1441.                           fprintf(opt.log,"External: %s%s"LF,adr,fil);
  1442.                         test_flush;
  1443.                       }
  1444.                       /* FIN log */
  1445.                       
  1446.                       // Θcrire lien
  1447.                       if ((p_type==2) || (p_type==-2)) {  // base href ou codebase, sauter
  1448.                         lastsaved=eadr-1+1;  // sauter "
  1449.                       } else if (forbidden_url==1) {    // le lien ne sera pas chargΘ, rΘfΘrence externe!
  1450.                         if ((opt.getmode & 1) && (ptr>0)) {
  1451.                           if (p_type!=-1) {     // pas que le nom de fichier (pas classe java)
  1452.                             if (!opt.external) {
  1453.                               if (!strstr(adr,"://")) {
  1454.                                 HT_ADD("http://");
  1455.                               }
  1456.                               HT_ADD(adr);
  1457.                               if (*fil!='/')
  1458.                                 HT_ADD("/");
  1459.                               HT_ADD(fil);
  1460.                               //
  1461.                             } else {    // fichier/page externe, mais on veut gΘnΘrer une erreur
  1462.                               //
  1463.                               int patch_it=0;
  1464.                               int add_url=0;
  1465.                               char* cat_name=NULL;
  1466.                               char* cat_data=NULL;
  1467.                               int cat_data_len=0;
  1468.                               
  1469.                               // ajouter lien external
  1470.                               switch ((fil[strlen(fil)-1]=='/')?1:(ishtml(fil))) {
  1471.                               case 1: case -2:       // html ou rΘpertoire
  1472.                                 if (opt.getmode & 1) {  // sauver html
  1473.                                   patch_it=1;   // redirect
  1474.                                   add_url=1;    // avec link?
  1475.                                   cat_name="external.html";
  1476.                                   cat_data=HTS_DATA_UNKNOWN_HTML;
  1477.                                   cat_data_len=HTS_DATA_UNKNOWN_HTML_LEN;
  1478.                                 }
  1479.                                 break;
  1480.                               default:    // inconnu
  1481.                                 if ( (strfield2(fil+strlen(fil)-4,".gif")) 
  1482.                                   || (strfield2(fil+strlen(fil)-4,".jpg")) 
  1483.                                   || (strfield2(fil+strlen(fil)-4,".xbm")) 
  1484.                                   || (ishtml(fil)!=0) ) {
  1485.                                   patch_it=1;   // redirect
  1486.                                   add_url=1;    // avec link aussi
  1487.                                   cat_name="external.gif";
  1488.                                   cat_data=HTS_DATA_UNKNOWN_GIF;
  1489.                                   cat_data_len=HTS_DATA_UNKNOWN_GIF_LEN;
  1490.                                 }
  1491.                                 break;
  1492.                               }// html,gif
  1493.                               
  1494.                               if (patch_it) {
  1495.                                 char save[HTS_URLMAXSIZE*2];
  1496.                                 char tempo[HTS_URLMAXSIZE*2];
  1497.                                 strcpy(save,opt.path_html);
  1498.                                 strcat(save,cat_name);
  1499.                                 if (lienrelatif(tempo,save,savename)==0) {
  1500.                                   HT_ADD(tempo);    // page externe
  1501.                                   if (add_url) {
  1502.                                     HT_ADD("?link=");    // page externe
  1503.                                     HT_ADD(adr);
  1504.                                     if (*fil!='/')
  1505.                                       HT_ADD("/");
  1506.                                     HT_ADD(fil);
  1507.                                   }
  1508.                                 }
  1509.                                 
  1510.                                 // Θcrire fichier?
  1511.                                 if (!fexist(fconcat(opt.path_html,cat_name))) {
  1512.                                   FILE* fp = filecreate(fconcat(opt.path_html,cat_name));
  1513.                                   if (fp) {
  1514.                                     if (cat_data_len==0) {   // texte
  1515.                                       verif_backblue(opt.path_html);
  1516.                                       fprintf(fp,"%s%s","<!-- Created by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"LF,cat_data);
  1517.                                     } else {                    // data
  1518.                                       fwrite(cat_data,cat_data_len,1,fp);
  1519.                                     }
  1520.                                     fclose(fp);
  1521.                                     usercommand(0,NULL,fconcat(opt.path_html,cat_name));
  1522.                                   }
  1523.                                 }
  1524.                               }  else {    // Θcrire normalement le nom de fichier
  1525.                                 HT_ADD("http://");
  1526.                                 HT_ADD(adr);
  1527.                                 if (*fil!='/')
  1528.                                   HT_ADD("/");
  1529.                                 HT_ADD(fil);
  1530.                               }// patcher?
  1531.                             }  // external
  1532.                           } else {  // que le nom de fichier (classe java)
  1533.                             // en gros recopie de plus bas: copier codebase et base
  1534.                             if (p_flush) {
  1535.                               char tempo[HTS_URLMAXSIZE*2];    // <-- ajoutΘ
  1536.                               char tempo_pat[HTS_URLMAXSIZE*2];
  1537.                               tempo_pat[0]='\0';
  1538.                               strcpy(tempo,fil);  // <-- ajoutΘ
  1539.                               {
  1540.                                 char* a=tempo+strlen(tempo)-1;
  1541.                                 while( ((int) a > (int) tempo) && (*a) && (*a!='/')) a--;
  1542.                                 if (*a=='/') {
  1543.                                   char tempo2[HTS_URLMAXSIZE*2];
  1544.                                   strcpy(tempo2,a+1);
  1545.                                   strncat(tempo_pat,tempo,(int) a-(int) tempo+1);  // chemin
  1546.                                   strcpy(tempo,tempo2);                     // fichier
  1547.                                 }
  1548.                               }
  1549.                               
  1550.                               // Θrire codebase="chemin"
  1551.                               if ((opt.getmode & 1) && (ptr>0)) {
  1552.                                 char tempo4[HTS_URLMAXSIZE*2];
  1553.                                 tempo4[0]='\0';
  1554.                                 
  1555.                                 if (strnotempty(tempo_pat)) {
  1556.                                   HT_ADD("codebase=\"http://");
  1557.                                   HT_ADD(adr);
  1558.                                   if (*tempo_pat!='/') HT_ADD("/");
  1559.                                   HT_ADD(tempo_pat);
  1560.                                   HT_ADD("\" ");
  1561.                                 }
  1562.                                 
  1563.                                 strncat(tempo4,lastsaved,(int) p_flush-(int) lastsaved);
  1564.                                 HT_ADD(tempo4);    // refresh code="
  1565.                                 HT_ADD(tempo);
  1566.                               }
  1567.                             }
  1568.                           }
  1569.                         }
  1570.                         lastsaved=eadr-1;
  1571.                       } 
  1572.                       /*
  1573.                       else if (opt.urlmode==1) {    // ABSOLU, c'est le cas le moins courant
  1574.                       //  NE FONCTIONNE PAS!!  (et est inutile)
  1575.                       if ((opt.getmode & 1) && (ptr>0)) {    // ecrire les html
  1576.                       // Θcrire le lien modifiΘ, absolu
  1577.                       HT_ADD("file:");
  1578.                       if (*save=='/')
  1579.                       HT_ADD(save+1)
  1580.                       else
  1581.                       HT_ADD(save)
  1582.                       }
  1583.                       lastsaved=eadr-1;    // dernier Θcrit+1 (enfin euh apres on fait un ++ alors hein)
  1584.                       }
  1585.                       */
  1586.                       else if (opt.urlmode==2) {  // RELATIF
  1587.                         char tempo[HTS_URLMAXSIZE*2];
  1588.                         tempo[0]='\0';
  1589.                         // calculer le lien relatif
  1590.                         
  1591.                         if (lienrelatif(tempo,save,savename)==0) {
  1592.                           if ((opt.debug>1) && (opt.log!=NULL)) {
  1593.                             fspc(opt.log,"debug"); fprintf(opt.log,"relative link at %s build with %s and %s: %s"LF,adr,save,savename,tempo);
  1594.                             test_flush;
  1595.                           }
  1596.                           
  1597.                           // lien applet (code) - il faut placer un codebase avant
  1598.                           if (p_type==-1) {  // que le nom de fichier
  1599.                             
  1600.                             if (p_flush) {
  1601.                               char tempo_pat[HTS_URLMAXSIZE*2];
  1602.                               tempo_pat[0]='\0';
  1603.                               {
  1604.                                 char* a=tempo+strlen(tempo)-1;
  1605.                                 while( ((int) a > (int) tempo) && (*a) && (*a!='/')) a--;
  1606.                                 if (*a=='/') {
  1607.                                   char tempo2[HTS_URLMAXSIZE*2];
  1608.                                   strcpy(tempo2,a+1);
  1609.                                   strncat(tempo_pat,tempo,(int) a-(int) tempo+1);  // chemin
  1610.                                   strcpy(tempo,tempo2);                     // fichier
  1611.                                 }
  1612.                               }
  1613.                               
  1614.                               // Θrire codebase="chemin"
  1615.                               if ((opt.getmode & 1) && (ptr>0)) {
  1616.                                 char tempo4[HTS_URLMAXSIZE*2];
  1617.                                 tempo4[0]='\0';
  1618.                                 
  1619.                                 if (strnotempty(tempo_pat)) {
  1620.                                   HT_ADD("codebase=\"");
  1621.                                   HT_ADD(tempo_pat);
  1622.                                   HT_ADD("\" ");
  1623.                                 }
  1624.                                 
  1625.                                 strncat(tempo4,lastsaved,(int) p_flush-(int) lastsaved);
  1626.                                 HT_ADD(tempo4);    // refresh code="
  1627.                               }
  1628.                             }
  1629.                             //lastsaved=adr;    // dernier Θcrit+1
  1630.                           }                              
  1631.                           
  1632.                           if ((opt.getmode & 1) && (ptr>0)) {
  1633.                             // Θcrire le lien modifiΘ, relatif
  1634.                             HT_ADD(tempo);
  1635.                           }
  1636.                           lastsaved=eadr-1;    // dernier Θcrit+1 (enfin euh apres on fait un ++ alors hein)
  1637.                         } else {
  1638.                           if (opt.errlog) {
  1639.                             fprintf(opt.errlog,"Error building relative link %s and %s"LF,save,savename);
  1640.                             test_flush;
  1641.                           }
  1642.                         }
  1643.                       }  // sinon le lien sera Θcrit normalement
  1644.                       
  1645.                       
  1646. #if 0
  1647.                       if (fexist(save)) {    // le fichier existe..
  1648.                         adr[0]='\0';
  1649.                         //if ((opt.debug>0) && (opt.log!=NULL)) {
  1650.                         if (opt.errlog) {
  1651.                           fspc(opt.errlog,"warning"); fprintf(opt.errlog,"Link has already been written on disk, cancelled: %s"LF,save);
  1652.                           test_flush;
  1653.                         }
  1654.                       }
  1655. #endif                            
  1656.                       
  1657.                       if ((adr[0]!='\0') && (p_type!=2) && (p_type!=-2) && ( (forbidden_url!=1) || (just_test_it))) {  // si le fichier n'existe pas, ajouter α la liste                            
  1658.                         // n'y a-t-il pas trop de liens?
  1659.                         if (lien_tot+1 >= lien_max-4) {    // trop de liens!
  1660.                           printf("PANIC! : Too many URLs : >%d [%d]\n",lien_tot,__LINE__);
  1661.                           if (opt.errlog) {
  1662.                             fprintf(opt.errlog,LF"Too many URLs, giving up..(>%d)"LF,lien_max);
  1663.                             fprintf(opt.errlog,"To avoid that: use #L option for more links (example: -#L1000000)"LF);
  1664.                             test_flush;
  1665.                           }
  1666.                           if ((opt.getmode & 1) && (ptr>0)) { if (fp) { fclose(fp); fp=NULL; } }
  1667.                           XH_uninit;   // dΘsallocation mΘmoire & buffers
  1668.                           return 0;
  1669.                           
  1670.                         } else {    // noter le lien sur la listes des liens α charger
  1671.                           int pass_fix,dejafait=0;
  1672.                           
  1673.                           // Calculer la prioritΘ de ce lien
  1674.                           if ((opt.getmode & 4)==0) {    // traiter html aprΦs
  1675.                             pass_fix=0;
  1676.                           } else {    // vΘrifier que ce n'est pas un !html
  1677.                             if (!ishtml(fil))
  1678.                               pass_fix=1;        // prioritΘ infΘrieure (traiter aprΦs)
  1679.                             else
  1680.                               pass_fix=max(0,numero_passe);    // prioritΘ normale
  1681.                           }
  1682.                           
  1683.                           // vΘrifier que le lien n'a pas dΘja ΘtΘ notΘ
  1684.                           // si c'est le cas, alors il faut s'assurer que la prioritΘ associΘe
  1685.                           // au fichier est la plus grande des deux prioritΘs
  1686.                           //
  1687.                           // On part de la fin et on essaye de se presser (Θconomise temps machine)
  1688. #if HTS_HASH
  1689.                           {
  1690.                             int i=hash_read(&hash,save,"",0);      // lecture type 0 (sav)
  1691.                             if (i>=0) {
  1692.                               liens[i]->depth=maximum(liens[i]->depth,liens[ptr]->depth-1);
  1693.                               dejafait=1;
  1694.                             }
  1695.                           }
  1696. #else
  1697.                           {
  1698.                             register int l;
  1699.                             register int i;
  1700.                             l=strlen(save);  // opti
  1701.                             for(i=lien_tot-1;(i>=0) && (dejafait==0);i--) {
  1702.                               if (liens[i]->sav_len==l) {    // mΩme taille de chaεne
  1703.                                 if (strcmp(liens[i]->sav,save)==0) {    // existe dΘja
  1704.                                   liens[i]->depth=maximum(liens[i]->depth,liens[ptr]->depth-1);
  1705.                                   dejafait=1;
  1706.                                 }
  1707.                               }
  1708.                             }
  1709.                           }
  1710. #endif
  1711.                           
  1712.                           // le lien n'a jamais ΘtΘ crΘΘ.
  1713.                           // cette fois ci, on le crΘe!
  1714.                           if (!dejafait) {                                
  1715.                             //
  1716.                             // >>>> CREER LE LIEN <<<<
  1717.                             //
  1718.                             // enregistrer lien α charger
  1719.                             //liens[lien_tot]->adr[0]=liens[lien_tot]->fil[0]=liens[lien_tot]->sav[0]='\0';
  1720.                             // mΩme adresse: l'objet pΦre est l'objet pΦre de l'actuel
  1721.                             
  1722.                             // DEBUT ROBOTS.TXT AJOUT
  1723.                             if (!just_test_it) {
  1724.                               if (strfield(adr,"ftp://")==0) {    // non ftp
  1725.                                 if (opt.robots) {    // rΘcupΘrer robots
  1726.                                   if (ishtml(fil)!=0) {                       // pas la peine pour des fichiers isolΘs
  1727.                                     if (checkrobots(&robots,adr,"") != -1) {    // robots.txt ?
  1728.                                       checkrobots_set(&robots,adr,"");          // ajouter entrΘe vide
  1729.                                       if (checkrobots(&robots,adr,"") == -1) {    // robots.txt ?
  1730.                                         // enregistrer robots.txt (MACRO)
  1731.                                         liens_record(adr,"/robots.txt","","","");
  1732.                                         if (liens[lien_tot]==NULL) {  // erreur, pas de place rΘservΘe
  1733.                                           printf("PANIC! : Not enough memory [%d]\n",__LINE__);
  1734.                                           if (opt.errlog) { 
  1735.                                             fprintf(opt.errlog,"Not enough memory, can not re-allocate %d bytes"LF,(add_tab_alloc+1)*sizeof(lien_url));
  1736.                                             test_flush;
  1737.                                           }
  1738.                                           if ((opt.getmode & 1) && (ptr>0)) { if (fp) { fclose(fp); fp=NULL; } }
  1739.                                           XH_uninit;    // dΘsallocation mΘmoire & buffers
  1740.                                           return 0;
  1741.                                         }  
  1742.                                         liens[lien_tot]->testmode=0;          // pas mode test
  1743.                                         liens[lien_tot]->link_import=0;       // pas mode import     
  1744.                                         liens[lien_tot]->premier=lien_tot;
  1745.                                         liens[lien_tot]->precedent=ptr;
  1746.                                         liens[lien_tot]->depth=0;
  1747.                                         liens[lien_tot]->pass2=max(0,numero_passe);
  1748.                                         liens[lien_tot]->retry=0;
  1749.                                         lien_tot++;  // UN LIEN DE PLUS
  1750. #if DEBUG_ROBOTS
  1751.                                         printf("robots.txt: added file robots.txt for %s\n",adr);
  1752. #endif
  1753.                                         if ((opt.debug>1) && (opt.log!=NULL)) {
  1754.                                           fspc(opt.log,"debug"); fprintf(opt.log,"robots.txt added at %s"LF,adr);
  1755.                                           test_flush;
  1756.                                         }
  1757.                                       } else {
  1758.                                         if (opt.errlog) {   
  1759.                                           fprintf(opt.errlog,"Unexpected robots.txt error at %d"LF,__LINE__);
  1760.                                           test_flush;
  1761.                                         }
  1762.                                       }
  1763.                                     }
  1764.                                   }
  1765.                                 }
  1766.                               }
  1767.                             }
  1768.                             // FIN ROBOTS.TXT AJOUT
  1769.                             
  1770.                             // enregistrer (MACRO)
  1771.                             liens_record(adr,fil,save,former_adr,former_fil);
  1772.                             if (liens[lien_tot]==NULL) {  // erreur, pas de place rΘservΘe
  1773.                               printf("PANIC! : Not enough memory [%d]\n",__LINE__);
  1774.                               if (opt.errlog) { 
  1775.                                 fprintf(opt.errlog,"Not enough memory, can not re-allocate %d bytes"LF,(add_tab_alloc+1)*sizeof(lien_url));
  1776.                                 test_flush;
  1777.                               }
  1778.                               if ((opt.getmode & 1) && (ptr>0)) { if (fp) { fclose(fp); fp=NULL; } }
  1779.                               XH_uninit;    // dΘsallocation mΘmoire & buffers
  1780.                               return 0;
  1781.                             }  
  1782.                             
  1783.                             // mode test?
  1784.                             if (!just_test_it)
  1785.                               liens[lien_tot]->testmode=0;          // pas mode test
  1786.                             else
  1787.                               liens[lien_tot]->testmode=1;          // mode test
  1788.                             if (!import_done)
  1789.                               liens[lien_tot]->link_import=0;       // pas mode import
  1790.                             else
  1791.                               liens[lien_tot]->link_import=1;       // mode import
  1792.                             // Θcrire autres paramΦtres de la structure-lien
  1793.                             if ((meme_adresse) && (!import_done) && (liens[ptr]->premier != 0))
  1794.                               liens[lien_tot]->premier=liens[ptr]->premier;
  1795.                             else    // sinon l'objet pΦre est le prΘcΘdent lui mΩme
  1796.                               liens[lien_tot]->premier=lien_tot;
  1797.                             // liens[lien_tot]->premier=ptr;
  1798.                             
  1799.                             liens[lien_tot]->precedent=ptr;
  1800.                             // noter la prioritΘ
  1801.                             if (!set_prio_to)
  1802.                               liens[lien_tot]->depth=liens[ptr]->depth-1;
  1803.                             else
  1804.                               liens[lien_tot]->depth=max(0,min(liens[ptr]->depth-1,set_prio_to-1));         // PRIORITE NULLE (catch page)
  1805.                             // noter pass
  1806.                             liens[lien_tot]->pass2=pass_fix;
  1807.                             liens[lien_tot]->retry=opt.retry;
  1808.                             
  1809.                             //strcpy(liens[lien_tot]->adr,adr);
  1810.                             //strcpy(liens[lien_tot]->fil,fil);
  1811.                             //strcpy(liens[lien_tot]->sav,save); 
  1812.                             if ((opt.debug>1) && (opt.log!=NULL)) {
  1813.                               if (!just_test_it) {
  1814.                                 fspc(opt.log,"debug"); fprintf(opt.log,"OK, NOTE: %s%s -> %s"LF,liens[lien_tot]->adr,liens[lien_tot]->fil,liens[lien_tot]->sav);
  1815.                               } else {
  1816.                                 fspc(opt.log,"debug"); fprintf(opt.log,"OK, TEST: %s%s"LF,liens[lien_tot]->adr,liens[lien_tot]->fil);
  1817.                               }
  1818.                               test_flush;
  1819.                             }
  1820.                             
  1821.                             lien_tot++;  // UN LIEN DE PLUS
  1822.                           } else { // if !dejafait
  1823.                             if ((opt.debug>1) && (opt.log!=NULL)) {
  1824.                               fspc(opt.log,"debug"); fprintf(opt.log,"link has already been recorded, cancelled: %s"LF,save);
  1825.                               test_flush;
  1826.                             }
  1827.                             
  1828.                           }
  1829.                           
  1830.                           
  1831.                         }   // si pas trop de liens
  1832.                       }   // si adr[0]!='\0'
  1833.                       
  1834.                       
  1835.                     }  // if adr[0]!='\0' 
  1836.                     
  1837.                   }  // if adr[0]!='\0'
  1838.                   
  1839.                 }    // if strlen(lien)>0
  1840.                 
  1841.               }   // if ok==0      
  1842.               
  1843.               adr=eadr-1;  // ** sauter
  1844.               
  1845.             }  // if (p) 
  1846.             
  1847.           }  // si '<' ou '>'
  1848.           
  1849.           // plus loin
  1850.           adr++;  
  1851.           
  1852.           // ----------
  1853.           // Θcrire peu α peu
  1854.           if ((opt.getmode & 1) && (ptr>0)) HT_ADD_ADR;
  1855.           lastsaved=adr;    // dernier Θcrit+1
  1856.           // ----------
  1857.           
  1858.           // pour les stats du shell si parsing trop long
  1859. #if HTS_ANALYSTE==2
  1860.           _hts_in_html_done=(100 * ((int) adr - (int) r.adr) ) / (int)(r.size);
  1861.           if (_hts_in_html_poll) {
  1862.             LLint nb;
  1863.             int nbk;
  1864.             _hts_in_html_poll=0;
  1865.             // temps α attendre, et remplir autant que l'on peut le cache (backing)
  1866.             back_wait(back,back_max,&opt,&cache,HTS_STAT.stat_timestart);        
  1867.             back_fillmax(back,back_max,&opt,&cache,liens,ptr,numero_passe,lien_tot);
  1868.  
  1869.             engine_stats();
  1870.             nb=back_transfered(HTS_STAT.stat_bytes,back,back_max);
  1871.             nbk=backlinks_done(liens,lien_tot,ptr);
  1872.             //if (!hts_htmlcheck_loop(back,back_max,-1,ptr,lien_tot,nb,new_stat_bytes,(int) (time_local()-stat_timestart),back_nsoc(back,back_max) )) {
  1873.             if (!hts_htmlcheck_loop(back,back_max,0,ptr,lien_tot,nb,new_stat_bytes,(int) (time_local()-HTS_STAT.stat_timestart),back_nsoc(back,back_max), HTS_STAT.stat_files,HTS_STAT.stat_updated_files,fspc(NULL,"error"),(int)HTS_STAT.rate,nbk )) {
  1874.               if (opt.errlog) {
  1875.                 fspc(opt.errlog,"info"); fprintf(opt.errlog,"Exit requested by shell or user"LF);
  1876.                 test_flush;
  1877.               } 
  1878.               exit_xh=1;  // exit requested
  1879.               XH_uninit;
  1880.               return 0;
  1881.               //adr = r.adr + r.size;  // exit
  1882.             } else if (_hts_cancel==1) {
  1883.               adr = r.adr + r.size;  // exit
  1884.               _hts_cancel=0;
  1885.             }
  1886.           }
  1887.  
  1888.           // refresh the backing system each 2 seconds
  1889.           if (engine_stats()) {
  1890.             back_wait(back,back_max,&opt,&cache,HTS_STAT.stat_timestart);        
  1891.             back_fillmax(back,back_max,&opt,&cache,liens,ptr,numero_passe,lien_tot);
  1892.           }
  1893. #endif
  1894.         } while(( ((int) adr) - ((int) r.adr) ) < r.size);
  1895. #if HTS_ANALYSTE==2
  1896.         _hts_in_html_parsing=0;  // flag
  1897.         _hts_cancel=0;           // pas de cancel
  1898. #endif
  1899.         if ((opt.getmode & 1) && (ptr>0)) {
  1900.           HT_ADD_END;    // achever
  1901.         }
  1902.         //
  1903.         //
  1904.         //
  1905.       }  // if !error
  1906.       
  1907.       
  1908.       if (opt.getmode & 1) { if (fp) { fclose(fp); fp=NULL; } }
  1909.       // sauver fichier
  1910.       //structcheck(savename);
  1911.       //filesave(r.adr,r.size,savename);
  1912.       
  1913. #if HTS_ANALYSTE
  1914.     }  // analyse OK
  1915. #endif
  1916.         
  1917.