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