35 #define HTTPD_HTTP_VERSION "HTTP/1.1 "
36 #define HTTPD_CRLF "\r\n"
72 #define XX(num, str) if(status == num) {return #str;}
132 if (status_str) len += strlen(status_str);
139 (*response)->status = status;
140 (*response)->msg = malloc(len*
sizeof(
char));
141 if (!(*response)->msg) {
143 HPD_LOG_ERROR(context,
"Failed to destroy response (code: %d).", rc);
148 (*response)->context = context;
154 if (status_str) strcat((*response)->msg, status_str);
160 HPD_LOG_ERROR(context,
"Failed to destroy response (code: %d).", rc2);;
188 size_t msg_len = strlen(res->
msg)+strlen(field)+2+strlen(value)+strlen(
HTTPD_CRLF)+1;
190 msg = realloc(res->
msg, msg_len*
sizeof(
char));
194 strcat(res->
msg, field);
195 strcat(res->
msg,
": ");
196 strcat(res->
msg, value);
224 const char *expires,
const char *max_age,
225 const char *domain,
const char *
path,
226 int secure,
int http_only,
227 const char *extension)
236 size_t msg_len = strlen(res->
msg) + 12 + strlen(field) + 1 + strlen(value) + strlen(
HTTPD_CRLF) + 1;
239 if (expires) msg_len += 10 + strlen(expires);
240 if (max_age) msg_len += 10 + strlen(max_age);
241 if (domain) msg_len += 9 + strlen(domain);
242 if (path) msg_len += 7 + strlen(path);
243 if (secure) msg_len += 8;
244 if (http_only) msg_len += 10;
245 if (extension) msg_len += 2 + strlen(extension);
248 msg = realloc(res->
msg, msg_len*
sizeof(
char));
253 strcat(res->
msg,
"Set-Cookie: ");
254 strcat(res->
msg, field);
255 strcat(res->
msg,
"=");
256 strcat(res->
msg, value);
259 strcat(res->
msg,
"; Expires=");
260 strcat(res->
msg, expires);
263 strcat(res->
msg,
"; Max-Age=");
264 strcat(res->
msg, max_age);
267 strcat(res->
msg,
"; Domain=");
268 strcat(res->
msg, domain);
271 strcat(res->
msg,
"; Domain=");
272 strcat(res->
msg, domain);
274 if (secure) strcat(res->
msg,
"; Secure");
275 if (http_only) strcat(res->
msg,
"; HttpOnly");
277 strcat(res->
msg,
"; ");
278 strcat(res->
msg, extension);
hpd_error_t hpd_httpd_response_create(hpd_httpd_response_t **response, hpd_httpd_request_t *req, hpd_status_t status)
Create the reponse and constructs the status line.
enum hpd_status hpd_status_t
#define HPD_LOG_RETURN(CONTEXT, E, FMT,...)
hpd_tcpd_conn_t * conn
Connection to client.
hpd_error_t hpd_httpd_response_sendf(hpd_httpd_response_t *res, const char *fmt,...)
Send response to client.
hpd_error_t hpd_httpd_response_add_cookie(hpd_httpd_response_t *res, const char *field, const char *value, const char *expires, const char *max_age, const char *domain, const char *path, int secure, int http_only, const char *extension)
Add cookie header to response.
hpd_error_t hpd_httpd_response_vsendf(hpd_httpd_response_t *res, const char *fmt, va_list arg)
Send response to client.
#define HTTPD_HTTP_VERSION
hpd_error_t http_request_get_context(hpd_httpd_request_t *req, const hpd_module_t **context)
hpd_error_t hpd_tcpd_conn_get_ip(hpd_tcpd_conn_t *conn, const char **ip)
Get the IP address of the client.
#define HPD_HTTP_STATUS_CODE_MAP(XX)
[hpd_method_t]
hpd_error_t http_request_get_connection(hpd_httpd_request_t *req, hpd_tcpd_conn_t **conn)
Get the connection of a request.
hpd_error_t hpd_httpd_response_destroy(hpd_httpd_response_t *res)
Destroy a hpd_httpd_response.
#define HPD_LOG_VERBOSE(CONTEXT, FMT,...)
hpd_tcpd_conn_t * conn
The connection to send on.
enum hpd_error hpd_error_t
#define HPD_LOG_RETURN_E_ALLOC(CONTEXT)
hpd_error_t hpd_httpd_response_add_header(hpd_httpd_response_t *res, const char *field, const char *value)
Add header to a response.
char * msg
Status/headers to send.
#define HPD_LOG_RETURN_E_NULL(CONTEXT)
All data to represent a connection.
hpd_error_t hpd_tcpd_conn_sendf(hpd_tcpd_conn_t *conn, const char *fmt,...)
Send message on connection.
hpd_error_t hpd_tcpd_conn_close(hpd_tcpd_conn_t *conn)
Close a connection, after the remaining data has been sent.
const hpd_module_t * context
hpd_error_t hpd_tcpd_conn_vsendf(hpd_tcpd_conn_t *conn, const char *fmt, va_list vp)
Send message on connection.
#define HPD_LOG_WARN(CONTEXT, FMT,...)
static char * httpd_status_codes_to_str(hpd_status_t status)
Convert a status code to string.
#define HPD_LOG_ERROR(CONTEXT, FMT,...)