GNU libmicrohttpd  0.9.29
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 "connection.h"
#include "memorypool.h"
Include dependency graph for response.c:

Go to the source code of this file.

Macros

#define MHD_NO_DEPRECATION   1
 

Functions

static int add_response_entry (struct MHD_Response *response, enum MHD_ValueKind kind, const char *header, const char *content)
 
_MHD_EXTERN int MHD_add_response_header (struct MHD_Response *response, const char *header, const char *content)
 
_MHD_EXTERN int MHD_add_response_footer (struct MHD_Response *response, const char *footer, const char *content)
 
_MHD_EXTERN int 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)
 
_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)
 
int 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 void free_callback (void *cls)
 
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_fd (size_t size, int fd)
 
_MHD_EXTERN struct MHD_ResponseMHD_create_response_from_fd64 (uint64_t size, int fd)
 
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 int MHD_upgrade_action (struct MHD_UpgradeResponseHandle *urh, enum MHD_UpgradeAction action,...)
 
int MHD_response_execute_upgrade_ (struct MHD_Response *response, struct MHD_Connection *connection)
 
_MHD_EXTERN struct MHD_ResponseMHD_create_response_for_upgrade (MHD_UpgradeHandler upgrade_handler, void *upgrade_handler_cls)
 
_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

Definition in file response.c.

Macro Definition Documentation

◆ MHD_NO_DEPRECATION

#define MHD_NO_DEPRECATION   1

Definition at line 26 of file response.c.

Function Documentation

◆ add_response_entry()

static int 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 63 of file response.c.

References MHD_Response::first_header, MHD_HTTP_Header::header, MHD_HTTP_Header::kind, MHD_NO, MHD_YES, MHD_HTTP_Header::next, NULL, and MHD_HTTP_Header::value.

Referenced by MHD_add_response_footer(), and MHD_add_response_header().

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 339 of file response.c.

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

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 403 of file response.c.

References MHD_Response::fd.

Referenced by MHD_create_response_from_fd_at_offset64().

Here is the caller graph for this function:

◆ MHD_create_response_for_upgrade()

_MHD_EXTERN struct MHD_Response* MHD_create_response_for_upgrade ( MHD_UpgradeHandler  upgrade_handler,
void *  upgrade_handler_cls 
)

Create a response object that can be used for 101 UPGRADE responses, for example to implement WebSockets. After sending the response, control over the data stream is given to the callback (which can then, for example, start some bi-directional communication). If the response is queued for multiple connections, the callback will be called for each connection. The callback will ONLY be called after the response header was successfully passed to the OS; if there are communication errors before, the usual MHD connection error handling code will be performed.

Setting the correct HTTP code (i.e. MHD_HTTP_SWITCHING_PROTOCOLS) and setting correct HTTP headers for the upgrade must be done manually (this way, it is possible to implement most existing WebSocket versions using this API; in fact, this API might be useful for any protocol switch, not just WebSockets). Note that draft-ietf-hybi-thewebsocketprotocol-00 cannot be implemented this way as the header "HTTP/1.1 101 WebSocket Protocol Handshake" cannot be generated; instead, MHD will always produce "HTTP/1.1 101 Switching Protocols" (if the response code 101 is used).

As usual, the response object can be extended with header information and then be used any number of times (as long as the header information is not connection-specific).

Parameters
upgrade_handlerfunction to call with the 'upgraded' socket
upgrade_handler_clsclosure for upgrade_handler
Returns
NULL on error (i.e. invalid arguments, out of memory)

Definition at line 952 of file response.c.

References MHD_add_response_header(), MHD_destroy_response(), MHD_HTTP_HEADER_CONNECTION, MHD_NO, MHD_SIZE_UNKNOWN, MHD_Response::mutex, NULL, MHD_Response::reference_count, MHD_Response::total_size, MHD_Response::upgrade_handler, and MHD_Response::upgrade_handler_cls.

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 1028 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_response_execute_upgrade_()

int MHD_response_execute_upgrade_ ( struct MHD_Response response,
struct MHD_Connection connection 
)

We are done sending the header of a given response to the client. Now it is time to perform the upgrade and hand over the connection to the application.

Parameters
responsethe response that was created for an upgrade
connectionthe specific connection we are upgrading
Returns
MHD_YES on success, MHD_NO on failure (will cause connection to be closed)

Definition at line 708 of file response.c.

References _, _MHD_EXTERN, MHD_Connection::client_context, MHD_UpgradeResponseHandle::connection, MHD_Connection::daemon, DLL_insert, EXTRA_CHECK, MHD_EPOLL_STATE_UNREADY, MHD_get_response_header(), MHD_HTTP_HEADER_UPGRADE, MHD_INVALID_SOCKET, MHD_NO, MHD_PANIC, MHD_pool_allocate(), MHD_pool_get_free(), MHD_SCKT_FD_FITS_FDSET_, MHD_socket_close_chk_, MHD_socket_last_strerr_, MHD_socket_nonblocking_(), MHD_suspend_connection(), MHD_USE_EPOLL, MHD_USE_POLL, MHD_USE_THREAD_PER_CONNECTION, MHD_USE_TLS, MHD_YES, NULL, MHD_Daemon::options, MHD_Connection::pool, MHD_Connection::read_buffer, MHD_Connection::read_buffer_offset, RESERVE_EBUF_SIZE, MHD_Connection::socket_fd, MHD_Response::upgrade_handler, MHD_Response::upgrade_handler_cls, and MHD_Connection::urh.

Referenced by MHD_connection_handle_idle().

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

◆ MHD_set_response_options()

int 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 303 of file response.c.

References MHD_Response::flags, MHD_NO, MHD_RO_END, and MHD_YES.

◆ MHD_upgrade_action()

_MHD_EXTERN int MHD_upgrade_action ( struct MHD_UpgradeResponseHandle urh,
enum MHD_UpgradeAction  action,
  ... 
)

This connection-specific callback is provided by MHD to applications (unusual) during the MHD_UpgradeHandler. It allows applications to perform 'special' actions on the underlying socket from the upgrade.

Parameters
urhthe handle identifying the connection to perform the upgrade action on.
actionwhich action should be performed
...arguments to the action (depends on the action)
Returns
MHD_NO on error, MHD_YES on success

Definition at line 625 of file response.c.

References _, MHD_UpgradeResponseHandle::connection, MHD_Connection::daemon, MHD_Connection::idle_handler, MHD_cleanup_upgraded_connection_(), MHD_connection_close_(), MHD_CONNECTION_CLOSED, MHD_CONNECTION_IN_CLEANUP, MHD_CONNECTION_UPGRADE_CLOSED, MHD_destroy_response(), MHD_INVALID_SOCKET, MHD_NO, MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN, MHD_socket_close_chk_, MHD_UPGRADE_ACTION_CLOSE, MHD_USE_THREAD_PER_CONNECTION, MHD_USE_TLS, MHD_YES, NULL, MHD_Daemon::options, MHD_Connection::response, MHD_Daemon::shutdown, MHD_Connection::socket_fd, MHD_Connection::state, and MHD_UpgradeResponseHandle::was_closed.

Here is the call graph for this function: