I'm doing an exercise about Proxy Server. I have some problems when caching in Proxy Server. The following source code is used when the server sends a response to proxy and proxy forward it to the client. It's fine.
//that code is ok
fstream File;
while (P->isClientClose == FALSE && P->isServerClose == FALSE){
memset(Data, 0, 1024);
int length = recv(*(P->SERVER), Data, sizeof(Data), 0);
if (length <= 0)
break;
length = send(*(P->CLIENT), Data, length, 0);
if (length <= 0)
break;
}
But when I try to write HTTP response to a file and then read all characters from file to send to the client, I have a problem. browser said: ERR_CONTENT_DECODING_FAILED
I'm testing how does proxy cache work, but I can't understand where is error. Even when i create a string Temp(Data), and use send(*(P->CLIENT), Temp.c_str(), length, 0), client still said that error. Please help me. :D
//that code is error
fstream File;
while (P->isClientClose == FALSE && P->isServerClose == FALSE){
memset(Data, 0, 1024);
int length = recv(*(P->SERVER), Data, sizeof(Data), 0);
if (length <= 0)
break;
File.open("test.dat", ios::out|ios::binary);
File << Data;
File.close();
File.open("test.dat", ios::in|ios::ate|ios::binary);
ifstream::pos_type pos = File.tellg();
int size = pos;
cout << "size: " << size << endl;
char *pChars = new char[size+1]{};
File.seekg(0, ios::beg);
File.read(pChars, size);
File.close();
length = send(*(P->CLIENT), pChars, length, 0);
delete[]pChars;
if (length <= 0)
break;
}