* Added wxsocket lib and sample (I hope I don't forget some file)
* Updated some wx data and makefiles * Updates on wxStream (reorganization) makefile for Windows will nearly follow wxSocket should work on wxGTK (I've tested it) * IPC over Network is included git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@684 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -26,6 +26,10 @@
|
||||
|
||||
#include "wx/datstrm.h"
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// wxDataInputStream
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
wxDataInputStream::wxDataInputStream(wxInputStream& s)
|
||||
: wxFilterInputStream(s)
|
||||
{
|
||||
@@ -126,6 +130,10 @@ wxString wxDataInputStream::ReadString()
|
||||
return wx_string;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// wxDataOutputStream
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
wxDataOutputStream::wxDataOutputStream(wxOutputStream& s)
|
||||
: wxFilterOutputStream(s)
|
||||
{
|
||||
@@ -192,3 +200,12 @@ void wxDataOutputStream::WriteDouble(double d)
|
||||
#endif
|
||||
Write(buf, 10);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// wxDataStream
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
wxDataStream::wxDataStream(wxStream& stream)
|
||||
: wxDataInputStream(stream), wxDataOutputStream(stream)
|
||||
{
|
||||
}
|
||||
|
378
src/common/ftp.cpp
Normal file
378
src/common/ftp.cpp
Normal file
@@ -0,0 +1,378 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Name: ftp.cpp
|
||||
// Purpose: FTP protocol
|
||||
// Author: Guilhem Lavaux
|
||||
// Modified by:
|
||||
// Created: 07/07/1997
|
||||
// RCS-ID: $Id$
|
||||
// Copyright: (c) 1997, 1998 Guilhem Lavaux
|
||||
// Licence: wxWindows license
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation "ftp.h"
|
||||
#endif
|
||||
#include <memory.h>
|
||||
#include <stdlib.h>
|
||||
#include "wx/string.h"
|
||||
#include "wx/utils.h"
|
||||
// #include "wx/data.h"
|
||||
#define WXSOCK_INTERNAL
|
||||
#include "wx/sckaddr.h"
|
||||
#undef WXSOCK_INTERNAL
|
||||
#include "wx/socket.h"
|
||||
#include "wx/url.h"
|
||||
#include "wx/sckstrm.h"
|
||||
#include "wx/protocol/protocol.h"
|
||||
#include "wx/protocol/ftp.h"
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
#pragma hdrstop
|
||||
#endif
|
||||
|
||||
#define FTP_BSIZE 1024
|
||||
|
||||
#if !USE_SHARED_LIBRARY
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxFTP, wxProtocol)
|
||||
IMPLEMENT_PROTOCOL(wxFTP, "ftp", "ftp", TRUE)
|
||||
#endif
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
////// wxFTP constructor and destructor ////////////////////////
|
||||
////////////////////////////////////////////////////////////////
|
||||
|
||||
wxFTP::wxFTP()
|
||||
: wxProtocol()
|
||||
{
|
||||
char tmp[256];
|
||||
|
||||
m_lastError = wxPROTO_NOERR;
|
||||
m_streaming = FALSE;
|
||||
|
||||
m_user = "anonymous";
|
||||
wxGetUserName(tmp, 256);
|
||||
m_passwd.sprintf("%s@",tmp);
|
||||
wxGetHostName(tmp, 256);
|
||||
m_passwd += tmp;
|
||||
|
||||
SetNotify(0);
|
||||
}
|
||||
|
||||
wxFTP::~wxFTP()
|
||||
{
|
||||
SendCommand("QUIT", '2');
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
////// wxFTP connect and login methods /////////////////////////
|
||||
////////////////////////////////////////////////////////////////
|
||||
bool wxFTP::Connect(wxSockAddress& addr)
|
||||
{
|
||||
if (!m_handler) {
|
||||
m_lastError = wxPROTO_NOHNDLR;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!wxProtocol::Connect(addr)) {
|
||||
m_lastError = wxPROTO_NETERR;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!m_user || !m_passwd) {
|
||||
m_lastError = wxPROTO_CONNERR;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
wxString command;
|
||||
|
||||
if (!GetResult('2')) {
|
||||
Close();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
command.sprintf("USER %s", (const char *)m_user);
|
||||
if (!SendCommand(command, '3')) {
|
||||
Close();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
command.sprintf("PASS %s", (const char *)m_passwd);
|
||||
if (!SendCommand(command, '2')) {
|
||||
Close();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxFTP::Connect(const wxString& host)
|
||||
{
|
||||
wxIPV4address addr;
|
||||
wxString my_host = host;
|
||||
|
||||
addr.Hostname(my_host);
|
||||
addr.Service("ftp");
|
||||
|
||||
return Connect(addr);
|
||||
}
|
||||
|
||||
bool wxFTP::Close()
|
||||
{
|
||||
if (m_streaming) {
|
||||
m_lastError = wxPROTO_STREAMING;
|
||||
return FALSE;
|
||||
}
|
||||
if (m_connected)
|
||||
SendCommand(wxString("QUIT"), '2');
|
||||
return wxSocketClient::Close();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
////// wxFTP low-level methods /////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////
|
||||
bool wxFTP::SendCommand(const wxString& command, char exp_ret)
|
||||
{
|
||||
wxString tmp_str;
|
||||
|
||||
if (m_streaming) {
|
||||
m_lastError = wxPROTO_STREAMING;
|
||||
return FALSE;
|
||||
}
|
||||
tmp_str = command + "\r\n";
|
||||
if (Write((char *)tmp_str.GetData(), tmp_str.Length()).Error()) {
|
||||
m_lastError = wxPROTO_NETERR;
|
||||
return FALSE;
|
||||
}
|
||||
return GetResult(exp_ret);
|
||||
}
|
||||
|
||||
bool wxFTP::GetResult(char exp)
|
||||
{
|
||||
if ((m_lastError = GetLine(this, m_lastResult)))
|
||||
return FALSE;
|
||||
if (m_lastResult[0UL] != exp) {
|
||||
m_lastError = wxPROTO_PROTERR;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (m_lastResult[3UL] == '-') {
|
||||
wxString key = m_lastResult.Left((size_t)3);
|
||||
|
||||
key += ' ';
|
||||
|
||||
while (m_lastResult.Index(key) != 0) {
|
||||
if ((m_lastError = GetLine(this, m_lastResult)))
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
////// wxFTP low-level methods /////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////
|
||||
bool wxFTP::ChDir(const wxString& dir)
|
||||
{
|
||||
wxString str = dir;
|
||||
|
||||
str.Prepend("CWD ");
|
||||
return SendCommand(str, '2');
|
||||
}
|
||||
|
||||
bool wxFTP::MkDir(const wxString& dir)
|
||||
{
|
||||
wxString str = dir;
|
||||
str.Prepend("MKD ");
|
||||
return SendCommand(str, '2');
|
||||
}
|
||||
|
||||
bool wxFTP::RmDir(const wxString& dir)
|
||||
{
|
||||
wxString str = dir;
|
||||
|
||||
str.Prepend("PWD ");
|
||||
return SendCommand(str, '2');
|
||||
}
|
||||
|
||||
wxString wxFTP::Pwd()
|
||||
{
|
||||
int beg, end;
|
||||
|
||||
if (!SendCommand("PWD", '2'))
|
||||
return wxString((char *)NULL);
|
||||
|
||||
beg = m_lastResult.Find('\"',FALSE);
|
||||
end = m_lastResult.Find('\"',TRUE);
|
||||
|
||||
return wxString(beg+1, end);
|
||||
}
|
||||
|
||||
bool wxFTP::Rename(const wxString& src, const wxString& dst)
|
||||
{
|
||||
wxString str;
|
||||
|
||||
str = "RNFR " + src;
|
||||
if (!SendCommand(str, '3'))
|
||||
return FALSE;
|
||||
|
||||
str = "RNTO " + dst;
|
||||
return SendCommand(str, '2');
|
||||
}
|
||||
|
||||
bool wxFTP::RmFile(const wxString& path)
|
||||
{
|
||||
wxString str;
|
||||
|
||||
str = "DELE ";
|
||||
str += path;
|
||||
return SendCommand(str, '2');
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
////// wxFTP download*upload ///////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////
|
||||
|
||||
class wxInputFTPStream : public wxSocketInputStream {
|
||||
public:
|
||||
wxFTP *m_ftp;
|
||||
|
||||
wxInputFTPStream(wxFTP *ftp_clt, wxSocketBase *sock)
|
||||
: wxSocketInputStream(*sock), m_ftp(ftp_clt) {}
|
||||
virtual ~wxInputFTPStream(void)
|
||||
{
|
||||
if (Eof())
|
||||
m_ftp->GetResult('2');
|
||||
else
|
||||
m_ftp->Abort();
|
||||
delete m_i_socket;
|
||||
}
|
||||
};
|
||||
|
||||
class wxOutputFTPStream : public wxSocketOutputStream {
|
||||
public:
|
||||
wxFTP *m_ftp;
|
||||
|
||||
wxOutputFTPStream(wxFTP *ftp_clt, wxSocketBase *sock)
|
||||
: wxSocketOutputStream(*sock), m_ftp(ftp_clt) {}
|
||||
virtual ~wxOutputFTPStream(void)
|
||||
{
|
||||
if (Bad())
|
||||
m_ftp->GetResult('2');
|
||||
else
|
||||
m_ftp->Abort();
|
||||
delete m_o_socket;
|
||||
}
|
||||
};
|
||||
|
||||
wxSocketClient *wxFTP::GetPort()
|
||||
{
|
||||
wxIPV4address addr;
|
||||
wxSocketClient *client;
|
||||
struct sockaddr sin;
|
||||
int a[6];
|
||||
wxString straddr;
|
||||
int addr_pos;
|
||||
|
||||
if (!SendCommand("PASV", '2'))
|
||||
return NULL;
|
||||
|
||||
sin.sa_family = AF_INET;
|
||||
addr_pos = m_lastResult.Find('(');
|
||||
if (addr_pos == -1) {
|
||||
m_lastError = wxPROTO_PROTERR;
|
||||
return NULL;
|
||||
}
|
||||
straddr = m_lastResult(addr_pos+1, m_lastResult.Length());
|
||||
sscanf((const char *)straddr,"%d,%d,%d,%d,%d,%d",&a[2],&a[3],&a[4],&a[5],&a[0],&a[1]);
|
||||
sin.sa_data[2] = (char)a[2];
|
||||
sin.sa_data[3] = (char)a[3];
|
||||
sin.sa_data[4] = (char)a[4];
|
||||
sin.sa_data[5] = (char)a[5];
|
||||
sin.sa_data[0] = (char)a[0];
|
||||
sin.sa_data[1] = (char)a[1];
|
||||
|
||||
addr.Disassemble(&sin, sizeof(sin));
|
||||
|
||||
client = m_handler->CreateClient();
|
||||
if (!client->Connect(addr)) {
|
||||
delete client;
|
||||
return NULL;
|
||||
}
|
||||
client->Notify(FALSE);
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
bool wxFTP::Abort(void)
|
||||
{
|
||||
m_streaming = FALSE;
|
||||
if (!SendCommand("ABOR", '4'))
|
||||
return FALSE;
|
||||
return GetResult('2');
|
||||
}
|
||||
|
||||
wxInputStream *wxFTP::GetInputStream(const wxString& path)
|
||||
{
|
||||
wxString tmp_str;
|
||||
|
||||
if (!SendCommand("TYPE I", '2'))
|
||||
return NULL;
|
||||
|
||||
wxSocketClient *sock = GetPort();
|
||||
|
||||
if (!sock) {
|
||||
m_lastError = wxPROTO_NETERR;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
tmp_str = "RETR " + path;
|
||||
if (!SendCommand(tmp_str, '1'))
|
||||
return NULL;
|
||||
|
||||
return new wxInputFTPStream(this, sock);
|
||||
}
|
||||
|
||||
wxOutputStream *wxFTP::GetOutputStream(const wxString& path)
|
||||
{
|
||||
wxString tmp_str;
|
||||
|
||||
if (!SendCommand("TYPE I", '2'))
|
||||
return NULL;
|
||||
|
||||
wxSocketClient *sock = GetPort();
|
||||
|
||||
tmp_str = "STOR " + path;
|
||||
if (!SendCommand(tmp_str, '1'))
|
||||
return FALSE;
|
||||
|
||||
return new wxOutputFTPStream(this, sock);
|
||||
}
|
||||
|
||||
wxList *wxFTP::GetList(const wxString& wildcard)
|
||||
{
|
||||
wxList *file_list = new wxList;
|
||||
wxSocketBase *sock = GetPort();
|
||||
wxString tmp_str = "NLST";
|
||||
|
||||
if (!wildcard.IsNull())
|
||||
tmp_str += wildcard;
|
||||
|
||||
if (!SendCommand(tmp_str, '1')) {
|
||||
delete sock;
|
||||
delete file_list;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (GetLine(sock, tmp_str) == wxPROTO_NOERR) {
|
||||
file_list->Append((wxObject *)(new wxString(tmp_str)));
|
||||
}
|
||||
|
||||
if (!GetResult('2')) {
|
||||
delete sock;
|
||||
file_list->DeleteContents(TRUE);
|
||||
delete file_list;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return file_list;
|
||||
}
|
251
src/common/http.cpp
Normal file
251
src/common/http.cpp
Normal file
@@ -0,0 +1,251 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Name: http.cpp
|
||||
// Purpose: HTTP protocol
|
||||
// Author: Guilhem Lavaux
|
||||
// Modified by:
|
||||
// Created: August 1997
|
||||
// RCS-ID: $Id$
|
||||
// Copyright: (c) 1997, 1998 Guilhem Lavaux
|
||||
// Licence: wxWindows license
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation "http.h"
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "wx/string.h"
|
||||
#include "wx/tokenzr.h"
|
||||
#include "wx/socket.h"
|
||||
#include "wx/protocol/protocol.h"
|
||||
#include "wx/protocol/http.h"
|
||||
#include "wx/sckstrm.h"
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
#pragma hdrstop
|
||||
#endif
|
||||
|
||||
#if !USE_SHARED_LIBRARY
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol)
|
||||
IMPLEMENT_PROTOCOL(wxHTTP, "http", "80", TRUE)
|
||||
#endif
|
||||
|
||||
#define HTTP_BSIZE 2048
|
||||
|
||||
wxHTTP::wxHTTP()
|
||||
: wxProtocol(),
|
||||
m_headers(wxKEY_STRING)
|
||||
{
|
||||
m_addr = NULL;
|
||||
m_read = FALSE;
|
||||
|
||||
SetNotify(REQ_LOST);
|
||||
}
|
||||
|
||||
wxHTTP::~wxHTTP()
|
||||
{
|
||||
// wxString isn't a wxObject
|
||||
wxNode *node = m_headers.First();
|
||||
wxString *string;
|
||||
|
||||
while (node) {
|
||||
string = (wxString *)node->Data();
|
||||
delete string;
|
||||
node = node->Next();
|
||||
}
|
||||
}
|
||||
|
||||
wxString wxHTTP::GetContentType()
|
||||
{
|
||||
return GetHeader("Content-Type");
|
||||
}
|
||||
|
||||
void wxHTTP::SetHeader(const wxString& header, const wxString& h_data)
|
||||
{
|
||||
if (m_read) {
|
||||
m_headers.Clear();
|
||||
m_read = FALSE;
|
||||
}
|
||||
|
||||
wxNode *node = m_headers.Find(header);
|
||||
|
||||
if (!node)
|
||||
m_headers.Append(header, (wxObject *)(new wxString(h_data)));
|
||||
else {
|
||||
wxString *str = (wxString *)node->Data();
|
||||
(*str) = h_data;
|
||||
}
|
||||
}
|
||||
|
||||
wxString wxHTTP::GetHeader(const wxString& header)
|
||||
{
|
||||
wxNode *node = m_headers.Find(header);
|
||||
if (!node)
|
||||
return (char *)NULL;
|
||||
|
||||
return *((wxString *)node->Data());
|
||||
}
|
||||
|
||||
void wxHTTP::SendHeaders()
|
||||
{
|
||||
wxNode *head = m_headers.First();
|
||||
|
||||
while (head) {
|
||||
wxString *str = (wxString *)head->Data();
|
||||
char buf[100];
|
||||
|
||||
sprintf(buf, "%s: %s\n\r", head->key.string, str->GetData());
|
||||
Write(buf, strlen(buf));
|
||||
|
||||
head = head->Next();
|
||||
}
|
||||
}
|
||||
|
||||
bool wxHTTP::ParseHeaders()
|
||||
{
|
||||
wxString line;
|
||||
|
||||
m_headers.Clear();
|
||||
m_read = TRUE;
|
||||
|
||||
while (1) {
|
||||
m_error = GetLine(this, line);
|
||||
if (m_error != wxPROTO_NOERR)
|
||||
return FALSE;
|
||||
|
||||
if (line.Length() == 0)
|
||||
break;
|
||||
|
||||
int pos = line.Find(':');
|
||||
if (pos == -1)
|
||||
return FALSE;
|
||||
|
||||
wxString left_str = line(0, pos);
|
||||
wxString right_str = line(pos+1, line.Length());
|
||||
|
||||
right_str = right_str.Strip(wxString::leading);
|
||||
|
||||
wxString *str = new wxString(right_str);
|
||||
|
||||
m_headers.Append(left_str, (wxObject *) str);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxHTTP::Connect(const wxString& host)
|
||||
{
|
||||
wxIPV4address *addr;
|
||||
|
||||
if (m_connected) {
|
||||
delete m_addr;
|
||||
m_addr = NULL;
|
||||
Close();
|
||||
}
|
||||
|
||||
m_addr = addr = new wxIPV4address();
|
||||
|
||||
if (!addr->Hostname(host)) {
|
||||
delete m_addr;
|
||||
m_addr = NULL;
|
||||
m_error = wxPROTO_NETERR;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!addr->Service("http"))
|
||||
addr->Service(80);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxHTTP::Connect(wxSockAddress& addr)
|
||||
{
|
||||
struct sockaddr *raw_addr;
|
||||
size_t len;
|
||||
|
||||
m_addr = (wxSockAddress *)(addr.GetClassInfo()->CreateObject());
|
||||
|
||||
addr.Build(raw_addr, len);
|
||||
m_addr->Disassemble(raw_addr, len);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
|
||||
{
|
||||
char *tmp_buf;
|
||||
char buf[HTTP_BSIZE];
|
||||
|
||||
switch (req) {
|
||||
case wxHTTP_GET:
|
||||
tmp_buf = "GET";
|
||||
break;
|
||||
default:
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
sprintf(buf, "%s %s HTTP/1.0\n\r", tmp_buf, (const char *)path);
|
||||
Write(buf, strlen(buf));
|
||||
SendHeaders();
|
||||
sprintf(buf, "\n\r");
|
||||
Write(buf, strlen(buf));
|
||||
|
||||
wxString tmp_str;
|
||||
|
||||
m_error = GetLine(this, tmp_str);
|
||||
if (m_error != wxPROTO_NOERR)
|
||||
return FALSE;
|
||||
|
||||
if (!tmp_str.Contains("HTTP/")) {
|
||||
// TODO: support HTTP v0.9 which can have no header.
|
||||
SetHeader("Content-Length", "-1");
|
||||
SetHeader("Content-Type", "none/none");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
wxStringTokenizer token(tmp_str,' ');
|
||||
wxString tmp_str2;
|
||||
|
||||
token.NextToken();
|
||||
tmp_str2 = token.NextToken();
|
||||
|
||||
switch (atoi(tmp_str2)) {
|
||||
case 200:
|
||||
break;
|
||||
default:
|
||||
m_error = wxPROTO_NOFILE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return ParseHeaders();
|
||||
}
|
||||
|
||||
class wxHTTPStream : public wxSocketInputStream {
|
||||
public:
|
||||
wxHTTP *m_http;
|
||||
wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
|
||||
virtual ~wxHTTPStream(void) { m_http->Abort(); }
|
||||
};
|
||||
|
||||
bool wxHTTP::Abort(void)
|
||||
{
|
||||
return wxSocketClient::Close();
|
||||
}
|
||||
|
||||
wxInputStream *wxHTTP::GetInputStream(const wxString& path)
|
||||
{
|
||||
wxHTTPStream *inp_stream = new wxHTTPStream(this);
|
||||
|
||||
if (!m_addr || m_connected) {
|
||||
m_error = wxPROTO_CONNERR;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!wxProtocol::Connect(*m_addr))
|
||||
return NULL;
|
||||
|
||||
if (!BuildRequest(path, wxHTTP_GET))
|
||||
return NULL;
|
||||
|
||||
return inp_stream;
|
||||
}
|
109
src/common/protocol.cpp
Normal file
109
src/common/protocol.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Name: protocol.cpp
|
||||
// Purpose: Implement protocol base class
|
||||
// Author: Guilhem Lavaux
|
||||
// Modified by:
|
||||
// Created: 07/07/1997
|
||||
// RCS-ID: $Id$
|
||||
// Copyright: (c) 1997, 1998 Guilhem Lavaux
|
||||
// Licence: wxWindows license
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation "protocol.h"
|
||||
#endif
|
||||
|
||||
#ifdef WXPREC
|
||||
#include <wx/wxprec.h>
|
||||
#else
|
||||
#include <wx/wx.h>
|
||||
#endif
|
||||
|
||||
#include "wx/protocol/protocol.h"
|
||||
#include "wx/url.h"
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
#pragma hdrstop
|
||||
#endif
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
// wxProtoInfo
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
* --------------------------------------------------------------
|
||||
* --------- wxProtoInfo CONSTRUCTOR ----------------------------
|
||||
* --------------------------------------------------------------
|
||||
*/
|
||||
|
||||
wxProtoInfo::wxProtoInfo(const char *name, const char *serv,
|
||||
const bool need_host1, wxClassInfo *info)
|
||||
{
|
||||
m_protoname = name;
|
||||
m_servname = serv;
|
||||
m_cinfo = info;
|
||||
m_needhost = need_host1;
|
||||
next = wxURL::g_protocols;
|
||||
wxURL::g_protocols = this;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
// wxProtocol ///////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient)
|
||||
|
||||
wxProtocol::wxProtocol()
|
||||
: wxSocketClient()
|
||||
{
|
||||
}
|
||||
|
||||
bool wxProtocol::Reconnect()
|
||||
{
|
||||
wxIPV4address addr;
|
||||
|
||||
if (!GetPeer(addr)) {
|
||||
Close();
|
||||
return FALSE;
|
||||
}
|
||||
if (!Close())
|
||||
return FALSE;
|
||||
if (!Connect(addr))
|
||||
return FALSE;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
wxProtocolError GetLine(wxSocketBase *sock, wxString& result) {
|
||||
#define PROTO_BSIZE 2048
|
||||
size_t avail, size;
|
||||
char tmp_buf[PROTO_BSIZE], tmp_str[PROTO_BSIZE];
|
||||
char *ret;
|
||||
bool found;
|
||||
|
||||
avail = sock->Read(tmp_buf, PROTO_BSIZE).LastCount();
|
||||
if (sock->LastError() != 0 || avail == 0)
|
||||
return wxPROTO_NETERR;
|
||||
|
||||
memcpy(tmp_str, tmp_buf, avail);
|
||||
|
||||
// Not implemented on all systems
|
||||
// ret = (char *)memccpy(tmp_str, tmp_buf, '\n', avail);
|
||||
found = FALSE;
|
||||
for (ret=tmp_str;ret < (tmp_str+avail); ret++)
|
||||
if (*ret == '\n') {
|
||||
found = TRUE;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!found)
|
||||
return wxPROTO_PROTERR;
|
||||
*ret = 0;
|
||||
|
||||
result = tmp_str;
|
||||
result = result.Left(result.Length()-1);
|
||||
|
||||
size = ret-tmp_str+1;
|
||||
sock->CreatePushbackBefore(&tmp_buf[size], avail-size);
|
||||
return wxPROTO_NOERR;
|
||||
#undef PROTO_BSIZE
|
||||
}
|
391
src/common/sckaddr.cpp
Normal file
391
src/common/sckaddr.cpp
Normal file
@@ -0,0 +1,391 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Name: sckaddr.cpp
|
||||
// Purpose: Network address manager
|
||||
// Author: Guilhem Lavaux
|
||||
// Modified by:
|
||||
// Created: 26/04/97
|
||||
// RCS-ID: $Id$
|
||||
// Copyright: (c) 1997, 1998 Guilhem Lavaux
|
||||
// Licence: wxWindows license
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation "sckaddr.h"
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <memory.h>
|
||||
|
||||
#include "wx/defs.h"
|
||||
#include "wx/object.h"
|
||||
|
||||
#if defined(__WINDOWS__)
|
||||
#include <winsock.h>
|
||||
#endif // __WINDOWS__
|
||||
|
||||
#if defined(__UNIX__)
|
||||
|
||||
#ifdef VMS
|
||||
#include <socket.h>
|
||||
#include <in.h>
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#endif // __UNIX__
|
||||
|
||||
#include "wx/sckaddr.h"
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
#pragma hdrstop
|
||||
#endif
|
||||
|
||||
#define CHECK_ADDRTYPE(var, type)
|
||||
|
||||
#if !USE_SHARED_LIBRARY
|
||||
IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject)
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxSockAddress)
|
||||
#ifdef ENABLE_IPV6
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxSockAddress)
|
||||
#endif
|
||||
#ifdef __UNIX__
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
wxIPV4address::wxIPV4address()
|
||||
{
|
||||
m_addr = new sockaddr_in;
|
||||
Clear();
|
||||
}
|
||||
|
||||
wxIPV4address::~wxIPV4address()
|
||||
{
|
||||
}
|
||||
|
||||
int wxIPV4address::SockAddrLen()
|
||||
{
|
||||
return sizeof(*m_addr);
|
||||
}
|
||||
|
||||
int wxIPV4address::GetFamily()
|
||||
{
|
||||
return AF_INET;
|
||||
}
|
||||
|
||||
void wxIPV4address::Clear()
|
||||
{
|
||||
memset(m_addr, 0, sizeof(*m_addr));
|
||||
m_addr->sin_family = AF_INET;
|
||||
m_addr->sin_addr.s_addr = INADDR_ANY;
|
||||
}
|
||||
|
||||
/*
|
||||
const wxSockAddress& wxIPV4address::operator =(const wxSockAddress& addr)
|
||||
{
|
||||
wxIPV4address *ip_addr = (wxIPV4address *)&addr;
|
||||
CHECK_ADDRTYPE(addr, wxIPV4address);
|
||||
m_addr = ip_addr->m_addr;
|
||||
return *this;
|
||||
}
|
||||
*/
|
||||
|
||||
bool wxIPV4address::Hostname(const wxString& name)
|
||||
{
|
||||
struct hostent *hostent;
|
||||
struct in_addr *addr;
|
||||
|
||||
if (name.IsNull())
|
||||
return FALSE;
|
||||
|
||||
if (!name.IsNumber()) {
|
||||
if ((hostent = gethostbyname(name.GetData())) == 0) {
|
||||
return FALSE;
|
||||
}
|
||||
} else {
|
||||
long len_addr = inet_addr(name.GetData());
|
||||
if (len_addr == -1)
|
||||
return FALSE;
|
||||
m_addr->sin_addr.s_addr = len_addr;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
addr = (struct in_addr *) *(hostent->h_addr_list);
|
||||
|
||||
m_addr->sin_addr.s_addr = addr[0].s_addr;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxIPV4address::Hostname(unsigned long addr)
|
||||
{
|
||||
m_addr->sin_addr.s_addr = htonl(addr);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxIPV4address::Service(const wxString& name)
|
||||
{
|
||||
struct servent *servent;
|
||||
|
||||
if (name.IsNull())
|
||||
return FALSE;
|
||||
|
||||
if (!name.IsNumber()) {
|
||||
if ((servent = getservbyname(name, "tcp")) == 0)
|
||||
return FALSE;
|
||||
} else {
|
||||
if ((servent = getservbyport(atoi(name), "tcp")) == 0) {
|
||||
m_addr->sin_port = htons(atoi(name));
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
m_addr->sin_port = servent->s_port;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxIPV4address::Service(unsigned short port)
|
||||
{
|
||||
m_addr->sin_port = htons(port);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxIPV4address::LocalHost()
|
||||
{
|
||||
static char buf[256];
|
||||
|
||||
if (gethostname(buf, sizeof(buf)) < 0)
|
||||
return Hostname("localhost");
|
||||
else
|
||||
return Hostname(buf);
|
||||
}
|
||||
|
||||
wxString wxIPV4address::Hostname()
|
||||
{
|
||||
struct hostent *h_ent;
|
||||
|
||||
h_ent = gethostbyaddr((char *)&(m_addr->sin_addr), sizeof(m_addr->sin_addr),
|
||||
GetFamily());
|
||||
return wxString(h_ent->h_name);
|
||||
}
|
||||
|
||||
unsigned short wxIPV4address::Service()
|
||||
{
|
||||
return ntohs(m_addr->sin_port);
|
||||
}
|
||||
|
||||
void wxIPV4address::Build(struct sockaddr *&addr, size_t& len)
|
||||
{
|
||||
addr = (struct sockaddr *)m_addr;
|
||||
len = sizeof(*m_addr);
|
||||
}
|
||||
|
||||
void wxIPV4address::Disassemble(struct sockaddr *addr, size_t len)
|
||||
{
|
||||
if (len != sizeof(*m_addr))
|
||||
return;
|
||||
*m_addr = *(struct sockaddr_in *)addr;
|
||||
}
|
||||
|
||||
#ifdef IPV6_ENABLE
|
||||
|
||||
wxIPV6address::wxIPV6address()
|
||||
{
|
||||
m_addr = new sockaddr_in6;
|
||||
Clear();
|
||||
}
|
||||
|
||||
wxIPV6address::~wxIPV6address()
|
||||
{
|
||||
}
|
||||
|
||||
int wxIPV6address::SockAddrLen()
|
||||
{
|
||||
return sizeof(*m_addr);
|
||||
}
|
||||
|
||||
int wxIPV6address::GetFamily()
|
||||
{
|
||||
return AF_INET6;
|
||||
}
|
||||
|
||||
void wxIPV6address::Clear()
|
||||
{
|
||||
memset(m_addr, 0, sizeof(*m_addr));
|
||||
m_addr->sin6_family = AF_INET6;
|
||||
m_addr->sin6_addr.s_addr = INADDR_ANY;
|
||||
}
|
||||
|
||||
/*
|
||||
const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr)
|
||||
{
|
||||
wxIPV6address *ip_addr = (wxIPV6address *)&addr;
|
||||
|
||||
CHECK_ADDRTYPE(addr, wxIPV6address);
|
||||
m_addr = ip_addr->m_addr;
|
||||
return *this;
|
||||
}
|
||||
*/
|
||||
|
||||
bool wxIPV6address::Hostname(const wxString& name)
|
||||
{
|
||||
struct hostent *hostent;
|
||||
struct in_addr *addr;
|
||||
|
||||
if (name.IsNull())
|
||||
return FALSE;
|
||||
|
||||
if (!name.IsNumber()) {
|
||||
hostent = gethostbyname2((char*) name, AF_INET6);
|
||||
if (!hostent)
|
||||
return FALSE;
|
||||
} else {
|
||||
// Don't how to do
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
addr = (struct in6_addr *) *(hostent->h_addr_list);
|
||||
|
||||
m_addr->sin6_addr.s6_addr = addr[0].s6_addr;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxIPV6address::Hostname(unsigned char addr[16])
|
||||
{
|
||||
m_addr->sin6_addr.s6_addr = addr;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxIPV6address::Service(const char *name)
|
||||
{
|
||||
struct servent *servent;
|
||||
|
||||
if (!name || !strlen(name))
|
||||
return FALSE;
|
||||
|
||||
if (!isdigit(*name)) {
|
||||
if ((servent = getservbyname((char*) name, "tcp")) == 0)
|
||||
return FALSE;
|
||||
} else {
|
||||
if ((servent = getservbyport(atoi(name), "tcp")) == 0) {
|
||||
m_addr->sin_port = htons(atoi(name));
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
m_addr->sin_port = servent->s_port;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxIPV6address::Service(unsigned short port)
|
||||
{
|
||||
m_addr->sin_port = htons(port);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxIPV6address::LocalHost()
|
||||
{
|
||||
static char buf[256];
|
||||
|
||||
if (gethostname(buf, sizeof(buf)) < 0)
|
||||
return Hostname("localhost");
|
||||
else
|
||||
return Hostname(buf);
|
||||
}
|
||||
|
||||
const wxString& wxIPV6address::Hostname()
|
||||
{
|
||||
struct hostent *h_ent;
|
||||
|
||||
h_ent = gethostbyaddr((char *)&(m_addr->sin_addr), sizeof(m_addr->sin_addr),
|
||||
GetFamily());
|
||||
return wxString(h_ent->h_name);
|
||||
}
|
||||
|
||||
unsigned short wxIPV6address::Service()
|
||||
{
|
||||
return ntohs(m_addr->sin_port);
|
||||
}
|
||||
|
||||
void wxIPV6address::Build(struct sockaddr& *addr, size_t& len)
|
||||
{
|
||||
len = sizeof(*m_addr);
|
||||
addr = m_addr;
|
||||
}
|
||||
|
||||
void wxIPV6address::Disassemble(struct sockaddr& *addr, size_t len)
|
||||
{
|
||||
if (len != sizeof(*m_addr))
|
||||
return;
|
||||
*m_addr = *(struct sockaddr_in6 *)addr;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __UNIX__
|
||||
#include <sys/un.h>
|
||||
|
||||
wxUNIXaddress::wxUNIXaddress()
|
||||
{
|
||||
m_addr = new sockaddr_un;
|
||||
Clear();
|
||||
}
|
||||
|
||||
wxUNIXaddress::~wxUNIXaddress()
|
||||
{
|
||||
}
|
||||
|
||||
int wxUNIXaddress::SockAddrLen()
|
||||
{
|
||||
return sizeof(*m_addr);
|
||||
}
|
||||
|
||||
int wxUNIXaddress::GetFamily()
|
||||
{
|
||||
return AF_UNIX;
|
||||
}
|
||||
|
||||
void wxUNIXaddress::Clear()
|
||||
{
|
||||
memset(m_addr, 0, sizeof(m_addr));
|
||||
m_addr->sun_family = AF_UNIX;
|
||||
}
|
||||
|
||||
/*
|
||||
const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr)
|
||||
{
|
||||
wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr;
|
||||
CHECK_ADDRTYPE(addr, wxUNIXaddress);
|
||||
m_addr = unx_addr->m_addr;
|
||||
return *this;
|
||||
}
|
||||
*/
|
||||
|
||||
void wxUNIXaddress::Filename(const wxString& fname)
|
||||
{
|
||||
sprintf(m_addr->sun_path, "%s", WXSTRINGCAST fname);
|
||||
}
|
||||
|
||||
wxString wxUNIXaddress::Filename()
|
||||
{
|
||||
return wxString(m_addr->sun_path);
|
||||
}
|
||||
|
||||
void wxUNIXaddress::Build(struct sockaddr*& addr, size_t& len)
|
||||
{
|
||||
addr = (struct sockaddr *)m_addr;
|
||||
len = sizeof(*m_addr);
|
||||
}
|
||||
|
||||
void wxUNIXaddress::Disassemble(struct sockaddr *addr, size_t len)
|
||||
{
|
||||
if (len != sizeof(*m_addr))
|
||||
return;
|
||||
*m_addr = *(struct sockaddr_un *)addr;
|
||||
}
|
||||
#endif
|
40
src/common/sckfile.cpp
Normal file
40
src/common/sckfile.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Name: sckfile.cpp
|
||||
// Purpose: File protocol
|
||||
// Author: Guilhem Lavaux
|
||||
// Modified by:
|
||||
// Created: 20/07/97
|
||||
// RCS-ID: $Id$
|
||||
// Copyright: (c) 1997, 1998 Guilhem Lavaux
|
||||
// Licence: wxWindows license
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation "sckfile.h"
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <wx/fstream.h>
|
||||
#include "wx/protocol/file.h"
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
#pragma hdrstop
|
||||
#endif
|
||||
|
||||
#if !USE_SHARED_LIBRARY
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxFileProto, wxProtocol)
|
||||
IMPLEMENT_PROTOCOL(wxFileProto, "file", NULL, FALSE)
|
||||
#endif
|
||||
|
||||
wxFileProto::wxFileProto()
|
||||
: wxProtocol()
|
||||
{
|
||||
}
|
||||
|
||||
wxFileProto::~wxFileProto()
|
||||
{
|
||||
}
|
||||
|
||||
wxInputStream *wxFileProto::GetInputStream(const wxString& path)
|
||||
{
|
||||
return new wxFileInputStream(path);
|
||||
}
|
503
src/common/sckipc.cpp
Normal file
503
src/common/sckipc.cpp
Normal file
@@ -0,0 +1,503 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Name: sckipc.cpp
|
||||
// Purpose: Interprocess communication implementation (wxSocket version)
|
||||
// Author: Julian Smart, Guilhem Lavaux
|
||||
// Modified by: Guilhem Lavaux (big rewrite) May 1997, 1998
|
||||
// Created: 1993
|
||||
// RCS-ID: $Id$
|
||||
// Copyright: (c) Julian Smart 1993, Guilhem Lavaux 1997, 1998
|
||||
// Licence: wxWindows license
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation "sckipc.h"
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef WXPREC
|
||||
#include <wx/wxprec.h>
|
||||
#else
|
||||
#include <wx/wx.h>
|
||||
#endif
|
||||
|
||||
#include "wx/socket.h"
|
||||
#include "wx/sckipc.h"
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
#pragma hdrstop
|
||||
#endif
|
||||
|
||||
#if !USE_SHARED_LIBRARY
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxTCPServer, wxServerBase)
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxTCPClient, wxClientBase)
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxTCPConnection, wxConnectionBase)
|
||||
#endif
|
||||
|
||||
// It seems to be already defined somewhere in the Xt includes.
|
||||
#ifndef __XT__
|
||||
// Message codes
|
||||
enum {
|
||||
IPC_EXECUTE = 1,
|
||||
IPC_REQUEST,
|
||||
IPC_POKE,
|
||||
IPC_ADVISE_START,
|
||||
IPC_ADVISE_REQUEST,
|
||||
IPC_ADVISE,
|
||||
IPC_ADVISE_STOP,
|
||||
IPC_REQUEST_REPLY,
|
||||
IPC_FAIL,
|
||||
IPC_CONNECT,
|
||||
IPC_DISCONNECT
|
||||
};
|
||||
#endif
|
||||
|
||||
void Server_OnRequest(wxSocketServer& server,
|
||||
wxSocketBase::wxRequestEvent evt,
|
||||
char *cdata);
|
||||
void Client_OnRequest(wxSocketBase& sock,
|
||||
wxSocketBase::wxRequestEvent evt,
|
||||
char *cdata);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// wxTCPClient
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
wxTCPClient::wxTCPClient (void)
|
||||
: wxClientBase()
|
||||
{
|
||||
}
|
||||
|
||||
wxTCPClient::~wxTCPClient (void)
|
||||
{
|
||||
}
|
||||
|
||||
bool wxTCPClient::ValidHost(const wxString& host)
|
||||
{
|
||||
wxIPV4address addr;
|
||||
|
||||
return addr.Hostname(host);
|
||||
}
|
||||
|
||||
wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host,
|
||||
const wxString& server_name,
|
||||
const wxString& topic)
|
||||
{
|
||||
wxIPV4address addr;
|
||||
wxSocketHandler *hsock = &wxSocketHandler::Master();
|
||||
wxSocketClient *client = hsock->CreateClient();
|
||||
wxSocketStream *stream = new wxSocketStream(*client);
|
||||
wxDataStream data_s(*stream);
|
||||
|
||||
client->SetNotify(wxSocketBase::REQ_READ | wxSocketBase::REQ_LOST);
|
||||
addr.Service(server_name);
|
||||
addr.Hostname(host);
|
||||
|
||||
if (!client->Connect(addr)) {
|
||||
delete client;
|
||||
return NULL;
|
||||
}
|
||||
client->Notify(FALSE);
|
||||
|
||||
// Send topic name, and enquire whether this has succeeded
|
||||
unsigned char msg;
|
||||
|
||||
data_s.Write8(IPC_CONNECT);
|
||||
data_s.WriteString(topic);
|
||||
|
||||
msg = data_s.Read8();
|
||||
|
||||
// OK! Confirmation.
|
||||
if (msg == IPC_CONNECT) {
|
||||
wxTCPConnection *connection = (wxTCPConnection *)OnMakeConnection ();
|
||||
if (connection) {
|
||||
if (!connection->IsKindOf(CLASSINFO(wxTCPConnection))) {
|
||||
delete connection;
|
||||
return NULL;
|
||||
}
|
||||
connection->m_topic = topic;
|
||||
client->Callback(Client_OnRequest);
|
||||
client->CallbackData((char *)connection);
|
||||
client->Notify(TRUE);
|
||||
return connection;
|
||||
} else {
|
||||
delete client;
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
delete client;
|
||||
return NULL;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wxConnectionBase *wxTCPClient::OnMakeConnection()
|
||||
{
|
||||
return new wxTCPConnection;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// wxTCPServer
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
wxTCPServer::wxTCPServer (void)
|
||||
: wxServerBase()
|
||||
{
|
||||
}
|
||||
|
||||
bool wxTCPServer::Create(const wxString& server_name)
|
||||
{
|
||||
wxIPV4address addr;
|
||||
wxSocketHandler *hsock = &wxSocketHandler::Master();
|
||||
wxSocketServer *server;
|
||||
|
||||
addr.Service(server_name);
|
||||
|
||||
// Create a socket listening on specified port
|
||||
server = hsock->CreateServer(addr);
|
||||
server->Callback((wxSocketBase::wxSockCbk)Server_OnRequest);
|
||||
server->SetNotify(wxSocketBase::REQ_ACCEPT);
|
||||
|
||||
server->CallbackData((char *)this);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
wxTCPServer::~wxTCPServer (void)
|
||||
{
|
||||
}
|
||||
|
||||
wxConnectionBase *wxTCPServer::OnAcceptConnection(const wxString& topic)
|
||||
{
|
||||
return new wxTCPConnection();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// wxTCPConnection
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
wxTCPConnection::wxTCPConnection (void)
|
||||
: wxConnectionBase(),
|
||||
m_sock(NULL), m_sockstrm(NULL), m_codec(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
wxTCPConnection::~wxTCPConnection (void)
|
||||
{
|
||||
wxDELETE(m_sock);
|
||||
wxDELETE(m_codec);
|
||||
wxDELETE(m_sockstrm);
|
||||
}
|
||||
|
||||
void wxTCPConnection::Compress(bool on)
|
||||
{
|
||||
// Use wxLZWStream
|
||||
}
|
||||
|
||||
// Calls that CLIENT can make.
|
||||
bool wxTCPConnection::Disconnect (void)
|
||||
{
|
||||
// Send the the disconnect message to the peer.
|
||||
m_codec->Write8(IPC_DISCONNECT);
|
||||
m_sock->Close();
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxTCPConnection::Execute (char *data, int size, wxDataFormat format)
|
||||
{
|
||||
if (!m_sock->IsConnected())
|
||||
return FALSE;
|
||||
|
||||
// Prepare EXECUTE message
|
||||
m_codec->Write8(IPC_EXECUTE);
|
||||
m_codec->Write8(format);
|
||||
if (size < 0)
|
||||
m_codec->WriteString(data);
|
||||
else {
|
||||
m_codec->Write32(size);
|
||||
m_codec->Write(data, size);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
char *wxTCPConnection::Request (const wxString& item, int *size, wxDataFormat format)
|
||||
{
|
||||
if (!m_sock->IsConnected())
|
||||
return NULL;
|
||||
|
||||
m_codec->Write8(IPC_REQUEST);
|
||||
m_codec->WriteString(item);
|
||||
m_codec->Write8(format);
|
||||
|
||||
// If Unpack doesn't initialize it.
|
||||
int ret;
|
||||
|
||||
ret = m_codec->Read8();
|
||||
if (ret == IPC_FAIL)
|
||||
return NULL;
|
||||
else {
|
||||
size_t s;
|
||||
char *data = NULL;
|
||||
|
||||
s = m_codec->Read32();
|
||||
data = new char[s];
|
||||
m_codec->Read(data, s);
|
||||
|
||||
if (size)
|
||||
*size = s;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
bool wxTCPConnection::Poke (const wxString& item, char *data, int size, wxDataFormat format)
|
||||
{
|
||||
if (!m_sock->IsConnected())
|
||||
return FALSE;
|
||||
|
||||
m_codec->Write8(IPC_POKE);
|
||||
m_codec->WriteString(item);
|
||||
m_codec->Write8(format);
|
||||
if (size < 0)
|
||||
m_codec->WriteString(data);
|
||||
else {
|
||||
m_codec->Write32(size);
|
||||
m_codec->Write(data, size);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxTCPConnection::StartAdvise (const wxString& item)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (!m_sock->IsConnected())
|
||||
return FALSE;
|
||||
|
||||
m_codec->Write8(IPC_ADVISE_START);
|
||||
m_codec->WriteString(item);
|
||||
|
||||
ret = m_codec->Read8();
|
||||
|
||||
if (ret != IPC_FAIL)
|
||||
return TRUE;
|
||||
else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
bool wxTCPConnection::StopAdvise (const wxString& item)
|
||||
{
|
||||
int msg;
|
||||
|
||||
if (!m_sock->IsConnected())
|
||||
return FALSE;
|
||||
|
||||
m_codec->Write8(IPC_ADVISE_STOP);
|
||||
m_codec->WriteString(item);
|
||||
|
||||
msg = m_codec->Read8();
|
||||
|
||||
if (msg != IPC_FAIL)
|
||||
return TRUE;
|
||||
else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Calls that SERVER can make
|
||||
bool wxTCPConnection::Advise (const wxString& item,
|
||||
char *data, int size, wxDataFormat format)
|
||||
{
|
||||
if (!m_sock->IsConnected())
|
||||
return FALSE;
|
||||
|
||||
m_codec->Write8(IPC_ADVISE);
|
||||
m_codec->WriteString(item);
|
||||
m_codec->Write8(format);
|
||||
if (size < 0)
|
||||
m_codec->WriteString(data);
|
||||
else {
|
||||
m_codec->Write32(size);
|
||||
m_codec->Write(data, size);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void Client_OnRequest(wxSocketBase& sock, wxSocketBase::wxRequestEvent evt,
|
||||
char *cdata)
|
||||
{
|
||||
int msg = 0;
|
||||
wxTCPConnection *connection = (wxTCPConnection *)cdata;
|
||||
wxDataStream *codec;
|
||||
wxString topic_name = connection->m_topic;
|
||||
wxString item;
|
||||
|
||||
// The socket handler signals us that we lost the connection: destroy all.
|
||||
if (evt == wxSocketBase::EVT_LOST) {
|
||||
sock.Close();
|
||||
connection->OnDisconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
// Receive message number.
|
||||
codec = connection->m_codec;
|
||||
msg = codec->Read8();
|
||||
|
||||
switch (msg) {
|
||||
case IPC_EXECUTE: {
|
||||
char *data;
|
||||
size_t size;
|
||||
wxDataFormat format;
|
||||
|
||||
format = (wxDataFormat)codec->Read8();
|
||||
size = codec->Read32();
|
||||
data = new char[size];
|
||||
codec->Read(data, size);
|
||||
|
||||
connection->OnExecute (topic_name, data, size, format);
|
||||
|
||||
delete [] data;
|
||||
break;
|
||||
}
|
||||
case IPC_ADVISE: {
|
||||
char *data;
|
||||
size_t size;
|
||||
wxDataFormat format;
|
||||
|
||||
item = codec->ReadString();
|
||||
format = (wxDataFormat)codec->Read8();
|
||||
size = codec->Read32();
|
||||
data = new char[size];
|
||||
codec->Read(data, size);
|
||||
|
||||
connection->OnAdvise (topic_name, item, data, size, format);
|
||||
|
||||
delete [] data;
|
||||
break;
|
||||
}
|
||||
case IPC_ADVISE_START: {
|
||||
item = codec->ReadString();
|
||||
|
||||
bool ok = connection->OnStartAdvise (topic_name, item);
|
||||
if (ok)
|
||||
codec->Write8(IPC_ADVISE_START);
|
||||
else
|
||||
codec->Write8(IPC_FAIL);
|
||||
|
||||
break;
|
||||
}
|
||||
case IPC_ADVISE_STOP: {
|
||||
item = codec->ReadString();
|
||||
|
||||
bool ok = connection->OnStopAdvise (topic_name, item);
|
||||
if (ok)
|
||||
codec->Write8(IPC_ADVISE_STOP);
|
||||
else
|
||||
codec->Write8(IPC_FAIL);
|
||||
|
||||
break;
|
||||
}
|
||||
case IPC_POKE: {
|
||||
wxDataFormat format;
|
||||
size_t size;
|
||||
char *data;
|
||||
|
||||
item = codec->ReadString();
|
||||
format = (wxDataFormat)codec->Read8();
|
||||
size = codec->Read32();
|
||||
data = new char[size];
|
||||
codec->Read(data, size);
|
||||
|
||||
connection->OnPoke (topic_name, item, data, size, format);
|
||||
|
||||
delete [] data;
|
||||
|
||||
break;
|
||||
}
|
||||
case IPC_REQUEST: {
|
||||
wxDataFormat format;
|
||||
|
||||
item = codec->ReadString();
|
||||
format = (wxDataFormat)codec->Read8();
|
||||
|
||||
int user_size = -1;
|
||||
char *user_data = connection->OnRequest (topic_name, item, &user_size, format);
|
||||
|
||||
if (user_data) {
|
||||
codec->Write8(IPC_REQUEST_REPLY);
|
||||
if (user_size != -1) {
|
||||
codec->Write32(user_size);
|
||||
codec->Write(user_data, user_size);
|
||||
} else
|
||||
codec->WriteString(user_data);
|
||||
} else
|
||||
codec->Write8(IPC_FAIL);
|
||||
|
||||
break;
|
||||
}
|
||||
case IPC_DISCONNECT: {
|
||||
sock.Close();
|
||||
connection->OnDisconnect();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
codec->Write8(IPC_FAIL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Server_OnRequest(wxSocketServer& server,
|
||||
wxSocketBase::wxRequestEvent evt, char *cdata)
|
||||
{
|
||||
wxTCPServer *ipcserv = (wxTCPServer *)cdata;
|
||||
wxSocketStream *stream;
|
||||
wxDataStream *codec;
|
||||
|
||||
if (evt != wxSocketBase::EVT_ACCEPT)
|
||||
return;
|
||||
|
||||
/* Accept the connection, getting a new socket */
|
||||
wxSocketBase *sock = server.Accept();
|
||||
sock->Notify(FALSE);
|
||||
sock->SetNotify(wxSocketBase::REQ_READ | wxSocketBase::REQ_LOST);
|
||||
|
||||
stream = new wxSocketStream(*sock);
|
||||
codec = new wxDataStream(*stream);
|
||||
|
||||
if (!sock->Ok())
|
||||
return;
|
||||
|
||||
int msg;
|
||||
msg = codec->Read8();
|
||||
|
||||
if (msg == IPC_CONNECT) {
|
||||
wxString topic_name;
|
||||
topic_name = codec->ReadString();
|
||||
|
||||
/* Register new socket with the notifier */
|
||||
wxTCPConnection *new_connection =
|
||||
(wxTCPConnection *)ipcserv->OnAcceptConnection (topic_name);
|
||||
if (new_connection) {
|
||||
if (!new_connection->IsKindOf(CLASSINFO(wxTCPConnection))) {
|
||||
delete new_connection;
|
||||
codec->Write8(IPC_FAIL);
|
||||
return;
|
||||
}
|
||||
// Acknowledge success
|
||||
codec->Write8(IPC_CONNECT);
|
||||
|
||||
new_connection->m_topic = topic_name;
|
||||
new_connection->m_sockstrm = stream;
|
||||
new_connection->m_codec = codec;
|
||||
sock->Callback(Client_OnRequest);
|
||||
sock->CallbackData((char *)new_connection);
|
||||
sock->Notify(TRUE);
|
||||
} else {
|
||||
// Send failure message
|
||||
codec->Write8(IPC_FAIL);
|
||||
}
|
||||
}
|
||||
}
|
68
src/common/sckstrm.cpp
Normal file
68
src/common/sckstrm.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Name: sckstrm.h
|
||||
// Purpose: wxSocket*Stream
|
||||
// Author: Guilhem Lavaux
|
||||
// Modified by:
|
||||
// Created: 17/07/97
|
||||
// RCS-ID: $Id$
|
||||
// Copyright: (c)
|
||||
// Licence: wxWindows licence
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation "sckstrm.h"
|
||||
#endif
|
||||
|
||||
#include "wx/stream.h"
|
||||
#include "wx/socket.h"
|
||||
#include "wx/sckstrm.h"
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// wxSocketOutputStream
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
wxSocketOutputStream::wxSocketOutputStream(wxSocketBase& s)
|
||||
: m_o_socket(&s)
|
||||
{
|
||||
}
|
||||
|
||||
wxSocketOutputStream::~wxSocketOutputStream()
|
||||
{
|
||||
}
|
||||
|
||||
wxOutputStream& wxSocketOutputStream::Write(const void *buffer, size_t size)
|
||||
{
|
||||
m_o_socket->Write((const char *)buffer, size);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// wxSocketInputStream
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
wxSocketInputStream::wxSocketInputStream(wxSocketBase& s)
|
||||
: m_i_socket(&s)
|
||||
{
|
||||
}
|
||||
|
||||
wxSocketInputStream::~wxSocketInputStream()
|
||||
{
|
||||
}
|
||||
|
||||
wxInputStream& wxSocketInputStream::Read(void *buffer, size_t size)
|
||||
{
|
||||
m_i_socket->Read((char *)buffer, size);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// wxSocketStream (IO)
|
||||
// ---------------------------------------------------------------------------
|
||||
wxSocketStream::wxSocketStream(wxSocketBase& i_s, wxSocketBase& o_s)
|
||||
: wxSocketInputStream(i_s), wxSocketOutputStream(o_s)
|
||||
{
|
||||
}
|
||||
|
||||
wxSocketStream::wxSocketStream(wxSocketBase& s)
|
||||
: wxSocketInputStream(s), wxSocketOutputStream(s)
|
||||
{
|
||||
}
|
1532
src/common/socket.cpp
Normal file
1532
src/common/socket.cpp
Normal file
File diff suppressed because it is too large
Load Diff
@@ -549,9 +549,23 @@ wxOutputStream& wxOutputStream::operator<<(wxObject& obj)
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxStream
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
wxStream::wxStream()
|
||||
: wxInputStream(), wxOutputStream()
|
||||
{
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxFilterInputStream
|
||||
// ----------------------------------------------------------------------------
|
||||
wxFilterInputStream::wxFilterInputStream()
|
||||
: wxInputStream(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
wxFilterInputStream::wxFilterInputStream(wxInputStream& stream)
|
||||
: wxInputStream(NULL)
|
||||
{
|
||||
@@ -578,10 +592,14 @@ off_t wxFilterInputStream::DoTellInput() const
|
||||
return m_parent_i_stream->TellI();
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxFilterOutputStream
|
||||
// ----------------------------------------------------------------------------
|
||||
wxFilterOutputStream::wxFilterOutputStream()
|
||||
: wxOutputStream(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
wxFilterOutputStream::wxFilterOutputStream(wxOutputStream& stream)
|
||||
: wxOutputStream(NULL)
|
||||
{
|
||||
@@ -608,6 +626,19 @@ off_t wxFilterOutputStream::DoTellOutput() const
|
||||
return m_parent_o_stream->TellO();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxFilterStream
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
wxFilterStream::wxFilterStream()
|
||||
{
|
||||
}
|
||||
|
||||
wxFilterStream::wxFilterStream(wxStream& stream)
|
||||
: wxFilterInputStream(stream), wxFilterOutputStream(stream)
|
||||
{
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Some IOManip function
|
||||
// ----------------------------------------------------------------------------
|
||||
|
104
src/common/tokenzr.cpp
Normal file
104
src/common/tokenzr.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Name: tokenzr.cpp
|
||||
// Purpose: String tokenizer
|
||||
// Author: Guilhem Lavaux
|
||||
// Modified by:
|
||||
// Created: 04/22/98
|
||||
// RCS-ID: $Id$
|
||||
// Copyright: (c) Guilhem Lavaux
|
||||
// Licence: wxWindows licence
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation "tokenzr.h"
|
||||
#endif
|
||||
|
||||
#include "wx/object.h"
|
||||
#include "wx/string.h"
|
||||
#include "wx/tokenzr.h"
|
||||
|
||||
wxStringTokenizer::wxStringTokenizer(const wxString& to_tokenize,
|
||||
const wxString& delims,
|
||||
bool ret_delims)
|
||||
: wxObject()
|
||||
{
|
||||
m_string = to_tokenize;
|
||||
m_delims = delims;
|
||||
m_retdelims = ret_delims;
|
||||
}
|
||||
|
||||
wxStringTokenizer::~wxStringTokenizer()
|
||||
{
|
||||
}
|
||||
|
||||
off_t wxStringTokenizer::FindDelims(const wxString& str, const wxString& delims)
|
||||
{
|
||||
int i, j;
|
||||
register int s_len = str.Length(),
|
||||
len = delims.Length();
|
||||
|
||||
for (i=0;i<s_len;i++) {
|
||||
register char c = str[i];
|
||||
|
||||
for (j=0;j<len;j++)
|
||||
if (delims[j] == c)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int wxStringTokenizer::CountTokens()
|
||||
{
|
||||
wxString p_string = m_string;
|
||||
bool found = TRUE;
|
||||
int pos, count = 1;
|
||||
|
||||
if (p_string.Length() == 0)
|
||||
return 0;
|
||||
|
||||
while (found) {
|
||||
pos = FindDelims(p_string, m_delims);
|
||||
if (pos != -1) {
|
||||
count++;
|
||||
p_string = p_string(0, pos);
|
||||
} else
|
||||
found = FALSE;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
bool wxStringTokenizer::HasMoreToken()
|
||||
{
|
||||
return (m_string.Length() != 0);
|
||||
}
|
||||
|
||||
wxString wxStringTokenizer::NextToken()
|
||||
{
|
||||
register off_t pos, pos2;
|
||||
wxString r_string;
|
||||
|
||||
if (m_string.IsNull())
|
||||
return m_string;
|
||||
|
||||
pos = FindDelims(m_string, m_delims);
|
||||
if (pos == -1) {
|
||||
r_string = m_string;
|
||||
m_string = (char *)NULL;
|
||||
|
||||
return r_string;
|
||||
}
|
||||
|
||||
if (m_retdelims) {
|
||||
if (!pos) {
|
||||
pos++;
|
||||
pos2 = 1;
|
||||
} else
|
||||
pos2 = pos;
|
||||
} else
|
||||
pos2 = pos + 1;
|
||||
|
||||
r_string = m_string.Left((size_t)pos);
|
||||
m_string = m_string.Mid((size_t)pos2);
|
||||
|
||||
return r_string;
|
||||
}
|
277
src/common/url.cpp
Normal file
277
src/common/url.cpp
Normal file
@@ -0,0 +1,277 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Name: url.cpp
|
||||
// Purpose: URL parser
|
||||
// Author: Guilhem Lavaux
|
||||
// Modified by:
|
||||
// Created: 20/07/1997
|
||||
// RCS-ID: $Id$
|
||||
// Copyright: (c) 1997, 1998 Guilhem Lavaux
|
||||
// Licence: wxWindows license
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation "url.h"
|
||||
#endif
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
// wxWindows headers
|
||||
#include <wx/string.h>
|
||||
#include <wx/list.h>
|
||||
#include <wx/utils.h>
|
||||
|
||||
// wxSocket header
|
||||
#include "wx/url.h"
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
#pragma hdrstop
|
||||
#endif
|
||||
|
||||
#if !USE_SHARED_LIBRARY
|
||||
IMPLEMENT_CLASS(wxProtoInfo, wxObject)
|
||||
IMPLEMENT_CLASS(wxURL, wxObject)
|
||||
#endif
|
||||
|
||||
// Protocols list
|
||||
wxProtoInfo *wxURL::g_protocols = NULL;
|
||||
wxHTTP wxURL::g_proxy;
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
// wxURL ////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
* --------------------------------------------------------------
|
||||
* --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
|
||||
* --------------------------------------------------------------
|
||||
*/
|
||||
|
||||
wxURL::wxURL(const wxString& url)
|
||||
{
|
||||
m_protocol = NULL;
|
||||
if (g_proxy.IsConnected()) {
|
||||
m_protocol = &g_proxy;
|
||||
m_protoname = "proxy";
|
||||
m_path = url;
|
||||
return;
|
||||
}
|
||||
m_url = url;
|
||||
m_error = wxURL_NOERR;
|
||||
}
|
||||
|
||||
bool wxURL::ParseURL()
|
||||
{
|
||||
wxString last_url = m_url;
|
||||
|
||||
// Clean up
|
||||
CleanData();
|
||||
|
||||
// Extract protocol name
|
||||
if (!PrepProto(last_url)) {
|
||||
m_error = wxURL_SNTXERR;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Find and create the protocol object
|
||||
if (!FetchProtocol()) {
|
||||
m_error = wxURL_NOPROTO;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Do we need a host name ?
|
||||
if (m_protoinfo->m_needhost) {
|
||||
// Extract it
|
||||
if (!PrepHost(last_url)) {
|
||||
m_error = wxURL_SNTXERR;
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
// Extract full path
|
||||
if (!PrepPath(last_url)) {
|
||||
m_error = wxURL_NOPATH;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
m_error = wxURL_NOERR;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void wxURL::CleanData()
|
||||
{
|
||||
if (m_protoname != "proxy")
|
||||
delete m_protocol;
|
||||
}
|
||||
|
||||
wxURL::~wxURL()
|
||||
{
|
||||
CleanData();
|
||||
}
|
||||
|
||||
/*
|
||||
* --------------------------------------------------------------
|
||||
* --------- wxURL urls decoders --------------------------------
|
||||
* --------------------------------------------------------------
|
||||
*/
|
||||
bool wxURL::PrepProto(wxString& url)
|
||||
{
|
||||
int pos;
|
||||
|
||||
// Find end
|
||||
pos = url.Find(':');
|
||||
if (pos == -1)
|
||||
return FALSE;
|
||||
|
||||
m_protoname = url(0, pos);
|
||||
|
||||
url = url(pos+1, url.Length());
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxURL::PrepHost(wxString& url)
|
||||
{
|
||||
int pos, pos2;
|
||||
|
||||
if ((url[0UL] != '/') || (url[1UL] != '/'))
|
||||
return FALSE;
|
||||
|
||||
url = url(2, url.Length());
|
||||
|
||||
pos = url.Find('/');
|
||||
if (pos == -1)
|
||||
return FALSE;
|
||||
|
||||
pos2 = url.Find(':');
|
||||
if (pos2 != -1 && pos2 < pos) {
|
||||
m_servname = url(pos2, pos);
|
||||
if (!m_servname.IsNumber())
|
||||
return FALSE;
|
||||
pos2 = pos;
|
||||
}
|
||||
|
||||
m_hostname = url(0, pos);
|
||||
|
||||
url = url(url.Find('/'), url.Length());
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxURL::PrepPath(wxString& url)
|
||||
{
|
||||
if (url.Length() != 0)
|
||||
m_path = url;
|
||||
else
|
||||
m_path = "/";
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxURL::FetchProtocol()
|
||||
{
|
||||
wxProtoInfo *info = g_protocols;
|
||||
|
||||
while (info) {
|
||||
if (m_protoname == info->m_protoname) {
|
||||
if (m_servname.IsNull())
|
||||
m_servname = info->m_servname;
|
||||
|
||||
m_protoinfo = info;
|
||||
m_protocol = (wxProtocol *)m_protoinfo->m_cinfo->CreateObject();
|
||||
wxSocketHandler::Master().Register(m_protocol);
|
||||
return TRUE;
|
||||
}
|
||||
info = info->next;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/*
|
||||
* --------------------------------------------------------------
|
||||
* --------- wxURL get ------------------------------------------
|
||||
* --------------------------------------------------------------
|
||||
*/
|
||||
wxInputStream *wxURL::GetInputStream(void)
|
||||
{
|
||||
wxIPV4address addr;
|
||||
wxInputStream *the_i_stream = NULL;
|
||||
|
||||
if (!m_protocol)
|
||||
if (!ParseURL())
|
||||
return NULL;
|
||||
|
||||
if (!m_protocol) {
|
||||
m_error = wxURL_NOPROTO;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
m_error = wxURL_NOERR;
|
||||
if (m_protoinfo->m_needhost) {
|
||||
if (!addr.Hostname(m_hostname)) {
|
||||
m_error = wxURL_NOHOST;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
addr.Service(m_servname);
|
||||
|
||||
if (!m_protocol->Connect(addr)) {
|
||||
m_error = wxURL_CONNERR;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
the_i_stream = m_protocol->GetInputStream(m_path);
|
||||
if (!the_i_stream) {
|
||||
m_error = wxURL_PROTOERR;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return the_i_stream;
|
||||
}
|
||||
|
||||
void wxURL::SetDefaultProxy(const wxString& url_proxy)
|
||||
{
|
||||
g_proxy.Close();
|
||||
|
||||
if (url_proxy.IsNull())
|
||||
return;
|
||||
|
||||
wxString tmp_str = url_proxy;
|
||||
int pos = tmp_str.Find(':');
|
||||
wxString hostname = tmp_str(0, pos),
|
||||
port = tmp_str(pos, tmp_str.Length()-pos);
|
||||
wxIPV4address addr;
|
||||
|
||||
addr.Hostname(hostname);
|
||||
addr.Service(port);
|
||||
|
||||
g_proxy.Connect(addr);
|
||||
}
|
||||
|
||||
void wxURL::SetProxy(const wxString& url_proxy)
|
||||
{
|
||||
if (url_proxy.IsNull()) {
|
||||
m_proxy.Close();
|
||||
return;
|
||||
}
|
||||
|
||||
CleanData();
|
||||
|
||||
wxString tmp_str;
|
||||
wxString hostname, port;
|
||||
int pos;
|
||||
wxIPV4address addr;
|
||||
|
||||
tmp_str = url_proxy;
|
||||
pos = tmp_str.Find(':');
|
||||
hostname = tmp_str(0, pos);
|
||||
port = tmp_str(pos, tmp_str.Length()-pos);
|
||||
|
||||
addr.Hostname(hostname);
|
||||
addr.Service(port);
|
||||
|
||||
m_proxy.Connect(addr);
|
||||
|
||||
m_protocol = &m_proxy;
|
||||
m_protoname = "proxy";
|
||||
m_path = url_proxy;
|
||||
}
|
10
src/gtk.inc
10
src/gtk.inc
@@ -51,9 +51,19 @@ LIB_CPP_SRC=\
|
||||
common/mstream.cpp \
|
||||
common/zstream.cpp \
|
||||
common/objstrm.cpp \
|
||||
common/sckstrm.cpp \
|
||||
common/validate.cpp \
|
||||
common/valtext.cpp \
|
||||
common/wxexpr.cpp \
|
||||
common/tokenzr.cpp \
|
||||
common/socket.cpp \
|
||||
common/sckaddr.cpp \
|
||||
common/protocol.cpp \
|
||||
common/url.cpp \
|
||||
common/http.cpp \
|
||||
common/ftp.cpp \
|
||||
common/sckfile.cpp \
|
||||
common/sckipc.cpp \
|
||||
\
|
||||
gtk/accel.cpp \
|
||||
gtk/app.cpp \
|
||||
|
@@ -400,6 +400,50 @@ IMPLEMENT_DYNAMIC_CLASS(wxToolBar95, wxToolBarBase)
|
||||
|
||||
#endif
|
||||
|
||||
#include "wx/sckaddr.h"
|
||||
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxSockAddress)
|
||||
#ifdef ENABLE_IPV6
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxSockAddress)
|
||||
#endif
|
||||
#ifndef __UNIX__
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
|
||||
#endif
|
||||
|
||||
#include "wx/socket.h"
|
||||
|
||||
IMPLEMENT_CLASS(wxSocketBase, wxEvtHandler)
|
||||
IMPLEMENT_CLASS(wxSocketClient, wxSocketBase)
|
||||
IMPLEMENT_CLASS(wxSocketServer, wxSocketBase)
|
||||
IMPLEMENT_CLASS(wxSocketHandler, wxObject)
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent, wxEvent)
|
||||
|
||||
#include "wx/url.h"
|
||||
|
||||
IMPLEMENT_CLASS(wxProtoInfo, wxObject)
|
||||
IMPLEMENT_CLASS(wxURL, wxObject)
|
||||
|
||||
#include "wx/protocol/http.h"
|
||||
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol)
|
||||
IMPLEMENT_PROTOCOL(wxHTTP, "http", "80", TRUE)
|
||||
|
||||
#include "wx/protocol/ftp.h"
|
||||
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxFTP, wxProtocol)
|
||||
IMPLEMENT_PROTOCOL(wxFTP, "ftp", "ftp", TRUE)
|
||||
|
||||
#include "wx/protocol/sckfile.h"
|
||||
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxFileProto, wxProtocol)
|
||||
IMPLEMENT_PROTOCOL(wxFileProto, "file", NULL, FALSE)
|
||||
|
||||
#include "wx/sckipc.h"
|
||||
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxTCPServer, wxServerBase)
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxTCPClient, wxClientBase)
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxTCPConnection, wxConnectionBase)
|
||||
|
||||
#include "wx/statusbr.h"
|
||||
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxStatusBar, wxWindow)
|
||||
|
@@ -400,6 +400,50 @@ IMPLEMENT_DYNAMIC_CLASS(wxToolBar95, wxToolBarBase)
|
||||
|
||||
#endif
|
||||
|
||||
#include "wx/sckaddr.h"
|
||||
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxSockAddress)
|
||||
#ifdef ENABLE_IPV6
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxSockAddress)
|
||||
#endif
|
||||
#ifndef __UNIX__
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
|
||||
#endif
|
||||
|
||||
#include "wx/socket.h"
|
||||
|
||||
IMPLEMENT_CLASS(wxSocketBase, wxEvtHandler)
|
||||
IMPLEMENT_CLASS(wxSocketClient, wxSocketBase)
|
||||
IMPLEMENT_CLASS(wxSocketServer, wxSocketBase)
|
||||
IMPLEMENT_CLASS(wxSocketHandler, wxObject)
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent, wxEvent)
|
||||
|
||||
#include "wx/url.h"
|
||||
|
||||
IMPLEMENT_CLASS(wxProtoInfo, wxObject)
|
||||
IMPLEMENT_CLASS(wxURL, wxObject)
|
||||
|
||||
#include "wx/protocol/http.h"
|
||||
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol)
|
||||
IMPLEMENT_PROTOCOL(wxHTTP, "http", "80", TRUE)
|
||||
|
||||
#include "wx/protocol/ftp.h"
|
||||
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxFTP, wxProtocol)
|
||||
IMPLEMENT_PROTOCOL(wxFTP, "ftp", "ftp", TRUE)
|
||||
|
||||
#include "wx/protocol/sckfile.h"
|
||||
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxFileProto, wxProtocol)
|
||||
IMPLEMENT_PROTOCOL(wxFileProto, "file", NULL, FALSE)
|
||||
|
||||
#include "wx/sckipc.h"
|
||||
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxTCPServer, wxServerBase)
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxTCPClient, wxClientBase)
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxTCPConnection, wxConnectionBase)
|
||||
|
||||
#include "wx/statusbr.h"
|
||||
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxStatusBar, wxWindow)
|
||||
|
Reference in New Issue
Block a user