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 attrfree(attrlist_t al) { unsigned i; for(i=0;i<al->len;i++) { if(al->data[i].value) kore_mem_free(al->data[i].value); } if(al->data) kore_mem_free(al->data); }
void namefree(void) { unsigned i; for(i=0;i<name_list.len;i++) { kore_mem_free(name_list.data[i]); } kore_mem_free(name_list.data); name_list.data=NULL; name_list.len=0; }
void kore_connection_remove(struct connection *c) { struct netbuf *nb, *next; struct spdy_stream *s, *snext; kore_debug("kore_connection_remove(%p)", c); if (c->ssl != NULL) SSL_free(c->ssl); close(c->fd); if (c->inflate_started) inflateEnd(&(c->z_inflate)); if (c->deflate_started) deflateEnd(&(c->z_deflate)); for (nb = TAILQ_FIRST(&(c->send_queue)); nb != NULL; nb = next) { next = TAILQ_NEXT(nb, list); TAILQ_REMOVE(&(c->send_queue), nb, list); if (nb->buf != NULL) kore_mem_free(nb->buf); kore_pool_put(&nb_pool, nb); } for (nb = TAILQ_FIRST(&(c->recv_queue)); nb != NULL; nb = next) { next = TAILQ_NEXT(nb, list); TAILQ_REMOVE(&(c->recv_queue), nb, list); kore_mem_free(nb->buf); kore_pool_put(&nb_pool, nb); } for (s = TAILQ_FIRST(&(c->spdy_streams)); s != NULL; s = snext) { snext = TAILQ_NEXT(s, list); TAILQ_REMOVE(&(c->spdy_streams), s, list); if (s->hblock != NULL) { if (s->hblock->header_block != NULL) kore_mem_free(s->hblock->header_block); kore_mem_free(s->hblock); } kore_mem_free(s); } kore_worker_connection_remove(c); kore_pool_put(&connection_pool, c); }
int serve_style_css(struct http_request *req) { char *date; time_t tstamp; tstamp = 0; if (http_request_header(req, "if-modified-since", &date)) { tstamp = kore_date_to_time(date); kore_mem_free(date); kore_debug("header was present with %ld", tstamp); } if (tstamp != 0 && tstamp <= asset_mtime_style_css) { http_response(req, 304, NULL, 0); } else { date = kore_time_to_date(asset_mtime_style_css); if (date != NULL) http_response_header(req, "last-modified", date); http_response_header(req, "content-type", "text/css"); http_response(req, 200, asset_style_css, asset_len_style_css); } return (KORE_RESULT_OK); }
int serve_style_css(struct http_request *req) { int ret; char *date; time_t tstamp; tstamp = 0; if (http_request_header_get(req, "if-modified-since", &date)) { tstamp = kore_date_to_time(date); kore_mem_free(date); kore_debug("header was present with %ld", tstamp); } if (tstamp != 0 && tstamp <= static_mtime_css_style) { ret = http_response(req, 304, NULL, 0); } else { date = kore_time_to_date(static_mtime_css_style); if (date != NULL) http_response_header_add(req, "last-modified", date); http_response_header_add(req, "content-type", "text/css"); ret = http_response(req, 200, static_css_style, static_len_css_style); } return (ret); }
void kore_buf_replace_string(struct kore_buf *b, char *src, void *dst, size_t len) { char *key, *end, *tmp, *p; size_t blen, off, off2, nlen, klen; off = 0; klen = strlen(src); for (;;) { blen = b->offset; nlen = blen + len; p = (char *)b->data; key = kore_mem_find(p + off, b->offset - off, src, klen); if (key == NULL) break; end = key + klen; off = key - p; off2 = ((char *)(b->data + b->offset) - end); tmp = kore_malloc(nlen); memcpy(tmp, p, off); if (dst != NULL) memcpy((tmp + off), dst, len); memcpy((tmp + off + len), end, off2); kore_mem_free(b->data); b->data = (u_int8_t *)tmp; b->offset = off + len + off2; b->length = nlen; off = off + len; } }
int kore_connection_accept(struct listener *l, struct connection **out) { socklen_t len; struct connection *c; kore_debug("kore_connection_accept(%p)", l); *out = NULL; len = sizeof(struct sockaddr_in); c = (struct connection *)kore_malloc(sizeof(*c)); if ((c->fd = accept(l->fd, (struct sockaddr *)&(c->sin), &len)) == -1) { kore_mem_free(c); kore_debug("accept(): %s", errno_s); return (KORE_RESULT_ERROR); } if (!kore_connection_nonblock(c->fd)) { close(c->fd); kore_mem_free(c); return (KORE_RESULT_ERROR); } c->owner = l; c->ssl = NULL; c->flags = 0; c->inflate_started = 0; c->deflate_started = 0; c->client_stream_id = 0; c->proto = CONN_PROTO_UNKNOWN; c->state = CONN_STATE_SSL_SHAKE; c->wsize_initial = SPDY_INIT_WSIZE; c->idle_timer.start = 0; c->idle_timer.length = KORE_IDLE_TIMER_MAX; TAILQ_INIT(&(c->send_queue)); TAILQ_INIT(&(c->recv_queue)); TAILQ_INIT(&(c->spdy_streams)); kore_worker_connection_add(c); kore_connection_start_idletimer(c); *out = c; return (KORE_RESULT_OK); }
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); kore_mem_free(d); }
static void pool_region_destroy(struct kore_pool *pool) { struct kore_pool_region *reg; kore_debug("pool_region_destroy(%p)", pool); /* Take care iterating when modifying list contents */ while (!LIST_EMPTY(&pool->regions)) { reg = LIST_FIRST(&pool->regions); LIST_REMOVE(reg, list); kore_mem_free(reg->start); kore_mem_free(reg); } /* Freelist references into the regions memory allocations */ LIST_INIT(&pool->freelist); pool->elms = 0; }
void template_free(template_t *t) { struct entry *curr, *next; if(!t) return; /* t=NULL then just leave */ for(curr=t->entry_list;curr;curr=next) { next=curr->next; kore_mem_free(curr); } // free(t); }
/* * This is the function that is executed by our task which is created * in the page_handler() callback. * * It sets up a CURL POST request to /post_back passing along the * user argument which it receives from its channel from page_handler(). */ int run_curl(struct kore_task *t) { int l; struct kore_buf *b; u_int32_t len; CURLcode res; u_int8_t *data; CURL *curl; char user[64], fields[128]; /* * Read the channel in order to obtain the user argument * that was written to it by page_handler(). */ len = kore_task_channel_read(t, user, sizeof(user)); if (len > sizeof(user)) return (KORE_RESULT_ERROR); l = snprintf(fields, sizeof(fields), "user=%.*s", len, user); if (l == -1 || (size_t)l >= sizeof(fields)) return (KORE_RESULT_ERROR); if ((curl = curl_easy_init()) == NULL) return (KORE_RESULT_ERROR); b = kore_buf_create(128); /* Do CURL magic. */ curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_WRITEDATA, b); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_cb); curl_easy_setopt(curl, CURLOPT_URL, "https://127.0.0.1:4443/post_back"); res = curl_easy_perform(curl); if (res != CURLE_OK) { kore_buf_free(b); curl_easy_cleanup(curl); return (KORE_RESULT_ERROR); } /* * Grab the response from the CURL request and write the * result back to the task channel. */ data = kore_buf_release(b, &len); kore_task_channel_write(t, data, len); kore_mem_free(data); return (KORE_RESULT_OK); }
u_int8_t * kore_buf_release(struct kore_buf *buf, u_int32_t *len) { u_int8_t *p; p = buf->data; *len = buf->offset; kore_mem_free(buf); return (p); }
void kore_pool_cleanup(struct kore_pool *pool) { pool->elms = 0; pool->inuse = 0; pool->elen = 0; pool->slen = 0; if (pool->name != NULL) { kore_mem_free(pool->name); pool->name = NULL; } pool_region_destroy(pool); }
int serve_b64test(struct http_request *req) { int i; u_int32_t len; struct kore_buf *res; u_int8_t *data; res = kore_buf_create(1024); for (i = 0; b64tests[i] != NULL; i++) test_base64((u_int8_t *)b64tests[i], strlen(b64tests[i]), res); data = kore_buf_release(res, &len); http_response_header(req, "content-type", "text/plain"); http_response(req, 200, data, len); kore_mem_free(data); return (KORE_RESULT_OK); }
void net_recv_reset(struct connection *c, u_int32_t len, int (*cb)(struct netbuf *)) { kore_debug("net_recv_reset(): %p %d", c, len); if (c->rnb->type != NETBUF_RECV) fatal("net_recv_reset(): wrong netbuf type"); c->rnb->cb = cb; c->rnb->s_off = 0; c->rnb->b_len = len; if (c->rnb->b_len <= c->rnb->m_len && c->rnb->m_len < (NETBUF_SEND_PAYLOAD_MAX / 2)) return; kore_mem_free(c->rnb->buf); c->rnb->m_len = len; c->rnb->buf = kore_malloc(c->rnb->m_len); }
void * kore_realloc(void *ptr, size_t len) { struct meminfo *mem; void *nptr; if (ptr == NULL) { nptr = kore_malloc(len); } else { mem = KORE_MEMINFO(ptr); if (mem->magic != KORE_MEM_MAGIC) fatal("kore_realloc(): magic boundary not found"); nptr = kore_malloc(len); memcpy(nptr, ptr, KORE_MEMSIZE(ptr)); kore_mem_free(ptr); } return (nptr); }
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); }
void net_remove_netbuf(struct netbuf_head *list, struct netbuf *nb) { kore_debug("net_remove_netbuf(%p, %p)", list, nb); if (nb->type == NETBUF_RECV) fatal("net_remove_netbuf(): cannot remove recv netbuf"); if (nb->flags & NETBUF_MUST_RESEND) { kore_debug("retaining %p (MUST_RESEND)", nb); nb->flags |= NETBUF_FORCE_REMOVE; return; } if (!(nb->flags & NETBUF_IS_STREAM)) { kore_mem_free(nb->buf); } else if (nb->cb != NULL) { (void)nb->cb(nb); } TAILQ_REMOVE(list, nb, list); kore_pool_put(&nb_pool, nb); }
/* set an attribute */ static void attrsetn_internal(attrlist_t al, const char *name, int safe_fl, const _char *value, size_t len) { attr_t *at; at=setup_access(al, name); /* a null value would delete the attribute */ if(value==NULL) { if(at) attrdel(al, at); /* delete if there is no value */ return; } /* discard the old attribute values */ kore_mem_free(at->value); at->value=NULL; /* set the attribute */ if(safe_fl) { at->len=html_escape_len(value, len); at->value=kore_malloc(at->len+1); html_escape(at->value, at->len, value); } else { at->len=len; at->value=kore_malloc(len+1); memcpy(at->value, value, at->len+1); } }
void kore_buf_free(struct kore_buf *buf) { kore_mem_free(buf->data); kore_mem_free(buf); }
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); }