DCL 3.7.4
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_HAVE_THIS_FILE__
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) : HttpHeader(L"Set-Cookie")
37{
38 __DCL_ASSERT(_name != NULL);
39
40 StringBuilder sb = _name;
41 sb += L"=";
42 if (_value == NULL) {
43 sb += L"deleted";
44 }
45 else {
46 sb += _value;
47
48 if (_comment) {
49 sb += L"; Comment=";
50 sb += _comment;
51 }
52 if (_domain) {
53 sb += L"; Domain=";
54 sb += _domain;
55 }
56 if (_maxAge > 0) {
57 sb += L"; Max-Age=";
58 sb += Int32::toString(_maxAge);
59 }
60 if (_path) {
61 sb += L"; Path=";
62 sb += _path;
63 }
64 if (_secure) {
65 sb += L"; Secure";
66 }
67 if (nVersion > 0) {
68 sb += L"; Version=";
69 sb += Int32::toString(nVersion);
70 }
71 }
72
73 __content = sb.toString();
74}
75
76#endif // RFC2109
77
78static String __GetGmtStr(time_t time)
79{
80 struct tm t;
81#ifdef __WINNT__
82 __DCL_VERIFY(gmtime_s(&t, &time) == 0);
83#else
84 __DCL_VERIFY(gmtime_r(&time, &t) != NULL);
85#endif
86
87 CharBuffer* buf = CharBuffer::create(DATETIME_FORMAT_BUFFER_SIZE);
88 size_t n = wcsftime(buf->data(), DATETIME_FORMAT_BUFFER_SIZE,
89 L"%a, %d %b %Y %H:%M:%S GMT", &t);
90 __DCL_ASSERT(buf->__allocLength >= n);
91 buf->__dataLength = n;
92
93 String r = buf;
94 buf->release();
95 return r;
96}
97
99 const wchar_t* _name,
100 const wchar_t* _value /* = NULL */,
101 time_t _expires /* = 0 */,
102 const wchar_t* _path /* = NULL */,
103 const wchar_t* _domain /* = NULL */,
104 bool _secure /* = false */
105) : HttpHeader(L"Set-Cookie")
106{
107 __DCL_ASSERT(_name != NULL);
108
109 StringBuilder sb = _name;
110 sb += L"=";
111 if (_value == NULL) {
112 // MSIE에서 null-value를 가지는 쿠키를 지우지 못한다
113 // 1년 1초 전의 시간을 할당하여 쿠키를 삭제한다.
114 sb += L"deleted";
115 sb += L"; Expires=";
116 sb += __GetGmtStr(time(NULL) - 31536001);
117 }
118 else {
119 sb += _value;
120 if (_expires > 0) {
121 sb += L"; Expires=";
122 sb += __GetGmtStr(_expires);
123 }
124 if (_path) {
125 sb += L"; Path=";
126 sb += _path;
127 }
128 if (_domain) {
129 sb += L"; Domain=";
130 sb += _domain;
131 }
132 if (_secure) {
133 sb += L"; Secure";
134 }
135 }
136
137 __content = sb.toString();
138}
139
141 : HttpHeader(L"Expires")
142{
143 __content = __GetGmtStr(_expires);
144}
145
146__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:312
#define DATETIME_FORMAT_BUFFER_SIZE
Definition DateTime.h:21
IOException *size_t r
Definition MediaInfo.cpp:82
#define __DCL_VERIFY(expr)
Definition Object.h:396
#define __DCL_ASSERT(expr)
Definition Object.h:394
#define __T(str)
Definition Object.h:60
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:93