int chunks_add_tail(chunks_t *c, char *b, int sz) { struct chunk_ctx *ctx = chunk_ctx_alloc(c->pool); if (ctx == NULL) return 0; chunk_ctx_append(c->pool, ctx, b, sz); c->list = dllist_insert_tail(c->list, ctx); c->total_bytes = c->total_bytes + sz; return sz; }
int chunk_ctx_from_txt_file(struct mpool * pool, struct chunk_ctx *ctx, const char *file) { int read = 0; char text[CHUNKS_BUFFER_SIZE + 1]; FILE *fp = fopen(file, "rb"); if (fp) { while (!feof(fp)) { memset(text, 0, sizeof(text)); read = fread(text, sizeof(char), CHUNKS_BUFFER_SIZE, fp); chunk_ctx_append(pool, ctx, text, read); } fclose(fp); return 1; } return 0; }
int calipso_reply_send_header(calipso_reply_t *reply) { int i; hash_node_t *node; hash_t * hash = reply->header; struct chunk_ctx *ctx_header = chunk_ctx_alloc(reply->pool); u_int16_t status = calipso_reply_get_status(reply); const char *message = calipso_http_status_get_message(status); /* First we will have to send the status code */ chunk_ctx_printf(reply->pool, ctx_header, "HTTP/1.1 %d %s\r\n", status, message); for (i = 0; i < hash_table_get_size(hash); i++) { node = hash->nodes[i]; while(node) { chunk_ctx_printf(reply->pool, ctx_header, "%s: %s\r\n", node->key, (char *)node->data); #ifdef DEBUG_HEADER TRACE("HEADER_LINE %s: %s\n", node->key, (char *)node->data); #endif node = node->next; } } chunk_ctx_append(reply->pool, ctx_header, "\r\n" , 2); CNUNKS_ADD_HEAD_CTX(reply->out_filter, ctx_header); reply->bytes_to_send = reply->out_filter->total_bytes; //calipso_reply_get_replybuf_size(reply); calipso_reply_set_replybuf_size(reply, reply->out_filter->total_bytes); return (1); }
/* string operations */ int chunk_ctx_printf(struct mpool * pool, struct chunk_ctx *ctx, char *fmt, ...) { char *buf; int bytes; va_list arg_ptr, apcopy; va_start(arg_ptr, fmt); va_copy(apcopy, arg_ptr); bytes = vsnprintf(NULL, 0, fmt, apcopy); va_end(apcopy); buf = malloc(bytes + 1); if(buf) { /* cpo_vslprintf */ bytes = vsnprintf(buf, bytes + 1, fmt, arg_ptr); if(bytes) { chunk_ctx_append(pool, ctx, buf, bytes); } } va_end(arg_ptr); free(buf); return bytes; }