Files
cweb/http/http_response.c
cdricms 658fe48bb8 batman
2024-10-28 12:06:06 +01:00

35 lines
942 B
C

#include "http_content_type.h"
#include "http_response.h"
#include "http_status.h"
#include <stdio.h>
#include <string.h>
bool construct_response(HttpResponse __res, char *out) {
unsigned long length;
if ((length = strlen(__res.body)) > __res.content_length) {
fprintf(stderr, "[ERROR] %s: %lu > %lu",
"The size of the body is greater than what was set in "
"content_length.",
length, __res.content_length);
return false;
}
char status_message[256];
http_status_message(__res.status_code, status_message, 256);
char content_type[256];
http_content_type(__res.content_type, content_type, 256);
sprintf(
out,
"HTTP/1.1 %d %s\r\nContent-Type: %s\r\nContent-Length: %lu\r\n\r\n%s",
__res.status_code, status_message, content_type, __res.content_length,
__res.body);
return true;
}
void http_respond(HttpResponse __res) {
char response[BUFSIZ];
// TODO: Handle return
construct_response(__res, response);
}