home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 71 / CDROM71.ISO / internet / navoff / data1.cab / Sources / src / htscatchurl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-01  |  8.1 KB  |  297 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: URL catch .h                                           */
  38. /* Author: Xavier Roche                                         */
  39. /* ------------------------------------------------------------ */
  40.  
  41. // Fichier intercepteur d'URL .c
  42.  
  43. /* specific definitions */
  44. /* specific definitions */
  45. #include "htsbase.h"
  46. #include "htsnet.h"
  47. #include "htslib.h"
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <time.h>
  52. #include <fcntl.h>
  53. #if HTS_WIN
  54. #else
  55. #include <arpa/inet.h>
  56. #endif
  57. /* END specific definitions */
  58.  
  59. /* dΘfinitions globales */
  60. #include "htsglobal.h"
  61.  
  62. /* htslib */
  63. /*#include "htslib.h"*/
  64.  
  65. /* catch url */
  66. #include "htscatchurl.h"
  67.  
  68.  
  69. // URL Link catcher
  70.  
  71. // 0- Init the URL catcher with standard port
  72.  
  73. // catch_url_init(&port,&return_host);
  74. T_SOC catch_url_init_std(int* port_prox,char* adr_prox) {
  75.   T_SOC soc;
  76.   int try_to_listen_to[]={8080,3128,80,81,82,8081,3129,31337,0,-1};
  77.   int i=0;
  78.   do {
  79.     soc=catch_url_init(&try_to_listen_to[i],adr_prox);
  80.     *port_prox=try_to_listen_to[i];
  81.     i++;
  82.   } while( (soc == INVALID_SOCKET) && (try_to_listen_to[i]>=0));
  83.   return soc;
  84. }
  85.  
  86.  
  87. // 1- Init the URL catcher
  88.  
  89. // catch_url_init(&port,&return_host);
  90. T_SOC catch_url_init(int* port,char* adr) {
  91.   T_SOC soc = INVALID_SOCKET;
  92.   char h_loc[256+2];
  93.  
  94.   /*
  95. #ifdef _WIN32
  96.   {
  97.     WORD   wVersionRequested;
  98.     WSADATA wsadata;
  99.     int stat;
  100.     wVersionRequested = 0x0101;
  101.     stat = WSAStartup( wVersionRequested, &wsadata );
  102.     if (stat != 0) {
  103.       return INVALID_SOCKET;
  104.     } else if (LOBYTE(wsadata.wVersion) != 1  && HIBYTE(wsadata.wVersion) != 1) {
  105.       WSACleanup();
  106.       return INVALID_SOCKET;
  107.     }
  108.   }
  109. #endif
  110.   */
  111.  
  112.   if (gethostname(h_loc,256)==0) {    // host name
  113.     t_hostent* hp_loc;    
  114.     if ( (hp_loc=gethostbyname(h_loc)) ) {  // notre host      
  115.       if ( (soc=socket(AF_INET,SOCK_STREAM,0)) != INVALID_SOCKET) {
  116.         struct sockaddr_in server;
  117.         // effacer structure
  118.         bzero((char *)&server, sizeof(server));
  119.         server.sin_family = AF_INET;
  120.         server.sin_port = htons((unsigned short int) *port);    // demander port qque
  121.         // copie adresse locale
  122.         bcopy(hp_loc->h_addr, (char *)&server.sin_addr, hp_loc->h_length);
  123.         // // NAAN server.sin_addr.s_addr=htonl(INADDR_ANY);
  124.         if ( bind(soc,(struct sockaddr*) &server,sizeof(struct sockaddr)) == 0 ) {
  125.           struct sockaddr_in server2;
  126.           int len;
  127.           len=sizeof(server2);
  128.           // effacer structure
  129.           bzero((char *)&server2, sizeof(server2));
  130.           if (getsockname(soc,(struct sockaddr*) &server2,&len) == 0) {
  131.             *port=ntohs(server.sin_port);  // rΘcupΘrer port
  132.             if (listen(soc,10)>=0) {    // au pif le 10
  133.               char* dot = (char*) inet_ntoa(server2.sin_addr);
  134.               strcpy(adr,dot);
  135.             } else {
  136. #if _WIN32
  137.               closesocket(soc);
  138. #else
  139.               close(soc);
  140. #endif
  141.               soc=INVALID_SOCKET;
  142.             }
  143.             
  144.             
  145.           } else {
  146. #if _WIN32
  147.             closesocket(soc);
  148. #else
  149.             close(soc);
  150. #endif
  151.             soc=INVALID_SOCKET;
  152.           }
  153.           
  154.           
  155.         } else {
  156. #if _WIN32
  157.           closesocket(soc);
  158. #else
  159.           close(soc);
  160. #endif
  161.           soc=INVALID_SOCKET;
  162.         }
  163.       }
  164.     }
  165.   }
  166.   return soc;
  167. }
  168.  
  169. // 2 - Wait for URL
  170.  
  171. // catch_url
  172. // returns 0 if error
  173. // url: buffer where URL must be stored - or ip:port in case of failure
  174. // data: 32Kb
  175. int catch_url(T_SOC soc,char* url,char* method,char* data) {
  176.   int retour=0;
  177.  
  178.   // connexion (accept)
  179.   if (soc != INVALID_SOCKET) {
  180.     T_SOC soc2;
  181.     struct sockaddr dummyaddr;
  182.     int dummylen = sizeof(struct sockaddr);  
  183.     while ( (soc2=accept(soc,&dummyaddr,&dummylen)) == INVALID_SOCKET);
  184.   /*
  185. #ifdef _WIN32
  186.     closesocket(soc);
  187. #else
  188.     close(soc);
  189. #endif
  190.   */
  191.     soc = soc2;
  192.     /* INFOS */
  193.     {
  194.       struct sockaddr_in server2;
  195.       int len;
  196.       len=sizeof(server2);
  197.       // effacer structure
  198.       bzero((char *)&server2, sizeof(server2));
  199.       if (getpeername(soc,(struct sockaddr*) &server2,&len) == 0) {
  200.         char* dot = (char*) inet_ntoa(server2.sin_addr);
  201.         sprintf(url,"%s:%d",dot,htons(server2.sin_port));  
  202.       }
  203.     }
  204.     /* INFOS */
  205.  
  206.     // rΘception
  207.     if (soc != INVALID_SOCKET) {
  208.       char line[1000];
  209.       char protocol[256];
  210.       line[0]=protocol[0]='\0';
  211.       //
  212.       socinput(soc,line,1000);
  213.       if (strnotempty(line)) {
  214.         if (sscanf(line,"%s %s %s",method,url,protocol) == 3) {
  215.           char url_adr[HTS_URLMAXSIZE*2];
  216.           char url_fil[HTS_URLMAXSIZE*2];
  217.           // mΘthode en majuscule
  218.           int i,r=0;
  219.           url_adr[0]=url_fil[0]='\0';
  220.           //
  221.           for(i=0;i<(int) strlen(method);i++) {
  222.             if ((method[i]>='a') && (method[i]<='z'))
  223.               method[i]-=('a'-'A');
  224.           }
  225.           // adresse du lien
  226.           if (ident_url(url,url_adr,url_fil)>=0) {
  227.             // Traitement des en-tΩtes
  228.             char loc[HTS_URLMAXSIZE*2];
  229.             htsblk blkretour;
  230.             bzero((char *)&blkretour, sizeof(htsblk));    // effacer
  231.             blkretour.location=loc;    // si non nul, contiendra l'adresse vΘritable en cas de moved xx
  232.             // Lire en tΩtes restants
  233.             sprintf(data,"%s %s %s\r\n",method,url_fil,protocol);
  234.             while(strnotempty(line)) {
  235.               socinput(soc,line,1000);
  236.               treathead(NULL,NULL,NULL,&blkretour,line);  // traiter
  237.               strcat(data,line);
  238.               strcat(data,"\r\n");
  239.             }
  240.             // CR/LF final de l'en tΩte inutile car dΘja placΘ via la ligne vide juste au dessus
  241.             //strcat(data,"\r\n");
  242.             if (blkretour.totalsize>0) {
  243.               int len=(int)min(blkretour.totalsize,32000);
  244.               int pos=strlen(data);
  245.               // Copier le reste (post Θventuel)
  246.               while((len>0) && ((r=recv(soc,(char*) data+pos,len,0))>0) ) {
  247.                 pos+=r;
  248.                 len-=r;
  249.                 data[pos]='\0';       // terminer par NULL
  250.               }
  251.             }
  252.             // Envoyer page
  253.             sprintf(line,CATCH_RESPONSE);
  254.             send(soc,line,strlen(line),0);
  255.             // OK!
  256.             retour=1;
  257.           }
  258.         }
  259.       }  // sinon erreur
  260.     }
  261.   }
  262.   if (soc != INVALID_SOCKET) {
  263. #ifdef _WIN32
  264.     closesocket(soc);
  265.     /*
  266.     WSACleanup();
  267.     */
  268. #else
  269.     close(soc);
  270. #endif
  271.   }
  272.   return retour;
  273. }
  274.  
  275.  
  276.  
  277. // Lecture de ligne sur socket
  278. void socinput(T_SOC soc,char* s,int max) {
  279.   int c;
  280.   int j=0;
  281.   do {
  282.     unsigned char b;
  283.     if (recv(soc,(char*) &b,1,0)==1) {
  284.       c=b;
  285.       switch(c) {
  286.         case 13: break;  // sauter CR
  287.         case 10: c=-1; break;
  288.         case 9: case 12: break;  // sauter ces caractΦres
  289.         default: s[j++]=(char) c; break;
  290.       }
  291.     } else
  292.       c=EOF;
  293.   }  while((c!=-1) && (c!=EOF) && (j<(max-1)));
  294.   s[j++]='\0';
  295. }
  296.  
  297.