view src/kaigo/horori/searcher/include/net.h @ 59:48e46bfe97fa

kaigo: pre-release eraline.
author pyon@macmini
date Wed, 12 Aug 2020 19:57:58 +0900
parents 4e14902379da
children
line wrap: on
line source

// Filename   : net.h
// Last Change: 2020-05-01 ‹à 08:47:39.
//
#pragma once

#include <wx/sstream.h>
#include <wx/wfstream.h>
#include <wx/zstream.h>
#include <wx/tarstrm.h>
#include <wx/protocol/http.h>

wxString HttpGetText(wxString addr, wxString port, wxString url)
{
	wxHTTP get;
	get.SetFlags(wxSOCKET_WAITALL|wxSOCKET_BLOCK);
	while (!get.Connect(addr, wxAtoi(port)))
		wxSleep(1);

	wxString res;
	wxInputStream *httpStream = get.GetInputStream(url);
	if (get.GetError() == wxPROTO_NOERR) {
		wxStringOutputStream out_stream(&res);
		httpStream->Read(out_stream);
	}

	wxDELETE(httpStream);
	get.Close();

	return res;
};

bool HttpGetFile(wxString addr, wxString port, wxString url, wxString file)
{
	bool ret = false;
	wxHTTP get;
	get.SetFlags(wxSOCKET_WAITALL|wxSOCKET_BLOCK);
	while (!get.Connect(addr, wxAtoi(port)))
		wxSleep(1);

	wxInputStream *httpStream = get.GetInputStream(url);
	if (get.GetError() == wxPROTO_NOERR) {
		wxFileOutputStream out_stream(file);
		httpStream->Read(out_stream);
		ret = true;
	}

	wxDELETE(httpStream);
	get.Close();
	return ret;
};

bool HttpGetTgzFile(wxString addr, wxString port, wxString url, wxString dir)
{
	bool ret = false;
	wxHTTP get;
	get.SetTimeout(30);
	get.SetFlags(wxSOCKET_WAITALL|wxSOCKET_BLOCK);
	while (!get.Connect(addr, wxAtoi(port)))
		wxSleep(1);

	wxInputStream *httpStream = get.GetInputStream(url);
	if (get.GetError() == wxPROTO_NOERR) {
		//int size = httpStream->GetSize();
		wxZlibInputStream zlib_istream(httpStream);

		wxTarEntry* entry;
		wxTarInputStream tar_istream(zlib_istream);
		int i = 1;
		while ((entry = tar_istream.GetNextEntry()) != NULL) {
			//wxString name = entry->GetName();
			wxFileOutputStream file_ostream(wxString::Format(wxT("%s/%d"), dir, i++));
			file_ostream.Write(tar_istream);
			file_ostream.Close();
		}
		ret = true;
		//wxDELETE(httpStream);
	}

	get.Close();
	return ret;
};

bool HttpPostFile(wxString addr, wxString port, wxString url, wxString file)
{
	bool ret = false;

	wxMemoryBuffer buf;
	wxByte b[8192];
	wxFFile f;
	if (!f.Open(file, wxT("rb"))) {
        wxMessageBox(wxT("Cannot open file."));
        return ret;
    }
	for (;;) {
		size_t len = f.Read(b, sizeof(b));
		if ((size_t)wxInvalidOffset == len) {
			return ret;
		}
		if (len == 0) break; // success
		buf.AppendData(b, len);
	}
	f.Close();

	wxHTTP post;
	post.SetTimeout(30);
	post.SetFlags(wxSOCKET_WAITALL|wxSOCKET_BLOCK);
	post.SetPostBuffer(wxT("application/gzip"), buf);

	while (!post.Connect(addr, wxAtoi(port)))
		wxSleep(1);

	wxInputStream *httpStream = post.GetInputStream(url);
	if (httpStream != NULL) {
		wxString res;
		wxStringOutputStream out_stream(&res);
		httpStream->Read(out_stream);
		wxDELETE(httpStream);
		//wxMessageBox(res);
		ret = true;
	}

	post.Close();
	return ret;
};