1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include "headers.h"
using std::string;
Headers::Headers()
{
head = current = NULL;
}
Headers::~Headers()
{
HeaderNode *node;
while (node = head) {
head = node->next;
free(node->name);
free(node->value);
delete node;
}
head = current = NULL;
}
void Headers::Replace(const char *name, const char *value)
{
HeaderNode *node = head;
while (node) {
if (!strcasecmp(node->name, name)) {
free(node->value);
node->value = strdup(value);
return;
}
node = node->next;
}
Add(name, value);
}
void Headers::Add(const char *name, int value)
{
char buf[15];
sprintf(buf,"%d",value);
Add(name,buf);
}
void Headers::Add(const char *name, const char *value)
{
HeaderNode *node = new HeaderNode;
node->name = strdup(name);
node->value = strdup(value);
node->next = head;
head = node;
}
const char *Headers::Header(const char *name)
{
const char *h_name, *value;
h_name = FirstHeader(&value);
while (h_name && strcasecmp(name, h_name)) h_name = NextHeader(&value);
return (!!h_name) ? value : NULL;
}
string Headers::AllHeader(const char *name)
{
const char *h_name, *value;
string allvalue;
h_name = FirstHeader(&value);
while (h_name) {
if (!strcasecmp(name, h_name)) {
string pom = value;
if (allvalue != "")
allvalue += "; ";
allvalue += pom.find(";") != -1
? pom.substr(0, pom.find(";"))
: pom;
}
h_name = NextHeader(&value);
}
return allvalue;
}
const char *Headers::FirstHeader(const char **value)
{
current = head;
if (!current) return NULL;
*value = current->value;
return current->name;
}
const char *Headers::NextHeader(const char **value)
{
current = current->next;
if (!current) return NULL;
*value = current->value;
return current->name;
}
|