OSDN Git Service

HLS試作
[iptd/iPTd.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             sprintf_s(tmp, sizeof(tmp), "%lld", len);
102             if (strcmp(tmp, val->cString()) == 0)
103             {
104                 content_length = len;
105             }
106         }
107     }
108
109     if (MIMEType == NULL)
110     {
111         MIMEType = String::stringWithUTF8String("text/plain");
112     }
113     if (encoding == NULL)
114     {
115         encoding = String::stringWithUTF8String("us-ascii");
116     }
117
118     URLResponse *ret = URLResponse::initWithURL(url, MIMEType, content_length, encoding);
119     if (ret == NULL)
120     {
121         return NULL;
122     }
123
124     _statusCode = statusCode;
125     _HTTPVersion = HTTPVersion;
126     _HTTPVersion->retain();
127     _headerFields = headerFields;
128     _headerFields->retain();
129
130     return this;
131 }
132
133 Integer HTTPURLResponse::statusCode()
134 {
135     DebugLog2("HTTPURLResponse::statusCode()");
136
137     return _statusCode;
138 }
139
140 Dictionary *HTTPURLResponse::allHeaderFields()
141 {
142     DebugLog2("HTTPURLResponse::allHeaderFields()");
143
144     return _headerFields;
145 }
146
147 const char *HTTPURLResponse::className()
148 {
149     return "HTTPURLResponse";
150 }
151
152 } // Raym