/* * Send error message back to a client. */ int send_error(struct mg_connection *conn, int status, const char *reason, const char *fmt, ...) { va_list ap; conn->status_code = status; (void) mg_printf(conn, "HTTP/1.1 %d %s\r\n" "Content-Type: text/html\r\n" "Connection: close\r\n" "\r\n", status, reason); /* Errors 1xx, 204 and 304 MUST NOT send a body */ if(status > 199 && status != 204 && status != 304) { char buf[BUFSIZ]; int len; conn->num_bytes_sent = 0; va_start(ap, fmt); len = mg_vsnprintf(conn, buf, sizeof(buf), fmt, ap); va_end(ap); conn->num_bytes_sent += mg_write(conn, buf, len); cry(conn, "%s", buf); } return(1); }
void mg_send_http_error(struct mg_connection *conn, int status, const char *reason, const char *fmt, ...) { char buf[BUFSIZ]; va_list ap; int len; conn->request_info.status_code = status; buf[0] = '\0'; len = 0; /* Errors 1xx, 204 and 304 MUST NOT send a body */ if (status > 199 && status != 204 && status != 304) { len = mg_snprintf(conn, buf, sizeof(buf), "Error %d: %s", status, reason); cry(conn, "%s", buf); buf[len++] = '\n'; va_start(ap, fmt); len += mg_vsnprintf(conn, buf + len, sizeof(buf) - len, fmt, ap); va_end(ap); } DEBUG_TRACE(("[%s]", buf)); mg_printf(conn, "HTTP/1.1 %d %s\r\n" "Content-Type: text/plain\r\n" "Content-Length: %d\r\n" "Connection: %s\r\n\r\n", status, reason, len, suggest_connection_header(conn)); conn->num_bytes_sent += mg_printf(conn, "%s", buf); }
static int mg_snprintf(char *buf, size_t buflen, const char *fmt, ...) { va_list ap; int n; va_start(ap, fmt); n = mg_vsnprintf(buf, buflen, fmt, ap); va_end(ap); return n; }
int mg_printf(struct mg_connection *conn, const char *fmt, ...) { char buf[BUFSIZ]; int len; va_list ap; va_start(ap, fmt); len = mg_vsnprintf(conn, buf, sizeof(buf), fmt, ap); va_end(ap); return mg_write(conn, buf, (size_t)len); }
int Response::vprintf(const char *fmt, va_list ap) { char buf[BUFSIZE]; int len = mg_vsnprintf(conn, buf, sizeof(buf), fmt, ap); return mg_write(conn, buf, (size_t)len); }