As an addition to my previous post on Creating a web server in pure C, I'm having some trouble with the Sending function. Here's two code snippets:
int Send(char *message)
{
int length, bytes_sent;
length = strlen(message);
bytes_sent = send(connecting_socket, message, length, 0);
return bytes_sent;
}
This code sends void * to the current socket. Works like a charm!
Now here comes the SendHTML
void SendHTML(char *Status_code, char *Content_Type, char *HTML)
{
char *head = "\r\nHTTP/1.1 ";
char *content_head = "\r\nContent-Type: ";
char *server_head = "\r\nServer: PT06";
char *length_head = "\r\nContent-Length: ";
char *date_head = "\r\nDate: ";
char *newline = "\r\n";
char Content_Length[100];
int content_length = strlen(HTML);
sprintf(Content_Length, "%i", content_length);
char *message = malloc((
strlen(head) +
strlen(content_head) +
strlen(server_head) +
strlen(length_head) +
strlen(date_head) +
strlen(newline) +
strlen(Status_code) +
strlen(Content_Type) +
strlen(Content_Length) +
content_length +
sizeof(char)) * 2);
if ( message != NULL )
{
time_t rawtime;
time ( &rawtime );
strcpy(message, head);
strcat(message, Status_code);
strcat(message, content_head);
strcat(message, Content_Type);
strcat(message, server_head);
strcat(message, length_head);
strcat(message, Content_Length);
strcat(message, date_head);
strcat(message, (char*)ctime(&rawtime));
strcat(message, newline);
strcat(message, HTML);
Send(message);
free(message);
}
}
If I were to add
Send("Oh end of HTML Sending eh?");
after Send(message)
and before free(message)
, this isn't sent to the browser?
I figured this might be a HTTP 1.1 issue, does the RFC say that i can only do one Send? Does the browser Close the connection after it receives the first message?
How do I solve this so I could do the following:
SendHTML("200 OK", "text/plain", "HAaaaii!!");
Send("lolwut?");
This should result in the Browser showing:
HAaaaii!!lolwut?