0
다음 코드는 HTTP
프로토콜에 응답 헤더를 작성하려고합니다. 왜 코드가 작동하지 않는지에 대해서는 언급하지 않습니다. 기능 빌드 응답 버퍼의 내용, 파일 기술자 소켓에 기록 :C 문자열 작성
void write_fd(struct http_response* response, int client_socket)
{
int length = strlen(response->file_content + MAX_HEADER_LENGTH);
char response_content[length];
response_content[0] = '\0';
printf("-- Descriptor %i, start responding\n", client_socket);
write_fd_resp_line(response, response_content);
//printf("%s\n", response_content); - "HTTP/1.1 GET OK\n"
write_fd_date(response_content);
//printf("%s\n", response_content); - Segmentation Fault
write_fd_server_name(response_content);
write_fd_con_type(response, response_content);
write_fd_doc_content(response, response_content);
int sended = 0;
int content_length = strlen(response_content) + 1;
int n;
while(sended != content_length) {
n = write(client_socket, response_content + sended, content_length - sended);
if(n <= 0) break;
sended += n;
printf("-- Descriptor - %i, sended %i/%i\n", client_socket, sended, content_length);
}
}
을하지만 난 변화하고 있습니다 :
char response_content[length];
기능
char* response_content = malloc(length);
에 작품, 서버 소켓에 응답 내용을 씁니다,하지만 그 후 분할 오류가 발생합니다. 나는 왜 그런지 이해하지 못한다. 패턴
write_fd_*
와
가
기능과 유사합니다
void write_fd_resp_line(http_response* response, char* response_content)
{
char *tmp;
char code_str[4];
tmp = (char*) get_status_code_name(response->code);
snprintf(code_str, 4, "%d", response->code);
strcat(response_content, HTTP_VERSION);
strcat(response_content, " ");
strcat(response_content, code_str);
strcat(response_content, " ");
strcat(response_content, tmp);
strcat(response_content, "\n");
}
이것은 typo'strlen (response-> file_content + MAX_HEADER_LENGTH)입니다. '->'strlen (response-> file_content) + MAX_HEADER_LENGTH; –
@iharob 나는 그것이 OP의 불황의 중요한 근원이라고 다소 의심한다. – WhozCraig
네가 맞아! 나는 그것을 보지 못했다! – kris14an