27 #include <sys/types.h>
28 #include <sys/socket.h>
30 #include <tqsocketnotifier.h>
31 #include <tqcstring.h>
33 #include "kresolver.h"
34 #include "ksocketaddress.h"
35 #include "ksocketdevice.h"
36 #include "khttpproxysocketdevice.h"
38 using namespace KNetwork;
42 class KNetwork::KHttpProxySocketDevicePrivate
50 KHttpProxySocketDevicePrivate()
61 : d(new KHttpProxySocketDevicePrivate)
92 d->reply = d->request = TQCString();
111 if (d->proxy.family() == AF_UNSPEC)
128 return parseServerReply();
134 if (
m_sockfd == -1 && (d->proxy.family() == AF_UNSPEC ||
135 node.isEmpty() || service.isEmpty()))
138 setError(IO_ConnectError, NotSupported);
156 TQString request = TQString::fromLatin1(
"CONNECT %1:%2 HTTP/1.1\r\n"
157 "Cache-Control: no-cache\r\n"
160 TQString node2 = node;
161 if (node.contains(
':'))
162 node2 =
'[' + node +
']';
164 d->request = TQString(request.arg(node2).arg(service)).latin1();
167 return parseServerReply();
170 bool KHttpProxySocketDevice::parseServerReply()
174 if (
error() == InProgress)
176 else if (
error() != NoError)
179 if (!d->request.isEmpty())
182 TQ_LONG written =
tqwriteBlock(d->request, d->request.length());
185 qDebug(
"KHttpProxySocketDevice: would block writing request!");
186 if (
error() == WouldBlock)
187 setError(IO_ConnectError, InProgress);
188 return error() == WouldBlock;
190 qDebug(
"KHttpProxySocketDevice: request written");
192 d->request.remove(0, written);
194 if (!d->request.isEmpty())
196 setError(IO_ConnectError, InProgress);
208 TQ_LONG avail = bytesAvailable();
209 qDebug(
"KHttpProxySocketDevice: %ld bytes available", avail);
213 setError(IO_ConnectError, InProgress);
219 TQByteArray buf(avail);
223 TQCString fullHeaders = d->reply + buf.data();
225 index = fullHeaders.find(
"\r\n\r\n");
231 d->reply += buf.data();
232 setError(IO_ConnectError, InProgress);
237 index -= d->reply.length();
238 d->reply += fullHeaders.mid(d->reply.length(), index + 4);
246 if (d->reply.right(3) ==
"\r\n\r")
248 else if (d->reply.right(2) ==
"\r\n")
250 else if (d->reply.right(1) ==
"\r")
257 if ((state == 3 && c ==
'\n') ||
258 (state == 1 && c ==
'\n') ||
267 qDebug(
"KHttpProxySocketDevice: get reply: %s\n",
268 d->reply.left(d->reply.find(
'\r')).data());
269 if (d->reply.left(7) !=
"HTTP/1." ||
270 (index = d->reply.find(
' ')) == -1 ||
271 d->reply[index + 1] !=
'2')
273 setError(IO_ConnectError, NetFailure);