static int rhizome_server_set_response(rhizome_http_request *r, const struct http_response *h) { strbuf b = strbuf_local((char *) r->buffer, r->buffer_size); strbuf_build_http_response(b, h); if (r->buffer == NULL || strbuf_overrun(b)) { // Need a bigger buffer if (r->buffer) free(r->buffer); r->buffer_size = strbuf_count(b) + 1; r->buffer = malloc(r->buffer_size); if (r->buffer == NULL) { WHYF_perror("malloc(%u)", r->buffer_size); r->buffer_size = 0; return WHY("Cannot send response, out of memory"); } strbuf_init(b, (char *) r->buffer, r->buffer_size); strbuf_build_http_response(b, h); if (strbuf_overrun(b)) return WHYF("Bug! Cannot send response, buffer not big enough"); } r->buffer_length = strbuf_len(b); r->buffer_offset = 0; r->request_type |= RHIZOME_HTTP_REQUEST_FROMBUFFER; if (debug & DEBUG_RHIZOME_TX) DEBUGF("Sending HTTP response: %s", alloca_toprint(120, (const char *)r->buffer, r->buffer_length)); return 0; }
void logArgv(int level, struct __sourceloc whence, const char *label, int argc, const char *const *argv) { if (level != LOG_LEVEL_SILENT) { struct strbuf b; strbuf_init(&b, NULL, 0); strbuf_append_argv(&b, argc, argv); size_t len = strbuf_count(&b); strbuf_init(&b, alloca(len + 1), len + 1); strbuf_append_argv(&b, argc, argv); _log_iterator it; _log_iterator_start(&it); _rotate_log_file(&it); while (_log_iterator_next(&it, level)) { _log_prefix_whence(&it, whence); if (label) { xputs(label, it.xpf); xputc(' ', it.xpf); } xputs(strbuf_str(&b), it.xpf); } } }
/* Compute the length of the string produced by toprint_str(). If dstStrLen == -1 then returns the exact number of characters in the printable representation (excluding the terminating nul), otherwise returns dstStrLen. @author Andrew Bettison <*****@*****.**> */ size_t toprint_str_len(const char *srcStr, const char quotes[2]) { return strbuf_count(strbuf_toprint_quoted(strbuf_local(NULL, 0), quotes, srcStr)); }
/* Compute the length of the string produced by toprint(). If dstStrLen == -1 then returns the exact number of characters in the printable representation (excluding the terminating nul), otherwise returns dstStrLen. @author Andrew Bettison <*****@*****.**> */ size_t toprint_len(const char *srcBuf, size_t srcBytes, const char quotes[2]) { return strbuf_count(strbuf_toprint_quoted_len(strbuf_local(NULL, 0), quotes, srcBuf, srcBytes)); }