libexpress 0.0.1
Express Server Framework in C
Loading...
Searching...
No Matches
Data Structures | Macros | Functions
res.h File Reference

This contains all the response related material. More...

#include <libhmap/hmap.h>
#include <stdbool.h>

Go to the source code of this file.

Data Structures

struct  res_t
 Response being sent to the client. More...
 

Macros

#define __BASE__   ""
 Base from where files are looked up.
 

Functions

void status_codes_init (void)
 Initialize the status_codes hmap. More...
 
void mime_init (void)
 Initialize the mime_types hmap. More...
 
void res_send (res_t *res)
 Send response to client. More...
 
void res_send_file (res_t *res, const char *path)
 Send a file to the client. More...
 
void set_res_body (res_t *res, char *body)
 Set the body of the response. More...
 
void set_res_status (res_t *res, u_int16_t status)
 Set the status code of the response. More...
 
void set_res_header (res_t *res, const char *header, char *value)
 Set a particular header of the response. More...
 
char * get_res_header (res_t *res, const char *header)
 Get the value of a particular header of the response. More...
 

Detailed Description

This contains all the response related material.

Author
Saurav Pal (resyfer)
Bug:
No known bugs

Function Documentation

◆ get_res_header()

char * get_res_header ( res_t res,
const char *  header 
)

Get the value of a particular header of the response.

Parameters
resResponse
headerHeader name
Returns
char* Value of header in the response.

◆ mime_init()

void mime_init ( void  )

Initialize the mime_types hmap.

The mime_types is initialized to contain all of the popular extensions and their mime-types.

◆ res_send()

void res_send ( res_t res)

Send response to client.

This sends the response to the client (gets the client ID through res->client) and sets the res->sent to true. It prevents sending a response if res->sent is already true.

Parameters
resResponse

◆ res_send_file()

void res_send_file ( res_t res,
const char *  path 
)

Send a file to the client.

It sends the file at the specified path to the client. The usage can be like the following

#undef BASE #define BASE "/home/user/server"

res_send_file(res, BASE "/index.html");

Parameters
resResponse
pathAbsolute path of the file.

Malloc gives error on modifying the pointer itself

◆ set_res_body()

void set_res_body ( res_t res,
char *  body 
)

Set the body of the response.

Parameters
resResponse
bodyBody to send to client.

◆ set_res_header()

void set_res_header ( res_t res,
const char *  header,
char *  value 
)

Set a particular header of the response.

It sets a header to the given value in the response sent by the client and any case of setting it multiple times will lead to the latest one being considered.

Parameters
resResponse
headerHeader name
valueValue

◆ set_res_status()

void set_res_status ( res_t res,
u_int16_t  status 
)

Set the status code of the response.

Parameters
resResponse
statusStatus Code for HTTP Response

◆ status_codes_init()

void status_codes_init ( void  )

Initialize the status_codes hmap.

The status_codes is initialized to contain all of the status codes as well as their message.