Dear "patient" mailing list,
I would greatly appreciate it if you could point out to me the bare
minimum I need from the Lynx source code to make the attached file
rep_client.c firewall-capable. I would need to connect to the proxy:
proxy.cli.di.unipi.it at port 8080.
I hope my request won't offend anyone. If it does, please don't get mad at
me! :-)
Regards,
Vieri Di Paola
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/resource.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#define HUGE_STRING_LEN 1024
#define PROGRAM_NAME "nph-blast_report"
/*#define ENGINE_HOST "blast.planet.nlm.nih.gov"*/
#define ENGINE_HOST "www.ncbi.nlm.nih.gov"
#define ENGINE_PORT 80
#define TRUE 1
#define FALSE 0
static int connect_handler(int fd);
static void error_handler(int error);
char buffer[HUGE_STRING_LEN];
char *ReadFileInMemory(FILE *fd, int len, int filter);
main(int argc, char **argv)
{
char *query_string;
query_string = ReadFileInMemory(stdin, 0, FALSE);
if(query_string == NULL) {
printf("No data read. No requests send\n");
exit(1);
}
if(!SendBlastRequest(query_string)) {
printf("Error sending request to BLAST server\n");
exit(1);
}
exit(0);
}
SendBlastRequest(char *query_string)
{
int Port = ENGINE_PORT, Length;
char *blast_engine_host = ENGINE_HOST;
int sock;
char myhostname[128];
struct sockaddr_in server;
struct hostent *hp;
int nbytes, i;
fd_set fds;
if(query_string == NULL || (Length = strlen(query_string)) == 0)
return FALSE;
/* Creating new socket */
memset(&server, '\0', sizeof(server));
if((sock = socket(AF_INET, SOCK_STREAM, 0)) <= 0) {
error_handler(-2);
exit(1);
}
/* Getting address of remote host from DNS */
if((hp = gethostbyname(blast_engine_host)) == NULL) {
error_handler(-3);
printf("\nError in detection of host address\n");
exit(1);
}
memcpy((void *) &server.sin_addr, hp->h_addr, hp->h_length);
server.sin_family = hp->h_addrtype; /* Family is AF_INET */
server.sin_port = htons(Port); /* Server port to connect to */
/* Connecting to the WWW server */
if(connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
error_handler(-4);
printf("Errno is %d\n", errno);
return FALSE;
}
/* Sockets are connected. Now sending request */
sprintf(buffer, "POST /cgi-bin/BLAST/%s HTTP/1.0\n", PROGRAM_NAME);
write(sock, buffer, strlen(buffer));
gethostname(myhostname, sizeof(myhostname));
sprintf(buffer, "User-Agent: SpecialClient from %s\n", myhostname);
write(sock, buffer, strlen(buffer));
sprintf(buffer, "Connection: Keep-Alive\n");
write(sock, buffer, strlen(buffer));
sprintf(buffer, "Content-type: application/x-www-form-urlencoded\n");
write(sock, buffer, strlen(buffer));
sprintf(buffer, "Content-Length: %d\n\n", Length);
write(sock, buffer, strlen(buffer));
write(sock, query_string, Length);
/* Now waiting for data back and printing data on STDOUT */
while (TRUE) {
FD_ZERO(&fds);
FD_SET(sock, &fds);
if ((i = select(sock+1, &fds, NULL, NULL, NULL)) != 0) {
if (FD_ISSET(sock, &fds)) { /* Socket activity */
/* We read from socket and write to stdout */
if((nbytes = read(sock, buffer, HUGE_STRING_LEN)) > 0) {
if(write(1, buffer, nbytes) == EOF)
break;
} else if(nbytes == 0) {
break;
} else {
break;
}
} else {
error_handler(-5);
break; /* This must not happend ... */
}
} else {
error_handler(-6);
break; /* Timeout expired if any ... */
}
}
close(sock);
fflush(stdout);
close(0);
return TRUE;
}
char *ReadFileInMemory(FILE *fd, int len, int filter)
{
int bytes = 0;
char *in_buff;
int new_size = HUGE_STRING_LEN;
int buff_len = 0;
register int i;
if(fd == NULL)
return NULL;
if(len == 0) {
/* initial allocation of memory */
if((in_buff = malloc(HUGE_STRING_LEN)) == NULL) {
printf("Error in allocating memory\n");
return NULL;
}
while ((bytes = fread(in_buff + buff_len, 1,
HUGE_STRING_LEN, fd)) > 0) {
new_size += bytes;
buff_len += bytes;
if ((in_buff = realloc(in_buff, new_size)) == NULL) {
printf("Error in reallocating memory\n");
return NULL;
}
}
in_buff[buff_len] = '\0';
} else {
in_buff = malloc(len+1);
while(buff_len < len) {
if((bytes = fread(in_buff+buff_len, 1, len, fd)) == 0
|| bytes == len) {
buff_len += bytes;
break; /* EOF or done*/
} else if (bytes < 0) {
/* some error setting may be here */
return NULL;
}
buff_len += bytes;
}
in_buff[buff_len] = '\0';
}
/* some filtering of non-printing characters */
if(filter) {
for(i = 0; i < buff_len; i++) {
if (!isprint(in_buff[i]) && !isspace(in_buff[i]))
in_buff[i] = ' ';
}
}
return(in_buff);
}
static void error_handler(int error)
{
printf("Error code is %d<BR>");
fflush(stdout);
return;
}