DCL 4.0
Loading...
Searching...
No Matches
HttpHeader.cpp
Go to the documentation of this file.
1
2#include <wchar.h> // wcsftime
3#include <dcl/Object.h>
4
5#if __DCL_HAVE_ALLOC_DEBUG
6#undef __DCL_ALLOC_LEVEL
7#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
8#endif
9
10#include <dcl/HttpHeader.h>
11#include <dcl/DateTime.h>
12#include <dcl/Numeric.h>
13
14#if __DCL_DEBUG
15#undef __THIS_FILE__
16static const wchar_t* __THIS_FILE__ = __T("dcl/HttpHeader.cpp");
17#endif
18
19__DCL_BEGIN_NAMESPACE
20
22{
23 return __name + __T(": ") + __content;
24}
25
26#ifdef RFC2109
28 const wchar_t* _name,
29 const wchar_t* _value /* = NULL */,
30 const wchar_t* _comment /* = NULL */,
31 const wchar_t* _domain /* = NULL */,
32 int _maxAge /* = 0 */, // 초 단위
33 const wchar_t* _path /* = NULL */,
34 bool _secure /* = false */,
35 int nVersion /* = 1 */
36 )
37 : HttpHeader(L"Set-Cookie")
38{
39 __DCL_ASSERT(_name != NULL);
40
41 StringBuilder sb = _name;
42 sb += L"=";
43 if (_value == NULL) {
44 sb += L"deleted";
45 }
46 else {
47
48 sb += _value;
49
50 if (_comment) {
51 sb += L"; Comment=";
52 sb += _comment;
53 }
54 if (_domain) {
55 sb += L"; Domain=";
56 sb += _domain;
57 }
58 if (_maxAge > 0) {
59 sb += L"; Max-Age=";
60 sb += Int32::toString(_maxAge);
61 }
62 if (_path) {
63 sb += L"; Path=";
64 sb += _path;
65 }
66 if (_secure) {
67 sb += L"; Secure";
68 }
69 if (nVersion > 0) {
70 sb += L"; Version=";
71 sb += Int32::toString(nVersion);
72 }
73 }
74
75 __content = sb.toString();
76}
77
78#endif // RFC2109
79
80static String __GetGmtStr(time_t time)
81{
82 struct tm t;
83#if __DCL_WINDOWS
84 __DCL_VERIFY(gmtime_s(&t, &time) == 0);
85#else
86 __DCL_VERIFY(gmtime_r(&time, &t) != NULL);
87#endif
88
89 CharBuffer* buf = CharBuffer::create(DATETIME_FORMAT_BUFFER_SIZE);
90 size_t n = wcsftime(buf->data(), DATETIME_FORMAT_BUFFER_SIZE,
91 L"%a, %d %b %Y %H:%M:%S GMT", &t);
92 __DCL_ASSERT(buf->__allocLength >= n);
93 buf->__dataLength = n;
94
95 String r = buf;
96 buf->release();
97 return r;
98}
99
101 const wchar_t* _name,
102 const wchar_t* _value /* = NULL */,
103 time_t _expires /* = 0 */,
104 const wchar_t* _path /* = NULL */,
105 const wchar_t* _domain /* = NULL */,
106 bool _secure /* = false */)
107 : HttpHeader(L"Set-Cookie")
108{
109 __DCL_ASSERT(_name != NULL);
110
111 StringBuilder sb = _name;
112 sb += L"=";
113 if (_value == NULL) {
114 // MSIE에서 null-value를 가지는 쿠키를 지우지 못한다
115 // 1년 1초 전의 시간을 할당하여 쿠키를 삭제한다.
116 sb += L"deleted";
117 sb += L"; Expires=";
118 sb += __GetGmtStr(time(NULL) - 31536001);
119 }
120 else {
121 sb += _value;
122
123 if (_expires > 0) {
124 sb += L"; Expires=";
125 sb += __GetGmtStr(_expires);
126 }
127 if (_path) {
128 sb += L"; Path=";
129 sb += _path;
130 }
131 if (_domain) {
132 sb += L"; Domain=";
133 sb += _domain;
134 }
135 if (_secure) {
136 sb += L"; Secure";
137 }
138 }
139
140 __content = sb.toString();
141}
142
144 : HttpHeader(L"Expires")
145{
146 __content = __GetGmtStr(_expires);
147}
148
149__DCL_END_NAMESPACE
DCLCAPI errno_t gmtime_s(struct tm *__tm, const time_t *__time)
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:340
#define DATETIME_FORMAT_BUFFER_SIZE
Definition DateTime.h:21
#define __DCL_VERIFY(expr)
Definition Object.h:373
#define __DCL_ASSERT(expr)
Definition Object.h:371
#define __T(str)
Definition Object.h:44
ByteString r
ByteBuffer * buf
void CharsetConvertException *size_t n
Definition SQLField.cpp:253
HttpExpires(time_t _expires)
String __content
Definition HttpHeader.h:24
String toString() const
String __name
Definition HttpHeader.h:23
String toString(unsigned _base=10) const
Definition Numeric.inl:87