1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/* ------------------------------------------------------------ */
/*
HTTrack Website Copier, Offline Browser for Windows and Unix
Copyright (C) Xavier Roche and other contributors
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Important notes:
- We hereby ask people using this source NOT to use it in purpose of grabbing
emails addresses, or collecting any other private information on persons.
This would disgrace our work, and spoil the many hours we spent on it.
Please visit our Website: http://www.httrack.com
*/
/* ------------------------------------------------------------ */
/* File: URL catch .h */
/* Author: Xavier Roche */
/* ------------------------------------------------------------ */
/* Internal engine bytecode */
#define HTS_INTERNAL_BYTECODE
// Fichier intercepteur d'URL .c
/* specific definitions */
/* specific definitions */
#include "htsbase.h"
#include "htsnet.h"
#include "htslib.h"
#ifndef _WIN32_WCE
#include <fcntl.h>
#endif
#ifdef _WIN32
#else
#include <arpa/inet.h>
#endif
/* END specific definitions */
/* dfinitions globales */
#include "htsglobal.h"
/* htslib */
/*#include "htslib.h"*/
/* catch url */
#include "htscatchurl.h"
// URL Link catcher
// 0- Init the URL catcher with standard port
// catch_url_init(&port,&return_host);
HTSEXT_API T_SOC catch_url_init_std(int* port_prox,char* adr_prox) {
T_SOC soc;
int try_to_listen_to[]={8080,3128,80,81,82,8081,3129,31337,0,-1};
int i=0;
do {
soc=catch_url_init(&try_to_listen_to[i],adr_prox);
*port_prox=try_to_listen_to[i];
i++;
} while( (soc == INVALID_SOCKET) && (try_to_listen_to[i]>=0));
return soc;
}
// 1- Init the URL catcher
// catch_url_init(&port,&return_host);
HTSEXT_API T_SOC catch_url_init(int* port,char* adr) {
T_SOC soc = INVALID_SOCKET;
char h_loc[256+2];
if (gethostname(h_loc,256)==0) { // host name
SOCaddr server;
int server_size=sizeof(server);
t_hostent* hp_loc;
t_fullhostent buffer;
// effacer structure
memset(&server, 0, sizeof(server));
if ( (hp_loc=vxgethostbyname(h_loc, &buffer)) ) { // notre host
// copie adresse
SOCaddr_copyaddr(server, server_size, hp_loc->h_addr_list[0], hp_loc->h_length);
if ( (soc = (T_SOC) socket(SOCaddr_sinfamily(server), SOCK_STREAM, 0)) != INVALID_SOCKET) {
SOCaddr_initport(server, *port);
if ( bind(soc,(struct sockaddr*) &server,server_size) == 0 ) {
SOCaddr server2;
int len;
len=sizeof(server2);
// effacer structure
memset(&server2, 0, sizeof(server2));
if (getsockname(soc,(struct sockaddr*) &server2,&len) == 0) {
*port=ntohs(SOCaddr_sinport(server)); // rcuprer port
if (listen(soc,10)>=0) { // au pif le 10
SOCaddr_inetntoa(adr, 128, server2, len);
} else {
#ifdef _WIN32
closesocket(soc);
#else
close(soc);
#endif
soc=INVALID_SOCKET;
}
} else {
#ifdef _WIN32
closesocket(soc);
#else
close(soc);
#endif
soc=INVALID_SOCKET;
}
} else {
#ifdef _WIN32
closesocket(soc);
#else
close(soc);
#endif
soc=INVALID_SOCKET;
}
}
}
}
return soc;
}
// 2 - Wait for URL
// catch_url
// returns 0 if error
// url: buffer where URL must be stored - or ip:port in case of failure
// data: 32Kb
HTSEXT_API int catch_url(T_SOC soc,char* url,char* method,char* data) {
int retour=0;
// connexion (accept)
if (soc != INVALID_SOCKET) {
T_SOC soc2;
while ( (soc2 = (T_SOC) accept(soc, NULL, NULL)) == INVALID_SOCKET);
/*
#ifdef _WIN32
closesocket(soc);
#else
close(soc);
#endif
*/
soc = soc2;
/* INFOS */
{
SOCaddr server2;
int len;
len=sizeof(server2);
// effacer structure
memset(&server2, 0, sizeof(server2));
if (getpeername(soc,(struct sockaddr*) &server2,&len) == 0) {
char dot[256+2];
SOCaddr_inetntoa(dot, 256, server2, sizeof(server2));
sprintf(url,"%s:%d", dot, htons(SOCaddr_sinport(server2)));
}
}
/* INFOS */
// rception
if (soc != INVALID_SOCKET) {
char line[1000];
char protocol[256];
line[0]=protocol[0]='\0';
//
socinput(soc,line,1000);
if (strnotempty(line)) {
if (sscanf(line,"%s %s %s",method,url,protocol) == 3) {
char BIGSTK url_adr[HTS_URLMAXSIZE*2];
char BIGSTK url_fil[HTS_URLMAXSIZE*2];
// mthode en majuscule
int i,r=0;
url_adr[0]=url_fil[0]='\0';
//
for(i=0;i<(int) strlen(method);i++) {
if ((method[i]>='a') && (method[i]<='z'))
method[i]-=('a'-'A');
}
// adresse du lien
if (ident_url_absolute(url,url_adr,url_fil)>=0) {
// Traitement des en-ttes
char BIGSTK loc[HTS_URLMAXSIZE*2];
htsblk blkretour;
memset(&blkretour, 0, sizeof(htsblk)); // effacer
blkretour.location=loc; // si non nul, contiendra l'adresse vritable en cas de moved xx
// Lire en ttes restants
sprintf(data,"%s %s %s\r\n",method,url_fil,protocol);
while(strnotempty(line)) {
socinput(soc,line,1000);
treathead(NULL,NULL,NULL,&blkretour,line); // traiter
strcatbuff(data,line);
strcatbuff(data,"\r\n");
}
// CR/LF final de l'en tte inutile car dja plac via la ligne vide juste au dessus
//strcatbuff(data,"\r\n");
if (blkretour.totalsize>0) {
int len=(int)min(blkretour.totalsize,32000);
int pos = (int) strlen(data);
// Copier le reste (post ventuel)
while((len>0) && ((r=recv(soc,(char*) data+pos,len,0))>0) ) {
pos+=r;
len-=r;
data[pos]='\0'; // terminer par NULL
}
}
// Envoyer page
sprintf(line,CATCH_RESPONSE);
send(soc,line,(int)strlen(line),0);
// OK!
retour=1;
}
}
} // sinon erreur
}
}
if (soc != INVALID_SOCKET) {
#ifdef _WIN32
closesocket(soc);
/*
WSACleanup();
*/
#else
close(soc);
#endif
}
return retour;
}
// Lecture de ligne sur socket
void socinput(T_SOC soc,char* s,int max) {
int c;
int j=0;
do {
unsigned char b;
if (recv(soc,(char*) &b,1,0)==1) {
c=b;
switch(c) {
case 13: break; // sauter CR
case 10: c=-1; break;
case 9: case 12: break; // sauter ces caractres
default: s[j++]=(char) c; break;
}
} else
c=EOF;
} while((c!=-1) && (c!=EOF) && (j<(max-1)));
s[j++]='\0';
}
|