OSDN Git Service

initial files
[iptd/iPTd_R3.git] / src / Raym / HTTPURLResponse.cpp
1 //
2 // HTTPURLResponse.cpp
3 //
4
5 #define DBG_LEVEL 0
6 #include <Raym/Log.h>
7 #include <Raym/HTTPURLResponse.h>
8
9 namespace Raym
10 {
11
12 #define RELEASE(P1)         \
13     if ((P1) != NULL)       \
14     {                       \
15         (P1)->release();    \
16         (P1) = NULL;        \
17     }
18
19 HTTPURLResponse::HTTPURLResponse()
20 {
21     DebugLog2("HTTPURLResponse::HTTPURLResponse()");
22
23     _statusCode = 0;
24     _HTTPVersion = NULL;
25     _headerFields = NULL;
26 }
27
28 HTTPURLResponse::~HTTPURLResponse()
29 {
30     _statusCode = 0;
31     RELEASE(_HTTPVersion);
32     RELEASE(_headerFields);
33
34     DebugLog2("HTTPURLResponse::~HTTPURLResponse()\n");
35 }
36
37 HTTPURLResponse *HTTPURLResponse::alloc()
38 {
39     DebugLog2("HTTPURLResponse::alloc()");
40
41     return new HTTPURLResponse();
42 }
43
44 HTTPURLResponse *HTTPURLResponse::initWithURL(URL *url, Integer statusCode, String *HTTPVersion, Dictionary *headerFields)
45 {
46     DebugLog2("HTTPURLResponse::initWithURL()");
47
48     if ((url == NULL) || (statusCode == 0) || (HTTPVersion == NULL) || (headerFields == NULL))
49     {
50         DebugLog3("error: parameter check NG.");
51         return NULL;
52     }
53
54     String *MIMEType = NULL;
55     long long content_length = URLResponseUnknownLength;
56     String *encoding = NULL;
57
58     Array *keys = headerFields->allKeys();
59     for (uint i = 0; i < keys->count(); ++i)
60     {
61         String *key = (String *)keys->objectAtIndex(i);
62         String *val = headerFields->stringForKey(key);
63
64         if (key->lowercaseString()->isEqualToString("content-type"))
65         {
66 #ifdef _WIN32
67             char *value = _strdup(val->lowercaseString()->cString());
68 #else
69             char *value = strdup(val->lowercaseString()->cString());
70 #endif
71             if (value != NULL)
72             {
73                 char *p = strchr(value, ';');
74                 if (p != NULL)
75                 {
76                     *p = '\0';
77                     ++p;
78                     while (*p == ' ')
79                     {
80                         ++p;
81                     }
82                 }
83                 MIMEType = String::stringWithUTF8String(value);
84                 if (p != NULL)
85                 {
86                     if (MIMEType->isEqualToString("text/html"))
87                     {
88                         if (strncmp("charset=", p, 8) == 0)
89                         {
90                             encoding = String::stringWithUTF8String(&p[8]);
91                         }
92                     }
93                 }
94                 free(value);
95             }
96         }
97         if (key->lowercaseString()->isEqualToString("content-length"))
98         {
99             long long len = atoll(val->cString());
100             char tmp[32];
101 #ifdef _WIN32
102             sprintf_s(tmp, sizeof(tmp), "%lld", len);
103 #else
104             sprintf(tmp, "%lld", len);
105 #endif
106             if (strcmp(tmp, val->cString()) == 0)
107             {
108                 content_length = len;
109             }
110         }
111     }
112
113     if (MIMEType == NULL)
114     {
115         MIMEType = String::stringWithUTF8String("text/plain");
116     }
117     if (encoding == NULL)
118     {
119         encoding = String::stringWithUTF8String("us-ascii");
120     }
121
122     URLResponse *ret = URLResponse::initWithURL(url, MIMEType, content_length, encoding);
123     if (ret == NULL)
124     {
125         return NULL;
126     }
127
128     _statusCode = statusCode;
129     _HTTPVersion = HTTPVersion;
130     _HTTPVersion->retain();
131     _headerFields = headerFields;
132     _headerFields->retain();
133
134     return this;
135 }
136
137 Integer HTTPURLResponse::statusCode()
138 {
139     DebugLog2("HTTPURLResponse::statusCode()");
140
141     return _statusCode;
142 }
143
144 Dictionary *HTTPURLResponse::allHeaderFields()
145 {
146     DebugLog2("HTTPURLResponse::allHeaderFields()");
147
148     return _headerFields;
149 }
150
151 const char *HTTPURLResponse::className()
152 {
153     return "HTTPURLResponse";
154 }
155
156 } // Raym