part_t * part_new(const char *id, const char *filename, const char *content_type, const char *transfer_encoding, part_t * next) { part_t *part = (part_t *) malloc(sizeof(part_t)); part->header = NULL; part->next = next; part->deleteOnExit = 0; strcpy(part->id, id); strcpy(part->filename, filename); if (content_type) strcpy(part->content_type, content_type); else part->content_type[0] = '\0'; part->header = hpairnode_new(HEADER_CONTENT_ID, id, part->header); /* TODO (#1#): encoding is always binary. implement also others! */ /* part->header = hpairnode_new(HEADER_CONTENT_TRANSFER_ENCODING, "binary", part->header);*/ strcpy(part->transfer_encoding, transfer_encoding ? transfer_encoding : "binary"); if (content_type) { part->header = hpairnode_new(HEADER_CONTENT_TYPE, content_type, part->header); } else { /* TODO (#1#): get content-type from mime type list */ } return part; }
static hpair_t * _mime_process_header(char *buffer) { int i = 0, c = 0, proc_key, begin = 0; hpair_t *first = NULL, *last = NULL; char key[1054], value[1054]; key[0] = '\0'; value[0] = '\0'; proc_key = 1; while (buffer[i] != '\0') { if (buffer[i] == '\r' && buffer[i + 1] == '\n') { value[c] = '\0'; if (last) { last->next = hpairnode_new(key, value, NULL); last = last->next; } else { first = last = hpairnode_new(key, value, NULL); } proc_key = 1; c = 0; i++; } else if (buffer[i] == ':') { key[c] = '\0'; c = 0; begin = 0; proc_key = 0; } else { if (proc_key) key[c++] = buffer[i]; else { if (buffer[i] != ' ') begin = 1; if (begin) value[c++] = buffer[i]; } } i++; } return first; }
int httpd_set_header(httpd_conn_t * conn, const char *key, const char *value) { hpair_t *p; if (conn == NULL) { log_warn("Connection object is NULL"); return 0; } for (p = conn->header; p; p = p->next) { if (p->key && !strcmp(p->key, key)) { free(p->value); p->value = strdup(value); return 1; } } conn->header = hpairnode_new(key, value, conn->header); return 0; }
hpair_t * hpairnode_copy(const hpair_t * src) { hpair_t *pair; if (src == NULL) return NULL; pair = hpairnode_new(src->key, src->value, NULL); return pair; }
int httpd_add_header(httpd_conn_t * conn, const char *key, const char *value) { if (!conn) { log_warn("Connection object is NULL"); return 0; } conn->header = hpairnode_new(key, value, conn->header); return 1; }
content_type_t * content_type_new(const char *content_type_str) { hpair_t *pair = NULL, *last = NULL; content_type_t *ct; char ch, key[256], value[256]; int inQuote = 0, i = 0, c = 0, begin = 0, len; int mode = 0; /* 0: searching ';' 1: process key 2: process value */ /* Create object */ ct = (content_type_t *) malloc(sizeof(content_type_t)); ct->params = NULL; len = strlen(content_type_str); while (i <= len) { if (i != len) ch = content_type_str[i++]; else { ch = ' '; i++; } switch (mode) { case 0: if (ch == ';') { ct->type[c] = '\0'; c = 0; mode = 1; } else if (ch != ' ' && ch != '\t' && ch != '\r') ct->type[c++] = ch; break; case 1: if (ch == '=') { key[c] = '\0'; c = 0; mode = 2; } else if (ch != ' ' && ch != '\t' && ch != '\r') key[c++] = ch; break; case 2: if (ch != ' ') begin = 1; if ((ch == ' ' || ch == ';') && !inQuote && begin) { value[c] = '\0'; pair = hpairnode_new(key, value, NULL); if (ct->params == NULL) ct->params = pair; else last->next = pair; last = pair; c = 0; begin = 0; mode = 1; } else if (ch == '"') inQuote = !inQuote; else if (begin && ch != '\r') value[c++] = ch; break; } } return ct; }