home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 71 / CDROM71.ISO / internet / navoff / data1.cab / Sources / src / htslib.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-01  |  13.3 KB  |  438 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: Subroutines .h                                         */
  38. /* Author: Xavier Roche                                         */
  39. /* ------------------------------------------------------------ */
  40.  
  41. // Fichier librairie .h
  42.  
  43. #ifndef HTS_DEFH
  44. #define HTS_DEFH 
  45.  
  46. /* dΘfinitions globales */
  47. #include "htsglobal.h"
  48.  
  49. /* basic net definitions */
  50. #include "htsbasenet.h"
  51.  
  52. /* cookies et auth */
  53. #include "htsbauth.h"
  54.  
  55. // Attention, dΘfinition existante Θgalement dans le shell
  56. // (α modifier avec celle-ci)
  57. #define POSTTOK "?>post"
  58.  
  59. #include <stdio.h>
  60.  
  61. /*
  62. #if HTS_WIN
  63.  #include <winsock.h>
  64.  typedef SOCKET T_SOC;
  65.  typedef struct hostent FAR t_hostent;
  66. #else
  67.  typedef int T_SOC;
  68.  typedef struct hostent t_hostent;
  69. #endif
  70. */
  71.  
  72. /*
  73. #include <ctype.h>
  74. #if HTS_WIN
  75.  #include <winsock.h>
  76.  typedef SOCKET T_SOC;
  77.  // pour read
  78.  #include <io.h>
  79.  // pour FindFirstFile
  80.  #include <winbase.h>
  81. #else
  82.  typedef int T_SOC;
  83.  #define INVALID_SOCKET -1
  84.  #include <netdb.h>
  85.  #include <sys/types.h>
  86.  #include <sys/socket.h>
  87.  #include <netinet/in.h>
  88.  #include <sys/time.h>
  89.  #include <sys/ioctl.h>
  90.  // gethostname & co0
  91.  #include <unistd.h>
  92.  // inet_addr
  93.  #include <arpa/inet.h>
  94.  // pas la peine normalement..
  95. #if HTS_PLATFORM!=3
  96.  #include <sys/filio.h>
  97. #else
  98. #ifndef HTS_DO_NOT_REDEFINE_in_addr_t
  99.  typedef unsigned long in_addr_t;
  100. #endif
  101. #endif
  102.  
  103. #ifndef min
  104.  #define min(a,b) ((a)>(b)?(b):(a))
  105.  #define max(a,b) ((a)>(b)?(a):(b))
  106. #endif
  107. #define Sleep(a) { if (((a)*1000)%1000000) usleep(((a)*1000)%1000000); if (((a)*1000)/1000000) sleep(((a)*1000)/1000000); }
  108. #endif
  109. */
  110.  
  111. /*
  112. #include <stdio.h>
  113. #include <stdlib.h>
  114. #include <string.h>
  115. #include <time.h>
  116. #include <fcntl.h>
  117. // pour utimbuf
  118. #if HTS_PLATFORM!=3
  119. #include <utime.h>
  120. #else
  121. #include <utime.h>
  122. #endif
  123. */
  124.  
  125.  /*
  126. // teste ΘgalitΘ de 2 chars, case insensitive
  127. #define hichar(a) ((((a)>='a') && ((a)<='z')) ? ((a)-('a'-'A')) : (a))
  128. #define streql(a,b) (hichar(a)==hichar(b))
  129.  
  130. // caractΦre maj
  131. #define isUpperLetter(a) ( ((a) >= 'A') && ((a) <= 'Z') )
  132. */
  133. /*
  134. #if HTS_PLATFORM!=3
  135. #ifdef __cplusplus
  136. extern "C" {
  137. #endif
  138. #if HTS_PLATFORM!=2
  139. #if HTS_PLATFORM!=1
  140.  int   open      (const char *, int, ...);
  141. #endif
  142.  //int   read      (int,const char*,int);
  143.  //int   write     (int,char*,int);
  144. #endif
  145. #if HTS_PLATFORM!=1
  146.  int   close     (int);
  147.  void* calloc    (size_t,size_t);
  148.  void* malloc    (size_t);
  149.  void* realloc   (void*,size_t);
  150.  void  free      (void*);
  151. #endif
  152. #if HTS_WIN==0
  153.  void  bzero     (char*,unsigned int);
  154.  void  bcopy     (const char*,char*,unsigned int);
  155.  //int   strcasecmp(const char*,const char*);
  156. #endif
  157.  //int   strlen    (const char *);
  158. #if HTS_WIN
  159.  //int   mkdir     (const char*);
  160. #else
  161.  int   mkdir     (const char*,mode_t);
  162.  int   isdigit   (char);
  163.  int   isalpha   (char);
  164.  int   isalnum   (char);
  165. #endif
  166. #ifdef __cplusplus
  167. }
  168. #endif
  169. #endif
  170. */
  171.  
  172. /*
  173. // conversion Θventuelle / vers antislash
  174. #if HTS_WIN
  175. char* antislash(char* s);
  176. #else
  177. #define antislash(A) (A)
  178. #endif
  179. */
  180.  
  181. /*
  182. #if HTS_WIN
  183. #define  bzero(a,b) memset(a,0,b)
  184. #define  bcopy(a,b,c) memcpy(b,a,c)
  185. #endif
  186.  
  187. // tracer malloc()
  188. #if HTS_TRACE_MALLOC
  189. #define malloct(A)    hts_malloc(A,0)
  190. #define calloct(A,B)  hts_malloc(A,B)
  191. #define freet(A)      hts_free(A)
  192. #define realloct(A,B) hts_realloc(A,B)
  193. void  hts_freeall();
  194. void* hts_malloc    (size_t,size_t);
  195. void  hts_free      (void*);
  196. void* hts_realloc   (void*,size_t);
  197. #else
  198. #define malloct(A)    malloc(A)
  199. #define calloct(A,B)  calloc(A,B)
  200. #define freet(A)      free(A)
  201. #define realloct(A,B) realloc(A,B)
  202. #endif
  203. */
  204.  
  205. #include "htsopt.h"
  206.  
  207. // structure pour paramΦtres supplΘmentaires lors de la requΩte
  208. typedef struct {
  209.   short int user_agent_send;  // user agent (ex: httrack/1.0 [sun])
  210.   short int http11;           // l'en tΩte peut (doit) Ωtre signΘ HTTP/1.1 et non HTTP/1.0
  211.   char user_agent[64];
  212.   char lang_iso[64];
  213.   t_proxy proxy;              // proxy
  214. } htsrequest;
  215.  
  216.  
  217. // structure pour retour d'une connexion/prise d'en tΩte
  218. typedef struct {
  219.   int statuscode;        // status-code, -1=erreur, 200=OK,201=..etc (cf RFC1945)
  220.   short int notmodified; // page ou fichier NON modifiΘ (transfΘrΘ)
  221.   short int is_write;    // sortie sur disque (out) ou en mΘmoire (adr)
  222.   short int is_chunk;    // mode chunk
  223.   char* adr;             // adresse du bloc de mΘmoire, NULL=vide
  224.   FILE* out;             // Θcriture directe sur disque (si is_write=1)
  225.   LLint size;            // taille fichier
  226.   char msg[80];          // message Θventuel si Θchec ("\0"=non prΘcisΘ)
  227.   char contenttype[64];  // content-type ("text/html" par exemple)
  228.   char* location;        // on copie dedans Θventuellement la vΘritable 'location'
  229.   LLint totalsize;       // taille totale α tΘlΘcharger (-1=inconnue)
  230.   short int is_file;     // ce n'est pas une socket mais un descripteur de fichier si 1
  231.   T_SOC soc;             // ID socket
  232.   FILE* fp;              // fichier pour file://
  233.   char lastmodified[64]; // Last-Modified
  234.   char etag[64];         // Etag
  235.   char cdispo[256];      // Content-Disposition coupΘ
  236.   /* */
  237.   htsrequest req;  // paramΦtres pour la requΩte
  238.   /*char digest[32+2];  // digest md5 gΘnΘrΘ par le moteur ("" si non gΘnΘrΘ)*/
  239. } htsblk;
  240.  
  241.  
  242. /* ANCIENNE STURCTURE pour cache 1.0 */
  243. typedef struct {
  244.   int statuscode;  // ANCIENNE STURCTURE - status-code, -1=erreur, 200=OK,201=..etc (cf RFC1945)
  245.   int notmodified; // ANCIENNE STURCTURE - page ou fichier NON modifiΘ (transfΘrΘ)
  246.   int is_write;    // ANCIENNE STURCTURE - sortie sur disque (out) ou en mΘmoire (adr)
  247.   char* adr;       // ANCIENNE STURCTURE - adresse du bloc de mΘmoire, NULL=vide
  248.   FILE* out;       // ANCIENNE STURCTURE - Θcriture directe sur disque (si is_write=1)
  249.   int size;        // ANCIENNE STURCTURE - taille fichier
  250.   char msg[80];    // ANCIENNE STURCTURE - message Θventuel si Θchec ("\0"=non prΘcisΘ)
  251.   char contenttype[64];  // ANCIENNE STURCTURE - content-type ("text/html" par exemple)
  252.   char* location;  // ANCIENNE STURCTURE - on copie dedans Θventuellement la vΘritable 'location'
  253.   int totalsize;   // ANCIENNE STURCTURE - taille totale α tΘlΘcharger (-1=inconnue)
  254.   int is_file;     // ANCIENNE STURCTURE - ce n'est pas une socket mais un descripteur de fichier si 1
  255.   T_SOC soc;       // ANCIENNE STURCTURE - ID socket
  256.   FILE* fp;        // ANCIENNE STURCTURE - fichier pour file://
  257.   t_proxy proxy;   // ANCIENNE STURCTURE - proxy
  258.   int user_agent_send;  // ANCIENNE STURCTURE - user agent (ex: httrack/1.0 [sun])
  259.   char user_agent[64];
  260.   int http11;           // ANCIENNE STURCTURE - l'en tΩte doit Ωtre signΘ HTTP/1.1 et non HTTP/1.0
  261. } OLD_htsblk;
  262. /* fin ANCIENNE STURCTURE pour cache 1.0 */
  263.  
  264. // cache pour le dns, pour Θviterd de faire des gethostbyname sans arrΩt
  265. typedef struct t_dnscache {
  266.   char iadr[1024];
  267.   char host_addr[16];    // 4 octets normalement ()
  268.   int host_length;       // 4 normalement - ==0  alors en cours de rΘsolution
  269.                          //                 ==-1 alors erreur (host n'Θxiste pas)
  270.   struct t_dnscache* n;
  271. } t_dnscache;
  272.  
  273.  
  274. typedef struct {
  275.   LLint HTS_TOTAL_RECV;      // flux entrant reτu
  276.   LLint stat_bytes;          // octets Θcrits sur disque
  277.   int HTS_TOTAL_RECV_STATE;  // status: 0 tout va bien 1: ralentir un peu 2: ralentir 3: beaucoup
  278.   double stat_timestart;     // dΘpart
  279.   double imstat_timestart;   // dΘpart pour calcul instantannΘ
  280.   LLint  istat_bytes;        // calcul pour instantannΘ
  281.   int stat_files;            // nombre de fichiers Θcrits
  282.   int stat_updated_files;    // nombre de fichiers mis α jour
  283.   //
  284.   LLint rate;
  285. } hts_stat_struct;
  286.  
  287.  
  288.  
  289. /*
  290. #ifdef __cplusplus
  291. extern "C" {
  292. #endif
  293. */
  294.  
  295. // fonctions unix/winsock
  296. int hts_read(htsblk* r,char* buff,int size);
  297. void HTS_TOTAL_RECV_CHECK(int var);
  298.  
  299. // fonctions principales
  300. int http_fopen(char* adr,char* fil,htsblk* retour);
  301. int http_xfopen(int mode,int treat,int waitconnect,char* xsend,char* adr,char* fil,htsblk* retour);
  302. int http_sendhead(t_cookie* cookie,int mode,char* xsend,char* adr,char* fil,char* referer_adr,char* referer_fil,htsblk* retour);
  303. htsblk httpget(char* url);
  304. //int newhttp(char* iadr,char* err=NULL);
  305. int newhttp(char* iadr,htsblk* retour,int port,int waitconnect);
  306. HTS_INLINE void deletehttp(htsblk* r);
  307. HTS_INLINE void deletesoc(T_SOC soc);
  308. htsblk http_location(char* adr,char* fil,char* loc);
  309. htsblk http_test(char* adr,char* fil,char* loc);
  310. void http_fread(T_SOC soc,htsblk* retour);
  311. LLint http_fread1(htsblk* r);
  312. void treathead(t_cookie* cookie,char* adr,char* fil,htsblk* retour,char* rcvd);
  313. void treatfirstline(htsblk* retour,char* rcvd);
  314. void infostatuscode(char* msg,int statuscode);
  315.  
  316. // sous-fonctions
  317. htsblk xhttpget(char* adr,char* fil);
  318. htsblk http_gethead(char* adr,char* fil);
  319. LLint http_xfread1(htsblk* r,int bufl);
  320. HTS_INLINE t_hostent* hts_gethostbyname(char* iadr);
  321. t_hostent* _hts_ghbn(t_dnscache* cache,char* iadr,t_hostent* retour);
  322. int ftp_available(void);
  323. #if HTS_DNSCACHE
  324. int hts_dnstest(char* _iadr);
  325. t_dnscache* _hts_cache(void);
  326. int _hts_lockdns(int i);
  327. #endif
  328.  
  329. // outils divers
  330. HTS_INLINE double time_local(void);
  331. HTS_INLINE double mtime_local(void);
  332. void sec2str(char *s,double t);
  333. void qsec2str(char *st,double t);
  334. void time_gmt_rfc822(char* s);
  335. void time_local_rfc822(char* s);
  336. struct tm* convert_time_rfc822(char* s);
  337. int set_filetime(char* file,struct tm* tm_time);
  338. int set_filetime_rfc822(char* file,char* date);
  339. HTS_INLINE void time_rfc822(char* s,struct tm * A);
  340. HTS_INLINE void time_rfc822_local(char* s,struct tm * A);
  341. char* int2bytes(LLint n);
  342. char* int2bytessec(long int n);
  343. char** int2bytes2(LLint n);
  344. HTS_INLINE int sendc(T_SOC soc,char* s);
  345. void finput(int fd,char* s,int max);
  346. int binput(char* buff,char* s,int max);
  347. void linput(FILE* fp,char* s,int max);
  348. void linput_trim(FILE* fp,char* s,int max);
  349. void linput_cpp(FILE* fp,char* s,int max);
  350. void rawlinput(FILE* fp,char* s,int max);
  351. int strfield(const char* f,const char* s);
  352. #define strfield2(f,s) ( (strlen(f)!=strlen(s)) ? 0 : (strfield(f,s)) )
  353. char* strstrcase(char *s,char *o);
  354. int ident_url(char* url,char* adr,char* fil);
  355. void fil_simplifie(char* f);
  356. int ishtml(char* urlfil);
  357. int ishtml_ext(char* a);
  358. int ishttperror(int err);
  359. void guess_httptype(char *s,char *fil);
  360. void get_httptype(char *s,char *fil,int flag);
  361. void give_mimext(char *s,char *st);
  362. int is_knowntype(char *fil);
  363. char* get_ext(char *fil);
  364. int may_unknown(char* st);
  365. char* jump_identification(char*);
  366. HTS_INLINE char* jump_protocol(char* source);
  367. void code64(char* a,char* b);
  368. void unescape_amp(char* s);
  369. void escape_spc_url(char* s);
  370. void escape_in_url(char* s);
  371. void escape_check_url(char* s);
  372. void x_escape_http(char* s,int mode);
  373. HTS_INLINE int ehexh(char c);
  374. char* unescape_http(char* s);
  375. char* antislash_unescaped(char* s);
  376. int ehex(char* s);
  377. char* concat(const char* a,const char* b);
  378. #define copychar(a) concat((a),NULL)
  379. #if HTS_DOSNAME
  380. char* fconcat(char* a,char* b);
  381. char* fconv(char* a);
  382. #else
  383. #define fconv(a) (a)
  384. #define fconcat(a,b) concat(a,b)
  385. #endif
  386. char* fslash(char* a);
  387. char* __fslash(char* a);
  388.  
  389. char* convtolower(char* a);
  390. char* concat(const char* a,const char* b);
  391. void hts_lowcase(char* s);
  392. void hts_replace(char *s,char from,char to);
  393.  
  394. /* Spaces: CR,LF,TAB,FF */
  395. #define  is_space(c)     ( ((c)==' ') || ((c)=='\"') || ((c)==10) || ((c)==13) || ((c)==9) || ((c)==12) || ((c)=='\'') )
  396. #define  is_realspace(c) ( ((c)==' ')                || ((c)==10) || ((c)==13) || ((c)==9) || ((c)==12)                )
  397. //HTS_INLINE int is_space(char);
  398. //HTS_INLINE int is_realspace(char);
  399.  
  400. void cut_path(char* fullpath,char* path,char* pname);
  401. int fexist(char* s);
  402. /*LLint fsize(char* s);    */
  403. int fpsize(FILE* fp);
  404. int fsize(char* s);    
  405.  
  406. // Threads
  407. #if USE_PTHREAD
  408. typedef void* ( *beginthread_type )( void * );
  409. unsigned long _beginthread( beginthread_type start_address, unsigned stack_size, void *arglist );
  410. #endif
  411.  
  412. /*
  413. #ifdef __cplusplus
  414. }
  415. #endif
  416. */
  417.  
  418.  
  419.  
  420. /* variables globales */
  421. //extern LLint HTS_TOTAL_RECV;  // flux entrant reτu
  422. //extern int HTS_TOTAL_RECV_STATE;  // status: 0 tout va bien 1: ralentir un peu 2: ralentir 3: beaucoup
  423. extern hts_stat_struct HTS_STAT;
  424. extern int _DEBUG_HEAD;
  425. extern FILE* ioinfo;
  426.  
  427. /* constantes */
  428. extern const char hts_mime_keep[][32];
  429. extern const char hts_mime[][2][32];
  430. extern const char hts_detect[][32];
  431. extern const char hts_detectURL[][32];
  432. extern const char hts_detectandleave[][32];
  433. extern const char hts_detect_js[][32];
  434.  
  435. #endif
  436.  
  437.  
  438.