Patch level :
Files correlati : Ricompilazione Demo : [ ] Commento : Aggiunta gestione protocollo HTTP tramite le chiamate http_get e http_get_dir git-svn-id: svn://10.65.10.50/trunk@6971 c028cbd2-c16b-5b4b-a496-9718f37d4682
This commit is contained in:
parent
4b6b88e9dd
commit
d2856aefe3
@ -23,7 +23,7 @@ bool rpc_Start()
|
||||
{
|
||||
delete _client;
|
||||
_client = NULL;
|
||||
ok = error_box("Errore di inizializzazione del client.");
|
||||
ok = error_box("Errore di inizializzazione del socket client");
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
@ -287,3 +287,40 @@ bool rpc_UserLogout()
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
bool http_get(const char* server,
|
||||
const char* remote_file,
|
||||
const char* local_file)
|
||||
{
|
||||
TSocketClient client;
|
||||
if (!client.IsOk())
|
||||
return error_box("Impossibile inizializzare il client HTTP");
|
||||
|
||||
unsigned long connection = client.QueryConnection("80", server);
|
||||
bool ok = connection != 0;
|
||||
if (ok)
|
||||
{
|
||||
ok = client.HttpGetFile(remote_file, local_file) != 0;
|
||||
client.RemoveConnection(connection);
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool http_dir(const char* server, const char* remote_dir, TString_array& list)
|
||||
{
|
||||
TSocketClient client;
|
||||
if (!client.IsOk())
|
||||
return error_box("Impossibile inizializzare il client HTTP");
|
||||
|
||||
unsigned long connection = client.QueryConnection("80", server);
|
||||
bool ok = connection != 0;
|
||||
if (ok)
|
||||
{
|
||||
ok = client.HttpGetDir(remote_dir, list) != 0;
|
||||
client.RemoveConnection(connection);
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
@ -21,8 +21,10 @@ bool rpc_UserLogin(const char* server, const char* user,
|
||||
const char* password, const char* application);
|
||||
bool rpc_UserLogout();
|
||||
|
||||
|
||||
bool rpc_Start();
|
||||
bool rpc_Stop();
|
||||
|
||||
bool http_get(const char* server, const char* remote_file, const char* local_file);
|
||||
bool http_dir(const char* server, const char* remote_dir, TString_array& list);
|
||||
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
#define XVT_INCL_NATIVE
|
||||
#include <strings.h>
|
||||
#include "progind.h"
|
||||
|
||||
// skstream.h
|
||||
// Copyright (C) 1995, 1996 by John C. Wang. All Rights Reserved.
|
||||
@ -18,9 +18,7 @@
|
||||
// [JCW 95-Dec-20] comments added for distribution 95a
|
||||
// [JCW 96-Jan-01] removed UDP capabilities from skstream
|
||||
|
||||
#include <iostream.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <fstream.h>
|
||||
#include "winsock.h"
|
||||
|
||||
//
|
||||
@ -517,6 +515,8 @@ TConnection* TSocketClient::OnQueryConnection(const char* service,
|
||||
if (cur_socket == NULL)
|
||||
{
|
||||
TString strServer = server;
|
||||
if (strServer.compare("http://", 7, TRUE) == 0)
|
||||
strServer.ltrim(7);
|
||||
if (strServer.empty() || stricmp(strServer, "localhost") == 0)
|
||||
strServer = "127.0.0.1";
|
||||
|
||||
@ -592,3 +592,121 @@ void TSocketClient::ReleaseBuffer()
|
||||
m_dwSize = 0;
|
||||
}
|
||||
}
|
||||
|
||||
BOOL TSocketClient::HttpGetFile(const char* remote, const char* local)
|
||||
{
|
||||
if (!cur_socket->is_open())
|
||||
return FALSE;
|
||||
|
||||
BOOL ok = FALSE;
|
||||
|
||||
TString buf(4096);
|
||||
buf << "GET " << remote << " HTTP/1.0\r\n\r\n";
|
||||
|
||||
cur_socket->sync();
|
||||
cur_socket->write(buf, buf.len());
|
||||
cur_socket->flush();
|
||||
if (cur_socket->good())
|
||||
{
|
||||
const clock_t start = clock();
|
||||
|
||||
long size = 0;
|
||||
for (int r = 0; !cur_socket->eof(); r++)
|
||||
{
|
||||
cur_socket->getline(buf.get_buffer(), buf.size(), '\n');
|
||||
if (buf.blank())
|
||||
break;
|
||||
if (r == 0 && buf.find("404") > 0)
|
||||
return FALSE; // File not found
|
||||
if (buf.compare("Content-length:", 15, TRUE) == 0)
|
||||
{
|
||||
const int colon = buf.find(':');
|
||||
size = atol(buf.mid(colon+1));
|
||||
}
|
||||
}
|
||||
|
||||
ofstream outfile(local, ios::out | ios::binary);
|
||||
if (outfile.good())
|
||||
{
|
||||
TString msg;
|
||||
msg << remote << " - ";
|
||||
if (size > 1024)
|
||||
msg << (size / 1024) << " K";
|
||||
else
|
||||
msg << size << ' ';
|
||||
msg << "bytes.\nTempo residuo stimato: ";
|
||||
TProgind pi(size, msg, TRUE, TRUE);
|
||||
long total = 0;
|
||||
|
||||
while (!cur_socket->eof())
|
||||
{
|
||||
cur_socket->read(buf.get_buffer(), buf.size());
|
||||
const int count = cur_socket->gcount();
|
||||
if (count > 0)
|
||||
{
|
||||
outfile.write(buf, count);
|
||||
|
||||
total += count;
|
||||
bool tick = pi.setstatus(total);
|
||||
if (pi.iscancelled())
|
||||
break;
|
||||
|
||||
if (tick)
|
||||
{
|
||||
const double estimated_ticks = double(size - total) * double(clock() - start) / total;
|
||||
long secs = long(estimated_ticks / CLOCKS_PER_SEC) + 1;
|
||||
CHECK(secs >= 0, "Bad time estimation");
|
||||
const int hours = int(secs / 3600L);
|
||||
secs %= 3600L;
|
||||
const int mins = int(secs / 60L);
|
||||
secs %= 60L;
|
||||
const int append_pos = msg.find("o: ")+3;
|
||||
msg.cut(append_pos);
|
||||
TString16 tempo;
|
||||
tempo.format("%02d:%02d:%02ld", hours, mins, secs);
|
||||
msg << tempo;
|
||||
pi.set_text(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
ok = pi.isfinished();
|
||||
}
|
||||
else
|
||||
error_box("Impossibile scrivere il file %s", local);
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
BOOL TSocketClient::HttpGetDir(const char* remote, TString_array& list)
|
||||
{
|
||||
TFilename local; local.temp();
|
||||
const BOOL ok = HttpGetFile(remote, local);
|
||||
if (ok)
|
||||
{
|
||||
ifstream s(local);
|
||||
TString riga(1024);
|
||||
while (!s.eof())
|
||||
{
|
||||
s.getline(riga.get_buffer(), riga.size());
|
||||
const int href = riga.find("HREF=");
|
||||
if (href > 0)
|
||||
{
|
||||
const int start = riga.find('"', href) + 1;
|
||||
if (start > href)
|
||||
{
|
||||
const int stop = riga.find('"', start);
|
||||
if (stop > start)
|
||||
{
|
||||
const TString& name = riga.sub(start, stop);
|
||||
list.add(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
s.close();
|
||||
::remove(local);
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
@ -53,6 +53,9 @@ public:
|
||||
virtual void ReleaseBuffer();
|
||||
virtual BOOL IsOk() const { return TRUE; }
|
||||
|
||||
BOOL HttpGetFile(const char* remote, const char* local);
|
||||
BOOL HttpGetDir(const char* remote, TString_array& list);
|
||||
|
||||
TSocketClient();
|
||||
virtual ~TSocketClient();
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user