9 #define stricmp strcasecmp
11 #include "HttpURLConnection.h"
16 #define DEFAULT_PORT 80;
18 Field::Field(char *field
)
20 char *p
= strtok(field
, ": \t\r\n");
22 p
= strtok(NULL
, " \t\r\n");
26 Field::Field(const char *k
, const char *v
)
32 Field::Field(const Field
&o
)
38 Field
&Field::operator = (const Field
&o
)
45 bool Field::operator == (const Field
&o
)
47 return (key
== o
.key
) && (value
== o
.value
);
50 HttpURLConnection::HttpURLConnection(const URL
&Url
)
51 : connected(false), doInput(true), doOutput(false), url(Url
)
57 __response_code
= HTTP_UNKNOWN
;
60 HttpURLConnection::~HttpURLConnection()
77 void HttpURLConnection::disconnect()
85 const char *HttpURLConnection::getRequestMethod() const
87 return __method
.c_str();
90 void HttpURLConnection::setRequestMethod(const char *method
)
95 void HttpURLConnection::setRequestProperty(const char *key
, const char *value
)
97 if (__request
== NULL
) {
98 __request
= new Fields
;
100 __request
->push_back(Field(key
, value
));
103 istream
&HttpURLConnection::getInputStream()
109 return __sock
->getInputStream();
112 ostream
&HttpURLConnection::getOutputStream()
117 return __sock
->getOutputStream();
120 void HttpURLConnection::setDoInput(bool doInput
)
122 this->doInput
= doInput
;
125 void HttpURLConnection::setDoOutput(bool doOutput
)
127 this->doOutput
= doOutput
;
130 void HttpURLConnection::connect()
133 int port
= url
.getPort();
135 const char *protocol
= url
.getProtocol();
136 if (!stricmp(protocol
, "http")) {
138 } else if (!stricmp(protocol
, "ipp")) {
144 __sock
= new Socket(url
.getHost(), port
);
145 if (__sock
->fail()) {
146 __error_msg
= __sock
->getLastError();
153 const char *HttpURLConnection::getContentType()
155 return getHeaderField("Content-Type");
158 const char *HttpURLConnection::getContentEncoding()
160 return getHeaderField("Content-Encoding");
163 int HttpURLConnection::getContentLength()
165 const char *p
= getHeaderField("Content-Length");
166 return p
? atoi(p
) : -1;
169 const char *HttpURLConnection::getHeaderField(const char *s
)
171 if (__response
== NULL
) {
175 for (Fields::iterator it
= __response
->begin(); it
!= __response
->end(); it
++) {
176 if ((*it
).key
== s
) {
177 return (*it
).value
.c_str();
184 HTTP_RESPONSECODE
HttpURLConnection::getResponseCode()
186 if (__response
== NULL
) {
189 return __response_code
;
192 const char *HttpURLConnection::getResponseMessage()
194 if (__response
== NULL
) {
197 return __response_message
.c_str();
200 void HttpURLConnection::action()
213 void HttpURLConnection::setRequest()
216 setRequestProperty("Host", url
.getHost());
217 ostream
&os
= getOutputStream();
218 os
<< __method
<< ' ' << url
.getFile() << " HTTP/1.1" << '\r' << '\n';
219 for (Fields::iterator it
= __request
->begin(); it
!= __request
->end(); it
++) {
220 os
<< (*it
).key
<< ": " << (*it
).value
<< '\r' << '\n';
237 void HttpURLConnection::setContent()
241 void HttpURLConnection::getResponse()
245 if (__response
== NULL
) {
246 __response
= new Fields
;
248 istream
&is
= getInputStream();
252 if (!is
.getline(buffer
, sizeof(buffer
))) {
253 __error_msg
= __sock
->getLastError();
256 buffer
[is
.gcount() - 2] = '\0';
257 __response_message
= buffer
;
259 char *p
= strtok(NULL
, " ");
260 __response_code
= p
? (HTTP_RESPONSECODE
)atoi(p
) : HTTP_UNKNOWN
;
262 while (is
.getline(buffer
, sizeof(buffer
))) {
263 if (buffer
[0] == '\r') {
266 buffer
[is
.gcount() - 2] = '\0';
267 __response
->push_back(Field(buffer
));
270 int size
= getContentLength();
275 if (__response_code
!= HTTP_CONTINUE
) {
276 const char *s
= getHeaderField("Connection");
279 __error_msg
= "cannot found \"Connection\" field";
280 } else if (stricmp(s
, "Keep-Alive")) {
285 switch (__response_code
) {
286 case HTTP_MOVED_TEMP
:
288 const char *p
= getHeaderField("Location");
310 void HttpURLConnection::getContent()
312 const int maxBufSize
= 1024;
314 int size
= getContentLength();
316 istream
&is
= getInputStream();
317 int bufsize
= min(size
, maxBufSize
);
318 char buf
[maxBufSize
];
319 while (size
> 0 && is
.read(buf
, bufsize
)) {