목차
Proxy Server
1. Proxy Server란?
2. Proxy Server의 기능
<표차례>
<그림차례>
1. Proxy Server란?
2. Proxy Server의 기능
<표차례>
<그림차례>
본문내용
c, len, nextflag=0;
char buf[4096+1];
char buf2[4096+1];
char method[20];
char proto[20];
char servname[150];
char port[20];
char HTTPver[20];
char newURL[1024];
char *pt;
int servsd;
struct sockaddr_in cli_addr, serv_addr;
struct hostent *hent;
if( (rc = read(clisd, buf, sizeof(buf)-1)) <= 0 ) {
perror("read error!!!"); close(clisd); return -1;
}
buf[rc] = 0;
printf( "=============>\nfrom BROWSER 1 == %d[%s]\n", rc, buf );
fflush(stdout); /**/
pt = strstr( buf, "\n" );
++pt; /* skip '\n' */
len = (char*)&buf[rc]-pt;
memcpy( buf2, pt, len );
buf2[len] = 0;
if( buf[0] == 'P' ) {
/* POST method인데 Content-type: 부분이 없다면 더 읽어야 한다. */
/* 이러한 현상은 netscape3.0일 경우이고 msie3.0에서는 그렇지 않다.*/
if( strstr( buf, "Content-type:" ) == NULL )
nextflag=1; /* POST */
}
/********
** parse URL and connect to WEB server and request newURL
***********************************************************/
/* parse URL */
parseURL( buf, method, proto, servname, port, HTTPver, newURL );
/* connect to WEB server */
servsd = socket(AF_INET, SOCK_STREAM, 0 );
if( servsd < 0 ) { perror("socket to server error!!!"); return -1; }
memset( (char*)&serv_addr, 0, sizeof(serv_addr) );
serv_addr.sin_family = AF_INET;
hent = gethostbyname( servname );
if( hent == NULL ) {
printf( "gethostbyname(%s) Error!!!", servname );
close( servsd );
return -1;
}
memcpy( (char*)&serv_addr.sin_addr, hent->h_addr_list[0], sizeof(struct
in_addr) );
serv_addr.sin_port = htons(atoi(port));
if( connect(servsd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))<0 ) {
perror("connect to WEB server error!!!"); return -1;
}
/* make newURL and send it to WEB server */
if( !nextflag )
sprintf( buf, "%s %s %s \n%s\n\n", method, newURL, HTTPver, buf2 );
else
sprintf( buf, "%s %s %s \n%s", method, newURL, HTTPver, buf2 );
if( write( servsd, buf, strlen(buf) ) < 0 ) {
perror("request newURL to WEB server Error!!!"); return -1;
};
printf( "=======> SND to WEB = [%s]\n", buf ); /**/
if( nextflag ) { /* more to read : NETSCAPE */
if( (rc = read(clisd, buf, sizeof(buf)-1)) <= 0 ) {
perror("read error from BROWSER !!!"); close(servsd); return -1;
}
buf[rc] = 0;
printf( "from BROWSER 2 == %d[%s]\n", rc, buf );
fflush(stdout); /**/
if( write( servsd, buf, rc ) < 0 ) {
perror("send POST data to WEB server Error!!!"); return -1;
};
printf( "=======> SND2 to WEB = [%s]\n", buf ); /**/
}
/*서버에게 요청을 보냈으니 이제는 서버의 응답을 읽어 브라우저로 보내야한다*/
printf("------ Send response to BROWSER .... --------\n");
toClient( servsd, clisd );
return 0;
}
int toClient( servsd, clisd )
int servsd;
int clisd;
{
int i;
int rc=1;
char buf[4096+1];
while( rc > 0 ) {
if( (rc = read(servsd, buf, sizeof(buf)-1)) <= 0 ) {
perror("connection closeed from WEB server!!!\n");
close(servsd);
close(clisd);
return -1;
}
buf[rc] = 0;
printf( "from WEB == %d[%.60s]\n", rc, buf );
fflush(stdout); /**/
if( write( clisd, buf, rc ) < 0 ) {
perror("-----Send to BROWSER Error!!!---------");
};
printf("-----After Send to BROWSER !!! [%.60s]---------\n", buf);
fflush(stdout); /**/
char buf[4096+1];
char buf2[4096+1];
char method[20];
char proto[20];
char servname[150];
char port[20];
char HTTPver[20];
char newURL[1024];
char *pt;
int servsd;
struct sockaddr_in cli_addr, serv_addr;
struct hostent *hent;
if( (rc = read(clisd, buf, sizeof(buf)-1)) <= 0 ) {
perror("read error!!!"); close(clisd); return -1;
}
buf[rc] = 0;
printf( "=============>\nfrom BROWSER 1 == %d[%s]\n", rc, buf );
fflush(stdout); /**/
pt = strstr( buf, "\n" );
++pt; /* skip '\n' */
len = (char*)&buf[rc]-pt;
memcpy( buf2, pt, len );
buf2[len] = 0;
if( buf[0] == 'P' ) {
/* POST method인데 Content-type: 부분이 없다면 더 읽어야 한다. */
/* 이러한 현상은 netscape3.0일 경우이고 msie3.0에서는 그렇지 않다.*/
if( strstr( buf, "Content-type:" ) == NULL )
nextflag=1; /* POST */
}
/********
** parse URL and connect to WEB server and request newURL
***********************************************************/
/* parse URL */
parseURL( buf, method, proto, servname, port, HTTPver, newURL );
/* connect to WEB server */
servsd = socket(AF_INET, SOCK_STREAM, 0 );
if( servsd < 0 ) { perror("socket to server error!!!"); return -1; }
memset( (char*)&serv_addr, 0, sizeof(serv_addr) );
serv_addr.sin_family = AF_INET;
hent = gethostbyname( servname );
if( hent == NULL ) {
printf( "gethostbyname(%s) Error!!!", servname );
close( servsd );
return -1;
}
memcpy( (char*)&serv_addr.sin_addr, hent->h_addr_list[0], sizeof(struct
in_addr) );
serv_addr.sin_port = htons(atoi(port));
if( connect(servsd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))<0 ) {
perror("connect to WEB server error!!!"); return -1;
}
/* make newURL and send it to WEB server */
if( !nextflag )
sprintf( buf, "%s %s %s \n%s\n\n", method, newURL, HTTPver, buf2 );
else
sprintf( buf, "%s %s %s \n%s", method, newURL, HTTPver, buf2 );
if( write( servsd, buf, strlen(buf) ) < 0 ) {
perror("request newURL to WEB server Error!!!"); return -1;
};
printf( "=======> SND to WEB = [%s]\n", buf ); /**/
if( nextflag ) { /* more to read : NETSCAPE */
if( (rc = read(clisd, buf, sizeof(buf)-1)) <= 0 ) {
perror("read error from BROWSER !!!"); close(servsd); return -1;
}
buf[rc] = 0;
printf( "from BROWSER 2 == %d[%s]\n", rc, buf );
fflush(stdout); /**/
if( write( servsd, buf, rc ) < 0 ) {
perror("send POST data to WEB server Error!!!"); return -1;
};
printf( "=======> SND2 to WEB = [%s]\n", buf ); /**/
}
/*서버에게 요청을 보냈으니 이제는 서버의 응답을 읽어 브라우저로 보내야한다*/
printf("------ Send response to BROWSER .... --------\n");
toClient( servsd, clisd );
return 0;
}
int toClient( servsd, clisd )
int servsd;
int clisd;
{
int i;
int rc=1;
char buf[4096+1];
while( rc > 0 ) {
if( (rc = read(servsd, buf, sizeof(buf)-1)) <= 0 ) {
perror("connection closeed from WEB server!!!\n");
close(servsd);
close(clisd);
return -1;
}
buf[rc] = 0;
printf( "from WEB == %d[%.60s]\n", rc, buf );
fflush(stdout); /**/
if( write( clisd, buf, rc ) < 0 ) {
perror("-----Send to BROWSER Error!!!---------");
};
printf("-----After Send to BROWSER !!! [%.60s]---------\n", buf);
fflush(stdout); /**/