I am writing a web server, in which I recieve the data from a client through a WinSock
socket, and parse the data into parts. Then, according to the method found and the resource requested, I want to build a new packet which I will send back to the client.
I have this function, used to build the packet to send nack to client: (BUFFER_SIZE=2048
)
char* build_packet(char *ver, char *code, char *content, long length, long *p_length)
{
char *packet = (char *)malloc(BUFFER_SIZE);
char *len_str = malloc(BUFF_64);
_itoa(length, len_str, 10);
char *content_len = "Content-length: ";
char *content_type = "Content-Type: ";
long len = strlen(ver) + 1;
len += strlen(code) + strlen(HTTP_DELIM);
len += strlen(content_len) + strlen(len_str) + strlen(HTTP_DELIM);
len += strlen(content_type) + strlen(HTTP_HTML_TYPE_TEXT) +strlen(HTTP_DELIM);
len += strlen(content);
*p_length = len;
// This is where problems start...
// ##############################################################
strncat(ver, packet, BUFFER_SIZE - 1); // Trouble starts here
strncat(" ", packet, BUFFER_SIZE - 1);
strncat(code, packet, BUFFER_SIZE - 1);
strncat(HTTP_DELIM, packet, BUFFER_SIZE - 1);
strncat(content_len, packet, BUFFER_SIZE - 1);
strncat(len_str, packet, BUFFER_SIZE - 1);
strncat(HTTP_DELIM, packet, BUFFER_SIZE - 1);
strncat(content_type, packet, BUFFER_SIZE - 1);
strncat(HTTP_HTML_TYPE_TEXT, packet, BUFFER_SIZE - 1);
strncat(HTTP_DELIM, packet, BUFFER_SIZE - 1);
strncat(content, packet, BUFFER_SIZE - 1);
return packet;
}
(Of course I should (and I will) add validation to the strncat so that it will eventually send all data)
My question is: why is the first strncat
call not working and raises a Access Violation error? What am I doing wrong?