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 calipso_request_init_handler(calipso_client_t * client) { cpo_event_t *event = client->event; calipso_request_t *request = calipso_request_alloc(); if (request) { request->reply = calipso_reply_alloc(); calipso_reply_init(request->reply); } else return CPO_ERR; if (request->reply) request->reply->request = request; else return CPO_ERR; event->handler_read = (void*)calipso_request_event_read_handler; event->handler_write = (void*)calipso_request_event_write_handler; request->header = hash_table_create(MAX_HASH_REQUEST_HEADER, NULL); calipso_request_set_method(request, NULL); calipso_request_set_uri(request, "/"); calipso_request_set_version(request, "HTTP/1.1"); calipso_request_set_querystring(request, NULL); calipso_request_set_handler(request, NULL); client->client_persistent_hdl = request_persistent_handler; calipso_request_set_client(request, client); /* header head and body */ request->header_buf = chunk_ctx_alloc(request->pool); request->in_filter = chunks_alloc(request->pool); if (request->header_buf) { request->header_buf->b = cpo_pool_malloc(request->pool, INPUTBUFSZ); } /*XXX: pipelining ? */ //!calipso_client_pipeline_request(client, request); calipso_client_set_request(client, request); return CPO_OK; }
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); }
static int mod_autoind_make_index_table(calipso_reply_t * reply, const char *uri, int prop) { DIR *dir; struct stat sb; struct tm tm; char date[26]; struct dirent *ent; cpo_array_t arr; cpo_autoind_entry_t *entry; int i,dname_len; char *dname; const char * directory = calipso_resource_get_path(reply->resource); struct chunk_ctx * cb = chunk_ctx_alloc(reply->pool); arr.elem_size = sizeof(cpo_autoind_entry_t); arr.v = calloc(40 , sizeof(cpo_autoind_entry_t) ); arr.num = 0; arr.max = 32; if ((dir = opendir(directory)) == NULL) { calipso_reply_set_status(reply, HTTP_FORBIDDEN); return CPO_ERR; } while ((ent = readdir(dir)) != NULL) { if(!strncmp(ent->d_name, ".", 1)) { continue; } if( stat(ent->d_name , &sb) != -1) { entry = cpo_array_push(&arr); entry->name = cpo_pool_strdup(reply->pool, ent->d_name); entry->mtime = sb.st_mtime; entry->size = sb.st_size; entry->is_dir = S_ISDIR(sb.st_mode); } } closedir(dir); cpo_array_qsort(&arr, mod_autoindex_cmp_entries); chunk_ctx_printf(reply->pool, cb, indheadfoot[0], uri, uri); if(strcmp(uri,"/") ) { char dir[FILENAME_MAX]; strncpy(dir, uri, sizeof(dir)); chunk_ctx_printf(reply->pool, cb, "<a href=\"%s\">%s</a>%s %40s\n", dirname(dir), PARENT_DIR_NAME, add_space(PARENT_DIR_NAME, sizeof(PARENT_DIR_NAME), SPACE_CHAR), "-"); } for(i =0; i < arr.num; i++) { entry = cpo_array_get_at(&arr, i); dname_len = cpo_strlen(entry->name); if(ETALON_SPACE < dname_len) { int dots; dname = cpo_pool_strdup(reply->pool, entry->name); for(dots = 4; dots >= 1; dots--) { dname[ETALON_SPACE-dots] = ((dots == 1) ? '>' : '.'); } dname[ETALON_SPACE]='\0'; dname_len = ETALON_SPACE; } else { dname = entry->name; } if(dname) { cpo_gmtime(&tm, &entry->mtime); cpo_snprintf(date, sizeof(date), "%02d-%s-%d %02d:%02d ", tm.tm_mday, months[tm.tm_mon ], tm.tm_year, tm.tm_hour, tm.tm_min); if(entry->is_dir) { chunk_ctx_printf(reply->pool, cb, "<a href=\"%s%s/\">%s/</a>%s%10s %20s\n", uri, entry->name , dname, add_space(dname, dname_len, SPACE_CHAR), date, "-"); } else { chunk_ctx_printf(reply->pool, cb, "<a href=\"%s%s\">%s</a>%s %s %20.llu\n", uri, entry->name , dname, add_space(dname, dname_len ,SPACE_CHAR), date, (uintmax_t) entry->size); } //free dname } } chunk_ctx_printf(reply->pool, cb, indheadfoot[1], ""); CNUNKS_ADD_TAIL_CTX(reply->out_filter, cb); free(arr.v); return CPO_OK; }