You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.3 KiB
39 lines
1.3 KiB
3 years ago
|
#include "response.h"
|
||
|
|
||
|
Response* newResponse(){
|
||
|
Response *response = malloc( sizeof( Response ) );
|
||
|
memset(response, 0, sizeof(Response));
|
||
|
response->headers = malloc( sizeof( HeaderList ) );
|
||
|
response->headers->header = newHeader("Content-Length: 0");
|
||
|
response->headers->next = NULL;
|
||
|
addHeader(response->headers, "Content-Type: text/plain");
|
||
|
response->statusCode = 200;
|
||
|
response->statusMessage = "OK";
|
||
|
response->version = 1.1;
|
||
|
return response;
|
||
|
}
|
||
|
|
||
|
char* responseToString( Response *rsp ){
|
||
|
// HTTP/x.x xxx OK \r\n
|
||
|
int fullLength = 13 + strlen(rsp->statusMessage) + 2 +
|
||
|
// Headers \r\n\r\n
|
||
|
headerListCharLength(rsp->headers) + 4 + strlen(rsp->body);
|
||
|
char retString[fullLength];
|
||
|
|
||
|
sprintf(retString, "HTTP/%.1f %3d %s\r\n%s\r\n%s", rsp->version, rsp->statusCode,
|
||
|
rsp->statusMessage,headersToString(rsp->headers),rsp->body);
|
||
|
return strdup(retString);
|
||
|
}
|
||
|
|
||
|
void responseSetBody(Response *rsp, char *string, bool updateContentLength){
|
||
|
rsp->body = string;
|
||
|
|
||
|
|
||
|
if ( updateContentLength ){
|
||
|
Header *contentLengthHeader = getHeader(rsp->headers, "content-length");
|
||
|
char *value = malloc(sizeof(char) * 20);
|
||
|
sprintf(value, "%lu", strlen(string) );
|
||
|
contentLengthHeader->value = strdup(value);
|
||
|
}
|
||
|
}
|