summaryrefslogtreecommitdiff
path: root/src/journal
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2014-03-16 20:05:50 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2014-03-17 01:55:48 -0400
commite7216d112aff3ba4ce196db95b86d77d5a1b234e (patch)
tree26b7e09325919ebff2cb06396250e3c4e7ea2982 /src/journal
parentcc64d0175a3c2c974709e9962c00fbe04d74c43f (diff)
microhttpd-util: use static buffer for static messages
Most of the messages we send do not require a allocating and freeing a buffer, to optimize this by using const strings. Also, rename respond_error to mhd_respond*, since it is used not only for errors. Make use of information from printf to avoid one extra call to strlen.
Diffstat (limited to 'src/journal')
-rw-r--r--src/journal/journal-gatewayd.c38
-rw-r--r--src/journal/journal-remote.c26
-rw-r--r--src/journal/microhttpd-util.c66
-rw-r--r--src/journal/microhttpd-util.h16
4 files changed, 78 insertions, 68 deletions
diff --git a/src/journal/journal-gatewayd.c b/src/journal/journal-gatewayd.c
index d47b27ef7f..9a88676242 100644
--- a/src/journal/journal-gatewayd.c
+++ b/src/journal/journal-gatewayd.c
@@ -450,20 +450,20 @@ static int request_handler_entries(
r = open_journal(m);
if (r < 0)
- return respond_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to open journal: %s\n", strerror(-r));
+ return mhd_respondf(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to open journal: %s\n", strerror(-r));
if (request_parse_accept(m, connection) < 0)
- return respond_error(connection, MHD_HTTP_BAD_REQUEST, "Failed to parse Accept header.\n");
+ return mhd_respond(connection, MHD_HTTP_BAD_REQUEST, "Failed to parse Accept header.\n");
if (request_parse_range(m, connection) < 0)
- return respond_error(connection, MHD_HTTP_BAD_REQUEST, "Failed to parse Range header.\n");
+ return mhd_respond(connection, MHD_HTTP_BAD_REQUEST, "Failed to parse Range header.\n");
if (request_parse_arguments(m, connection) < 0)
- return respond_error(connection, MHD_HTTP_BAD_REQUEST, "Failed to parse URL arguments.\n");
+ return mhd_respond(connection, MHD_HTTP_BAD_REQUEST, "Failed to parse URL arguments.\n");
if (m->discrete) {
if (!m->cursor)
- return respond_error(connection, MHD_HTTP_BAD_REQUEST, "Discrete seeks require a cursor specification.\n");
+ return mhd_respond(connection, MHD_HTTP_BAD_REQUEST, "Discrete seeks require a cursor specification.\n");
m->n_entries = 1;
m->n_entries_set = true;
@@ -476,7 +476,7 @@ static int request_handler_entries(
else if (m->n_skip < 0)
r = sd_journal_seek_tail(m->journal);
if (r < 0)
- return respond_error(connection, MHD_HTTP_BAD_REQUEST, "Failed to seek in journal.\n");
+ return mhd_respond(connection, MHD_HTTP_BAD_REQUEST, "Failed to seek in journal.\n");
response = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, 4*1024, request_reader_entries, m, NULL);
if (!response)
@@ -612,14 +612,14 @@ static int request_handler_fields(
r = open_journal(m);
if (r < 0)
- return respond_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to open journal: %s\n", strerror(-r));
+ return mhd_respondf(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to open journal: %s\n", strerror(-r));
if (request_parse_accept(m, connection) < 0)
- return respond_error(connection, MHD_HTTP_BAD_REQUEST, "Failed to parse Accept header.\n");
+ return mhd_respond(connection, MHD_HTTP_BAD_REQUEST, "Failed to parse Accept header.\n");
r = sd_journal_query_unique(m->journal, field);
if (r < 0)
- return respond_error(connection, MHD_HTTP_BAD_REQUEST, "Failed to query unique fields.\n");
+ return mhd_respond(connection, MHD_HTTP_BAD_REQUEST, "Failed to query unique fields.\n");
response = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, 4*1024, request_reader_fields, m, NULL);
if (!response)
@@ -678,10 +678,10 @@ static int request_handler_file(
fd = open(path, O_RDONLY|O_CLOEXEC);
if (fd < 0)
- return respond_error(connection, MHD_HTTP_NOT_FOUND, "Failed to open file %s: %m\n", path);
+ return mhd_respondf(connection, MHD_HTTP_NOT_FOUND, "Failed to open file %s: %m\n", path);
if (fstat(fd, &st) < 0)
- return respond_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to stat file: %m\n");
+ return mhd_respondf(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to stat file: %m\n");
response = MHD_create_response_from_fd_at_offset(st.st_size, fd, 0);
if (!response)
@@ -745,15 +745,15 @@ static int request_handler_machine(
r = open_journal(m);
if (r < 0)
- return respond_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to open journal: %s\n", strerror(-r));
+ return mhd_respondf(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to open journal: %s\n", strerror(-r));
r = sd_id128_get_machine(&mid);
if (r < 0)
- return respond_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to determine machine ID: %s\n", strerror(-r));
+ return mhd_respondf(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to determine machine ID: %s\n", strerror(-r));
r = sd_id128_get_boot(&bid);
if (r < 0)
- return respond_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to determine boot ID: %s\n", strerror(-r));
+ return mhd_respondf(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to determine boot ID: %s\n", strerror(-r));
hostname = gethostname_malloc();
if (!hostname)
@@ -761,11 +761,11 @@ static int request_handler_machine(
r = sd_journal_get_usage(m->journal, &usage);
if (r < 0)
- return respond_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to determine disk usage: %s\n", strerror(-r));
+ return mhd_respondf(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to determine disk usage: %s\n", strerror(-r));
r = sd_journal_get_cutoff_realtime_usec(m->journal, &cutoff_from, &cutoff_to);
if (r < 0)
- return respond_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to determine disk usage: %s\n", strerror(-r));
+ return mhd_respondf(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to determine disk usage: %s\n", strerror(-r));
parse_env_file("/etc/os-release", NEWLINE, "PRETTY_NAME", &os_name, NULL);
@@ -822,8 +822,8 @@ static int request_handler(
assert(method);
if (!streq(method, "GET"))
- return respond_error(connection, MHD_HTTP_METHOD_NOT_ACCEPTABLE,
- "Unsupported method.\n");
+ return mhd_respond(connection, MHD_HTTP_METHOD_NOT_ACCEPTABLE,
+ "Unsupported method.\n");
if (!*connection_cls) {
@@ -853,7 +853,7 @@ static int request_handler(
if (streq(url, "/machine"))
return request_handler_machine(connection, *connection_cls);
- return respond_error(connection, MHD_HTTP_NOT_FOUND, "Not found.\n");
+ return mhd_respond(connection, MHD_HTTP_NOT_FOUND, "Not found.\n");
}
static int help(void) {
diff --git a/src/journal/journal-remote.c b/src/journal/journal-remote.c
index 1a1ca2c480..78401839d5 100644
--- a/src/journal/journal-remote.c
+++ b/src/journal/journal-remote.c
@@ -411,7 +411,7 @@ static int process_http_upload(
if (r < 0) {
log_error("Failed to store received data of size %zu: %s",
*upload_data_size, strerror(-r));
- return respond_oom_internal(connection);
+ return mhd_respond_oom(connection);
}
*upload_data_size = 0;
} else
@@ -425,8 +425,8 @@ static int process_http_upload(
break;
else if (r < 0) {
log_warning("Failed to process data for connection %p", connection);
- return respond_error(connection, MHD_HTTP_UNPROCESSABLE_ENTITY,
- "Processing failed: %s", strerror(-r));
+ return mhd_respondf(connection, MHD_HTTP_UNPROCESSABLE_ENTITY,
+ "Processing failed: %s", strerror(-r));
}
}
@@ -437,11 +437,11 @@ static int process_http_upload(
if (source_non_empty(source)) {
log_warning("EOF reached with incomplete data");
- return respond_error(connection, MHD_HTTP_EXPECTATION_FAILED,
- "Trailing data not processed.");
+ return mhd_respond(connection, MHD_HTTP_EXPECTATION_FAILED,
+ "Trailing data not processed.");
}
- return respond_error(connection, MHD_HTTP_ACCEPTED, "OK.\n");
+ return mhd_respond(connection, MHD_HTTP_ACCEPTED, "OK.\n");
};
static int request_handler(
@@ -470,19 +470,19 @@ static int request_handler(
*connection_cls);
if (!streq(method, "POST"))
- return respond_error(connection, MHD_HTTP_METHOD_NOT_ACCEPTABLE,
- "Unsupported method.\n");
+ return mhd_respond(connection, MHD_HTTP_METHOD_NOT_ACCEPTABLE,
+ "Unsupported method.\n");
if (!streq(url, "/upload"))
- return respond_error(connection, MHD_HTTP_NOT_FOUND,
- "Not found.\n");
+ return mhd_respond(connection, MHD_HTTP_NOT_FOUND,
+ "Not found.\n");
header = MHD_lookup_connection_value(connection,
MHD_HEADER_KIND, "Content-Type");
if (!header || !streq(header, "application/vnd.fdo.journal"))
- return respond_error(connection, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE,
- "Content-Type: application/vnd.fdo.journal"
- " is required.\n");
+ return mhd_respond(connection, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE,
+ "Content-Type: application/vnd.fdo.journal"
+ " is required.\n");
if (trust_pem) {
r = check_permissions(connection, &code);
diff --git a/src/journal/microhttpd-util.c b/src/journal/microhttpd-util.c
index f7f12e1a8e..250be93dbe 100644
--- a/src/journal/microhttpd-util.c
+++ b/src/journal/microhttpd-util.c
@@ -48,31 +48,45 @@ void microhttpd_logger(void *arg, const char *fmt, va_list ap) {
}
-int respond_oom_internal(struct MHD_Connection *connection) {
- const char *m = "Out of memory.\n";
-
+static int mhd_respond_internal(struct MHD_Connection *connection,
+ enum MHD_RequestTerminationCode code,
+ char *buffer,
+ size_t size,
+ enum MHD_ResponseMemoryMode mode) {
struct MHD_Response *response;
- int ret;
+ int r;
assert(connection);
- response = MHD_create_response_from_buffer(strlen(m), (char*) m, MHD_RESPMEM_PERSISTENT);
+ response = MHD_create_response_from_buffer(size, buffer, mode);
if (!response)
return MHD_NO;
+ log_debug("Queing response %u: %s", code, buffer);
MHD_add_response_header(response, "Content-Type", "text/plain");
- ret = MHD_queue_response(connection, MHD_HTTP_SERVICE_UNAVAILABLE, response);
+ r = MHD_queue_response(connection, code, response);
MHD_destroy_response(response);
- return ret;
+ return r;
}
-_printf_(3,4)
-int respond_error(struct MHD_Connection *connection,
- unsigned code,
- const char *format, ...) {
+int mhd_respond(struct MHD_Connection *connection,
+ enum MHD_RequestTerminationCode code,
+ const char *message) {
+
+ return mhd_respond_internal(connection, code,
+ (char*) message, strlen(message),
+ MHD_RESPMEM_PERSISTENT);
+}
+
+int mhd_respond_oom(struct MHD_Connection *connection) {
+ return mhd_respond(connection, MHD_HTTP_SERVICE_UNAVAILABLE, "Out of memory.\n");
+}
+
+int mhd_respondf(struct MHD_Connection *connection,
+ enum MHD_RequestTerminationCode code,
+ const char *format, ...) {
- struct MHD_Response *response;
char *m;
int r;
va_list ap;
@@ -87,17 +101,9 @@ int respond_error(struct MHD_Connection *connection,
if (r < 0)
return respond_oom(connection);
- response = MHD_create_response_from_buffer(strlen(m), m, MHD_RESPMEM_MUST_FREE);
- if (!response) {
+ r = mhd_respond_internal(connection, code, m, r, MHD_RESPMEM_MUST_FREE);
+ if (r == MHD_NO)
free(m);
- return respond_oom(connection);
- }
-
- log_debug("Queing response %u: %s", code, m);
- MHD_add_response_header(response, "Content-Type", "text/plain");
- r = MHD_queue_response(connection, code, response);
- MHD_destroy_response(response);
-
return r;
}
@@ -229,8 +235,8 @@ int check_permissions(struct MHD_Connection *connection, int *code) {
MHD_CONNECTION_INFO_GNUTLS_SESSION);
if (!ci) {
log_error("MHD_get_connection_info failed: session is unencrypted");
- *code = respond_error(connection, MHD_HTTP_FORBIDDEN,
- "Encrypted connection is required");
+ *code = mhd_respond(connection, MHD_HTTP_FORBIDDEN,
+ "Encrypted connection is required");
return -EPERM;
}
session = ci->tls_session;
@@ -238,15 +244,15 @@ int check_permissions(struct MHD_Connection *connection, int *code) {
r = get_client_cert(session, &client_cert);
if (r < 0) {
- *code = respond_error(connection, MHD_HTTP_UNAUTHORIZED,
- "Authorization through certificate is required");
+ *code = mhd_respond(connection, MHD_HTTP_UNAUTHORIZED,
+ "Authorization through certificate is required");
return -EPERM;
}
r = get_auth_dn(client_cert, &buf);
if (r < 0) {
- *code = respond_error(connection, MHD_HTTP_UNAUTHORIZED,
- "Failed to determine distinguished name from certificate");
+ *code = mhd_respond(connection, MHD_HTTP_UNAUTHORIZED,
+ "Failed to determine distinguished name from certificate");
return -EPERM;
}
@@ -255,8 +261,8 @@ int check_permissions(struct MHD_Connection *connection, int *code) {
r = verify_cert_authorized(session);
if (r < 0) {
log_warning("Client is not authorized");
- *code = respond_error(connection, MHD_HTTP_UNAUTHORIZED,
- "Client certificate not signed by recognized authority");
+ *code = mhd_respond(connection, MHD_HTTP_UNAUTHORIZED,
+ "Client certificate not signed by recognized authority");
}
return r;
}
diff --git a/src/journal/microhttpd-util.h b/src/journal/microhttpd-util.h
index cd14ac4dcf..df4d003eb9 100644
--- a/src/journal/microhttpd-util.h
+++ b/src/journal/microhttpd-util.h
@@ -28,14 +28,18 @@
void microhttpd_logger(void *arg, const char *fmt, va_list ap) _printf_(2, 0);
-int respond_oom_internal(struct MHD_Connection *connection);
-
/* respond_oom() must be usable with return, hence this form. */
-#define respond_oom(connection) log_oom(), respond_oom_internal(connection)
+#define respond_oom(connection) log_oom(), mhd_respond_oom(connection)
+
+int mhd_respondf(struct MHD_Connection *connection,
+ unsigned code,
+ const char *format, ...) _printf_(3,4);
+
+int mhd_respond(struct MHD_Connection *connection,
+ unsigned code,
+ const char *message);
-int respond_error(struct MHD_Connection *connection,
- unsigned code,
- const char *format, ...);
+int mhd_respond_oom(struct MHD_Connection *connection);
int check_permissions(struct MHD_Connection *connection, int *code);