1 /**
2 * Phoebe DOM Implementation.
3 *
4 * This is a C++ approximation of the W3C DOM model, which follows
5 * fairly closely the specifications in the various .idl files, copies of
6 * which are provided for reference. Most important is this one:
7 *
8 * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html
9 *
10 * Authors:
11 * Bob Jamison
12 *
13 * Copyright (C) 2006 Bob Jamison
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 */
32 #include "httpclient.h"
34 namespace org
35 {
36 namespace w3c
37 {
38 namespace dom
39 {
40 namespace io
41 {
46 /**
47 *
48 */
49 HttpClient::HttpClient()
50 {
51 }
54 /**
55 *
56 */
57 HttpClient::~HttpClient()
58 {
59 }
62 /**
63 *
64 */
65 bool HttpClient::openGet(const URI &uri)
66 {
68 socket.disconnect();
70 if (uri.getScheme() == URI::SCHEME_HTTP)
71 socket.enableSSL(false);
72 else if (uri.getScheme() == URI::SCHEME_HTTPS)
73 socket.enableSSL(true);
74 else
75 {
76 printf("Bad proto scheme:%d\n", uri.getScheme());
77 return false;
78 }
80 DOMString host = uri.getHost();
81 int port = uri.getPort();
82 DOMString path = uri.getPath();
83 if (path.size() == 0)
84 path = "/";
86 //printf("host:%s port:%d, path:%s\n", host.c_str(), port, path.c_str());
88 if (!socket.connect(host, port))
89 {
90 return false;
91 }
93 DOMString msg = "GET ";
94 msg.append(path);
95 msg.append(" HTTP/1.0\r\n\r\n");
96 //printf("msg:'%s'\n", msg.c_str());
98 //# Make the request
99 if (!socket.write(msg))
100 {
101 return false;
102 }
104 //# Read the HTTP headers
105 while (true)
106 {
107 if (!socket.readLine(msg))
108 return false;
109 printf("header:'%s'\n", msg.c_str());
110 if (msg.size() < 1)
111 break;
112 }
114 return true;
115 }
118 /**
119 *
120 */
121 int HttpClient::read()
122 {
123 int ret = socket.read();
124 return ret;
125 }
127 /**
128 *
129 */
130 bool HttpClient::write(int ch)
131 {
132 if (!socket.write(ch))
133 return false;
134 return true;
135 }
137 /**
138 *
139 */
140 bool HttpClient::write(const DOMString &msg)
141 {
142 if (!socket.write(msg))
143 return false;
144 return true;
145 }
147 /**
148 *
149 */
150 bool HttpClient::close()
151 {
152 socket.disconnect();
153 return true;
154 }
158 } //namespace io
159 } //namespace dom
160 } //namespace w3c
161 } //namespace org
164 //#########################################################################
165 //# E N D O F F I L E
166 //#########################################################################