1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
/*
Virtual Observatory Client
Copyright © 2018-9 F.Hroch (hroch@physics.muni.cz)
This file is part of Munipack.
Munipack is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Munipack is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Munipack. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../config.h"
#include "voclient.h"
#include <wx/wx.h>
#include <wx/protocol/http.h>
#include <wx/wfstream.h>
#include <wx/ffile.h>
#include <wx/txtstrm.h>
VOclient::VOclient(): wxHTTP() {}
VOclient::VOclient(const wxString& server): wxHTTP()
{
Connect(server);
}
bool VOclient::Connect(const wxString& server)
{
SetFlags(wxSOCKET_WAITALL);
SetHeader("User-Agent",wxString("Munipack/")+wxString(PACKAGE_VERSION));
wxLogInfo("Connecting VO server http://"+server);
return wxHTTP::Connect(server);
}
bool VOclient::Get(const wxString& path, const wxString& filename)
{
wxLogInfo("Sending query "+path);
wxInputStream *istream = GetInputStream(path);
wxLogInfo("HTTP response: %d",GetResponse());
wxLogInfo("HTTP protocol state: %d",GetError());
wxLogInfo("Content-Type (code):",GetContentType());
wxLogInfo("Content-Type (head):",GetHeader("Content-Type"));
wxLogInfo("Content-Length:",GetHeader("Content-Length"));
if( GetResponse() != 200 )
wxLogError("HTTP response: %d (failed).",GetResponse());
if( istream && GetResponse() == 200 && GetError() == wxPROTO_NOERR ) {
wxLogInfo("Downloading data ("+filename+") ...");
wxTextInputStream text(*istream,"\r\n");
wxFFile file(filename,"w");
wxFFileOutputStream ostream(file);
wxTextOutputStream cout(ostream);
while( ! istream->Eof() && istream->IsOk() ) {
wxString line = text.ReadLine();
if( istream->LastRead() > 0 )
cout.WriteString(line+"\n");
}
}
delete istream;
if( GetError() != wxPROTO_NOERR )
wxLogError("Network error %d",(int) GetError());
return GetResponse() == 200 && GetError() == wxPROTO_NOERR;
}
|