I read HTML response from a website using SSL_read() in C. I used HTTP/1.0 in request message. Here is my function:
void Reveive_response(char *resp, SSL *ssl, int body_required) {
char header[1048576];
char body[1048576];
int bytes; // number of bytes actually read
int received = 0; // number of bytes received
int i, line_length;
char c[1];
memset(header, '\0', sizeof(header)); // header assign = '\0'
memset(body, '\0', sizeof(body)); // body assign = '\0'
/***********Try to read byte by byte***********/
i = 0;
line_length = 0; // to check length of each line
do {
bytes = SSL_read(ssl, c, 1); // read 1 byte to c[0]
if (bytes <= 0) break; // read fall or connection closed
if (c[0] == '\n') { // if '\n'
if (line_length == 0) break; // empty line, so end header
else line_length = 0; // else reset for new line
} else if ( c[0] != '\r') line_length++; // inc length
header[i++] = c[0]; // add to header
received += bytes; // count
} while (1);
fprintf(f, "%s\n", header);
//printf("Received...%d\n", received);
printf("\n##### Header DONE #####\n");
strcpy(resp, header); // return via resp
/***********************************************/
/********Then try to read body if needed********/
char *buf = malloc(1024*sizeof(char));
if (body_required) {//read body
do {
memset(buf, '\0', 1024*sizeof(char));
bytes = SSL_read(ssl, buf, 1024);
if (bytes <= 0) break;
//printf("Wait : ");
//getchar();
printf("%s", buf);
strcat(body, buf);
} while (1);
printf("\n##### Body DONE #####\n");
}
fprintf(f, "%s\n", body);
free(buf);
/***********************************************/
fprintf(f, "=============================\n");
}
The header is OK. But when I want to read body, I received this:
I don't know where I went wrong. Any ideas? Please help.