28#include <sys/socket.h>
30#include <tqsocketnotifier.h>
34#include "tdesocketaddress.h"
35#include "tdesocketdevice.h"
36#include "khttpproxysocketdevice.h"
42class KNetwork::KHttpProxySocketDevicePrivate
50 KHttpProxySocketDevicePrivate()
55KHttpProxySocketDevice::KHttpProxySocketDevice(
const TDESocketBase* parent)
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();
170bool KHttpProxySocketDevice::parseServerReply()
174 if (
error() == InProgress) {
177 else if (
error() != NoError) {
182 if (!d->request.isEmpty())
185 TQ_LONG written =
writeBlock(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);
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);
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.
KHttpProxySocketDevice(const TDESocketBase *=0L)
Constructor.
void setProxyServer(const KResolverEntry &proxy)
Sets the proxy server address.
const KResolverEntry & proxyServer() const
Retrieves the proxy server address.
virtual TDESocketAddress peerAddress() const
Return the peer address.
virtual void close()
Closes the socket.
virtual bool connect(const KResolverEntry &address)
Overrides connection.
virtual ~KHttpProxySocketDevice()
Destructor.
virtual TDESocketAddress externalAddress() const
Return the externally visible address.
TDESocketAddress address() const
Retrieves the socket address associated with this entry.
A generic socket address.
virtual TQString serviceName() const
Returns the service name for this socket.
virtual TQString nodeName() const
Returns the node name of this socket.
Basic socket functionality.
SocketError error() const
Retrieves the socket error code.
bool blocking() const
Retrieves this socket's blocking mode.
Low-level socket functionality.
virtual TQ_LONG peekBlock(char *data, TQ_ULONG maxlen)
Peeks data in the socket.
virtual TQ_LONG writeBlock(const char *data, TQ_ULONG len)
Writes data to the socket.
virtual void close()
Closes the socket.
virtual TQ_LONG readBlock(char *data, TQ_ULONG maxlen)
Reads data from this socket.
int m_sockfd
The socket file descriptor.
virtual TQ_LONG bytesAvailable() const
Returns the number of bytes available for reading without blocking.
virtual bool connect(const KResolverEntry &address)
Connect to a remote host.
@ CanConnectString
Can connect to hostnames.
@ CanNotUseDatagrams
Can not use datagrams.
@ CanNotListen
Can not listen.
@ CanNotBind
Can not bind.
A namespace to store all networking-related (socket) classes.