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"
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) {
177 else if (
error() != NoError) {
182 if (!d->request.isEmpty())
185 TQ_LONG written =
tqwriteBlock(d->request, d->request.length());
188 qDebug(
"KHttpProxySocketDevice: would block writing request!");
189 if (
error() == WouldBlock)
190 setError(IO_ConnectError, InProgress);
191 return error() == WouldBlock;
193 qDebug(
"KHttpProxySocketDevice: request written");
195 d->request.remove(0, written);
197 if (!d->request.isEmpty())
199 setError(IO_ConnectError, InProgress);
211 TQ_LONG avail = bytesAvailable();
212 qDebug(
"KHttpProxySocketDevice: %ld bytes available", avail);
216 setError(IO_ConnectError, InProgress);
222 TQByteArray buf(avail);
226 TQCString fullHeaders = d->reply + buf.data();
228 index = fullHeaders.find(
"\r\n\r\n");
234 d->reply += buf.data();
235 setError(IO_ConnectError, InProgress);
240 index -= d->reply.length();
241 d->reply += fullHeaders.mid(d->reply.length(), index + 4);
249 if (d->reply.right(3) ==
"\r\n\r")
251 else if (d->reply.right(2) ==
"\r\n")
253 else if (d->reply.right(1) ==
"\r")
260 if ((state == 3 && c ==
'\n') ||
261 (state == 1 && c ==
'\n') ||
270 qDebug(
"KHttpProxySocketDevice: get reply: %s\n",
271 d->reply.left(d->reply.find(
'\r')).data());
272 if (d->reply.left(7) !=
"HTTP/1." ||
273 (index = d->reply.find(
' ')) == -1 ||
274 d->reply[index + 1] !=
'2')
276 setError(IO_ConnectError, NetFailure);
virtual TQString nodeName() const
Returns the node name of this socket.
bool blocking() const
Retrieves this socket's blocking mode.
virtual TQString serviceName() const
Returns the service name for this socket.
const KResolverEntry & proxyServer() const
Retrieves the proxy server address.
KHttpProxySocketDevice(const KSocketBase *=0L)
Constructor.
virtual int getch()
Reads one character from the socket.
void resetError()
Resets the socket error code and the I/O Device's status.
SocketError error() const
Retrieves the socket error code.
A generic socket address.
Can connect to hostnames.
virtual bool connect(const KResolverEntry &address)
Overrides connection.
virtual TQ_LONG peekBlock(char *data, TQ_ULONG maxlen)
Peeks data in the socket.
A namespace to store all networking-related (socket) classes.
static KResolverEntry defaultProxy
This is the default proxy server to be used.
virtual KSocketAddress peerAddress() const
Return the peer address.
The low-level backend for HTTP proxying.
void setError(int status, SocketError error)
Sets the socket's error code and the I/O Device's status.
virtual TQT_TQIO_LONG tqwriteBlock(const char *data, TQT_TQIO_ULONG len)
Writes data to the socket.
KSocketAddress address() const
Retrieves the socket address associated with this entry.
void setProxyServer(const KResolverEntry &proxy)
Sets the proxy server address.
virtual int capabilities() const
Sets our capabilities.
virtual bool connect(const KResolverEntry &address)
Connect to a remote host.
virtual void close()
Closes the socket.
int m_sockfd
The socket file descriptor.
virtual void close()
Closes the socket.
virtual KSocketAddress externalAddress() const
Return the externally visible address.
Low-level socket functionality.
virtual ~KHttpProxySocketDevice()
Destructor.
virtual TQT_TQIO_LONG tqreadBlock(char *data, TQT_TQIO_ULONG maxlen)
Reads data from this socket.
Basic socket functionality.