@ -2,13 +2,14 @@
@@ -2,13 +2,14 @@
/* Copyright 2022 Ivan Polyakov */
# include "response.h"
# include <assert.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
static size_t calc_res_status_sz ( const rpd_res * res ) ;
static size_t calc_res_status_len ( const rpd_res * res ) ;
static size_t calc_res_headers_sz ( const rpd_keyval * res ) ;
static size_t calc_res_headers_len ( const rpd_keyval * res ) ;
void rpd_res_init ( rpd_res * dest )
{
@ -20,12 +21,12 @@ void rpd_res_init(rpd_res *dest)
@@ -20,12 +21,12 @@ void rpd_res_init(rpd_res *dest)
int rpd_res_headers_str ( char * * dest , const rpd_res * src )
{
size_t size , i = 0 ;
size_t len , i = 0 ;
char * ptr ;
size = calc_res_headers_sz ( & src - > headers ) ;
len = calc_res_headers_len ( & src - > headers ) ;
* dest = ( char * ) malloc ( sizeof ( char ) * size ) ;
* dest = ( char * ) malloc ( sizeof ( char ) * ( len + 1 ) ) ;
if ( ! * dest ) {
perror ( " malloc " ) ;
return 1 ;
@ -46,53 +47,55 @@ int rpd_res_headers_str(char **dest, const rpd_res *src)
@@ -46,53 +47,55 @@ int rpd_res_headers_str(char **dest, const rpd_res *src)
int rpd_res_str ( char * * dest , const rpd_res * res )
{
size_t headers_size , size ;
size_t headers_len , len ;
char * ptr , * headers ;
size = headers_size = calc_res_headers_sz ( & res - > headers ) ;
size + = calc_res_status_sz ( res ) ;
len = headers_len = calc_res_headers_len ( & res - > headers ) ;
len + = calc_res_status_len ( res ) ;
len + = 2 ; /* CRLF */
if ( res - > body )
size + = 2 + strlen ( res - > body ) ;
len + = strlen ( res - > body ) ;
* dest = ( char * ) malloc ( sizeof ( char ) * size ) ;
* dest = ( char * ) malloc ( sizeof ( char ) * ( len + 1 ) ) ;
if ( ! * dest ) {
perror ( " malloc " ) ;
return 1 ;
}
/* header */
/* Status header */
ptr = * dest ;
ptr + = sprintf ( ptr , " Status: %d \r \n " , res - > status ) ;
/* Headers */
if ( rpd_res_headers_str ( & headers , res ) ) {
return 1 ;
}
memcpy ( ptr , headers , headers_size ) ;
memcpy ( ptr , headers , headers_len ) ;
free ( headers ) ;
ptr + = headers_size ;
ptr + = headers_len ;
/* CRLF */
memcpy ( ptr , " \r \n " , 2 ) ;
ptr + = 2 ;
/* body */
/* Content */
if ( res - > body ) {
int bodylen = strlen ( res - > body ) ;
memcpy ( ptr , res - > body , bodylen ) ;
ptr + = bodylen ;
}
( * dest ) [ len ] = * ptr = ' \0 ' ;
return 0 ;
}
static size_t calc_res_status_sz ( const rpd_res * res )
static size_t calc_res_status_len ( const rpd_res * res )
{
size_t size = 0 ;
size + = strlen ( " Status: \r \n " ) ;
size + = 3 ; /* plus status code */
return size ;
return strlen ( " Status: 000 \r \n " ) ;
}
static size_t calc_res_headers_sz ( const rpd_keyval * headers )
static size_t calc_res_headers_len ( const rpd_keyval * headers )
{
size_t size = 0 , i = 0 ;
@ -116,7 +119,11 @@ void rpd_res_cleanup(rpd_res *res)
@@ -116,7 +119,11 @@ void rpd_res_cleanup(rpd_res *res)
res - > body = NULL ;
}
if ( res - > headers . capacity ) {
rpd_keyval_cleanup ( & res - > headers ) ;
}
rpd_keyval_cleanup ( & res - > headers ) ;
}
void rpd_res_free ( rpd_res * res )
{
rpd_res_cleanup ( res ) ;
rpd_keyval_free ( & res - > headers ) ;
}