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);
void setError(int status, SocketError error)
Sets the socket's error code and the I/O Device's status.
void resetError()
Resets the socket error code and the I/O Device's status.
virtual int getch()
Reads one character from the socket.
The low-level backend for HTTP proxying.
virtual int capabilities() const
Sets our capabilities.
virtual KSocketAddress peerAddress() const
Return the peer address.
virtual KSocketAddress externalAddress() const
Return the externally visible address.
static KResolverEntry defaultProxy
This is the default proxy server to be used.
void setProxyServer(const KResolverEntry &proxy)
Sets the proxy server address.
const KResolverEntry & proxyServer() const
Retrieves the proxy server address.
virtual void close()
Closes the socket.
virtual bool connect(const KResolverEntry &address)
Overrides connection.
virtual ~KHttpProxySocketDevice()
Destructor.
KHttpProxySocketDevice(const KSocketBase *=0L)
Constructor.
KSocketAddress address() const
Retrieves the socket address associated with this entry.
A generic socket address.
virtual TQString nodeName() const
Returns the node name of this socket.
virtual TQString serviceName() const
Returns the service name for this socket.
Basic socket functionality.
bool blocking() const
Retrieves this socket's blocking mode.
SocketError error() const
Retrieves the socket error code.
Low-level socket functionality.
virtual TQT_TQIO_LONG tqwriteBlock(const char *data, TQT_TQIO_ULONG len)
Writes data to the socket.
virtual void close()
Closes the socket.
@ CanNotListen
Can not listen.
@ CanConnectString
Can connect to hostnames.
@ CanNotUseDatagrams
Can not use datagrams.
@ CanNotBind
Can not bind.
virtual bool connect(const KResolverEntry &address)
Connect to a remote host.
virtual TQT_TQIO_LONG tqreadBlock(char *data, TQT_TQIO_ULONG maxlen)
Reads data from this socket.
int m_sockfd
The socket file descriptor.
virtual TQ_LONG peekBlock(char *data, TQ_ULONG maxlen)
Peeks data in the socket.
A namespace to store all networking-related (socket) classes.