27 #include <sys/types.h>
28 #include <sys/socket.h>
30 #include <tqsocketnotifier.h>
31 #include <tqcstring.h>
33 #include "kresolver.h"
34 #include "tdesocketaddress.h"
35 #include "tdesocketdevice.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 tqDebug(
"KHttpProxySocketDevice: would block writing request!");
189 if (
error() == WouldBlock)
190 setError(IO_ConnectError, InProgress);
191 return error() == WouldBlock;
193 tqDebug(
"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 tqDebug(
"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 tqDebug(
"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);
int m_sockfd
The socket file descriptor.
@ CanNotListen
Can not listen.
The low-level backend for HTTP proxying.
virtual TQString nodeName() const
Returns the node name of this socket.
A namespace to store all networking-related (socket) classes.
virtual bool connect(const KResolverEntry &address)
Connect to a remote host.
virtual bool connect(const KResolverEntry &address)
Overrides connection.
virtual int getch()
Reads one character from the socket.
void resetError()
Resets the socket error code and the I/O Device's status.
Basic socket functionality.
Low-level socket functionality.
@ CanNotBind
Can not bind.
static KResolverEntry defaultProxy
This is the default proxy server to be used.
virtual TQT_TQIO_LONG tqwriteBlock(const char *data, TQT_TQIO_ULONG len)
Writes data to the socket.
const KResolverEntry & proxyServer() const
Retrieves the proxy server address.
virtual void close()
Closes the socket.
virtual TQ_LONG peekBlock(char *data, TQ_ULONG maxlen)
Peeks data in the socket.
virtual void close()
Closes the socket.
@ CanNotUseDatagrams
Can not use datagrams.
void setError(int status, SocketError error)
Sets the socket's error code and the I/O Device's status.
@ CanConnectString
Can connect to hostnames.
virtual ~KHttpProxySocketDevice()
Destructor.
KHttpProxySocketDevice(const TDESocketBase *=0L)
Constructor.
void setProxyServer(const KResolverEntry &proxy)
Sets the proxy server address.
bool blocking() const
Retrieves this socket's blocking mode.
virtual TDESocketAddress peerAddress() const
Return the peer address.
TDESocketAddress address() const
Retrieves the socket address associated with this entry.
virtual TQString serviceName() const
Returns the service name for this socket.
virtual TDESocketAddress externalAddress() const
Return the externally visible address.
A generic socket address.
virtual TQT_TQIO_LONG tqreadBlock(char *data, TQT_TQIO_ULONG maxlen)
Reads data from this socket.
SocketError error() const
Retrieves the socket error code.
virtual int capabilities() const
Sets our capabilities.