summaryrefslogtreecommitdiffhomepage
path: root/NetEx/HttpSession.cpp
blob: 78f42aa2b416d06fbea4e33c98a45a696c1ab293 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.
*/

#include "HttpSession.h"

#include <stdio.h>
#include <stdlib.h>

#include "HttpParam.h"
#include "List.h"
#include "NetLayer.h"
#include "Text.h"


HttpSession::HttpSession()
{
    id          = GenerateUniqueID();
    access_time = NetLayer::GetUTC();
}


HttpSession::~HttpSession()
{
    attributes.destroy();
}


Text
HttpSession::GetAttribute(const char* name)
{
    ListIter<HttpParam> iter = attributes;
    while (++iter) {
        HttpParam* p = iter.value();

        if (p->name == name)
            return p->value;
    }

    return Text();
}


void
HttpSession::SetAttribute(const char* name, const char* value)
{
    ListIter<HttpParam> iter = attributes;
    while (++iter) {
        HttpParam* p = iter.value();

        if (p->name == name) {
            p->value = value;
            return;
        }
    }

    HttpParam* param = new HttpParam(name, value);
    if (param)
        attributes.append(param);
}


void
HttpSession::DelAttribute(const char* name)
{
    ListIter<HttpParam> iter = attributes;
    while (++iter) {
        HttpParam* p = iter.value();

        if (p->name == name) {
            delete iter.removeItem();
            return;
        }
    }
}


int
HttpSession::GetIntAttribute(const char* name)
{
    ListIter<HttpParam> iter = attributes;
    while (++iter) {
        HttpParam* p = iter.value();

        if (p->name == name) {
            int result = ::atoi(p->value.data());
            return result;
        }
    }

    return 0;
}


void
HttpSession::SetIntAttribute(const char* name, int value)
{
    char buf[32];
    sprintf(buf, "%d", value);

    ListIter<HttpParam> iter = attributes;
    while (++iter) {
        HttpParam* p = iter.value();

        if (p->name == name) {
            p->value = buf;
            return;
        }
    }

    HttpParam* param = new HttpParam(name, buf);
    if (param)
        attributes.append(param);
}


void
HttpSession::DelIntAttribute(const char* name)
{
    DelAttribute(name);
}


Text
HttpSession::GenerateUniqueID()
{
    char unique[17];

    for (int i = 0; i < 16; i++) {
        char c = rand() % 25 + 'a';
        unique[i] = c;
    }

    unique[16] = 0;
    return unique;
}


void
HttpSession::Access()
{
    access_time = NetLayer::GetUTC();
}