Salve a tutti avrei un problema da sottoporre:
devo realizzare un semplice proxy HTTP/1.0 per scopi didattici,in C UNIX e
con le librerie socket di berkeley(quelle classiche)
e ho un problema con il codice che ho scritto:
#include <netdb.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
char* proxy(char* request){
int n;
int sockfd, portno;
struct sockaddr_in serv_addr;
struct hostent *server;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket",-1);
server = gethostbyname("127.0.0.1");
if (server == NULL) {
error("ERROR, no such host\n",-1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(80);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting",-1);
char* response;
response=calloc(40000000,sizeof(char));
if(0)
printf("\nRequest from host: %s",request);
FullWrite(sockfd,request,strlen(request));
FullRead(sockfd,response,39999999);
printf("HTTP response received from server: \n%s",response);
return response;
}
in finale response deve contenere l'header della risposta ottenuta dal server e il content della response,
ovvero il corpo della risorsa richiesta.
Per pagine html funziona benissimo, ma se una pagina ottenuta come response contiene delle immagini,
il browser invia naturalmente per ognuna di esse una request dell'immagine al server,e
le response relative a queste richiestye non vengono ricevute in maniera esatta,
come si vede nello scambio request-response qui sotto.
//----------------------inizio comunicazione
GET http://localhost/wsdl_files/w3c_home.png HTTP/1.0
Host: localhost
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.10) Gecko/20050715 Firefox/1.0.6 SUSE/1.0.6-16
Accept: image/png,*/*;q=0.5
Accept-Language: it,it-it;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: close
Proxy-Connection: close
Referer: http://localhost/wsdl.htm
HTTP/1.1 200 OK
Date: Wed, 06 Sep 2006 13:39:43 GMT
Server: Apache/1.3.37 (Unix)
Last-Modified: Sat, 02 Sep 2006 18:00:13 GMT
ETag: "20ab2-790-44f9c6ad"
Accept-Ranges: bytes
Content-Length: 1936
Connection: close
Content-Type: image/png
PNG
â–’
//-------------fine comunicazione
come si nota del corpo del file png vengono trasmessi solo pochi byte.
Lo stesso succede utilizzando altri formati.
Grazie anticipate a chi mi volesse aiutare.