웹 사이트의 http 응답 메시지를 수신 할 때 몇 가지 문제가 있습니다. 나는 함수를 호출 할 때 응답 메시지가 완료되지 않은처럼 보인다C에서 OpenSSL을 사용하여 HTTP 응답 메시지 받기
void Reveive_response(char *resp, SSL *ssl) {
const int BUFFER_SIZE = 1024;
char response[1048576];
char *buffer = NULL; // to read from ssl
char *check = (char *) malloc(BUFFER_SIZE*sizeof(char));
int bytes; // number of bytes actually read
int received = 0; // number of bytes received
buffer = (char *) malloc(BUFFER_SIZE*sizeof(char)); // malloc
memset(response, '\0', sizeof(response)); // response
assign = '\0'
do{
memset(buffer, '\0', BUFFER_SIZE); // empty buffer
bytes = SSL_read(ssl, buffer, BUFFER_SIZE);
if (bytes < 0) {
printf("Error: Receive response\n");
exit(0);
}
if (bytes == 0) break;
received += bytes;
printf("Received...%d bytes\n", received);
strncat(response, buffer, bytes); // concat buffer to response
} while (SSL_pending(ssl)); // while pending
response[received] = '\0';
printf("Receive DONE\n");
printf("Response: \n%s\n", response);
free(buffer);
strcpy(resp, response); // return via resp
}
:
이 제 기능입니다. 이처럼 : 내가 다시 함수를 호출하는 경우
Received...1014 bytes
Received...1071 bytes
Receive DONE
Response:
HTTP/1.1 200 OK
<... something else....>
Vary: Accept-Encoding
Content-Type: text/html
Conne
는 다음에, 반환
Received...39 bytes
Receive DONE
Response:
ction: keep-alive
Content-Length: 0
필드 연결이 분리되었다. 왜 내 기능이 모든 응답 메시지를받지 못했습니까? 나는 내부 루프를 사용했다. 내가 어디로 잘못 됐는지 말해줘? 고맙습니다.
감사합니다. 나는 곧 당신에게 대답하려고 노력할 것이다. – thanhdx
안녕하세요, 더 많은 정보를 읽은 후에도 여전히 질문이 있습니다. – thanhdx
- CRLF까지 텍스트 줄을 읽으려면 어떻게해야합니까? SSL_read()는 버퍼를 바이트 단위로 읽습니다. 버퍼에 바이트를 읽고 각 행을 처리하기 위해 파싱해야합니까? 또는 CRLF (아마도 너무 느리게 실행될 때까지 하나씩 바이트를 읽으십시오)? 아니면 다른 것? – thanhdx