GNU libmicrohttpd 1.0.1
Loading...
Searching...
No Matches
response.c File Reference

Methods for managing response objects. More...

#include "mhd_options.h"
#include "internal.h"
#include "response.h"
#include "mhd_limits.h"
#include "mhd_sockets.h"
#include "mhd_itc.h"
#include "mhd_str.h"
#include "connection.h"
#include "memorypool.h"
#include "mhd_send.h"
#include "mhd_compat.h"
#include "mhd_assert.h"
Include dependency graph for response.c:

Go to the source code of this file.

Macros

#define MHD_NO_DEPRECATION   1
 
#define MHD_FILE_READ_BLOCK_SIZE   4096 /* 4k */
 
#define _MHD_insert_header_first(presponse, phdr)
 
#define _MHD_insert_header_last(presponse, phdr)
 
#define _MHD_remove_header(presponse, phdr)
 

Functions

bool MHD_add_response_entry_no_alloc_ (struct MHD_Response *response, enum MHD_ValueKind kind, char *header, size_t header_len, char *content, size_t content_len)
 
bool MHD_add_response_entry_no_check_ (struct MHD_Response *response, enum MHD_ValueKind kind, const char *header, size_t header_len, const char *content, size_t content_len)
 
static bool add_response_entry_n (struct MHD_Response *response, enum MHD_ValueKind kind, const char *header, size_t header_len, const char *content, size_t content_len)
 
static enum MHD_Result add_response_entry (struct MHD_Response *response, enum MHD_ValueKind kind, const char *header, const char *content)
 
static enum MHD_Result add_response_header_connection (struct MHD_Response *response, const char *value)
 
static enum MHD_Result del_response_header_connection (struct MHD_Response *response, const char *value)
 
_MHD_EXTERN enum MHD_Result MHD_add_response_header (struct MHD_Response *response, const char *header, const char *content)
 
_MHD_EXTERN enum MHD_Result MHD_add_response_footer (struct MHD_Response *response, const char *footer, const char *content)
 
_MHD_EXTERN enum MHD_Result MHD_del_response_header (struct MHD_Response *response, const char *header, const char *content)
 
_MHD_EXTERN int MHD_get_response_headers (struct MHD_Response *response, MHD_KeyValueIterator iterator, void *iterator_cls)
 
_MHD_EXTERN const char * MHD_get_response_header (struct MHD_Response *response, const char *key)
 
struct MHD_HTTP_Res_HeaderMHD_get_response_element_n_ (struct MHD_Response *response, enum MHD_ValueKind kind, const char *key, size_t key_len)
 
bool MHD_check_response_header_token_ci (const struct MHD_Response *response, const char *key, size_t key_len, const char *token, size_t token_len)
 
_MHD_EXTERN struct MHD_ResponseMHD_create_response_from_callback (uint64_t size, size_t block_size, MHD_ContentReaderCallback crc, void *crc_cls, MHD_ContentReaderFreeCallback crfc)
 
_MHD_EXTERN enum MHD_Result MHD_set_response_options (struct MHD_Response *response, enum MHD_ResponseFlags flags,...)
 
static ssize_t file_reader (void *cls, uint64_t pos, char *buf, size_t max)
 
static ssize_t pipe_reader (void *cls, uint64_t pos, char *buf, size_t max)
 
static void free_callback (void *cls)
 
_MHD_EXTERN struct MHD_ResponseMHD_create_response_from_fd_at_offset (size_t size, int fd, off_t offset)
 
_MHD_EXTERN struct MHD_ResponseMHD_create_response_from_fd_at_offset64 (uint64_t size, int fd, uint64_t offset)
 
_MHD_EXTERN struct MHD_ResponseMHD_create_response_from_pipe (int fd)
 
_MHD_EXTERN struct MHD_ResponseMHD_create_response_from_fd (size_t size, int fd)
 
_MHD_EXTERN struct MHD_ResponseMHD_create_response_from_fd64 (uint64_t size, int fd)
 
_MHD_EXTERN struct MHD_ResponseMHD_create_response_from_data (size_t size, void *data, int must_free, int must_copy)
 
_MHD_EXTERN struct MHD_ResponseMHD_create_response_from_buffer (size_t size, void *buffer, enum MHD_ResponseMemoryMode mode)
 
_MHD_EXTERN struct MHD_ResponseMHD_create_response_from_buffer_static (size_t size, const void *buffer)
 
_MHD_EXTERN struct MHD_ResponseMHD_create_response_from_buffer_copy (size_t size, const void *buffer)
 
_MHD_EXTERN struct MHD_ResponseMHD_create_response_from_buffer_with_free_callback (size_t size, void *buffer, MHD_ContentReaderFreeCallback crfc)
 
_MHD_EXTERN struct MHD_ResponseMHD_create_response_from_buffer_with_free_callback_cls (size_t size, const void *buffer, MHD_ContentReaderFreeCallback crfc, void *crfc_cls)
 
_MHD_EXTERN struct MHD_ResponseMHD_create_response_from_iovec (const struct MHD_IoVec *iov, unsigned int iovcnt, MHD_ContentReaderFreeCallback free_cb, void *cls)
 
_MHD_EXTERN struct MHD_ResponseMHD_create_response_empty (enum MHD_ResponseFlags flags)
 
_MHD_EXTERN void MHD_destroy_response (struct MHD_Response *response)
 
void MHD_increment_response_rc (struct MHD_Response *response)
 

Detailed Description

Methods for managing response objects.

Author
Daniel Pittman
Christian Grothoff
Karlson2k (Evgeny Grin)

Definition in file response.c.

Macro Definition Documentation

◆ _MHD_insert_header_first

#define _MHD_insert_header_first ( presponse,
phdr )
Value:
do { \
mhd_assert (NULL == phdr->next); \
mhd_assert (NULL == phdr->prev); \
if (NULL == presponse->first_header) \
{ \
mhd_assert (NULL == presponse->last_header); \
presponse->first_header = phdr; \
presponse->last_header = phdr; \
} \
else \
{ \
mhd_assert (NULL != presponse->last_header); \
presponse->first_header->prev = phdr; \
phdr->next = presponse->first_header; \
presponse->first_header = phdr; \
} \
} while (0)
#define NULL

Insert a new header at the first position of the response

Definition at line 77 of file response.c.

Referenced by add_response_header_connection().

◆ _MHD_insert_header_last

#define _MHD_insert_header_last ( presponse,
phdr )
Value:
do { \
mhd_assert (NULL == phdr->next); \
mhd_assert (NULL == phdr->prev); \
if (NULL == presponse->last_header) \
{ \
mhd_assert (NULL == presponse->first_header); \
presponse->last_header = phdr; \
presponse->first_header = phdr; \
} \
else \
{ \
mhd_assert (NULL != presponse->first_header); \
presponse->last_header->next = phdr; \
phdr->prev = presponse->last_header; \
presponse->last_header = phdr; \
} \
} while (0)

Insert a new header at the last position of the response

Definition at line 98 of file response.c.

Referenced by MHD_add_response_entry_no_alloc_().

◆ _MHD_remove_header

#define _MHD_remove_header ( presponse,
phdr )
Value:
do { \
mhd_assert (NULL != presponse->first_header); \
mhd_assert (NULL != presponse->last_header); \
if (NULL == phdr->prev) \
{ \
mhd_assert (phdr == presponse->first_header); \
presponse->first_header = phdr->next; \
} \
else \
{ \
mhd_assert (phdr != presponse->first_header); \
mhd_assert (phdr == phdr->prev->next); \
phdr->prev->next = phdr->next; \
} \
if (NULL == phdr->next) \
{ \
mhd_assert (phdr == presponse->last_header); \
presponse->last_header = phdr->prev; \
} \
else \
{ \
mhd_assert (phdr != presponse->last_header); \
mhd_assert (phdr == phdr->next->prev); \
phdr->next->prev = phdr->prev; \
} \
} while (0)

Remove a header from the response

Definition at line 120 of file response.c.

Referenced by del_response_header_connection(), MHD_add_response_header(), and MHD_del_response_header().

◆ MHD_FILE_READ_BLOCK_SIZE

#define MHD_FILE_READ_BLOCK_SIZE   4096 /* 4k */

Size of single file read operation for file-backed responses.

Definition at line 70 of file response.c.

Referenced by MHD_create_response_from_fd_at_offset64(), and MHD_create_response_from_pipe().

◆ MHD_NO_DEPRECATION

#define MHD_NO_DEPRECATION   1

Definition at line 28 of file response.c.

Function Documentation

◆ add_response_entry()

static enum MHD_Result add_response_entry ( struct MHD_Response * response,
enum MHD_ValueKind kind,
const char * header,
const char * content )
static

Add a header or footer line to the response.

Parameters
responseresponse to add a header to
kindheader or footer
headerthe header to add
contentvalue to add
Returns
MHD_NO on error (i.e. invalid header or content format).

Definition at line 297 of file response.c.

References add_response_entry_n(), MHD_HTTP_Res_Header::header, MHD_HTTP_Res_Header::kind, MHD_NO, MHD_YES, and NULL.

Here is the call graph for this function:

◆ add_response_entry_n()

static bool add_response_entry_n ( struct MHD_Response * response,
enum MHD_ValueKind kind,
const char * header,
size_t header_len,
const char * content,
size_t content_len )
static

Add a header or footer line to the response.

Parameters
headerthe header to add, does not need to be zero-terminated
header_lenthe length of the header
contentvalue to add, does not need to be zero-terminated
content_lenthe length of the content
Returns
false on error (out-of-memory, invalid header or content), true if succeed

Definition at line 256 of file response.c.

References MHD_HTTP_Res_Header::header, MHD_HTTP_Res_Header::kind, MHD_add_response_entry_no_check_(), and NULL.

Referenced by add_response_entry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_response_header_connection()

static enum MHD_Result add_response_header_connection ( struct MHD_Response * response,
const char * value )
static

Add "Connection:" header to the response with special processing.

"Connection:" header value will be combined with any existing "Connection:" header, "close" token (if any) will be de-duplicated and moved to the first position.

Parameters
responsethe response to add a header to
valuethe value to add
Returns
MHD_NO on error (no memory).

the length of the "Connection" key

< the length of the value

< the length of the existing "Connection" value

< the size of the buffer

< the length of the normalised value

< the temporal buffer

< existing "Connection" header

< the value has "close" token

< existing "Connection" header has "close" token

< position of addition in the buf

< new "Connection" header

Definition at line 328 of file response.c.

References _MHD_insert_header_first, MHD_Response::flags_auto, MHD_HTTP_Res_Header::header, MHD_HTTP_Res_Header::header_size, MHD_HTTP_Res_Header::kind, mhd_assert, MHD_calloc_(), MHD_get_response_element_n_(), MHD_HEADER_KIND, MHD_HTTP_HEADER_CONNECTION, MHD_NO, MHD_RAF_HAS_CONNECTION_CLOSE, MHD_RAF_HAS_CONNECTION_HDR, MHD_STATICSTR_LEN_, MHD_str_remove_token_caseless_(), MHD_str_remove_tokens_caseless_(), MHD_YES, NULL, SSIZE_MAX, MHD_HTTP_Res_Header::value, and MHD_HTTP_Res_Header::value_size.

Referenced by MHD_add_response_header().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ del_response_header_connection()

static enum MHD_Result del_response_header_connection ( struct MHD_Response * response,
const char * value )
static

Remove tokens from "Connection:" header of the response.

Provided tokens will be removed from "Connection:" header value.

Parameters
responsethe response to manipulate "Connection:" header
valuethe tokens to remove
Returns
MHD_NO on error (no headers or tokens found).

< existing "Connection" header

Definition at line 517 of file response.c.

References _MHD_remove_header, MHD_Response::flags_auto, MHD_HTTP_Res_Header::header, MHD_get_response_element_n_(), MHD_HEADER_KIND, MHD_HTTP_HEADER_CONNECTION, MHD_NO, MHD_RAF_HAS_CONNECTION_CLOSE, MHD_RAF_HAS_CONNECTION_HDR, MHD_STATICSTR_LEN_, MHD_str_remove_tokens_caseless_(), MHD_YES, NULL, MHD_HTTP_Res_Header::value, and MHD_HTTP_Res_Header::value_size.

Referenced by MHD_del_response_header().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ file_reader()

static ssize_t file_reader ( void * cls,
uint64_t pos,
char * buf,
size_t max )
static

Given a file descriptor, read data from the file to generate the response.

Parameters
clspointer to the response
posoffset in the file to access
bufwhere to write the data
maxnumber of bytes to write at most
Returns
number of bytes written

Definition at line 1086 of file response.c.

References MHD_Response::fd, MHD_Response::fd_off, INT32_MAX, MHD_CONTENT_READER_END_OF_STREAM, MHD_CONTENT_READER_END_WITH_ERROR, and SSIZE_MAX.

Referenced by MHD_create_response_from_fd_at_offset64().

Here is the caller graph for this function:

◆ free_callback()

static void free_callback ( void * cls)
static

Destroy file reader context. Closes the file descriptor.

Parameters
clspointer to file descriptor

Definition at line 1213 of file response.c.

References MHD_Response::fd.

Referenced by MHD_create_response_from_fd_at_offset64(), and MHD_create_response_from_pipe().

Here is the caller graph for this function:

◆ MHD_add_response_entry_no_alloc_()

bool MHD_add_response_entry_no_alloc_ ( struct MHD_Response * response,
enum MHD_ValueKind kind,
char * header,
size_t header_len,
char * content,
size_t content_len )

Add preallocated strings a header or footer line to the response without checking.

Header/footer strings are not checked and assumed to be correct.

The string must not be statically allocated! The strings must be malloc()'ed and zero terminated. The strings will be free()'ed when the response is destroyed.

Parameters
responseresponse to add a header to
kindheader or footer
headerthe header string to add, must be malloc()'ed and zero-terminated
header_lenthe length of the header
contentthe value string to add, must be malloc()'ed and zero-terminated
content_lenthe length of the content

Definition at line 167 of file response.c.

References _MHD_insert_header_last, MHD_HTTP_Res_Header::header, MHD_HTTP_Res_Header::header_size, MHD_HTTP_Res_Header::kind, mhd_assert, MHD_calloc_(), NULL, MHD_HTTP_Res_Header::value, and MHD_HTTP_Res_Header::value_size.

Referenced by MHD_add_response_entry_no_check_(), queue_auth_required_response3_inner(), and transmit_error_response_len().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MHD_add_response_entry_no_check_()

bool MHD_add_response_entry_no_check_ ( struct MHD_Response * response,
enum MHD_ValueKind kind,
const char * header,
size_t header_len,
const char * content,
size_t content_len )

Add a header or footer line to the response without checking.

It is assumed that parameters are correct.

Parameters
responseresponse to add a header to
kindheader or footer
headerthe header to add, does not need to be zero-terminated
header_lenthe length of the header
contentvalue to add, does not need to be zero-terminated
content_lenthe length of the content
Returns
false on error (like out-of-memory), true if succeed

Definition at line 207 of file response.c.

References MHD_HTTP_Res_Header::header, MHD_HTTP_Res_Header::kind, MHD_add_response_entry_no_alloc_(), mhd_assert, and NULL.

Referenced by add_response_entry_n().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MHD_check_response_header_token_ci()

bool MHD_check_response_header_token_ci ( const struct MHD_Response * response,
const char * key,
size_t key_len,
const char * token,
size_t token_len )

Check whether response header contains particular token.

Token could be surrounded by spaces and tabs and delimited by comma. Case-insensitive match used for header names and tokens.

Parameters
responsethe response to query
keyheader name
key_lenthe length of key, not including optional terminating null-character.
tokenthe token to find
token_lenthe length of token, not including optional terminating null-character.
Returns
true if token is found in specified header, false otherwise

Definition at line 930 of file response.c.

References MHD_Response::first_header, MHD_HTTP_Res_Header::header, MHD_HTTP_Res_Header::header_size, MHD_HTTP_Res_Header::kind, mhd_assert, MHD_HEADER_KIND, MHD_str_equal_caseless_bin_n_(), MHD_str_has_token_caseless_(), MHD_HTTP_Res_Header::next, NULL, and MHD_HTTP_Res_Header::value.

Here is the call graph for this function:

◆ MHD_increment_response_rc()

void MHD_increment_response_rc ( struct MHD_Response * response)

Increments the reference counter for the response.

Parameters
responseobject to modify

Definition at line 2335 of file response.c.

References MHD_mutex_lock_chk_, MHD_mutex_unlock_chk_, MHD_Response::mutex, and MHD_Response::reference_count.

Referenced by MHD_queue_response().

Here is the caller graph for this function:

◆ MHD_set_response_options()

_MHD_EXTERN enum MHD_Result MHD_set_response_options ( struct MHD_Response * response,
enum MHD_ResponseFlags flags,
... )

Set special flags and options for a response.

Parameters
responsethe response to modify
flagsto set for the response
...MHD_RO_END terminated list of options
Returns
MHD_YES on success, MHD_NO on error

Definition at line 1028 of file response.c.

References MHD_Response::flags, MHD_Response::flags_auto, MHD_NO, MHD_RAF_HAS_CONTENT_LENGTH, MHD_RF_HEAD_ONLY_RESPONSE, MHD_RF_INSANITY_HEADER_CONTENT_LENGTH, MHD_RO_END, MHD_YES, and MHD_Response::total_size.

◆ pipe_reader()

static ssize_t pipe_reader ( void * cls,
uint64_t pos,
char * buf,
size_t max )
static

Given a pipe descriptor, read data from the pipe to generate the response.

Parameters
clspointer to the response
posoffset in the pipe to access (ignored)
bufwhere to write the data
maxnumber of bytes to write at most
Returns
number of bytes written

Definition at line 1174 of file response.c.

References MHD_Response::fd, INT_MAX, MHD_CONTENT_READER_END_OF_STREAM, MHD_CONTENT_READER_END_WITH_ERROR, SSIZE_MAX, and UINT_MAX.

Referenced by MHD_create_response_from_pipe().

Here is the caller graph for this function: