Я пытаюсь реализовать прокси.
Я могу получить resposne с сервера, но возникают проблемы с отправкой ответа клиенту.
Для первого полученного ответа он содержит заголовок и часть тела сообщения.
Мне нужно изменить заголовок, поэтому мне пришлось разделить заголовок и тело сообщения.
Что касается остальных пакетов, я считаю, что заголовок для изменения отсутствует, поэтому просто отправьте их напрямую.
//receive response from server
char res_buf[1024];
char* res_buffer = (char*)malloc(sizeof(res_buf));
char* res_line = (char*)malloc(sizeof(res_buf));
int packet=0;
while(1){
char response[1024];
int n = recv(connfd_to_server, res_buf, 1024,0);
strcpy(res_buffer, res_buf);
if (n <=0){
printf("will break");
break;
}
else{
if(packet == 0){ //header modification
res_line = strtok(res_buf, "\r\n");
for(int i = 0; i<=12; i++){
if(i==10){ //add this in the 11th line
strcat(response, "Via:1.1\r\n");
}
else{
strcat(response, res_line);
strcat(response, "\r\n");//everyline ends with'\r\n'
}
res_line = strtok(NULL, "\r\n");
}
//printf("response header is:\n%s\n", response);
//Here problem comes!
**//Here I try to copy messageBody to response, but failed**
char* messageBody = (char*)malloc(1024);
char* mess_temp = (char*)malloc(1024);
mess_temp = strtok(res_buffer,"\r\n");
for(int j = 0; j<=11; j++){
mess_temp = strtok(NULL,"\r\n");
//here, ignore header content(first 12lines)
}
mess_temp = strtok(NULL,"");
printf("mess_temp last is%s\n", mess_temp);
memcpy(messageBody, mess_temp,1024);//copy messageBody
strcat(response, messageBody);
printf("response is:\n%s\n", response);
send(connfd_to_client, response, 1024, 0);
packet++;**
}
else{//there is no header to be modified in rest packet,just send
send(connfd_to_client, res_buf, n, 0);
}
}
}
Я использую memcpy () для копирования байта тела сообщения за байтом, учитывая, что в теле сообщения может быть NULL.
Вот результат
И на веб-странице написано "ошибка кодирования контента".
Похоже, что memcoy () не скопировал тело сообщения успешно?
Я не уверен, что попробовать сейчас. Не могли бы вы помочь мне
Большое спасибо заранее