home *** CD-ROM | disk | FTP | other *** search
/ PC Open 48 / pcopen48.iso / Internet / HtTrack / DATA1.CAB / Sources / src / htscatchurl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-21  |  8.0 KB  |  296 lines

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