/* al - NULL to use a "dumping" mode */ void template_apply(template_t *t, attrlist_t al, kore_buf_t *out) { struct entry *e; char buf[64]; /* TODO: max macro length */ attr_t *tmp; /* TODO: we could cache the attrget lookup */ // fflush(stdout); /* we'll be using posix i/o instead of iso c */ for(e=t->entry_list;e;e=e->next) { // kore_log(LOG_DEBUG,"::%u:%u:%.*s", // e->type, e->len, e->len, e->data); /* ignore macro type if the macro is too large */ if(al && e->type && (e->len<(sizeof buf-1))) { memcpy(buf, e->data, e->len); buf[e->len]=0; tmp = attrget(al, buf); if(tmp) kore_buf_append(out, tmp->value, tmp->len); } else if(!al && e->type) { /* debug mode because al==NULL */ kore_log(LOG_DEBUG,"%s (%u)", e->data, e->len); } else { /* raw data */ if(e->data) kore_buf_append(out,e->data,e->len); } } }
void test_base64(u_int8_t *src, u_int32_t slen, struct kore_buf *res) { char *in; u_int32_t len; u_int8_t *out; kore_buf_appendf(res, "test '%s'\n", src); if (!kore_base64_encode(src, slen, &in)) { kore_buf_appendf(res, "encoding '%s' failed\n", src); } else { kore_buf_appendf(res, "encoded: '%s'\n", in); if (!kore_base64_decode(in, &out, &len)) { kore_buf_appendf(res, "decoding failed\n"); } else { kore_buf_appendf(res, "decoded: "); kore_buf_append(res, out, len); kore_buf_appendf(res, "\n"); kore_mem_free(out); } kore_mem_free(in); } kore_buf_appendf(res, "\n"); }
void kore_buf_appendv(struct kore_buf *buf, const char *fmt, va_list args) { int l; va_list copy; char *b, sb[BUFSIZ]; va_copy(copy, args); l = vsnprintf(sb, sizeof(sb), fmt, args); if (l == -1) fatal("kore_buf_appendv(): vsnprintf error"); if ((size_t)l >= sizeof(sb)) { l = vasprintf(&b, fmt, copy); if (l == -1) fatal("kore_buf_appendv(): error or truncation"); } else { b = sb; } kore_buf_append(buf, b, l); if (b != sb) free(b); va_end(copy); }
size_t curl_write_cb(char *ptr, size_t size, size_t nmemb, void *udata) { struct kore_buf *b = udata; kore_buf_append(b, ptr, size * nmemb); return (size * nmemb); }
void kore_buf_appendb(struct kore_buf *buf, struct kore_buf *src) { u_int8_t *d; u_int32_t len; d = kore_buf_release(src, &len); kore_buf_append(buf, d, len); }
char * kore_buf_stringify(struct kore_buf *buf) { char c; c = '\0'; kore_buf_append(buf, &c, sizeof(c)); return ((char *)buf->data); }
void kore_buf_appendb(struct kore_buf *buf, struct kore_buf *src) { u_int8_t *d; size_t len; d = kore_buf_release(src, &len); kore_buf_append(buf, d, len); kore_mem_free(d); }
void kore_buf_appendf(struct kore_buf *buf, const char *fmt, ...) { va_list args; char b[2048]; va_start(args, fmt); vsnprintf(b, sizeof(b), fmt, args); va_end(args); kore_buf_append(buf, (u_int8_t *)b, strlen(b)); }
void kore_buf_appendv(struct kore_buf *buf, struct buf_vec *v, u_int16_t count) { u_int16_t i; struct buf_vec *p; p = v; for (i = 0; i < count; i++) { kore_buf_append(buf, p->data, p->length); p++; } }
char * kore_buf_stringify(struct kore_buf *buf, size_t *len) { char c; if (len != NULL) *len = buf->offset; c = '\0'; kore_buf_append(buf, &c, sizeof(c)); return ((char *)buf->data); }
int serve_file_upload(struct http_request *req) { u_int8_t *d; struct kore_buf *b; struct http_file *f; size_t len; char *name, buf[BUFSIZ]; b = kore_buf_alloc(asset_len_upload_html); kore_buf_append(b, asset_upload_html, asset_len_upload_html); if (req->method == HTTP_METHOD_POST) { if (req->http_body_fd != -1) kore_log(LOG_NOTICE, "file is on disk"); http_populate_multipart_form(req); if (http_argument_get_string(req, "firstname", &name)) { kore_buf_replace_string(b, "$firstname$", name, strlen(name)); } else { kore_buf_replace_string(b, "$firstname$", NULL, 0); } if ((f = http_file_lookup(req, "file")) != NULL) { (void)snprintf(buf, sizeof(buf), "%s is %ld bytes", f->filename, f->length); kore_buf_replace_string(b, "$upload$", buf, strlen(buf)); } else { kore_buf_replace_string(b, "$upload$", NULL, 0); } } else { kore_buf_replace_string(b, "$upload$", NULL, 0); kore_buf_replace_string(b, "$firstname$", NULL, 0); } d = kore_buf_release(b, &len); http_response_header(req, "content-type", "text/html"); http_response(req, 200, d, len); kore_free(d); return (KORE_RESULT_OK); }
int serve_file_upload(struct http_request *req) { int r; u_int8_t *d; struct kore_buf *b; u_int32_t len; char *name, buf[BUFSIZ]; b = kore_buf_create(asset_len_upload_html); kore_buf_append(b, asset_upload_html, asset_len_upload_html); if (req->method == HTTP_METHOD_POST) { http_populate_multipart_form(req, &r); if (http_argument_get_string("firstname", &name, &len)) { kore_buf_replace_string(b, "$firstname$", name, len); } else { kore_buf_replace_string(b, "$firstname$", NULL, 0); } if (http_file_lookup(req, "file", &name, &d, &len)) { (void)snprintf(buf, sizeof(buf), "%s is %d bytes", name, len); kore_buf_replace_string(b, "$upload$", buf, strlen(buf)); } else { kore_buf_replace_string(b, "$upload$", NULL, 0); } } else { kore_buf_replace_string(b, "$upload$", NULL, 0); kore_buf_replace_string(b, "$firstname$", NULL, 0); } d = kore_buf_release(b, &len); http_response_header(req, "content-type", "text/html"); http_response(req, 200, d, len); kore_mem_free(d); return (KORE_RESULT_OK); }
int serve_params_test(struct http_request *req) { struct kore_buf *b; u_int8_t *d; u_int32_t len; int r, i; char *test, name[10]; http_populate_arguments(req); b = kore_buf_create(asset_len_params_html); kore_buf_append(b, asset_params_html, asset_len_params_html); /* * The GET parameters will be filtered out on POST. */ if (http_argument_get_string("arg1", &test, &len)) { kore_buf_replace_string(b, "$arg1$", test, len); } else { kore_buf_replace_string(b, "$arg1$", NULL, 0); } if (http_argument_get_string("arg2", &test, &len)) { kore_buf_replace_string(b, "$arg2$", test, len); } else { kore_buf_replace_string(b, "$arg2$", NULL, 0); } if (req->method == HTTP_METHOD_GET) { kore_buf_replace_string(b, "$test1$", NULL, 0); kore_buf_replace_string(b, "$test2$", NULL, 0); kore_buf_replace_string(b, "$test3$", NULL, 0); if (http_argument_get_uint16("id", &r)) kore_log(LOG_NOTICE, "id: %d", r); else kore_log(LOG_NOTICE, "No id set"); http_response_header(req, "content-type", "text/html"); d = kore_buf_release(b, &len); http_response(req, 200, d, len); kore_mem_free(d); return (KORE_RESULT_OK); } for (i = 1; i < 4; i++) { (void)snprintf(name, sizeof(name), "test%d", i); if (http_argument_get_string(name, &test, &len)) { (void)snprintf(name, sizeof(name), "$test%d$", i); kore_buf_replace_string(b, name, test, len); } else { (void)snprintf(name, sizeof(name), "$test%d$", i); kore_buf_replace_string(b, name, NULL, 0); } } http_response_header(req, "content-type", "text/html"); d = kore_buf_release(b, &len); http_response(req, 200, d, len); kore_mem_free(d); return (KORE_RESULT_OK); }
void kore_accesslog_gather(void *arg, u_int64_t now, int force) { int id; struct kore_worker *kw; struct kore_alog_header *hdr; struct kore_domain *dom; size_t off, remain; if (logbuf == NULL) logbuf = kore_buf_alloc(LOGBUF_SIZE); for (id = 0; id < worker_count; id++) { kw = kore_worker_data(id); accesslog_lock(kw); if (force || kw->lb.offset >= KORE_ACCESSLOG_SYNC) { kore_buf_append(logbuf, kw->lb.buf, kw->lb.offset); kw->lb.offset = 0; } accesslog_unlock(kw); } if (force || logbuf->offset >= LOGBUF_SIZE) { off = 0; remain = logbuf->offset; while (remain > 0) { if (remain < sizeof(*hdr)) { kore_log(LOG_ERR, "invalid log buffer: (%zu remain)", remain); break; } hdr = (struct kore_alog_header *)(logbuf->data + off); off += sizeof(*hdr); remain -= sizeof(*hdr); if (hdr->loglen > remain) { kore_log(LOG_ERR, "invalid log header: %u (%zu remain)", hdr->loglen, remain); break; } if ((dom = kore_domain_byid(hdr->domain)) == NULL) fatal("unknown domain id %u", hdr->domain); if (dom->logbuf == NULL) dom->logbuf = kore_buf_alloc(DOMAIN_LOGBUF_LEN); kore_buf_append(dom->logbuf, &logbuf->data[off], hdr->loglen); off += hdr->loglen; remain -= hdr->loglen; accesslog_flush(dom, now, force); } kore_buf_reset(logbuf); } if (force) kore_domain_callback(accesslog_flush_cb); }